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
9 package org.opendaylight.openflowplugin.applications.frm.impl;
11 import static org.opendaylight.openflowplugin.applications.frm.util.FrmUtil.getNodeIdFromNodeIdentifier;
12 import static org.opendaylight.openflowplugin.applications.frm.util.FrmUtil.isGroupExistsOnDevice;
14 import com.google.common.base.Preconditions;
15 import com.google.common.util.concurrent.FutureCallback;
16 import com.google.common.util.concurrent.Futures;
17 import com.google.common.util.concurrent.ListenableFuture;
18 import com.google.common.util.concurrent.MoreExecutors;
19 import java.util.ArrayList;
20 import java.util.List;
21 import org.opendaylight.infrautils.utils.concurrent.LoggingFutures;
22 import org.opendaylight.openflowplugin.applications.frm.BundleMessagesCommiter;
23 import org.opendaylight.openflowplugin.applications.frm.ForwardingRulesManager;
24 import org.opendaylight.openflowplugin.applications.frm.NodeConfigurator;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.groups.Group;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeRef;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.onf.bundle.service.rev170124.AddBundleMessagesInput;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.onf.bundle.service.rev170124.AddBundleMessagesInputBuilder;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.onf.bundle.service.rev170124.AddBundleMessagesOutput;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.onf.bundle.service.rev170124.add.bundle.messages.input.MessagesBuilder;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.onf.bundle.service.rev170124.add.bundle.messages.input.messages.Message;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.onf.bundle.service.rev170124.add.bundle.messages.input.messages.MessageBuilder;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.onf.bundle.service.rev170124.bundle.inner.message.grouping.BundleInnerMessage;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.onf.bundle.service.rev170124.bundle.inner.message.grouping.bundle.inner.message.BundleAddGroupCaseBuilder;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.onf.bundle.service.rev170124.bundle.inner.message.grouping.bundle.inner.message.BundleRemoveGroupCaseBuilder;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.onf.bundle.service.rev170124.bundle.inner.message.grouping.bundle.inner.message.BundleUpdateGroupCaseBuilder;
40 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;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.onf.bundle.service.rev170124.bundle.inner.message.grouping.bundle.inner.message.bundle.remove.group._case.RemoveGroupCaseDataBuilder;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.onf.bundle.service.rev170124.bundle.inner.message.grouping.bundle.inner.message.bundle.update.group._case.UpdateGroupCaseDataBuilder;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.onf.rev170124.BundleFlags;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.onf.rev170124.BundleId;
45 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
46 import org.opendaylight.yangtools.yang.common.RpcResult;
47 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
48 import org.opendaylight.yangtools.yang.common.Uint32;
49 import org.slf4j.Logger;
50 import org.slf4j.LoggerFactory;
52 public class BundleGroupForwarder implements BundleMessagesCommiter<Group> {
54 private static final Logger LOG = LoggerFactory.getLogger(BundleGroupForwarder.class);
55 private static final BundleFlags BUNDLE_FLAGS = new BundleFlags(true, true);
56 private final ForwardingRulesManager forwardingRulesManager;
57 private final NodeConfigurator nodeConfigurator;
59 public BundleGroupForwarder(final ForwardingRulesManager forwardingRulesManager) {
60 this.forwardingRulesManager = Preconditions.checkNotNull(forwardingRulesManager,
61 "ForwardingRulesManager can not be null!");
62 this.nodeConfigurator = Preconditions.checkNotNull(forwardingRulesManager.getNodeConfigurator(),
63 "NodeConfigurator can not be null!");
67 public void remove(final InstanceIdentifier<Group> identifier, final Group group,
68 final InstanceIdentifier<FlowCapableNode> nodeIdent, final BundleId bundleId) {
69 final NodeId nodeId = getNodeIdFromNodeIdentifier(nodeIdent);
70 nodeConfigurator.enqueueJob(nodeId.getValue(), () -> {
71 final List<Message> messages = new ArrayList<>(1);
72 BundleInnerMessage bundleInnerMessage = new BundleRemoveGroupCaseBuilder()
73 .setRemoveGroupCaseData(new RemoveGroupCaseDataBuilder(group).build()).build();
74 Message message = new MessageBuilder().setNode(new NodeRef(nodeIdent.firstIdentifierOf(Node.class)))
75 .setBundleInnerMessage(bundleInnerMessage).build();
76 messages.add(message);
77 AddBundleMessagesInput addBundleMessagesInput = new AddBundleMessagesInputBuilder()
78 .setNode(new NodeRef(nodeIdent.firstIdentifierOf(Node.class))).setBundleId(bundleId)
79 .setFlags(BUNDLE_FLAGS).setMessages(new MessagesBuilder().setMessage(messages).build()).build();
80 LOG.trace("Pushing group remove message {} to bundle {} for device {}", addBundleMessagesInput,
81 bundleId.getValue(), nodeId.getValue());
82 final ListenableFuture<RpcResult<AddBundleMessagesOutput>> resultFuture = forwardingRulesManager
83 .getSalBundleService().addBundleMessages(addBundleMessagesInput);
84 Futures.addCallback(resultFuture, new BundleRemoveGroupCallBack(group.getGroupId().getValue(), nodeId),
85 MoreExecutors.directExecutor());
86 LoggingFutures.addErrorLogging(resultFuture, LOG, "removeBundleGroup");
93 public void update(final InstanceIdentifier<Group> identifier, final Group originalGroup, final Group updatedGroup,
94 final InstanceIdentifier<FlowCapableNode> nodeIdent, final BundleId bundleId) {
95 final NodeId nodeId = getNodeIdFromNodeIdentifier(nodeIdent);
96 nodeConfigurator.enqueueJob(nodeId.getValue(), () -> {
97 final List<Message> messages = new ArrayList<>(1);
98 BundleInnerMessage bundleInnerMessage = new BundleUpdateGroupCaseBuilder()
99 .setUpdateGroupCaseData(new UpdateGroupCaseDataBuilder(updatedGroup).build()).build();
100 Message message = new MessageBuilder().setNode(new NodeRef(nodeIdent.firstIdentifierOf(Node.class)))
101 .setBundleInnerMessage(bundleInnerMessage).build();
102 messages.add(message);
103 AddBundleMessagesInput addBundleMessagesInput = new AddBundleMessagesInputBuilder()
104 .setNode(new NodeRef(nodeIdent.firstIdentifierOf(Node.class))).setBundleId(bundleId)
105 .setFlags(BUNDLE_FLAGS).setMessages(new MessagesBuilder().setMessage(messages).build()).build();
106 LOG.trace("Pushing group update message {} to bundle {} for device {}", addBundleMessagesInput,
107 bundleId.getValue(), nodeId.getValue());
108 final ListenableFuture<RpcResult<AddBundleMessagesOutput>> resultFuture = forwardingRulesManager
109 .getSalBundleService().addBundleMessages(addBundleMessagesInput);
110 Futures.addCallback(resultFuture, new BundleUpdateGroupCallBack(originalGroup.getGroupId().getValue(),
111 nodeId), MoreExecutors.directExecutor());
112 LoggingFutures.addErrorLogging(resultFuture, LOG, "updateBundleGroup");
118 public ListenableFuture<RpcResult<AddBundleMessagesOutput>> add(final InstanceIdentifier<Group> identifier,
119 final Group group, final InstanceIdentifier<FlowCapableNode> nodeIdent, final BundleId bundleId) {
120 final NodeId nodeId = getNodeIdFromNodeIdentifier(nodeIdent);
121 final Uint32 groupId = group.getGroupId().getValue();
122 return nodeConfigurator.enqueueJob(nodeId.getValue(), () -> {
123 if (isGroupExistsOnDevice(nodeIdent, groupId, forwardingRulesManager)) {
124 LOG.debug("Group {} already exists in the device. Ignoring the add DTCN", groupId);
125 return Futures.immediateFuture(RpcResultBuilder.<AddBundleMessagesOutput>success().build());
127 final List<Message> messages = new ArrayList<>(1);
128 BundleInnerMessage bundleInnerMessage = new BundleAddGroupCaseBuilder()
129 .setAddGroupCaseData(new AddGroupCaseDataBuilder(group).build()).build();
130 Message message = new MessageBuilder().setNode(new NodeRef(nodeIdent.firstIdentifierOf(Node.class)))
131 .setBundleInnerMessage(bundleInnerMessage).build();
132 messages.add(message);
133 AddBundleMessagesInput addBundleMessagesInput = new AddBundleMessagesInputBuilder()
134 .setNode(new NodeRef(nodeIdent.firstIdentifierOf(Node.class))).setBundleId(bundleId)
135 .setFlags(BUNDLE_FLAGS).setMessages(new MessagesBuilder().setMessage(messages).build()).build();
136 LOG.trace("Pushing group add message {} to bundle {} for device {}", addBundleMessagesInput,
137 bundleId.getValue(), nodeId.getValue());
138 ListenableFuture<RpcResult<AddBundleMessagesOutput>> resultFuture = forwardingRulesManager
139 .getSalBundleService().addBundleMessages(addBundleMessagesInput);
140 Futures.addCallback(resultFuture, new BundleAddGroupCallBack(groupId, nodeId),
141 MoreExecutors.directExecutor());
146 private final class BundleAddGroupCallBack implements FutureCallback<RpcResult<AddBundleMessagesOutput>> {
147 private final Uint32 groupId;
148 private final NodeId nodeId;
150 private BundleAddGroupCallBack(final Uint32 groupId, final NodeId nodeId) {
151 this.groupId = groupId;
152 this.nodeId = nodeId;
156 public void onSuccess(RpcResult<AddBundleMessagesOutput> result) {
157 if (result.isSuccessful()) {
158 forwardingRulesManager.getDevicesGroupRegistry().storeGroup(nodeId, groupId);
159 LOG.debug("Group add with id {} finished without error for node {}", groupId, nodeId);
161 LOG.error("Group add with id {} failed for node {} with error: {}", groupId, nodeId,
162 result.getErrors().toString());
167 public void onFailure(Throwable throwable) {
168 LOG.error("Service call for adding group {} failed for node {} with error ", groupId, nodeId, throwable);
172 private final class BundleUpdateGroupCallBack implements FutureCallback<RpcResult<AddBundleMessagesOutput>> {
173 private final Uint32 groupId;
174 private final NodeId nodeId;
176 private BundleUpdateGroupCallBack(final Uint32 groupId, final NodeId nodeId) {
177 this.groupId = groupId;
178 this.nodeId = nodeId;
182 public void onSuccess(RpcResult<AddBundleMessagesOutput> result) {
183 if (result.isSuccessful()) {
184 forwardingRulesManager.getDevicesGroupRegistry().storeGroup(nodeId, groupId);
185 LOG.debug("Group update with id {} finished without error for node {}", groupId, nodeId);
187 LOG.error("Group update with id {} failed for node {} with error: {}", groupId, nodeId,
188 result.getErrors().toString());
193 public void onFailure(Throwable throwable) {
194 LOG.error("Service call for updating group {} failed for node {}", groupId, nodeId, throwable);
198 private final class BundleRemoveGroupCallBack implements FutureCallback<RpcResult<AddBundleMessagesOutput>> {
199 private final Uint32 groupId;
200 private final NodeId nodeId;
202 private BundleRemoveGroupCallBack(final Uint32 groupId, final NodeId nodeId) {
203 this.groupId = groupId;
204 this.nodeId = nodeId;
208 public void onSuccess(RpcResult<AddBundleMessagesOutput> result) {
209 if (result.isSuccessful()) {
210 LOG.debug("Group remove with id {} finished without error for node {}", groupId, nodeId);
211 forwardingRulesManager.getDevicesGroupRegistry().removeGroup(nodeId, groupId);
213 LOG.error("Group remove with id {} failed for node {} with error {}", groupId, nodeId,
214 result.getErrors().toString());
219 public void onFailure(Throwable throwable) {
220 LOG.error("Service call for removing group {} failed for node {} with error", groupId, nodeId, throwable);