Fix for ONOS-5033 hosts - dynamic or static

Change-Id: I3791370db0037968003abc23c918c63119d2dba2
diff --git a/cli/src/main/java/org/onosproject/cli/net/HostsListCommand.java b/cli/src/main/java/org/onosproject/cli/net/HostsListCommand.java
index d337452..3ec3ce3 100644
--- a/cli/src/main/java/org/onosproject/cli/net/HostsListCommand.java
+++ b/cli/src/main/java/org/onosproject/cli/net/HostsListCommand.java
@@ -38,7 +38,7 @@
 public class HostsListCommand extends AbstractShellCommand {
 
     private static final String FMT =
-            "id=%s, mac=%s, location=%s/%s, vlan=%s, ip(s)=%s%s";
+            "id=%s, mac=%s, location=%s/%s, vlan=%s, ip(s)=%s%s, configured=%s";
 
     private static final String FMT_SHORT =
             "id=%s, mac=%s, location=%s/%s, vlan=%s, ip(s)=%s";
@@ -93,7 +93,9 @@
         } else {
             print(FMT, host.id(), host.mac(),
                   host.location().deviceId(), host.location().port(),
-                  host.vlan(), host.ipAddresses(), annotations(host.annotations()));
+                  host.vlan(), host.ipAddresses(), annotations(host.annotations()),
+                  host.configured());
         }
     }
 }
+
diff --git a/core/api/src/main/java/org/onosproject/net/DefaultHost.java b/core/api/src/main/java/org/onosproject/net/DefaultHost.java
index 38a8e0d..7fc77c3 100644
--- a/core/api/src/main/java/org/onosproject/net/DefaultHost.java
+++ b/core/api/src/main/java/org/onosproject/net/DefaultHost.java
@@ -36,6 +36,7 @@
     private final VlanId vlan;
     private final HostLocation location;
     private final Set<IpAddress> ips;
+    private final boolean configured;
 
     /**
      * Creates an end-station host using the supplied information.
@@ -51,11 +52,30 @@
     public DefaultHost(ProviderId providerId, HostId id, MacAddress mac,
                        VlanId vlan, HostLocation location, Set<IpAddress> ips,
                        Annotations... annotations) {
+        this(providerId, id, mac, vlan, location, ips, false, annotations);
+    }
+
+    /**
+     * Creates an end-station host using the supplied information.
+     *
+     * @param providerId  provider identity
+     * @param id          host identifier
+     * @param mac         host MAC address
+     * @param vlan        host VLAN identifier
+     * @param location    host location
+     * @param ips         host IP addresses
+     * @param configured  true if configured via NetworkConfiguration
+     * @param annotations optional key/value annotations
+     */
+    public DefaultHost(ProviderId providerId, HostId id, MacAddress mac,
+                       VlanId vlan, HostLocation location, Set<IpAddress> ips,
+                       boolean configured, Annotations... annotations) {
         super(providerId, id, annotations);
         this.mac = mac;
         this.vlan = vlan;
         this.location = location;
         this.ips = new HashSet<>(ips);
+        this.configured = configured;
     }
 
     @Override
@@ -84,6 +104,11 @@
     }
 
     @Override
+    public boolean configured() {
+        return configured;
+    }
+
+    @Override
     public int hashCode() {
         return Objects.hash(id, mac, vlan, location);
     }
@@ -114,6 +139,7 @@
                 .add("location", location())
                 .add("ipAddresses", ipAddresses())
                 .add("annotations", annotations())
+                .add("configured", configured())
                 .toString();
     }
 
diff --git a/core/api/src/main/java/org/onosproject/net/Host.java b/core/api/src/main/java/org/onosproject/net/Host.java
index b9621b7..5094727 100644
--- a/core/api/src/main/java/org/onosproject/net/Host.java
+++ b/core/api/src/main/java/org/onosproject/net/Host.java
@@ -63,6 +63,14 @@
      */
     HostLocation location();
 
+    /**
+     * Returns true if configured by NetworkConfiguration.
+     * @return configured/learnt dynamically
+     */
+    default boolean configured() {
+        return false;
+    }
     // TODO: explore capturing list of recent locations to aid in mobility
 
 }
