Extend Network Config Host Provider to support multihoming

Change-Id: I6e9dd18a5189a7bf35a617a00bd46e4a32acf524
diff --git a/providers/netcfghost/src/main/java/org/onosproject/provider/netcfghost/NetworkConfigHostProvider.java b/providers/netcfghost/src/main/java/org/onosproject/provider/netcfghost/NetworkConfigHostProvider.java
index 5e7c4e9..59e8234 100644
--- a/providers/netcfghost/src/main/java/org/onosproject/provider/netcfghost/NetworkConfigHostProvider.java
+++ b/providers/netcfghost/src/main/java/org/onosproject/provider/netcfghost/NetworkConfigHostProvider.java
@@ -26,7 +26,6 @@
 import org.onlab.packet.VlanId;
 import org.onosproject.core.ApplicationId;
 import org.onosproject.core.CoreService;
-import org.onosproject.net.ConnectPoint;
 import org.onosproject.net.Host;
 import org.onosproject.net.HostId;
 import org.onosproject.net.HostLocation;
@@ -43,7 +42,10 @@
 import org.onosproject.net.provider.ProviderId;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
+
+import java.util.Collections;
 import java.util.Set;
+import java.util.stream.Collectors;
 
 /**
  * Host provider that uses network config service to discover hosts.
@@ -106,14 +108,14 @@
      *
      * @param mac MAC address of the host
      * @param vlan VLAN ID of the host
-     * @param hloc Location of the host
+     * @param locations Location of the host
      * @param ips Set of IP addresses of the host
      */
-    protected void addHost(MacAddress mac, VlanId vlan, HostLocation hloc, Set<IpAddress> ips) {
+    protected void addHost(MacAddress mac, VlanId vlan, Set<HostLocation> locations, Set<IpAddress> ips) {
         HostId hid = HostId.hostId(mac, vlan);
         HostDescription desc = (ips != null) ?
-                new DefaultHostDescription(mac, vlan, hloc, ips, true) :
-                new DefaultHostDescription(mac, vlan, hloc, true);
+                new DefaultHostDescription(mac, vlan, locations, ips, true) :
+                new DefaultHostDescription(mac, vlan, locations, Collections.emptySet(), true);
         providerService.hostDetected(hid, desc, false);
     }
 
@@ -123,12 +125,12 @@
      *
      * @param mac MAC address of the host
      * @param vlan VLAN ID of the host
-     * @param hloc Location of the host
+     * @param locations Location of the host
      * @param ips Set of IP addresses of the host
      */
-    protected void updateHost(MacAddress mac, VlanId vlan, HostLocation hloc, Set<IpAddress> ips) {
+    protected void updateHost(MacAddress mac, VlanId vlan, Set<HostLocation> locations, Set<IpAddress> ips) {
         HostId hid = HostId.hostId(mac, vlan);
-        HostDescription desc = new DefaultHostDescription(mac, vlan, hloc, ips, true);
+        HostDescription desc = new DefaultHostDescription(mac, vlan, locations, ips, true);
         providerService.hostDetected(hid, desc, true);
     }
 
@@ -150,9 +152,10 @@
             BasicHostConfig hostConfig =
                     networkConfigRegistry.getConfig(hostId, BasicHostConfig.class);
             Set<IpAddress> ipAddresses = hostConfig.ipAddresses();
-            ConnectPoint location = hostConfig.location();
-            HostLocation hloc = new HostLocation(location, System.currentTimeMillis());
-            addHost(mac, vlan, hloc, ipAddresses);
+            Set<HostLocation> locations = hostConfig.locations().stream()
+                    .map(hostLocation -> new HostLocation(hostLocation, System.currentTimeMillis()))
+                    .collect(Collectors.toSet());
+            addHost(mac, vlan, locations, ipAddresses);
         });
     }
 
@@ -172,21 +175,22 @@
             BasicHostConfig hostConfig =
                     networkConfigRegistry.getConfig(hostId, BasicHostConfig.class);
             Set<IpAddress> ipAddresses = null;
-            HostLocation hloc = null;
+            Set<HostLocation> locations = null;
 
             // Note: There will be no config presented in the CONFIG_REMOVE case
             if (hostConfig != null) {
                 ipAddresses = hostConfig.ipAddresses();
-                ConnectPoint location = hostConfig.location();
-                hloc = new HostLocation(location, System.currentTimeMillis());
+                locations = hostConfig.locations().stream()
+                        .map(hostLocation -> new HostLocation(hostLocation, System.currentTimeMillis()))
+                        .collect(Collectors.toSet());
             }
 
             switch (event.type()) {
                 case CONFIG_ADDED:
-                    addHost(mac, vlan, hloc, ipAddresses);
+                    addHost(mac, vlan, locations, ipAddresses);
                     break;
                 case CONFIG_UPDATED:
-                    updateHost(mac, vlan, hloc, ipAddresses);
+                    updateHost(mac, vlan, locations, ipAddresses);
                     break;
                 case CONFIG_REMOVED:
                     removeHost(mac, vlan);
diff --git a/providers/netcfghost/src/test/java/org/onosproject/provider/netcfghost/NetworkConfigHostProviderTest.java b/providers/netcfghost/src/test/java/org/onosproject/provider/netcfghost/NetworkConfigHostProviderTest.java
index 81422c8..ac47dba 100644
--- a/providers/netcfghost/src/test/java/org/onosproject/provider/netcfghost/NetworkConfigHostProviderTest.java
+++ b/providers/netcfghost/src/test/java/org/onosproject/provider/netcfghost/NetworkConfigHostProviderTest.java
@@ -16,6 +16,7 @@
 
 package org.onosproject.provider.netcfghost;
 
+import com.google.common.collect.Sets;
 import org.junit.Before;
 import org.junit.Test;
 import org.onlab.packet.IpAddress;
@@ -49,7 +50,7 @@
     private VlanId vlan = VlanId.vlanId(VlanId.UNTAGGED);
     private DeviceId deviceId = DeviceId.deviceId("of:0000000000000001");
     private PortNumber port = PortNumber.portNumber(5);
-    private HostLocation hloc = new HostLocation(deviceId, port, 100);
+    private Set<HostLocation> locations = Sets.newHashSet(new HostLocation(deviceId, port, 100));
     private Set<IpAddress> ips = new HashSet<>();
     private HostId hostId = HostId.hostId(mac, vlan);
     private HostDescription hostDescription;
@@ -61,12 +62,12 @@
         // Initialize test variables
         ips.add(IpAddress.valueOf("10.0.0.1"));
         ips.add(IpAddress.valueOf("192.168.0.1"));
-        hostDescription = new DefaultHostDescription(mac, vlan, hloc, ips);
+        hostDescription = new DefaultHostDescription(mac, vlan, locations, ips, true);
     }
 
     @Test
     public void testAddHost() throws Exception {
-        provider.addHost(mac, vlan, hloc, ips);
+        provider.addHost(mac, vlan, locations, ips);
         assertThat(providerService.hostId, is(hostId));
         assertThat(providerService.hostDescription, is(hostDescription));
         assertThat(providerService.event, is("hostDetected"));
@@ -75,7 +76,7 @@
 
     @Test
     public void testUpdateHost() throws Exception {
-        provider.updateHost(mac, vlan, hloc, ips);
+        provider.updateHost(mac, vlan, locations, ips);
         assertThat(providerService.hostId, is(hostId));
         assertThat(providerService.hostDescription, is(hostDescription));
         assertThat(providerService.event, is("hostDetected"));