Fix: enforce to match VNI for ARP request in broadcast mode

Change-Id: I4dd31866c843f16d527e9d6c25ea1f99946c8990
diff --git a/apps/openstacknetworking/app/src/main/java/org/onosproject/openstacknetworking/impl/OpenstackSwitchingHandler.java b/apps/openstacknetworking/app/src/main/java/org/onosproject/openstacknetworking/impl/OpenstackSwitchingHandler.java
index 332ed7f..1d74e9a 100644
--- a/apps/openstacknetworking/app/src/main/java/org/onosproject/openstacknetworking/impl/OpenstackSwitchingHandler.java
+++ b/apps/openstacknetworking/app/src/main/java/org/onosproject/openstacknetworking/impl/OpenstackSwitchingHandler.java
@@ -24,6 +24,8 @@
 import org.apache.felix.scr.annotations.ReferenceCardinality;
 import org.onlab.packet.Ethernet;
 import org.onlab.packet.VlanId;
+import org.onosproject.cfg.ComponentConfigService;
+import org.onosproject.cfg.ConfigProperty;
 import org.onosproject.cluster.ClusterService;
 import org.onosproject.cluster.LeadershipService;
 import org.onosproject.cluster.NodeId;
@@ -56,12 +58,15 @@
 import org.slf4j.Logger;
 
 import java.util.Objects;
+import java.util.Set;
 import java.util.concurrent.ExecutorService;
 
 import static java.util.concurrent.Executors.newSingleThreadExecutor;
 import static org.onlab.util.Tools.groupedThreads;
 import static org.onosproject.openstacknetworking.api.Constants.ACL_TABLE;
-import static org.onosproject.openstacknetworking.api.Constants.DHCP_ARP_TABLE;
+import static org.onosproject.openstacknetworking.api.Constants.ARP_BROADCAST_MODE;
+import static org.onosproject.openstacknetworking.api.Constants.ARP_TABLE;
+import static org.onosproject.openstacknetworking.api.Constants.DHCP_TABLE;
 import static org.onosproject.openstacknetworking.api.Constants.FLAT_TABLE;
 import static org.onosproject.openstacknetworking.api.Constants.FORWARDING_TABLE;
 import static org.onosproject.openstacknetworking.api.Constants.OPENSTACK_NETWORKING_APP_ID;
@@ -74,6 +79,7 @@
 import static org.onosproject.openstacknetworking.api.Constants.PRIORITY_TUNNEL_TAG_RULE;
 import static org.onosproject.openstacknetworking.api.Constants.STAT_FLAT_OUTBOUND_TABLE;
 import static org.onosproject.openstacknetworking.api.Constants.VTAG_TABLE;
+import static org.onosproject.openstacknetworking.util.OpenstackNetworkingUtil.getPropertyValue;
 import static org.onosproject.openstacknetworking.util.OpenstackNetworkingUtil.swapStaleLocation;
 import static org.onosproject.openstacknetworking.util.RulePopulatorUtil.buildExtension;
 import static org.onosproject.openstacknode.api.OpenstackNode.NodeType.COMPUTE;
@@ -89,6 +95,7 @@
 
     private final Logger log = getLogger(getClass());
 
+    private static final String ARP_MODE = "arpMode";
     private static final String ERR_SET_FLOWS_VNI = "Failed to set flows for %s: Failed to get VNI for %s";
 
     @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
@@ -107,6 +114,9 @@
     protected ClusterService clusterService;
 
     @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
+    protected ComponentConfigService configService;
+
+    @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
     protected LeadershipService leadershipService;
 
     @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
@@ -162,12 +172,22 @@
         NetworkType type = osNetworkService.network(instPort.networkId()).getNetworkType();
         switch (type) {
             case VXLAN:
-                setTunnelTagFlowRules(instPort, install);
+                setTunnelTagIpFlowRules(instPort, install);
                 setForwardingRulesForVxlan(instPort, install);
+
+                if (ARP_BROADCAST_MODE.equals(getArpMode())) {
+                    setTunnelTagArpFlowRules(instPort, install);
+                }
+
                 break;
             case VLAN:
-                setVlanTagFlowRules(instPort, install);
+                setVlanTagIpFlowRules(instPort, install);
                 setForwardingRulesForVlan(instPort, install);
+
+                if (ARP_BROADCAST_MODE.equals(getArpMode())) {
+                    setVlanTagArpFlowRules(instPort, install);
+                }
+
                 break;
             case FLAT:
                 setFlatJumpRules(instPort, install);
@@ -190,10 +210,20 @@
 
         switch (type) {
             case VXLAN:
-                setTunnelTagFlowRules(instPort, false);
+                setTunnelTagIpFlowRules(instPort, false);
+
+                if (ARP_BROADCAST_MODE.equals(getArpMode())) {
+                    setTunnelTagArpFlowRules(instPort, false);
+                }
+
                 break;
             case VLAN:
-                setVlanTagFlowRules(instPort, false);
+                setVlanTagIpFlowRules(instPort, false);
+
+                if (ARP_BROADCAST_MODE.equals(getArpMode())) {
+                    setVlanTagArpFlowRules(instPort, false);
+                }
+
                 break;
             case FLAT:
                 setFlatJumpRules(instPort, false);
@@ -219,7 +249,7 @@
                 selector.build(),
                 treatment.build(),
                 PRIORITY_FLAT_JUMP_UPSTREAM_RULE,
-                DHCP_ARP_TABLE,
+                DHCP_TABLE,
                 install);
 
         Network network = osNetworkService.network(port.networkId());
@@ -247,7 +277,7 @@
                 selector.build(),
                 treatment.build(),
                 PRIORITY_FLAT_JUMP_DOWNSTREAM_RULE,
-                DHCP_ARP_TABLE,
+                DHCP_TABLE,
                 install);
 
         selector = DefaultTrafficSelector.builder();
