2 * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.openflowplugin.impl.registry.flow;
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 com.romix.scala.collection.concurrent.TrieMap;
20 import java.util.ArrayList;
21 import java.util.Arrays;
22 import java.util.Collection;
23 import java.util.Collections;
24 import java.util.HashSet;
25 import java.util.Iterator;
26 import java.util.List;
28 import java.util.Objects;
29 import java.util.concurrent.ConcurrentMap;
30 import java.util.concurrent.atomic.AtomicInteger;
31 import java.util.function.Consumer;
32 import javax.annotation.concurrent.GuardedBy;
33 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
34 import org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction;
35 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
36 import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
37 import org.opendaylight.openflowplugin.api.openflow.registry.flow.DeviceFlowRegistry;
38 import org.opendaylight.openflowplugin.api.openflow.registry.flow.FlowDescriptor;
39 import org.opendaylight.openflowplugin.api.openflow.registry.flow.FlowRegistryKey;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowId;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.Flow;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
45 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
46 import org.opendaylight.yangtools.yang.binding.KeyedInstanceIdentifier;
47 import org.slf4j.Logger;
48 import org.slf4j.LoggerFactory;
51 * Created by Martin Bobak <mbobak@cisco.com> on 8.4.2015.
53 public class DeviceFlowRegistryImpl implements DeviceFlowRegistry {
54 private static final Logger LOG = LoggerFactory.getLogger(DeviceFlowRegistryImpl.class);
55 private static final String ALIEN_SYSTEM_FLOW_ID = "#UF$TABLE*";
56 private static final AtomicInteger UNACCOUNTED_FLOWS_COUNTER = new AtomicInteger(0);
59 private final BiMap<FlowRegistryKey, FlowDescriptor> flowRegistry = HashBiMap.create();
61 private final Collection<FlowRegistryKey> marks = new HashSet<>();
62 private final DataBroker dataBroker;
63 private final KeyedInstanceIdentifier<Node, NodeKey> instanceIdentifier;
64 private final List<ListenableFuture<List<Optional<FlowCapableNode>>>> lastFillFutures = new ArrayList<>();
66 // Specifies what to do with flow read from datastore
67 private final Consumer<Flow> flowConsumer = flow -> {
68 // Create flow registry key from flow
69 final FlowRegistryKey key = FlowRegistryKeyFactory.create(flow);
71 // Now, we will update the registry, but we will also try to prevent duplicate entries
72 if (!flowRegistry.containsKey(key)) {
73 LOG.trace("Found flow with table ID : {} and flow ID : {}", flow.getTableId(), flow.getId().getValue());
74 final FlowDescriptor descriptor = FlowDescriptorFactory.create(flow.getTableId(), flow.getId());
75 store(key, descriptor);
80 public DeviceFlowRegistryImpl(final DataBroker dataBroker, final KeyedInstanceIdentifier<Node, NodeKey> instanceIdentifier) {
81 this.dataBroker = dataBroker;
82 this.instanceIdentifier = instanceIdentifier;
86 public ListenableFuture<List<Optional<FlowCapableNode>>> fill() {
87 LOG.debug("Filling flow registry with flows for node: {}", instanceIdentifier.getKey().getId().getValue());
89 // Prepare path for read transaction
90 // TODO: Read only Tables, and not entire FlowCapableNode (fix Yang model)
91 final InstanceIdentifier<FlowCapableNode> path = instanceIdentifier.augmentation(FlowCapableNode.class);
93 // First, try to fill registry with flows from DS/Configuration
94 CheckedFuture<Optional<FlowCapableNode>, ReadFailedException> configFuture = fillFromDatastore(LogicalDatastoreType.CONFIGURATION, path);
96 // Now, try to fill registry with flows from DS/Operational
97 // in case of cluster fail over, when clients are not using DS/Configuration
98 // for adding flows, but only RPCs
99 CheckedFuture<Optional<FlowCapableNode>, ReadFailedException> operationalFuture = fillFromDatastore(LogicalDatastoreType.OPERATIONAL, path);
101 // And at last, chain and return futures created above.
102 // Also, cache this future, so call to DeviceFlowRegistry.close() will be able
103 // to cancel this future immediately if it will be still in progress
104 final ListenableFuture<List<Optional<FlowCapableNode>>> lastFillFuture = Futures.allAsList(Arrays.asList(configFuture, operationalFuture));
105 lastFillFutures.add(lastFillFuture);
106 return lastFillFuture;
109 private CheckedFuture<Optional<FlowCapableNode>, ReadFailedException> fillFromDatastore(final LogicalDatastoreType logicalDatastoreType, final InstanceIdentifier<FlowCapableNode> path) {
110 // Create new read-only transaction
111 final ReadOnlyTransaction transaction = dataBroker.newReadOnlyTransaction();
113 // Bail out early if transaction is null
114 if (transaction == null) {
115 return Futures.immediateFailedCheckedFuture(
116 new ReadFailedException("Read transaction is null"));
119 // Prepare read operation from datastore for path
120 final CheckedFuture<Optional<FlowCapableNode>, ReadFailedException> future =
121 transaction.read(logicalDatastoreType, path);
123 // Bail out early if future is null
124 if (future == null) {
125 return Futures.immediateFailedCheckedFuture(
126 new ReadFailedException("Future from read transaction is null"));
129 Futures.addCallback(future, new FutureCallback<Optional<FlowCapableNode>>() {
131 public void onSuccess(Optional<FlowCapableNode> result) {
132 result.asSet().stream()
133 .filter(Objects::nonNull)
134 .filter(flowCapableNode -> Objects.nonNull(flowCapableNode.getTable()))
135 .flatMap(flowCapableNode -> flowCapableNode.getTable().stream())
136 .filter(Objects::nonNull)
137 .filter(table -> Objects.nonNull(table.getFlow()))
138 .flatMap(table -> table.getFlow().stream())
139 .filter(Objects::nonNull)
140 .filter(flow -> Objects.nonNull(flow.getId()))
141 .forEach(flowConsumer);
143 // After we are done with reading from datastore, close the transaction
148 public void onFailure(Throwable t) {
149 // Even when read operation failed, close the transaction
158 public FlowDescriptor retrieveIdForFlow(final FlowRegistryKey flowRegistryKey) {
159 LOG.trace("Retrieving flow descriptor for flow hash : {}", flowRegistryKey.hashCode());
160 FlowDescriptor flowDescriptor = flowRegistry.get(flowRegistryKey);
161 // Get FlowDescriptor from flow registry
162 if(flowDescriptor == null){
163 if (LOG.isTraceEnabled()) {
164 LOG.trace("Failed to retrieve flow descriptor for flow hash : {}, trying with custom equals method", flowRegistryKey.hashCode());
166 for(Map.Entry<FlowRegistryKey, FlowDescriptor> fd : flowRegistry.entrySet()) {
167 if (fd.getKey().equals(flowRegistryKey)) {
168 flowDescriptor = fd.getValue();
173 return flowDescriptor;
178 public void store(final FlowRegistryKey flowRegistryKey, final FlowDescriptor flowDescriptor) {
179 LOG.trace("Storing flowDescriptor with table ID : {} and flow ID : {} for flow hash : {}",
180 flowDescriptor.getTableKey().getId(), flowDescriptor.getFlowId().getValue(), flowRegistryKey.hashCode());
181 synchronized (flowRegistryKey) {
183 flowRegistry.put(flowRegistryKey, flowDescriptor);
184 } catch (IllegalArgumentException ex) {
185 LOG.error("Flow with flowId {} already exists in table {}", flowDescriptor.getFlowId().getValue(),
186 flowDescriptor.getTableKey().getId());
187 final FlowId newFlowId = createAlienFlowId(flowDescriptor.getTableKey().getId());
188 final FlowDescriptor newFlowDescriptor = FlowDescriptorFactory.
189 create(flowDescriptor.getTableKey().getId(), newFlowId);
190 flowRegistry.put(flowRegistryKey, newFlowDescriptor);
196 public void update(FlowRegistryKey newFlowRegistryKey,FlowDescriptor flowDescriptor){
197 LOG.trace("Updating the entry with hash: {}", newFlowRegistryKey.hashCode());
198 synchronized (newFlowRegistryKey) {
199 flowRegistry.forcePut(newFlowRegistryKey, flowDescriptor);
204 public FlowId storeIfNecessary(final FlowRegistryKey flowRegistryKey) {
205 LOG.trace("Trying to retrieve flow ID for flow hash : {}", flowRegistryKey.hashCode());
207 // First, try to get FlowDescriptor from flow registry
208 FlowDescriptor flowDescriptor = retrieveIdForFlow(flowRegistryKey);
210 // We was not able to retrieve FlowDescriptor, so we will at least try to generate it
211 if (flowDescriptor == null) {
212 LOG.trace("Flow descriptor for flow hash : {} not found, generating alien flow ID", flowRegistryKey.hashCode());
213 final short tableId = flowRegistryKey.getTableId();
214 final FlowId alienFlowId = createAlienFlowId(tableId);
215 flowDescriptor = FlowDescriptorFactory.create(tableId, alienFlowId);
217 // Finally we got flowDescriptor, so now we will store it to registry,
218 // so next time we won't need to generate it again
219 store(flowRegistryKey, flowDescriptor);
222 return flowDescriptor.getFlowId();
226 public void markToBeremoved(final FlowRegistryKey flowRegistryKey) {
227 synchronized (marks) {
228 marks.add(flowRegistryKey);
231 LOG.trace("Flow hash {} was marked for removal.", flowRegistryKey.hashCode());
235 public void removeMarked() {
236 synchronized (marks) {
237 for (FlowRegistryKey flowRegistryKey : marks) {
238 LOG.trace("Removing flow descriptor for flow hash : {}", flowRegistryKey.hashCode());
239 flowRegistry.remove(flowRegistryKey);
247 public Map<FlowRegistryKey, FlowDescriptor> getAllFlowDescriptors() {
248 return Collections.unmodifiableMap(flowRegistry);
252 public void close() {
253 final Iterator<ListenableFuture<List<Optional<FlowCapableNode>>>> iterator = lastFillFutures.iterator();
255 while(iterator.hasNext()) {
256 final ListenableFuture<List<Optional<FlowCapableNode>>> next = iterator.next();
257 boolean success = next.cancel(true);
258 LOG.trace("Cancelling filling flow registry with flows job {} with result: {}", next, success);
262 flowRegistry.clear();
267 static FlowId createAlienFlowId(final short tableId) {
268 final String alienId = ALIEN_SYSTEM_FLOW_ID + tableId + '-' + UNACCOUNTED_FLOWS_COUNTER.incrementAndGet();
269 return new FlowId(alienId);