Bump upstreams
[openflowplugin.git] / applications / forwardingrules-manager / src / main / java / org / opendaylight / openflowplugin / applications / frm / impl / FlowForwarder.java
index cd4219f437cf04ec319e637cbf5ec48ab6d55bbe..6ff32f3089242bcef2f60886aa850a0f4d5dd90a 100644 (file)
@@ -7,6 +7,7 @@
  */
 package org.opendaylight.openflowplugin.applications.frm.impl;
 
+import static java.util.Objects.requireNonNull;
 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;
@@ -14,7 +15,6 @@ import static org.opendaylight.openflowplugin.applications.frm.util.FrmUtil.getN
 import static org.opendaylight.openflowplugin.applications.frm.util.FrmUtil.isFlowDependentOnGroup;
 import static org.opendaylight.openflowplugin.applications.frm.util.FrmUtil.isGroupExistsOnDevice;
 
-import com.google.common.base.Preconditions;
 import com.google.common.util.concurrent.FluentFuture;
 import com.google.common.util.concurrent.FutureCallback;
 import com.google.common.util.concurrent.Futures;
@@ -26,7 +26,6 @@ import java.util.concurrent.ExecutionException;
 import java.util.concurrent.Future;
 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;
@@ -61,9 +60,8 @@ 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;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.onf.rev170124.BundleId;
-import org.opendaylight.yangtools.concepts.ListenerRegistration;
 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
-import org.opendaylight.yangtools.yang.common.RpcError;
+import org.opendaylight.yangtools.yang.common.ErrorType;
 import org.opendaylight.yangtools.yang.common.RpcResult;
 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
 import org.opendaylight.yangtools.yang.common.Uint32;
