@Test public void testComputeLayer2SelfEdges() { String c1Name = "c1"; String i1Name = "i1"; String i2Name = "i2"; Configuration c1 = _cb.setHostname(c1Name).build(); Vrf v1 = _vb.setOwner(c1).build(); _ib.setOwner(c1).setVrf(v1).setActive(true); Interface i1 = _ib.setName(i1Name).build(); i1.setSwitchport(true); i1.setSwitchportMode(SwitchportMode.ACCESS); i1.setAccessVlan(2); Vrf v2 = _vb.setOwner(c1).build(); Interface i2 = _ib.setVrf(v2).setName(i2Name).build(); i2.setSwitchport(true); i2.setSwitchportMode(SwitchportMode.ACCESS); i2.setAccessVlan(2); ImmutableSet.Builder<Layer2Edge> builder = ImmutableSet.builder(); computeLayer2SelfEdges(c1, builder::add); assertThat( builder.build(), equalTo(ImmutableSet.of(new Layer2Edge(c1Name, i1Name, 2, c1Name, i2Name, 2, null)))); }
@VisibleForTesting static void computeLayer2SelfEdges( @Nonnull Configuration config, @Nonnull Consumer<Layer2Edge> edges) { String hostname = config.getHostname(); Map<Integer, ImmutableList.Builder<String>> switchportsByVlanBuilder = new HashMap<>(); config.getAllInterfaces().values().stream() .filter(Interface::getActive) .forEach( i -> { if (i.getSwitchportMode() == SwitchportMode.TRUNK) { i.getAllowedVlans().stream() .forEach( vlan -> switchportsByVlanBuilder .computeIfAbsent(vlan, n -> ImmutableList.builder()) .add(i.getName())); } else if (i.getSwitchportMode() == SwitchportMode.ACCESS && i.getAccessVlan() != null) { switchportsByVlanBuilder .computeIfAbsent(i.getAccessVlan(), n -> ImmutableList.builder()) .add(i.getName()); } }); Map<Integer, List<String>> switchportsByVlan = CollectionUtil.toImmutableMap( switchportsByVlanBuilder, Entry::getKey, e -> e.getValue().build()); switchportsByVlan.forEach( (vlanId, interfaceNames) -> { String firstInterface = interfaceNames.get(0); Layer2Node firstNode = new Layer2Node(hostname, firstInterface, vlanId); for (String otherInterface : interfaceNames.subList(1, interfaceNames.size())) { Layer2Node otherNode = new Layer2Node(hostname, otherInterface, vlanId); edges.accept(new Layer2Edge(firstNode, otherNode, null)); } }); Map<Integer, Layer2Vni> vniSettingsByVlan = config.getVrfs().values().stream() .flatMap(vrf -> vrf.getLayer2Vnis().values().stream()) .collect(ImmutableMap.toImmutableMap(Layer2Vni::getVlan, Function.identity())); config.getAllInterfaces().values().stream() .filter(Interface::getActive) .filter(i -> i.getInterfaceType() == InterfaceType.VLAN && i.getVlan() != null) .forEach( irbInterface -> { String irbName = irbInterface.getName(); int vlanId = irbInterface.getVlan(); computeSelfSwitchportNonSwitchportEdges(switchportsByVlan, hostname, irbName, vlanId) .forEach(edges::accept); Optional.ofNullable(vniSettingsByVlan.get(vlanId)) .map(vniSettings -> computeVniName(vniSettings.getVni())) .ifPresent( vniName -> edges.accept( new Layer2Edge( hostname, irbName, null, hostname, vniName, null, null))); }); vniSettingsByVlan.forEach( (vlanId, vniSettings) -> { String vniName = computeVniName(vniSettings.getVni()); computeSelfSwitchportNonSwitchportEdges(switchportsByVlan, hostname, vniName, vlanId) .forEach(edges::accept); }); }
TopologyUtil { @VisibleForTesting static void computeLayer2SelfEdges( @Nonnull Configuration config, @Nonnull Consumer<Layer2Edge> edges) { String hostname = config.getHostname(); Map<Integer, ImmutableList.Builder<String>> switchportsByVlanBuilder = new HashMap<>(); config.getAllInterfaces().values().stream() .filter(Interface::getActive) .forEach( i -> { if (i.getSwitchportMode() == SwitchportMode.TRUNK) { i.getAllowedVlans().stream() .forEach( vlan -> switchportsByVlanBuilder .computeIfAbsent(vlan, n -> ImmutableList.builder()) .add(i.getName())); } else if (i.getSwitchportMode() == SwitchportMode.ACCESS && i.getAccessVlan() != null) { switchportsByVlanBuilder .computeIfAbsent(i.getAccessVlan(), n -> ImmutableList.builder()) .add(i.getName()); } }); Map<Integer, List<String>> switchportsByVlan = CollectionUtil.toImmutableMap( switchportsByVlanBuilder, Entry::getKey, e -> e.getValue().build()); switchportsByVlan.forEach( (vlanId, interfaceNames) -> { String firstInterface = interfaceNames.get(0); Layer2Node firstNode = new Layer2Node(hostname, firstInterface, vlanId); for (String otherInterface : interfaceNames.subList(1, interfaceNames.size())) { Layer2Node otherNode = new Layer2Node(hostname, otherInterface, vlanId); edges.accept(new Layer2Edge(firstNode, otherNode, null)); } }); Map<Integer, Layer2Vni> vniSettingsByVlan = config.getVrfs().values().stream() .flatMap(vrf -> vrf.getLayer2Vnis().values().stream()) .collect(ImmutableMap.toImmutableMap(Layer2Vni::getVlan, Function.identity())); config.getAllInterfaces().values().stream() .filter(Interface::getActive) .filter(i -> i.getInterfaceType() == InterfaceType.VLAN && i.getVlan() != null) .forEach( irbInterface -> { String irbName = irbInterface.getName(); int vlanId = irbInterface.getVlan(); computeSelfSwitchportNonSwitchportEdges(switchportsByVlan, hostname, irbName, vlanId) .forEach(edges::accept); Optional.ofNullable(vniSettingsByVlan.get(vlanId)) .map(vniSettings -> computeVniName(vniSettings.getVni())) .ifPresent( vniName -> edges.accept( new Layer2Edge( hostname, irbName, null, hostname, vniName, null, null))); }); vniSettingsByVlan.forEach( (vlanId, vniSettings) -> { String vniName = computeVniName(vniSettings.getVni()); computeSelfSwitchportNonSwitchportEdges(switchportsByVlan, hostname, vniName, vlanId) .forEach(edges::accept); }); } }
TopologyUtil { @VisibleForTesting static void computeLayer2SelfEdges( @Nonnull Configuration config, @Nonnull Consumer<Layer2Edge> edges) { String hostname = config.getHostname(); Map<Integer, ImmutableList.Builder<String>> switchportsByVlanBuilder = new HashMap<>(); config.getAllInterfaces().values().stream() .filter(Interface::getActive) .forEach( i -> { if (i.getSwitchportMode() == SwitchportMode.TRUNK) { i.getAllowedVlans().stream() .forEach( vlan -> switchportsByVlanBuilder .computeIfAbsent(vlan, n -> ImmutableList.builder()) .add(i.getName())); } else if (i.getSwitchportMode() == SwitchportMode.ACCESS && i.getAccessVlan() != null) { switchportsByVlanBuilder .computeIfAbsent(i.getAccessVlan(), n -> ImmutableList.builder()) .add(i.getName()); } }); Map<Integer, List<String>> switchportsByVlan = CollectionUtil.toImmutableMap( switchportsByVlanBuilder, Entry::getKey, e -> e.getValue().build()); switchportsByVlan.forEach( (vlanId, interfaceNames) -> { String firstInterface = interfaceNames.get(0); Layer2Node firstNode = new Layer2Node(hostname, firstInterface, vlanId); for (String otherInterface : interfaceNames.subList(1, interfaceNames.size())) { Layer2Node otherNode = new Layer2Node(hostname, otherInterface, vlanId); edges.accept(new Layer2Edge(firstNode, otherNode, null)); } }); Map<Integer, Layer2Vni> vniSettingsByVlan = config.getVrfs().values().stream() .flatMap(vrf -> vrf.getLayer2Vnis().values().stream()) .collect(ImmutableMap.toImmutableMap(Layer2Vni::getVlan, Function.identity())); config.getAllInterfaces().values().stream() .filter(Interface::getActive) .filter(i -> i.getInterfaceType() == InterfaceType.VLAN && i.getVlan() != null) .forEach( irbInterface -> { String irbName = irbInterface.getName(); int vlanId = irbInterface.getVlan(); computeSelfSwitchportNonSwitchportEdges(switchportsByVlan, hostname, irbName, vlanId) .forEach(edges::accept); Optional.ofNullable(vniSettingsByVlan.get(vlanId)) .map(vniSettings -> computeVniName(vniSettings.getVni())) .ifPresent( vniName -> edges.accept( new Layer2Edge( hostname, irbName, null, hostname, vniName, null, null))); }); vniSettingsByVlan.forEach( (vlanId, vniSettings) -> { String vniName = computeVniName(vniSettings.getVni()); computeSelfSwitchportNonSwitchportEdges(switchportsByVlan, hostname, vniName, vlanId) .forEach(edges::accept); }); } private  TopologyUtil(); }
TopologyUtil { @VisibleForTesting static void computeLayer2SelfEdges( @Nonnull Configuration config, @Nonnull Consumer<Layer2Edge> edges) { String hostname = config.getHostname(); Map<Integer, ImmutableList.Builder<String>> switchportsByVlanBuilder = new HashMap<>(); config.getAllInterfaces().values().stream() .filter(Interface::getActive) .forEach( i -> { if (i.getSwitchportMode() == SwitchportMode.TRUNK) { i.getAllowedVlans().stream() .forEach( vlan -> switchportsByVlanBuilder .computeIfAbsent(vlan, n -> ImmutableList.builder()) .add(i.getName())); } else if (i.getSwitchportMode() == SwitchportMode.ACCESS && i.getAccessVlan() != null) { switchportsByVlanBuilder .computeIfAbsent(i.getAccessVlan(), n -> ImmutableList.builder()) .add(i.getName()); } }); Map<Integer, List<String>> switchportsByVlan = CollectionUtil.toImmutableMap( switchportsByVlanBuilder, Entry::getKey, e -> e.getValue().build()); switchportsByVlan.forEach( (vlanId, interfaceNames) -> { String firstInterface = interfaceNames.get(0); Layer2Node firstNode = new Layer2Node(hostname, firstInterface, vlanId); for (String otherInterface : interfaceNames.subList(1, interfaceNames.size())) { Layer2Node otherNode = new Layer2Node(hostname, otherInterface, vlanId); edges.accept(new Layer2Edge(firstNode, otherNode, null)); } }); Map<Integer, Layer2Vni> vniSettingsByVlan = config.getVrfs().values().stream() .flatMap(vrf -> vrf.getLayer2Vnis().values().stream()) .collect(ImmutableMap.toImmutableMap(Layer2Vni::getVlan, Function.identity())); config.getAllInterfaces().values().stream() .filter(Interface::getActive) .filter(i -> i.getInterfaceType() == InterfaceType.VLAN && i.getVlan() != null) .forEach( irbInterface -> { String irbName = irbInterface.getName(); int vlanId = irbInterface.getVlan(); computeSelfSwitchportNonSwitchportEdges(switchportsByVlan, hostname, irbName, vlanId) .forEach(edges::accept); Optional.ofNullable(vniSettingsByVlan.get(vlanId)) .map(vniSettings -> computeVniName(vniSettings.getVni())) .ifPresent( vniName -> edges.accept( new Layer2Edge( hostname, irbName, null, hostname, vniName, null, null))); }); vniSettingsByVlan.forEach( (vlanId, vniSettings) -> { String vniName = computeVniName(vniSettings.getVni()); computeSelfSwitchportNonSwitchportEdges(switchportsByVlan, hostname, vniName, vlanId) .forEach(edges::accept); }); } private  TopologyUtil(); static @Nonnull Layer1Topology cleanLayer1PhysicalTopology(
      @Nonnull Layer1Topology rawLayer1Topology,
      @Nonnull Map<String, Configuration> configurations); static @Nonnull Optional<Layer1Topology> unionLayer1PhysicalTopologies(
      Optional<Layer1Topology> topology1, Optional<Layer1Topology> topology2); static @Nonnull Layer2Topology computeLayer2Topology(
      @Nonnull Layer1Topology layer1LogicalTopology,
      VxlanTopology vxlanTopology,
      @Nonnull Map<String, Configuration> configurations); static @Nonnull Topology computeRawLayer3Topology(
      @Nonnull Optional<Layer1Topology> rawLayer1PhysicalTopology,
      @Nonnull Optional<Layer1Topology> layer1LogicalTopology,
      @Nonnull Optional<Layer2Topology> layer2Topology,
      @Nonnull Map<String, Configuration> configurations); static @Nonnull Topology computeLayer3Topology(
      Topology rawLayer3Topology, Set<Edge> overlayEdges); static @Nullable Interface getInterface(
      @Nonnull Layer1Node layer1Node, @Nonnull Map<String, Configuration> configurations); static @Nullable Interface getInterface(
      @Nonnull Layer2Node layer2Node, @Nonnull Map<String, Configuration> configurations); static IpsecTopology computeIpsecTopology(Map<String, Configuration> configurations); static Map<String, Set<Interface>> computeNodeInterfaces(
      Map<String, Configuration> configurations); static Topology synthesizeL3Topology(Map<String, Configuration> configurations); static @Nonnull Layer1Topology computeLayer1LogicalTopology(
      Layer1Topology layer1PhysicalTopology, Map<String, Configuration> configurations); @Nonnull static TunnelTopology computeInitialTunnelTopology(
      Map<String, Configuration> configurations); @Nonnull static TunnelTopology pruneUnreachableTunnelEdges(
      TunnelTopology initialTunnelTopology,
      NetworkConfigurations configurations,
      TracerouteEngine tracerouteEngine); }
