FRM code cleanup.
[openflowplugin.git] / applications / forwardingrules-manager / src / main / java / org / opendaylight / openflowplugin / applications / frm / impl / FlowForwarder.java
index 170fa5551c94e6e7066ce6e37d95e0d5d88f82c1..99935e4f1213bf28db965f7f18290a6a7c386ae3 100644 (file)
@@ -10,7 +10,7 @@ package org.opendaylight.openflowplugin.applications.frm.impl;
 import static org.opendaylight.openflowplugin.applications.frm.util.FrmUtil.buildGroupInstanceIdentifier;
 import static org.opendaylight.openflowplugin.applications.frm.util.FrmUtil.getActiveBundle;
 import static org.opendaylight.openflowplugin.applications.frm.util.FrmUtil.getFlowId;
-import static org.opendaylight.openflowplugin.applications.frm.util.FrmUtil.getNodeIdFromNodeIdentifier;
+import static org.opendaylight.openflowplugin.applications.frm.util.FrmUtil.getNodeIdValueFromNodeIdentifier;
 import static org.opendaylight.openflowplugin.applications.frm.util.FrmUtil.isFlowDependentOnGroup;
 import static org.opendaylight.openflowplugin.applications.frm.util.FrmUtil.isGroupExistsOnDevice;
 
@@ -24,9 +24,8 @@ import com.google.common.util.concurrent.SettableFuture;
 import java.util.Optional;
 import java.util.concurrent.ExecutionException;
 import java.util.concurrent.Future;
-import org.opendaylight.infrautils.utils.concurrent.JdkFutures;
+import org.opendaylight.infrautils.utils.concurrent.LoggingFutures;
 import org.opendaylight.mdsal.binding.api.DataBroker;
-import org.opendaylight.mdsal.binding.api.DataTreeIdentifier;
 import org.opendaylight.mdsal.binding.api.ReadTransaction;
 import org.opendaylight.mdsal.binding.api.WriteTransaction;
 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
@@ -57,7 +56,6 @@ import org.opendaylight.yang.gen.v1.urn.opendaylight.group.service.rev130918.Add
 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.service.rev130918.AddGroupOutput;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.GroupRef;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.groups.Group;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeRef;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
@@ -67,6 +65,7 @@ import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
 import org.opendaylight.yangtools.yang.common.RpcError;
 import org.opendaylight.yangtools.yang.common.RpcResult;
 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
