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 java.util.Objects.requireNonNull;
11 import static org.opendaylight.openflowplugin.applications.frm.util.FrmUtil.buildGroupInstanceIdentifier;
12 import static org.opendaylight.openflowplugin.applications.frm.util.FrmUtil.getActiveBundle;
13 import static org.opendaylight.openflowplugin.applications.frm.util.FrmUtil.getFlowId;
14 import static org.opendaylight.openflowplugin.applications.frm.util.FrmUtil.getNodeIdValueFromNodeIdentifier;
15 import static org.opendaylight.openflowplugin.applications.frm.util.FrmUtil.isFlowDependentOnGroup;
16 import static org.opendaylight.openflowplugin.applications.frm.util.FrmUtil.isGroupExistsOnDevice;
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.ReadTransaction;
30 import org.opendaylight.mdsal.binding.api.WriteTransaction;
31 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
32 import org.opendaylight.openflowplugin.applications.frm.ForwardingRulesManager;
33 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Uri;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowId;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.Table;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.TableKey;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.Flow;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.StaleFlow;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.StaleFlowBuilder;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.StaleFlowKey;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.AddFlowInput;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.AddFlowInputBuilder;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.AddFlowOutput;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.FlowTableRef;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.RemoveFlowInputBuilder;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.RemoveFlowOutput;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.UpdateFlowInput;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.UpdateFlowInputBuilder;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.UpdateFlowOutput;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.flow.update.OriginalFlowBuilder;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.flow.update.UpdatedFlowBuilder;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.FlowRef;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.service.rev130918.AddGroupInput;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.service.rev130918.AddGroupInputBuilder;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.service.rev130918.AddGroupOutput;
57 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.GroupRef;
58 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.groups.Group;
59 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeRef;
60 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
61 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
62 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.onf.rev170124.BundleId;
63 import org.opendaylight.yangtools.concepts.ListenerRegistration;
64 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
65 import org.opendaylight.yangtools.yang.common.ErrorType;
66 import org.opendaylight.yangtools.yang.common.RpcResult;
67 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
68 import org.opendaylight.yangtools.yang.common.Uint32;
69 import org.slf4j.Logger;
70 import org.slf4j.LoggerFactory;
73 * FlowForwarder It implements
74 * {@link org.opendaylight.mdsal.binding.api.DataTreeChangeListener}
75 * for WildCardedPath to {@link Flow} and ForwardingRulesCommiter interface for
76 * methods: add, update and remove {@link Flow} processing for
77 * {@link org.opendaylight.mdsal.binding.api.DataTreeModification}.
79 public class FlowForwarder extends AbstractListeningCommiter<Flow> {
81 private static final Logger LOG = LoggerFactory.getLogger(FlowForwarder.class);
83 private static final String GROUP_EXISTS_IN_DEVICE_ERROR = "GROUPEXISTS";
85 private ListenerRegistration<FlowForwarder> listenerRegistration;
87 private final BundleFlowForwarder bundleFlowForwarder;
89 public FlowForwarder(final ForwardingRulesManager manager, final DataBroker db,
90 final ListenerRegistrationHelper registrationHelper) {
91 super(manager, db, registrationHelper);
92 bundleFlowForwarder = new BundleFlowForwarder(manager);
96 public void deregisterListener() {
101 public void close() {
102 if (listenerRegistration != null) {
103 listenerRegistration.close();
104 listenerRegistration = null;
109 public void remove(final InstanceIdentifier<Flow> identifier, final Flow removeDataObj,
110 final InstanceIdentifier<FlowCapableNode> nodeIdent) {
112 final TableKey tableKey = identifier.firstKeyOf(Table.class);
113 if (tableIdValidationPrecondition(tableKey, removeDataObj)) {
114 BundleId bundleId = getActiveBundle(nodeIdent, provider);
115 if (bundleId != null) {
116 provider.getBundleFlowListener().remove(identifier, removeDataObj, nodeIdent, bundleId);
118 final String nodeId = getNodeIdValueFromNodeIdentifier(nodeIdent);
119 nodeConfigurator.enqueueJob(nodeId, () -> {
120 final RemoveFlowInputBuilder builder = new RemoveFlowInputBuilder(removeDataObj);
121 builder.setFlowRef(new FlowRef(identifier));
122 builder.setNode(new NodeRef(nodeIdent.firstIdentifierOf(Node.class)));
123 builder.setFlowTable(new FlowTableRef(nodeIdent.child(Table.class, tableKey)));
125 // This method is called only when a given flow object has been
126 // removed from datastore. So FRM always needs to set strict flag
127 // into remove-flow input so that only a flow entry associated with
128 // a given flow object is removed.
129 builder.setTransactionUri(new Uri(provider.getNewTransactionId())).setStrict(Boolean.TRUE);
130 final ListenableFuture<RpcResult<RemoveFlowOutput>> resultFuture =
131 provider.getSalFlowService().removeFlow(builder.build());
132 LoggingFutures.addErrorLogging(resultFuture, LOG, "removeFlow");
139 // TODO: Pull this into ForwardingRulesCommiter and override it here
142 public ListenableFuture<RpcResult<RemoveFlowOutput>> removeWithResult(final InstanceIdentifier<Flow> identifier,
143 final Flow removeDataObj, final InstanceIdentifier<FlowCapableNode> nodeIdent) {
145 ListenableFuture<RpcResult<RemoveFlowOutput>> resultFuture = SettableFuture.create();
146 final TableKey tableKey = identifier.firstKeyOf(Table.class);
147 if (tableIdValidationPrecondition(tableKey, removeDataObj)) {
148 final RemoveFlowInputBuilder builder = new RemoveFlowInputBuilder(removeDataObj);
149 builder.setFlowRef(new FlowRef(identifier));
150 builder.setNode(new NodeRef(nodeIdent.firstIdentifierOf(Node.class)));
151 builder.setFlowTable(new FlowTableRef(nodeIdent.child(Table.class, tableKey)));
153 // This method is called only when a given flow object has been
154 // removed from datastore. So FRM always needs to set strict flag
155 // into remove-flow input so that only a flow entry associated with
156 // a given flow object is removed.
157 builder.setTransactionUri(new Uri(provider.getNewTransactionId())).setStrict(Boolean.TRUE);
158 resultFuture = provider.getSalFlowService().removeFlow(builder.build());
165 public void update(final InstanceIdentifier<Flow> identifier, final Flow original, final Flow update,
166 final InstanceIdentifier<FlowCapableNode> nodeIdent) {
168 final TableKey tableKey = identifier.firstKeyOf(Table.class);
169 if (tableIdValidationPrecondition(tableKey, update)) {
170 BundleId bundleId = getActiveBundle(nodeIdent, provider);
171 if (bundleId != null) {
172 provider.getBundleFlowListener().update(identifier, original, update, nodeIdent, bundleId);
174 final String nodeId = getNodeIdValueFromNodeIdentifier(nodeIdent);
175 nodeConfigurator.enqueueJob(nodeId, () -> {
176 final UpdateFlowInputBuilder builder = new UpdateFlowInputBuilder();
177 builder.setNode(new NodeRef(nodeIdent.firstIdentifierOf(Node.class)));
178 builder.setFlowRef(new FlowRef(identifier));
179 builder.setTransactionUri(new Uri(provider.getNewTransactionId()));
181 // This method is called only when a given flow object in datastore
182 // has been updated. So FRM always needs to set strict flag into
183 // update-flow input so that only a flow entry associated with
184 // a given flow object is updated.
185 builder.setUpdatedFlow(new UpdatedFlowBuilder(update).setStrict(Boolean.TRUE).build());
186 builder.setOriginalFlow(new OriginalFlowBuilder(original).setStrict(Boolean.TRUE).build());
188 Uint32 groupId = isFlowDependentOnGroup(update);
189 if (groupId != null) {
190 LOG.trace("The flow {} is dependent on group {}. Checking if the group is already present",
191 getFlowId(identifier), groupId);
192 if (isGroupExistsOnDevice(nodeIdent, groupId, provider)) {
193 LOG.trace("The dependent group {} is already programmed. Updating the flow {}", groupId,
194 getFlowId(identifier));
195 return provider.getSalFlowService().updateFlow(builder.build());
197 LOG.trace("The dependent group {} isn't programmed yet. Pushing the group", groupId);
198 ListenableFuture<RpcResult<AddGroupOutput>> groupFuture = pushDependentGroup(nodeIdent,
200 SettableFuture<RpcResult<UpdateFlowOutput>> resultFuture = SettableFuture.create();
201 Futures.addCallback(groupFuture,
202 new UpdateFlowCallBack(builder.build(), nodeId, resultFuture, groupId),
203 MoreExecutors.directExecutor());
208 LOG.trace("The flow {} is not dependent on any group. Updating the flow",
209 getFlowId(identifier));
210 return provider.getSalFlowService().updateFlow(builder.build());
217 public Future<? extends RpcResult<?>> add(final InstanceIdentifier<Flow> identifier, final Flow addDataObj,
218 final InstanceIdentifier<FlowCapableNode> nodeIdent) {
220 final TableKey tableKey = identifier.firstKeyOf(Table.class);
221 if (tableIdValidationPrecondition(tableKey, addDataObj)) {
222 BundleId bundleId = getActiveBundle(nodeIdent, provider);
223 if (bundleId != null) {
224 return provider.getBundleFlowListener().add(identifier, addDataObj, nodeIdent, bundleId);
226 final String nodeId = getNodeIdValueFromNodeIdentifier(nodeIdent);
227 nodeConfigurator.enqueueJob(nodeId, () -> {
228 final AddFlowInputBuilder builder = new AddFlowInputBuilder(addDataObj);
230 builder.setNode(new NodeRef(nodeIdent.firstIdentifierOf(Node.class)));
231 builder.setFlowRef(new FlowRef(identifier));
232 builder.setFlowTable(new FlowTableRef(nodeIdent.child(Table.class, tableKey)));
233 builder.setTransactionUri(new Uri(provider.getNewTransactionId()));
234 Uint32 groupId = isFlowDependentOnGroup(addDataObj);
235 if (groupId != null) {
236 LOG.trace("The flow {} is dependent on group {}. Checking if the group is already present",
237 getFlowId(new FlowRef(identifier)), groupId);
238 if (isGroupExistsOnDevice(nodeIdent, groupId, provider)) {
239 LOG.trace("The dependent group {} is already programmed. Adding the flow {}", groupId,
240 getFlowId(new FlowRef(identifier)));
241 return provider.getSalFlowService().addFlow(builder.build());
243 LOG.trace("The dependent group {} isn't programmed yet. Pushing the group", groupId);
244 ListenableFuture<RpcResult<AddGroupOutput>> groupFuture = pushDependentGroup(nodeIdent,
246 SettableFuture<RpcResult<AddFlowOutput>> resultFuture = SettableFuture.create();
247 Futures.addCallback(groupFuture, new AddFlowCallBack(builder.build(), nodeId, groupId,
248 resultFuture), MoreExecutors.directExecutor());
253 LOG.trace("The flow {} is not dependent on any group. Adding the flow",
254 getFlowId(new FlowRef(identifier)));
255 return provider.getSalFlowService().addFlow(builder.build());
259 return Futures.immediateFuture(null);
263 public void createStaleMarkEntity(final InstanceIdentifier<Flow> identifier, final Flow del,
264 final InstanceIdentifier<FlowCapableNode> nodeIdent) {
265 LOG.debug("Creating Stale-Mark entry for the switch {} for flow {} ", nodeIdent, del);
266 StaleFlow staleFlow = makeStaleFlow(identifier, del, nodeIdent);
267 persistStaleFlow(staleFlow, nodeIdent);
271 protected InstanceIdentifier<Flow> getWildCardPath() {
272 return InstanceIdentifier.create(Nodes.class).child(Node.class).augmentation(FlowCapableNode.class)
273 .child(Table.class).child(Flow.class);
276 private static boolean tableIdValidationPrecondition(final TableKey tableKey, final Flow flow) {
277 requireNonNull(tableKey, "TableKey can not be null or empty!");
278 requireNonNull(flow, "Flow can not be null or empty!");
279 if (!tableKey.getId().equals(flow.getTableId())) {
280 LOG.warn("TableID in URI tableId={} and in palyload tableId={} is not same.", flow.getTableId(),
287 private static StaleFlow makeStaleFlow(final InstanceIdentifier<Flow> identifier, final Flow del,
288 final InstanceIdentifier<FlowCapableNode> nodeIdent) {
289 StaleFlowBuilder staleFlowBuilder = new StaleFlowBuilder(del);
290 return staleFlowBuilder.setId(del.getId()).build();
293 private void persistStaleFlow(final StaleFlow staleFlow, final InstanceIdentifier<FlowCapableNode> nodeIdent) {
294 WriteTransaction writeTransaction = dataBroker.newWriteOnlyTransaction();
295 writeTransaction.put(LogicalDatastoreType.CONFIGURATION, getStaleFlowInstanceIdentifier(staleFlow, nodeIdent),
298 FluentFuture<?> submitFuture = writeTransaction.commit();
299 handleStaleFlowResultFuture(submitFuture);
302 private static void handleStaleFlowResultFuture(final FluentFuture<?> submitFuture) {
303 submitFuture.addCallback(new FutureCallback<Object>() {
305 public void onSuccess(final Object result) {
306 LOG.debug("Stale Flow creation success");
310 public void onFailure(final Throwable throwable) {
311 LOG.error("Stale Flow creation failed", throwable);
313 }, MoreExecutors.directExecutor());
317 private static InstanceIdentifier<org.opendaylight.yang.gen.v1.urn.opendaylight
318 .flow.inventory.rev130819.tables.table.StaleFlow> getStaleFlowInstanceIdentifier(
319 final StaleFlow staleFlow, final InstanceIdentifier<FlowCapableNode> nodeIdent) {
320 return nodeIdent.child(Table.class, new TableKey(staleFlow.getTableId())).child(
321 org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.StaleFlow.class,
322 new StaleFlowKey(new FlowId(staleFlow.getId())));
325 private ListenableFuture<RpcResult<AddGroupOutput>> pushDependentGroup(
326 final InstanceIdentifier<FlowCapableNode> nodeIdent, final Uint32 groupId) {
328 //TODO This read to the DS might have a performance impact.
329 //if the dependent group is not installed than we should just cache the parent group,
330 //till we receive the dependent group DTCN and then push it.
332 InstanceIdentifier<Group> groupIdent = buildGroupInstanceIdentifier(nodeIdent, groupId);
333 ListenableFuture<RpcResult<AddGroupOutput>> resultFuture;
334 LOG.info("Reading the group from config inventory: {}", groupId);
335 try (ReadTransaction readTransaction = provider.getReadTransaction()) {
336 Optional<Group> group = readTransaction.read(LogicalDatastoreType.CONFIGURATION, groupIdent).get();
337 if (group.isPresent()) {
338 final AddGroupInputBuilder builder = new AddGroupInputBuilder(group.get());
339 builder.setNode(new NodeRef(nodeIdent.firstIdentifierOf(Node.class)));
340 builder.setGroupRef(new GroupRef(nodeIdent));
341 builder.setTransactionUri(new Uri(provider.getNewTransactionId()));
342 AddGroupInput addGroupInput = builder.build();
343 resultFuture = provider.getSalGroupService().addGroup(addGroupInput);
345 resultFuture = RpcResultBuilder.<AddGroupOutput>failed()
346 .withError(ErrorType.APPLICATION,
347 "Group " + groupId + " not present in the config inventory").buildFuture();
349 } catch (InterruptedException | ExecutionException e) {
350 LOG.error("Error while reading group from config datastore for the group ID {}", groupId, e);
351 resultFuture = RpcResultBuilder.<AddGroupOutput>failed()
352 .withError(ErrorType.APPLICATION,
353 "Error while reading group " + groupId + " from inventory").buildFuture();
358 private final class AddFlowCallBack implements FutureCallback<RpcResult<AddGroupOutput>> {
359 private final AddFlowInput addFlowInput;
360 private final String nodeId;
361 private final Uint32 groupId;
362 private final SettableFuture<RpcResult<AddFlowOutput>> resultFuture;
364 private AddFlowCallBack(final AddFlowInput addFlowInput, final String nodeId, final Uint32 groupId,
365 final SettableFuture<RpcResult<AddFlowOutput>> resultFuture) {
366 this.addFlowInput = addFlowInput;
367 this.nodeId = nodeId;
368 this.groupId = groupId;
369 this.resultFuture = resultFuture;
373 public void onSuccess(final RpcResult<AddGroupOutput> rpcResult) {
374 if (rpcResult.isSuccessful() || rpcResult.getErrors().size() == 1
375 && rpcResult.getErrors().iterator().next().getMessage().contains(GROUP_EXISTS_IN_DEVICE_ERROR)) {
376 provider.getDevicesGroupRegistry().storeGroup(nodeId, groupId);
377 Futures.addCallback(provider.getSalFlowService().addFlow(addFlowInput),
378 new FutureCallback<RpcResult<AddFlowOutput>>() {
380 public void onSuccess(final RpcResult<AddFlowOutput> result) {
381 resultFuture.set(result);
385 public void onFailure(final Throwable failure) {
386 resultFuture.setException(failure);
388 }, MoreExecutors.directExecutor());
390 LOG.debug("Flow add with id {} finished without error for node {}",
391 getFlowId(addFlowInput.getFlowRef()), nodeId);
393 LOG.error("Flow add with id {} failed for node {} with error {}", getFlowId(addFlowInput.getFlowRef()),
394 nodeId, rpcResult.getErrors());
395 resultFuture.set(RpcResultBuilder.<AddFlowOutput>failed()
396 .withRpcErrors(rpcResult.getErrors()).build());
401 public void onFailure(final Throwable throwable) {
402 LOG.error("Service call for adding flow with id {} failed for node {}",
403 getFlowId(addFlowInput.getFlowRef()), nodeId, throwable);
404 resultFuture.setException(throwable);
408 private final class UpdateFlowCallBack implements FutureCallback<RpcResult<AddGroupOutput>> {
409 private final UpdateFlowInput updateFlowInput;
410 private final String nodeId;
411 private final Uint32 groupId;
412 private final SettableFuture<RpcResult<UpdateFlowOutput>> resultFuture;
414 private UpdateFlowCallBack(final UpdateFlowInput updateFlowInput, final String nodeId,
415 final SettableFuture<RpcResult<UpdateFlowOutput>> resultFuture, final Uint32 groupId) {
416 this.updateFlowInput = updateFlowInput;
417 this.nodeId = nodeId;
418 this.groupId = groupId;
419 this.resultFuture = resultFuture;
423 public void onSuccess(final RpcResult<AddGroupOutput> rpcResult) {
424 if (rpcResult.isSuccessful() || rpcResult.getErrors().size() == 1
425 && rpcResult.getErrors().iterator().next().getMessage().contains(GROUP_EXISTS_IN_DEVICE_ERROR)) {
426 provider.getDevicesGroupRegistry().storeGroup(nodeId, groupId);
427 Futures.addCallback(provider.getSalFlowService().updateFlow(updateFlowInput),
428 new FutureCallback<RpcResult<UpdateFlowOutput>>() {
430 public void onSuccess(final RpcResult<UpdateFlowOutput> result) {
431 resultFuture.set(result);
435 public void onFailure(final Throwable failure) {
436 resultFuture.setException(failure);
438 }, MoreExecutors.directExecutor());
440 LOG.debug("Flow update with id {} finished without error for node {}",
441 getFlowId(updateFlowInput.getFlowRef()), nodeId);
443 LOG.error("Flow update with id {} failed for node {} with error {}",
444 getFlowId(updateFlowInput.getFlowRef()), nodeId, rpcResult.getErrors());
445 resultFuture.set(RpcResultBuilder.<UpdateFlowOutput>failed()
446 .withRpcErrors(rpcResult.getErrors()).build());
451 public void onFailure(final Throwable throwable) {
452 LOG.error("Service call for updating flow with id {} failed for node {}",
453 getFlowId(updateFlowInput.getFlowRef()), nodeId, throwable);
454 resultFuture.setException(throwable);