CORD-810 Remove VLAN extension selector from OFDPA 3

Change-Id: I973c5c3cb468998782e3b79ca1105a3f57dc265f
diff --git a/drivers/default/src/main/java/org/onosproject/driver/extensions/Ofdpa3ExtensionSelectorInterpreter.java b/drivers/default/src/main/java/org/onosproject/driver/extensions/Ofdpa3ExtensionSelectorInterpreter.java
index 388d0e6..e86dee9 100644
--- a/drivers/default/src/main/java/org/onosproject/driver/extensions/Ofdpa3ExtensionSelectorInterpreter.java
+++ b/drivers/default/src/main/java/org/onosproject/driver/extensions/Ofdpa3ExtensionSelectorInterpreter.java
@@ -28,14 +28,8 @@
 import org.projectfloodlight.openflow.protocol.oxm.OFOxm;
 import org.projectfloodlight.openflow.protocol.oxm.OFOxmOfdpaMplsL2Port;
 import org.projectfloodlight.openflow.protocol.oxm.OFOxmOfdpaOvid;
-import org.projectfloodlight.openflow.protocol.oxm.OFOxmVlanVid;
-import org.projectfloodlight.openflow.protocol.oxm.OFOxmVlanVidMasked;
-import org.projectfloodlight.openflow.types.OFVlanVidMatch;
 import org.projectfloodlight.openflow.types.U16;
 import org.projectfloodlight.openflow.types.U32;
