class IpOwners {

  @VisibleForTesting
  static Map<Ip, Map<String, Map<String, Set<String>>>> computeIpIfaceOwners(
      Map<String, Set<Interface>> allInterfaces,
      Map<Ip, Map<String, Set<String>>> activeDeviceOwnedIps) {

    // Helper mapping: hostname -> vrf -> interfaces
    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, /* Ip */
        ipEntry ->
            toImmutableMap(
                ipEntry.getValue(),
                Entry::getKey, /* hostname */
                nodeEntry -> {
                  String hostname = nodeEntry.getKey();
                  Set<String> ownerIfaces = nodeEntry.getValue();
                  return hostsToVrfsToIfaces.get(hostname).entrySet().stream()
                      // Filter to VRFs containing interfaces that own this IP
                      .filter(vrfEntry -> !Collections.disjoint(vrfEntry.getValue(), ownerIfaces))
                      .collect(
                          // Map each VRF to its set of interfaces that own this IP
                          ImmutableMap.toImmutableMap(
                              Entry::getKey,
                              vrfEntry -> Sets.intersection(vrfEntry.getValue(), ownerIfaces)));
                }));
  }

  public  IpOwners(Map<String, Configuration> configurations);

}