Introduced a new API to match on auxLocations in getConnectedHosts

Change-Id: I3df5493898bd389a5dfe631053f5ce51c076c106
diff --git a/core/api/src/main/java/org/onosproject/net/host/HostService.java b/core/api/src/main/java/org/onosproject/net/host/HostService.java
index 349f6db..12da84c 100644
--- a/core/api/src/main/java/org/onosproject/net/host/HostService.java
+++ b/core/api/src/main/java/org/onosproject/net/host/HostService.java
@@ -89,6 +89,17 @@
     Set<Host> getConnectedHosts(ConnectPoint connectPoint);
 
     /**
+     * Returns the set of host that attach to the specified connect point.
+     *
+     * @param connectPoint connect point
+     * @param matchAuxLocations true to match on the auxLocations, false to match on locations of the hosts
+     * @return set of hosts connected to the connection point
+     */
+    default Set<Host> getConnectedHosts(ConnectPoint connectPoint, boolean matchAuxLocations) {
+        return getConnectedHosts(connectPoint);
+    }
+
+    /**
      * Returns the set of hosts that attach to the specified device.
      *
      * @param deviceId device identifier
diff --git a/core/api/src/main/java/org/onosproject/net/host/HostStore.java b/core/api/src/main/java/org/onosproject/net/host/HostStore.java
index b6b27b5..2bad087 100644
--- a/core/api/src/main/java/org/onosproject/net/host/HostStore.java
+++ b/core/api/src/main/java/org/onosproject/net/host/HostStore.java
@@ -135,6 +135,17 @@
     Set<Host> getConnectedHosts(ConnectPoint connectPoint);
 
     /**
+     * Returns the set of host that attach to the specified connect point.
+     *
+     * @param connectPoint connect point
+     * @param matchAuxLocations true to match on the auxLocations, false to match on locations of the hosts
+     * @return set of hosts connected to the connection point
+     */
+    default Set<Host> getConnectedHosts(ConnectPoint connectPoint, boolean matchAuxLocations) {
+        return getConnectedHosts(connectPoint);
+    }
+
+    /**
      * Returns the set of hosts that attach to the specified device.
      *
      * @param deviceId infrastructure device identifier
diff --git a/core/net/src/main/java/org/onosproject/net/host/impl/HostManager.java b/core/net/src/main/java/org/onosproject/net/host/impl/HostManager.java
index 3bacd7f..07c7996 100644
--- a/core/net/src/main/java/org/onosproject/net/host/impl/HostManager.java
+++ b/core/net/src/main/java/org/onosproject/net/host/impl/HostManager.java
@@ -419,9 +419,14 @@
 
     @Override
     public Set<Host> getConnectedHosts(ConnectPoint connectPoint) {
+        return getConnectedHosts(connectPoint, false);
+    }
+
+    @Override
+    public Set<Host> getConnectedHosts(ConnectPoint connectPoint, boolean matchAuxLocations) {
         checkPermission(HOST_READ);
         checkNotNull(connectPoint, "Connection point cannot be null");
-        return store.getConnectedHosts(connectPoint);
+        return store.getConnectedHosts(connectPoint, matchAuxLocations);
     }
 
     @Override
diff --git a/core/store/dist/src/main/java/org/onosproject/store/host/impl/DistributedHostStore.java b/core/store/dist/src/main/java/org/onosproject/store/host/impl/DistributedHostStore.java
index c68cee3..faea6ec 100644
--- a/core/store/dist/src/main/java/org/onosproject/store/host/impl/DistributedHostStore.java
+++ b/core/store/dist/src/main/java/org/onosproject/store/host/impl/DistributedHostStore.java
@@ -350,8 +350,23 @@
 
     @Override
     public Set<Host> getConnectedHosts(ConnectPoint connectPoint) {
+        return getConnectedHosts(connectPoint, false);
+    }
+
+    @Override
+    public Set<Host> getConnectedHosts(ConnectPoint connectPoint, boolean matchAuxLocations) {
+        Predicate<Map.Entry<HostId, DefaultHost>> predicate;
+        if (matchAuxLocations) {
+            predicate = entry -> {
+                Set<HostLocation> auxLocations = entry.getValue().auxLocations();
+                return auxLocations != null && entry.getValue().auxLocations().contains(connectPoint);
+            };
+        } else {
+            predicate = entry -> entry.getValue().locations().contains(connectPoint);
+        }
+
         Set<Host> filtered = hosts.entrySet().stream()
-                .filter(entry -> entry.getValue().locations().contains(connectPoint))
+                .filter(predicate)
                 .map(Map.Entry::getValue)
                 .collect(Collectors.toSet());
         return ImmutableSet.copyOf(filtered);
diff --git a/core/store/dist/src/test/java/org/onosproject/store/host/impl/DistributedHostStoreTest.java b/core/store/dist/src/test/java/org/onosproject/store/host/impl/DistributedHostStoreTest.java
index 18e8a26..b199a9d 100644
--- a/core/store/dist/src/test/java/org/onosproject/store/host/impl/DistributedHostStoreTest.java
+++ b/core/store/dist/src/test/java/org/onosproject/store/host/impl/DistributedHostStoreTest.java
@@ -100,6 +100,30 @@
                                                                 HOST_ADDRESS,
                                                                 HOST_LEARNT_WITH_ADDRESSES.configured(),
                                                                 HOST_LEARNT_WITH_ADDRESSES.annotations());
+
+    private static final HostDescription HOST_DESC_WITHOUT_AUX =
+            new DefaultHostDescription(HOSTID.mac(), HOSTID.vlanId(),
+                    Sets.newHashSet(HOST_LOC11), null,
+                    Sets.newHashSet(IP1, IP2), VlanId.NONE, EthType.EtherType.UNKNOWN.ethType(),
+                    false);
+    private static final HostDescription HOST_DESC_WITH_AUX =
+            new DefaultHostDescription(HOSTID1.mac(), HOSTID1.vlanId(),
+                    Sets.newHashSet(HOST_LOC11), Sets.newHashSet(HOST_LOC12),
+                    Sets.newHashSet(IP1, IP2), VlanId.NONE, EthType.EtherType.UNKNOWN.ethType(),
+                    false);
+    private  static final Host HOST_WITHOUT_AUX =
+            new DefaultHost(PID, HOSTID,
+                    HOST_DESC_WITHOUT_AUX.hwAddress(), HOST_DESC_WITHOUT_AUX.vlan(),
+                    HOST_DESC_WITHOUT_AUX.locations(), HOST_DESC_WITHOUT_AUX.auxLocations(),
+                    HOST_DESC_WITHOUT_AUX.ipAddress(), HOST_DESC_WITHOUT_AUX.innerVlan(), HOST_DESC_WITHOUT_AUX.tpid(),
+                    HOST_DESC_WITHOUT_AUX.configured(), false);
+    private  static final Host HOST_WITH_AUX =
+            new DefaultHost(PID, HOSTID1,
+                    HOST_DESC_WITH_AUX.hwAddress(), HOST_DESC_WITH_AUX.vlan(),
+                    HOST_DESC_WITH_AUX.locations(), HOST_DESC_WITH_AUX.auxLocations(),
+                    HOST_DESC_WITH_AUX.ipAddress(), HOST_DESC_WITH_AUX.innerVlan(), HOST_DESC_WITH_AUX.tpid(),
+                    HOST_DESC_WITH_AUX.configured(), false);
+
     private static final MapEvent<HostId, DefaultHost> HOST_EVENT =
             new MapEvent<>("foobar", HOSTID, new Versioned<>(NEW_HOST, 0), new Versioned<>(OLD_HOST, 0));
     private static final DefaultHost HOST1 = new DefaultHost(PID, HOSTID, HOSTID.mac(), HOSTID.vlanId(),
@@ -383,6 +407,19 @@
         assertNull(delegate.lastEvent.prevSubject());
     }
 
+    @Test
+    public void testGetConnectedHost() {
+        ecXHostStore.createOrUpdateHost(PID, HOSTID, HOST_DESC_WITHOUT_AUX, false);
+        ecXHostStore.createOrUpdateHost(PID, HOSTID1, HOST_DESC_WITH_AUX, false);
+
+        assertEquals(Sets.newHashSet(HOST_WITHOUT_AUX, HOST_WITH_AUX),
+                ecXHostStore.getConnectedHosts(HOST_LOC11, false));
+        assertEquals(Sets.newHashSet(),
+                ecXHostStore.getConnectedHosts(HOST_LOC11, true));
+        assertEquals(Sets.newHashSet(HOST_WITH_AUX),
+                ecXHostStore.getConnectedHosts(HOST_LOC12, true));
+    }
+
     private class TestStoreDelegate implements HostStoreDelegate {
         public HostEvent lastEvent;