2 * Copyright (c) 2014, 2017 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.applications.frm.impl;
10 import static org.opendaylight.openflowplugin.applications.frm.util.FrmUtil.buildGroupInstanceIdentifier;
11 import static org.opendaylight.openflowplugin.applications.frm.util.FrmUtil.getActiveBundle;
12 import static org.opendaylight.openflowplugin.applications.frm.util.FrmUtil.getFlowId;
13 import static org.opendaylight.openflowplugin.applications.frm.util.FrmUtil.getNodeIdFromNodeIdentifier;
14 import static org.opendaylight.openflowplugin.applications.frm.util.FrmUtil.isFlowDependentOnGroup;
15 import static org.opendaylight.openflowplugin.applications.frm.util.FrmUtil.isGroupExistsOnDevice;
17 import com.google.common.base.Preconditions;
18 import com.google.common.util.concurrent.FluentFuture;
19 import com.google.common.util.concurrent.FutureCallback;
20 import com.google.common.util.concurrent.Futures;
21 import com.google.common.util.concurrent.ListenableFuture;
22 import com.google.common.util.concurrent.MoreExecutors;
23 import com.google.common.util.concurrent.SettableFuture;
24 import java.util.Optional;
25 import java.util.concurrent.ExecutionException;
26 import java.util.concurrent.Future;
27 import org.opendaylight.infrautils.utils.concurrent.LoggingFutures;
28 import org.opendaylight.mdsal.binding.api.DataBroker;
29 import org.opendaylight.mdsal.binding.api.DataTreeIdentifier;
30 import org.opendaylight.mdsal.binding.api.ReadTransaction;
31 import org.opendaylight.mdsal.binding.api.WriteTransaction;
32 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
33 import org.opendaylight.openflowplugin.applications.frm.ForwardingRulesManager;
34 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Uri;
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;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.TableKey;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.Flow;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.StaleFlow;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.StaleFlowBuilder;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.StaleFlowKey;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.AddFlowInput;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.AddFlowInputBuilder;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.AddFlowOutput;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.FlowTableRef;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.RemoveFlowInputBuilder;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.RemoveFlowOutput;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.UpdateFlowInput;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.UpdateFlowInputBuilder;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.UpdateFlowOutput;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.flow.update.OriginalFlowBuilder;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.flow.update.UpdatedFlowBuilder;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.FlowRef;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.service.rev130918.AddGroupInput;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.service.rev130918.AddGroupInputBuilder;
57 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.service.rev130918.AddGroupOutput;
58 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.GroupRef;
59 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.groups.Group;
60 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
61 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeRef;
62 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
63 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
64 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.onf.rev170124.BundleId;
65 import org.opendaylight.yangtools.concepts.ListenerRegistration;
66 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
67 import org.opendaylight.yangtools.yang.common.RpcError;
68 import org.opendaylight.yangtools.yang.common.RpcResult;
69 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
70 import org.opendaylight.yangtools.yang.common.Uint32;
71 import org.slf4j.Logger;
72 import org.slf4j.LoggerFactory;
75 * FlowForwarder It implements
76 * {@link org.opendaylight.mdsal.binding.api.DataTreeChangeListener}
77 * for WildCardedPath to {@link Flow} and ForwardingRulesCommiter interface for
78 * methods: add, update and remove {@link Flow} processing for
79 * {@link org.opendaylight.mdsal.binding.api.DataTreeModification}.
81 public class FlowForwarder extends AbstractListeningCommiter<Flow> {
83 private static final Logger LOG = LoggerFactory.getLogger(FlowForwarder.class);
85 private static final String GROUP_EXISTS_IN_DEVICE_ERROR = "GROUPEXISTS";
87 private ListenerRegistration<FlowForwarder> listenerRegistration;
89 public FlowForwarder(final ForwardingRulesManager manager, final DataBroker db) {
94 @SuppressWarnings("IllegalCatch")
95 public void registerListener() {
96 final DataTreeIdentifier<Flow> treeId = DataTreeIdentifier.create(LogicalDatastoreType.CONFIGURATION,
99 listenerRegistration = dataBroker.registerDataTreeChangeListener(treeId, FlowForwarder.this);
100 } catch (final Exception e) {
101 LOG.warn("FRM Flow DataTreeChange listener registration fail!");
102 LOG.debug("FRM Flow DataTreeChange listener registration fail ..", e);
103 throw new IllegalStateException("FlowForwarder startup fail! System needs restart.", e);
109 public void deregisterListener() {
114 public void close() {
115 if (listenerRegistration != null) {
116 listenerRegistration.close();
117 listenerRegistration = null;
122 public void remove(final InstanceIdentifier<Flow> identifier, final Flow removeDataObj,
123 final InstanceIdentifier<FlowCapableNode> nodeIdent) {
125 final TableKey tableKey = identifier.firstKeyOf(Table.class);
126 if (tableIdValidationPrecondition(tableKey, removeDataObj)) {
127 BundleId bundleId = getActiveBundle(nodeIdent, provider);
128 if (bundleId != null) {
129 provider.getBundleFlowListener().remove(identifier, removeDataObj, nodeIdent, bundleId);
131 final NodeId nodeId = getNodeIdFromNodeIdentifier(nodeIdent);
132 nodeConfigurator.enqueueJob(nodeId.getValue(), () -> {
133 final RemoveFlowInputBuilder builder = new RemoveFlowInputBuilder(removeDataObj);
134 builder.setFlowRef(new FlowRef(identifier));
135 builder.setNode(new NodeRef(nodeIdent.firstIdentifierOf(Node.class)));
136 builder.setFlowTable(new FlowTableRef(nodeIdent.child(Table.class, tableKey)));
138 // This method is called only when a given flow object has been
139 // removed from datastore. So FRM always needs to set strict flag
140 // into remove-flow input so that only a flow entry associated with
141 // a given flow object is removed.
142 builder.setTransactionUri(new Uri(provider.getNewTransactionId())).setStrict(Boolean.TRUE);
143 final ListenableFuture<RpcResult<RemoveFlowOutput>> resultFuture =
144 provider.getSalFlowService().removeFlow(builder.build());
145 LoggingFutures.addErrorLogging(resultFuture, LOG, "removeFlow");
152 // TODO: Pull this into ForwardingRulesCommiter and override it here
155 public ListenableFuture<RpcResult<RemoveFlowOutput>> removeWithResult(final InstanceIdentifier<Flow> identifier,
156 final Flow removeDataObj, final InstanceIdentifier<FlowCapableNode> nodeIdent) {
158 ListenableFuture<RpcResult<RemoveFlowOutput>> resultFuture = SettableFuture.create();
159 final TableKey tableKey = identifier.firstKeyOf(Table.class);
160 if (tableIdValidationPrecondition(tableKey, removeDataObj)) {
161 final RemoveFlowInputBuilder builder = new RemoveFlowInputBuilder(removeDataObj);
162 builder.setFlowRef(new FlowRef(identifier));
163 builder.setNode(new NodeRef(nodeIdent.firstIdentifierOf(Node.class)));
164 builder.setFlowTable(new FlowTableRef(nodeIdent.child(Table.class, tableKey)));
166 // This method is called only when a given flow object has been
167 // removed from datastore. So FRM always needs to set strict flag
168 // into remove-flow input so that only a flow entry associated with
169 // a given flow object is removed.
170 builder.setTransactionUri(new Uri(provider.getNewTransactionId())).setStrict(Boolean.TRUE);
171 resultFuture = provider.getSalFlowService().removeFlow(builder.build());
178 public void update(final InstanceIdentifier<Flow> identifier, final Flow original, final Flow update,
179 final InstanceIdentifier<FlowCapableNode> nodeIdent) {
181 final TableKey tableKey = identifier.firstKeyOf(Table.class);
182 if (tableIdValidationPrecondition(tableKey, update)) {
183 BundleId bundleId = getActiveBundle(nodeIdent, provider);
184 if (bundleId != null) {
185 provider.getBundleFlowListener().update(identifier, original, update, nodeIdent, bundleId);
187 final NodeId nodeId = getNodeIdFromNodeIdentifier(nodeIdent);
188 nodeConfigurator.enqueueJob(nodeId.getValue(), () -> {
189 final UpdateFlowInputBuilder builder = new UpdateFlowInputBuilder();
190 builder.setNode(new NodeRef(nodeIdent.firstIdentifierOf(Node.class)));
191 builder.setFlowRef(new FlowRef(identifier));
192 builder.setTransactionUri(new Uri(provider.getNewTransactionId()));
194 // This method is called only when a given flow object in datastore
195 // has been updated. So FRM always needs to set strict flag into
196 // update-flow input so that only a flow entry associated with
197 // a given flow object is updated.
198 builder.setUpdatedFlow(new UpdatedFlowBuilder(update).setStrict(Boolean.TRUE).build());
199 builder.setOriginalFlow(new OriginalFlowBuilder(original).setStrict(Boolean.TRUE).build());
201 Uint32 groupId = isFlowDependentOnGroup(update);
202 if (groupId != null) {
203 LOG.trace("The flow {} is dependent on group {}. Checking if the group is already present",
204 getFlowId(new FlowRef(identifier)), groupId);
205 if (isGroupExistsOnDevice(nodeIdent, groupId, provider)) {
206 LOG.trace("The dependent group {} is already programmed. Updating the flow {}", groupId,
207 getFlowId(new FlowRef(identifier)));
208 return provider.getSalFlowService().updateFlow(builder.build());
210 LOG.trace("The dependent group {} isn't programmed yet. Pushing the group", groupId);
211 ListenableFuture<RpcResult<AddGroupOutput>> groupFuture = pushDependentGroup(nodeIdent,
213 SettableFuture<RpcResult<UpdateFlowOutput>> resultFuture = SettableFuture.create();
214 Futures.addCallback(groupFuture,
215 new UpdateFlowCallBack(builder.build(), nodeId, resultFuture, groupId),
216 MoreExecutors.directExecutor());
221 LOG.trace("The flow {} is not dependent on any group. Updating the flow",
222 getFlowId(new FlowRef(identifier)));
223 return provider.getSalFlowService().updateFlow(builder.build());
230 public Future<? extends RpcResult<?>> add(final InstanceIdentifier<Flow> identifier, final Flow addDataObj,
231 final InstanceIdentifier<FlowCapableNode> nodeIdent) {
233 final TableKey tableKey = identifier.firstKeyOf(Table.class);
234 if (tableIdValidationPrecondition(tableKey, addDataObj)) {
235 BundleId bundleId = getActiveBundle(nodeIdent, provider);
236 if (bundleId != null) {
237 return provider.getBundleFlowListener().add(identifier, addDataObj, nodeIdent, bundleId);
239 final NodeId nodeId = getNodeIdFromNodeIdentifier(nodeIdent);
240 nodeConfigurator.enqueueJob(nodeId.getValue(), () -> {
241 final AddFlowInputBuilder builder = new AddFlowInputBuilder(addDataObj);
243 builder.setNode(new NodeRef(nodeIdent.firstIdentifierOf(Node.class)));
244 builder.setFlowRef(new FlowRef(identifier));
245 builder.setFlowTable(new FlowTableRef(nodeIdent.child(Table.class, tableKey)));
246 builder.setTransactionUri(new Uri(provider.getNewTransactionId()));
247 Uint32 groupId = isFlowDependentOnGroup(addDataObj);
248 if (groupId != null) {
249 LOG.trace("The flow {} is dependent on group {}. Checking if the group is already present",
250 getFlowId(new FlowRef(identifier)), groupId);
251 if (isGroupExistsOnDevice(nodeIdent, groupId, provider)) {
252 LOG.trace("The dependent group {} is already programmed. Adding the flow {}", groupId,
253 getFlowId(new FlowRef(identifier)));
254 return provider.getSalFlowService().addFlow(builder.build());
256 LOG.trace("The dependent group {} isn't programmed yet. Pushing the group", groupId);
257 ListenableFuture<RpcResult<AddGroupOutput>> groupFuture = pushDependentGroup(nodeIdent,
259 SettableFuture<RpcResult<AddFlowOutput>> resultFuture = SettableFuture.create();
260 Futures.addCallback(groupFuture, new AddFlowCallBack(builder.build(), nodeId, groupId,
261 resultFuture), MoreExecutors.directExecutor());
266 LOG.trace("The flow {} is not dependent on any group. Adding the flow",
267 getFlowId(new FlowRef(identifier)));
268 return provider.getSalFlowService().addFlow(builder.build());
272 return Futures.immediateFuture(null);
276 public void createStaleMarkEntity(InstanceIdentifier<Flow> identifier, Flow del,
277 InstanceIdentifier<FlowCapableNode> nodeIdent) {
278 LOG.debug("Creating Stale-Mark entry for the switch {} for flow {} ", nodeIdent, del);
279 StaleFlow staleFlow = makeStaleFlow(identifier, del, nodeIdent);
280 persistStaleFlow(staleFlow, nodeIdent);
284 protected InstanceIdentifier<Flow> getWildCardPath() {
285 return InstanceIdentifier.create(Nodes.class).child(Node.class).augmentation(FlowCapableNode.class)
286 .child(Table.class).child(Flow.class);
289 private static boolean tableIdValidationPrecondition(final TableKey tableKey, final Flow flow) {
290 Preconditions.checkNotNull(tableKey, "TableKey can not be null or empty!");
291 Preconditions.checkNotNull(flow, "Flow can not be null or empty!");
292 if (!tableKey.getId().equals(flow.getTableId())) {
293 LOG.warn("TableID in URI tableId={} and in palyload tableId={} is not same.", flow.getTableId(),
300 private StaleFlow makeStaleFlow(InstanceIdentifier<Flow> identifier, Flow del,
301 InstanceIdentifier<FlowCapableNode> nodeIdent) {
302 StaleFlowBuilder staleFlowBuilder = new StaleFlowBuilder(del);
303 return staleFlowBuilder.setId(del.getId()).build();
306 private void persistStaleFlow(StaleFlow staleFlow, InstanceIdentifier<FlowCapableNode> nodeIdent) {
307 WriteTransaction writeTransaction = dataBroker.newWriteOnlyTransaction();
308 writeTransaction.put(LogicalDatastoreType.CONFIGURATION, getStaleFlowInstanceIdentifier(staleFlow, nodeIdent),
311 FluentFuture<?> submitFuture = writeTransaction.commit();
312 handleStaleFlowResultFuture(submitFuture);
315 private void handleStaleFlowResultFuture(FluentFuture<?> submitFuture) {
316 submitFuture.addCallback(new FutureCallback<Object>() {
318 public void onSuccess(Object result) {
319 LOG.debug("Stale Flow creation success");
323 public void onFailure(Throwable throwable) {
324 LOG.error("Stale Flow creation failed", throwable);
326 }, MoreExecutors.directExecutor());
330 private InstanceIdentifier<org.opendaylight.yang.gen.v1.urn.opendaylight
331 .flow.inventory.rev130819.tables.table.StaleFlow> getStaleFlowInstanceIdentifier(
332 StaleFlow staleFlow, InstanceIdentifier<FlowCapableNode> nodeIdent) {
333 return nodeIdent.child(Table.class, new TableKey(staleFlow.getTableId())).child(
334 org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.StaleFlow.class,
335 new StaleFlowKey(new FlowId(staleFlow.getId())));
338 private ListenableFuture<RpcResult<AddGroupOutput>> pushDependentGroup(
339 final InstanceIdentifier<FlowCapableNode> nodeIdent, final Uint32 groupId) {
341 //TODO This read to the DS might have a performance impact.
342 //if the dependent group is not installed than we should just cache the parent group,
343 //till we receive the dependent group DTCN and then push it.
345 InstanceIdentifier<Group> groupIdent = buildGroupInstanceIdentifier(nodeIdent, groupId);
346 ListenableFuture<RpcResult<AddGroupOutput>> resultFuture;
347 LOG.info("Reading the group from config inventory: {}", groupId);
348 try (ReadTransaction readTransaction = provider.getReadTransaction()) {
349 Optional<Group> group = readTransaction.read(LogicalDatastoreType.CONFIGURATION, groupIdent).get();
350 if (group.isPresent()) {
351 final AddGroupInputBuilder builder = new AddGroupInputBuilder(group.get());
352 builder.setNode(new NodeRef(nodeIdent.firstIdentifierOf(Node.class)));
353 builder.setGroupRef(new GroupRef(nodeIdent));
354 builder.setTransactionUri(new Uri(provider.getNewTransactionId()));
355 AddGroupInput addGroupInput = builder.build();
356 resultFuture = this.provider.getSalGroupService().addGroup(addGroupInput);
358 resultFuture = Futures.immediateFuture(RpcResultBuilder.<AddGroupOutput>failed()
359 .withError(RpcError.ErrorType.APPLICATION,
360 "Group " + groupId + " not present in the config inventory").build());
362 } catch (InterruptedException | ExecutionException e) {
363 LOG.error("Error while reading group from config datastore for the group ID {}", groupId, e);
364 resultFuture = Futures.immediateFuture(RpcResultBuilder.<AddGroupOutput>failed()
365 .withError(RpcError.ErrorType.APPLICATION,
366 "Error while reading group " + groupId + " from inventory").build());
371 private final class AddFlowCallBack implements FutureCallback<RpcResult<AddGroupOutput>> {
372 private final AddFlowInput addFlowInput;
373 private final NodeId nodeId;
374 private final Uint32 groupId;
375 private final SettableFuture<RpcResult<AddFlowOutput>> resultFuture;
377 private AddFlowCallBack(final AddFlowInput addFlowInput, final NodeId nodeId, Uint32 groupId,
378 SettableFuture<RpcResult<AddFlowOutput>> resultFuture) {
379 this.addFlowInput = addFlowInput;
380 this.nodeId = nodeId;
381 this.groupId = groupId;
382 this.resultFuture = resultFuture;
386 public void onSuccess(RpcResult<AddGroupOutput> rpcResult) {
387 if (rpcResult.isSuccessful() || rpcResult.getErrors().size() == 1
388 && rpcResult.getErrors().iterator().next().getMessage().contains(GROUP_EXISTS_IN_DEVICE_ERROR)) {
389 provider.getDevicesGroupRegistry().storeGroup(nodeId, groupId);
390 Futures.addCallback(provider.getSalFlowService().addFlow(addFlowInput),
391 new FutureCallback<RpcResult<AddFlowOutput>>() {
393 public void onSuccess(RpcResult<AddFlowOutput> result) {
394 resultFuture.set(result);
398 public void onFailure(Throwable failure) {
399 resultFuture.setException(failure);
401 }, MoreExecutors.directExecutor());
403 LOG.debug("Flow add with id {} finished without error for node {}",
404 getFlowId(addFlowInput.getFlowRef()), nodeId);
406 LOG.error("Flow add with id {} failed for node {} with error {}", getFlowId(addFlowInput.getFlowRef()),
407 nodeId, rpcResult.getErrors());
408 resultFuture.set(RpcResultBuilder.<AddFlowOutput>failed()
409 .withRpcErrors(rpcResult.getErrors()).build());
414 public void onFailure(Throwable throwable) {
415 LOG.error("Service call for adding flow with id {} failed for node {}",
416 getFlowId(addFlowInput.getFlowRef()), nodeId, throwable);
417 resultFuture.setException(throwable);
421 private final class UpdateFlowCallBack implements FutureCallback<RpcResult<AddGroupOutput>> {
422 private final UpdateFlowInput updateFlowInput;
423 private final NodeId nodeId;
424 private final Uint32 groupId;
425 private final SettableFuture<RpcResult<UpdateFlowOutput>> resultFuture;
427 private UpdateFlowCallBack(final UpdateFlowInput updateFlowInput, final NodeId nodeId,
428 SettableFuture<RpcResult<UpdateFlowOutput>> resultFuture, Uint32 groupId) {
429 this.updateFlowInput = updateFlowInput;
430 this.nodeId = nodeId;
431 this.groupId = groupId;
432 this.resultFuture = resultFuture;
436 public void onSuccess(RpcResult<AddGroupOutput> rpcResult) {
437 if (rpcResult.isSuccessful() || rpcResult.getErrors().size() == 1
438 && rpcResult.getErrors().iterator().next().getMessage().contains(GROUP_EXISTS_IN_DEVICE_ERROR)) {
439 provider.getDevicesGroupRegistry().storeGroup(nodeId, groupId);
440 Futures.addCallback(provider.getSalFlowService().updateFlow(updateFlowInput),
441 new FutureCallback<RpcResult<UpdateFlowOutput>>() {
443 public void onSuccess(RpcResult<UpdateFlowOutput> result) {
444 resultFuture.set(result);
448 public void onFailure(Throwable failure) {
449 resultFuture.setException(failure);
451 }, MoreExecutors.directExecutor());
453 LOG.debug("Flow update with id {} finished without error for node {}",
454 getFlowId(updateFlowInput.getFlowRef()), nodeId);
456 LOG.error("Flow update with id {} failed for node {} with error {}",
457 getFlowId(updateFlowInput.getFlowRef()), nodeId, rpcResult.getErrors());
458 resultFuture.set(RpcResultBuilder.<UpdateFlowOutput>failed()
459 .withRpcErrors(rpcResult.getErrors()).build());
464 public void onFailure(Throwable throwable) {
465 LOG.error("Service call for updating flow with id {} failed for node {}",
466 getFlowId(updateFlowInput.getFlowRef()), nodeId, throwable);
467 resultFuture.setException(throwable);