@Override public SortedSet<Edge> getTopologyEdges() { return _topology.getEdges(); } }
@VisibleForTesting static Multiset<Row> getLayer3Edges( Map<String, Configuration> configurations, Set<String> includeNodes, Set<String> includeRemoteNodes, Topology topology) { return topology.getEdges().stream() .filter( layer3Edge -> includeNodes.contains(layer3Edge.getNode1()) && includeRemoteNodes.contains(layer3Edge.getNode2())) .map(layer3edge -> layer3EdgeToRow(configurations, layer3edge)) .collect(Collectors.toCollection(HashMultiset::create)); }
@Override public void describeTo(Description description) { description.appendText( String.format("is neighbor of %s in provided topology: %s", _node, _topology.getEdges())); }
private static SortedSet<VerboseEdge> getLayer3Edges( Map<String, Configuration> configs, Topology topology) { SortedSet<VerboseEdge> layer3Edges = new TreeSet<>(); for (Edge edge : topology.getEdges()) { Configuration n1 = configs.get(edge.getNode1()); Interface i1 = n1.getAllInterfaces().get(edge.getInt1()); Configuration n2 = configs.get(edge.getNode2()); Interface i2 = n2.getAllInterfaces().get(edge.getInt2()); layer3Edges.add(new VerboseEdge(i1, i2, edge)); } return layer3Edges; }
/** * Compute the layer 3 topology from the layer-2 topology and layer-3 information contained in the * configurations. */ public static @Nonnull Topology computeLayer3Topology( @Nonnull Layer2Topology layer2Topology, @Nonnull Map<String, Configuration> configurations) { return new Topology( synthesizeL3Topology(configurations).getEdges().stream() .filter(edge -> layer2Topology.inSameBroadcastDomain(edge.getHead(), edge.getTail())) .collect(ImmutableSortedSet.toImmutableSortedSet(Comparator.naturalOrder()))); }
@Override protected boolean matchesSafely(NodeInterfacePair item, Description mismatchDescription) { Set<NodeInterfacePair> neighbors = _topology.getNeighbors(item); if (neighbors == null || neighbors.isEmpty()) { mismatchDescription.appendText( String.format( "%s has no neighbors in provided topology: %s", item, _topology.getEdges())); return false; } if (neighbors.stream().noneMatch(neighbor -> neighbor.getHostname().equals(_node))) { mismatchDescription.appendText( String.format("%s was not among the neighbors of %s: %s", _node, item, neighbors)); return false; } return true; } }
@Test public void testPruneInterface2() { Topology topo = new Topology(_bothEdges); topo.prune(null, null, ImmutableSet.of(_nip2)); assertThat(topo.getEdges(), equalTo(ImmutableSet.of())); assertThat(topo.getInterfaceEdges(), equalTo(ImmutableMap.of())); assertThat(topo.getNodeEdges(), equalTo(ImmutableMap.of())); assertThat(topo.getNeighbors(_nip1), equalTo(ImmutableSet.of())); assertThat(topo.getNeighbors(_nip3), equalTo(ImmutableSet.of())); }
@Test public void testPruneNode2() { Topology topo = new Topology(_bothEdges); topo.prune(null, ImmutableSet.of("n2"), null); assertThat(topo.getEdges(), equalTo(ImmutableSet.of())); assertThat(topo.getInterfaceEdges(), equalTo(ImmutableMap.of())); assertThat(topo.getNodeEdges(), equalTo(ImmutableMap.of())); assertThat(topo.getNeighbors(_nip1), equalTo(ImmutableSet.of())); assertThat(topo.getNeighbors(_nip3), equalTo(ImmutableSet.of())); }
@Test public void testPruneInterface1() { Topology topo = new Topology(_bothEdges); topo.prune(null, null, ImmutableSet.of(_nip1)); assertThat(topo.getEdges(), equalTo(_edge2to3Set)); assertThat( topo.getInterfaceEdges(), equalTo(ImmutableMap.of(_nip2, _edge2to3Set, _nip3, _edge2to3Set))); assertThat( topo.getNodeEdges(), equalTo(ImmutableMap.of("n2", _edge2to3Set, "n3", _edge2to3Set))); assertThat(topo.getNeighbors(_nip2), equalTo(ImmutableSet.of(_nip3))); assertThat(topo.getNeighbors(_nip3), equalTo(ImmutableSet.of())); }
@Test public void testPruneNode3() { Topology topo = new Topology(_bothEdges); topo.prune(null, ImmutableSet.of("n3"), null); assertThat(topo.getEdges(), equalTo(_edge1to2Set)); assertThat( topo.getInterfaceEdges(), equalTo(ImmutableMap.of(_nip1, _edge1to2Set, _nip2, _edge1to2Set))); assertThat( topo.getNodeEdges(), equalTo(ImmutableMap.of("n1", _edge1to2Set, "n2", _edge1to2Set))); assertThat(topo.getNeighbors(_nip1), equalTo(ImmutableSet.of(_nip2))); assertThat(topo.getNeighbors(_nip2), equalTo(ImmutableSet.of())); }
@Test public void testPruneNode1() { Topology topo = new Topology(_bothEdges); topo.prune(null, ImmutableSet.of("n1"), null); assertThat(topo.getEdges(), equalTo(_edge2to3Set)); assertThat( topo.getInterfaceEdges(), equalTo(ImmutableMap.of(_nip2, _edge2to3Set, _nip3, _edge2to3Set))); assertThat( topo.getNodeEdges(), equalTo(ImmutableMap.of("n2", _edge2to3Set, "n3", _edge2to3Set))); assertThat(topo.getNeighbors(_nip2), equalTo(ImmutableSet.of(_nip3))); assertThat(topo.getNeighbors(_nip3), equalTo(ImmutableSet.of())); }
@Test public void testEmptyTopology() { SortedSet<Edge> edges = ImmutableSortedSet.of(); Topology topo = new Topology(edges); assertThat(topo.getEdges(), equalTo(ImmutableSet.of())); assertThat(topo.getInterfaceEdges(), equalTo(ImmutableMap.of())); assertThat(topo.getNodeEdges(), equalTo(ImmutableMap.of())); assertThat( topo.getNeighbors(new NodeInterfacePair("node", "iface")), equalTo(ImmutableSet.of())); }
@Test public void testPruneInterface3() { Topology topo = new Topology(_bothEdges); topo.prune(null, null, ImmutableSet.of(_nip3)); assertThat(topo.getEdges(), equalTo(_edge1to2Set)); assertThat( topo.getInterfaceEdges(), equalTo(ImmutableMap.of(_nip1, _edge1to2Set, _nip2, _edge1to2Set))); assertThat( topo.getNodeEdges(), equalTo(ImmutableMap.of("n1", _edge1to2Set, "n2", _edge1to2Set))); assertThat(topo.getNeighbors(_nip1), equalTo(ImmutableSet.of(_nip2))); assertThat(topo.getNeighbors(_nip2), equalTo(ImmutableSet.of())); } }
@Test public void testTwoEdges() { Topology topo = new Topology(_bothEdges); assertThat(topo.getEdges(), equalTo(_bothEdges)); assertThat( topo.getInterfaceEdges(), equalTo(ImmutableMap.of(_nip1, _edge1to2Set, _nip2, _bothEdges, _nip3, _edge2to3Set))); assertThat( topo.getNodeEdges(), equalTo(ImmutableMap.of("n1", _edge1to2Set, "n2", _bothEdges, "n3", _edge2to3Set))); assertThat(topo.getNeighbors(_nip1), equalTo(ImmutableSet.of(_nip2))); assertThat(topo.getNeighbors(_nip2), equalTo(ImmutableSet.of(_nip3))); assertThat(topo.getNeighbors(_nip3), equalTo(ImmutableSet.of())); }
@Test public void testPruneEdge() { Topology topo = new Topology(_bothEdges); topo.prune(_edge1to2Set, null, null); assertThat(topo.getEdges(), equalTo(_edge2to3Set)); assertThat( topo.getInterfaceEdges(), equalTo(ImmutableMap.of(_nip2, _edge2to3Set, _nip3, _edge2to3Set))); assertThat( topo.getNodeEdges(), equalTo(ImmutableMap.of("n2", _edge2to3Set, "n3", _edge2to3Set))); assertThat(topo.getNeighbors(_nip1), equalTo(ImmutableSet.of())); assertThat(topo.getNeighbors(_nip2), equalTo(ImmutableSet.of(_nip3))); assertThat(topo.getNeighbors(_nip3), equalTo(ImmutableSet.of())); }
@Test public void testOneEdge() { NodeInterfacePair nip1 = new NodeInterfacePair("n1", "i1"); NodeInterfacePair nip2 = new NodeInterfacePair("n2", "i2"); Edge edge = new Edge(nip1, nip2); SortedSet<Edge> edges = ImmutableSortedSet.of(edge); Topology topo = new Topology(edges); assertThat(topo.getEdges(), equalTo(edges)); assertThat(topo.getInterfaceEdges(), equalTo(ImmutableMap.of(nip1, edges, nip2, edges))); assertThat(topo.getNodeEdges(), equalTo(ImmutableMap.of("n1", edges, "n2", edges))); assertThat(topo.getNeighbors(nip1), equalTo(ImmutableSet.of(nip2))); assertThat(topo.getNeighbors(nip2), equalTo(ImmutableSet.of())); assertThat(topo.getNeighbors(new NodeInterfacePair("x", "y")), equalTo(ImmutableSet.of())); }
@Test public void testSynthesizeTopology_asymmetricPartialOverlap() { NetworkFactory nf = new NetworkFactory(); Configuration.Builder cb = nf.configurationBuilder().setConfigurationFormat(ConfigurationFormat.CISCO_IOS); Configuration c1 = cb.build(); Configuration c2 = cb.build(); nf.interfaceBuilder().setOwner(c1).setAddresses(new InterfaceAddress("1.2.3.4/24")).build(); nf.interfaceBuilder().setOwner(c2).setAddresses(new InterfaceAddress("1.2.3.17/28")).build(); Topology t = TopologyUtil.synthesizeL3Topology( ImmutableMap.of(c1.getHostname(), c1, c2.getHostname(), c2)); assertThat(t.getEdges(), empty()); }
@Test public void testSynthesizeTopology_asymmetric() { NetworkFactory nf = new NetworkFactory(); Configuration.Builder cb = nf.configurationBuilder().setConfigurationFormat(ConfigurationFormat.CISCO_IOS); Configuration c1 = cb.build(); Configuration c2 = cb.build(); Interface i1 = nf.interfaceBuilder().setOwner(c1).setAddresses(new InterfaceAddress("1.2.3.4/24")).build(); Interface i2 = nf.interfaceBuilder().setOwner(c2).setAddresses(new InterfaceAddress("1.2.3.5/28")).build(); Topology t = TopologyUtil.synthesizeL3Topology( ImmutableMap.of(c1.getHostname(), c1, c2.getHostname(), c2)); assertThat(t.getEdges(), equalTo(ImmutableSet.of(new Edge(i1, i2), new Edge(i2, i1)))); }
@Test public void testSynthesizeTopology_asymmetricSharedIp() { NetworkFactory nf = new NetworkFactory(); Configuration.Builder cb = nf.configurationBuilder().setConfigurationFormat(ConfigurationFormat.CISCO_IOS); Configuration c1 = cb.build(); Configuration c2 = cb.build(); nf.interfaceBuilder().setOwner(c1).setAddresses(new InterfaceAddress("1.2.3.4/24")).build(); nf.interfaceBuilder().setOwner(c2).setAddresses(new InterfaceAddress("1.2.3.4/28")).build(); Topology t = TopologyUtil.synthesizeL3Topology( ImmutableMap.of(c1.getHostname(), c1, c2.getHostname(), c2)); assertThat(t.getEdges(), empty()); } }
@Test public void testSynthesizeTopology_selfEdges() { NetworkFactory nf = new NetworkFactory(); Configuration c = nf.configurationBuilder().setConfigurationFormat(ConfigurationFormat.CISCO_IOS).build(); Vrf v1 = nf.vrfBuilder().setOwner(c).setName("v1").build(); Vrf v2 = nf.vrfBuilder().setOwner(c).setName("v2").build(); Interface.Builder builder = nf.interfaceBuilder().setOwner(c); Interface i1 = builder.setAddresses(new InterfaceAddress("1.2.3.4/24")).setVrf(v1).build(); Interface i2 = builder.setAddresses(new InterfaceAddress("1.2.3.5/24")).setVrf(v1).build(); Interface i3 = builder.setAddresses(new InterfaceAddress("1.2.3.6/24")).setVrf(v2).build(); Topology t = TopologyUtil.synthesizeL3Topology(ImmutableMap.of(c.getHostname(), c)); assertThat( t.getEdges(), equalTo( ImmutableSet.of( new Edge(i1, i3), new Edge(i3, i1), new Edge(i2, i3), new Edge(i3, i2)))); }