Fix findbugs violations in applications
[openflowplugin.git] / applications / forwardingrules-manager / src / main / java / org / opendaylight / openflowplugin / applications / frm / impl / FlowForwarder.java
index 5d939a96624d14a3cc4392e8d8887d62c0c8e2f1..c3cc81cde40c5024f674cfb9214ccc46957fd0ef 100644 (file)
@@ -1,5 +1,5 @@
 /**
- * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
+ * Copyright (c) 2014, 2017 Cisco Systems, Inc. and others.  All rights reserved.
  *
  * This program and the accompanying materials are made available under the
  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
@@ -11,14 +11,15 @@ import com.google.common.base.Preconditions;
 import com.google.common.util.concurrent.CheckedFuture;
 import com.google.common.util.concurrent.FutureCallback;
 import com.google.common.util.concurrent.Futures;
+import com.google.common.util.concurrent.MoreExecutors;
 import com.google.common.util.concurrent.SettableFuture;
-import java.util.concurrent.Callable;
 import java.util.concurrent.Future;
 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
 import org.opendaylight.controller.md.sal.binding.api.DataTreeIdentifier;
 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
 import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException;
+import org.opendaylight.infrautils.utils.concurrent.JdkFutures;
 import org.opendaylight.openflowplugin.applications.frm.ForwardingRulesManager;
 import org.opendaylight.openflowplugin.common.wait.SimpleTaskRetryLooper;
 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Uri;
@@ -31,10 +32,12 @@ import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.ta
 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.StaleFlowBuilder;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.StaleFlowKey;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.AddFlowInputBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.AddFlowOutput;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.FlowTableRef;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.RemoveFlowInputBuilder;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.RemoveFlowOutput;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.UpdateFlowInputBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.UpdateFlowOutput;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.flow.update.OriginalFlowBuilder;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.flow.update.UpdatedFlowBuilder;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.FlowRef;
@@ -48,37 +51,33 @@ import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
 /**
- * FlowForwarder
- * It implements {@link org.opendaylight.controller.md.sal.binding.api.DataTreeChangeListener}}
- * for WildCardedPath to {@link Flow} and ForwardingRulesCommiter interface for methods:
- *  add, update and remove {@link Flow} processing for
- *  {@link org.opendaylight.controller.md.sal.common.api.data.AsyncDataChangeEvent}.
+ * FlowForwarder It implements
+ * {@link org.opendaylight.controller.md.sal.binding.api.DataTreeChangeListener}
+ * for WildCardedPath to {@link Flow} and ForwardingRulesCommiter interface for
+ * methods: add, update and remove {@link Flow} processing for
+ * {@link org.opendaylight.controller.md.sal.binding.api.DataTreeModification}.
  */
 public class FlowForwarder extends AbstractListeningCommiter<Flow> {
 
     private static final Logger LOG = LoggerFactory.getLogger(FlowForwarder.class);
-
     private final DataBroker dataBroker;
-
     private ListenerRegistration<FlowForwarder> listenerRegistration;
 
-    public FlowForwarder (final ForwardingRulesManager manager, final DataBroker db) {
-        super(manager, Flow.class);
+    public FlowForwarder(final ForwardingRulesManager manager, final DataBroker db) {
+        super(manager);
         dataBroker = Preconditions.checkNotNull(db, "DataBroker can not be null!");
         registrationListener(db);
     }
 
+    @SuppressWarnings("IllegalCatch")
     private void registrationListener(final DataBroker db) {
-        final DataTreeIdentifier<Flow> treeId = new DataTreeIdentifier<>(LogicalDatastoreType.CONFIGURATION, getWildCardPath());
+        final DataTreeIdentifier<Flow> treeId = new DataTreeIdentifier<>(LogicalDatastoreType.CONFIGURATION,
+                getWildCardPath());
         try {
             SimpleTaskRetryLooper looper = new SimpleTaskRetryLooper(ForwardingRulesManagerImpl.STARTUP_LOOP_TICK,
                     ForwardingRulesManagerImpl.STARTUP_LOOP_MAX_RETRIES);
-            listenerRegistration = looper.loopUntilNoException(new Callable<ListenerRegistration<FlowForwarder>>() {
-                @Override
-                public ListenerRegistration<FlowForwarder> call() throws Exception {
-                    return db.registerDataTreeChangeListener(treeId, FlowForwarder.this);
-                }
-            });
+            listenerRegistration = looper
+                    .loopUntilNoException(() -> db.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);
@@ -89,20 +88,14 @@ public class FlowForwarder extends AbstractListeningCommiter<Flow> {
     @Override
     public void close() {
         if (listenerRegistration != null) {
-            try {
-                listenerRegistration.close();
-            } catch (final Exception e) {
-                LOG.warn("Error by stop FRM FlowChangeListener: {}", e.getMessage());
-                LOG.debug("Error by stop FRM FlowChangeListener..", e);
-            }
+            listenerRegistration.close();
             listenerRegistration = null;
         }
     }
 
     @Override
-    public void remove(final InstanceIdentifier<Flow> identifier,
-                       final Flow removeDataObj,
-                       final InstanceIdentifier<FlowCapableNode> nodeIdent) {
+    public void remove(final InstanceIdentifier<Flow> identifier, final Flow removeDataObj,
+            final InstanceIdentifier<FlowCapableNode> nodeIdent) {
 
         final TableKey tableKey = identifier.firstKeyOf(Table.class, TableKey.class);
         if (tableIdValidationPrecondition(tableKey, removeDataObj)) {
@@ -115,21 +108,18 @@ public class FlowForwarder extends AbstractListeningCommiter<Flow> {
             // 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);
-            provider.getSalFlowService().removeFlow(builder.build());
+            builder.setTransactionUri(new Uri(provider.getNewTransactionId())).setStrict(Boolean.TRUE);
+            final Future<RpcResult<RemoveFlowOutput>> resultFuture =
+                    provider.getSalFlowService().removeFlow(builder.build());
+            JdkFutures.addErrorLogging(resultFuture, LOG, "removeFlow");
         }
     }
 
-
-
-
-    //TODO: Pull this into ForwardingRulesCommiter and override it here
+    // TODO: Pull this into ForwardingRulesCommiter and override it here
 
     @Override
     public Future<RpcResult<RemoveFlowOutput>> removeWithResult(final InstanceIdentifier<Flow> identifier,
-                       final Flow removeDataObj,
-                       final InstanceIdentifier<FlowCapableNode> nodeIdent) {
+            final Flow removeDataObj, final InstanceIdentifier<FlowCapableNode> nodeIdent) {
 
         Future<RpcResult<RemoveFlowOutput>> resultFuture = SettableFuture.create();
         final TableKey tableKey = identifier.firstKeyOf(Table.class, TableKey.class);
@@ -143,20 +133,16 @@ public class FlowForwarder extends AbstractListeningCommiter<Flow> {
             // 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);
+            builder.setTransactionUri(new Uri(provider.getNewTransactionId())).setStrict(Boolean.TRUE);
             resultFuture = provider.getSalFlowService().removeFlow(builder.build());
         }
 
         return resultFuture;
     }
 
-
-
     @Override
-    public void update(final InstanceIdentifier<Flow> identifier,
-                       final Flow original, final Flow update,
-                       final InstanceIdentifier<FlowCapableNode> nodeIdent) {
+    public void update(final InstanceIdentifier<Flow> identifier, final Flow original, final Flow update,
+            final InstanceIdentifier<FlowCapableNode> nodeIdent) {
 
         final TableKey tableKey = identifier.firstKeyOf(Table.class, TableKey.class);
         if (tableIdValidationPrecondition(tableKey, update)) {
@@ -170,18 +156,20 @@ public class FlowForwarder extends AbstractListeningCommiter<Flow> {
             // has been updated. So FRM always needs to set strict flag into
             // update-flow input so that only a flow entry associated with
             // a given flow object is updated.
-            builder.setUpdatedFlow((new UpdatedFlowBuilder(update)).setStrict(Boolean.TRUE).build());
-            builder.setOriginalFlow((new OriginalFlowBuilder(original)).setStrict(Boolean.TRUE).build());
+            builder.setUpdatedFlow(new UpdatedFlowBuilder(update).setStrict(Boolean.TRUE).build());
+            builder.setOriginalFlow(new OriginalFlowBuilder(original).setStrict(Boolean.TRUE).build());
 
-            provider.getSalFlowService().updateFlow(builder.build());
+            final Future<RpcResult<UpdateFlowOutput>> resultFuture =
+                    provider.getSalFlowService().updateFlow(builder.build());
+            JdkFutures.addErrorLogging(resultFuture, LOG, "updateFlow");
         }
     }
 
     @Override
-    public void add(final InstanceIdentifier<Flow> identifier,
-                    final Flow addDataObj,
-                    final InstanceIdentifier<FlowCapableNode> nodeIdent) {
+    public Future<RpcResult<AddFlowOutput>> add(final InstanceIdentifier<Flow> identifier, final Flow addDataObj,
+            final InstanceIdentifier<FlowCapableNode> nodeIdent) {
 
+        Future<RpcResult<AddFlowOutput>> future;
         final TableKey tableKey = identifier.firstKeyOf(Table.class, TableKey.class);
         if (tableIdValidationPrecondition(tableKey, addDataObj)) {
             final AddFlowInputBuilder builder = new AddFlowInputBuilder(addDataObj);
@@ -190,12 +178,17 @@ public class FlowForwarder extends AbstractListeningCommiter<Flow> {
             builder.setFlowRef(new FlowRef(identifier));
             builder.setFlowTable(new FlowTableRef(nodeIdent.child(Table.class, tableKey)));
             builder.setTransactionUri(new Uri(provider.getNewTransactionId()));
-            provider.getSalFlowService().addFlow(builder.build());
+            future = provider.getSalFlowService().addFlow(builder.build());
+        } else {
+            future = Futures.<RpcResult<AddFlowOutput>>immediateFuture(null);
         }
+
+        return future;
     }
 
     @Override
-    public void createStaleMarkEntity(InstanceIdentifier<Flow> identifier, Flow del, InstanceIdentifier<FlowCapableNode> nodeIdent) {
+    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());
 
         StaleFlow staleFlow = makeStaleFlow(identifier, del, nodeIdent);
@@ -203,33 +196,33 @@ public class FlowForwarder extends AbstractListeningCommiter<Flow> {
 
     }
 
-
-
     @Override
     protected InstanceIdentifier<Flow> getWildCardPath() {
-        return InstanceIdentifier.create(Nodes.class).child(Node.class)
-                .augmentation(FlowCapableNode.class).child(Table.class).child(Flow.class);
+        return InstanceIdentifier.create(Nodes.class).child(Node.class).augmentation(FlowCapableNode.class)
+                .child(Table.class).child(Flow.class);
     }
 
-    private static boolean tableIdValidationPrecondition (final TableKey tableKey, final Flow flow) {
+    private static boolean tableIdValidationPrecondition(final TableKey tableKey, final Flow flow) {
         Preconditions.checkNotNull(tableKey, "TableKey can not be null or empty!");
         Preconditions.checkNotNull(flow, "Flow can not be null or empty!");
-        if (! tableKey.getId().equals(flow.getTableId())) {
-            LOG.warn("TableID in URI tableId={} and in palyload tableId={} is not same.",
-                    flow.getTableId(), tableKey.getId());
+        if (!tableKey.getId().equals(flow.getTableId())) {
+            LOG.warn("TableID in URI tableId={} and in palyload tableId={} is not same.", flow.getTableId(),
+                    tableKey.getId());
             return false;
         }
         return true;
     }
 
-    private StaleFlow makeStaleFlow(InstanceIdentifier<Flow> identifier, Flow del, InstanceIdentifier<FlowCapableNode> nodeIdent){
-         StaleFlowBuilder staleFlowBuilder = new StaleFlowBuilder(del);
+    private StaleFlow makeStaleFlow(InstanceIdentifier<Flow> identifier, Flow del,
+            InstanceIdentifier<FlowCapableNode> nodeIdent) {
+        StaleFlowBuilder staleFlowBuilder = new StaleFlowBuilder(del);
         return staleFlowBuilder.setId(del.getId()).build();
     }
 
-    private void persistStaleFlow(StaleFlow staleFlow, InstanceIdentifier<FlowCapableNode> nodeIdent){
+    private void persistStaleFlow(StaleFlow staleFlow, InstanceIdentifier<FlowCapableNode> nodeIdent) {
         WriteTransaction writeTransaction = dataBroker.newWriteOnlyTransaction();
-        writeTransaction.put(LogicalDatastoreType.CONFIGURATION, getStaleFlowInstanceIdentifier(staleFlow, nodeIdent), staleFlow, false);
+        writeTransaction.put(LogicalDatastoreType.CONFIGURATION, getStaleFlowInstanceIdentifier(staleFlow, nodeIdent),
+                staleFlow, false);
 
         CheckedFuture<Void, TransactionCommitFailedException> submitFuture = writeTransaction.submit();
         handleStaleFlowResultFuture(submitFuture);
@@ -243,18 +236,18 @@ public class FlowForwarder extends AbstractListeningCommiter<Flow> {
             }
 
             @Override
-            public void onFailure(Throwable t) {
-                LOG.error("Stale Flow creation failed {}", t);
+            public void onFailure(Throwable throwable) {
+                LOG.error("Stale Flow creation failed {}", throwable);
             }
-        });
+        }, MoreExecutors.directExecutor());
 
     }
 
-    private 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(org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.StaleFlow.class,
-                        new StaleFlowKey(new FlowId(staleFlow.getId())));
+    private 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(
+                org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.StaleFlow.class,
+                new StaleFlowKey(new FlowId(staleFlow.getId())));
     }
 }
-