01f7178e2f3cb656f92dd08ec4d3e0c55ab64a0d
[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
55     // Specifies what to do with flow read from datastore
56     private final Consumer<Flow> flowConsumer = flow -> {
57         // Create flow registry key from flow
58         final FlowRegistryKey key = FlowRegistryKeyFactory.create(flow);
59
60         // Now, we will update the registry, but we will also try to prevent duplicate entries
61         if (!flowRegistry.containsKey(key)) {
62             LOG.trace("Found flow with table ID : {} and flow ID : {}", flow.getTableId(), flow.getId().getValue());
63             final FlowDescriptor descriptor = FlowDescriptorFactory.create(flow.getTableId(), flow.getId());
64             store(key, descriptor);
65         }
66     };
67
68     public DeviceFlowRegistryImpl(final DataBroker dataBroker, final KeyedInstanceIdentifier<Node, NodeKey> instanceIdentifier) {
69         this.dataBroker = dataBroker;
70         this.instanceIdentifier = instanceIdentifier;
71     }
72
73     @Override
74     public ListenableFuture<List<Optional<FlowCapableNode>>> fill() {
75         LOG.debug("Filling flow registry with flows for node: {}", instanceIdentifier.getKey().getId().getValue());
76
77         // Prepare path for read transaction
78         // TODO: Read only Tables, and not entire FlowCapableNode (fix Yang model)
79         final InstanceIdentifier<FlowCapableNode> path = instanceIdentifier.augmentation(FlowCapableNode.class);
80
81         // First, try to fill registry with flows from DS/Configuration
82         CheckedFuture<Optional<FlowCapableNode>, ReadFailedException> configFuture = fillFromDatastore(LogicalDatastoreType.CONFIGURATION, path);
83
84         // Now, try to fill registry with flows from DS/Operational
85         // in case of cluster fail over, when clients are not using DS/Configuration
86         // for adding flows, but only RPCs
87         CheckedFuture<Optional<FlowCapableNode>, ReadFailedException> operationalFuture = fillFromDatastore(LogicalDatastoreType.OPERATIONAL, path);
88
89         // And at last, chain and return futures created above.
90         // Also, cache this future, so call to DeviceFlowRegistry.close() will be able
91         // to cancel this future immediately if it will be still in progress
92         final ListenableFuture<List<Optional<FlowCapableNode>>> lastFillFuture = Futures.allAsList(Arrays.asList(configFuture, operationalFuture));
93         lastFillFutures.add(lastFillFuture);
94         return lastFillFuture;
95     }
96
97     private CheckedFuture<Optional<FlowCapableNode>, ReadFailedException> fillFromDatastore(final LogicalDatastoreType logicalDatastoreType, final InstanceIdentifier<FlowCapableNode> path) {
98         // Create new read-only transaction
99         final ReadOnlyTransaction transaction = dataBroker.newReadOnlyTransaction();
100
101         // Bail out early if transaction is null
102         if (transaction == null) {
103             return Futures.immediateFailedCheckedFuture(
104                     new ReadFailedException("Read transaction is null"));
105         }
106
107         // Prepare read operation from datastore for path
108         final CheckedFuture<Optional<FlowCapableNode>, ReadFailedException> future =
109                 transaction.read(logicalDatastoreType, path);
110
111         // Bail out early if future is null
112         if (future == null) {
113             return Futures.immediateFailedCheckedFuture(
114                     new ReadFailedException("Future from read transaction is null"));
115         }
116
117         Futures.addCallback(future, new FutureCallback<Optional<FlowCapableNode>>() {
118             @Override
119             public void onSuccess(Optional<FlowCapableNode> result) {
120                 result.asSet().stream()
121                         .filter(Objects::nonNull)
122                         .filter(flowCapableNode -> Objects.nonNull(flowCapableNode.getTable()))
123                         .flatMap(flowCapableNode -> flowCapableNode.getTable().stream())
124                         .filter(Objects::nonNull)
125                         .filter(table -> Objects.nonNull(table.getFlow()))
126                         .flatMap(table -> table.getFlow().stream())
127                         .filter(Objects::nonNull)
128                         .filter(flow -> Objects.nonNull(flow.getId()))
129                         .forEach(flowConsumer);
130
131                 // After we are done with reading from datastore, close the transaction
132                 transaction.close();
133             }
134
135             @Override
136             public void onFailure(Throwable t) {
137                 // Even when read operation failed, close the transaction
138                 transaction.close();
139             }
140         });
141
142         return future;
143     }
144
145     @Override
146     public FlowDescriptor retrieveIdForFlow(final FlowRegistryKey flowRegistryKey) {
147         LOG.trace("Retrieving flow descriptor for flow hash : {}", flowRegistryKey.hashCode());
148         FlowDescriptor flowDescriptor = flowRegistry.get(flowRegistryKey);
149         // Get FlowDescriptor from flow registry
150         if(flowDescriptor == null){
151             if (LOG.isTraceEnabled()) {
152                 LOG.trace("Failed to retrieve flow descriptor for flow hash : {}, trying with custom equals method", flowRegistryKey.hashCode());
153             }
154             for(Map.Entry<FlowRegistryKey, FlowDescriptor> fd : flowRegistry.entrySet()) {
155                 if (fd.getKey().equals(flowRegistryKey)) {
156                     flowDescriptor = fd.getValue();
157                     break;
158                 }
159             }
160         }
161         return flowDescriptor;
162     }
163
164     @Override
165     public void store(final FlowRegistryKey flowRegistryKey, final FlowDescriptor flowDescriptor) {
166         try {
167           LOG.trace("Storing flowDescriptor with table ID : {} and flow ID : {} for flow hash : {}",
168                     flowDescriptor.getTableKey().getId(), flowDescriptor.getFlowId().getValue(), flowRegistryKey.hashCode());
169           flowRegistry.put(flowRegistryKey, flowDescriptor);
170         } catch (IllegalArgumentException ex) {
171           LOG.warn("Flow with flowId {} already exists in table {}", flowDescriptor.getFlowId().getValue(),
172                     flowDescriptor.getTableKey().getId());
173           final FlowId newFlowId = createAlienFlowId(flowDescriptor.getTableKey().getId());
174           final FlowDescriptor newFlowDescriptor = FlowDescriptorFactory.
175             create(flowDescriptor.getTableKey().getId(), newFlowId);
176           flowRegistry.put(flowRegistryKey, newFlowDescriptor);
177         }
178     }
179
180     @Override
181     public void update(final FlowRegistryKey newFlowRegistryKey, final FlowDescriptor flowDescriptor) {
182         LOG.trace("Updating the entry with hash: {}", newFlowRegistryKey.hashCode());
183         flowRegistry.forcePut(newFlowRegistryKey, flowDescriptor);
184     }
185
186     @Override
187     public FlowId storeIfNecessary(final FlowRegistryKey flowRegistryKey) {
188         LOG.trace("Trying to retrieve flow ID for flow hash : {}", flowRegistryKey.hashCode());
189
190         // First, try to get FlowDescriptor from flow registry
191         FlowDescriptor flowDescriptor = retrieveIdForFlow(flowRegistryKey);
192
193         // We was not able to retrieve FlowDescriptor, so we will at least try to generate it
194         if (flowDescriptor == null) {
195             LOG.trace("Flow descriptor for flow hash : {} not found, generating alien flow ID", flowRegistryKey.hashCode());
196             final short tableId = flowRegistryKey.getTableId();
197             final FlowId alienFlowId = createAlienFlowId(tableId);
198             flowDescriptor = FlowDescriptorFactory.create(tableId, alienFlowId);
199
200             // Finally we got flowDescriptor, so now we will store it to registry,
201             // so next time we won't need to generate it again
202             store(flowRegistryKey, flowDescriptor);
203         }
204
205         return flowDescriptor.getFlowId();
206     }
207
208     @Override
209     public void removeDescriptor(final FlowRegistryKey flowRegistryKey) {
210         LOG.trace("Removing flow descriptor for flow hash : {}", flowRegistryKey.hashCode());
211         flowRegistry.remove(flowRegistryKey);
212     }
213
214     @Override
215     public Map<FlowRegistryKey, FlowDescriptor> getAllFlowDescriptors() {
216         return Collections.unmodifiableMap(flowRegistry);
217     }
218
219     @Override
220     public void close() {
221         final Iterator<ListenableFuture<List<Optional<FlowCapableNode>>>> iterator = lastFillFutures.iterator();
222
223         while(iterator.hasNext()) {
224             final ListenableFuture<List<Optional<FlowCapableNode>>> next = iterator.next();
225             boolean success = next.cancel(true);
226             LOG.trace("Cancelling filling flow registry with flows job {} with result: {}", next, success);
227             iterator.remove();
228         }
229
230         flowRegistry.clear();
231     }
232
233     @VisibleForTesting
234     static FlowId createAlienFlowId(final short tableId) {
235         final String alienId = ALIEN_SYSTEM_FLOW_ID + tableId + '-' + UNACCOUNTED_FLOWS_COUNTER.incrementAndGet();
236         return new FlowId(alienId);
237     }
238 }