[CORD-801] OFDPA pipeline for OpenvSwitch

Drops TTL_IN, TTL_OUT and MPLS_BOS instructions which are not supported by OpenvSwitch.
This will allow us to run OFDPA pipeline with OpenvSwitch >= 2.6.0

Change-Id: Icfa6f2fdaa857877a57ae4c719354483b24c5816
diff --git a/drivers/default/src/main/java/org/onosproject/driver/pipeline/CpqdOfdpa2Pipeline.java b/drivers/default/src/main/java/org/onosproject/driver/pipeline/CpqdOfdpa2Pipeline.java
index 5b9fd04..ecc3dbf 100644
--- a/drivers/default/src/main/java/org/onosproject/driver/pipeline/CpqdOfdpa2Pipeline.java
+++ b/drivers/default/src/main/java/org/onosproject/driver/pipeline/CpqdOfdpa2Pipeline.java
@@ -22,20 +22,16 @@
 import org.onlab.packet.MacAddress;
 import org.onlab.packet.VlanId;
 import org.onosproject.core.ApplicationId;
-import org.onosproject.core.CoreService;
-import org.onosproject.net.DeviceId;
 import org.onosproject.net.Port;
 import org.onosproject.net.PortNumber;
 import org.onosproject.net.behaviour.NextGroup;
 import org.onosproject.net.behaviour.PipelinerContext;
-import org.onosproject.net.device.DeviceService;
 import org.onosproject.net.flow.DefaultFlowRule;
 import org.onosproject.net.flow.DefaultTrafficSelector;
 import org.onosproject.net.flow.DefaultTrafficTreatment;
 import org.onosproject.net.flow.FlowRule;
 import org.onosproject.net.flow.FlowRuleOperations;
 import org.onosproject.net.flow.FlowRuleOperationsContext;
-import org.onosproject.net.flow.FlowRuleService;
 import org.onosproject.net.flow.TrafficSelector;
 import org.onosproject.net.flow.TrafficTreatment;
 import org.onosproject.net.flow.criteria.Criteria;
@@ -51,13 +47,13 @@
 import org.onosproject.net.flow.criteria.VlanIdCriterion;
 import org.onosproject.net.flow.instructions.Instruction;
 import org.onosproject.net.flow.instructions.Instructions.OutputInstruction;
+import org.onosproject.net.flow.instructions.L3ModificationInstruction;
 import org.onosproject.net.flow.instructions.L2ModificationInstruction.ModVlanIdInstruction;
 import org.onosproject.net.flowobjective.FilteringObjective;
 import org.onosproject.net.flowobjective.ForwardingObjective;
 import org.onosproject.net.flowobjective.ObjectiveError;
 import org.onosproject.net.group.Group;
 import org.onosproject.net.group.GroupKey;
-import org.onosproject.net.group.GroupService;
 import org.onosproject.net.packet.PacketPriority;
 import org.slf4j.Logger;
 
@@ -72,7 +68,7 @@
 
 
 /**
- * Driver for software switch emulation of the OFDPA 2.0 pipeline.
+ * Driver for software switch emulation of the OFDPA pipeline.
  * The software switch is the CPqD OF 1.3 switch. Unfortunately the CPqD switch
  * does not handle vlan tags and mpls labels simultaneously, which requires us
  * to do some workarounds in the driver. This driver is meant for the use of
@@ -83,25 +79,25 @@
 
     private final Logger log = getLogger(getClass());
 
+    /**
+     * Determines whether this pipeline support copy ttl instructions or not.
+     *
+     * @return true if copy ttl instructions are supported
+     */
+    protected boolean supportCopyTtl() {
+        return true;
+    }
+
     @Override
