@Test public void testProcessHsrpGroups() { Map<Ip, Map<String, Set<String>>> ipOwners = new HashMap<>(); Configuration c1 = Configuration.builder() .setHostname("c1") .setConfigurationFormat(ConfigurationFormat.CISCO_IOS) .build(); Configuration c2 = Configuration.builder() .setHostname("c2") .setConfigurationFormat(ConfigurationFormat.CISCO_IOS) .build(); Table<Ip, Integer, Set<Interface>> groups = HashBasedTable.create(); Interface i1 = Interface.builder().setOwner(c1).setName("i1").build(); Interface i2 = Interface.builder().setOwner(c2).setName("i2").build(); Ip ip = Ip.parse("1.1.1.1"); i1.setHsrpGroups( ImmutableMap.of( 1, HsrpGroup.builder().setPriority(100).setGroupNumber(1).setIp(ip).build())); i2.setHsrpGroups( ImmutableMap.of( 1, HsrpGroup.builder().setPriority(200).setGroupNumber(1).setIp(ip).build())); extractHsrp(groups, i1); extractHsrp(groups, i2); processHsrpGroups(ipOwners, groups); assertThat(ipOwners.get(ip).get(c2.getHostname()), equalTo(ImmutableSet.of(i2.getName()))); }
@VisibleForTesting static void processHsrpGroups( Map<Ip, Map<String, Set<String>>> ipOwners, Table<Ip, Integer, Set<Interface>> hsrpGroups) { hsrpGroups .cellSet() .forEach( cell -> { Ip ip = cell.getRowKey(); assert ip != null; Integer groupNum = cell.getColumnKey(); assert groupNum != null; Set<Interface> candidates = cell.getValue(); assert candidates != null; Interface hsrpMaster = Collections.max( candidates, Comparator.comparingInt( (Interface i) -> i.getHsrpGroups().get(groupNum).getPriority()) .thenComparing(i -> i.getConcreteAddress().getIp())); ipOwners .computeIfAbsent(ip, k -> new HashMap<>()) .computeIfAbsent(hsrpMaster.getOwner().getHostname(), k -> new HashSet<>()) .add(hsrpMaster.getName()); }); }
IpOwners { @VisibleForTesting static void processHsrpGroups( Map<Ip, Map<String, Set<String>>> ipOwners, Table<Ip, Integer, Set<Interface>> hsrpGroups) { hsrpGroups .cellSet() .forEach( cell -> { Ip ip = cell.getRowKey(); assert ip != null; Integer groupNum = cell.getColumnKey(); assert groupNum != null; Set<Interface> candidates = cell.getValue(); assert candidates != null; Interface hsrpMaster = Collections.max( candidates, Comparator.comparingInt( (Interface i) -> i.getHsrpGroups().get(groupNum).getPriority()) .thenComparing(i -> i.getConcreteAddress().getIp())); ipOwners .computeIfAbsent(ip, k -> new HashMap<>()) .computeIfAbsent(hsrpMaster.getOwner().getHostname(), k -> new HashSet<>()) .add(hsrpMaster.getName()); }); } }
IpOwners { @VisibleForTesting static void processHsrpGroups( Map<Ip, Map<String, Set<String>>> ipOwners, Table<Ip, Integer, Set<Interface>> hsrpGroups) { hsrpGroups .cellSet() .forEach( cell -> { Ip ip = cell.getRowKey(); assert ip != null; Integer groupNum = cell.getColumnKey(); assert groupNum != null; Set<Interface> candidates = cell.getValue(); assert candidates != null; Interface hsrpMaster = Collections.max( candidates, Comparator.comparingInt( (Interface i) -> i.getHsrpGroups().get(groupNum).getPriority()) .thenComparing(i -> i.getConcreteAddress().getIp())); ipOwners .computeIfAbsent(ip, k -> new HashMap<>()) .computeIfAbsent(hsrpMaster.getOwner().getHostname(), k -> new HashSet<>()) .add(hsrpMaster.getName()); }); } IpOwners(Map<String, Configuration> configurations); }
IpOwners { @VisibleForTesting static void processHsrpGroups( Map<Ip, Map<String, Set<String>>> ipOwners, Table<Ip, Integer, Set<Interface>> hsrpGroups) { hsrpGroups .cellSet() .forEach( cell -> { Ip ip = cell.getRowKey(); assert ip != null; Integer groupNum = cell.getColumnKey(); assert groupNum != null; Set<Interface> candidates = cell.getValue(); assert candidates != null; Interface hsrpMaster = Collections.max( candidates, Comparator.comparingInt( (Interface i) -> i.getHsrpGroups().get(groupNum).getPriority()) .thenComparing(i -> i.getConcreteAddress().getIp())); ipOwners .computeIfAbsent(ip, k -> new HashMap<>()) .computeIfAbsent(hsrpMaster.getOwner().getHostname(), k -> new HashSet<>()) .add(hsrpMaster.getName()); }); } IpOwners(Map<String, Configuration> configurations); static Map<String, Map<String, Set<Ip>>> computeInterfaceOwnedIps(
      Map<String, Configuration> configurations, boolean excludeInactive); static Map<Ip, Set<String>> computeIpNodeOwners(
      Map<String, Configuration> configurations, boolean excludeInactive); Map<Ip, Map<String, Set<String>>> getActiveDeviceOwnedIps(); Map<Ip, Map<String, Set<String>>> getAllDeviceOwnedIps(); Map<String, Map<String, IpSpace>> getAllInterfaceHostIps(); Map<String, Map<String, IpSpace>> getInterfaceOwnedIpSpaces(); Map<Ip, Map<String, Set<String>>> getIpVrfOwners(); Map<String, Map<String, Map<String, IpSpace>>> getVrfIfaceOwnedIpSpaces(); }
IpOwners { @VisibleForTesting static void processHsrpGroups( Map<Ip, Map<String, Set<String>>> ipOwners, Table<Ip, Integer, Set<Interface>> hsrpGroups) { hsrpGroups .cellSet() .forEach( cell -> { Ip ip = cell.getRowKey(); assert ip != null; Integer groupNum = cell.getColumnKey(); assert groupNum != null; Set<Interface> candidates = cell.getValue(); assert candidates != null; Interface hsrpMaster = Collections.max( candidates, Comparator.comparingInt( (Interface i) -> i.getHsrpGroups().get(groupNum).getPriority()) .thenComparing(i -> i.getConcreteAddress().getIp())); ipOwners .computeIfAbsent(ip, k -> new HashMap<>()) .computeIfAbsent(hsrpMaster.getOwner().getHostname(), k -> new HashSet<>()) .add(hsrpMaster.getName()); }); } IpOwners(Map<String, Configuration> configurations); static Map<String, Map<String, Set<Ip>>> computeInterfaceOwnedIps(
      Map<String, Configuration> configurations, boolean excludeInactive); static Map<Ip, Set<String>> computeIpNodeOwners(
      Map<String, Configuration> configurations, boolean excludeInactive); Map<Ip, Map<String, Set<String>>> getActiveDeviceOwnedIps(); Map<Ip, Map<String, Set<String>>> getAllDeviceOwnedIps(); Map<String, Map<String, IpSpace>> getAllInterfaceHostIps(); Map<String, Map<String, IpSpace>> getInterfaceOwnedIpSpaces(); Map<Ip, Map<String, Set<String>>> getIpVrfOwners(); Map<String, Map<String, Map<String, IpSpace>>> getVrfIfaceOwnedIpSpaces();  }