Bug 7453 - FlowRemoved doesn't have Removed Reason Information
[openflowplugin.git] / openflowplugin-impl / src / main / java / org / opendaylight / openflowplugin / impl / translator / FlowRemovedTranslator.java
index c5683d2ebc171a071b446bbae668efa65be1a9e5..86d4c9c3a40983334157f3a031b8bb1bd69f7787 100644 (file)
@@ -7,20 +7,34 @@
  */
 package org.opendaylight.openflowplugin.impl.translator;
 
+import java.util.Objects;
+import java.util.Optional;
 import org.opendaylight.openflowplugin.api.openflow.device.DeviceInfo;
 import org.opendaylight.openflowplugin.api.openflow.device.MessageTranslator;
-import org.opendaylight.openflowplugin.api.openflow.md.util.OpenflowVersion;
-import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.match.MatchConvertorImpl;
+import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.ConvertorExecutor;
+import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.data.VersionDatapathIdConvertorData;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.FlowRemovedBuilder;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.FlowCookie;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.RemovedFlowReason;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.MatchBuilder;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeRef;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.FlowRemoved;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
 /**
  * translate {@link FlowRemoved} message to FlowRemoved notification (omit instructions)
  */
 public class FlowRemovedTranslator implements MessageTranslator<FlowRemoved, org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.FlowRemoved> {
+    private final ConvertorExecutor convertorExecutor;
+    private static final Logger logger = LoggerFactory.getLogger(FlowRemovedTranslator.class);
+    public FlowRemovedTranslator(ConvertorExecutor convertorExecutor) {
+        this.convertorExecutor = convertorExecutor;
+    }
+
+    protected ConvertorExecutor getConvertorExecutor() {
+        return convertorExecutor;
+    }
 
     @Override
     public org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.FlowRemoved translate(FlowRemoved input, DeviceInfo deviceInfo, Object connectionDistinguisher) {
@@ -31,12 +45,22 @@ public class FlowRemovedTranslator implements MessageTranslator<FlowRemoved, org
                 .setPriority(input.getPriority())
                 .setTableId(translateTableId(input));
 
+        if(Objects.nonNull(input.getReason())) {
+            flowRemovedBld.setReason(translateReason(input));
+        }
+
         return flowRemovedBld.build();
     }
 
     protected MatchBuilder translateMatch(FlowRemoved flowRemoved, DeviceInfo deviceInfo) {
-        return MatchConvertorImpl.fromOFMatchToSALMatch(flowRemoved.getMatch(),
-                deviceInfo.getDatapathId(), OpenflowVersion.OF13);
+        final VersionDatapathIdConvertorData datapathIdConvertorData = new VersionDatapathIdConvertorData(deviceInfo.getVersion());
+        datapathIdConvertorData.setDatapathId(deviceInfo.getDatapathId());
+
+        final Optional<MatchBuilder> matchBuilderOptional = getConvertorExecutor().convert(
+                flowRemoved.getMatch(),
+                datapathIdConvertorData);
+
+        return matchBuilderOptional.orElse(new MatchBuilder());
     }
 
     /**
@@ -48,4 +72,22 @@ public class FlowRemovedTranslator implements MessageTranslator<FlowRemoved, org
     protected Short translateTableId(FlowRemoved flowRemoved) {
         return flowRemoved.getTableId().getValue().shortValue();
     }
+
+
+    private RemovedFlowReason translateReason(FlowRemoved removedFlow) {
+        logger.debug("--Entering translateReason within FlowRemovedTranslator with reason:{} " + removedFlow.getReason());
+        switch (removedFlow.getReason()) {
+            case OFPRRIDLETIMEOUT:
+                return RemovedFlowReason.OFPRRIDLETIMEOUT;
+            case OFPRRHARDTIMEOUT:
+                return RemovedFlowReason.OFPRRHARDTIMEOUT;
+            case OFPRRDELETE:
+                return RemovedFlowReason.OFPRRDELETE;
+            case OFPRRGROUPDELETE:
+                return RemovedFlowReason.OFPRRGROUPDELETE;
+            default:
+                logger.debug("The flow is being deleted for some unknown reason  ");
+                return RemovedFlowReason.OFPRRDELETE;
+        }
+    }
 }