@VisibleForTesting Map<Edge, Set<AbstractRoute>> computeRoutesWithDestIpEdge(Topology topology) { try (ActiveSpan span = GlobalTracer.get() .buildSpan("ForwardingAnalysisImpl.computeRoutesWithDestIpEdge") .startActive()) { assert span != null; // avoid unused warning ImmutableMap.Builder<Edge, Set<AbstractRoute>> routesByEdgeBuilder = ImmutableMap.builder(); _routesWhereDstIpCanBeArpIp.forEach( (hostname, routesWhereDstIpCanBeArpIpByVrf) -> routesWhereDstIpCanBeArpIpByVrf.forEach( (vrf, routesWhereDstIpCanBeArpIpByOutInterface) -> routesWhereDstIpCanBeArpIpByOutInterface.forEach( (outInterface, routes) -> { NodeInterfacePair out = new NodeInterfacePair(hostname, outInterface); Set<NodeInterfacePair> receivers = topology.getNeighbors(out); receivers.forEach( receiver -> routesByEdgeBuilder.put(new Edge(out, receiver), routes)); }))); return routesByEdgeBuilder.build(); } }
Fib fib = fibs.get(hostname).get(vrf); NodeInterfacePair out = new NodeInterfacePair(hostname, outInterface); Set<NodeInterfacePair> receivers = topology.getNeighbors(out); receivers.forEach( receiver -> {
@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 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 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 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 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 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 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())); }