ONOS-2309: Add check in HostMonitor to make sure we only send out edge ports

Change-Id: Id128319163786487c5b545fbc51e47097edfe0ab
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 9926338..26b96eb 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
@@ -25,6 +25,7 @@
 import org.onlab.packet.MacAddress;
 import org.onlab.packet.VlanId;
 import org.onosproject.incubator.net.intf.InterfaceService;
+import org.onosproject.net.edge.EdgePortService;
 import org.onosproject.net.provider.AbstractListenerProviderRegistry;
 import org.onosproject.net.config.NetworkConfigEvent;
 import org.onosproject.net.config.NetworkConfigListener;
@@ -90,6 +91,9 @@
     @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
     protected InterfaceService interfaceService;
 
+    @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
+    protected EdgePortService edgePortService;
+
     private HostMonitor monitor;
 
     @Activate
@@ -97,7 +101,7 @@
         store.setDelegate(delegate);
         eventDispatcher.addSink(HostEvent.class, listenerRegistry);
         networkConfigService.addListener(networkConfigListener);
-        monitor = new HostMonitor(packetService, this, interfaceService);
+        monitor = new HostMonitor(packetService, this, interfaceService, edgePortService);
         monitor.start();
         log.info("Started");
     }
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 d437bf3..44f8cbf 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
@@ -31,6 +31,7 @@
 import org.onosproject.incubator.net.intf.InterfaceService;
 import org.onosproject.net.ConnectPoint;
 import org.onosproject.net.Host;
+import org.onosproject.net.edge.EdgePortService;
 import org.onosproject.net.flow.DefaultTrafficTreatment;
 import org.onosproject.net.flow.TrafficTreatment;
 import org.onosproject.net.host.HostProvider;
@@ -39,6 +40,8 @@
 import org.onosproject.net.packet.OutboundPacket;
 import org.onosproject.net.packet.PacketService;
 import org.onosproject.net.provider.ProviderId;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
 import java.nio.ByteBuffer;
 import java.util.Collections;
@@ -56,9 +59,13 @@
  * </p>
  */
 public class HostMonitor implements TimerTask {
+
+    private Logger log = LoggerFactory.getLogger(getClass());
+
     private PacketService packetService;
     private HostManager hostManager;
     private InterfaceService interfaceService;
+    private EdgePortService edgePortService;
 
     private final Set<IpAddress> monitoredAddresses;
 
@@ -79,11 +86,13 @@
      * @param interfaceService interface service for interface information
      */
     public HostMonitor(PacketService packetService, HostManager hostManager,
-                       InterfaceService interfaceService) {
+                       InterfaceService interfaceService,
+                       EdgePortService edgePortService) {
 
         this.packetService = packetService;
         this.hostManager = hostManager;
         this.interfaceService = interfaceService;
+        this.edgePortService = edgePortService;
 
         monitoredAddresses = Collections.newSetFromMap(new ConcurrentHashMap<>());
         hostProviders = new ConcurrentHashMap<>();
@@ -173,6 +182,11 @@
             return;
         }
 
+        if (!edgePortService.isEdgePoint(intf.connectPoint())) {
+            log.warn("Attempt to send probe out non-edge port: {}", intf);
+            return;
+        }
+
         for (InterfaceIpAddress ia : intf.ipAddresses()) {
             if (ia.subnetAddress().contains(targetIp)) {
                 sendProbe(intf.connectPoint(), targetIp, ia.ipAddress(),
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 d6ff473..4385ac2 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
@@ -19,6 +19,7 @@
 import com.google.common.collect.Lists;
 import com.google.common.collect.Multimap;
 import org.junit.After;
+import org.junit.Before;
 import org.junit.Test;
 import org.onlab.packet.ARP;
 import org.onlab.packet.Ethernet;
@@ -37,6 +38,7 @@
 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;
 import org.onosproject.net.flow.instructions.Instructions.OutputInstruction;
 import org.onosproject.net.host.HostProvider;
@@ -51,6 +53,7 @@
 import java.util.List;
 import java.util.Set;
 
+import static org.easymock.EasyMock.anyObject;
 import static org.easymock.EasyMock.createMock;
 import static org.easymock.EasyMock.expect;
 import static org.easymock.EasyMock.expectLastCall;
@@ -70,8 +73,18 @@
             new InterfaceIpAddress(SOURCE_ADDR, IpPrefix.valueOf("10.0.0.0/24"));
     private MacAddress sourceMac = MacAddress.valueOf(1L);
 
+    private EdgePortService edgePortService;
+
     private HostMonitor hostMonitor;
 
+    @Before
+    public void setUp() {
+        edgePortService = createMock(EdgePortService.class);
+        expect(edgePortService.isEdgePoint(anyObject(ConnectPoint.class)))
+                .andReturn(true).anyTimes();
+        replay(edgePortService);
+    }
+
     @After
     public void shutdown() {
         hostMonitor.shutdown();
@@ -96,7 +109,7 @@
         expectLastCall().once();
         replay(hostProvider);
 
-        hostMonitor = new HostMonitor(null, hostManager, null);
+        hostMonitor = new HostMonitor(null, hostManager, null, edgePortService);
 
         hostMonitor.registerHostProvider(hostProvider);
         hostMonitor.addMonitoringFor(TARGET_IP_ADDR);
@@ -144,7 +157,7 @@
 
 
         // Run the test
-        hostMonitor = new HostMonitor(packetService, hostManager, interfaceService);
+        hostMonitor = new HostMonitor(packetService, hostManager, interfaceService, edgePortService);
 
         hostMonitor.addMonitoringFor(TARGET_IP_ADDR);
         hostMonitor.run(null);
@@ -216,7 +229,7 @@
 
 
         // Run the test
-        hostMonitor = new HostMonitor(packetService, hostManager, interfaceService);
+        hostMonitor = new HostMonitor(packetService, hostManager, interfaceService, edgePortService);
 
         hostMonitor.addMonitoringFor(TARGET_IP_ADDR);
         hostMonitor.run(null);