Fix bugs in host subsystem

- Configured flag is not updated when a learnt host is overridden by a configured host
- NetworkConfigHostProvider should override IP addresses provided by HostLocationProvider
- Adding some unit tests

Change-Id: Id764af6acfeb5fa8f02b5a17aadf686bd6ac97fb
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 76d14e7..4e4c9fa 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
@@ -204,14 +204,11 @@
                            }
 
                            final Annotations annotations;
-                           final boolean configured;
                            if (existingHost != null) {
                                annotations = merge((DefaultAnnotations) existingHost.annotations(),
                                        hostDescription.annotations());
-                               configured = existingHost.configured();
                            } else {
                                annotations = hostDescription.annotations();
-                               configured = hostDescription.configured();
                            }
 
                            return new DefaultHost(providerId,
@@ -220,7 +217,7 @@
                                                   hostDescription.vlan(),
                                                   hostDescription.locations(),
                                                   addresses,
-                                                  configured,
+                                                  hostDescription.configured(),
                                                   annotations);
                        });
         return null;
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 99f2947..fa61a19 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
@@ -34,6 +34,7 @@
 import java.util.Set;
 
 import static junit.framework.TestCase.assertTrue;
+import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertFalse;
 
 /**
@@ -50,6 +51,12 @@
     private static final IpAddress IP2 = IpAddress.valueOf("10.2.0.3");
 
     private static final ProviderId PID = new ProviderId("of", "foo");
+    private static final ProviderId PID2 = new ProviderId("of", "foo2");
+
+    private static final HostDescription HOST_LEARNT =
+            createHostDesc(HOSTID, Sets.newHashSet(IP1), false);
+    private static final HostDescription HOST_CONFIGURED =
+            createHostDesc(HOSTID, Sets.newHashSet(IP1), true);
 
     @Before
     public void setUp() {
@@ -118,11 +125,37 @@
         assertTrue(hosts.size() == 0);
     }
 
-    private HostDescription createHostDesc(HostId hostId, Set<IpAddress> ips) {
+    @Test
+    public void testHostOverride() {
+        Host hostInStore;
+        ecXHostStore.createOrUpdateHost(PID, HOSTID, HOST_LEARNT, false);
+        hostInStore = ecXHostStore.getHost(HOSTID);
+        assertFalse(hostInStore.configured());
+        assertEquals(PID, hostInStore.providerId());
+
+        // Expect: configured host should override learnt host
+        ecXHostStore.createOrUpdateHost(PID2, HOSTID, HOST_CONFIGURED, true);
+        hostInStore = ecXHostStore.getHost(HOSTID);
+        assertTrue(hostInStore.configured());
+        assertEquals(PID2, hostInStore.providerId());
+
+        // Expect: learnt host should not override configured host
+        ecXHostStore.createOrUpdateHost(PID, HOSTID, HOST_LEARNT, false);
+        hostInStore = ecXHostStore.getHost(HOSTID);
+        assertTrue(hostInStore.configured());
+        assertEquals(PID2, hostInStore.providerId());
+    }
+
+    private static HostDescription createHostDesc(HostId hostId, Set<IpAddress> ips) {
+        return createHostDesc(hostId, ips, false);
+    }
+
+    private static HostDescription createHostDesc(HostId hostId, Set<IpAddress> ips,
+                                                  boolean configured) {
         return new DefaultHostDescription(hostId.mac(),
                 hostId.vlanId(),
                 HostLocation.NONE,
-                ips);
+                ips,
+                configured);
     }
-
 }
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 59e8234..7394da8 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
@@ -116,7 +116,7 @@
         HostDescription desc = (ips != null) ?
                 new DefaultHostDescription(mac, vlan, locations, ips, true) :
                 new DefaultHostDescription(mac, vlan, locations, Collections.emptySet(), true);
-        providerService.hostDetected(hid, desc, false);
+        providerService.hostDetected(hid, desc, true);
     }
 
     /**