def build_slices(topo, policy): predicates1 = util.build_external_predicate(topo, nc.Header({'dstport': 80})) predicates2 = util.build_external_predicate(topo, nc.Header({'dstport': 22})) slice1 = slicing.ident_map_slice(topo, predicates1) slice2 = slicing.ident_map_slice(topo, predicates2) return [(slice1, policy), (slice2, policy)]
def linear_hosts(*linear_nodes): """Returns topo, [(slice, policy)]""" topo = nxtopo.from_graph(nx.path_graph(4)) nodes = [set(ns) for ns in linear_nodes] for n in topo.nodes(): h1 = 100 + n*10 + 1 h2 = 100 + n*10 + 2 topo.add_host(h1) topo.add_host(h2) topo.add_link(n, h1) topo.add_link(n, h2) for ns in nodes: if n in ns: ns.update([h1, h2]) topo.finalize() edge_policies = [] for ns in linear_nodes: predicates = {} for n in ns: h1 = 100 + n*10 + 1 h2 = 100 + n*10 + 2 h1_port = topo.node[h1]['port'][0] h2_port = topo.node[h2]['port'][0] predicates[h1_port] = nc.Top() predicates[h2_port] = nc.Top() edge_policies.append(predicates) topos = [topo.subgraph(ns) for ns in nodes] slices = [slicing.ident_map_slice(t, pol) for t, pol in zip(topos, edge_policies)] combined = zip(slices, [policy_gen.flood_observe(t) for t in topos]) return (topo, combined)
def testBasicCompile(self): topo, policies = linear((0, 1, 2, 3), (0, 1, 2, 3)) slices = [slicing.ident_map_slice(topo, {}) for p in policies] combined = zip(slices, policies) compiled = ec.compile_slices(topo, combined) self.assertIsNotNone(sat.shared_io(topo, policies[0], policies[1])) self.assertIsNone(sat.shared_io(topo, compiled[0], compiled[1])) self.assertTrue(sat.compiled_correctly(topo, policies[0], compiled[0])) self.assertTrue(sat.compiled_correctly(topo, policies[1], compiled[1])) topo, policies = linear((0, 1, 2), (2, 3)) slices = [slicing.ident_map_slice(topo, {}) for p in policies] combined = zip(slices, policies) compiled = ec.compile_slices(topo, combined) self.assertIsNone(sat.shared_io(topo, policies[0], policies[1])) self.assertIsNone(sat.shared_io(topo, compiled[0], compiled[1])) self.assertTrue(sat.compiled_correctly(topo, policies[0], compiled[0])) self.assertTrue(sat.compiled_correctly(topo, policies[1], compiled[1])) topo, policies = linear((0, 1, 2), (1, 2, 3)) slices = [slicing.ident_map_slice(topo, {}) for p in policies] combined = zip(slices, policies) compiled = ec.compile_slices(topo, combined) self.assertIsNotNone(sat.shared_io(topo, policies[0], policies[1])) self.assertIsNone(sat.shared_io(topo, compiled[0], compiled[1])) self.assertTrue(sat.compiled_correctly(topo, policies[0], compiled[0])) self.assertTrue(sat.compiled_correctly(topo, policies[1], compiled[1])) topo, policies = linear((0, 1), (2, 3)) slices = [slicing.ident_map_slice(topo, {}) for p in policies] combined = zip(slices, policies) compiled = ec.compile_slices(topo, combined) self.assertIsNone(sat.shared_io(topo, policies[0], policies[1])) self.assertIsNone(sat.shared_io(topo, compiled[0], compiled[1])) self.assertTrue(sat.compiled_correctly(topo, policies[0], compiled[0])) self.assertTrue(sat.compiled_correctly(topo, policies[1], compiled[1])) topo, policies = linear_all_ports((0, 1), (2, 3)) slices = [slicing.ident_map_slice(topo, {}) for p in policies] combined = zip(slices, policies) compiled = ec.compile_slices(topo, combined) self.assertIsNone(sat.shared_io(topo, policies[0], policies[1])) self.assertIsNone(sat.shared_io(topo, compiled[0], compiled[1])) self.assertTrue(sat.compiled_correctly(topo, policies[0], compiled[0])) self.assertTrue(sat.compiled_correctly(topo, policies[1], compiled[1]))
def k4(): """K4 complete graph.""" topo = nxtopo.from_graph(nx.complete_graph(4)) topo.finalize() topos = [topo.subgraph(nodes) for nodes in k4_nodes] edge_policies = [{} for t in topos] slices = [slicing.ident_map_slice(t, pol) for t, pol in zip(topos, edge_policies)] combined = zip(slices, [policy_gen.flood_observe(t) for t in topos]) return (topo, combined)
def testBasic(self): topo, policies = linear((0, 1, 2, 3), (0, 1, 2, 3)) slices = [slicing.ident_map_slice(topo, {}) for p in policies] combined = zip(slices, policies) compiled = cp.compile_slices(combined) self.assertTrue(sat.separate(topo, compiled[0], compiled[1]))