Fix: do not handle ARP packets from openstack events

Change-Id: Ie5abc3d3453263f785d7157b5eff9ba83be730a8
diff --git a/apps/k8s-networking/app/src/main/java/org/onosproject/k8snetworking/impl/K8sRoutingArpHandler.java b/apps/k8s-networking/app/src/main/java/org/onosproject/k8snetworking/impl/K8sRoutingArpHandler.java
index 8e80419..93bde9b 100644
--- a/apps/k8s-networking/app/src/main/java/org/onosproject/k8snetworking/impl/K8sRoutingArpHandler.java
+++ b/apps/k8s-networking/app/src/main/java/org/onosproject/k8snetworking/impl/K8sRoutingArpHandler.java
@@ -27,10 +27,12 @@
 import org.onosproject.core.ApplicationId;
 import org.onosproject.core.CoreService;
 import org.onosproject.k8snetworking.api.K8sFlowRuleService;
+import org.onosproject.k8snode.api.K8sHostAdminService;
 import org.onosproject.k8snode.api.K8sNode;
 import org.onosproject.k8snode.api.K8sNodeAdminService;
 import org.onosproject.k8snode.api.K8sNodeEvent;
 import org.onosproject.k8snode.api.K8sNodeListener;
+import org.onosproject.net.DeviceId;
 import org.onosproject.net.flow.DefaultTrafficSelector;
 import org.onosproject.net.flow.DefaultTrafficTreatment;
 import org.onosproject.net.flow.TrafficSelector;
@@ -60,6 +62,7 @@
 import static org.onosproject.k8snetworking.api.Constants.K8S_NETWORKING_APP_ID;
 import static org.onosproject.k8snetworking.api.Constants.PRIORITY_ARP_POD_RULE;
 import static org.onosproject.k8snetworking.api.Constants.PRIORITY_ARP_REPLY_RULE;
