Remove dead code from DeviceFlowRegistryImpl
[openflowplugin.git] / openflowplugin-impl / src / main / java / org / opendaylight / openflowplugin / impl / registry / flow / DeviceFlowRegistryImpl.java
index 0c3b65e92012d49058f1ef04af73649b3a273246..bd628ff9407383fd0b2faa75f0dff2db323d6ee6 100644 (file)
@@ -8,29 +8,29 @@
 package org.opendaylight.openflowplugin.impl.registry.flow;
 
 import com.google.common.annotations.VisibleForTesting;
-import com.google.common.base.Optional;
 import com.google.common.collect.BiMap;
 import com.google.common.collect.HashBiMap;
 import com.google.common.collect.Maps;
-import com.google.common.util.concurrent.CheckedFuture;
+import com.google.common.util.concurrent.FluentFuture;
 import com.google.common.util.concurrent.FutureCallback;
 import com.google.common.util.concurrent.Futures;
 import com.google.common.util.concurrent.ListenableFuture;
 import com.google.common.util.concurrent.MoreExecutors;
 import java.util.ArrayList;
 import java.util.Arrays;
+import java.util.Collections;
 import java.util.Iterator;
 import java.util.List;
 import java.util.Map;
 import java.util.Objects;
+import java.util.Optional;
 import java.util.concurrent.atomic.AtomicInteger;
 import java.util.function.Consumer;
 import javax.annotation.Nonnull;
 import javax.annotation.concurrent.ThreadSafe;
-import org.opendaylight.controller.md.sal.binding.api.DataBroker;
-import org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction;
-import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
-import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
+import org.opendaylight.mdsal.binding.api.DataBroker;
+import org.opendaylight.mdsal.binding.api.ReadTransaction;
+import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
 import org.opendaylight.openflowplugin.api.openflow.registry.flow.DeviceFlowRegistry;
 import org.opendaylight.openflowplugin.api.openflow.registry.flow.FlowDescriptor;
 import org.opendaylight.openflowplugin.api.openflow.registry.flow.FlowRegistryKey;
