2 * Copyright (c) 2018 Ericsson India Global Services Pvt Ltd. 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.getFlowId;
12 import static org.opendaylight.openflowplugin.applications.frm.util.FrmUtil.getNodeIdFromNodeIdentifier;
13 import static org.opendaylight.openflowplugin.applications.frm.util.FrmUtil.getTableId;
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.FutureCallback;
19 import com.google.common.util.concurrent.Futures;
20 import com.google.common.util.concurrent.ListenableFuture;
21 import com.google.common.util.concurrent.MoreExecutors;
22 import com.google.common.util.concurrent.SettableFuture;
23 import java.util.ArrayList;
24 import java.util.Collections;
25 import java.util.List;
26 import java.util.Optional;
27 import java.util.concurrent.ExecutionException;
28 import org.opendaylight.infrautils.utils.concurrent.LoggingFutures;
29 import org.opendaylight.mdsal.binding.api.ReadTransaction;
30 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
31 import org.opendaylight.openflowplugin.applications.frm.BundleMessagesCommiter;
32 import org.opendaylight.openflowplugin.applications.frm.ForwardingRulesManager;
33 import org.opendaylight.openflowplugin.applications.frm.NodeConfigurator;
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.tables.table.Flow;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.FlowTableRef;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.FlowRef;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.service.rev130918.AddGroupInputBuilder;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.GroupRef;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.groups.Group;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeRef;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.onf.bundle.service.rev170124.AddBundleMessagesInput;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.onf.bundle.service.rev170124.AddBundleMessagesInputBuilder;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.onf.bundle.service.rev170124.AddBundleMessagesOutput;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.onf.bundle.service.rev170124.add.bundle.messages.input.MessagesBuilder;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.onf.bundle.service.rev170124.add.bundle.messages.input.messages.Message;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.onf.bundle.service.rev170124.add.bundle.messages.input.messages.MessageBuilder;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.onf.bundle.service.rev170124.bundle.inner.message.grouping.BundleInnerMessage;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.onf.bundle.service.rev170124.bundle.inner.message.grouping.bundle.inner.message.BundleAddFlowCaseBuilder;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.onf.bundle.service.rev170124.bundle.inner.message.grouping.bundle.inner.message.BundleAddGroupCaseBuilder;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.onf.bundle.service.rev170124.bundle.inner.message.grouping.bundle.inner.message.BundleRemoveFlowCaseBuilder;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.onf.bundle.service.rev170124.bundle.inner.message.grouping.bundle.inner.message.bundle.add.flow._case.AddFlowCaseDataBuilder;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.onf.bundle.service.rev170124.bundle.inner.message.grouping.bundle.inner.message.bundle.add.group._case.AddGroupCaseDataBuilder;
57 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.onf.bundle.service.rev170124.bundle.inner.message.grouping.bundle.inner.message.bundle.remove.flow._case.RemoveFlowCaseDataBuilder;
58 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.onf.rev170124.BundleFlags;
59 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.onf.rev170124.BundleId;
60 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
61 import org.opendaylight.yangtools.yang.common.RpcError;
62 import org.opendaylight.yangtools.yang.common.RpcResult;
63 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
64 import org.slf4j.Logger;
65 import org.slf4j.LoggerFactory;
67 public class BundleFlowForwarder implements BundleMessagesCommiter<Flow> {
69 private static final Logger LOG = LoggerFactory.getLogger(BundleFlowForwarder.class);
70 private static final BundleFlags BUNDLE_FLAGS = new BundleFlags(true, true);
71 private final ForwardingRulesManager forwardingRulesManager;
72 private final NodeConfigurator nodeConfigurator;
74 public BundleFlowForwarder(ForwardingRulesManager forwardingRulesManager) {
75 this.forwardingRulesManager = Preconditions.checkNotNull(forwardingRulesManager,
76 "ForwardingRulesManager can not be null!");
77 this.nodeConfigurator = Preconditions.checkNotNull(forwardingRulesManager.getNodeConfigurator(),
78 "NodeConfigurator can not be null!");
81 public void remove(final InstanceIdentifier<Flow> identifier, final Flow flow,
82 final InstanceIdentifier<FlowCapableNode> nodeIdent, final BundleId bundleId) {
83 final List<Message> messages = new ArrayList<>(1);
84 String node = nodeIdent.firstKeyOf(Node.class).getId().getValue();
85 BundleInnerMessage bundleInnerMessage = new BundleRemoveFlowCaseBuilder()
86 .setRemoveFlowCaseData(new RemoveFlowCaseDataBuilder(flow).build()).build();
87 Message message = new MessageBuilder().setNode(new NodeRef(nodeIdent.firstIdentifierOf(Node.class)))
88 .setBundleInnerMessage(bundleInnerMessage).build();
89 messages.add(message);
90 AddBundleMessagesInput addBundleMessagesInput = new AddBundleMessagesInputBuilder()
91 .setNode(new NodeRef(nodeIdent.firstIdentifierOf(Node.class))).setBundleId(bundleId)
92 .setFlags(BUNDLE_FLAGS).setMessages(new MessagesBuilder().setMessage(messages).build()).build();
93 final ListenableFuture<RpcResult<AddBundleMessagesOutput>> resultFuture = forwardingRulesManager
94 .getSalBundleService().addBundleMessages(addBundleMessagesInput);
95 LOG.trace("Pushing flow remove message {} to bundle {} for device {}", addBundleMessagesInput,
96 bundleId.getValue(), node);
97 LoggingFutures.addErrorLogging(resultFuture, LOG, "removeBundleFlow");
100 public void update(final InstanceIdentifier<Flow> identifier, final Flow originalFlow, final Flow updatedFlow,
101 final InstanceIdentifier<FlowCapableNode> nodeIdent, final BundleId bundleId) {
102 remove(identifier, originalFlow, nodeIdent, bundleId);
103 add(identifier, updatedFlow, nodeIdent, bundleId);
107 public ListenableFuture<RpcResult<AddBundleMessagesOutput>> add(final InstanceIdentifier<Flow> identifier,
109 final InstanceIdentifier<FlowCapableNode> nodeIdent, final BundleId bundleId) {
110 final NodeId nodeId = getNodeIdFromNodeIdentifier(nodeIdent);
111 return nodeConfigurator.enqueueJob(nodeId.getValue(), () -> {
112 BundleInnerMessage bundleInnerMessage = new BundleAddFlowCaseBuilder()
113 .setAddFlowCaseData(new AddFlowCaseDataBuilder(flow).build()).build();
114 Message message = new MessageBuilder().setNode(new NodeRef(nodeIdent.firstIdentifierOf(Node.class)))
115 .setBundleInnerMessage(bundleInnerMessage).build();
116 ListenableFuture<RpcResult<AddBundleMessagesOutput>> groupFuture = pushDependentGroup(nodeIdent, flow,
117 identifier, bundleId);
118 SettableFuture<RpcResult<AddBundleMessagesOutput>> resultFuture = SettableFuture.create();
119 Futures.addCallback(groupFuture, new BundleFlowCallBack(nodeIdent, bundleId, message, identifier,
120 resultFuture), MoreExecutors.directExecutor());
125 private ListenableFuture<RpcResult<AddBundleMessagesOutput>> pushDependentGroup(
126 final InstanceIdentifier<FlowCapableNode> nodeIdent, Flow updatedFlow, InstanceIdentifier<Flow> identifier,
128 //TODO This read to the DS might have a performance impact.
129 //if the dependent group is not installed than we should just cache the parent group,
130 //till we receive the dependent group DTCN and then push it.
131 Long groupId = isFlowDependentOnGroup(updatedFlow);
132 ListenableFuture<RpcResult<AddBundleMessagesOutput>> resultFuture;
133 if (groupId != null) {
134 LOG.trace("The flow {} is dependent on group {}. Checking if the group is already present",
135 getFlowId(new FlowRef(identifier)), groupId);
136 if (isGroupExistsOnDevice(nodeIdent, groupId, forwardingRulesManager)) {
137 LOG.trace("The dependent group {} is already programmed. Updating the flow {}", groupId,
138 getFlowId(new FlowRef(identifier)));
139 resultFuture = Futures.immediateFuture(RpcResultBuilder.<AddBundleMessagesOutput>success().build());
141 LOG.trace("The dependent group {} isn't programmed yet. Pushing the group", groupId);
142 InstanceIdentifier<Group> groupIdent = buildGroupInstanceIdentifier(nodeIdent, groupId);
143 LOG.info("Reading the group from config inventory: {}", groupId);
144 try (ReadTransaction readTransaction = forwardingRulesManager.getReadTransaction()) {
145 Optional<Group> group = readTransaction.read(LogicalDatastoreType.CONFIGURATION, groupIdent).get();
146 if (group.isPresent()) {
147 final AddGroupInputBuilder builder = new AddGroupInputBuilder(group.get());
148 builder.setNode(new NodeRef(nodeIdent.firstIdentifierOf(Node.class)));
149 builder.setGroupRef(new GroupRef(nodeIdent));
150 builder.setTransactionUri(new Uri(forwardingRulesManager.getNewTransactionId()));
151 BundleInnerMessage bundleInnerMessage = new BundleAddGroupCaseBuilder()
152 .setAddGroupCaseData(new AddGroupCaseDataBuilder(group.get()).build()).build();
153 Message groupMessage = new MessageBuilder().setNode(
154 new NodeRef(nodeIdent.firstIdentifierOf(Node.class)))
155 .setBundleInnerMessage(bundleInnerMessage).build();
156 final List<Message> messages = new ArrayList<>(1);
157 messages.add(groupMessage);
158 AddBundleMessagesInput addBundleMessagesInput = new AddBundleMessagesInputBuilder()
159 .setNode(new NodeRef(nodeIdent.firstIdentifierOf(Node.class))).setBundleId(bundleId)
160 .setFlags(BUNDLE_FLAGS).setMessages(new MessagesBuilder().setMessage(messages).build())
162 LOG.trace("Pushing flow update message {} to bundle {} for device {}", addBundleMessagesInput,
163 bundleId.getValue(), getNodeIdFromNodeIdentifier(nodeIdent));
164 resultFuture = forwardingRulesManager
165 .getSalBundleService().addBundleMessages(addBundleMessagesInput);
166 Futures.transformAsync(resultFuture, rpcResult -> {
167 if (rpcResult.isSuccessful()) {
168 forwardingRulesManager.getDevicesGroupRegistry()
169 .storeGroup(getNodeIdFromNodeIdentifier(nodeIdent), groupId);
170 LOG.trace("Group {} stored in cache", groupId);
172 return Futures.immediateFuture(null);
173 }, MoreExecutors.directExecutor());
175 LOG.debug("Group {} not present in the config inventory", groupId);
176 resultFuture = Futures.immediateFuture(RpcResultBuilder.<AddBundleMessagesOutput>failed()
177 .withError(RpcError.ErrorType.APPLICATION,
178 "Group " + groupId + " not present in the config inventory").build());
180 } catch (InterruptedException | ExecutionException e) {
181 LOG.error("Error while reading group from config datastore for the group ID {}", groupId, e);
182 resultFuture = Futures.immediateFuture(RpcResultBuilder.<AddBundleMessagesOutput>failed()
183 .withError(RpcError.ErrorType.APPLICATION,
184 "Group " + groupId + " not present in the config inventory").build());
188 resultFuture = Futures.immediateFuture(RpcResultBuilder.<AddBundleMessagesOutput>success().build());
193 private final class BundleFlowCallBack implements FutureCallback<RpcResult<AddBundleMessagesOutput>> {
194 private final InstanceIdentifier<FlowCapableNode> nodeIdent;
195 private final BundleId bundleId;
196 private final Message messages;
197 private final NodeId nodeId;
198 private final String flowId;
199 private final short tableId;
200 private final SettableFuture<RpcResult<AddBundleMessagesOutput>> resultFuture;
202 BundleFlowCallBack(InstanceIdentifier<FlowCapableNode> nodeIdent, BundleId bundleId, Message messages,
203 InstanceIdentifier<Flow> identifier , SettableFuture<RpcResult<AddBundleMessagesOutput>> resultFuture) {
204 this.nodeIdent = nodeIdent;
205 this.bundleId = bundleId;
206 this.messages = messages;
207 this.resultFuture = resultFuture;
208 this.flowId = getFlowId(new FlowRef(identifier));
209 this.tableId = getTableId(new FlowTableRef(identifier));
210 nodeId = getNodeIdFromNodeIdentifier(nodeIdent);
214 public void onSuccess(RpcResult<AddBundleMessagesOutput> rpcResult) {
215 if (rpcResult.isSuccessful()) {
216 AddBundleMessagesInput addBundleMessagesInput = new AddBundleMessagesInputBuilder()
217 .setNode(new NodeRef(nodeIdent.firstIdentifierOf(Node.class))).setBundleId(bundleId)
218 .setFlags(BUNDLE_FLAGS).setMessages(new MessagesBuilder().setMessage(
219 Collections.singletonList(messages)).build()).build();
221 LOG.trace("Pushing flow add message {} to bundle {} for device {}", addBundleMessagesInput,
222 bundleId.getValue(), nodeId.getValue());
224 final ListenableFuture<RpcResult<AddBundleMessagesOutput>> addFuture =
225 forwardingRulesManager.getSalBundleService().addBundleMessages(addBundleMessagesInput);
226 Futures.addCallback(addFuture, new FutureCallback<RpcResult<AddBundleMessagesOutput>>() {
228 public void onSuccess(RpcResult<AddBundleMessagesOutput> result) {
229 resultFuture.set(result);
230 if (!result.getErrors().isEmpty()) {
231 LOG.error("Flow add with flowId {} and tableId {} failed for node {} with error: {}",
232 flowId, tableId, nodeId.getValue(), result.getErrors().toString());
238 public void onFailure(Throwable failure) {
239 resultFuture.setException(failure);
241 }, MoreExecutors.directExecutor());
243 LOG.error("Error {} while pushing flow add bundle {} for device {}", rpcResult.getErrors(), messages,
245 resultFuture.set(rpcResult);
250 public void onFailure(Throwable throwable) {
251 LOG.error("Error while pushing flow add bundle {} for device {}", messages, nodeId.getValue());
252 resultFuture.setException(throwable);