+
diff --git a/core/api/src/main/java/org/onosproject/net/host/DefaultHostDescription.java b/core/api/src/main/java/org/onosproject/net/host/DefaultHostDescription.java
index 3503eca..5f9cfd8 100644
--- a/core/api/src/main/java/org/onosproject/net/host/DefaultHostDescription.java
+++ b/core/api/src/main/java/org/onosproject/net/host/DefaultHostDescription.java
@@ -40,6 +40,7 @@
     private final VlanId vlan;
     private final HostLocation location;
     private final Set<IpAddress> ip;
+    private final boolean configured;
 
     /**
      * Creates a host description using the supplied information.
@@ -83,11 +84,46 @@
     public DefaultHostDescription(MacAddress mac, VlanId vlan,
                                   HostLocation location, Set<IpAddress> ip,
                                   SparseAnnotations... annotations) {
+        this(mac, vlan, location, ip, false, annotations);
+    }
+
+    /**
+     * Creates a host description using the supplied information.
+     *
+     * @param mac          host MAC address
+     * @param vlan         host VLAN identifier
+     * @param location     host location
+     * @param configured   true if configured via NetworkConfiguration
+     * @param annotations  optional key/value annotations map
+     */
+    public DefaultHostDescription(MacAddress mac, VlanId vlan,
+                                  HostLocation location,
+                                  boolean configured,
+                                  SparseAnnotations... annotations) {
+        this(mac, vlan, location, Collections.<IpAddress>emptySet(),
+             configured, annotations);
+    }
+
+    /**
+     * Creates a host description using the supplied information.
+     *
+     * @param mac          host MAC address
+     * @param vlan         host VLAN identifier
+     * @param location     host location
+     * @param ip           host IP address
+     * @param configured   true if configured via NetworkConfiguration
+     * @param annotations  optional key/value annotations map
+     */
+    public DefaultHostDescription(MacAddress mac, VlanId vlan,
+                                  HostLocation location, Set<IpAddress> ip,
+                                  boolean configured,
+                                  SparseAnnotations... annotations) {
         super(annotations);
         this.mac = mac;
         this.vlan = vlan;
         this.location = location;
         this.ip = ImmutableSet.copyOf(ip);
+        this.configured = configured;
     }
 
     @Override
@@ -111,12 +147,18 @@
     }
 
     @Override
+    public boolean configured() {
+        return configured;
+    }
+
+    @Override
     public String toString() {
         return toStringHelper(this)
                 .add("mac", mac)
                 .add("vlan", vlan)
                 .add("location", location)
                 .add("ipAddress", ip)
+                .add("configured", configured)
                 .toString();
     }
 
@@ -139,5 +181,4 @@
         }
         return false;
     }
-
 }
diff --git a/core/api/src/main/java/org/onosproject/net/host/HostDescription.java b/core/api/src/main/java/org/onosproject/net/host/HostDescription.java
index ad423a3..d7687ac 100644
--- a/core/api/src/main/java/org/onosproject/net/host/HostDescription.java
+++ b/core/api/src/main/java/org/onosproject/net/host/HostDescription.java
@@ -55,4 +55,12 @@
      * @return host IP address
      */
     Set<IpAddress> ipAddress();
+
+    /**
+     * Returns true if configured by NetworkConfiguration.
+     * @return configured/learnt dynamically
+     */
+    default boolean configured() {
+        return false;
+    }
 }
diff --git a/core/common/src/main/java/org/onosproject/codec/impl/HostCodec.java b/core/common/src/main/java/org/onosproject/codec/impl/HostCodec.java
index 8bd27c7..b5f9b02 100644
--- a/core/common/src/main/java/org/onosproject/codec/impl/HostCodec.java
+++ b/core/common/src/main/java/org/onosproject/codec/impl/HostCodec.java
@@ -39,7 +39,8 @@
         final ObjectNode result = context.mapper().createObjectNode()
                 .put("id", host.id().toString())
                 .put("mac", host.mac().toString())
