Probe immediately when IPs are added to host monitor

rather than waiting for the next polling cycle.

Change-Id: Iffaf50f7a589b52be659b82b8a289e04a5de4ca6
diff --git a/core/net/src/main/java/org/onosproject/net/host/impl/HostMonitor.java b/core/net/src/main/java/org/onosproject/net/host/impl/HostMonitor.java
index 0d2d373..c2bac09 100644
--- a/core/net/src/main/java/org/onosproject/net/host/impl/HostMonitor.java
+++ b/core/net/src/main/java/org/onosproject/net/host/impl/HostMonitor.java
@@ -107,6 +107,7 @@
      */
     void addMonitoringFor(IpAddress ip) {
         monitoredAddresses.add(ip);
+        probe(ip);
     }
 
     /**
@@ -151,28 +152,30 @@
 
     @Override
     public void run(Timeout timeout) throws Exception {
-        for (IpAddress ip : monitoredAddresses) {
-            Set<Host> hosts = hostManager.getHostsByIp(ip);
-
-            if (hosts.isEmpty()) {
-                sendRequest(ip);
-            } else {
-                for (Host host : hosts) {
-                    HostProvider provider = hostProviders.get(host.providerId());
-                    if (provider == null) {
-                        hostProviders.remove(host.providerId(), null);
-                    } else {
-                        provider.triggerProbe(host);
-                    }
-                }
-            }
-        }
+        monitoredAddresses.forEach(this::probe);
 
         synchronized (this) {
             this.timeout = Timer.getTimer().newTimeout(this, probeRate, TimeUnit.MILLISECONDS);
         }
     }
 
+    private void probe(IpAddress ip) {
+        Set<Host> hosts = hostManager.getHostsByIp(ip);
+
+        if (hosts.isEmpty()) {
+            sendRequest(ip);
+        } else {
+            for (Host host : hosts) {
+                HostProvider provider = hostProviders.get(host.providerId());
+                if (provider == null) {
+                    hostProviders.remove(host.providerId(), null);
+                } else {
+                    provider.triggerProbe(host);
+                }
+            }
+        }
+    }
+
     /**
      * Sends an ARP or NDP request for the given IP address.
      *
diff --git a/core/net/src/test/java/org/onosproject/net/host/impl/HostMonitorTest.java b/core/net/src/test/java/org/onosproject/net/host/impl/HostMonitorTest.java
index d74023b..21bec18 100644
--- a/core/net/src/test/java/org/onosproject/net/host/impl/HostMonitorTest.java
+++ b/core/net/src/test/java/org/onosproject/net/host/impl/HostMonitorTest.java
@@ -35,10 +35,8 @@
 import org.onosproject.net.Device;
 import org.onosproject.net.DeviceId;
 import org.onosproject.net.Host;
-import org.onosproject.net.MastershipRole;
 import org.onosproject.net.Port;
 import org.onosproject.net.PortNumber;
-import org.onosproject.net.device.DeviceListener;
 import org.onosproject.net.device.DeviceServiceAdapter;
 import org.onosproject.net.edge.EdgePortService;
 import org.onosproject.net.flow.instructions.Instruction;
@@ -101,56 +99,37 @@
 
     @Test
     public void testMonitorIpv4HostExists() throws Exception {
-        ProviderId id = new ProviderId("fake://", "id");
-
-        Host host = createMock(Host.class);
-        expect(host.providerId()).andReturn(id);
-        replay(host);
-
-        HostManager hostManager = createMock(HostManager.class);
-        expect(hostManager.getHostsByIp(TARGET_IPV4_ADDR))
-                .andReturn(Collections.singleton(host));
-        replay(hostManager);
-
-        HostProvider hostProvider = createMock(HostProvider.class);
-        expect(hostProvider.id()).andReturn(id).anyTimes();
-        hostProvider.triggerProbe(host);
-        expectLastCall().once();
-        replay(hostProvider);
-
-        hostMonitor = new HostMonitor(null, hostManager, null, edgePortService);
-
-        hostMonitor.registerHostProvider(hostProvider);
-        hostMonitor.addMonitoringFor(TARGET_IPV4_ADDR);
-
-        hostMonitor.run(null);
-
-        verify(hostProvider);
+        testMonitorHostExists(TARGET_IPV4_ADDR);
     }
 
     @Test
     public void testMonitorIpv6HostExists() throws Exception {
+        testMonitorHostExists(TARGET_IPV6_ADDR);
+    }
+
+    private void testMonitorHostExists(IpAddress hostIp) throws Exception {
         ProviderId id = new ProviderId("fake://", "id");
 
         Host host = createMock(Host.class);
-        expect(host.providerId()).andReturn(id);
+        expect(host.providerId()).andReturn(id).anyTimes();
         replay(host);
 
         HostManager hostManager = createMock(HostManager.class);
-        expect(hostManager.getHostsByIp(TARGET_IPV6_ADDR))
-                .andReturn(Collections.singleton(host));
+        expect(hostManager.getHostsByIp(hostIp))
+                .andReturn(Collections.singleton(host))
+                .anyTimes();
         replay(hostManager);
 
         HostProvider hostProvider = createMock(HostProvider.class);
         expect(hostProvider.id()).andReturn(id).anyTimes();
         hostProvider.triggerProbe(host);
-        expectLastCall().once();
+        expectLastCall().times(2);
         replay(hostProvider);
 
         hostMonitor = new HostMonitor(null, hostManager, null, edgePortService);
 
         hostMonitor.registerHostProvider(hostProvider);
-        hostMonitor.addMonitoringFor(TARGET_IPV6_ADDR);
+        hostMonitor.addMonitoringFor(hostIp);
 
         hostMonitor.run(null);
 
@@ -202,7 +181,7 @@
 
         // Check that a packet was sent to our PacketService and that it has
         // the properties we expect
-        assertEquals(1, packetService.packets.size());
+        assertEquals(2, packetService.packets.size());
         OutboundPacket packet = packetService.packets.get(0);
 
         // Check the output port is correct
@@ -271,7 +250,7 @@
 
         // Check that a packet was sent to our PacketService and that it has
         // the properties we expect
-        assertEquals(1, packetService.packets.size());
+        assertEquals(2, packetService.packets.size());
         OutboundPacket packet = packetService.packets.get(0);
 
         // Check the output port is correct
@@ -342,7 +321,7 @@
 
         // Check that a packet was sent to our PacketService and that it has
         // the properties we expect
-        assertEquals(1, packetService.packets.size());
+        assertEquals(2, packetService.packets.size());
         OutboundPacket packet = packetService.packets.get(0);
 
         // Check the output port is correct
@@ -412,7 +391,7 @@
 
         // Check that a packet was sent to our PacketService and that it has
         // the properties we expect
-        assertEquals(1, packetService.packets.size());
+        assertEquals(2, packetService.packets.size());
         OutboundPacket packet = packetService.packets.get(0);
 
         // Check the output port is correct
@@ -460,26 +439,11 @@
         }
 
         @Override
-        public int getDeviceCount() {
-            return 0;
-        }
-
-        @Override
         public Iterable<Device> getDevices() {
             return devices;
         }
 
         @Override
-        public Device getDevice(DeviceId deviceId) {
-            return null;
-        }
-
-        @Override
-        public MastershipRole getRole(DeviceId deviceId) {
-            return null;
-        }
-
-        @Override
         public List<Port> getPorts(DeviceId deviceId) {
             List<Port> ports = Lists.newArrayList();
             for (Port p : devicePorts.get(deviceId)) {
@@ -487,23 +451,5 @@
             }
             return ports;
         }
-
-        @Override
-        public Port getPort(DeviceId deviceId, PortNumber portNumber) {
-            return null;
-        }
-
-        @Override
-        public boolean isAvailable(DeviceId deviceId) {
-            return false;
-        }
-
-        @Override
-        public void addListener(DeviceListener listener) {
-        }
-
-        @Override
-        public void removeListener(DeviceListener listener) {
-        }
     }
 }