Bug 5540 - FlowConvertor, FlowStatsResponseConvertor, FlowInstructionResponseConvertor
[openflowplugin.git] / openflowplugin-impl / src / main / java / org / opendaylight / openflowplugin / impl / services / SalFlowServiceImpl.java
index 4c16bf01c37ddbb8e9e8151346681c64527b2e6c..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,37 +76,44 @@ 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);
         }
 
-        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));
+        LOG.trace("Calling add flow for flow with ID ={}.", flowId);
+        final ListenableFuture<RpcResult<AddFlowOutput>> future =
+                flowAdd.processFlowModInputBuilders(flowAdd.toFlowModInputs(input));
         Futures.addCallback(future, new FutureCallback<RpcResult<AddFlowOutput>>() {
             @Override
             public void onSuccess(final RpcResult<AddFlowOutput> rpcResult) {
                 if (rpcResult.isSuccessful()) {
-                    LOG.debug("flow add finished without error, id={}", flowId.getValue());
+                    if(LOG.isDebugEnabled()) {
+                        LOG.debug("flow add with id={},finished without error,", flowId.getValue());
+                    }
                     if (itemLifecycleListener != null) {
                         KeyedInstanceIdentifier<Flow, FlowKey> flowPath = createFlowPath(flowDescriptor,
-                                deviceContext.getDeviceState().getNodeInstanceIdentifier());
+                                deviceContext.getDeviceInfo().getNodeInstanceIdentifier());
                         final FlowBuilder flowBuilder = new FlowBuilder(input).setId(flowDescriptor.getFlowId());
                         itemLifecycleListener.onAdded(flowPath, flowBuilder.build());
                     }
                 } else {
-                    LOG.debug("flow add failed with error, id={}", flowId.getValue());
-                }
+                LOG.error("flow add failed for id={}, errors={}", flowId.getValue(), errorsToString(rpcResult.getErrors()));
+            }
             }
 
             @Override
             public void onFailure(final Throwable throwable) {
                 deviceContext.getDeviceFlowRegistry().markToBeremoved(flowRegistryKey);
-                LOG.trace("Service call for adding flows failed, id={}.", flowId.getValue(), throwable);
+                LOG.error("Service call for adding flow with  id={} failed, reason {} .", flowId.getValue(), throwable);
             }
         });
 
@@ -117,44 +124,50 @@ public class SalFlowServiceImpl implements SalFlowService, ItemLifeCycleSource {
     public Future<RpcResult<RemoveFlowOutput>> removeFlow(final RemoveFlowInput input) {
         LOG.trace("Calling remove flow for flow with ID ={}.", input.getFlowRef());
 
-        final ListenableFuture<RpcResult<RemoveFlowOutput>> future = flowRemove.processFlowModInputBuilders(flowRemove.toFlowModInputs(input));
+        final ListenableFuture<RpcResult<RemoveFlowOutput>> future =
+                flowRemove.processFlowModInputBuilders(flowRemove.toFlowModInputs(input));
         Futures.addCallback(future, new FutureCallback<RpcResult<RemoveFlowOutput>>() {
             @Override
             public void onSuccess(final RpcResult<RemoveFlowOutput> result) {
                 if (result.isSuccessful()) {
+                    if(LOG.isDebugEnabled()) {
+                        LOG.debug("flow removed finished without error,");
+                    }
                     FlowRegistryKey flowRegistryKey = FlowRegistryKeyFactory.create(input);
                     deviceContext.getDeviceFlowRegistry().markToBeremoved(flowRegistryKey);
                     if (itemLifecycleListener != null) {
-                        final FlowDescriptor flowDescriptor = deviceContext.getDeviceFlowRegistry().retrieveIdForFlow(flowRegistryKey);
+                        final FlowDescriptor flowDescriptor =
+                                deviceContext.getDeviceFlowRegistry().retrieveIdForFlow(flowRegistryKey);
                         if (flowDescriptor != null) {
                             KeyedInstanceIdentifier<Flow, FlowKey> flowPath = createFlowPath(flowDescriptor,
-                                    deviceContext.getDeviceState().getNodeInstanceIdentifier());
+                                    deviceContext.getDeviceInfo().getNodeInstanceIdentifier());
                             itemLifecycleListener.onRemoved(flowPath);
                         }
                     }
                 } else {
-                    if (LOG.isTraceEnabled()) {
-                        StringBuilder errors = new StringBuilder();
-                        Collection<RpcError> rpcErrors = result.getErrors();
-                        if (null != rpcErrors && rpcErrors.size() > 0) {
-                            for (RpcError rpcError : rpcErrors) {
-                                errors.append(rpcError.getMessage());
-                            }
-                        }
-                        LOG.trace("Flow modification failed. Errors : {}", errors.toString());
-                    }
+                    LOG.error("Flow remove failed with errors : {}",errorsToString(result.getErrors()));
                 }
             }
 
             @Override
             public void onFailure(final Throwable throwable) {
-                LOG.trace("Flow modification failed..", throwable);
+                LOG.error("Service call for removing flow with id {} failed ,reason {}",input.getFlowRef().getValue(), throwable);
             }
         });
 
         return future;
     }
 
+    private final String errorsToString(final Collection<RpcError> rpcErrors) {
+        final StringBuilder errors = new StringBuilder();
+        if ((null != rpcErrors) && (rpcErrors.size() > 0)) {
+            for (final RpcError rpcError : rpcErrors) {
+                errors.append(rpcError.getMessage());
+            }
+        }
+        return errors.toString();
+    }
+
     @Override
     public Future<RpcResult<UpdateFlowOutput>> updateFlow(final UpdateFlowInput input) {
         final UpdateFlowInput in = input;
@@ -188,34 +201,44 @@ 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.getDeviceState().getNodeInstanceIdentifier());
+                                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);
 
                     if (itemLifecycleListener != null) {
                         KeyedInstanceIdentifier<Flow, FlowKey> flowPath = createFlowPath(flowDescriptor,
-                                deviceContext.getDeviceState().getNodeInstanceIdentifier());
-                        final FlowBuilder flowBuilder = new FlowBuilder(input.getUpdatedFlow()).setId(flowDescriptor.getFlowId());
-                        itemLifecycleListener.onAdded(flowPath, flowBuilder.build());
+                                deviceContext.getDeviceInfo().getNodeInstanceIdentifier());
+                        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());
+                        }
                     }
+
+
                 }
             }
 
             @Override
             public void onFailure(final Throwable throwable) {
-                LOG.debug("Flow update failed", throwable);
+                LOG.error("Service call for updating flow failed, reason{}", throwable);
             }
         });
         return future;