Update MRI projects for Aluminium
[openflowplugin.git] / openflowplugin-impl / src / main / java / org / opendaylight / openflowplugin / impl / registry / flow / DeviceFlowRegistryImpl.java
index 8940655b556e9972d9dc0396280e0136f5bd2732..0fee0d39aadaeda0f5280055282574e5ee43514b 100644 (file)
@@ -18,7 +18,6 @@ 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;
@@ -26,12 +25,10 @@ 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.eclipse.jdt.annotation.NonNull;
 import org.opendaylight.mdsal.binding.api.DataBroker;
 import org.opendaylight.mdsal.binding.api.ReadTransaction;
 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
-import org.opendaylight.mdsal.common.api.ReadFailedException;
 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;
@@ -41,13 +38,15 @@ import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.ta
 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.general.rev140714.GeneralAugMatchNodesNodeTableFlow;
-import org.opendaylight.yangtools.util.concurrent.FluentFutures;
 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
 import org.opendaylight.yangtools.yang.binding.KeyedInstanceIdentifier;
+import org.opendaylight.yangtools.yang.common.Uint8;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
-@ThreadSafe
+/*
+ * this class is marked to be thread safe
+ */
 public class DeviceFlowRegistryImpl implements DeviceFlowRegistry {
     private static final Logger LOG = LoggerFactory.getLogger(DeviceFlowRegistryImpl.class);
     private static final String ALIEN_SYSTEM_FLOW_ID = "#UF$TABLE*";
@@ -107,46 +106,28 @@ public class DeviceFlowRegistryImpl implements DeviceFlowRegistry {
 
     private FluentFuture<Optional<FlowCapableNode>> fillFromDatastore(final LogicalDatastoreType logicalDatastoreType,
                               final InstanceIdentifier<FlowCapableNode> path) {
-        // Create new read-only transaction
-        final ReadTransaction transaction = dataBroker.newReadOnlyTransaction();
-
-        // Bail out early if transaction is null
-        if (transaction == null) {
-            return FluentFutures.immediateFailedFluentFuture(
-                    new ReadFailedException("Read transaction is null"));
-        }
-
         // Prepare read operation from datastore for path
-        final FluentFuture<Optional<FlowCapableNode>> future = transaction.read(logicalDatastoreType, path);
-
-        // Bail out early if future is null
-        if (future == null) {
-            return FluentFutures.immediateFailedFluentFuture(
-                    new ReadFailedException("Future from read transaction is null"));
+        final FluentFuture<Optional<FlowCapableNode>> future;
+        try (ReadTransaction transaction = dataBroker.newReadOnlyTransaction()) {
+            future = transaction.read(logicalDatastoreType, path);
         }
 
         future.addCallback(new FutureCallback<Optional<FlowCapableNode>>() {
             @Override
-            public void onSuccess(Optional<FlowCapableNode> result) {
-                result.map(Collections::singleton).orElse(Collections.emptySet()).stream()
-                        .filter(Objects::nonNull)
-                        .filter(flowCapableNode -> Objects.nonNull(flowCapableNode.getTable()))
-                        .flatMap(flowCapableNode -> flowCapableNode.getTable().stream())
-                        .filter(Objects::nonNull)
-                        .filter(table -> Objects.nonNull(table.getFlow()))
-                        .flatMap(table -> table.getFlow().stream())
-                        .filter(Objects::nonNull)
-                        .filter(flow -> Objects.nonNull(flow.getId()))
-                        .forEach(flowConsumer);
-
-                // After we are done with reading from datastore, close the transaction
-                transaction.close();
+            public void onSuccess(final Optional<FlowCapableNode> result) {
+                result.ifPresent(flowCapableNode -> {
+                    flowCapableNode.nonnullTable().values().stream()
+                    .filter(Objects::nonNull)
+                    .flatMap(table -> table.nonnullFlow().values().stream())
+                    .filter(Objects::nonNull)
+                    .filter(flow -> flow.getId() != null)
+                    .forEach(flowConsumer);
+                });
             }
 
             @Override
-            public void onFailure(Throwable throwable) {
-                // Even when read operation failed, close the transaction
-                transaction.close();
+            public void onFailure(final Throwable throwable) {
+                LOG.debug("Failed to read {} path {}", logicalDatastoreType, path, throwable);
             }
         }, MoreExecutors.directExecutor());
 
@@ -154,7 +135,7 @@ public class DeviceFlowRegistryImpl implements DeviceFlowRegistry {
     }
 
     @Override
-    public FlowDescriptor retrieveDescriptor(@Nonnull final FlowRegistryKey flowRegistryKey) {
+    public FlowDescriptor retrieveDescriptor(@NonNull final FlowRegistryKey flowRegistryKey) {
         if (LOG.isTraceEnabled()) {
             LOG.trace("Retrieving flow descriptor for flow registry : {}", flowRegistryKey.toString());
         }
@@ -167,8 +148,8 @@ public class DeviceFlowRegistryImpl implements DeviceFlowRegistry {
     }
 
     @Override
-    public void storeDescriptor(@Nonnull final FlowRegistryKey flowRegistryKey,
-                                @Nonnull final FlowDescriptor flowDescriptor) {
+    public void storeDescriptor(@NonNull final FlowRegistryKey flowRegistryKey,
+                                @NonNull final FlowDescriptor flowDescriptor) {
         try {
             if (LOG.isTraceEnabled()) {
                 LOG.trace("Storing flowDescriptor with table ID : {} and flow ID : {} for flow hash : {}",
@@ -202,11 +183,8 @@ public class DeviceFlowRegistryImpl implements DeviceFlowRegistry {
 
             // 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.
-            storeDescriptor(
-                    flowRegistryKey,
-                    FlowDescriptorFactory.create(
-                            flowRegistryKey.getTableId(),
-                            createAlienFlowId(flowRegistryKey.getTableId())));
+            final Uint8 tableId = Uint8.valueOf(flowRegistryKey.getTableId());
+            storeDescriptor(flowRegistryKey, FlowDescriptorFactory.create(tableId, createAlienFlowId(tableId)));
         }
     }
 
@@ -252,7 +230,7 @@ public class DeviceFlowRegistryImpl implements DeviceFlowRegistry {
     }
 
     @VisibleForTesting
-    static FlowId createAlienFlowId(final short tableId) {
+    static FlowId createAlienFlowId(final Uint8 tableId) {
         final String alienId = ALIEN_SYSTEM_FLOW_ID + tableId + '-' + UNACCOUNTED_FLOWS_COUNTER.incrementAndGet();
         LOG.debug("Created alien flow id {} for table id {}", alienId, tableId);
         return new FlowId(alienId);