-                .put("vlan", host.vlan().toString());
+                .put("vlan", host.vlan().toString())
+                .put("configured", host.configured());
 
         final ArrayNode jsonIpAddresses = result.putArray("ipAddresses");
         for (final IpAddress ipAddress : host.ipAddresses()) {
diff --git a/core/net/src/main/java/org/onosproject/net/host/impl/BasicHostOperator.java b/core/net/src/main/java/org/onosproject/net/host/impl/BasicHostOperator.java
index 255ee46..3c60240 100644
--- a/core/net/src/main/java/org/onosproject/net/host/impl/BasicHostOperator.java
+++ b/core/net/src/main/java/org/onosproject/net/host/impl/BasicHostOperator.java
@@ -64,7 +64,7 @@
 
         SparseAnnotations sa = combine(cfg, descr.annotations());
         return new DefaultHostDescription(descr.hwAddress(), descr.vlan(),
-                                          location, ipAddresses, sa);
+                                          location, ipAddresses, descr.configured(), sa);
     }
 
     /**
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 b4449ae..356df9c 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
@@ -167,11 +167,14 @@
                            }
 
                            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,
@@ -180,6 +183,7 @@
                                                   hostDescription.vlan(),
                                                   location,
                                                   addresses,
+                                                  configured,
                                                   annotations);
                        });
         return null;
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 c3c8161..5e7c4e9 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
@@ -112,8 +112,8 @@
     protected void addHost(MacAddress mac, VlanId vlan, HostLocation hloc, Set<IpAddress> ips) {
         HostId hid = HostId.hostId(mac, vlan);
         HostDescription desc = (ips != null) ?
-                new DefaultHostDescription(mac, vlan, hloc, ips) :
-                new DefaultHostDescription(mac, vlan, hloc);
+                new DefaultHostDescription(mac, vlan, hloc, ips, true) :
+                new DefaultHostDescription(mac, vlan, hloc, true);
         providerService.hostDetected(hid, desc, false);
     }
 
@@ -128,7 +128,7 @@
      */
     protected void updateHost(MacAddress mac, VlanId vlan, HostLocation hloc, Set<IpAddress> ips) {
         HostId hid = HostId.hostId(mac, vlan);
-        HostDescription desc = new DefaultHostDescription(mac, vlan, hloc, ips);
+        HostDescription desc = new DefaultHostDescription(mac, vlan, hloc, ips, true);
         providerService.hostDetected(hid, desc, true);
     }
 
diff --git a/web/api/src/main/java/org/onosproject/rest/resources/HostsWebResource.java b/web/api/src/main/java/org/onosproject/rest/resources/HostsWebResource.java
index c3a1b51..350fe79 100644
--- a/web/api/src/main/java/org/onosproject/rest/resources/HostsWebResource.java
+++ b/web/api/src/main/java/org/onosproject/rest/resources/HostsWebResource.java
@@ -214,7 +214,7 @@
             // Update host inventory
 
             HostId hostId = HostId.hostId(mac, vlanId);
-            DefaultHostDescription desc = new DefaultHostDescription(mac, vlanId, hostLocation, ips, annotations);
+            DefaultHostDescription desc = new DefaultHostDescription(mac, vlanId, hostLocation, ips, true, annotations);
             hostProviderService.hostDetected(hostId, desc, false);
             return hostId;
         }
diff --git a/web/api/src/test/java/org/onosproject/rest/resources/HostResourceTest.java b/web/api/src/test/java/org/onosproject/rest/resources/HostResourceTest.java
index 1f992c9..072ca33 100644
--- a/web/api/src/test/java/org/onosproject/rest/resources/HostResourceTest.java
+++ b/web/api/src/test/java/org/onosproject/rest/resources/HostResourceTest.java
@@ -198,7 +198,7 @@
         @Override
         public boolean matchesSafely(JsonArray json) {
             boolean hostFound = false;
-            final int expectedAttributes = 5;
+            final int expectedAttributes = 6;
             for (int jsonHostIndex = 0; jsonHostIndex < json.size();
                  jsonHostIndex++) {