Bug 5540 - FlowConvertor, FlowStatsResponseConvertor, FlowInstructionResponseConvertor
[openflowplugin.git] / openflowplugin-impl / src / main / java / org / opendaylight / openflowplugin / impl / services / SalFlowServiceImpl.java
index cf2a160998321f1077b310cbbf79b142acebecb7..c0ab557425994f2d06426f6820db02fdbb4099ab 100644 (file)
@@ -14,6 +14,7 @@ import com.google.common.util.concurrent.ListenableFuture;
 import java.util.ArrayList;
 import java.util.Collection;
 import java.util.List;
+import java.util.Objects;
 import java.util.concurrent.Future;
 import javax.annotation.Nullable;
 import org.opendaylight.openflowplugin.api.openflow.device.DeviceContext;
@@ -25,7 +26,6 @@ import org.opendaylight.openflowplugin.api.openflow.rpc.ItemLifeCycleSource;
 import org.opendaylight.openflowplugin.api.openflow.rpc.listener.ItemLifecycleListener;
 import org.opendaylight.openflowplugin.impl.registry.flow.FlowDescriptorFactory;
 import org.opendaylight.openflowplugin.impl.registry.flow.FlowRegistryKeyFactory;
-import org.opendaylight.openflowplugin.impl.util.FlowUtil;
 import org.opendaylight.openflowplugin.openflow.md.util.FlowCreatorUtil;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowId;
@@ -76,16 +76,20 @@ public class SalFlowServiceImpl implements SalFlowService, ItemLifeCycleSource {
 
     @Override
     public Future<RpcResult<AddFlowOutput>> addFlow(final AddFlowInput input) {
+        final FlowRegistryKey flowRegistryKey = FlowRegistryKeyFactory.create(input);
         final FlowId flowId;
-        if (null != input.getFlowRef()) {
+        final FlowDescriptor flowDescriptor;
+
+        if (Objects.nonNull(input.getFlowRef())) {
             flowId = input.getFlowRef().getValue().firstKeyOf(Flow.class, FlowKey.class).getId();
+            flowDescriptor = FlowDescriptorFactory.create(input.getTableId(), flowId);
+            deviceContext.getDeviceFlowRegistry().store(flowRegistryKey, flowDescriptor);
         } else {
-            flowId = FlowUtil.createAlienFlowId(input.getTableId());
+            flowId = deviceContext.getDeviceFlowRegistry().storeIfNecessary(flowRegistryKey);
+            flowDescriptor = FlowDescriptorFactory.create(input.getTableId(), flowId);
         }
+
         LOG.trace("Calling add flow for flow with ID ={}.", flowId);
-        final FlowRegistryKey flowRegistryKey = FlowRegistryKeyFactory.create(input);
-        final FlowDescriptor flowDescriptor = FlowDescriptorFactory.create(input.getTableId(), flowId);
-        deviceContext.getDeviceFlowRegistry().store(flowRegistryKey, flowDescriptor);
         final ListenableFuture<RpcResult<AddFlowOutput>> future =
                 flowAdd.processFlowModInputBuilders(flowAdd.toFlowModInputs(input));
         Futures.addCallback(future, new FutureCallback<RpcResult<AddFlowOutput>>() {
@@ -197,18 +201,18 @@ public class SalFlowServiceImpl implements SalFlowService, ItemLifeCycleSource {
                 FlowRegistryKey updatedflowRegistryKey = FlowRegistryKeyFactory.create(updated);
                 final FlowRef flowRef = input.getFlowRef();
                 final DeviceFlowRegistry deviceFlowRegistry = deviceContext.getDeviceFlowRegistry();
-                deviceFlowRegistry.markToBeremoved(flowRegistryKey);
 
-                if (itemLifecycleListener != null) {
-                    final FlowDescriptor flowDescriptor = deviceContext.getDeviceFlowRegistry().retrieveIdForFlow(flowRegistryKey);
-                    if (flowDescriptor != null) {
+                if (flowRef == null) { //then this is equivalent to a delete
+                    deviceFlowRegistry.markToBeremoved(flowRegistryKey);
+
+                    if (itemLifecycleListener != null) {
+                        final FlowDescriptor flowDescriptor =
+                                deviceContext.getDeviceFlowRegistry().retrieveIdForFlow( flowRegistryKey);
                         KeyedInstanceIdentifier<Flow, FlowKey> flowPath = createFlowPath(flowDescriptor,
                                 deviceContext.getDeviceInfo().getNodeInstanceIdentifier());
                         itemLifecycleListener.onRemoved(flowPath);
                     }
-                }
-                //if provided, store flow id to flow registry
-                if (flowRef != null) {
+                } else { //this is either an add or an update
                     final FlowId flowId = flowRef.getValue().firstKeyOf(Flow.class, FlowKey.class).getId();
                     final FlowDescriptor flowDescriptor = FlowDescriptorFactory.create(updated.getTableId(), flowId);
                     deviceFlowRegistry.store(updatedflowRegistryKey, flowDescriptor);
@@ -216,9 +220,19 @@ public class SalFlowServiceImpl implements SalFlowService, ItemLifeCycleSource {
                     if (itemLifecycleListener != null) {
                         KeyedInstanceIdentifier<Flow, FlowKey> flowPath = createFlowPath(flowDescriptor,
                                 deviceContext.getDeviceInfo().getNodeInstanceIdentifier());
-                        final FlowBuilder flowBuilder = new FlowBuilder(input.getUpdatedFlow()).setId(flowDescriptor.getFlowId());
-                        itemLifecycleListener.onAdded(flowPath, flowBuilder.build());
+                        final FlowBuilder flowBuilder = new FlowBuilder(
+                                                    input.getUpdatedFlow()).setId(flowDescriptor.getFlowId());
+
+                        boolean isUpdate = null !=
+                                            deviceFlowRegistry.retrieveIdForFlow(flowRegistryKey);
+                        if (isUpdate) {
+                            itemLifecycleListener.onUpdated(flowPath, flowBuilder.build());
+                        } else {
+                            itemLifecycleListener.onAdded(flowPath, flowBuilder.build());
+                        }
                     }
+
+
                 }
             }