ONOS-2926 Remove IP instead of host when the IP mapping is released

Change-Id: Ifea3366ce8a18ea068e615636b3069e769221c0e
diff --git a/core/api/src/main/java/org/onosproject/net/host/HostProviderService.java b/core/api/src/main/java/org/onosproject/net/host/HostProviderService.java
index f7b7c49..bae9382 100644
--- a/core/api/src/main/java/org/onosproject/net/host/HostProviderService.java
+++ b/core/api/src/main/java/org/onosproject/net/host/HostProviderService.java
@@ -15,6 +15,7 @@
  */
 package org.onosproject.net.host;
 
+import org.onlab.packet.IpAddress;
 import org.onosproject.net.HostId;
 import org.onosproject.net.provider.ProviderService;
 
@@ -52,4 +53,11 @@
      */
     void hostVanished(HostId hostId);
 
+    /**
+     * Notifies the core when a host is no longer detected on a network.
+     *
+     * @param hostId id of the host that vanished
+     */
+    void removeIpFromHost(HostId hostId, IpAddress ipAddress);
+
 }
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 5894fe9..918ced4 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
@@ -55,6 +55,15 @@
     HostEvent removeHost(HostId hostId);
 
     /**
+     * Removes the specified ip from the host entry.
+     *
+     * @param hostId host identification
+     * @param ipAddress ipAddress to be removed
+     * @return remove event or null if host was not found
+     */
+    HostEvent removeIp(HostId hostId, IpAddress ipAddress);
+
+    /**
      * Returns the number of hosts in the store.
      *
      * @return host count
diff --git a/core/common/src/test/java/org/onosproject/store/trivial/SimpleHostStore.java b/core/common/src/test/java/org/onosproject/store/trivial/SimpleHostStore.java
index 264d049..72ec98c 100644
--- a/core/common/src/test/java/org/onosproject/store/trivial/SimpleHostStore.java
+++ b/core/common/src/test/java/org/onosproject/store/trivial/SimpleHostStore.java
@@ -160,6 +160,11 @@
     }
 
     @Override
+    public HostEvent removeIp(HostId hostId, IpAddress ipAddress) {
+        return null;
+    }
+
+    @Override
     public int getHostCount() {
         return hosts.size();
     }
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 43f346b..1473f33 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
@@ -236,6 +236,16 @@
                 post(event);
             }
         }
+
+        @Override
+        public void removeIpFromHost(HostId hostId, IpAddress ipAddress) {
+            checkNotNull(hostId, HOST_ID_NULL);
+            checkValidity();
+            HostEvent event = store.removeIp(hostId, ipAddress);
+            if (event != null) {
+                post(event);
+            }
+        }
     }
 
     // Store delegate to re-post events emitted from the store.
diff --git a/core/store/dist/src/main/java/org/onosproject/store/host/impl/ECHostStore.java b/core/store/dist/src/main/java/org/onosproject/store/host/impl/ECHostStore.java
index d0b827c..26c6a84 100644
--- a/core/store/dist/src/main/java/org/onosproject/store/host/impl/ECHostStore.java
+++ b/core/store/dist/src/main/java/org/onosproject/store/host/impl/ECHostStore.java
@@ -27,6 +27,7 @@
 import static org.slf4j.LoggerFactory.getLogger;
 
 import java.util.Collection;
+import java.util.HashSet;
 import java.util.Objects;
 import java.util.Set;
 import java.util.concurrent.atomic.AtomicReference;
@@ -197,6 +198,34 @@
     }
 
     @Override
+    public HostEvent removeIp(HostId hostId, IpAddress ipAddress) {
+        DefaultHost host = hosts.compute(hostId, (id, existingHost) -> {
+            if (existingHost != null) {
+                checkState(Objects.equals(hostId.mac(), existingHost.mac()),
+                        "Existing and new MAC addresses differ.");
+                checkState(Objects.equals(hostId.vlanId(), existingHost.vlan()),
+                        "Existing and new VLANs differ.");
+
+                Set<IpAddress> addresses = new HashSet<>(existingHost.ipAddresses());
+                if (addresses != null && addresses.contains(ipAddress)) {
+                    addresses.remove(ipAddress);
+                    return new DefaultHost(existingHost.providerId(),
+                            hostId,
+                            existingHost.mac(),
+                            existingHost.vlan(),
+                            existingHost.location(),
+                            ImmutableSet.copyOf(addresses),
+                            existingHost.annotations());
+                } else {
+                    return existingHost;
+                }
+            }
+            return null;
+        });
+        return host != null ? new HostEvent(HOST_UPDATED, host) : null;
+    }
+
+    @Override
     public int getHostCount() {
         return hosts.size();
     }