@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())); }
/** * 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; } }
@JsonCreator private static Topology jacksonCreateTopology(SortedSet<Edge> edges) { return new Topology(firstNonNull(edges, ImmutableSortedSet.of())); }
@Override public SortedSet<Edge> getTopologyEdges() { return _topology.getEdges(); } }
public Set<NodeInterfacePair> getNeighbors(NodeInterfacePair iface) { return getInterfaceEdges().getOrDefault(iface, ImmutableSortedSet.of()).stream() .filter(e -> e.getTail().equals(iface)) .map(Edge::getHead) .collect(ImmutableSet.toImmutableSet()); }
@Test public void testPruneFailedIpsecSessionEdges() { _topology.pruneFailedIpsecSessionEdges(_ipsecTopology, _configurations); // Edges involving host1:Tunnel3, host2:Tunnel4, host1:Tunnel7 will be pruned assertThat( _topology.getEdges(), equalTo( ImmutableSet.of( new Edge( new NodeInterfacePair("host1", "Tunnel1"), new NodeInterfacePair("host2", "Tunnel2")), new Edge( new NodeInterfacePair("host2", "Tunnel2"), new NodeInterfacePair("host1", "Tunnel1")), new Edge( new NodeInterfacePair("host1", "interface5"), new NodeInterfacePair("host2", "interface6")), new Edge( new NodeInterfacePair("host1", "Tunnel9"), new NodeInterfacePair("host2", "Tunnel10"))))); } }
private Topology computeLayer3Topology(NetworkSnapshot networkSnapshot) { try (ActiveSpan span = GlobalTracer.get().buildSpan("TopologyProviderImpl::computeLayer3Topology").startActive()) { assert span != null; // avoid unused warning Map<String, Configuration> configurations = _batfish.loadConfigurations(networkSnapshot); Topology topology = getRawLayer3Topology(networkSnapshot); topology.prune( _batfish.getEdgeBlacklist(networkSnapshot), _batfish.getNodeBlacklist(networkSnapshot), _batfish.getInterfaceBlacklist(networkSnapshot)); topology.pruneFailedIpsecSessionEdges( IpsecUtil.initIpsecTopology(configurations), configurations); return topology; } }
SortedSet<Edge> edges = topology.getNodeEdges().get(node); if (edges == null) {
_configurations.remove(router); topology.prune(null, toRemove, null);
@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(); } }
@Override public Topology getEnvironmentTopology() { return new Topology(ImmutableSortedSet.of()); }
@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)); }
Map<NodeInterfacePair, SortedSet<Edge>> ifaceEdges = topology.getInterfaceEdges();
SortedSet<Edge> edges = topology.getNodeEdges().get(node); if (edges == null) {
prune(failedIpsecEdges, ImmutableSet.of(), ImmutableSet.of());
Fib fib = fibs.get(hostname).get(vrf); NodeInterfacePair out = new NodeInterfacePair(hostname, outInterface); Set<NodeInterfacePair> receivers = topology.getNeighbors(out); receivers.forEach( receiver -> {
@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())); }
@Override public Topology getEnvironmentTopology() { return new Topology(ImmutableSortedSet.of()); }
@Override public void describeTo(Description description) { description.appendText( String.format("is neighbor of %s in provided topology: %s", _node, _topology.getEdges())); }