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

Change-Id: Ifea3366ce8a18ea068e615636b3069e769221c0e
diff --git a/apps/dhcp/src/main/java/org/onosproject/dhcp/DhcpStore.java b/apps/dhcp/src/main/java/org/onosproject/dhcp/DhcpStore.java
index c9fade9..5615af1 100644
--- a/apps/dhcp/src/main/java/org/onosproject/dhcp/DhcpStore.java
+++ b/apps/dhcp/src/main/java/org/onosproject/dhcp/DhcpStore.java
@@ -65,7 +65,7 @@
      *
      * @param hostId the host ID for which the mapping needs to be changed
      */
-    void releaseIP(HostId hostId);
+    Ip4Address releaseIP(HostId hostId);
 
     /**
      * Returns a collection of all the MacAddress to IPAddress mapping assigned to the hosts.
diff --git a/apps/dhcp/src/main/java/org/onosproject/dhcp/impl/DhcpManager.java b/apps/dhcp/src/main/java/org/onosproject/dhcp/impl/DhcpManager.java
index 345d5ad..153463a 100644
--- a/apps/dhcp/src/main/java/org/onosproject/dhcp/impl/DhcpManager.java
+++ b/apps/dhcp/src/main/java/org/onosproject/dhcp/impl/DhcpManager.java
@@ -481,7 +481,10 @@
                         }
                     }
                 } else if (incomingPacketType.getValue() == DHCPPacketType.DHCPRELEASE.getValue()) {
-                    dhcpStore.releaseIP(hostId);
+                    Ip4Address ip4Address = dhcpStore.releaseIP(hostId);
+                    if (ip4Address != null) {
+                        hostProviderService.removeIpFromHost(hostId, ip4Address);
+                    }
                 }
             }
         }
@@ -666,9 +669,10 @@
                 if ((ipAssignment.assignmentStatus() != IpAssignment.AssignmentStatus.Option_Expired) &&
                         (ipAssignment.leasePeriod() > 0) && (timeLapsed > (ipAssignment.leasePeriodMs()))) {
 
-                    dhcpStore.releaseIP(entry.getKey());
-                    // TODO remove only the IP from the host entry when the API is in place.
-                    hostProviderService.hostVanished(entry.getKey());
+                    Ip4Address ip4Address = dhcpStore.releaseIP(entry.getKey());
+                    if (ip4Address != null) {
+                        hostProviderService.removeIpFromHost(entry.getKey(), ipAssignment.ipAddress());
+                    }
                 }
             }
             timeout = Timer.getTimer().newTimeout(new PurgeListTask(), timerDelay, TimeUnit.MINUTES);
diff --git a/apps/dhcp/src/main/java/org/onosproject/dhcp/impl/DistributedDhcpStore.java b/apps/dhcp/src/main/java/org/onosproject/dhcp/impl/DistributedDhcpStore.java
index dbdadb3..63f69d4 100644
--- a/apps/dhcp/src/main/java/org/onosproject/dhcp/impl/DistributedDhcpStore.java
+++ b/apps/dhcp/src/main/java/org/onosproject/dhcp/impl/DistributedDhcpStore.java
@@ -212,7 +212,7 @@
     }
 
     @Override
-    public void releaseIP(HostId hostId) {
+    public Ip4Address releaseIP(HostId hostId) {
         if (allocationMap.containsKey(hostId)) {
             IpAssignment newAssignment = IpAssignment.builder(allocationMap.get(hostId).value())
                                                     .assignmentStatus(IpAssignment.AssignmentStatus.Option_Expired)
@@ -222,7 +222,9 @@
             if (ipWithinRange(freeIP)) {
                 freeIPPool.add(freeIP);
             }
+            return freeIP;
         }
+        return null;
     }
 
     @Override
diff --git a/apps/dhcp/src/test/java/org/onosproject/dhcp/impl/DhcpManagerTest.java b/apps/dhcp/src/test/java/org/onosproject/dhcp/impl/DhcpManagerTest.java
index 20b002a..fd4701c 100644
--- a/apps/dhcp/src/test/java/org/onosproject/dhcp/impl/DhcpManagerTest.java
+++ b/apps/dhcp/src/test/java/org/onosproject/dhcp/impl/DhcpManagerTest.java
@@ -25,6 +25,7 @@
 import org.onlab.packet.Ethernet;
 import org.onlab.packet.IPv4;
 import org.onlab.packet.Ip4Address;
+import org.onlab.packet.IpAddress;
 import org.onlab.packet.MacAddress;
 import org.onlab.packet.UDP;
 import org.onosproject.core.CoreServiceAdapter;
@@ -234,7 +235,8 @@
         public void setDefaultTimeoutForPurge(int timeInSeconds) {
         }
 
-        public void releaseIP(HostId hostId) {
+        public Ip4Address releaseIP(HostId hostId) {
+            return null;
         }
 
         public Map<HostId, IpAssignment> listAssignedMapping() {
@@ -338,6 +340,11 @@
         public void hostVanished(HostId hostId) {
         }
 
+        @Override
+        public void removeIpFromHost(HostId hostId, IpAddress ipAddress) {
+
+        }
+
     }
 
     /**
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();
     }
diff --git a/providers/host/src/test/java/org/onosproject/provider/host/impl/HostLocationProviderTest.java b/providers/host/src/test/java/org/onosproject/provider/host/impl/HostLocationProviderTest.java
index d010f17..db1eb0f 100644
--- a/providers/host/src/test/java/org/onosproject/provider/host/impl/HostLocationProviderTest.java
+++ b/providers/host/src/test/java/org/onosproject/provider/host/impl/HostLocationProviderTest.java
@@ -259,6 +259,10 @@
             removeCount++;
         }
 
+        @Override
+        public void removeIpFromHost(HostId hostId, IpAddress ipAddress) {
+        }
+
     }
 
     private class TestPacketService extends PacketServiceAdapter {
diff --git a/providers/ovsdb/host/src/test/java/org/onosproject/ovsdb/provider/host/OvsdbHostProviderTest.java b/providers/ovsdb/host/src/test/java/org/onosproject/ovsdb/provider/host/OvsdbHostProviderTest.java
index ad720c8..019cfa1 100644
--- a/providers/ovsdb/host/src/test/java/org/onosproject/ovsdb/provider/host/OvsdbHostProviderTest.java
+++ b/providers/ovsdb/host/src/test/java/org/onosproject/ovsdb/provider/host/OvsdbHostProviderTest.java
@@ -24,6 +24,7 @@
 import org.junit.After;
 import org.junit.Before;
 import org.junit.Test;
+import org.onlab.packet.IpAddress;
 import org.onlab.packet.MacAddress;
 import org.onosproject.net.DeviceId;
 import org.onosproject.net.HostId;
@@ -159,6 +160,11 @@
             removeCount++;
         }
 
+        @Override
+        public void removeIpFromHost(HostId hostId, IpAddress ipAddress) {
+
+        }
+
     }
 
     private class OvsdbControllerTest implements OvsdbController {