@Test public void testTryAddInterfaceToVlansTrunk() { int vlan = 1; IntegerSpace vlans = IntegerSpace.of(Range.closed(1, 3)); Map<Integer, ImmutableSet.Builder<NodeInterfacePair>> switchedVlanInterfaces = new HashMap<>(); Interface iface = _ib.build(); iface.setSwitchport(true); iface.setSwitchportMode(SwitchportMode.TRUNK); iface.setNativeVlan(vlan); iface.setAllowedVlans(vlans); tryAddInterfaceToVlans( ImmutableSet.of(NodeInterfacePair.of(iface)), false, vlans, switchedVlanInterfaces, iface); assertThat(switchedVlanInterfaces.keySet(), containsInAnyOrder(1, 2, 3)); switchedVlanInterfaces .values() .forEach(b -> assertThat(b.build(), contains(NodeInterfacePair.of(NODE, INTERFACE)))); }
@VisibleForTesting static void tryAddInterfaceToVlans( Set<NodeInterfacePair> specifiedInterfaces, boolean excludeShutInterfaces, IntegerSpace vlans, Map<Integer, Builder<NodeInterfacePair>> switchedVlanInterfaces, Interface iface) { NodeInterfacePair ifacePair = NodeInterfacePair.of(iface); if (!specifiedInterfaces.contains(ifacePair) || (excludeShutInterfaces && !iface.getActive()) || (iface.getInterfaceType() != InterfaceType.VLAN && !Boolean.TRUE.equals(iface.getSwitchport()))) { return; } if (iface.getInterfaceType() == InterfaceType.VLAN) { tryAddInterfaceToVlan(switchedVlanInterfaces, ifacePair, iface.getVlan(), vlans); } else if (iface.getSwitchportMode() == SwitchportMode.ACCESS) { tryAddInterfaceToVlan(switchedVlanInterfaces, ifacePair, iface.getAccessVlan(), vlans); } else if (iface.getSwitchportMode() == SwitchportMode.TRUNK) { iface.getAllowedVlans().stream() .forEach( allowedVlan -> tryAddInterfaceToVlan(switchedVlanInterfaces, ifacePair, allowedVlan, vlans)); } }
SwitchedVlanPropertiesAnswerer extends Answerer { @VisibleForTesting static void tryAddInterfaceToVlans( Set<NodeInterfacePair> specifiedInterfaces, boolean excludeShutInterfaces, IntegerSpace vlans, Map<Integer, Builder<NodeInterfacePair>> switchedVlanInterfaces, Interface iface) { NodeInterfacePair ifacePair = NodeInterfacePair.of(iface); if (!specifiedInterfaces.contains(ifacePair) || (excludeShutInterfaces && !iface.getActive()) || (iface.getInterfaceType() != InterfaceType.VLAN && !Boolean.TRUE.equals(iface.getSwitchport()))) { return; } if (iface.getInterfaceType() == InterfaceType.VLAN) { tryAddInterfaceToVlan(switchedVlanInterfaces, ifacePair, iface.getVlan(), vlans); } else if (iface.getSwitchportMode() == SwitchportMode.ACCESS) { tryAddInterfaceToVlan(switchedVlanInterfaces, ifacePair, iface.getAccessVlan(), vlans); } else if (iface.getSwitchportMode() == SwitchportMode.TRUNK) { iface.getAllowedVlans().stream() .forEach( allowedVlan -> tryAddInterfaceToVlan(switchedVlanInterfaces, ifacePair, allowedVlan, vlans)); } } }
SwitchedVlanPropertiesAnswerer extends Answerer { @VisibleForTesting static void tryAddInterfaceToVlans( Set<NodeInterfacePair> specifiedInterfaces, boolean excludeShutInterfaces, IntegerSpace vlans, Map<Integer, Builder<NodeInterfacePair>> switchedVlanInterfaces, Interface iface) { NodeInterfacePair ifacePair = NodeInterfacePair.of(iface); if (!specifiedInterfaces.contains(ifacePair) || (excludeShutInterfaces && !iface.getActive()) || (iface.getInterfaceType() != InterfaceType.VLAN && !Boolean.TRUE.equals(iface.getSwitchport()))) { return; } if (iface.getInterfaceType() == InterfaceType.VLAN) { tryAddInterfaceToVlan(switchedVlanInterfaces, ifacePair, iface.getVlan(), vlans); } else if (iface.getSwitchportMode() == SwitchportMode.ACCESS) { tryAddInterfaceToVlan(switchedVlanInterfaces, ifacePair, iface.getAccessVlan(), vlans); } else if (iface.getSwitchportMode() == SwitchportMode.TRUNK) { iface.getAllowedVlans().stream() .forEach( allowedVlan -> tryAddInterfaceToVlan(switchedVlanInterfaces, ifacePair, allowedVlan, vlans)); } } SwitchedVlanPropertiesAnswerer(Question question, IBatfish batfish); }
SwitchedVlanPropertiesAnswerer extends Answerer { @VisibleForTesting static void tryAddInterfaceToVlans( Set<NodeInterfacePair> specifiedInterfaces, boolean excludeShutInterfaces, IntegerSpace vlans, Map<Integer, Builder<NodeInterfacePair>> switchedVlanInterfaces, Interface iface) { NodeInterfacePair ifacePair = NodeInterfacePair.of(iface); if (!specifiedInterfaces.contains(ifacePair) || (excludeShutInterfaces && !iface.getActive()) || (iface.getInterfaceType() != InterfaceType.VLAN && !Boolean.TRUE.equals(iface.getSwitchport()))) { return; } if (iface.getInterfaceType() == InterfaceType.VLAN) { tryAddInterfaceToVlan(switchedVlanInterfaces, ifacePair, iface.getVlan(), vlans); } else if (iface.getSwitchportMode() == SwitchportMode.ACCESS) { tryAddInterfaceToVlan(switchedVlanInterfaces, ifacePair, iface.getAccessVlan(), vlans); } else if (iface.getSwitchportMode() == SwitchportMode.TRUNK) { iface.getAllowedVlans().stream() .forEach( allowedVlan -> tryAddInterfaceToVlan(switchedVlanInterfaces, ifacePair, allowedVlan, vlans)); } } SwitchedVlanPropertiesAnswerer(Question question, IBatfish batfish); static List<ColumnMetadata> createColumnMetadata(); static String getColumnName(String property); static Multiset<Row> getProperties(
      SpecifierContext ctxt,
      Map<String, Configuration> configurations,
      Set<String> nodes,
      InterfaceSpecifier interfaceSpecifier,
      boolean excludeShutInterfaces,
      IntegerSpace vlans,
      Map<String, ColumnMetadata> columns); @Override TableAnswerElement answer(NetworkSnapshot snapshot); }
