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 List<ListenableFuture<List<Optional<FlowCapableNode>>>> lastFillFutures = new ArrayList<>();
57 // Specifies what to do with flow read from datastore
58 private final Consumer<Flow> flowConsumer = flow -> {
59 // Create flow registry key from flow
60 final FlowRegistryKey key = FlowRegistryKeyFactory.create(flow);
62 // Now, we will update the registry, but we will also try to prevent duplicate entries
63 if (!flowRegistry.containsKey(key)) {
64 LOG.trace("Found flow with table ID : {} and flow ID : {}", flow.getTableId(), flow.getId().getValue());
65 final FlowDescriptor descriptor = FlowDescriptorFactory.create(flow.getTableId(), flow.getId());
66 store(key, descriptor);
71 public DeviceFlowRegistryImpl(final DataBroker dataBroker) {
72 this.dataBroker = dataBroker;
76 public ListenableFuture<List<Optional<FlowCapableNode>>> fill(final KeyedInstanceIdentifier<Node, NodeKey> instanceIdentifier) {
77 LOG.debug("Filling flow registry with flows for node: {}", instanceIdentifier);
79 // Prepare path for read transaction
80 // TODO: Read only Tables, and not entire FlowCapableNode (fix Yang model)
81 final InstanceIdentifier<FlowCapableNode> path = instanceIdentifier.augmentation(FlowCapableNode.class);
83 // First, try to fill registry with flows from DS/Configuration
84 CheckedFuture<Optional<FlowCapableNode>, ReadFailedException> configFuture = fillFromDatastore(LogicalDatastoreType.CONFIGURATION, path);
86 // Now, try to fill registry with flows from DS/Operational
87 // in case of cluster fail over, when clients are not using DS/Configuration
88 // for adding flows, but only RPCs
89 CheckedFuture<Optional<FlowCapableNode>, ReadFailedException> operationalFuture = fillFromDatastore(LogicalDatastoreType.OPERATIONAL, path);
91 // And at last, chain and return futures created above.
92 // Also, cache this future, so call to DeviceFlowRegistry.close() will be able
93 // to cancel this future immediately if it will be still in progress
94 final ListenableFuture<List<Optional<FlowCapableNode>>> lastFillFuture = Futures.allAsList(Arrays.asList(configFuture, operationalFuture));
95 lastFillFutures.add(lastFillFuture);
96 return lastFillFuture;
99 private CheckedFuture<Optional<FlowCapableNode>, ReadFailedException> fillFromDatastore(final LogicalDatastoreType logicalDatastoreType, final InstanceIdentifier<FlowCapableNode> path) {
100 // Create new read-only transaction
101 final ReadOnlyTransaction transaction = dataBroker.newReadOnlyTransaction();
103 // Bail out early if transaction is null
104 if (transaction == null) {
105 return Futures.immediateFailedCheckedFuture(
106 new ReadFailedException("Read transaction is null"));
109 // Prepare read operation from datastore for path
110 final CheckedFuture<Optional<FlowCapableNode>, ReadFailedException> future =
111 transaction.read(logicalDatastoreType, path);
113 // Bail out early if future is null
114 if (future == null) {
115 return Futures.immediateFailedCheckedFuture(
116 new ReadFailedException("Future from read transaction is null"));
119 Futures.addCallback(future, new FutureCallback<Optional<FlowCapableNode>>() {
121 public void onSuccess(Optional<FlowCapableNode> result) {
122 result.asSet().stream()
123 .flatMap(flowCapableNode -> flowCapableNode.getTable().stream())
124 .flatMap(table -> table.getFlow().stream())
125 .forEach(flowConsumer);
127 // After we are done with reading from datastore, close the transaction
132 public void onFailure(Throwable t) {
133 // Even when read operation failed, close the transaction
142 public FlowDescriptor retrieveIdForFlow(final FlowRegistryKey flowRegistryKey) {
143 LOG.trace("Retrieving flowDescriptor for flow hash: {}", flowRegistryKey.hashCode());
145 // Get FlowDescriptor from flow registry
146 return flowRegistry.get(flowRegistryKey);
150 public void store(final FlowRegistryKey flowRegistryKey, final FlowDescriptor flowDescriptor) {
151 LOG.trace("Storing flowDescriptor with table ID : {} and flow ID : {} for flow hash : {}", flowDescriptor.getTableKey().getId(), flowDescriptor.getFlowId().getValue(), flowRegistryKey.hashCode());
152 flowRegistry.put(flowRegistryKey, flowDescriptor);
156 public FlowId storeIfNecessary(final FlowRegistryKey flowRegistryKey) {
157 LOG.trace("Trying to retrieve flowDescriptor for flow hash: {}", flowRegistryKey.hashCode());
159 // First, try to get FlowDescriptor from flow registry
160 FlowDescriptor flowDescriptor = flowRegistry.get(flowRegistryKey);
162 // We was not able to retrieve FlowDescriptor, so we will at least try to generate it
163 if (flowDescriptor == null) {
164 final short tableId = flowRegistryKey.getTableId();
165 final FlowId alienFlowId = FlowUtil.createAlienFlowId(tableId);
166 flowDescriptor = FlowDescriptorFactory.create(tableId, alienFlowId);
168 // Finally we got flowDescriptor, so now we will store it to registry,
169 // so next time we won't need to generate it again
170 store(flowRegistryKey, flowDescriptor);
173 return flowDescriptor.getFlowId();
177 public void markToBeremoved(final FlowRegistryKey flowRegistryKey) {
178 synchronized (marks) {
179 marks.add(flowRegistryKey);
182 LOG.trace("Flow hash {} was marked for removal.", flowRegistryKey.hashCode());
186 public void removeMarked() {
187 synchronized (marks) {
188 for (FlowRegistryKey flowRegistryKey : marks) {
189 LOG.trace("Removing flowDescriptor for flow hash : {}", flowRegistryKey.hashCode());
190 flowRegistry.remove(flowRegistryKey);
198 public Map<FlowRegistryKey, FlowDescriptor> getAllFlowDescriptors() {
199 return Collections.unmodifiableMap(flowRegistry);
203 public void close() {
204 final Iterator<ListenableFuture<List<Optional<FlowCapableNode>>>> iterator = lastFillFutures.iterator();
206 while(iterator.hasNext()) {
207 final ListenableFuture<List<Optional<FlowCapableNode>>> next = iterator.next();
208 boolean success = next.cancel(true);
209 LOG.trace("Cancelling filling flow registry with flows job {} with result: {}", next, success);
213 flowRegistry.clear();