e744812c58df15337231b619959d620c91b01307
[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.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;
23 import java.util.Map;
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;
44
45 /**
46  * Created by Martin Bobak <mbobak@cisco.com> on 8.4.2015.
47  */
48 public class DeviceFlowRegistryImpl implements DeviceFlowRegistry {
49     private static final Logger LOG = LoggerFactory.getLogger(DeviceFlowRegistryImpl.class);
50
51     private final ConcurrentMap<FlowRegistryKey, FlowDescriptor> flowRegistry = new TrieMap<>();
52     @GuardedBy("marks")
53     private final Collection<FlowRegistryKey> marks = new HashSet<>();
54     private final DataBroker dataBroker;
55     private final List<ListenableFuture<List<Optional<FlowCapableNode>>>> lastFillFutures = new ArrayList<>();
56
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);
61
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);
67         }
68     };
69
70
71     public DeviceFlowRegistryImpl(final DataBroker dataBroker) {
72         this.dataBroker = dataBroker;
73     }
74
75     @Override
76     public ListenableFuture<List<Optional<FlowCapableNode>>> fill(final KeyedInstanceIdentifier<Node, NodeKey> instanceIdentifier) {
77         LOG.debug("Filling flow registry with flows for node: {}", instanceIdentifier);
78
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);
82
83         // First, try to fill registry with flows from DS/Configuration
84         CheckedFuture<Optional<FlowCapableNode>, ReadFailedException> configFuture = fillFromDatastore(LogicalDatastoreType.CONFIGURATION, path);
85
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);
90
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;
97     }
98
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();
102
103         // Bail out early if transaction is null
104         if (transaction == null) {
105             return Futures.immediateFailedCheckedFuture(
106                     new ReadFailedException("Read transaction is null"));
107         }
108
109         // Prepare read operation from datastore for path
110         final CheckedFuture<Optional<FlowCapableNode>, ReadFailedException> future =
111                 transaction.read(logicalDatastoreType, path);
112
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"));
117         }
118
119         Futures.addCallback(future, new FutureCallback<Optional<FlowCapableNode>>() {
120             @Override
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);
126
127                 // After we are done with reading from datastore, close the transaction
128                 transaction.close();
129             }
130
131             @Override
132             public void onFailure(Throwable t) {
133                 // Even when read operation failed, close the transaction
134                 transaction.close();
135             }
136         });
137
138         return future;
139     }
140
141     @Override
142     public FlowDescriptor retrieveIdForFlow(final FlowRegistryKey flowRegistryKey) {
143         LOG.trace("Retrieving flowDescriptor for flow hash: {}", flowRegistryKey.hashCode());
144
145         // Get FlowDescriptor from flow registry
146         return flowRegistry.get(flowRegistryKey);
147     }
148
149     @Override
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);
153     }
154
155     @Override
156     public FlowId storeIfNecessary(final FlowRegistryKey flowRegistryKey) {
157         LOG.trace("Trying to retrieve flowDescriptor for flow hash: {}", flowRegistryKey.hashCode());
158
159         // First, try to get FlowDescriptor from flow registry
160         FlowDescriptor flowDescriptor = flowRegistry.get(flowRegistryKey);
161
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);
167
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);
171         }
172
173         return flowDescriptor.getFlowId();
174     }
175
176     @Override
177     public void markToBeremoved(final FlowRegistryKey flowRegistryKey) {
178         synchronized (marks) {
179             marks.add(flowRegistryKey);
180         }
181
182         LOG.trace("Flow hash {} was marked for removal.", flowRegistryKey.hashCode());
183     }
184
185     @Override
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);
191             }
192
193             marks.clear();
194         }
195     }
196
197     @Override
198     public Map<FlowRegistryKey, FlowDescriptor> getAllFlowDescriptors() {
199         return Collections.unmodifiableMap(flowRegistry);
200     }
201
202     @Override
203     public void close() {
204         final Iterator<ListenableFuture<List<Optional<FlowCapableNode>>>> iterator = lastFillFutures.iterator();
205
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);
210             iterator.remove();
211         }
212
213         flowRegistry.clear();
214         marks.clear();
215     }
216 }