-    public void init(DeviceId deviceId, PipelinerContext context) {
-        this.deviceId = deviceId;
-
-        // Initialize OFDPA group handler
-        groupHandler = new CpqdOfdpa2GroupHandler();
-        groupHandler.init(deviceId, context);
-
-        serviceDirectory = context.directory();
-        coreService = serviceDirectory.get(CoreService.class);
-        flowRuleService = serviceDirectory.get(FlowRuleService.class);
-        groupService = serviceDirectory.get(GroupService.class);
-        flowObjectiveStore = context.store();
-        deviceService = serviceDirectory.get(DeviceService.class);
-
+    protected void initDriverId() {
         driverId = coreService.registerApplication(
                 "org.onosproject.driver.CpqdOfdpa2Pipeline");
+    }
 
-        initializePipeline();
+    @Override
+    protected void initGroupHander(PipelinerContext context) {
+        groupHandler = new CpqdOfdpa2GroupHandler();
+        groupHandler.init(deviceId, context);
     }
 
     /*
@@ -567,6 +563,13 @@
         TrafficTreatment.Builder tb = DefaultTrafficTreatment.builder();
         if (fwd.treatment() != null) {
             for (Instruction i : fwd.treatment().allInstructions()) {
+                if (!supportCopyTtl() && i instanceof L3ModificationInstruction) {
+                    L3ModificationInstruction l3instr = (L3ModificationInstruction) i;
+                    if (l3instr.subtype().equals(L3ModificationInstruction.L3SubType.TTL_IN) ||
+                            l3instr.subtype().equals(L3ModificationInstruction.L3SubType.TTL_OUT)) {
+                        continue;
+                    }
+                }
                 /*
                  * NOTE: OF-DPA does not support immediate instruction in
                  * L3 unicast and MPLS table.
diff --git a/drivers/default/src/main/java/org/onosproject/driver/pipeline/CpqdOfdpa2VlanPipeline.java b/drivers/default/src/main/java/org/onosproject/driver/pipeline/CpqdOfdpa2VlanPipeline.java
index 09fae88..30bdfe6 100644
--- a/drivers/default/src/main/java/org/onosproject/driver/pipeline/CpqdOfdpa2VlanPipeline.java
+++ b/drivers/default/src/main/java/org/onosproject/driver/pipeline/CpqdOfdpa2VlanPipeline.java
@@ -25,18 +25,14 @@
 import org.onlab.packet.Ethernet;
 import org.onlab.packet.VlanId;
 import org.onosproject.core.ApplicationId;
-import org.onosproject.core.CoreService;
-import org.onosproject.net.DeviceId;
 import org.onosproject.net.Port;
 import org.onosproject.net.PortNumber;
 import org.onosproject.net.behaviour.NextGroup;
 import org.onosproject.net.behaviour.PipelinerContext;
-import org.onosproject.net.device.DeviceService;
 import org.onosproject.net.flow.DefaultFlowRule;
 import org.onosproject.net.flow.DefaultTrafficSelector;
 import org.onosproject.net.flow.DefaultTrafficTreatment;
 import org.onosproject.net.flow.FlowRule;
-import org.onosproject.net.flow.FlowRuleService;
 import org.onosproject.net.flow.TrafficSelector;
 import org.onosproject.net.flow.TrafficTreatment;
 import org.onosproject.net.flow.criteria.Criteria;
@@ -51,7 +47,6 @@
 import org.onosproject.net.flowobjective.ObjectiveError;
 import org.onosproject.net.group.Group;
 import org.onosproject.net.group.GroupKey;
-import org.onosproject.net.group.GroupService;
 import org.slf4j.Logger;
 
 
@@ -70,24 +65,15 @@
     private final Logger log = getLogger(getClass());
 
     @Override
-    public void init(DeviceId deviceId, PipelinerContext context) {
-        this.deviceId = deviceId;
-
-        // Initialize OFDPA group handler
-        groupHandler = new CpqdOfdpa2GroupHandler();
-        groupHandler.init(deviceId, context);
-
-        serviceDirectory = context.directory();
-        coreService = serviceDirectory.get(CoreService.class);
-        flowRuleService = serviceDirectory.get(FlowRuleService.class);
-        groupService = serviceDirectory.get(GroupService.class);
-        flowObjectiveStore = context.store();
-        deviceService = serviceDirectory.get(DeviceService.class);
-
+    protected void initDriverId() {
         driverId = coreService.registerApplication(
                 "org.onosproject.driver.CpqdOfdpa2VlanPipeline");
+    }
 
-        initializePipeline();
+    @Override
+    protected void initGroupHander(PipelinerContext context) {
+        groupHandler = new CpqdOfdpa2GroupHandler();
+        groupHandler.init(deviceId, context);
     }
 
     /*
diff --git a/drivers/default/src/main/java/org/onosproject/driver/pipeline/Ofdpa2GroupHandler.java b/drivers/default/src/main/java/org/onosproject/driver/pipeline/Ofdpa2GroupHandler.java
index 7da0be5..f116c71 100644
--- a/drivers/default/src/main/java/org/onosproject/driver/pipeline/Ofdpa2GroupHandler.java
+++ b/drivers/default/src/main/java/org/onosproject/driver/pipeline/Ofdpa2GroupHandler.java
@@ -80,7 +80,7 @@
 import static org.slf4j.LoggerFactory.getLogger;
 
 /**
- * Group handler for OFDPA2 pipeline.
+ * Group handler that emulates Broadcom OF-DPA TTP on CpqD.
  */
 public class Ofdpa2GroupHandler {
     /*
@@ -129,6 +129,24 @@
     protected ConcurrentHashMap<Integer, NextObjective> pendingBuckets =
             new ConcurrentHashMap<>();
 
+    /**
+     * Determines whether this pipeline support copy ttl instructions or not.
+     *
+     * @return true if copy ttl instructions are supported
+     */
+    protected boolean supportCopyTtl() {
+        return true;
+    }
+
+    /**
+     * Determines whether this pipeline support set mpls bos instruction or not.
+     *
+     * @return true if set mpls bos instruction is supported
+     */
+    protected boolean supportSetMplsBos() {
+        return true;
+    }
+
     protected void init(DeviceId deviceId, PipelinerContext context) {
         this.deviceId = deviceId;
         this.flowObjectiveStore = context.store();
@@ -844,10 +862,14 @@
                 TrafficTreatment.Builder l3vpnTtb = DefaultTrafficTreatment.builder();
                 l3vpnTtb.pushMpls()
                         .setMpls(innermostLabel)
-                        .setMplsBos(true)
-                        .copyTtlOut()
-                        .group(new DefaultGroupId(
-                                onelabelGroupInfo.nextGroupDesc.givenGroupId()));
+                        .group(new DefaultGroupId(onelabelGroupInfo.nextGroupDesc.givenGroupId()));
+                if (supportCopyTtl()) {
+                    l3vpnTtb.copyTtlOut();
+                }
+                if (supportSetMplsBos()) {
+                    l3vpnTtb.setMplsBos(true);
+                }
+
                 GroupBucket l3vpnGrpBkt  =
                         DefaultGroupBucket.createIndirectGroupBucket(l3vpnTtb.build());
                 int l3vpnIndex = getNextAvailableIndex();
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 890af99..cadca75 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
@@ -147,10 +147,6 @@
     public void init(DeviceId deviceId, PipelinerContext context) {
         this.deviceId = deviceId;
 
-        // Initialize OFDPA group handler
-        groupHandler = new Ofdpa2GroupHandler();
-        groupHandler.init(deviceId, context);
-
         serviceDirectory = context.directory();
         coreService = serviceDirectory.get(CoreService.class);
         flowRuleService = serviceDirectory.get(FlowRuleService.class);
@@ -158,12 +154,22 @@
         flowObjectiveStore = context.store();
         deviceService = serviceDirectory.get(DeviceService.class);
 
-        driverId = coreService.registerApplication(
-                "org.onosproject.driver.Ofdpa2Pipeline");
+        initDriverId();
+        initGroupHander(context);
 
         initializePipeline();
     }
 
+    protected void initDriverId() {
+        driverId = coreService.registerApplication(
+                "org.onosproject.driver.Ofdpa2Pipeline");
+    }
+
+    protected void initGroupHander(PipelinerContext context) {
+        groupHandler = new Ofdpa2GroupHandler();
+        groupHandler.init(deviceId, context);
+    }
+
     protected void initializePipeline() {
         // OF-DPA does not require initializing the pipeline as it puts default
         // rules automatically in the hardware. However emulation of OFDPA in
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 4f9740c..966933b 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
@@ -18,16 +18,11 @@
 
 import org.onlab.packet.VlanId;
 import org.onosproject.core.ApplicationId;
-import org.onosproject.core.CoreService;
-import org.onosproject.net.DeviceId;
 import org.onosproject.net.behaviour.PipelinerContext;
-import org.onosproject.net.device.DeviceService;
 import org.onosproject.net.flow.FlowRule;
-import org.onosproject.net.flow.FlowRuleService;
 import org.onosproject.net.flow.criteria.PortCriterion;
 import org.onosproject.net.flow.criteria.VlanIdCriterion;
 import org.onosproject.net.flowobjective.ForwardingObjective;
-import org.onosproject.net.group.GroupService;
 
 import java.util.Collection;
 import java.util.List;
@@ -37,24 +32,15 @@
  */
 public class Ofdpa3Pipeline extends Ofdpa2Pipeline {
     @Override
-    public void init(DeviceId deviceId, PipelinerContext context) {
-        this.deviceId = deviceId;
-
-        // Initialize OFDPA group handler
-        groupHandler = new Ofdpa3GroupHandler();
-        groupHandler.init(deviceId, context);
-
-        serviceDirectory = context.directory();
-        coreService = serviceDirectory.get(CoreService.class);
-        flowRuleService = serviceDirectory.get(FlowRuleService.class);
-        groupService = serviceDirectory.get(GroupService.class);
-        flowObjectiveStore = context.store();
-        deviceService = serviceDirectory.get(DeviceService.class);
-
+    protected void initDriverId() {
         driverId = coreService.registerApplication(
                 "org.onosproject.driver.Ofdpa3Pipeline");
+    }
 
-        initializePipeline();
+    @Override
+    protected void initGroupHander(PipelinerContext context) {
+        groupHandler = new Ofdpa3GroupHandler();
+        groupHandler.init(deviceId, context);
     }
 
     @Override
diff --git a/drivers/default/src/main/java/org/onosproject/driver/pipeline/OvsOfdpa2GroupHandler.java b/drivers/default/src/main/java/org/onosproject/driver/pipeline/OvsOfdpa2GroupHandler.java
new file mode 100644
index 0000000..ccc2c86
--- /dev/null
+++ b/drivers/default/src/main/java/org/onosproject/driver/pipeline/OvsOfdpa2GroupHandler.java
@@ -0,0 +1,32 @@
+/*
+ * Copyright 2017-present Open Networking Laboratory
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.onosproject.driver.pipeline;
+
+/**
+ * Group handler that emulates Broadcom OF-DPA TTP on OVS.
+ */
+public class OvsOfdpa2GroupHandler extends CpqdOfdpa2GroupHandler {
+    @Override
+    protected boolean supportCopyTtl() {
+        return false;
+    }
+
+    @Override
+    protected boolean supportSetMplsBos() {
+        return false;
+    }
+}
diff --git a/drivers/default/src/main/java/org/onosproject/driver/pipeline/OvsOfdpa2Pipeline.java b/drivers/default/src/main/java/org/onosproject/driver/pipeline/OvsOfdpa2Pipeline.java
new file mode 100644
index 0000000..3854a91
--- /dev/null
+++ b/drivers/default/src/main/java/org/onosproject/driver/pipeline/OvsOfdpa2Pipeline.java
@@ -0,0 +1,45 @@
+/*
+ * Copyright 2017-present Open Networking Laboratory
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.onosproject.driver.pipeline;
+
+import org.onosproject.net.behaviour.PipelinerContext;
+/**
+ * Driver for software switch emulation of the OFDPA pipeline.
+ * The software switch is the OVS OF 1.3 switch. Unfortunately the OVS switch
+ * does not handle vlan tags and mpls labels simultaneously, which requires us
+ * to do some workarounds in the driver. This driver is meant for the use of
+ * the cpqd switch when MPLS is required. As a result this driver works only
+ * on incoming untagged packets.
+ */
+public class OvsOfdpa2Pipeline extends CpqdOfdpa2Pipeline {
+    @Override
+    protected void initDriverId() {
+        driverId = coreService.registerApplication(
+                "org.onosproject.driver.OvsOfdpa2Pipeline");
+    }
+
+    @Override
+    protected void initGroupHander(PipelinerContext context) {
+        groupHandler = new OvsOfdpa2GroupHandler();
+        groupHandler.init(deviceId, context);
+    }
+
+    @Override
+    protected boolean supportCopyTtl() {
+        return false;
+    }
+}
diff --git a/drivers/default/src/main/java/org/onosproject/driver/pipeline/OvsOfdpa2VlanPipeline.java b/drivers/default/src/main/java/org/onosproject/driver/pipeline/OvsOfdpa2VlanPipeline.java
new file mode 100644
index 0000000..42e50cd
--- /dev/null
+++ b/drivers/default/src/main/java/org/onosproject/driver/pipeline/OvsOfdpa2VlanPipeline.java
@@ -0,0 +1,48 @@
+/*
+ * Copyright 2017-present Open Networking Laboratory
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.onosproject.driver.pipeline;
+
+import org.onosproject.net.behaviour.PipelinerContext;
+
+/**
+ * Driver for software switch emulation of the OFDPA pipeline.
+ * The software switch is the OVS OF 1.3 switch. Unfortunately the OVS switch
+ * does not handle vlan tags and mpls labels simultaneously, which requires us
+ * to do some workarounds in the driver. This driver is meant for the use of
+ * the cpqd switch when MPLS is not a requirement from the ofdpa pipeline. As a
+ * result this driver correctly handles both incoming untagged and vlan-tagged
+ * packets.
+ *
+ */
+public class OvsOfdpa2VlanPipeline extends CpqdOfdpa2Pipeline {
+    @Override
+    protected void initDriverId() {
+        driverId = coreService.registerApplication(
+                "org.onosproject.driver.OvsOfdpa2VlanPipeline");
+    }
+
+    @Override
+    protected void initGroupHander(PipelinerContext context) {
+        groupHandler = new OvsOfdpa2GroupHandler();
+        groupHandler.init(deviceId, context);
+    }
+
+    @Override
+    protected boolean supportCopyTtl() {
+        return false;
+    }
+}
diff --git a/drivers/default/src/main/resources/onos-drivers.xml b/drivers/default/src/main/resources/onos-drivers.xml
index 273204e..c9411b9 100644
--- a/drivers/default/src/main/resources/onos-drivers.xml
+++ b/drivers/default/src/main/resources/onos-drivers.xml
@@ -118,7 +118,8 @@
         <behaviour api="org.onosproject.net.behaviour.ExtensionSelectorResolver"
                    impl="org.onosproject.driver.extensions.OfdpaExtensionSelectorInterpreter" />
     </driver>
-    <!--  Emulation of the ofdpa pipeline using a CPqD OF 1.3 software switch.
+
+    <!--  Emulation of the OFDPA pipeline using a CPqD OF 1.3 software switch.
        ~  Use this driver when MPLS functionality is required.
        ~  To use this driver, configure ONOS with the dpid of the device.
       -->
@@ -128,7 +129,8 @@
         <behaviour api="org.onosproject.net.behaviour.Pipeliner"
                    impl="org.onosproject.driver.pipeline.CpqdOfdpa2Pipeline"/>
     </driver>
-    <!--  Emulation of the ofdpa pipeline using a CPqD OF 1.3 software switch.
+
+    <!--  Emulation of the OFDPA pipeline using a CPqD OF 1.3 software switch.
        ~  Use this driver when VLAN functionality is required.
        ~  To use this driver, configure ONOS with the dpid of the device.
       -->
@@ -139,6 +141,28 @@
                    impl="org.onosproject.driver.pipeline.CpqdOfdpa2VlanPipeline"/>
     </driver>
 
+    <!--  Emulation of the OFDPA pipeline using a OVS OF 1.3 software switch.
+       ~  Use this driver when MPLS functionality is required.
+       ~  To use this driver, configure ONOS with the dpid of the device.
+     -->
+    <driver name="ofdpa-ovs" extends="default"
+            manufacturer="ONF"
+            hwVersion="OFDPA OVS" swVersion="OFDPA OVS">
+        <behaviour api="org.onosproject.net.behaviour.Pipeliner"
+                   impl="org.onosproject.driver.pipeline.OvsOfdpa2Pipeline"/>
+    </driver>
+
+    <!--  Emulation of the OFDPA pipeline using a OVS OF 1.3 software switch.
+       ~  Use this driver when VLAN functionality is required.
+       ~  To use this driver, configure ONOS with the dpid of the device.
+     -->
+    <driver name="ofdpa-ovs-vlan" extends="default"
+            manufacturer="ONF"
+            hwVersion="OFDPA OVS" swVersion="OFDPA OVS">
+        <behaviour api="org.onosproject.net.behaviour.Pipeliner"
+                   impl="org.onosproject.driver.pipeline.OvsOfdpa2VlanPipeline"/>
+    </driver>
+
     <driver name="celestica" extends="default"
             manufacturer="PMC GPON Networks" hwVersion="PAS5211 v2" swVersion="vOLT version 1.5.3.*">
         <behaviour api="org.onosproject.net.behaviour.Pipeliner"