Fixup Augmentable and Identifiable methods changing
[openflowplugin.git] / openflowplugin-impl / src / main / java / org / opendaylight / openflowplugin / impl / registry / flow / DeviceFlowRegistryImpl.java
index 257c5187a64544bbdf7125ae7f26a2a7b29b0d8f..83fd9b367f2bb688d7d9924365f5b82a09036175 100644 (file)
@@ -16,15 +16,17 @@ 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.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.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;
@@ -37,11 +39,13 @@ import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.Fl
 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.Flow;
 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.yang.binding.InstanceIdentifier;
 import org.opendaylight.yangtools.yang.binding.KeyedInstanceIdentifier;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
+@ThreadSafe
 public class DeviceFlowRegistryImpl implements DeviceFlowRegistry {
     private static final Logger LOG = LoggerFactory.getLogger(DeviceFlowRegistryImpl.class);
     private static final String ALIEN_SYSTEM_FLOW_ID = "#UF$TABLE*";
@@ -51,50 +55,57 @@ public class DeviceFlowRegistryImpl implements DeviceFlowRegistry {
     private final DataBroker dataBroker;
     private final KeyedInstanceIdentifier<Node, NodeKey> instanceIdentifier;
     private final List<ListenableFuture<List<Optional<FlowCapableNode>>>> lastFillFutures = new ArrayList<>();
+    private final Consumer<Flow> flowConsumer;
 
-    // Specifies what to do with flow read from datastore
-    private final Consumer<Flow> flowConsumer = flow -> {
-        // Create flow registry key from flow
-        final FlowRegistryKey key = FlowRegistryKeyFactory.create(flow);
-
-        // Now, we will update the registry, but we will also try to prevent duplicate entries
-        if (!flowRegistry.containsKey(key)) {
-            LOG.trace("Found flow with table ID : {} and flow ID : {}", flow.getTableId(), flow.getId().getValue());
-            final FlowDescriptor descriptor = FlowDescriptorFactory.create(flow.getTableId(), flow.getId());
-            store(key, descriptor);
-        }
-    };
-
-    public DeviceFlowRegistryImpl(final DataBroker dataBroker, final KeyedInstanceIdentifier<Node, NodeKey> instanceIdentifier) {
+    public DeviceFlowRegistryImpl(final short version,
+                                  final DataBroker dataBroker,
+                                  final KeyedInstanceIdentifier<Node, NodeKey> instanceIdentifier) {
         this.dataBroker = dataBroker;
         this.instanceIdentifier = instanceIdentifier;
+
+        // Specifies what to do with flow read from data store
+        flowConsumer = flow -> {
+            final FlowRegistryKey flowRegistryKey = FlowRegistryKeyFactory.create(version, flow);
+
+            if (getExistingKey(flowRegistryKey) == null) {
+                // Now, we will update the registry
+                storeDescriptor(flowRegistryKey, FlowDescriptorFactory.create(flow.getTableId(), flow.getId()));
+            }
+        };
     }
 
     @Override
     public ListenableFuture<List<Optional<FlowCapableNode>>> fill() {
-        LOG.debug("Filling flow registry with flows for node: {}", instanceIdentifier.getKey().getId().getValue());
+        if (LOG.isDebugEnabled()) {
+            LOG.debug("Filling flow registry with flows for node: {}", instanceIdentifier.getKey().getId().getValue());
+        }
 
         // Prepare path for read transaction
         // TODO: Read only Tables, and not entire FlowCapableNode (fix Yang model)
         final InstanceIdentifier<FlowCapableNode> path = instanceIdentifier.augmentation(FlowCapableNode.class);
 
         // First, try to fill registry with flows from DS/Configuration
-        CheckedFuture<Optional<FlowCapableNode>, ReadFailedException> configFuture = fillFromDatastore(LogicalDatastoreType.CONFIGURATION, path);
+        final CheckedFuture<Optional<FlowCapableNode>, ReadFailedException> 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
-        CheckedFuture<Optional<FlowCapableNode>, ReadFailedException> operationalFuture = fillFromDatastore(LogicalDatastoreType.OPERATIONAL, path);
+        final CheckedFuture<Optional<FlowCapableNode>, ReadFailedException> operationalFuture =
+                fillFromDatastore(LogicalDatastoreType.OPERATIONAL, path);
 
         // And at last, chain and return futures created above.
         // Also, cache this future, so call to DeviceFlowRegistry.close() will be able
         // to cancel this future immediately if it will be still in progress
-        final ListenableFuture<List<Optional<FlowCapableNode>>> lastFillFuture = Futures.allAsList(Arrays.asList(configFuture, operationalFuture));
+        final ListenableFuture<List<Optional<FlowCapableNode>>> lastFillFuture =
+                Futures.allAsList(Arrays.asList(configFuture, operationalFuture));
         lastFillFutures.add(lastFillFuture);
         return lastFillFuture;
     }
 
-    private CheckedFuture<Optional<FlowCapableNode>, ReadFailedException> fillFromDatastore(final LogicalDatastoreType logicalDatastoreType, final InstanceIdentifier<FlowCapableNode> path) {
+    private CheckedFuture<Optional<FlowCapableNode>, ReadFailedException>
+            fillFromDatastore(final LogicalDatastoreType logicalDatastoreType,
+                              final InstanceIdentifier<FlowCapableNode> path) {
         // Create new read-only transaction
         final ReadOnlyTransaction transaction = dataBroker.newReadOnlyTransaction();
 
@@ -116,7 +127,7 @@ public class DeviceFlowRegistryImpl implements DeviceFlowRegistry {
 
         Futures.addCallback(future, new FutureCallback<Optional<FlowCapableNode>>() {
             @Override
-            public void onSuccess(Optional<FlowCapableNode> result) {
+            public void onSuccess(@Nonnull Optional<FlowCapableNode> result) {
                 result.asSet().stream()
                         .filter(Objects::nonNull)
                         .filter(flowCapableNode -> Objects.nonNull(flowCapableNode.getTable()))
@@ -133,94 +144,107 @@ public class DeviceFlowRegistryImpl implements DeviceFlowRegistry {
             }
 
             @Override
-            public void onFailure(Throwable t) {
+            public void onFailure(Throwable throwable) {
                 // Even when read operation failed, close the transaction
                 transaction.close();
             }
-        });
+        }, MoreExecutors.directExecutor());
 
         return future;
     }
 
     @Override
-    public FlowDescriptor retrieveIdForFlow(final FlowRegistryKey flowRegistryKey) {
-        LOG.trace("Retrieving flow descriptor for flow hash : {}", flowRegistryKey.hashCode());
-        FlowDescriptor flowDescriptor = flowRegistry.get(flowRegistryKey);
-        // Get FlowDescriptor from flow registry
-        if(flowDescriptor == null){
-            if (LOG.isTraceEnabled()) {
-                LOG.trace("Failed to retrieve flow descriptor for flow hash : {}, trying with custom equals method", flowRegistryKey.hashCode());
-            }
-            for(Map.Entry<FlowRegistryKey, FlowDescriptor> fd : flowRegistry.entrySet()) {
-                if (fd.getKey().equals(flowRegistryKey)) {
-                    flowDescriptor = fd.getValue();
-                    break;
-                }
-            }
+    public FlowDescriptor retrieveDescriptor(@Nonnull final FlowRegistryKey flowRegistryKey) {
+        if (LOG.isTraceEnabled()) {
+            LOG.trace("Retrieving flow descriptor for flow registry : {}", flowRegistryKey.toString());
+        }
+
+        FlowRegistryKey existingFlowRegistryKey = getExistingKey(flowRegistryKey);
+        if (existingFlowRegistryKey != null) {
+            return flowRegistry.get(existingFlowRegistryKey);
         }
-        return flowDescriptor;
+        return null;
     }
 
     @Override
-    public void store(final FlowRegistryKey flowRegistryKey, final FlowDescriptor flowDescriptor) {
+    public void storeDescriptor(@Nonnull final FlowRegistryKey flowRegistryKey,
+                                @Nonnull final FlowDescriptor flowDescriptor) {
         try {
-          LOG.trace("Storing flowDescriptor with table ID : {} and flow ID : {} for flow hash : {}",
-                    flowDescriptor.getTableKey().getId(), flowDescriptor.getFlowId().getValue(), flowRegistryKey.hashCode());
-          flowRegistry.put(flowRegistryKey, flowDescriptor);
+            if (LOG.isTraceEnabled()) {
+                LOG.trace("Storing flowDescriptor with table ID : {} and flow ID : {} for flow hash : {}",
+                        flowDescriptor.getTableKey().getId(),
+                        flowDescriptor.getFlowId().getValue(),
+                        flowRegistryKey.toString());
+            }
+
+            addToFlowRegistry(flowRegistryKey, flowDescriptor);
         } catch (IllegalArgumentException ex) {
-          LOG.error("Flow with flowId {} already exists in table {}", flowDescriptor.getFlowId().getValue(),
-                    flowDescriptor.getTableKey().getId());
-          final FlowId newFlowId = createAlienFlowId(flowDescriptor.getTableKey().getId());
-          final FlowDescriptor newFlowDescriptor = FlowDescriptorFactory.
-            create(flowDescriptor.getTableKey().getId(), newFlowId);
-          flowRegistry.put(flowRegistryKey, newFlowDescriptor);
+            if (LOG.isWarnEnabled()) {
+                LOG.warn("Flow with flow ID {} already exists in table {}, generating alien flow ID",
+                        flowDescriptor.getFlowId().getValue(),
+                        flowDescriptor.getTableKey().getId());
+            }
+
+            // We are trying to store new flow to flow registry, but we already have different flow with same flow ID
+            // stored in registry, so we need to create alien ID for this new flow here.
+            addToFlowRegistry(
+                    flowRegistryKey,
+                    FlowDescriptorFactory.create(
+                            flowDescriptor.getTableKey().getId(),
+                            createAlienFlowId(flowDescriptor.getTableKey().getId())));
         }
     }
 
     @Override
-    public void update(final FlowRegistryKey newFlowRegistryKey, final FlowDescriptor flowDescriptor) {
-        LOG.trace("Updating the entry with hash: {}", newFlowRegistryKey.hashCode());
-        flowRegistry.forcePut(newFlowRegistryKey, flowDescriptor);
+    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());
+            }
+
+            // 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())));
+        }
     }
 
     @Override
-    public FlowId storeIfNecessary(final FlowRegistryKey flowRegistryKey) {
-        LOG.trace("Trying to retrieve flow ID for flow hash : {}", flowRegistryKey.hashCode());
-
-        // First, try to get FlowDescriptor from flow registry
-        FlowDescriptor flowDescriptor = retrieveIdForFlow(flowRegistryKey);
-
-        // We was not able to retrieve FlowDescriptor, so we will at least try to generate it
-        if (flowDescriptor == null) {
-            LOG.trace("Flow descriptor for flow hash : {} not found, generating alien flow ID", flowRegistryKey.hashCode());
-            final short tableId = flowRegistryKey.getTableId();
-            final FlowId alienFlowId = createAlienFlowId(tableId);
-            flowDescriptor = FlowDescriptorFactory.create(tableId, alienFlowId);
-
-            // Finally we got flowDescriptor, so now we will store it to registry,
-            // so next time we won't need to generate it again
-            store(flowRegistryKey, flowDescriptor);
+    public void addMark(final FlowRegistryKey flowRegistryKey) {
+        if (LOG.isTraceEnabled()) {
+            LOG.trace("Removing flow descriptor for flow hash : {}", flowRegistryKey.toString());
         }
 
-        return flowDescriptor.getFlowId();
+        removeFromFlowRegistry(flowRegistryKey);
     }
 
     @Override
-    public void removeDescriptor(final FlowRegistryKey flowRegistryKey) {
-        LOG.trace("Removing flow descriptor for flow hash : {}", flowRegistryKey.hashCode());
-        flowRegistry.remove(flowRegistryKey);
+    public void processMarks() {
+        // Do nothing
+    }
+
+    @Override
+    public void forEach(final Consumer<FlowRegistryKey> consumer) {
+        synchronized (flowRegistry) {
+            flowRegistry.keySet().forEach(consumer);
+        }
     }
 
     @Override
-    public Map<FlowRegistryKey, FlowDescriptor> getAllFlowDescriptors() {
-        return Collections.unmodifiableMap(flowRegistry);
+    public int size() {
+        return flowRegistry.size();
     }
 
     @Override
     public void close() {
         final Iterator<ListenableFuture<List<Optional<FlowCapableNode>>>> iterator = lastFillFutures.iterator();
 
-        while(iterator.hasNext()) {
+        // We need to force interrupt and clear all running futures that are trying to read flow IDs from data store
+        while (iterator.hasNext()) {
             final ListenableFuture<List<Optional<FlowCapableNode>>> next = iterator.next();
             boolean success = next.cancel(true);
             LOG.trace("Cancelling filling flow registry with flows job {} with result: {}", next, success);
@@ -233,6 +257,52 @@ public class DeviceFlowRegistryImpl implements DeviceFlowRegistry {
     @VisibleForTesting
     static FlowId createAlienFlowId(final short 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);
     }
-}
\ No newline at end of file
+
+    //Hashcode generation of the extension augmentation can differ for the same object received from the datastore and
+    // the one received after deserialization of switch message. OpenFlowplugin extensions are list, and the order in
+    // which it can receive the extensions back from switch can differ and that lead to a different hashcode. In that
+    // scenario, hashcode won't match and flowRegistry return the  related key. To overcome this issue, these methods
+    // make sure that key is stored only if it doesn't equals to any existing key.
+    private void addToFlowRegistry(final FlowRegistryKey flowRegistryKey, final FlowDescriptor flowDescriptor) {
+        FlowRegistryKey existingFlowRegistryKey = getExistingKey(flowRegistryKey);
+        if (existingFlowRegistryKey == null) {
+            flowRegistry.put(flowRegistryKey, flowDescriptor);
+        } else {
+            flowRegistry.put(existingFlowRegistryKey, flowDescriptor);
+        }
+    }
+
+    private void removeFromFlowRegistry(final FlowRegistryKey flowRegistryKey) {
+        FlowRegistryKey existingFlowRegistryKey = getExistingKey(flowRegistryKey);
+        if (existingFlowRegistryKey != null) {
+            flowRegistry.remove(existingFlowRegistryKey);
+        } else {
+            flowRegistry.remove(flowRegistryKey);
+        }
+    }
+
+    private FlowRegistryKey getExistingKey(final FlowRegistryKey flowRegistryKey) {
+        if (flowRegistryKey.getMatch().augmentation(GeneralAugMatchNodesNodeTableFlow.class) == null) {
+            if (flowRegistry.containsKey(flowRegistryKey)) {
+                return flowRegistryKey;
+            }
+        } else {
+            synchronized (flowRegistry) {
+                for (Map.Entry<FlowRegistryKey, FlowDescriptor> keyValueSet : flowRegistry.entrySet()) {
+                    if (keyValueSet.getKey().equals(flowRegistryKey)) {
+                        return keyValueSet.getKey();
+                    }
+                }
+            }
+        }
+        return null;
+    }
+
+    @VisibleForTesting
+    Map<FlowRegistryKey, FlowDescriptor> getAllFlowDescriptors() {
+        return flowRegistry;
+    }
+}