@@ -261,7 +291,7 @@
                 selector.build(),
                 treatment.build(),
                 PRIORITY_FLAT_JUMP_DOWNSTREAM_RULE,
-                DHCP_ARP_TABLE,
+                DHCP_TABLE,
                 install);
     }
 
@@ -446,18 +476,27 @@
                 });
     }
 
+    private void setTunnelTagArpFlowRules(InstancePort instPort, boolean install) {
+        setTunnelTagFlowRules(instPort, Ethernet.TYPE_ARP, install);
+    }
+
+    private void setTunnelTagIpFlowRules(InstancePort instPort, boolean install) {
+        setTunnelTagFlowRules(instPort, Ethernet.TYPE_IPV4, install);
+    }
+
     /**
      * Configures the flow rule which is for using VXLAN to tag the packet
      * based on the in_port number of a virtual instance.
-     * Note that this rule will be inserted in VNI table (table 0).
+     * Note that this rule will be inserted in vTag table.
      *
      * @param instPort instance port object
      * @param install install flag, add the rule if true, remove it otherwise
      */
-    private void setTunnelTagFlowRules(InstancePort instPort, boolean install) {
+    private void setTunnelTagFlowRules(InstancePort instPort,
+                                       short ethType,
+                                       boolean install) {
         TrafficSelector selector = DefaultTrafficSelector.builder()
-                // TODO: need to handle IPv6 in near future
-                .matchEthType(Ethernet.TYPE_IPV4)
+                .matchEthType(ethType)
                 .matchInPort(instPort.portNumber())
                 .build();
 
@@ -469,9 +508,9 @@
 
         TrafficTreatment.Builder tb = DefaultTrafficTreatment.builder()
                 .setTunnelId(getVni(instPort))
-                .transition(ACL_TABLE);
+                .transition(ARP_TABLE);
 
-        if (securityGroupService.isSecurityGroupEnabled()) {
+        if (securityGroupService.isSecurityGroupEnabled() && ethType == Ethernet.TYPE_IPV4) {
             tb.extension(ctTreatment, instPort.deviceId());
         }
 
@@ -485,18 +524,27 @@
                 install);
     }
 
+    private void setVlanTagIpFlowRules(InstancePort instPort, boolean install) {
+        setVlanTagFlowRules(instPort, Ethernet.TYPE_IPV4, install);
+    }
+
+    private void setVlanTagArpFlowRules(InstancePort instPort, boolean install) {
+        setVlanTagFlowRules(instPort, Ethernet.TYPE_ARP, install);
+    }
+
     /**
      * Configures the flow rule which is for using VLAN to tag the packet
      * based on the in_port number of a virtual instance.
-     * Note that this rule will be inserted in VNI table (table 0).
+     * Note that this rule will be inserted in vTag table.
      *
      * @param instPort instance port object
      * @param install install flag, add the rule if true, remove it otherwise
      */
-    private void setVlanTagFlowRules(InstancePort instPort, boolean install) {
+    private void setVlanTagFlowRules(InstancePort instPort,
+                                     short ethType,
+                                     boolean install) {
         TrafficSelector selector = DefaultTrafficSelector.builder()
-                // TODO: need to handle IPv6 in near future
-                .matchEthType(Ethernet.TYPE_IPV4)
+                .matchEthType(ethType)
                 .matchInPort(instPort.portNumber())
                 .build();
 
@@ -637,6 +685,11 @@
         return Long.valueOf(osNet.getProviderSegID());
     }
 
+    private String getArpMode() {
+        Set<ConfigProperty> properties = configService.getProperties(OpenstackSwitchingArpHandler.class.getName());
+        return getPropertyValue(properties, ARP_MODE);
+    }
+
     /**
      * An internal instance port listener which listens the port events generated
      * from VM. The corresponding L2 forwarding rules will be generated and