SwitchedVlanPropertiesAnswerer extends Answerer { @VisibleForTesting static void tryAddInterfaceToVlans( Set<NodeInterfacePair> specifiedInterfaces, boolean excludeShutInterfaces, IntegerSpace vlans, Map<Integer, Builder<NodeInterfacePair>> switchedVlanInterfaces, Interface iface) { NodeInterfacePair ifacePair = NodeInterfacePair.of(iface); if (!specifiedInterfaces.contains(ifacePair) || (excludeShutInterfaces && !iface.getActive()) || (iface.getInterfaceType() != InterfaceType.VLAN && !Boolean.TRUE.equals(iface.getSwitchport()))) { return; } if (iface.getInterfaceType() == InterfaceType.VLAN) { tryAddInterfaceToVlan(switchedVlanInterfaces, ifacePair, iface.getVlan(), vlans); } else if (iface.getSwitchportMode() == SwitchportMode.ACCESS) { tryAddInterfaceToVlan(switchedVlanInterfaces, ifacePair, iface.getAccessVlan(), vlans); } else if (iface.getSwitchportMode() == SwitchportMode.TRUNK) { iface.getAllowedVlans().stream() .forEach( allowedVlan -> tryAddInterfaceToVlan(switchedVlanInterfaces, ifacePair, allowedVlan, vlans)); } } SwitchedVlanPropertiesAnswerer(Question question, IBatfish batfish); static List<ColumnMetadata> createColumnMetadata(); static String getColumnName(String property); static Multiset<Row> getProperties(
      SpecifierContext ctxt,
      Map<String, Configuration> configurations,
      Set<String> nodes,
      InterfaceSpecifier interfaceSpecifier,
      boolean excludeShutInterfaces,
      IntegerSpace vlans,
      Map<String, ColumnMetadata> columns); @Override TableAnswerElement answer(NetworkSnapshot snapshot);  }