+import org.opendaylight.yangtools.yang.common.Uint32;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -84,28 +83,15 @@ public class FlowForwarder extends AbstractListeningCommiter<Flow> {
     private static final String GROUP_EXISTS_IN_DEVICE_ERROR = "GROUPEXISTS";
 
     private ListenerRegistration<FlowForwarder> listenerRegistration;
+
     private final BundleFlowForwarder bundleFlowForwarder;
 
-    public FlowForwarder(final ForwardingRulesManager manager, final DataBroker db) {
-        super(manager, db);
+    public FlowForwarder(final ForwardingRulesManager manager, final DataBroker db,
+                         final ListenerRegistrationHelper registrationHelper) {
+        super(manager, db, registrationHelper);
         bundleFlowForwarder = new BundleFlowForwarder(manager);
     }
 
-    @Override
-    @SuppressWarnings("IllegalCatch")
-    public void registerListener() {
-        final DataTreeIdentifier<Flow> treeId = DataTreeIdentifier.create(LogicalDatastoreType.CONFIGURATION,
-                getWildCardPath());
-        try {
-            listenerRegistration = dataBroker.registerDataTreeChangeListener(treeId, FlowForwarder.this);
-        } catch (final Exception e) {
-            LOG.warn("FRM Flow DataTreeChange listener registration fail!");
-            LOG.debug("FRM Flow DataTreeChange listener registration fail ..", e);
-            throw new IllegalStateException("FlowForwarder startup fail! System needs restart.", e);
-        }
-    }
-
-
     @Override
     public  void deregisterListener() {
         close();
@@ -127,21 +113,25 @@ public class FlowForwarder extends AbstractListeningCommiter<Flow> {
         if (tableIdValidationPrecondition(tableKey, removeDataObj)) {
             BundleId bundleId = getActiveBundle(nodeIdent, provider);
             if (bundleId != null) {
-                bundleFlowForwarder.remove(identifier, removeDataObj, nodeIdent, bundleId);
+                provider.getBundleFlowListener().remove(identifier, removeDataObj, nodeIdent, bundleId);
             } else {
-                final RemoveFlowInputBuilder builder = new RemoveFlowInputBuilder(removeDataObj);
-                builder.setFlowRef(new FlowRef(identifier));
-                builder.setNode(new NodeRef(nodeIdent.firstIdentifierOf(Node.class)));
-                builder.setFlowTable(new FlowTableRef(nodeIdent.child(Table.class, tableKey)));
-
-                // This method is called only when a given flow object has been
-                // removed from datastore. So FRM always needs to set strict flag
-                // into remove-flow input so that only a flow entry associated with
-                // a given flow object is removed.
-                builder.setTransactionUri(new Uri(provider.getNewTransactionId())).setStrict(Boolean.TRUE);
-                final Future<RpcResult<RemoveFlowOutput>> resultFuture =
-                        provider.getSalFlowService().removeFlow(builder.build());
-                JdkFutures.addErrorLogging(resultFuture, LOG, "removeFlow");
+                final String nodeId = getNodeIdValueFromNodeIdentifier(nodeIdent);
+                nodeConfigurator.enqueueJob(nodeId, () -> {
+                    final RemoveFlowInputBuilder builder = new RemoveFlowInputBuilder(removeDataObj);
+                    builder.setFlowRef(new FlowRef(identifier));
+                    builder.setNode(new NodeRef(nodeIdent.firstIdentifierOf(Node.class)));
+                    builder.setFlowTable(new FlowTableRef(nodeIdent.child(Table.class, tableKey)));
+
+                    // This method is called only when a given flow object has been
+                    // removed from datastore. So FRM always needs to set strict flag
+                    // into remove-flow input so that only a flow entry associated with
+                    // a given flow object is removed.
+                    builder.setTransactionUri(new Uri(provider.getNewTransactionId())).setStrict(Boolean.TRUE);
+                    final ListenableFuture<RpcResult<RemoveFlowOutput>> resultFuture =
+                            provider.getSalFlowService().removeFlow(builder.build());
+                    LoggingFutures.addErrorLogging(resultFuture, LOG, "removeFlow");
+                    return resultFuture;
+                });
             }
         }
     }
@@ -149,10 +139,10 @@ public class FlowForwarder extends AbstractListeningCommiter<Flow> {
     // TODO: Pull this into ForwardingRulesCommiter and override it here
 
     @Override
-    public Future<RpcResult<RemoveFlowOutput>> removeWithResult(final InstanceIdentifier<Flow> identifier,
+    public ListenableFuture<RpcResult<RemoveFlowOutput>> removeWithResult(final InstanceIdentifier<Flow> identifier,
             final Flow removeDataObj, final InstanceIdentifier<FlowCapableNode> nodeIdent) {
 
-        Future<RpcResult<RemoveFlowOutput>> resultFuture = SettableFuture.create();
+        ListenableFuture<RpcResult<RemoveFlowOutput>> resultFuture = SettableFuture.create();
         final TableKey tableKey = identifier.firstKeyOf(Table.class);
         if (tableIdValidationPrecondition(tableKey, removeDataObj)) {
             final RemoveFlowInputBuilder builder = new RemoveFlowInputBuilder(removeDataObj);
@@ -179,10 +169,10 @@ public class FlowForwarder extends AbstractListeningCommiter<Flow> {
         if (tableIdValidationPrecondition(tableKey, update)) {
             BundleId bundleId = getActiveBundle(nodeIdent, provider);
             if (bundleId != null) {
-                bundleFlowForwarder.update(identifier, original, update, nodeIdent, bundleId);
+                provider.getBundleFlowListener().update(identifier, original, update, nodeIdent, bundleId);
             } else {
-                final NodeId nodeId = getNodeIdFromNodeIdentifier(nodeIdent);
-                nodeConfigurator.enqueueJob(nodeId.getValue(), () -> {
+                final String nodeId = getNodeIdValueFromNodeIdentifier(nodeIdent);
+                nodeConfigurator.enqueueJob(nodeId, () -> {
                     final UpdateFlowInputBuilder builder = new UpdateFlowInputBuilder();
                     builder.setNode(new NodeRef(nodeIdent.firstIdentifierOf(Node.class)));
                     builder.setFlowRef(new FlowRef(identifier));
@@ -195,13 +185,13 @@ public class FlowForwarder extends AbstractListeningCommiter<Flow> {
                     builder.setUpdatedFlow(new UpdatedFlowBuilder(update).setStrict(Boolean.TRUE).build());
                     builder.setOriginalFlow(new OriginalFlowBuilder(original).setStrict(Boolean.TRUE).build());
 
-                    Long groupId = isFlowDependentOnGroup(update);
+                    Uint32 groupId = isFlowDependentOnGroup(update);
                     if (groupId != null) {
                         LOG.trace("The flow {} is dependent on group {}. Checking if the group is already present",
-                                getFlowId(new FlowRef(identifier)), groupId);
+                                getFlowId(identifier), groupId);
                         if (isGroupExistsOnDevice(nodeIdent, groupId, provider)) {
                             LOG.trace("The dependent group {} is already programmed. Updating the flow {}", groupId,
-                                    getFlowId(new FlowRef(identifier)));
+                                    getFlowId(identifier));
                             return provider.getSalFlowService().updateFlow(builder.build());
                         } else {
                             LOG.trace("The dependent group {} isn't programmed yet. Pushing the group", groupId);
@@ -216,7 +206,7 @@ public class FlowForwarder extends AbstractListeningCommiter<Flow> {
                     }
 
                     LOG.trace("The flow {} is not dependent on any group. Updating the flow",
-                            getFlowId(new FlowRef(identifier)));
+                            getFlowId(identifier));
                     return provider.getSalFlowService().updateFlow(builder.build());
                 });
             }
@@ -231,17 +221,17 @@ public class FlowForwarder extends AbstractListeningCommiter<Flow> {
         if (tableIdValidationPrecondition(tableKey, addDataObj)) {
             BundleId bundleId = getActiveBundle(nodeIdent, provider);
             if (bundleId != null) {
-                return bundleFlowForwarder.add(identifier, addDataObj, nodeIdent, bundleId);
+                return provider.getBundleFlowListener().add(identifier, addDataObj, nodeIdent, bundleId);
             } else {
-                final NodeId nodeId = getNodeIdFromNodeIdentifier(nodeIdent);
-                nodeConfigurator.enqueueJob(nodeId.getValue(), () -> {
+                final String nodeId = getNodeIdValueFromNodeIdentifier(nodeIdent);
+                nodeConfigurator.enqueueJob(nodeId, () -> {
                     final AddFlowInputBuilder builder = new AddFlowInputBuilder(addDataObj);
 
                     builder.setNode(new NodeRef(nodeIdent.firstIdentifierOf(Node.class)));
                     builder.setFlowRef(new FlowRef(identifier));
                     builder.setFlowTable(new FlowTableRef(nodeIdent.child(Table.class, tableKey)));
                     builder.setTransactionUri(new Uri(provider.getNewTransactionId()));
-                    Long groupId = isFlowDependentOnGroup(addDataObj);
+                    Uint32 groupId = isFlowDependentOnGroup(addDataObj);
                     if (groupId != null) {
                         LOG.trace("The flow {} is dependent on group {}. Checking if the group is already present",
                                 getFlowId(new FlowRef(identifier)), groupId);
@@ -272,7 +262,7 @@ public class FlowForwarder extends AbstractListeningCommiter<Flow> {
     @Override
     public void createStaleMarkEntity(InstanceIdentifier<Flow> identifier, Flow del,
             InstanceIdentifier<FlowCapableNode> nodeIdent) {
-        LOG.debug("Creating Stale-Mark entry for the switch {} for flow {} ", nodeIdent.toString(), del.toString());
+        LOG.debug("Creating Stale-Mark entry for the switch {} for flow {} ", nodeIdent, del);
         StaleFlow staleFlow = makeStaleFlow(identifier, del, nodeIdent);
         persistStaleFlow(staleFlow, nodeIdent);
     }
@@ -294,7 +284,7 @@ public class FlowForwarder extends AbstractListeningCommiter<Flow> {
         return true;
     }
 
-    private StaleFlow makeStaleFlow(InstanceIdentifier<Flow> identifier, Flow del,
+    private static StaleFlow makeStaleFlow(InstanceIdentifier<Flow> identifier, Flow del,
             InstanceIdentifier<FlowCapableNode> nodeIdent) {
         StaleFlowBuilder staleFlowBuilder = new StaleFlowBuilder(del);
         return staleFlowBuilder.setId(del.getId()).build();
@@ -303,13 +293,13 @@ public class FlowForwarder extends AbstractListeningCommiter<Flow> {
     private void persistStaleFlow(StaleFlow staleFlow, InstanceIdentifier<FlowCapableNode> nodeIdent) {
         WriteTransaction writeTransaction = dataBroker.newWriteOnlyTransaction();
         writeTransaction.put(LogicalDatastoreType.CONFIGURATION, getStaleFlowInstanceIdentifier(staleFlow, nodeIdent),
-                staleFlow, false);
+                staleFlow);
 
         FluentFuture<?> submitFuture = writeTransaction.commit();
         handleStaleFlowResultFuture(submitFuture);
     }
 
-    private void handleStaleFlowResultFuture(FluentFuture<?> submitFuture) {
+    private static void handleStaleFlowResultFuture(FluentFuture<?> submitFuture) {
         submitFuture.addCallback(new FutureCallback<Object>() {
             @Override
             public void onSuccess(Object result) {
@@ -318,13 +308,13 @@ public class FlowForwarder extends AbstractListeningCommiter<Flow> {
 
             @Override
             public void onFailure(Throwable throwable) {
-                LOG.error("Stale Flow creation failed {}", throwable);
+                LOG.error("Stale Flow creation failed", throwable);
             }
         }, MoreExecutors.directExecutor());
 
     }
 
-    private InstanceIdentifier<org.opendaylight.yang.gen.v1.urn.opendaylight
+    private static InstanceIdentifier<org.opendaylight.yang.gen.v1.urn.opendaylight
         .flow.inventory.rev130819.tables.table.StaleFlow> getStaleFlowInstanceIdentifier(
             StaleFlow staleFlow, InstanceIdentifier<FlowCapableNode> nodeIdent) {
         return nodeIdent.child(Table.class, new TableKey(staleFlow.getTableId())).child(
@@ -333,7 +323,7 @@ public class FlowForwarder extends AbstractListeningCommiter<Flow> {
     }
 
     private ListenableFuture<RpcResult<AddGroupOutput>> pushDependentGroup(
-            final InstanceIdentifier<FlowCapableNode> nodeIdent, final Long groupId) {
+            final InstanceIdentifier<FlowCapableNode> nodeIdent, final Uint32 groupId) {
 
         //TODO This read to the DS might have a performance impact.
         //if the dependent group is not installed than we should just cache the parent group,
@@ -367,11 +357,11 @@ public class FlowForwarder extends AbstractListeningCommiter<Flow> {
 
     private final class AddFlowCallBack implements FutureCallback<RpcResult<AddGroupOutput>> {
         private final AddFlowInput addFlowInput;
-        private final NodeId nodeId;
-        private final Long groupId;
+        private final String nodeId;
+        private final Uint32 groupId;
         private final SettableFuture<RpcResult<AddFlowOutput>> resultFuture;
 
-        private AddFlowCallBack(final AddFlowInput addFlowInput, final NodeId nodeId, Long groupId,
+        private AddFlowCallBack(final AddFlowInput addFlowInput, final String nodeId, Uint32 groupId,
                 SettableFuture<RpcResult<AddFlowOutput>> resultFuture) {
             this.addFlowInput = addFlowInput;
             this.nodeId = nodeId;
@@ -401,7 +391,7 @@ public class FlowForwarder extends AbstractListeningCommiter<Flow> {
                         getFlowId(addFlowInput.getFlowRef()), nodeId);
             } else {
                 LOG.error("Flow add with id {} failed for node {} with error {}", getFlowId(addFlowInput.getFlowRef()),
-                        nodeId, rpcResult.getErrors().toString());
+                        nodeId, rpcResult.getErrors());
                 resultFuture.set(RpcResultBuilder.<AddFlowOutput>failed()
                         .withRpcErrors(rpcResult.getErrors()).build());
             }
@@ -417,12 +407,12 @@ public class FlowForwarder extends AbstractListeningCommiter<Flow> {
 
     private final class UpdateFlowCallBack implements FutureCallback<RpcResult<AddGroupOutput>> {
         private final UpdateFlowInput updateFlowInput;
-        private final NodeId nodeId;
-        private final Long groupId;
+        private final String nodeId;
+        private final Uint32 groupId;
         private final SettableFuture<RpcResult<UpdateFlowOutput>> resultFuture;
 
-        private UpdateFlowCallBack(final UpdateFlowInput updateFlowInput, final NodeId nodeId,
-                SettableFuture<RpcResult<UpdateFlowOutput>> resultFuture, Long groupId) {
+        private UpdateFlowCallBack(final UpdateFlowInput updateFlowInput, final String nodeId,
+                SettableFuture<RpcResult<UpdateFlowOutput>> resultFuture, Uint32 groupId) {
             this.updateFlowInput = updateFlowInput;
             this.nodeId = nodeId;
             this.groupId = groupId;
@@ -451,7 +441,7 @@ public class FlowForwarder extends AbstractListeningCommiter<Flow> {
                         getFlowId(updateFlowInput.getFlowRef()), nodeId);
             } else {
                 LOG.error("Flow update with id {} failed for node {} with error {}",
-                        getFlowId(updateFlowInput.getFlowRef()), nodeId, rpcResult.getErrors().toString());
+                        getFlowId(updateFlowInput.getFlowRef()), nodeId, rpcResult.getErrors());
                 resultFuture.set(RpcResultBuilder.<UpdateFlowOutput>failed()
                         .withRpcErrors(rpcResult.getErrors()).build());
             }
@@ -464,4 +454,4 @@ public class FlowForwarder extends AbstractListeningCommiter<Flow> {
             resultFuture.setException(throwable);
         }
     }
-}
+}
\ No newline at end of file