@Test public void testComputeIpIfaceOwners() { String node = "node"; String vrfName = "vrf"; Vrf vrf = new Vrf(vrfName); String activeName = "active"; Ip activeIp = Ip.parse("1.1.1.1"); String inactiveName = "inactive"; Ip inactiveIp = Ip.parse("2.2.2.2"); int networkBits = 31; Map<String, Set<Interface>> allInterfaces = ImmutableMap.of( node, ImmutableSet.of( Interface.builder() .setAddress(ConcreteInterfaceAddress.create(activeIp, networkBits)) .setVrf(vrf) .setName(activeName) .build(), Interface.builder() .setAddress(ConcreteInterfaceAddress.create(inactiveIp, networkBits)) .setActive(false) .setName(inactiveName) .setVrf(vrf) .build())); Map<Ip, Map<String, Set<String>>> activeIps = ImmutableMap.of(activeIp, ImmutableMap.of(node, ImmutableSet.of(activeName))); Map<Ip, Map<String, Map<String, Set<String>>>> ipIfaceOwners = computeIpIfaceOwners(allInterfaces, activeIps); assertThat( ipIfaceOwners, equalTo( ImmutableMap.of( activeIp, ImmutableMap.of(node, ImmutableMap.of(vrfName, ImmutableSet.of(activeName)))))); }
@VisibleForTesting static Map<Ip, Map<String, Map<String, Set<String>>>> computeIpIfaceOwners( Map<String, Set<Interface>> allInterfaces, Map<Ip, Map<String, Set<String>>> activeDeviceOwnedIps) { Map<String, Map<String, Set<String>>> hostsToVrfsToIfaces = new HashMap<>(); allInterfaces.forEach( (hostname, ifaces) -> ifaces.forEach( iface -> { hostsToVrfsToIfaces .computeIfAbsent(hostname, n -> new HashMap<>()) .computeIfAbsent(iface.getVrfName(), n -> new HashSet<>()) .add(iface.getName()); })); return toImmutableMap( activeDeviceOwnedIps, Entry::getKey, ipEntry -> toImmutableMap( ipEntry.getValue(), Entry::getKey, nodeEntry -> { String hostname = nodeEntry.getKey(); Set<String> ownerIfaces = nodeEntry.getValue(); return hostsToVrfsToIfaces.get(hostname).entrySet().stream() .filter(vrfEntry -> !Collections.disjoint(vrfEntry.getValue(), ownerIfaces)) .collect( ImmutableMap.toImmutableMap( Entry::getKey, vrfEntry -> Sets.intersection(vrfEntry.getValue(), ownerIfaces))); })); }
IpOwners { @VisibleForTesting static Map<Ip, Map<String, Map<String, Set<String>>>> computeIpIfaceOwners( Map<String, Set<Interface>> allInterfaces, Map<Ip, Map<String, Set<String>>> activeDeviceOwnedIps) { Map<String, Map<String, Set<String>>> hostsToVrfsToIfaces = new HashMap<>(); allInterfaces.forEach( (hostname, ifaces) -> ifaces.forEach( iface -> { hostsToVrfsToIfaces .computeIfAbsent(hostname, n -> new HashMap<>()) .computeIfAbsent(iface.getVrfName(), n -> new HashSet<>()) .add(iface.getName()); })); return toImmutableMap( activeDeviceOwnedIps, Entry::getKey, ipEntry -> toImmutableMap( ipEntry.getValue(), Entry::getKey, nodeEntry -> { String hostname = nodeEntry.getKey(); Set<String> ownerIfaces = nodeEntry.getValue(); return hostsToVrfsToIfaces.get(hostname).entrySet().stream() .filter(vrfEntry -> !Collections.disjoint(vrfEntry.getValue(), ownerIfaces)) .collect( ImmutableMap.toImmutableMap( Entry::getKey, vrfEntry -> Sets.intersection(vrfEntry.getValue(), ownerIfaces))); })); } }
IpOwners { @VisibleForTesting static Map<Ip, Map<String, Map<String, Set<String>>>> computeIpIfaceOwners( Map<String, Set<Interface>> allInterfaces, Map<Ip, Map<String, Set<String>>> activeDeviceOwnedIps) { Map<String, Map<String, Set<String>>> hostsToVrfsToIfaces = new HashMap<>(); allInterfaces.forEach( (hostname, ifaces) -> ifaces.forEach( iface -> { hostsToVrfsToIfaces .computeIfAbsent(hostname, n -> new HashMap<>()) .computeIfAbsent(iface.getVrfName(), n -> new HashSet<>()) .add(iface.getName()); })); return toImmutableMap( activeDeviceOwnedIps, Entry::getKey, ipEntry -> toImmutableMap( ipEntry.getValue(), Entry::getKey, nodeEntry -> { String hostname = nodeEntry.getKey(); Set<String> ownerIfaces = nodeEntry.getValue(); return hostsToVrfsToIfaces.get(hostname).entrySet().stream() .filter(vrfEntry -> !Collections.disjoint(vrfEntry.getValue(), ownerIfaces)) .collect( ImmutableMap.toImmutableMap( Entry::getKey, vrfEntry -> Sets.intersection(vrfEntry.getValue(), ownerIfaces))); })); } IpOwners(Map<String, Configuration> configurations); }
IpOwners { @VisibleForTesting static Map<Ip, Map<String, Map<String, Set<String>>>> computeIpIfaceOwners( Map<String, Set<Interface>> allInterfaces, Map<Ip, Map<String, Set<String>>> activeDeviceOwnedIps) { Map<String, Map<String, Set<String>>> hostsToVrfsToIfaces = new HashMap<>(); allInterfaces.forEach( (hostname, ifaces) -> ifaces.forEach( iface -> { hostsToVrfsToIfaces .computeIfAbsent(hostname, n -> new HashMap<>()) .computeIfAbsent(iface.getVrfName(), n -> new HashSet<>()) .add(iface.getName()); })); return toImmutableMap( activeDeviceOwnedIps, Entry::getKey, ipEntry -> toImmutableMap( ipEntry.getValue(), Entry::getKey, nodeEntry -> { String hostname = nodeEntry.getKey(); Set<String> ownerIfaces = nodeEntry.getValue(); return hostsToVrfsToIfaces.get(hostname).entrySet().stream() .filter(vrfEntry -> !Collections.disjoint(vrfEntry.getValue(), ownerIfaces)) .collect( ImmutableMap.toImmutableMap( Entry::getKey, vrfEntry -> Sets.intersection(vrfEntry.getValue(), ownerIfaces))); })); } 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 Map<Ip, Map<String, Map<String, Set<String>>>> computeIpIfaceOwners( Map<String, Set<Interface>> allInterfaces, Map<Ip, Map<String, Set<String>>> activeDeviceOwnedIps) { Map<String, Map<String, Set<String>>> hostsToVrfsToIfaces = new HashMap<>(); allInterfaces.forEach( (hostname, ifaces) -> ifaces.forEach( iface -> { hostsToVrfsToIfaces .computeIfAbsent(hostname, n -> new HashMap<>()) .computeIfAbsent(iface.getVrfName(), n -> new HashSet<>()) .add(iface.getName()); })); return toImmutableMap( activeDeviceOwnedIps, Entry::getKey, ipEntry -> toImmutableMap( ipEntry.getValue(), Entry::getKey, nodeEntry -> { String hostname = nodeEntry.getKey(); Set<String> ownerIfaces = nodeEntry.getValue(); return hostsToVrfsToIfaces.get(hostname).entrySet().stream() .filter(vrfEntry -> !Collections.disjoint(vrfEntry.getValue(), ownerIfaces)) .collect( ImmutableMap.toImmutableMap( Entry::getKey, vrfEntry -> Sets.intersection(vrfEntry.getValue(), ownerIfaces))); })); } 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();  }