re-activate FindBugs
[openflowplugin.git] / applications / forwardingrules-manager / src / main / java / org / opendaylight / openflowplugin / applications / frm / impl / GroupForwarder.java
1 /**
2  * Copyright (c) 2014, 2017 Cisco Systems, Inc. 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 package org.opendaylight.openflowplugin.applications.frm.impl;
9
10 import static org.opendaylight.openflowplugin.applications.frm.util.FrmUtil.getActiveBundle;
11 import static org.opendaylight.openflowplugin.applications.frm.util.FrmUtil.getNodeIdFromNodeIdentifier;
12
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;
52
53 /**
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}.
59  */
60 public class GroupForwarder extends AbstractListeningCommiter<Group> {
61
62     private static final Logger LOG = LoggerFactory.getLogger(GroupForwarder.class);
63     private ListenerRegistration<GroupForwarder> listenerRegistration;
64     private final BundleGroupForwarder bundleGroupForwarder;
65
66     public GroupForwarder(final ForwardingRulesManager manager, final DataBroker db) {
67         super(manager, db);
68         this.bundleGroupForwarder = new BundleGroupForwarder(manager);
69     }
70
71     @SuppressWarnings("IllegalCatch")
72     @Override
73     public void registerListener() {
74         final DataTreeIdentifier<Group> treeId = new DataTreeIdentifier<>(LogicalDatastoreType.CONFIGURATION,
75                 getWildCardPath());
76
77         try {
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);
83         }
84     }
85
86     @Override
87     public  void deregisterListener() {
88         close();
89     }
90
91     @Override
92     public void close() {
93         if (listenerRegistration != null) {
94             listenerRegistration.close();
95             listenerRegistration = null;
96         }
97     }
98
99     @Override
100     protected InstanceIdentifier<Group> getWildCardPath() {
101         return InstanceIdentifier.create(Nodes.class).child(Node.class).augmentation(FlowCapableNode.class)
102                 .child(Group.class);
103     }
104
105     @Override
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);
111         } else {
112             final Group group = removeDataObj;
113             final RemoveGroupInputBuilder builder = new RemoveGroupInputBuilder(group);
114             final NodeId nodeId = getNodeIdFromNodeIdentifier(nodeIdent);
115
116             builder.setNode(new NodeRef(nodeIdent.firstIdentifierOf(Node.class)));
117             builder.setGroupRef(new GroupRef(identifier));
118             builder.setTransactionUri(new Uri(provider.getNewTransactionId()));
119
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");
126         }
127     }
128
129     // TODO: Pull this into ForwardingRulesCommiter and override it here
130     @Override
131     public Future<RpcResult<RemoveGroupOutput>> removeWithResult(final InstanceIdentifier<Group> identifier,
132             final Group removeDataObj, final InstanceIdentifier<FlowCapableNode> nodeIdent) {
133
134         final Group group = removeDataObj;
135         final RemoveGroupInputBuilder builder = new RemoveGroupInputBuilder(group);
136
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());
141     }
142
143     @Override
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);
149         } else {
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());
167                 return resultFuture;
168             });
169         }
170     }
171
172     @Override
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);
178         } else {
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());
193                         return resultFuture;
194                     });
195         }
196     }
197
198     @Override
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);
204
205     }
206
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();
211     }
212
213     private void persistStaleGroup(StaleGroup staleGroup, InstanceIdentifier<FlowCapableNode> nodeIdent) {
214         WriteTransaction writeTransaction = dataBroker.newWriteOnlyTransaction();
215         writeTransaction.put(LogicalDatastoreType.CONFIGURATION, getStaleGroupInstanceIdentifier(staleGroup, nodeIdent),
216                 staleGroup, false);
217
218         ListenableFuture<Void> submitFuture = writeTransaction.submit();
219         handleStaleGroupResultFuture(submitFuture);
220     }
221
222     private void handleStaleGroupResultFuture(ListenableFuture<Void> submitFuture) {
223         Futures.addCallback(submitFuture, new FutureCallback<Void>() {
224             @Override
225             public void onSuccess(Void result) {
226                 LOG.debug("Stale Group creation success");
227             }
228
229             @Override
230             public void onFailure(Throwable throwable) {
231                 LOG.error("Stale Group creation failed {}", throwable);
232             }
233         }, MoreExecutors.directExecutor());
234
235     }
236
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())));
241     }
242
243     private final class AddGroupCallBack implements FutureCallback<RpcResult<AddGroupOutput>> {
244         private final Long groupId;
245         private final NodeId nodeId;
246
247         private AddGroupCallBack(final Long groupId, final NodeId nodeId) {
248             this.groupId = groupId;
249             this.nodeId = nodeId;
250         }
251
252         @Override
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);
257             } else {
258                 LOG.debug("Group add with id {} failed for node {} with error {}", groupId, nodeId,
259                         result.getErrors());
260             }
261         }
262
263         @Override
264         public void onFailure(Throwable throwable) {
265             LOG.error("Service call for adding group {} failed for node with error {}", groupId, nodeId, throwable);
266         }
267     }
268
269     private final class UpdateGroupCallBack implements FutureCallback<RpcResult<UpdateGroupOutput>> {
270         private final Long groupId;
271         private final NodeId nodeId;
272
273         private UpdateGroupCallBack(final Long groupId, final NodeId nodeId) {
274             this.groupId = groupId;
275             this.nodeId = nodeId;
276         }
277
278         @Override
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);
283             } else {
284                 LOG.debug("Group update with id {} failed for node {} with error {}", groupId, nodeId,
285                         result.getErrors().toString());
286             }
287         }
288
289         @Override
290         public void onFailure(Throwable throwable) {
291             LOG.error("Service call for updating group {} failed for node {} with", groupId, nodeId,
292                     throwable);
293         }
294     }
295
296     private final class RemoveGroupCallBack implements FutureCallback<RpcResult<RemoveGroupOutput>> {
297         private final Long groupId;
298         private final NodeId nodeId;
299
300         private RemoveGroupCallBack(final Long groupId, final NodeId nodeId) {
301             this.groupId = groupId;
302             this.nodeId = nodeId;
303         }
304
305         @Override
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);
310             } else {
311                 LOG.debug("Group remove with id {} failed for node {} with error {}", groupId, nodeId,
312                         result.getErrors().toString());
313             }
314         }
315
316         @Override
317         public void onFailure(Throwable throwable) {
318             LOG.error("Service call for removing group {} failed for node with error {}", groupId, nodeId, throwable);
319         }
320     }
321 }