OPNFLWPLUG-1032: Neon-MRI: Bump odlparent, yangtools, mdsal
[openflowplugin.git] / applications / forwardingrules-manager / src / main / java / org / opendaylight / openflowplugin / applications / frm / impl / BundleFlowForwarder.java
1 /*
2  * Copyright (c) 2018 Ericsson India Global Services Pvt Ltd. 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
9 package org.opendaylight.openflowplugin.applications.frm.impl;
10
11 import static org.opendaylight.openflowplugin.applications.frm.util.FrmUtil.buildGroupInstanceIdentifier;
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;
16
17 import com.google.common.base.Optional;
18 import com.google.common.base.Preconditions;
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.ArrayList;
25 import java.util.List;
26 import java.util.concurrent.ExecutionException;
27 import java.util.concurrent.Future;
28 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
29 import org.opendaylight.infrautils.utils.concurrent.JdkFutures;
30 import org.opendaylight.openflowplugin.applications.frm.ForwardingRulesManager;
31 import org.opendaylight.openflowplugin.applications.frm.NodeConfigurator;
32 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Uri;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.Flow;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.AddFlowOutput;
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;
65
66 public class BundleFlowForwarder {
67
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;
72
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!");
78     }
79
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");
97     }
98
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             ListenableFuture<RpcResult<AddFlowOutput>> flowFuture = SettableFuture.create();
110             Futures.addCallback(groupFuture, new BundleFlowCallBack(nodeIdent, bundleId, message, flowFuture));
111             return flowFuture;
112         });
113     }
114
115     public Future<? extends RpcResult<?>> add(final InstanceIdentifier<Flow> identifier, final Flow flow,
116             final InstanceIdentifier<FlowCapableNode> nodeIdent, final BundleId bundleId) {
117         final NodeId nodeId = getNodeIdFromNodeIdentifier(nodeIdent);
118         return nodeConfigurator.enqueueJob(nodeId.getValue(), () -> {
119             BundleInnerMessage bundleInnerMessage = new BundleAddFlowCaseBuilder()
120                     .setAddFlowCaseData(new AddFlowCaseDataBuilder(flow).build()).build();
121             Message message = new MessageBuilder().setNode(new NodeRef(nodeIdent.firstIdentifierOf(Node.class)))
122                     .setBundleInnerMessage(bundleInnerMessage).build();
123             ListenableFuture<RpcResult<AddBundleMessagesOutput>> groupFuture = pushDependentGroup(nodeIdent, flow,
124                     identifier, bundleId);
125             ListenableFuture<RpcResult<AddFlowOutput>> flowFuture = SettableFuture.create();
126             Futures.addCallback(groupFuture, new BundleFlowCallBack(nodeIdent, bundleId, message, flowFuture),
127                     MoreExecutors.directExecutor());
128             return flowFuture;
129         });
130     }
131
132     private ListenableFuture<RpcResult<AddBundleMessagesOutput>> pushDependentGroup(
133             final InstanceIdentifier<FlowCapableNode> nodeIdent, Flow updatedFlow, InstanceIdentifier<Flow> identifier,
134             BundleId bundleId) {
135         //TODO This read to the DS might have a performance impact.
136         //if the dependent group is not installed than we should just cache the parent group,
137         //till we receive the dependent group DTCN and then push it.
138         Long groupId = isFlowDependentOnGroup(updatedFlow);
139         ListenableFuture<RpcResult<AddBundleMessagesOutput>> resultFuture;
140         if (groupId != null) {
141             LOG.trace("The flow {} is dependent on group {}. Checking if the group is already present",
142                     getFlowId(new FlowRef(identifier)), groupId);
143             if (isGroupExistsOnDevice(nodeIdent, groupId, forwardingRulesManager)) {
144                 LOG.trace("The dependent group {} is already programmed. Updating the flow {}", groupId,
145                         getFlowId(new FlowRef(identifier)));
146                 resultFuture = Futures.immediateFuture(RpcResultBuilder.<AddBundleMessagesOutput>success().build());
147             } else {
148                 LOG.trace("The dependent group {} isn't programmed yet. Pushing the group", groupId);
149                 InstanceIdentifier<Group> groupIdent = buildGroupInstanceIdentifier(nodeIdent, groupId);
150                 LOG.info("Reading the group from config inventory: {}", groupId);
151                 try {
152                     Optional<Group> group = forwardingRulesManager.getReadTranaction()
153                             .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())
169                                 .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);
179                             }
180                             return Futures.immediateFuture(null);
181                         }, MoreExecutors.directExecutor());
182                     } else {
183                         LOG.debug("Group {} not present in the config inventory", groupId);
184                         resultFuture = Futures.immediateFuture(RpcResultBuilder.<AddBundleMessagesOutput>success()
185                                 .build());
186                     }
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());
190                 }
191             }
192         } else {
193             resultFuture = Futures.immediateFuture(RpcResultBuilder.<AddBundleMessagesOutput>success().build());
194         }
195         return resultFuture;
196     }
197
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 ListenableFuture<RpcResult<AddBundleMessagesOutput>> flowFuture;
204
205         BundleFlowCallBack(InstanceIdentifier<FlowCapableNode> nodeIdent, BundleId bundleId, Message message,
206                 ListenableFuture<RpcResult<AddFlowOutput>> flowFuture) {
207             this.nodeIdent = nodeIdent;
208             this.bundleId = bundleId;
209             this.message = message;
210             nodeId = getNodeIdFromNodeIdentifier(nodeIdent);
211         }
212
213         @Override
214         public void onSuccess(RpcResult<AddBundleMessagesOutput> rpcResult) {
215             if (rpcResult.isSuccessful()) {
216                 List<Message> messages = new ArrayList<>(1);
217                 messages.add(message);
218                 AddBundleMessagesInput addBundleMessagesInput = new AddBundleMessagesInputBuilder()
219                         .setNode(new NodeRef(nodeIdent.firstIdentifierOf(Node.class))).setBundleId(bundleId)
220                         .setFlags(BUNDLE_FLAGS).setMessages(new MessagesBuilder().setMessage(messages).build()).build();
221                 LOG.trace("Pushing flow add message {} to bundle {} for device {}", addBundleMessagesInput,
222                         bundleId.getValue(), nodeId.getValue());
223                 flowFuture = forwardingRulesManager.getSalBundleService().addBundleMessages(addBundleMessagesInput);
224             } else {
225                 flowFuture = Futures.immediateFuture(null);
226             }
227         }
228
229         @Override
230         public void onFailure(Throwable throwable) {
231             flowFuture = Futures.immediateFailedFuture(null);
232             LOG.error("Error while pushing flow add bundle {} for device {}", message, nodeId);
233         }
234     }
235 }