Cisco Switch pipelines should filter the clear deffered action

Change-Id: Ia578a81dc111f5fe9ecba7e9f657efc3e6e94238
diff --git a/drivers/default/src/main/java/org/onosproject/driver/pipeline/CiscoN9kPipeliner.java b/drivers/default/src/main/java/org/onosproject/driver/pipeline/CiscoN9kPipeliner.java
new file mode 100644
index 0000000..60f27ca
--- /dev/null
+++ b/drivers/default/src/main/java/org/onosproject/driver/pipeline/CiscoN9kPipeliner.java
@@ -0,0 +1,92 @@
+/*
+ * Copyright 2018-present Open Networking Foundation
+ *
+ * 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.onlab.osgi.ServiceDirectory;
+import org.onosproject.net.Device;
+import org.onosproject.net.DeviceId;
+import org.onosproject.net.behaviour.PipelinerContext;
+import org.onosproject.net.device.DeviceService;
+import org.onosproject.net.flow.DefaultTrafficTreatment;
+import org.onosproject.net.flow.TrafficTreatment;
+import org.onosproject.net.flowobjective.DefaultForwardingObjective;
+import org.onosproject.net.flowobjective.ForwardingObjective;
+import org.slf4j.Logger;
+
+import java.util.Optional;
+
+import static org.slf4j.LoggerFactory.getLogger;
+
+/**
+ * Cisco N9K Switch single table pipeline abstraction.
+ */
+public class CiscoN9kPipeliner extends DefaultSingleTablePipeline {
+
+    private final Logger log = getLogger(getClass());
+    private ServiceDirectory serviceDirectory;
+    private DeviceId deviceId;
+
+    protected DeviceService deviceService;
+    @Override
+    public void init(DeviceId deviceId, PipelinerContext context) {
+        this.deviceId = deviceId;
+        this.serviceDirectory = context.directory();
+        this.deviceService = serviceDirectory.get(DeviceService.class);
+    }
+
+
+    @Override
+    public void forward(ForwardingObjective forwardObjective) {
+        ForwardingObjective newFwd = forwardObjective;
+        Device device = deviceService.getDevice(deviceId);
+
+        if (forwardObjective.treatment() != null && forwardObjective.treatment().clearedDeferred()) {
+            log.warn("Using 'clear actions' instruction which is not supported by {} {} {} Switch"
+                            + " removing the clear deferred from the forwarding objective",
+                    device.id(), device.manufacturer(), device.hwVersion());
+            newFwd = forwardingObjectiveWithoutCleardDef(forwardObjective).orElse(forwardObjective);
+        }
+
+        super.forward(newFwd);
+    }
+
+
+    private Optional<ForwardingObjective> forwardingObjectiveWithoutCleardDef(ForwardingObjective forwardingObjective) {
+        TrafficTreatment treatment = trafficTreatmentWithoutClearedDeffered(forwardingObjective.treatment());
+
+        DefaultForwardingObjective.Builder foBuilder = (DefaultForwardingObjective.Builder) forwardingObjective.copy();
+        foBuilder.withTreatment(treatment);
+
+        switch (forwardingObjective.op()) {
+            case ADD:
+                return Optional.of(foBuilder.add(forwardingObjective.context().orElse(null)));
+            case REMOVE:
+                return Optional.of(foBuilder.remove(forwardingObjective.context().orElse(null)));
+            default:
+                log.warn("Driver does not support other operations for forwarding objective");
+                return Optional.empty();
+        }
+
+    }
+
+
+    private TrafficTreatment trafficTreatmentWithoutClearedDeffered(TrafficTreatment treatment) {
+        return DefaultTrafficTreatment.builder(treatment)
+                .notWipeDeferred()
+                .build();
+    }
+
+}
diff --git a/drivers/default/src/main/resources/onos-drivers.xml b/drivers/default/src/main/resources/onos-drivers.xml
index 3768b12..9316057 100644
--- a/drivers/default/src/main/resources/onos-drivers.xml
+++ b/drivers/default/src/main/resources/onos-drivers.xml
@@ -295,5 +295,11 @@
                    impl="org.onosproject.driver.pipeline.JuniperPipeliner"/>
     </driver>
 
+    <driver name="ciscoof" extends="default"
+        manufacturer="Cisco Systems, Inc." hwVersion=".*" swVersion="7.0\(3\).*">
+        <behaviour api="org.onosproject.net.behaviour.Pipeliner"
+           impl="org.onosproject.driver.pipeline.CiscoN9kPipeliner" />
+    </driver>
+
 </drivers>