-import org.projectfloodlight.openflow.types.VlanVid;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
 
 /**
  * Interpreter for OFDPA3 OpenFlow selector extensions.
@@ -43,13 +37,9 @@
 public class Ofdpa3ExtensionSelectorInterpreter extends AbstractHandlerBehaviour
         implements ExtensionSelectorInterpreter, ExtensionSelectorResolver {
 
-    private final Logger log = LoggerFactory.getLogger(getClass());
-
     @Override
     public boolean supported(ExtensionSelectorType extensionSelectorType) {
-        if (extensionSelectorType.equals(ExtensionSelectorTypes.OFDPA_MATCH_VLAN_VID.type())) {
-            return true;
-        } else if (extensionSelectorType.equals(ExtensionSelectorTypes.OFDPA_MATCH_OVID.type())) {
+        if (extensionSelectorType.equals(ExtensionSelectorTypes.OFDPA_MATCH_OVID.type())) {
             return true;
         } else if (extensionSelectorType.equals(ExtensionSelectorTypes.OFDPA_MATCH_MPLS_L2_PORT.type())) {
             return true;
@@ -60,20 +50,7 @@
     @Override
     public OFOxm<?> mapSelector(OFFactory factory, ExtensionSelector extensionSelector) {
         ExtensionSelectorType type = extensionSelector.type();
-        if (type.equals(ExtensionSelectorType.ExtensionSelectorTypes.OFDPA_MATCH_VLAN_VID.type())) {
-            VlanId vlanId = ((OfdpaMatchVlanVid) extensionSelector).vlanId();
-            // Special VLAN 0x0000/0x1FFF required by OFDPA
-            if (vlanId.equals(VlanId.NONE)) {
-                OFVlanVidMatch vid = OFVlanVidMatch.ofRawVid((short) 0x0000);
-                OFVlanVidMatch mask = OFVlanVidMatch.ofRawVid((short) 0x1FFF);
-                return factory.oxms().vlanVidMasked(vid, mask);
-                // Normal case
-            } else if (vlanId.equals(VlanId.ANY)) {
-                return factory.oxms().vlanVidMasked(OFVlanVidMatch.PRESENT, OFVlanVidMatch.PRESENT);
-            } else {
-                return factory.oxms().vlanVid(OFVlanVidMatch.ofVlanVid(VlanVid.ofVlan(vlanId.toShort())));
-            }
-        } else if (type.equals(ExtensionSelectorTypes.OFDPA_MATCH_OVID.type())) {
+        if (type.equals(ExtensionSelectorTypes.OFDPA_MATCH_OVID.type())) {
             VlanId vlanId = ((Ofdpa3MatchOvid) extensionSelector).vlanId();
             if (vlanId.equals(VlanId.NONE)) {
                 throw new UnsupportedOperationException(
@@ -105,32 +82,7 @@
 
     @Override
     public ExtensionSelector mapOxm(OFOxm<?> oxm) {
-
-        if (oxm.getMatchField().equals(MatchField.VLAN_VID)) {
-            VlanId vlanId;
-            if (oxm.isMasked()) {
-                OFVlanVidMatch vid = ((OFOxmVlanVidMasked) oxm).getValue();
-                OFVlanVidMatch mask = ((OFOxmVlanVidMasked) oxm).getMask();
-
-                if (vid.equals(OFVlanVidMatch.ofRawVid((short) 0))) {
-                    vlanId = VlanId.NONE;
-                } else if (vid.equals(OFVlanVidMatch.PRESENT) &&
-                        mask.equals(OFVlanVidMatch.PRESENT)) {
-                    vlanId = VlanId.ANY;
-                } else {
-                    vlanId = VlanId.vlanId(vid.getVlan());
-                }
-            } else {
-                OFVlanVidMatch vid = ((OFOxmVlanVid) oxm).getValue();
-
-                if (!vid.isPresentBitSet()) {
-                    vlanId = VlanId.NONE;
-                } else {
-                    vlanId = VlanId.vlanId(vid.getVlan());
-                }
-            }
-            return new OfdpaMatchVlanVid(vlanId);
-        } else if (oxm.getMatchField().equals(MatchField.OFDPA_OVID)) {
+        if (oxm.getMatchField().equals(MatchField.OFDPA_OVID)) {
             VlanId vlanId;
             if (oxm.isMasked()) {
                 throw new UnsupportedOperationException(
@@ -171,9 +123,7 @@
 
     @Override
     public ExtensionSelector getExtensionSelector(ExtensionSelectorType type) {
-        if (type.equals(ExtensionSelectorType.ExtensionSelectorTypes.OFDPA_MATCH_VLAN_VID.type())) {
-            return new OfdpaMatchVlanVid();
-        } else if (type.equals(ExtensionSelectorTypes.OFDPA_MATCH_OVID.type())) {
+        if (type.equals(ExtensionSelectorTypes.OFDPA_MATCH_OVID.type())) {
             return new Ofdpa3MatchOvid();
         } else if (type.equals(ExtensionSelectorTypes.OFDPA_MATCH_MPLS_L2_PORT.type())) {
             return new Ofdpa3MatchMplsL2Port();
diff --git a/drivers/default/src/main/java/org/onosproject/driver/pipeline/Ofdpa2Pipeline.java b/drivers/default/src/main/java/org/onosproject/driver/pipeline/Ofdpa2Pipeline.java
index fd677c6..a698a81 100644
--- a/drivers/default/src/main/java/org/onosproject/driver/pipeline/Ofdpa2Pipeline.java
+++ b/drivers/default/src/main/java/org/onosproject/driver/pipeline/Ofdpa2Pipeline.java
@@ -193,6 +193,15 @@
         // software switches does require table-miss-entries.
     }
 
+    /**
+     * Determines whether this pipeline requires OFDPA match and set VLAN extensions.
+     *
+     * @return true to use the extensions
+     */
+    protected boolean requireVlanExtensions() {
+        return true;
+    }
+
     //////////////////////////////////////
     //  Flow Objectives
     //////////////////////////////////////