+import static org.onosproject.k8snetworking.util.K8sNetworkingUtil.allK8sDevices;
 import static org.slf4j.LoggerFactory.getLogger;
 
 /**
@@ -87,6 +90,9 @@
     protected K8sNodeAdminService k8sNodeService;
 
     @Reference(cardinality = ReferenceCardinality.MANDATORY)
+    protected K8sHostAdminService k8sHostService;
+
+    @Reference(cardinality = ReferenceCardinality.MANDATORY)
     protected K8sFlowRuleService k8sFlowRuleService;
 
     private ApplicationId appId;
@@ -118,6 +124,13 @@
     }
 
     private void processArpPacket(PacketContext context, Ethernet ethernet) {
+
+        DeviceId deviceId = context.inPacket().receivedFrom().deviceId();
+
+        if (!allK8sDevices(k8sNodeService, k8sHostService).contains(deviceId)) {
+            return;
+        }
+
         ARP arp = (ARP) ethernet.getPayload();
 
         if (arp.getOpCode() == ARP.OP_REPLY) {
diff --git a/apps/k8s-networking/app/src/main/java/org/onosproject/k8snetworking/impl/K8sSwitchingArpHandler.java b/apps/k8s-networking/app/src/main/java/org/onosproject/k8snetworking/impl/K8sSwitchingArpHandler.java
index 4c351af..f6c1d8b 100644
--- a/apps/k8s-networking/app/src/main/java/org/onosproject/k8snetworking/impl/K8sSwitchingArpHandler.java
+++ b/apps/k8s-networking/app/src/main/java/org/onosproject/k8snetworking/impl/K8sSwitchingArpHandler.java
@@ -36,12 +36,14 @@
 import org.onosproject.k8snetworking.api.K8sNetworkService;
 import org.onosproject.k8snetworking.api.K8sPort;
 import org.onosproject.k8snetworking.api.K8sServiceService;
+import org.onosproject.k8snode.api.K8sHostService;
 import org.onosproject.k8snode.api.K8sNode;
 import org.onosproject.k8snode.api.K8sNodeEvent;
 import org.onosproject.k8snode.api.K8sNodeListener;
 import org.onosproject.k8snode.api.K8sNodeService;
 import org.onosproject.mastership.MastershipService;
 import org.onosproject.net.ConnectPoint;
+import org.onosproject.net.DeviceId;
 import org.onosproject.net.PortNumber;
 import org.onosproject.net.device.DeviceService;
 import org.onosproject.net.flow.DefaultTrafficSelector;
@@ -82,11 +84,12 @@
 import static org.onosproject.k8snetworking.api.Constants.NODE_IP_PREFIX;
 import static org.onosproject.k8snetworking.api.Constants.PRIORITY_ARP_CONTROL_RULE;
 import static org.onosproject.k8snetworking.api.Constants.SERVICE_FAKE_MAC_STR;
+import static org.onosproject.k8snetworking.api.Constants.SHIFTED_IP_PREFIX;
 import static org.onosproject.k8snetworking.impl.OsgiPropertyConstants.ARP_MODE;
 import static org.onosproject.k8snetworking.impl.OsgiPropertyConstants.ARP_MODE_DEFAULT;
 import static org.onosproject.k8snetworking.impl.OsgiPropertyConstants.GATEWAY_MAC;
 import static org.onosproject.k8snetworking.impl.OsgiPropertyConstants.GATEWAY_MAC_DEFAULT;
-import static org.onosproject.k8snetworking.api.Constants.SHIFTED_IP_PREFIX;
+import static org.onosproject.k8snetworking.util.K8sNetworkingUtil.allK8sDevices;
 import static org.onosproject.k8snetworking.util.K8sNetworkingUtil.getPropertyValue;
 import static org.onosproject.k8snetworking.util.K8sNetworkingUtil.unshiftIpDomain;
 
@@ -139,6 +142,9 @@
     protected K8sNodeService k8sNodeService;
 
     @Reference(cardinality = ReferenceCardinality.MANDATORY)
+    protected K8sHostService k8sHostService;
+
+    @Reference(cardinality = ReferenceCardinality.MANDATORY)
     protected K8sNetworkService k8sNetworkService;
 
     @Reference(cardinality = ReferenceCardinality.MANDATORY)
@@ -214,6 +220,12 @@
             return;
         }
 
+        DeviceId deviceId = context.inPacket().receivedFrom().deviceId();
+
+        if (!allK8sDevices(k8sNodeService, k8sHostService).contains(deviceId)) {
+            return;
+        }
+
         ARP arpPacket = (ARP) ethPacket.getPayload();
         if (arpPacket.getOpCode() == ARP.OP_REQUEST) {
             processArpRequest(context, ethPacket);
diff --git a/apps/k8s-networking/app/src/main/java/org/onosproject/k8snetworking/util/K8sNetworkingUtil.java b/apps/k8s-networking/app/src/main/java/org/onosproject/k8snetworking/util/K8sNetworkingUtil.java
index e63a685..0b8c4c9 100644
--- a/apps/k8s-networking/app/src/main/java/org/onosproject/k8snetworking/util/K8sNetworkingUtil.java
+++ b/apps/k8s-networking/app/src/main/java/org/onosproject/k8snetworking/util/K8sNetworkingUtil.java
@@ -48,6 +48,7 @@
 import org.onosproject.k8snode.api.K8sHostService;
 import org.onosproject.k8snode.api.K8sNode;
 import org.onosproject.k8snode.api.K8sNodeService;
+import org.onosproject.k8snode.api.K8sRouterBridge;
 import org.onosproject.k8snode.api.K8sTunnelBridge;
 import org.onosproject.net.DeviceId;
 import org.onosproject.net.Port;
@@ -637,6 +638,48 @@
         return fsb.toString();
     }
 
+    /**
+     * Returns all device identifiers belong to kubernetes nodes and hosts.
+     *
+     * @param nodeService   node service
+     * @param hostService   host service
+     * @return all device identifiers belong to kubernetes nodes and hosts
+     */
+    public static Set<DeviceId> allK8sDevices(K8sNodeService nodeService,
+                                              K8sHostService hostService) {
+        Set<DeviceId> allDevIds = new HashSet<>();
+
+        Set<DeviceId> intgDevIds = nodeService.completeNodes().stream()
+                .map(K8sNode::intgBridge).collect(Collectors.toSet());
+        Set<DeviceId> extDevIds = nodeService.completeNodes().stream()
+                .map(K8sNode::extBridge).collect(Collectors.toSet());
+        Set<DeviceId> tunDevIds = nodeService.completeNodes().stream()
+                .map(K8sNode::tunBridge).collect(Collectors.toSet());
+        Set<DeviceId> localDevIds = nodeService.completeNodes().stream()
+                .map(K8sNode::localBridge).collect(Collectors.toSet());
+
+        Set<DeviceId> hostTunDevIds = new HashSet<>();
+        Set<DeviceId> hostRouterDevIds = new HashSet<>();
+
+        for (K8sHost host : hostService.completeHosts()) {
+            Set<K8sTunnelBridge> hostTunBrs = host.tunBridges();
+            Set<K8sRouterBridge> hostRouterBrs = host.routerBridges();
+            hostTunDevIds.addAll(hostTunBrs.stream().map(K8sTunnelBridge::deviceId)
+                    .collect(Collectors.toSet()));
+            hostRouterDevIds.addAll(hostRouterBrs.stream().map(K8sRouterBridge::deviceId)
+                    .collect(Collectors.toSet()));
+        }
+
+        allDevIds.addAll(intgDevIds);
+        allDevIds.addAll(extDevIds);
+        allDevIds.addAll(tunDevIds);
+        allDevIds.addAll(localDevIds);
+        allDevIds.addAll(hostTunDevIds);
+        allDevIds.addAll(hostRouterDevIds);
+
+        return allDevIds;
+    }
+
     private static int binLower(String binStr, int bits) {
         StringBuilder outBin = new StringBuilder(
                 binStr.substring(MASK_BEGIN_IDX, MASK_MAX_IDX - bits));