Fix for ONOS-5033 hosts - dynamic or static

Change-Id: I3791370db0037968003abc23c918c63119d2dba2
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;