Fix DHCP handling bug in HostLocationProvider

Change-Id: I69beb697736aec4540d2dfd0ba168141ab466ccf
diff --git a/providers/host/src/main/java/org/onosproject/provider/host/impl/HostLocationProvider.java b/providers/host/src/main/java/org/onosproject/provider/host/impl/HostLocationProvider.java
index e197a0d..461319a 100644
--- a/providers/host/src/main/java/org/onosproject/provider/host/impl/HostLocationProvider.java
+++ b/providers/host/src/main/java/org/onosproject/provider/host/impl/HostLocationProvider.java
@@ -475,9 +475,22 @@
 
             // IPv4: update location only
             } else if (eth.getEtherType() == Ethernet.TYPE_IPV4) {
-                // DHCP ACK: additionally update IP of DHCP client
-                Optional<DHCP> dhcp = findDhcp(eth);
-                if (useDhcp || !dhcp.isPresent()) {
+                DHCP dhcp = findDhcp(eth).orElse(null);
+                if (dhcp != null) {
+                    if (useDhcp) {
+                        // learn host (server or client) MAC address
+                        createOrUpdateHost(hid, srcMac, vlan, hloc, null);
+
+                        // DHCP ACK: additionally update IP of DHCP client
+                        if (dhcp.getPacketType().equals(DHCP.MsgType.DHCPACK)) {
+                            MacAddress hostMac = MacAddress.valueOf(dhcp.getClientHardwareAddress());
+                            VlanId hostVlan = VlanId.vlanId(eth.getVlanID());
+                            HostId hostId = HostId.hostId(hostMac, hostVlan);
+                            updateHostIp(hostId, IpAddress.valueOf(dhcp.getYourIPAddress()));
+                        }
+                    }
+                } else {
+                    // learn host MAC address
                     createOrUpdateHost(hid, srcMac, vlan, hloc, null);
                 }
             //
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 080e0e4..b43b50f 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
@@ -371,7 +371,7 @@
         // DHCP Ack
         testProcessor.process(new TestDhcpAckPacketContext(DEV1, false));
         assertThat("receiveDhcpAck. Two additional host descriptions expected",
-                providerService.descriptions.size(), is(2));
+                providerService.descriptions.size(), is(3));
 
         // Should also learn the MAC, location of DHCP server
         HostDescription descr2 = providerService.descriptions.get(1);
@@ -380,7 +380,14 @@
         assertThat(descr2.ipAddress().size(), is(0));
         assertThat(descr2.vlan(), is(VLAN));
 
-        // Should not update the IP address of the host.
+        // Should update the IP address of the client.
+        HostDescription descr3 = providerService.descriptions.get(2);
+        assertThat(descr3.location(), is(LOCATION));
+        assertThat(descr3.hwAddress(), is(MAC));
+        assertThat(descr3.ipAddress().size(), is(1));
+        IpAddress ip = descr3.ipAddress().iterator().next();
+        assertThat(ip, is(IP_ADDRESS.getIp4Address()));
+        assertThat(descr3.vlan(), is(VLAN));
     }
 
     /**