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.isFlowDependentOnGroup;
14 import static org.opendaylight.openflowplugin.applications.frm.util.FrmUtil.isGroupExistsOnDevice;
16 import com.google.common.base.Preconditions;
17 import com.google.common.util.concurrent.FutureCallback;
18 import com.google.common.util.concurrent.Futures;
19 import com.google.common.util.concurrent.ListenableFuture;
20 import com.google.common.util.concurrent.MoreExecutors;
21 import com.google.common.util.concurrent.SettableFuture;
22 import java.util.ArrayList;
23 import java.util.Collections;
24 import java.util.List;
25 import java.util.Optional;
26 import java.util.concurrent.ExecutionException;
27 import java.util.concurrent.Future;
28 import org.opendaylight.infrautils.utils.concurrent.JdkFutures;
29 import org.opendaylight.mdsal.binding.api.ReadTransaction;
30 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
31 import org.opendaylight.openflowplugin.applications.frm.ForwardingRulesManager;
32 import org.opendaylight.openflowplugin.applications.frm.NodeConfigurator;
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.tables.table.Flow;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.FlowRef;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.service.rev130918.AddGroupInputBuilder;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.GroupRef;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.groups.Group;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeRef;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.onf.bundle.service.rev170124.AddBundleMessagesInput;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.onf.bundle.service.rev170124.AddBundleMessagesInputBuilder;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.onf.bundle.service.rev170124.AddBundleMessagesOutput;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.onf.bundle.service.rev170124.add.bundle.messages.input.MessagesBuilder;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.onf.bundle.service.rev170124.add.bundle.messages.input.messages.Message;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.onf.bundle.service.rev170124.add.bundle.messages.input.messages.MessageBuilder;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.onf.bundle.service.rev170124.bundle.inner.message.grouping.BundleInnerMessage;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.onf.bundle.service.rev170124.bundle.inner.message.grouping.bundle.inner.message.BundleAddFlowCaseBuilder;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.onf.bundle.service.rev170124.bundle.inner.message.grouping.bundle.inner.message.BundleAddGroupCaseBuilder;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.onf.bundle.service.rev170124.bundle.inner.message.grouping.bundle.inner.message.BundleRemoveFlowCaseBuilder;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.onf.bundle.service.rev170124.bundle.inner.message.grouping.bundle.inner.message.BundleUpdateFlowCaseBuilder;
54 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;
55 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;
56 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;
57 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.onf.bundle.service.rev170124.bundle.inner.message.grouping.bundle.inner.message.bundle.update.flow._case.UpdateFlowCaseDataBuilder;
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.RpcResult;
62 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
63 import org.slf4j.Logger;
64 import org.slf4j.LoggerFactory;
66 public class BundleFlowForwarder {
68 private static final Logger LOG = LoggerFactory.getLogger(BundleFlowForwarder.class);
69 private static final BundleFlags BUNDLE_FLAGS = new BundleFlags(true, true);
70 private final ForwardingRulesManager forwardingRulesManager;
71 private final NodeConfigurator nodeConfigurator;
73 public BundleFlowForwarder(ForwardingRulesManager forwardingRulesManager) {
74 this.forwardingRulesManager = Preconditions.checkNotNull(forwardingRulesManager,
75 "ForwardingRulesManager can not be null!");
76 this.nodeConfigurator = Preconditions.checkNotNull(forwardingRulesManager.getNodeConfigurator(),
77 "NodeConfigurator can not be null!");
80 public void remove(final InstanceIdentifier<Flow> identifier, final Flow flow,
81 final InstanceIdentifier<FlowCapableNode> nodeIdent, final BundleId bundleId) {
82 final List<Message> messages = new ArrayList<>(1);
83 String node = nodeIdent.firstKeyOf(Node.class).getId().getValue();
84 BundleInnerMessage bundleInnerMessage = new BundleRemoveFlowCaseBuilder()
85 .setRemoveFlowCaseData(new RemoveFlowCaseDataBuilder(flow).build()).build();
86 Message message = new MessageBuilder().setNode(new NodeRef(nodeIdent.firstIdentifierOf(Node.class)))
87 .setBundleInnerMessage(bundleInnerMessage).build();
88 messages.add(message);
89 AddBundleMessagesInput addBundleMessagesInput = new AddBundleMessagesInputBuilder()
90 .setNode(new NodeRef(nodeIdent.firstIdentifierOf(Node.class))).setBundleId(bundleId)
91 .setFlags(BUNDLE_FLAGS).setMessages(new MessagesBuilder().setMessage(messages).build()).build();
92 final ListenableFuture<RpcResult<AddBundleMessagesOutput>> resultFuture = forwardingRulesManager
93 .getSalBundleService().addBundleMessages(addBundleMessagesInput);
94 LOG.trace("Pushing flow remove message {} to bundle {} for device {}", addBundleMessagesInput,
95 bundleId.getValue(), node);
96 JdkFutures.addErrorLogging(resultFuture, LOG, "removeBundleFlow");
99 public void update(final InstanceIdentifier<Flow> identifier, final Flow originalFlow, final Flow updatedFlow,
100 final InstanceIdentifier<FlowCapableNode> nodeIdent, final BundleId bundleId) {
101 final NodeId nodeId = getNodeIdFromNodeIdentifier(nodeIdent);
102 nodeConfigurator.enqueueJob(nodeId.getValue(), () -> {
103 BundleInnerMessage bundleInnerMessage = new BundleUpdateFlowCaseBuilder()
104 .setUpdateFlowCaseData(new UpdateFlowCaseDataBuilder(updatedFlow).build()).build();
105 Message message = new MessageBuilder().setNode(new NodeRef(nodeIdent.firstIdentifierOf(Node.class)))
106 .setBundleInnerMessage(bundleInnerMessage).build();
107 ListenableFuture<RpcResult<AddBundleMessagesOutput>> groupFuture = pushDependentGroup(nodeIdent,
108 updatedFlow, identifier, bundleId);
109 SettableFuture<RpcResult<AddBundleMessagesOutput>> resultFuture = SettableFuture.create();
110 Futures.addCallback(groupFuture, new BundleFlowCallBack(nodeIdent, bundleId, message, resultFuture),
111 MoreExecutors.directExecutor());
116 public Future<? extends RpcResult<?>> add(final InstanceIdentifier<Flow> identifier, final Flow flow,
117 final InstanceIdentifier<FlowCapableNode> nodeIdent, final BundleId bundleId) {
118 final NodeId nodeId = getNodeIdFromNodeIdentifier(nodeIdent);
119 return nodeConfigurator.enqueueJob(nodeId.getValue(), () -> {
120 BundleInnerMessage bundleInnerMessage = new BundleAddFlowCaseBuilder()
121 .setAddFlowCaseData(new AddFlowCaseDataBuilder(flow).build()).build();
122 Message message = new MessageBuilder().setNode(new NodeRef(nodeIdent.firstIdentifierOf(Node.class)))
123 .setBundleInnerMessage(bundleInnerMessage).build();
124 ListenableFuture<RpcResult<AddBundleMessagesOutput>> groupFuture = pushDependentGroup(nodeIdent, flow,
125 identifier, bundleId);
126 SettableFuture<RpcResult<AddBundleMessagesOutput>> resultFuture = SettableFuture.create();
127 Futures.addCallback(groupFuture, new BundleFlowCallBack(nodeIdent, bundleId, message, resultFuture),
128 MoreExecutors.directExecutor());
133 private ListenableFuture<RpcResult<AddBundleMessagesOutput>> pushDependentGroup(
134 final InstanceIdentifier<FlowCapableNode> nodeIdent, Flow updatedFlow, InstanceIdentifier<Flow> identifier,
136 //TODO This read to the DS might have a performance impact.
137 //if the dependent group is not installed than we should just cache the parent group,
138 //till we receive the dependent group DTCN and then push it.
139 Long groupId = isFlowDependentOnGroup(updatedFlow);
140 ListenableFuture<RpcResult<AddBundleMessagesOutput>> resultFuture;
141 if (groupId != null) {
142 LOG.trace("The flow {} is dependent on group {}. Checking if the group is already present",
143 getFlowId(new FlowRef(identifier)), groupId);
144 if (isGroupExistsOnDevice(nodeIdent, groupId, forwardingRulesManager)) {
145 LOG.trace("The dependent group {} is already programmed. Updating the flow {}", groupId,
146 getFlowId(new FlowRef(identifier)));
147 resultFuture = Futures.immediateFuture(RpcResultBuilder.<AddBundleMessagesOutput>success().build());
149 LOG.trace("The dependent group {} isn't programmed yet. Pushing the group", groupId);
150 InstanceIdentifier<Group> groupIdent = buildGroupInstanceIdentifier(nodeIdent, groupId);
151 LOG.info("Reading the group from config inventory: {}", groupId);
152 try (ReadTransaction readTransaction = forwardingRulesManager.getReadTransaction()) {
153 Optional<Group> group = readTransaction.read(LogicalDatastoreType.CONFIGURATION, groupIdent).get();
154 if (group.isPresent()) {
155 final AddGroupInputBuilder builder = new AddGroupInputBuilder(group.get());
156 builder.setNode(new NodeRef(nodeIdent.firstIdentifierOf(Node.class)));
157 builder.setGroupRef(new GroupRef(nodeIdent));
158 builder.setTransactionUri(new Uri(forwardingRulesManager.getNewTransactionId()));
159 BundleInnerMessage bundleInnerMessage = new BundleAddGroupCaseBuilder()
160 .setAddGroupCaseData(new AddGroupCaseDataBuilder(group.get()).build()).build();
161 Message groupMessage = new MessageBuilder().setNode(
162 new NodeRef(nodeIdent.firstIdentifierOf(Node.class)))
163 .setBundleInnerMessage(bundleInnerMessage).build();
164 final List<Message> messages = new ArrayList<>(1);
165 messages.add(groupMessage);
166 AddBundleMessagesInput addBundleMessagesInput = new AddBundleMessagesInputBuilder()
167 .setNode(new NodeRef(nodeIdent.firstIdentifierOf(Node.class))).setBundleId(bundleId)
168 .setFlags(BUNDLE_FLAGS).setMessages(new MessagesBuilder().setMessage(messages).build())
170 LOG.trace("Pushing flow update message {} to bundle {} for device {}", addBundleMessagesInput,
171 bundleId.getValue(), getNodeIdFromNodeIdentifier(nodeIdent));
172 resultFuture = forwardingRulesManager
173 .getSalBundleService().addBundleMessages(addBundleMessagesInput);
174 Futures.transformAsync(resultFuture, rpcResult -> {
175 if (rpcResult.isSuccessful()) {
176 forwardingRulesManager.getDevicesGroupRegistry()
177 .storeGroup(getNodeIdFromNodeIdentifier(nodeIdent), groupId);
178 LOG.trace("Group {} stored in cache", groupId);
180 return Futures.immediateFuture(null);
181 }, MoreExecutors.directExecutor());
183 LOG.debug("Group {} not present in the config inventory", groupId);
184 resultFuture = Futures.immediateFuture(RpcResultBuilder.<AddBundleMessagesOutput>success()
187 } catch (InterruptedException | ExecutionException e) {
188 LOG.error("Error while reading group from config datastore for the group ID {}", groupId, e);
189 resultFuture = Futures.immediateFuture(RpcResultBuilder.<AddBundleMessagesOutput>success().build());
193 resultFuture = Futures.immediateFuture(RpcResultBuilder.<AddBundleMessagesOutput>success().build());
198 private final class BundleFlowCallBack implements FutureCallback<RpcResult<AddBundleMessagesOutput>> {
199 private final InstanceIdentifier<FlowCapableNode> nodeIdent;
200 private final BundleId bundleId;
201 private final Message message;
202 private final NodeId nodeId;
203 private final SettableFuture<RpcResult<AddBundleMessagesOutput>> resultFuture;
205 BundleFlowCallBack(InstanceIdentifier<FlowCapableNode> nodeIdent, BundleId bundleId, Message message,
206 SettableFuture<RpcResult<AddBundleMessagesOutput>> resultFuture) {
207 this.nodeIdent = nodeIdent;
208 this.bundleId = bundleId;
209 this.message = message;
210 this.resultFuture = resultFuture;
211 nodeId = getNodeIdFromNodeIdentifier(nodeIdent);
215 public void onSuccess(RpcResult<AddBundleMessagesOutput> rpcResult) {
216 if (rpcResult.isSuccessful()) {
217 AddBundleMessagesInput addBundleMessagesInput = new AddBundleMessagesInputBuilder()
218 .setNode(new NodeRef(nodeIdent.firstIdentifierOf(Node.class))).setBundleId(bundleId)
219 .setFlags(BUNDLE_FLAGS).setMessages(new MessagesBuilder().setMessage(
220 Collections.singletonList(message)).build()).build();
222 LOG.trace("Pushing flow add message {} to bundle {} for device {}", addBundleMessagesInput,
223 bundleId.getValue(), nodeId.getValue());
225 final ListenableFuture<RpcResult<AddBundleMessagesOutput>> addFuture =
226 forwardingRulesManager.getSalBundleService().addBundleMessages(addBundleMessagesInput);
227 Futures.addCallback(addFuture, new FutureCallback<RpcResult<AddBundleMessagesOutput>>() {
229 public void onSuccess(RpcResult<AddBundleMessagesOutput> result) {
230 resultFuture.set(result);
234 public void onFailure(Throwable failure) {
235 resultFuture.setException(failure);
237 }, MoreExecutors.directExecutor());
239 resultFuture.set(rpcResult);
244 public void onFailure(Throwable throwable) {
245 LOG.error("Error while pushing flow add bundle {} for device {}", message, nodeId);
246 resultFuture.setException(throwable);