@@ -85,13 +85,13 @@ public class DeviceFlowRegistryImpl implements DeviceFlowRegistry {
         final InstanceIdentifier<FlowCapableNode> path = instanceIdentifier.augmentation(FlowCapableNode.class);
 
         // First, try to fill registry with flows from DS/Configuration
-        final CheckedFuture<Optional<FlowCapableNode>, ReadFailedException> configFuture =
+        final FluentFuture<Optional<FlowCapableNode>> configFuture =
                 fillFromDatastore(LogicalDatastoreType.CONFIGURATION, path);
 
         // Now, try to fill registry with flows from DS/Operational
         // in case of cluster fail over, when clients are not using DS/Configuration
         // for adding flows, but only RPCs
-        final CheckedFuture<Optional<FlowCapableNode>, ReadFailedException> operationalFuture =
+        final FluentFuture<Optional<FlowCapableNode>> operationalFuture =
                 fillFromDatastore(LogicalDatastoreType.OPERATIONAL, path);
 
         // And at last, chain and return futures created above.
@@ -103,50 +103,32 @@ public class DeviceFlowRegistryImpl implements DeviceFlowRegistry {
         return lastFillFuture;
     }
 
-    private CheckedFuture<Optional<FlowCapableNode>, ReadFailedException>
-            fillFromDatastore(final LogicalDatastoreType logicalDatastoreType,
+    private FluentFuture<Optional<FlowCapableNode>> fillFromDatastore(final LogicalDatastoreType logicalDatastoreType,
                               final InstanceIdentifier<FlowCapableNode> path) {
-        // Create new read-only transaction
-        final ReadOnlyTransaction transaction = dataBroker.newReadOnlyTransaction();
-
-        // Bail out early if transaction is null
-        if (transaction == null) {
-            return Futures.immediateFailedCheckedFuture(
-                    new ReadFailedException("Read transaction is null"));
-        }
-
         // Prepare read operation from datastore for path
-        final CheckedFuture<Optional<FlowCapableNode>, ReadFailedException> future =
-                transaction.read(logicalDatastoreType, path);
-
-        // Bail out early if future is null
-        if (future == null) {
-            return Futures.immediateFailedCheckedFuture(
-                    new ReadFailedException("Future from read transaction is null"));
+        final FluentFuture<Optional<FlowCapableNode>> future;
+        try (ReadTransaction transaction = dataBroker.newReadOnlyTransaction()) {
+            future = transaction.read(logicalDatastoreType, path);
         }
 
-        Futures.addCallback(future, new FutureCallback<Optional<FlowCapableNode>>() {
+        future.addCallback(new FutureCallback<Optional<FlowCapableNode>>() {
             @Override
             public void onSuccess(Optional<FlowCapableNode> result) {
-                result.asSet().stream()
+                result.map(Collections::singleton).orElse(Collections.emptySet()).stream()
                         .filter(Objects::nonNull)
-                        .filter(flowCapableNode -> Objects.nonNull(flowCapableNode.getTable()))
+                        .filter(flowCapableNode -> flowCapableNode.getTable() != null)
                         .flatMap(flowCapableNode -> flowCapableNode.getTable().stream())
                         .filter(Objects::nonNull)
-                        .filter(table -> Objects.nonNull(table.getFlow()))
+                        .filter(table -> table.getFlow() != null)
                         .flatMap(table -> table.getFlow().stream())
                         .filter(Objects::nonNull)
-                        .filter(flow -> Objects.nonNull(flow.getId()))
+                        .filter(flow -> flow.getId() != null)
                         .forEach(flowConsumer);
-
-                // After we are done with reading from datastore, close the transaction
-                transaction.close();
             }
 
             @Override
             public void onFailure(Throwable throwable) {
-                // Even when read operation failed, close the transaction
-                transaction.close();
+                LOG.debug("Failed to read {} path {}", logicalDatastoreType, path, throwable);
             }
         }, MoreExecutors.directExecutor());
 
@@ -197,11 +179,8 @@ public class DeviceFlowRegistryImpl implements DeviceFlowRegistry {
 
     @Override
     public void store(final FlowRegistryKey flowRegistryKey) {
-        if (Objects.isNull(retrieveDescriptor(flowRegistryKey))) {
-            if (LOG.isDebugEnabled()) {
-                LOG.debug("Flow descriptor for flow hash : {} not found, generating alien flow ID",
-                        flowRegistryKey.toString());
-            }
+        if (retrieveDescriptor(flowRegistryKey) == null) {
+            LOG.debug("Flow descriptor for flow hash : {} not found, generating alien flow ID", flowRegistryKey);
 
             // We do not found flow in flow registry, that means it do not have any ID already assigned, so we need
             // to generate new alien flow ID here.
@@ -285,14 +264,16 @@ public class DeviceFlowRegistryImpl implements DeviceFlowRegistry {
     }
 
     private FlowRegistryKey getExistingKey(final FlowRegistryKey flowRegistryKey) {
-        if (flowRegistryKey.getMatch().getAugmentation(GeneralAugMatchNodesNodeTableFlow.class) == null) {
+        if (flowRegistryKey.getMatch().augmentation(GeneralAugMatchNodesNodeTableFlow.class) == null) {
             if (flowRegistry.containsKey(flowRegistryKey)) {
                 return flowRegistryKey;
             }
         } else {
-            for (Map.Entry<FlowRegistryKey, FlowDescriptor> keyValueSet : flowRegistry.entrySet()) {
-                if (keyValueSet.getKey().equals(flowRegistryKey)) {
-                    return keyValueSet.getKey();
+            synchronized (flowRegistry) {
+                for (Map.Entry<FlowRegistryKey, FlowDescriptor> keyValueSet : flowRegistry.entrySet()) {
+                    if (keyValueSet.getKey().equals(flowRegistryKey)) {
+                        return keyValueSet.getKey();
+                    }
                 }
             }
         }