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 org.opendaylight.openflowplugin.applications.frm.util.FrmUtil.getActiveBundle;
11 import static org.opendaylight.openflowplugin.applications.frm.util.FrmUtil.getNodeIdFromNodeIdentifier;
13 import com.google.common.util.concurrent.FutureCallback;
14 import com.google.common.util.concurrent.Futures;
15 import com.google.common.util.concurrent.ListenableFuture;
16 import com.google.common.util.concurrent.MoreExecutors;
17 import java.util.concurrent.Future;
18 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
19 import org.opendaylight.controller.md.sal.binding.api.DataTreeIdentifier;
20 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
21 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
22 import org.opendaylight.infrautils.utils.concurrent.JdkFutures;
23 import org.opendaylight.openflowplugin.applications.frm.ForwardingRulesManager;
24 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Uri;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.service.rev130918.AddGroupInput;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.service.rev130918.AddGroupInputBuilder;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.service.rev130918.AddGroupOutput;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.service.rev130918.RemoveGroupInputBuilder;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.service.rev130918.RemoveGroupOutput;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.service.rev130918.UpdateGroupInput;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.service.rev130918.UpdateGroupInputBuilder;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.service.rev130918.UpdateGroupOutput;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.service.rev130918.group.update.OriginalGroupBuilder;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.service.rev130918.group.update.UpdatedGroupBuilder;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.GroupId;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.GroupRef;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.groups.Group;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.groups.StaleGroup;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.groups.StaleGroupBuilder;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.groups.StaleGroupKey;
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;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.onf.rev170124.BundleId;
47 import org.opendaylight.yangtools.concepts.ListenerRegistration;
48 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
49 import org.opendaylight.yangtools.yang.common.RpcResult;
50 import org.slf4j.Logger;
51 import org.slf4j.LoggerFactory;
54 * GroupForwarder It implements
55 * {@link org.opendaylight.controller.md.sal.binding.api.DataTreeChangeListener}
56 * for WildCardedPath to {@link Group} and ForwardingRulesCommiter interface for
57 * methods: add, update and remove {@link Group} processing for
58 * {@link org.opendaylight.controller.md.sal.binding.api.DataTreeModification}.
60 public class GroupForwarder extends AbstractListeningCommiter<Group> {
62 private static final Logger LOG = LoggerFactory.getLogger(GroupForwarder.class);
63 private ListenerRegistration<GroupForwarder> listenerRegistration;
64 private final BundleGroupForwarder bundleGroupForwarder;
66 public GroupForwarder(final ForwardingRulesManager manager, final DataBroker db) {
68 this.bundleGroupForwarder = new BundleGroupForwarder(manager);
71 @SuppressWarnings("IllegalCatch")
73 public void registerListener() {
74 final DataTreeIdentifier<Group> treeId = new DataTreeIdentifier<>(LogicalDatastoreType.CONFIGURATION,
78 listenerRegistration = dataBroker.registerDataTreeChangeListener(treeId, GroupForwarder.this);
79 } catch (final Exception e) {
80 LOG.warn("FRM Group DataTreeChange listener registration fail!");
81 LOG.debug("FRM Group DataTreeChange listener registration fail ..", e);
82 throw new IllegalStateException("GroupForwarder startup fail! System needs restart.", e);
87 public void deregisterListener() {
93 if (listenerRegistration != null) {
94 listenerRegistration.close();
95 listenerRegistration = null;
100 protected InstanceIdentifier<Group> getWildCardPath() {
101 return InstanceIdentifier.create(Nodes.class).child(Node.class).augmentation(FlowCapableNode.class)
106 public void remove(final InstanceIdentifier<Group> identifier, final Group removeDataObj,
107 final InstanceIdentifier<FlowCapableNode> nodeIdent) {
108 BundleId bundleId = getActiveBundle(nodeIdent, provider);
109 if (bundleId != null) {
110 bundleGroupForwarder.remove(identifier, removeDataObj, nodeIdent, bundleId);
112 final Group group = removeDataObj;
113 final RemoveGroupInputBuilder builder = new RemoveGroupInputBuilder(group);
114 final NodeId nodeId = getNodeIdFromNodeIdentifier(nodeIdent);
116 builder.setNode(new NodeRef(nodeIdent.firstIdentifierOf(Node.class)));
117 builder.setGroupRef(new GroupRef(identifier));
118 builder.setTransactionUri(new Uri(provider.getNewTransactionId()));
120 final ListenableFuture<RpcResult<RemoveGroupOutput>> resultFuture =
121 this.provider.getSalGroupService().removeGroup(builder.build());
122 Futures.addCallback(resultFuture,
123 new RemoveGroupCallBack(removeDataObj.getGroupId().getValue(), nodeId),
124 MoreExecutors.directExecutor());
125 JdkFutures.addErrorLogging(resultFuture, LOG, "removeGroup");
129 // TODO: Pull this into ForwardingRulesCommiter and override it here
131 public Future<RpcResult<RemoveGroupOutput>> removeWithResult(final InstanceIdentifier<Group> identifier,
132 final Group removeDataObj, final InstanceIdentifier<FlowCapableNode> nodeIdent) {
134 final Group group = removeDataObj;
135 final RemoveGroupInputBuilder builder = new RemoveGroupInputBuilder(group);
137 builder.setNode(new NodeRef(nodeIdent.firstIdentifierOf(Node.class)));
138 builder.setGroupRef(new GroupRef(identifier));
139 builder.setTransactionUri(new Uri(provider.getNewTransactionId()));
140 return this.provider.getSalGroupService().removeGroup(builder.build());
144 public void update(final InstanceIdentifier<Group> identifier, final Group original, final Group update,
145 final InstanceIdentifier<FlowCapableNode> nodeIdent) {
146 BundleId bundleId = getActiveBundle(nodeIdent, provider);
147 if (bundleId != null) {
148 bundleGroupForwarder.update(identifier, original, update, nodeIdent, bundleId);
150 final NodeId nodeId = getNodeIdFromNodeIdentifier(nodeIdent);
151 nodeConfigurator.enqueueJob(nodeId.getValue(), () -> {
152 final Group originalGroup = original;
153 final Group updatedGroup = update;
154 final UpdateGroupInputBuilder builder = new UpdateGroupInputBuilder();
155 builder.setNode(new NodeRef(nodeIdent.firstIdentifierOf(Node.class)));
156 builder.setGroupRef(new GroupRef(identifier));
157 builder.setTransactionUri(new Uri(provider.getNewTransactionId()));
158 builder.setUpdatedGroup(new UpdatedGroupBuilder(updatedGroup).build());
159 builder.setOriginalGroup(new OriginalGroupBuilder(originalGroup).build());
160 UpdateGroupInput updateGroupInput = builder.build();
161 final ListenableFuture<RpcResult<UpdateGroupOutput>> resultFuture;
162 resultFuture = this.provider.getSalGroupService().updateGroup(updateGroupInput);
163 JdkFutures.addErrorLogging(resultFuture, LOG, "updateGroup");
164 Futures.addCallback(resultFuture,
165 new UpdateGroupCallBack(updateGroupInput.getOriginalGroup().getGroupId().getValue(), nodeId),
166 MoreExecutors.directExecutor());
173 public Future<? extends RpcResult<?>> add(final InstanceIdentifier<Group> identifier, final Group addDataObj,
174 final InstanceIdentifier<FlowCapableNode> nodeIdent) {
175 BundleId bundleId = getActiveBundle(nodeIdent, provider);
176 if (bundleId != null) {
177 return bundleGroupForwarder.add(identifier, addDataObj, nodeIdent, bundleId);
179 final NodeId nodeId = getNodeIdFromNodeIdentifier(nodeIdent);
180 return nodeConfigurator
181 .enqueueJob(nodeId.getValue(), () -> {
182 final Group group = addDataObj;
183 final AddGroupInputBuilder builder = new AddGroupInputBuilder(group);
184 builder.setNode(new NodeRef(nodeIdent.firstIdentifierOf(Node.class)));
185 builder.setGroupRef(new GroupRef(identifier));
186 builder.setTransactionUri(new Uri(provider.getNewTransactionId()));
187 AddGroupInput addGroupInput = builder.build();
188 final ListenableFuture<RpcResult<AddGroupOutput>> resultFuture;
189 resultFuture = this.provider.getSalGroupService().addGroup(addGroupInput);
190 Futures.addCallback(resultFuture,
191 new AddGroupCallBack(addGroupInput.getGroupId().getValue(), nodeId),
192 MoreExecutors.directExecutor());
199 public void createStaleMarkEntity(InstanceIdentifier<Group> identifier, Group del,
200 InstanceIdentifier<FlowCapableNode> nodeIdent) {
201 LOG.debug("Creating Stale-Mark entry for the switch {} for Group {} ", nodeIdent.toString(), del.toString());
202 StaleGroup staleGroup = makeStaleGroup(identifier, del, nodeIdent);
203 persistStaleGroup(staleGroup, nodeIdent);
207 private StaleGroup makeStaleGroup(InstanceIdentifier<Group> identifier, Group del,
208 InstanceIdentifier<FlowCapableNode> nodeIdent) {
209 StaleGroupBuilder staleGroupBuilder = new StaleGroupBuilder(del);
210 return staleGroupBuilder.setGroupId(del.getGroupId()).build();
213 private void persistStaleGroup(StaleGroup staleGroup, InstanceIdentifier<FlowCapableNode> nodeIdent) {
214 WriteTransaction writeTransaction = dataBroker.newWriteOnlyTransaction();
215 writeTransaction.put(LogicalDatastoreType.CONFIGURATION, getStaleGroupInstanceIdentifier(staleGroup, nodeIdent),
218 ListenableFuture<Void> submitFuture = writeTransaction.submit();
219 handleStaleGroupResultFuture(submitFuture);
222 private void handleStaleGroupResultFuture(ListenableFuture<Void> submitFuture) {
223 Futures.addCallback(submitFuture, new FutureCallback<Void>() {
225 public void onSuccess(Void result) {
226 LOG.debug("Stale Group creation success");
230 public void onFailure(Throwable throwable) {
231 LOG.error("Stale Group creation failed {}", throwable);
233 }, MoreExecutors.directExecutor());
237 private InstanceIdentifier<org.opendaylight.yang.gen.v1.urn.opendaylight.group
238 .types.rev131018.groups.StaleGroup> getStaleGroupInstanceIdentifier(
239 StaleGroup staleGroup, InstanceIdentifier<FlowCapableNode> nodeIdent) {
240 return nodeIdent.child(StaleGroup.class, new StaleGroupKey(new GroupId(staleGroup.getGroupId())));
243 private final class AddGroupCallBack implements FutureCallback<RpcResult<AddGroupOutput>> {
244 private final Long groupId;
245 private final NodeId nodeId;
247 private AddGroupCallBack(final Long groupId, final NodeId nodeId) {
248 this.groupId = groupId;
249 this.nodeId = nodeId;
253 public void onSuccess(RpcResult<AddGroupOutput> result) {
254 if (result.isSuccessful()) {
255 provider.getDevicesGroupRegistry().storeGroup(nodeId, groupId);
256 LOG.debug("Group add with id {} finished without error for node {}", groupId, nodeId);
258 LOG.debug("Group add with id {} failed for node {} with error {}", groupId, nodeId,
264 public void onFailure(Throwable throwable) {
265 LOG.error("Service call for adding group {} failed for node with error {}", groupId, nodeId, throwable);
269 private final class UpdateGroupCallBack implements FutureCallback<RpcResult<UpdateGroupOutput>> {
270 private final Long groupId;
271 private final NodeId nodeId;
273 private UpdateGroupCallBack(final Long groupId, final NodeId nodeId) {
274 this.groupId = groupId;
275 this.nodeId = nodeId;
279 public void onSuccess(RpcResult<UpdateGroupOutput> result) {
280 if (result.isSuccessful()) {
281 provider.getDevicesGroupRegistry().storeGroup(nodeId, groupId);
282 LOG.debug("Group update with id {} finished without error for node {}", groupId, nodeId);
284 LOG.debug("Group update with id {} failed for node {} with error {}", groupId, nodeId,
285 result.getErrors().toString());
290 public void onFailure(Throwable throwable) {
291 LOG.error("Service call for updating group {} failed for node {} with", groupId, nodeId,
296 private final class RemoveGroupCallBack implements FutureCallback<RpcResult<RemoveGroupOutput>> {
297 private final Long groupId;
298 private final NodeId nodeId;
300 private RemoveGroupCallBack(final Long groupId, final NodeId nodeId) {
301 this.groupId = groupId;
302 this.nodeId = nodeId;
306 public void onSuccess(RpcResult<RemoveGroupOutput> result) {
307 if (result.isSuccessful()) {
308 LOG.debug("Group remove with id {} finished without error for node {}", groupId, nodeId);
309 provider.getDevicesGroupRegistry().removeGroup(nodeId, groupId);
311 LOG.debug("Group remove with id {} failed for node {} with error {}", groupId, nodeId,
312 result.getErrors().toString());
317 public void onFailure(Throwable throwable) {
318 LOG.error("Service call for removing group {} failed for node with error {}", groupId, nodeId, throwable);