Revert "Fix statistics race condition on big flows"
[openflowplugin.git] / openflowplugin-impl / src / main / java / org / opendaylight / openflowplugin / impl / registry / flow / DeviceFlowRegistryImpl.java
1 /*
2  * Copyright (c) 2015 Cisco Systems, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.openflowplugin.impl.registry.flow;
9
10 import com.google.common.annotations.VisibleForTesting;
11 import com.google.common.base.Optional;
12 import com.google.common.collect.BiMap;
13 import com.google.common.collect.HashBiMap;
14 import com.google.common.collect.Maps;
15 import com.google.common.util.concurrent.CheckedFuture;
16 import com.google.common.util.concurrent.FutureCallback;
17 import com.google.common.util.concurrent.Futures;
18 import com.google.common.util.concurrent.ListenableFuture;
19 import java.util.ArrayList;
20 import java.util.Arrays;
21 import java.util.Collections;
22 import java.util.Iterator;
23 import java.util.List;
24 import java.util.Map;
25 import java.util.Objects;
26 import java.util.concurrent.atomic.AtomicInteger;
27 import java.util.function.Consumer;
28 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
29 import org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction;
30 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
31 import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
32 import org.opendaylight.openflowplugin.api.openflow.registry.flow.DeviceFlowRegistry;
33 import org.opendaylight.openflowplugin.api.openflow.registry.flow.FlowDescriptor;
34 import org.opendaylight.openflowplugin.api.openflow.registry.flow.FlowRegistryKey;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowId;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.Flow;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
40 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
41 import org.opendaylight.yangtools.yang.binding.KeyedInstanceIdentifier;
42 import org.slf4j.Logger;
43 import org.slf4j.LoggerFactory;
44
45 public class DeviceFlowRegistryImpl implements DeviceFlowRegistry {
46     private static final Logger LOG = LoggerFactory.getLogger(DeviceFlowRegistryImpl.class);
47     private static final String ALIEN_SYSTEM_FLOW_ID = "#UF$TABLE*";
48     private static final AtomicInteger UNACCOUNTED_FLOWS_COUNTER = new AtomicInteger(0);
49
50     private final BiMap<FlowRegistryKey, FlowDescriptor> flowRegistry = Maps.synchronizedBiMap(HashBiMap.create());
51     private final DataBroker dataBroker;
52     private final KeyedInstanceIdentifier<Node, NodeKey> instanceIdentifier;
53     private final List<ListenableFuture<List<Optional<FlowCapableNode>>>> lastFillFutures = new ArrayList<>();
54     private final Consumer<Flow> flowConsumer;
55
56     public DeviceFlowRegistryImpl(final short version, final DataBroker dataBroker, final KeyedInstanceIdentifier<Node, NodeKey> instanceIdentifier) {
57         this.dataBroker = dataBroker;
58         this.instanceIdentifier = instanceIdentifier;
59
60         // Specifies what to do with flow read from datastore
61         flowConsumer = flow -> {
62             // Create flow registry key from flow
63             final FlowRegistryKey key = FlowRegistryKeyFactory.create(version, flow);
64
65             // Now, we will update the registry, but we will also try to prevent duplicate entries
66             if (!flowRegistry.containsKey(key)) {
67                 LOG.trace("Found flow with table ID : {} and flow ID : {}", flow.getTableId(), flow.getId().getValue());
68                 final FlowDescriptor descriptor = FlowDescriptorFactory.create(flow.getTableId(), flow.getId());
69                 store(key, descriptor);
70             }
71         };
72     }
73
74     @Override
75     public ListenableFuture<List<Optional<FlowCapableNode>>> fill() {
76         LOG.debug("Filling flow registry with flows for node: {}", instanceIdentifier.getKey().getId().getValue());
77
78         // Prepare path for read transaction
79         // TODO: Read only Tables, and not entire FlowCapableNode (fix Yang model)
80         final InstanceIdentifier<FlowCapableNode> path = instanceIdentifier.augmentation(FlowCapableNode.class);
81
82         // First, try to fill registry with flows from DS/Configuration
83         CheckedFuture<Optional<FlowCapableNode>, ReadFailedException> configFuture = fillFromDatastore(LogicalDatastoreType.CONFIGURATION, path);
84
85         // Now, try to fill registry with flows from DS/Operational
86         // in case of cluster fail over, when clients are not using DS/Configuration
87         // for adding flows, but only RPCs
88         CheckedFuture<Optional<FlowCapableNode>, ReadFailedException> operationalFuture = fillFromDatastore(LogicalDatastoreType.OPERATIONAL, path);
89
90         // And at last, chain and return futures created above.
91         // Also, cache this future, so call to DeviceFlowRegistry.close() will be able
92         // to cancel this future immediately if it will be still in progress
93         final ListenableFuture<List<Optional<FlowCapableNode>>> lastFillFuture = Futures.allAsList(Arrays.asList(configFuture, operationalFuture));
94         lastFillFutures.add(lastFillFuture);
95         return lastFillFuture;
96     }
97
98     private CheckedFuture<Optional<FlowCapableNode>, ReadFailedException> fillFromDatastore(final LogicalDatastoreType logicalDatastoreType, final InstanceIdentifier<FlowCapableNode> path) {
99         // Create new read-only transaction
100         final ReadOnlyTransaction transaction = dataBroker.newReadOnlyTransaction();
101
102         // Bail out early if transaction is null
103         if (transaction == null) {
104             return Futures.immediateFailedCheckedFuture(
105                     new ReadFailedException("Read transaction is null"));
106         }
107
108         // Prepare read operation from datastore for path
109         final CheckedFuture<Optional<FlowCapableNode>, ReadFailedException> future =
110                 transaction.read(logicalDatastoreType, path);
111
112         // Bail out early if future is null
113         if (future == null) {
114             return Futures.immediateFailedCheckedFuture(
115                     new ReadFailedException("Future from read transaction is null"));
116         }
117
118         Futures.addCallback(future, new FutureCallback<Optional<FlowCapableNode>>() {
119             @Override
120             public void onSuccess(Optional<FlowCapableNode> result) {
121                 result.asSet().stream()
122                         .filter(Objects::nonNull)
123                         .filter(flowCapableNode -> Objects.nonNull(flowCapableNode.getTable()))
124                         .flatMap(flowCapableNode -> flowCapableNode.getTable().stream())
125                         .filter(Objects::nonNull)
126                         .filter(table -> Objects.nonNull(table.getFlow()))
127                         .flatMap(table -> table.getFlow().stream())
128                         .filter(Objects::nonNull)
129                         .filter(flow -> Objects.nonNull(flow.getId()))
130                         .forEach(flowConsumer);
131
132                 // After we are done with reading from datastore, close the transaction
133                 transaction.close();
134             }
135
136             @Override
137             public void onFailure(Throwable t) {
138                 // Even when read operation failed, close the transaction
139                 transaction.close();
140             }
141         });
142
143         return future;
144     }
145
146     @Override
147     public FlowDescriptor retrieveIdForFlow(final FlowRegistryKey flowRegistryKey) {
148         LOG.trace("Retrieving flow descriptor for flow hash : {}", flowRegistryKey.hashCode());
149         FlowDescriptor flowDescriptor = flowRegistry.get(flowRegistryKey);
150         // Get FlowDescriptor from flow registry
151         if(flowDescriptor == null){
152             if (LOG.isTraceEnabled()) {
153                 LOG.trace("Failed to retrieve flow descriptor for flow hash : {}, trying with custom equals method", flowRegistryKey.hashCode());
154             }
155             for(Map.Entry<FlowRegistryKey, FlowDescriptor> fd : flowRegistry.entrySet()) {
156                 if (flowRegistryKey.equals(fd.getKey())) {
157                     flowDescriptor = fd.getValue();
158                     break;
159                 }
160             }
161         }
162         return flowDescriptor;
163     }
164
165     @Override
166     public void store(final FlowRegistryKey flowRegistryKey, final FlowDescriptor flowDescriptor) {
167         try {
168           LOG.trace("Storing flowDescriptor with table ID : {} and flow ID : {} for flow hash : {}",
169                     flowDescriptor.getTableKey().getId(), flowDescriptor.getFlowId().getValue(), flowRegistryKey.hashCode());
170           flowRegistry.put(flowRegistryKey, flowDescriptor);
171         } catch (IllegalArgumentException ex) {
172           LOG.warn("Flow with flowId {} already exists in table {}", flowDescriptor.getFlowId().getValue(),
173                     flowDescriptor.getTableKey().getId());
174           final FlowId newFlowId = createAlienFlowId(flowDescriptor.getTableKey().getId());
175           final FlowDescriptor newFlowDescriptor = FlowDescriptorFactory.
176             create(flowDescriptor.getTableKey().getId(), newFlowId);
177           flowRegistry.put(flowRegistryKey, newFlowDescriptor);
178         }
179     }
180
181     @Override
182     public void update(final FlowRegistryKey newFlowRegistryKey, final FlowDescriptor flowDescriptor) {
183         LOG.trace("Updating the entry with hash: {}", newFlowRegistryKey.hashCode());
184         flowRegistry.forcePut(newFlowRegistryKey, flowDescriptor);
185     }
186
187     @Override
188     public FlowId storeIfNecessary(final FlowRegistryKey flowRegistryKey) {
189         LOG.trace("Trying to retrieve flow ID for flow hash : {}", flowRegistryKey.hashCode());
190
191         // First, try to get FlowDescriptor from flow registry
192         FlowDescriptor flowDescriptor = retrieveIdForFlow(flowRegistryKey);
193
194         // We was not able to retrieve FlowDescriptor, so we will at least try to generate it
195         if (flowDescriptor == null) {
196             LOG.trace("Flow descriptor for flow hash : {} not found, generating alien flow ID", flowRegistryKey.hashCode());
197             final short tableId = flowRegistryKey.getTableId();
198             final FlowId alienFlowId = createAlienFlowId(tableId);
199             flowDescriptor = FlowDescriptorFactory.create(tableId, alienFlowId);
200
201             // Finally we got flowDescriptor, so now we will store it to registry,
202             // so next time we won't need to generate it again
203             store(flowRegistryKey, flowDescriptor);
204         }
205
206         return flowDescriptor.getFlowId();
207     }
208
209     @Override
210     public void removeDescriptor(final FlowRegistryKey flowRegistryKey) {
211         LOG.trace("Removing flow descriptor for flow hash : {}", flowRegistryKey.hashCode());
212         flowRegistry.remove(flowRegistryKey);
213     }
214
215     @Override
216     public Map<FlowRegistryKey, FlowDescriptor> getAllFlowDescriptors() {
217         return Collections.unmodifiableMap(flowRegistry);
218     }
219
220     @Override
221     public void close() {
222         final Iterator<ListenableFuture<List<Optional<FlowCapableNode>>>> iterator = lastFillFutures.iterator();
223
224         while(iterator.hasNext()) {
225             final ListenableFuture<List<Optional<FlowCapableNode>>> next = iterator.next();
226             boolean success = next.cancel(true);
227             LOG.trace("Cancelling filling flow registry with flows job {} with result: {}", next, success);
228             iterator.remove();
229         }
230
231         flowRegistry.clear();
232     }
233
234     @VisibleForTesting
235     static FlowId createAlienFlowId(final short tableId) {
236         final String alienId = ALIEN_SYSTEM_FLOW_ID + tableId + '-' + UNACCOUNTED_FLOWS_COUNTER.incrementAndGet();
237         return new FlowId(alienId);
238     }
239 }