@@ -78,43 +76,12 @@ import org.slf4j.LoggerFactory;
  * {@link org.opendaylight.mdsal.binding.api.DataTreeModification}.
  */
 public class FlowForwarder extends AbstractListeningCommiter<Flow> {
-
     private static final Logger LOG = LoggerFactory.getLogger(FlowForwarder.class);
-
     private static final String GROUP_EXISTS_IN_DEVICE_ERROR = "GROUPEXISTS";
 
-    private ListenerRegistration<FlowForwarder> listenerRegistration;
-
-    public FlowForwarder(final ForwardingRulesManager manager, final DataBroker db) {
-        super(manager, db);
-    }
-
-    @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();
-    }
-
-    @Override
-    public void close() {
-        if (listenerRegistration != null) {
-            listenerRegistration.close();
-            listenerRegistration = null;
-        }
+    public FlowForwarder(final ForwardingRulesManager manager, final DataBroker db,
+                         final ListenerRegistrationHelper registrationHelper) {
+        super(manager, db, registrationHelper);
     }
 
     @Override
@@ -272,8 +239,8 @@ public class FlowForwarder extends AbstractListeningCommiter<Flow> {
     }
 
     @Override
-    public void createStaleMarkEntity(InstanceIdentifier<Flow> identifier, Flow del,
-            InstanceIdentifier<FlowCapableNode> nodeIdent) {
+    public void createStaleMarkEntity(final InstanceIdentifier<Flow> identifier, final Flow del,
+            final InstanceIdentifier<FlowCapableNode> nodeIdent) {
         LOG.debug("Creating Stale-Mark entry for the switch {} for flow {} ", nodeIdent, del);
         StaleFlow staleFlow = makeStaleFlow(identifier, del, nodeIdent);
         persistStaleFlow(staleFlow, nodeIdent);
@@ -286,8 +253,8 @@ public class FlowForwarder extends AbstractListeningCommiter<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!");
+        requireNonNull(tableKey, "TableKey can not be null or empty!");
+        requireNonNull(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());
@@ -296,13 +263,13 @@ public class FlowForwarder extends AbstractListeningCommiter<Flow> {
         return true;
     }
 
-    private StaleFlow makeStaleFlow(InstanceIdentifier<Flow> identifier, Flow del,
-            InstanceIdentifier<FlowCapableNode> nodeIdent) {
+    private static StaleFlow makeStaleFlow(final InstanceIdentifier<Flow> identifier, final Flow del,
+            final 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(final StaleFlow staleFlow, final InstanceIdentifier<FlowCapableNode> nodeIdent) {
         WriteTransaction writeTransaction = dataBroker.newWriteOnlyTransaction();
         writeTransaction.put(LogicalDatastoreType.CONFIGURATION, getStaleFlowInstanceIdentifier(staleFlow, nodeIdent),
                 staleFlow);
@@ -311,24 +278,24 @@ public class FlowForwarder extends AbstractListeningCommiter<Flow> {
         handleStaleFlowResultFuture(submitFuture);
     }
 
-    private void handleStaleFlowResultFuture(FluentFuture<?> submitFuture) {
+    private static void handleStaleFlowResultFuture(final FluentFuture<?> submitFuture) {
         submitFuture.addCallback(new FutureCallback<Object>() {
             @Override
-            public void onSuccess(Object result) {
+            public void onSuccess(final Object result) {
                 LOG.debug("Stale Flow creation success");
             }
 
             @Override
-            public void onFailure(Throwable throwable) {
+            public void onFailure(final Throwable 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) {
+            final StaleFlow staleFlow, final 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())));
@@ -347,22 +314,22 @@ public class FlowForwarder extends AbstractListeningCommiter<Flow> {
         try (ReadTransaction readTransaction = provider.getReadTransaction()) {
             Optional<Group> group = readTransaction.read(LogicalDatastoreType.CONFIGURATION, groupIdent).get();
             if (group.isPresent()) {
-                final AddGroupInputBuilder builder = new AddGroupInputBuilder(group.get());
+                final AddGroupInputBuilder builder = new AddGroupInputBuilder(group.orElseThrow());
                 builder.setNode(new NodeRef(nodeIdent.firstIdentifierOf(Node.class)));
                 builder.setGroupRef(new GroupRef(nodeIdent));
                 builder.setTransactionUri(new Uri(provider.getNewTransactionId()));
                 AddGroupInput addGroupInput = builder.build();
-                resultFuture = this.provider.getSalGroupService().addGroup(addGroupInput);
+                resultFuture = provider.getSalGroupService().addGroup(addGroupInput);
             } else {
-                resultFuture = Futures.immediateFuture(RpcResultBuilder.<AddGroupOutput>failed()
-                        .withError(RpcError.ErrorType.APPLICATION,
-                                "Group " + groupId + " not present in the config inventory").build());
+                resultFuture = RpcResultBuilder.<AddGroupOutput>failed()
+                        .withError(ErrorType.APPLICATION,
+                                "Group " + groupId + " not present in the config inventory").buildFuture();
             }
         } catch (InterruptedException | ExecutionException e) {
             LOG.error("Error while reading group from config datastore for the group ID {}", groupId, e);
-            resultFuture = Futures.immediateFuture(RpcResultBuilder.<AddGroupOutput>failed()
-                    .withError(RpcError.ErrorType.APPLICATION,
-                            "Error while reading group " + groupId + " from inventory").build());
+            resultFuture = RpcResultBuilder.<AddGroupOutput>failed()
+                    .withError(ErrorType.APPLICATION,
+                            "Error while reading group " + groupId + " from inventory").buildFuture();
         }
         return resultFuture;
     }
@@ -373,8 +340,8 @@ public class FlowForwarder extends AbstractListeningCommiter<Flow> {
         private final Uint32 groupId;
         private final SettableFuture<RpcResult<AddFlowOutput>> resultFuture;
 
-        private AddFlowCallBack(final AddFlowInput addFlowInput, final String nodeId, Uint32 groupId,
-                SettableFuture<RpcResult<AddFlowOutput>> resultFuture) {
+        private AddFlowCallBack(final AddFlowInput addFlowInput, final String nodeId, final Uint32 groupId,
+                final SettableFuture<RpcResult<AddFlowOutput>> resultFuture) {
             this.addFlowInput = addFlowInput;
             this.nodeId = nodeId;
             this.groupId = groupId;
@@ -382,19 +349,19 @@ public class FlowForwarder extends AbstractListeningCommiter<Flow> {
         }
 
         @Override
-        public void onSuccess(RpcResult<AddGroupOutput> rpcResult) {
+        public void onSuccess(final RpcResult<AddGroupOutput> rpcResult) {
             if (rpcResult.isSuccessful() || rpcResult.getErrors().size() == 1
                     && rpcResult.getErrors().iterator().next().getMessage().contains(GROUP_EXISTS_IN_DEVICE_ERROR)) {
                 provider.getDevicesGroupRegistry().storeGroup(nodeId, groupId);
                 Futures.addCallback(provider.getSalFlowService().addFlow(addFlowInput),
                     new FutureCallback<RpcResult<AddFlowOutput>>() {
                         @Override
-                        public void onSuccess(RpcResult<AddFlowOutput> result) {
+                        public void onSuccess(final RpcResult<AddFlowOutput> result) {
                             resultFuture.set(result);
                         }
 
                         @Override
-                        public void onFailure(Throwable failure) {
+                        public void onFailure(final Throwable failure) {
                             resultFuture.setException(failure);
                         }
                     },  MoreExecutors.directExecutor());
@@ -410,7 +377,7 @@ public class FlowForwarder extends AbstractListeningCommiter<Flow> {
         }
 
         @Override
-        public void onFailure(Throwable throwable) {
+        public void onFailure(final Throwable throwable) {
             LOG.error("Service call for adding flow with id {} failed for node {}",
                     getFlowId(addFlowInput.getFlowRef()), nodeId, throwable);
             resultFuture.setException(throwable);
@@ -424,7 +391,7 @@ public class FlowForwarder extends AbstractListeningCommiter<Flow> {
         private final SettableFuture<RpcResult<UpdateFlowOutput>> resultFuture;
 
         private UpdateFlowCallBack(final UpdateFlowInput updateFlowInput, final String nodeId,
-                SettableFuture<RpcResult<UpdateFlowOutput>> resultFuture, Uint32 groupId) {
+                final SettableFuture<RpcResult<UpdateFlowOutput>> resultFuture, final Uint32 groupId) {
             this.updateFlowInput = updateFlowInput;
             this.nodeId = nodeId;
             this.groupId = groupId;
@@ -432,19 +399,19 @@ public class FlowForwarder extends AbstractListeningCommiter<Flow> {
         }
 
         @Override
-        public void onSuccess(RpcResult<AddGroupOutput> rpcResult) {
+        public void onSuccess(final RpcResult<AddGroupOutput> rpcResult) {
             if (rpcResult.isSuccessful() || rpcResult.getErrors().size() == 1
                     && rpcResult.getErrors().iterator().next().getMessage().contains(GROUP_EXISTS_IN_DEVICE_ERROR)) {
                 provider.getDevicesGroupRegistry().storeGroup(nodeId, groupId);
                 Futures.addCallback(provider.getSalFlowService().updateFlow(updateFlowInput),
                     new FutureCallback<RpcResult<UpdateFlowOutput>>() {
                         @Override
-                        public void onSuccess(RpcResult<UpdateFlowOutput> result) {
+                        public void onSuccess(final RpcResult<UpdateFlowOutput> result) {
                             resultFuture.set(result);
                         }
 
                         @Override
-                        public void onFailure(Throwable failure) {
+                        public void onFailure(final Throwable failure) {
                             resultFuture.setException(failure);
                         }
                     },  MoreExecutors.directExecutor());
@@ -460,10 +427,10 @@ public class FlowForwarder extends AbstractListeningCommiter<Flow> {
         }
 
         @Override
-        public void onFailure(Throwable throwable) {
+        public void onFailure(final Throwable throwable) {
             LOG.error("Service call for updating flow with id {} failed for node {}",
                     getFlowId(updateFlowInput.getFlowRef()), nodeId, throwable);
             resultFuture.setException(throwable);
         }
     }
-}
+}
\ No newline at end of file