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.base.Optional;
11 import com.google.common.util.concurrent.CheckedFuture;
12 import com.google.common.util.concurrent.FutureCallback;
13 import com.google.common.util.concurrent.Futures;
14 import com.google.common.util.concurrent.ListenableFuture;
15 import com.romix.scala.collection.concurrent.TrieMap;
16 import java.util.ArrayList;
17 import java.util.Arrays;
18 import java.util.Collection;
19 import java.util.Collections;
20 import java.util.HashSet;
21 import java.util.Iterator;
22 import java.util.List;
24 import java.util.concurrent.ConcurrentMap;
25 import java.util.function.Consumer;
26 import javax.annotation.concurrent.GuardedBy;
27 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
28 import org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction;
29 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
30 import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
31 import org.opendaylight.openflowplugin.api.openflow.registry.flow.DeviceFlowRegistry;
32 import org.opendaylight.openflowplugin.api.openflow.registry.flow.FlowDescriptor;
33 import org.opendaylight.openflowplugin.api.openflow.registry.flow.FlowRegistryKey;
34 import org.opendaylight.openflowplugin.impl.util.FlowUtil;
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;
46 * Created by Martin Bobak <mbobak@cisco.com> on 8.4.2015.
48 public class DeviceFlowRegistryImpl implements DeviceFlowRegistry {
49 private static final Logger LOG = LoggerFactory.getLogger(DeviceFlowRegistryImpl.class);
51 private final ConcurrentMap<FlowRegistryKey, FlowDescriptor> flowRegistry = new TrieMap<>();
53 private final Collection<FlowRegistryKey> marks = new HashSet<>();
54 private final DataBroker dataBroker;
55 private final KeyedInstanceIdentifier<Node, NodeKey> instanceIdentifier;
56 private final List<ListenableFuture<List<Optional<FlowCapableNode>>>> lastFillFutures = new ArrayList<>();
58 // Specifies what to do with flow read from datastore
59 private final Consumer<Flow> flowConsumer = flow -> {
60 // Create flow registry key from flow
61 final FlowRegistryKey key = FlowRegistryKeyFactory.create(flow);
63 // Now, we will update the registry, but we will also try to prevent duplicate entries
64 if (!flowRegistry.containsKey(key)) {
65 LOG.trace("Found flow with table ID : {} and flow ID : {}", flow.getTableId(), flow.getId().getValue());
66 final FlowDescriptor descriptor = FlowDescriptorFactory.create(flow.getTableId(), flow.getId());
67 store(key, descriptor);
72 public DeviceFlowRegistryImpl(final DataBroker dataBroker, final KeyedInstanceIdentifier<Node, NodeKey> instanceIdentifier) {
73 this.dataBroker = dataBroker;
74 this.instanceIdentifier = instanceIdentifier;
78 public ListenableFuture<List<Optional<FlowCapableNode>>> fill() {
79 LOG.debug("Filling flow registry with flows for node: {}", instanceIdentifier);
81 // Prepare path for read transaction
82 // TODO: Read only Tables, and not entire FlowCapableNode (fix Yang model)
83 final InstanceIdentifier<FlowCapableNode> path = instanceIdentifier.augmentation(FlowCapableNode.class);
85 // First, try to fill registry with flows from DS/Configuration
86 CheckedFuture<Optional<FlowCapableNode>, ReadFailedException> configFuture = fillFromDatastore(LogicalDatastoreType.CONFIGURATION, path);
88 // Now, try to fill registry with flows from DS/Operational
89 // in case of cluster fail over, when clients are not using DS/Configuration
90 // for adding flows, but only RPCs
91 CheckedFuture<Optional<FlowCapableNode>, ReadFailedException> operationalFuture = fillFromDatastore(LogicalDatastoreType.OPERATIONAL, path);
93 // And at last, chain and return futures created above.
94 // Also, cache this future, so call to DeviceFlowRegistry.close() will be able
95 // to cancel this future immediately if it will be still in progress
96 final ListenableFuture<List<Optional<FlowCapableNode>>> lastFillFuture = Futures.allAsList(Arrays.asList(configFuture, operationalFuture));
97 lastFillFutures.add(lastFillFuture);
98 return lastFillFuture;
101 private CheckedFuture<Optional<FlowCapableNode>, ReadFailedException> fillFromDatastore(final LogicalDatastoreType logicalDatastoreType, final InstanceIdentifier<FlowCapableNode> path) {
102 // Create new read-only transaction
103 final ReadOnlyTransaction transaction = dataBroker.newReadOnlyTransaction();
105 // Bail out early if transaction is null
106 if (transaction == null) {
107 return Futures.immediateFailedCheckedFuture(
108 new ReadFailedException("Read transaction is null"));
111 // Prepare read operation from datastore for path
112 final CheckedFuture<Optional<FlowCapableNode>, ReadFailedException> future =
113 transaction.read(logicalDatastoreType, path);
115 // Bail out early if future is null
116 if (future == null) {
117 return Futures.immediateFailedCheckedFuture(
118 new ReadFailedException("Future from read transaction is null"));
121 Futures.addCallback(future, new FutureCallback<Optional<FlowCapableNode>>() {
123 public void onSuccess(Optional<FlowCapableNode> result) {
124 result.asSet().stream()
125 .flatMap(flowCapableNode -> flowCapableNode.getTable().stream())
126 .flatMap(table -> table.getFlow().stream())
127 .forEach(flowConsumer);
129 // After we are done with reading from datastore, close the transaction
134 public void onFailure(Throwable t) {
135 // Even when read operation failed, close the transaction
144 public FlowDescriptor retrieveIdForFlow(final FlowRegistryKey flowRegistryKey) {
145 LOG.trace("Retrieving flowDescriptor for flow hash: {}", flowRegistryKey.hashCode());
147 // Get FlowDescriptor from flow registry
148 return flowRegistry.get(flowRegistryKey);
152 public void store(final FlowRegistryKey flowRegistryKey, final FlowDescriptor flowDescriptor) {
153 LOG.trace("Storing flowDescriptor with table ID : {} and flow ID : {} for flow hash : {}", flowDescriptor.getTableKey().getId(), flowDescriptor.getFlowId().getValue(), flowRegistryKey.hashCode());
154 flowRegistry.put(flowRegistryKey, flowDescriptor);
158 public FlowId storeIfNecessary(final FlowRegistryKey flowRegistryKey) {
159 LOG.trace("Trying to retrieve flowDescriptor for flow hash: {}", flowRegistryKey.hashCode());
161 // First, try to get FlowDescriptor from flow registry
162 FlowDescriptor flowDescriptor = flowRegistry.get(flowRegistryKey);
164 // We was not able to retrieve FlowDescriptor, so we will at least try to generate it
165 if (flowDescriptor == null) {
166 final short tableId = flowRegistryKey.getTableId();
167 final FlowId alienFlowId = FlowUtil.createAlienFlowId(tableId);
168 flowDescriptor = FlowDescriptorFactory.create(tableId, alienFlowId);
170 // Finally we got flowDescriptor, so now we will store it to registry,
171 // so next time we won't need to generate it again
172 store(flowRegistryKey, flowDescriptor);
175 return flowDescriptor.getFlowId();
179 public void markToBeremoved(final FlowRegistryKey flowRegistryKey) {
180 synchronized (marks) {
181 marks.add(flowRegistryKey);
184 LOG.trace("Flow hash {} was marked for removal.", flowRegistryKey.hashCode());
188 public void removeMarked() {
189 synchronized (marks) {
190 for (FlowRegistryKey flowRegistryKey : marks) {
191 LOG.trace("Removing flowDescriptor for flow hash : {}", flowRegistryKey.hashCode());
192 flowRegistry.remove(flowRegistryKey);
200 public Map<FlowRegistryKey, FlowDescriptor> getAllFlowDescriptors() {
201 return Collections.unmodifiableMap(flowRegistry);
205 public void close() {
206 final Iterator<ListenableFuture<List<Optional<FlowCapableNode>>>> iterator = lastFillFutures.iterator();
208 while(iterator.hasNext()) {
209 final ListenableFuture<List<Optional<FlowCapableNode>>> next = iterator.next();
210 boolean success = next.cancel(true);
211 LOG.trace("Cancelling filling flow registry with flows job {} with result: {}", next, success);
215 flowRegistry.clear();