TopologyUtil { @VisibleForTesting static void computeLayer2SelfEdges( @Nonnull Configuration config, @Nonnull Consumer<Layer2Edge> edges) { String hostname = config.getHostname(); Map<Integer, ImmutableList.Builder<String>> switchportsByVlanBuilder = new HashMap<>(); config.getAllInterfaces().values().stream() .filter(Interface::getActive) .forEach( i -> { if (i.getSwitchportMode() == SwitchportMode.TRUNK) { i.getAllowedVlans().stream() .forEach( vlan -> switchportsByVlanBuilder .computeIfAbsent(vlan, n -> ImmutableList.builder()) .add(i.getName())); } else if (i.getSwitchportMode() == SwitchportMode.ACCESS && i.getAccessVlan() != null) { switchportsByVlanBuilder .computeIfAbsent(i.getAccessVlan(), n -> ImmutableList.builder()) .add(i.getName()); } }); Map<Integer, List<String>> switchportsByVlan = CollectionUtil.toImmutableMap( switchportsByVlanBuilder, Entry::getKey, e -> e.getValue().build()); switchportsByVlan.forEach( (vlanId, interfaceNames) -> { String firstInterface = interfaceNames.get(0); Layer2Node firstNode = new Layer2Node(hostname, firstInterface, vlanId); for (String otherInterface : interfaceNames.subList(1, interfaceNames.size())) { Layer2Node otherNode = new Layer2Node(hostname, otherInterface, vlanId); edges.accept(new Layer2Edge(firstNode, otherNode, null)); } }); Map<Integer, Layer2Vni> vniSettingsByVlan = config.getVrfs().values().stream() .flatMap(vrf -> vrf.getLayer2Vnis().values().stream()) .collect(ImmutableMap.toImmutableMap(Layer2Vni::getVlan, Function.identity())); config.getAllInterfaces().values().stream() .filter(Interface::getActive) .filter(i -> i.getInterfaceType() == InterfaceType.VLAN && i.getVlan() != null) .forEach( irbInterface -> { String irbName = irbInterface.getName(); int vlanId = irbInterface.getVlan(); computeSelfSwitchportNonSwitchportEdges(switchportsByVlan, hostname, irbName, vlanId) .forEach(edges::accept); Optional.ofNullable(vniSettingsByVlan.get(vlanId)) .map(vniSettings -> computeVniName(vniSettings.getVni())) .ifPresent( vniName -> edges.accept( new Layer2Edge( hostname, irbName, null, hostname, vniName, null, null))); }); vniSettingsByVlan.forEach( (vlanId, vniSettings) -> { String vniName = computeVniName(vniSettings.getVni()); computeSelfSwitchportNonSwitchportEdges(switchportsByVlan, hostname, vniName, vlanId) .forEach(edges::accept); }); } private  TopologyUtil(); static @Nonnull Layer1Topology cleanLayer1PhysicalTopology(
      @Nonnull Layer1Topology rawLayer1Topology,
      @Nonnull Map<String, Configuration> configurations); static @Nonnull Optional<Layer1Topology> unionLayer1PhysicalTopologies(
      Optional<Layer1Topology> topology1, Optional<Layer1Topology> topology2); static @Nonnull Layer2Topology computeLayer2Topology(
      @Nonnull Layer1Topology layer1LogicalTopology,
      VxlanTopology vxlanTopology,
      @Nonnull Map<String, Configuration> configurations); static @Nonnull Topology computeRawLayer3Topology(
      @Nonnull Optional<Layer1Topology> rawLayer1PhysicalTopology,
      @Nonnull Optional<Layer1Topology> layer1LogicalTopology,
      @Nonnull Optional<Layer2Topology> layer2Topology,
      @Nonnull Map<String, Configuration> configurations); static @Nonnull Topology computeLayer3Topology(
      Topology rawLayer3Topology, Set<Edge> overlayEdges); static @Nullable Interface getInterface(
      @Nonnull Layer1Node layer1Node, @Nonnull Map<String, Configuration> configurations); static @Nullable Interface getInterface(
      @Nonnull Layer2Node layer2Node, @Nonnull Map<String, Configuration> configurations); static IpsecTopology computeIpsecTopology(Map<String, Configuration> configurations); static Map<String, Set<Interface>> computeNodeInterfaces(
      Map<String, Configuration> configurations); static Topology synthesizeL3Topology(Map<String, Configuration> configurations); static @Nonnull Layer1Topology computeLayer1LogicalTopology(
      Layer1Topology layer1PhysicalTopology, Map<String, Configuration> configurations); @Nonnull static TunnelTopology computeInitialTunnelTopology(
      Map<String, Configuration> configurations); @Nonnull static TunnelTopology pruneUnreachableTunnelEdges(
      TunnelTopology initialTunnelTopology,
      NetworkConfigurations configurations,
      TracerouteEngine tracerouteEngine);  }