@@ -409,9 +418,16 @@
             List<FlowRule> assignmentRules = new ArrayList<>();
 
             allRules.forEach(flowRule -> {
-                ExtensionCriterion extCriterion =
-                        (ExtensionCriterion) flowRule.selector().getCriterion(Criterion.Type.EXTENSION);
-                VlanId vlanId = ((OfdpaMatchVlanVid) extCriterion.extensionSelector()).vlanId();
+                VlanId vlanId;
+                if (requireVlanExtensions()) {
+                    ExtensionCriterion extCriterion =
+                            (ExtensionCriterion) flowRule.selector().getCriterion(Criterion.Type.EXTENSION);
+                    vlanId = ((OfdpaMatchVlanVid) extCriterion.extensionSelector()).vlanId();
+                } else {
+                    VlanIdCriterion vlanIdCriterion =
+                            (VlanIdCriterion) flowRule.selector().getCriterion(Criterion.Type.VLAN_VID);
+                    vlanId = vlanIdCriterion.vlanId();
+                }
                 if (!vlanId.equals(VlanId.NONE)) {
                     filteringRules.add(flowRule);
                 } else {
@@ -453,9 +469,12 @@
     }
 
     /**
-     * Allows untagged packets into pipeline by assigning a vlan id.
-     * Vlan assignment is done by the application.
-     * Allows tagged packets into pipeline as per configured port-vlan info.
+     * Internal implementation of processVlanIdFilter.
+     * <p>
+     * The is_present bit in set_vlan_vid action is required to be 0 in OFDPA i12.
+     * Since it is non-OF spec, we need an extension treatment for that.
+     * The useVlanExtension must be set to false for OFDPA i12.
+     * </p>
      *
      * @param portCriterion       port on device for which this filter is programmed
      * @param vidCriterion        vlan assigned to port, or NONE for untagged
@@ -464,33 +483,9 @@
      * @return list of FlowRule for port-vlan filters
      */
     protected List<FlowRule> processVlanIdFilter(PortCriterion portCriterion,
-                                                 VlanIdCriterion vidCriterion,
-                                                 VlanId assignedVlan,
-                                                 ApplicationId applicationId) {
-        return processVlanIdFilterInternal(portCriterion, vidCriterion, assignedVlan,
-                applicationId, true);
-    }
-
-    /**
-     * Internal implementation of processVlanIdFilter.
-     * <p>
-     * The is_present bit in set_vlan_vid action is required to be 0 in OFDPA i12.
-     * Since it is non-OF spec, we need an extension treatment for that.
-     * The useSetVlanExtension must be set to false for OFDPA i12.
-     * </p>
-     *
-     * @param portCriterion       port on device for which this filter is programmed
-     * @param vidCriterion        vlan assigned to port, or NONE for untagged
-     * @param assignedVlan        assigned vlan-id for untagged packets
-     * @param applicationId       for application programming this filter
-     * @param useSetVlanExtension use the setVlanVid extension that has is_present bit set to 0.
-     * @return list of FlowRule for port-vlan filters
-     */
-    protected List<FlowRule> processVlanIdFilterInternal(PortCriterion portCriterion,
                                                          VlanIdCriterion vidCriterion,
                                                          VlanId assignedVlan,
-                                                         ApplicationId applicationId,
-                                                         boolean useSetVlanExtension) {
+                                                         ApplicationId applicationId) {
         List<FlowRule> rules = new ArrayList<>();
         TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
         TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
@@ -501,26 +496,32 @@
 
         if (vidCriterion.vlanId() == VlanId.NONE) {
             // untagged packets are assigned vlans
-            OfdpaMatchVlanVid ofdpaMatchVlanVid = new OfdpaMatchVlanVid(VlanId.NONE);
-            selector.extension(ofdpaMatchVlanVid, deviceId);
-            if (useSetVlanExtension) {
+            preSelector = DefaultTrafficSelector.builder();
+            if (requireVlanExtensions()) {
+                OfdpaMatchVlanVid ofdpaMatchVlanVid = new OfdpaMatchVlanVid(VlanId.NONE);
+                selector.extension(ofdpaMatchVlanVid, deviceId);
                 OfdpaSetVlanVid ofdpaSetVlanVid = new OfdpaSetVlanVid(assignedVlan);
                 treatment.extension(ofdpaSetVlanVid, deviceId);
+
+                OfdpaMatchVlanVid preOfdpaMatchVlanVid = new OfdpaMatchVlanVid(assignedVlan);
+                preSelector.extension(preOfdpaMatchVlanVid, deviceId);
             } else {
+                selector.matchVlanId(VlanId.NONE);
                 treatment.setVlanId(assignedVlan);
+
+                preSelector.matchVlanId(assignedVlan);
+            }
+            preTreatment = DefaultTrafficTreatment.builder().transition(TMAC_TABLE);
+        } else {
+            if (requireVlanExtensions()) {
+                OfdpaMatchVlanVid ofdpaMatchVlanVid = new OfdpaMatchVlanVid(vidCriterion.vlanId());
+                selector.extension(ofdpaMatchVlanVid, deviceId);
+            } else {
+                selector.matchVlanId(vidCriterion.vlanId());
             }
 
-            preSelector = DefaultTrafficSelector.builder();
-            OfdpaMatchVlanVid preOfdpaMatchVlanVid = new OfdpaMatchVlanVid(assignedVlan);
-            preSelector.extension(preOfdpaMatchVlanVid, deviceId);
-            preTreatment = DefaultTrafficTreatment.builder().transition(TMAC_TABLE);
-
-        } else {
-            OfdpaMatchVlanVid ofdpaMatchVlanVid = new OfdpaMatchVlanVid(vidCriterion.vlanId());
-            selector.extension(ofdpaMatchVlanVid, deviceId);
-
             if (!assignedVlan.equals(vidCriterion.vlanId())) {
-                if (useSetVlanExtension) {
+                if (requireVlanExtensions()) {
                     OfdpaSetVlanVid ofdpaSetVlanVid = new OfdpaSetVlanVid(assignedVlan);
                     treatment.extension(ofdpaSetVlanVid, deviceId);
                 } else {
@@ -623,7 +624,11 @@
             TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
             TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
             selector.matchInPort(pnum);
-            selector.extension(ofdpaMatchVlanVid, deviceId);
+            if (requireVlanExtensions()) {
+                selector.extension(ofdpaMatchVlanVid, deviceId);
+            } else {
+                selector.matchVlanId(vidCriterion.vlanId());
+            }
             selector.matchEthType(Ethernet.TYPE_IPV4);
             selector.matchEthDst(ethCriterion.mac());
             treatment.transition(UNICAST_ROUTING_TABLE);
@@ -640,7 +645,11 @@
             selector = DefaultTrafficSelector.builder();
             treatment = DefaultTrafficTreatment.builder();
             selector.matchInPort(pnum);
-            selector.extension(ofdpaMatchVlanVid, deviceId);
+            if (requireVlanExtensions()) {
+                selector.extension(ofdpaMatchVlanVid, deviceId);
+            } else {
+                selector.matchVlanId(vidCriterion.vlanId());
+            }
             selector.matchEthType(Ethernet.MPLS_UNICAST);
             selector.matchEthDst(ethCriterion.mac());
             treatment.transition(MPLS_TABLE_0);
@@ -659,7 +668,11 @@
             selector = DefaultTrafficSelector.builder();
             treatment = DefaultTrafficTreatment.builder();
             selector.matchInPort(pnum);
-            selector.extension(ofdpaMatchVlanVid, deviceId);
+            if (requireVlanExtensions()) {
+                selector.extension(ofdpaMatchVlanVid, deviceId);
+            } else {
+                selector.matchVlanId(vidCriterion.vlanId());
+            }
             selector.matchEthType(Ethernet.TYPE_IPV6);
             selector.matchEthDst(ethCriterion.mac());
             treatment.transition(UNICAST_ROUTING_TABLE);
@@ -778,9 +791,12 @@
                 if (vlanId.equals(VlanId.NONE)) {
                     return;
                 }
-                OfdpaMatchVlanVid ofdpaMatchVlanVid =
-                        new OfdpaMatchVlanVid(vlanId);
-                sbuilder.extension(ofdpaMatchVlanVid, deviceId);
+                if (requireVlanExtensions()) {
+                    OfdpaMatchVlanVid ofdpaMatchVlanVid = new OfdpaMatchVlanVid(vlanId);
+                    sbuilder.extension(ofdpaMatchVlanVid, deviceId);
+                } else {
+                    sbuilder.matchVlanId(vlanId);
+                }
             } else if (criterion instanceof Icmpv6TypeCriterion ||
                     criterion instanceof Icmpv6CodeCriterion) {
                 /*
@@ -1083,8 +1099,12 @@
                 fail(fwd, ObjectiveError.BADPARAMS);
                 return -1;
             }
-            OfdpaMatchVlanVid ofdpaMatchVlanVid = new OfdpaMatchVlanVid(assignedVlan);
-            builderToUpdate.extension(ofdpaMatchVlanVid, deviceId);
+            if (requireVlanExtensions()) {
+                OfdpaMatchVlanVid ofdpaMatchVlanVid = new OfdpaMatchVlanVid(assignedVlan);
+                builderToUpdate.extension(ofdpaMatchVlanVid, deviceId);
+            } else {
+                builderToUpdate.matchVlanId(assignedVlan);
+            }
             builderToUpdate.matchEthType(Ethernet.TYPE_IPV4).matchIPDst(ipv4Dst);
             log.debug("processing IPv4 multicast specific forwarding objective {} -> next:{}"
                               + " in dev:{}", fwd.id(), fwd.nextId(), deviceId);
@@ -1197,8 +1217,12 @@
                     + "in dev:{} for vlan:{}",
                       fwd.id(), fwd.nextId(), deviceId, vlanIdCriterion.vlanId());
         }
-        OfdpaMatchVlanVid ofdpaMatchVlanVid = new OfdpaMatchVlanVid(vlanIdCriterion.vlanId());
-        filteredSelectorBuilder.extension(ofdpaMatchVlanVid, deviceId);
+        if (requireVlanExtensions()) {
+            OfdpaMatchVlanVid ofdpaMatchVlanVid = new OfdpaMatchVlanVid(vlanIdCriterion.vlanId());
+            filteredSelectorBuilder.extension(ofdpaMatchVlanVid, deviceId);
+        } else {
+            filteredSelectorBuilder.matchVlanId(vlanIdCriterion.vlanId());
+        }
         TrafficSelector filteredSelector = filteredSelectorBuilder.build();
 
         if (fwd.treatment() != null) {
diff --git a/drivers/default/src/main/java/org/onosproject/driver/pipeline/Ofdpa3Pipeline.java b/drivers/default/src/main/java/org/onosproject/driver/pipeline/Ofdpa3Pipeline.java
index 4df1e2d..5588228 100644
--- a/drivers/default/src/main/java/org/onosproject/driver/pipeline/Ofdpa3Pipeline.java
+++ b/drivers/default/src/main/java/org/onosproject/driver/pipeline/Ofdpa3Pipeline.java
@@ -17,7 +17,6 @@
 package org.onosproject.driver.pipeline;
 
 import com.google.common.collect.ImmutableList;
-import org.onlab.packet.VlanId;
 import org.onosproject.core.ApplicationId;
 import org.onosproject.driver.extensions.Ofdpa3MatchMplsL2Port;
 import org.onosproject.driver.extensions.Ofdpa3MatchOvid;
@@ -27,7 +26,6 @@
 import org.onosproject.driver.extensions.Ofdpa3SetMplsType;
 import org.onosproject.driver.extensions.Ofdpa3SetOvid;
 import org.onosproject.driver.extensions.Ofdpa3SetQosIndex;
-import org.onosproject.driver.extensions.OfdpaMatchVlanVid;
 import org.onosproject.net.behaviour.NextGroup;
 import org.onosproject.net.behaviour.PipelinerContext;
 import org.onosproject.net.flow.DefaultFlowRule;
@@ -85,6 +83,11 @@
     }
 
     @Override
+    protected boolean requireVlanExtensions() {
+        return false;
+    }
+
+    @Override
     protected void processFilter(FilteringObjective filteringObjective,
                                  boolean install,
                                  ApplicationId applicationId) {
@@ -215,7 +218,7 @@
         // Ofdpa supports this through the OVID meta-data type.
         TrafficSelector.Builder vlan1Selector = DefaultTrafficSelector.builder()
                 .matchInPort(portCriterion.port())
-                .extension(new OfdpaMatchVlanVid(innerVlanIdCriterion.vlanId()), deviceId)
+                .matchVlanId(innerVlanIdCriterion.vlanId())
                 .extension(new Ofdpa3MatchOvid(outerVlanIdCriterion.vlanId()), deviceId);
         // TODO understand for the future how to manage the vlan rewrite.
         TrafficTreatment.Builder vlan1Treatment = DefaultTrafficTreatment.builder()
@@ -239,7 +242,7 @@
         // We have to match on the outer vlan.
         TrafficSelector.Builder vlanSelector = DefaultTrafficSelector.builder()
                 .matchInPort(portCriterion.port())
-                .extension(new OfdpaMatchVlanVid(outerVlanIdCriterion.vlanId()), deviceId);
+                .matchVlanId(outerVlanIdCriterion.vlanId());
         // TODO understand for the future how to manage the vlan rewrite.
         TrafficTreatment.Builder vlanTreatment = DefaultTrafficTreatment.builder()
                 .popVlan()
@@ -259,15 +262,6 @@
     }
 
     @Override
-    protected List<FlowRule> processVlanIdFilter(PortCriterion portCriterion,
-                                                 VlanIdCriterion vidCriterion,
-                                                 VlanId assignedVlan,
-                                                 ApplicationId applicationId) {
-        return processVlanIdFilterInternal(portCriterion, vidCriterion, assignedVlan,
-                applicationId, false);
-    }
-
-    @Override
     protected Collection<FlowRule> processEthTypeSpecific(ForwardingObjective fwd) {
         if (isNotMplsBos(fwd.selector())) {
             return processEthTypeSpecificInternal(fwd, true, MPLS_TYPE_TABLE);