Fix bug 2413 NPE for group and meters
[controller.git] / opendaylight / md-sal / statistics-manager / src / main / java / org / opendaylight / controller / md / statistics / manager / impl / StatListenCommitGroup.java
1 /**
2  * Copyright (c) 2014 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
9 package org.opendaylight.controller.md.statistics.manager.impl;
10
11 import java.util.ArrayList;
12 import java.util.Collections;
13 import java.util.List;
14
15 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
16 import org.opendaylight.controller.md.sal.binding.api.ReadWriteTransaction;
17 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
18 import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
19 import org.opendaylight.controller.md.statistics.manager.StatPermCollector.StatCapabTypes;
20 import org.opendaylight.controller.md.statistics.manager.StatRpcMsgManager.TransactionCacheContainer;
21 import org.opendaylight.controller.md.statistics.manager.StatisticsManager;
22 import org.opendaylight.controller.md.statistics.manager.StatisticsManager.StatDataStoreOperation;
23 import org.opendaylight.controller.sal.binding.api.NotificationProviderService;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.transaction.rev131103.TransactionAware;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.transaction.rev131103.TransactionId;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.statistics.rev131111.GroupDescStatsUpdated;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.statistics.rev131111.GroupFeaturesUpdated;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.statistics.rev131111.GroupStatisticsUpdated;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.statistics.rev131111.NodeGroupDescStats;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.statistics.rev131111.NodeGroupDescStatsBuilder;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.statistics.rev131111.NodeGroupFeatures;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.statistics.rev131111.NodeGroupFeaturesBuilder;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.statistics.rev131111.NodeGroupStatistics;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.statistics.rev131111.NodeGroupStatisticsBuilder;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.statistics.rev131111.OpendaylightGroupStatisticsListener;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.statistics.rev131111.group.desc.GroupDescBuilder;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.statistics.rev131111.group.features.GroupFeatures;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.statistics.rev131111.group.features.GroupFeaturesBuilder;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.statistics.rev131111.group.statistics.GroupStatistics;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.statistics.rev131111.group.statistics.GroupStatisticsBuilder;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.group.desc.stats.reply.GroupDescStats;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.group.statistics.reply.GroupStats;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.groups.Group;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.groups.GroupBuilder;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.groups.GroupKey;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
51 import org.opendaylight.yangtools.yang.binding.DataObject;
52 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
53 import org.slf4j.Logger;
54 import org.slf4j.LoggerFactory;
55
56 import com.google.common.base.Optional;
57 import com.google.common.base.Preconditions;
58
59 /**
60  * statistics-manager
61  * org.opendaylight.controller.md.statistics.manager.impl
62  *
63  * StatListenCommitGroup
64  * Class is a NotifyListener for GroupStatistics and DataChangeListener for Config/DataStore for Group node.
65  * All expected (registered) GroupStatistics will be builded and commit to Operational/DataStore.
66  * DataChangeEven should call create/delete Group in Operational/DS
67  *
68  * @author <a href="mailto:vdemcak@cisco.com">Vaclav Demcak</a>
69  *
70  */
71 public class StatListenCommitGroup extends StatAbstractListenCommit<Group, OpendaylightGroupStatisticsListener>
72                                                     implements OpendaylightGroupStatisticsListener {
73
74     private static final Logger LOG = LoggerFactory.getLogger(StatListenCommitMeter.class);
75
76     public StatListenCommitGroup(final StatisticsManager manager,  final DataBroker db,
77             final NotificationProviderService nps) {
78         super(manager, db, nps, Group.class);
79     }
80
81     @Override
82     protected OpendaylightGroupStatisticsListener getStatNotificationListener() {
83         return this;
84     }
85
86     @Override
87     protected InstanceIdentifier<Group> getWildCardedRegistrationPath() {
88         return InstanceIdentifier.create(Nodes.class).child(Node.class)
89                 .augmentation(FlowCapableNode.class).child(Group.class);
90     }
91
92     @Override
93     public void onGroupDescStatsUpdated(final GroupDescStatsUpdated notification) {
94         final TransactionId transId = notification.getTransactionId();
95         final NodeId nodeId = notification.getId();
96         if ( ! isExpectedStatistics(transId, nodeId)) {
97             LOG.debug("Unregistred notification detect TransactionId {}", transId);
98             return;
99         }
100         manager.getRpcMsgManager().addNotification(notification, nodeId);
101         if (notification.isMoreReplies()) {
102             return;
103         }
104
105         /* Don't block RPC Notification thread */
106         manager.enqueue(new StatDataStoreOperation() {
107             @Override
108             public void applyOperation(final ReadWriteTransaction tx) {
109                 final InstanceIdentifier<Node> nodeIdent = InstanceIdentifier
110                         .create(Nodes.class).child(Node.class, new NodeKey(nodeId));
111                 /* Validate exist FlowCapableNode */
112                 final InstanceIdentifier<FlowCapableNode> fNodeIdent = nodeIdent.augmentation(FlowCapableNode.class);
113                 Optional<FlowCapableNode> fNode = Optional.absent();
114                 try {
115                     fNode = tx.read(LogicalDatastoreType.OPERATIONAL,fNodeIdent).checkedGet();
116                 }
117                 catch (final ReadFailedException e) {
118                     LOG.debug("Read Operational/DS for FlowCapableNode fail! {}", fNodeIdent, e);
119                 }
120                 if ( ! fNode.isPresent()) {
121                     return;
122                 }
123                 /* Get and Validate TransactionCacheContainer */
124                 final Optional<TransactionCacheContainer<?>> txContainer = getTransactionCacheContainer(transId, nodeId);
125                 if ( ! isTransactionCacheContainerValid(txContainer)) {
126                     return;
127                 }
128                 /* Prepare List actual Groups and not updated Groups will be removed */
129                 final List<Group> existGroups = fNode.get().getGroup() != null
130                         ? fNode.get().getGroup() : Collections.<Group> emptyList();
131                 final List<GroupKey> existGroupKeys = new ArrayList<>();
132                 for (final Group group : existGroups) {
133                     existGroupKeys.add(group.getKey());
134                 }
135                 /* GroupDesc processing */
136                 statGroupDescCommit(txContainer, tx, fNodeIdent, existGroupKeys);
137                 /* Delete all not presented Group Nodes */
138                 deleteAllNotPresentNode(fNodeIdent, tx, Collections.unmodifiableList(existGroupKeys));
139                 /* Notification for continue collecting statistics */
140                 notifyToCollectNextStatistics(nodeIdent);
141             }
142         });
143     }
144
145     @Override
146     public void onGroupFeaturesUpdated(final GroupFeaturesUpdated notification) {
147         Preconditions.checkNotNull(notification);
148         final TransactionId transId = notification.getTransactionId();
149         final NodeId nodeId = notification.getId();
150         if ( ! isExpectedStatistics(transId, nodeId)) {
151             LOG.debug("Unregistred notification detect TransactionId {}", transId);
152             return;
153         }
154         manager.getRpcMsgManager().addNotification(notification, nodeId);
155         if (notification.isMoreReplies()) {
156             return;
157         }
158
159         /* Don't block RPC Notification thread */
160         manager.enqueue(new StatDataStoreOperation() {
161             @Override
162             public void applyOperation(final ReadWriteTransaction tx) {
163                 /* Get and Validate TransactionCacheContainer */
164                 final Optional<TransactionCacheContainer<?>> txContainer = getTransactionCacheContainer(transId, nodeId);
165                 if ( ! isTransactionCacheContainerValid(txContainer)) {
166                     return;
167                 }
168
169                 final InstanceIdentifier<Node> nodeIdent = InstanceIdentifier
170                         .create(Nodes.class).child(Node.class, new NodeKey(nodeId));
171
172                 final List<? extends TransactionAware> cacheNotifs = txContainer.get().getNotifications();
173                 for (final TransactionAware notif : cacheNotifs) {
174                     if ( ! (notif instanceof GroupFeaturesUpdated)) {
175                         break;
176                     }
177                     final GroupFeatures stats = new GroupFeaturesBuilder((GroupFeaturesUpdated)notif).build();
178                     final InstanceIdentifier<NodeGroupFeatures> nodeGroupFeatureIdent =
179                             nodeIdent.augmentation(NodeGroupFeatures.class);
180                     final InstanceIdentifier<GroupFeatures> groupFeatureIdent = nodeGroupFeatureIdent
181                             .child(GroupFeatures.class);
182                     Optional<Node> node = Optional.absent();
183                     try {
184                         node = tx.read(LogicalDatastoreType.OPERATIONAL, nodeIdent).checkedGet();
185                     }
186                     catch (final ReadFailedException e) {
187                         LOG.debug("Read Operational/DS for Node fail! {}", nodeIdent, e);
188                     }
189                     if (node.isPresent()) {
190                         tx.merge(LogicalDatastoreType.OPERATIONAL, nodeGroupFeatureIdent, new NodeGroupFeaturesBuilder().build(), true);
191                         tx.put(LogicalDatastoreType.OPERATIONAL, groupFeatureIdent, stats);
192                         manager.registerAdditionalNodeFeature(nodeIdent, StatCapabTypes.GROUP_STATS);
193                     }
194                 }
195             }
196         });
197     }
198
199     @Override
200     public void onGroupStatisticsUpdated(final GroupStatisticsUpdated notification) {
201         Preconditions.checkNotNull(notification);
202         final TransactionId transId = notification.getTransactionId();
203         final NodeId nodeId = notification.getId();
204         if ( ! isExpectedStatistics(transId, nodeId)) {
205             LOG.debug("STAT-MANAGER - GroupStatisticsUpdated: unregistred notification detect TransactionId {}", transId);
206             return;
207         }
208         manager.getRpcMsgManager().addNotification(notification, nodeId);
209         if (notification.isMoreReplies()) {
210             return;
211         }
212
213         /* Don't block RPC Notification thread */
214         manager.enqueue(new StatDataStoreOperation() {
215             @Override
216             public void applyOperation(final ReadWriteTransaction tx) {
217
218                 final InstanceIdentifier<Node> nodeIdent = InstanceIdentifier
219                         .create(Nodes.class).child(Node.class, new NodeKey(nodeId));
220                 /* Node exist check */
221                 Optional<Node> node = Optional.absent();
222                 try {
223                     node = tx.read(LogicalDatastoreType.OPERATIONAL, nodeIdent).checkedGet();
224                 }
225                 catch (final ReadFailedException e) {
226                     LOG.debug("Read Operational/DS for Node fail! {}", nodeIdent, e);
227                 }
228                 if ( ! node.isPresent()) {
229                     return;
230                 }
231
232                 /* Get and Validate TransactionCacheContainer */
233                 final Optional<TransactionCacheContainer<?>> txContainer = getTransactionCacheContainer(transId, nodeId);
234                 if ( ! isTransactionCacheContainerValid(txContainer)) {
235                     return;
236                 }
237                 final List<? extends TransactionAware> cacheNotifs = txContainer.get().getNotifications();
238
239                 Optional<Group> notifGroup = Optional.absent();
240                 final Optional<? extends DataObject> inputObj = txContainer.get().getConfInput();
241                 if (inputObj.isPresent() && inputObj.get() instanceof Group) {
242                     notifGroup = Optional.<Group> of((Group)inputObj.get());
243                 }
244                 for (final TransactionAware notif : cacheNotifs) {
245                     if ( ! (notif instanceof GroupStatisticsUpdated)) {
246                         break;
247                     }
248                     statGroupCommit(((GroupStatisticsUpdated) notif).getGroupStats(), nodeIdent, tx);
249                 }
250                 if (notifGroup.isPresent()) {
251                     notifyToCollectNextStatistics(nodeIdent);
252                 }
253             }
254         });
255     }
256
257     private void statGroupCommit(final List<GroupStats> groupStats, final InstanceIdentifier<Node> nodeIdent,
258             final ReadWriteTransaction tx) {
259
260         Preconditions.checkNotNull(groupStats);
261         Preconditions.checkNotNull(nodeIdent);
262         Preconditions.checkNotNull(tx);
263
264         final InstanceIdentifier<FlowCapableNode> fNodeIdent = nodeIdent.augmentation(FlowCapableNode.class);
265
266         for (final GroupStats gStat : groupStats) {
267             final GroupStatistics stats = new GroupStatisticsBuilder(gStat).build();
268
269             final InstanceIdentifier<Group> groupIdent = fNodeIdent.child(Group.class, new GroupKey(gStat.getGroupId()));
270             final InstanceIdentifier<NodeGroupStatistics> nGroupStatIdent =groupIdent
271                     .augmentation(NodeGroupStatistics.class);
272             final InstanceIdentifier<GroupStatistics> gsIdent = nGroupStatIdent.child(GroupStatistics.class);
273             /* Statistics Writing */
274             Optional<Group> group = Optional.absent();
275             try {
276                 group = tx.read(LogicalDatastoreType.OPERATIONAL, groupIdent).checkedGet();
277             }
278             catch (final ReadFailedException e) {
279                 LOG.debug("Read Operational/DS for Group node fail! {}", groupIdent, e);
280             }
281             if (group.isPresent()) {
282                 tx.merge(LogicalDatastoreType.OPERATIONAL, nGroupStatIdent, new NodeGroupStatisticsBuilder().build(), true);
283                 tx.put(LogicalDatastoreType.OPERATIONAL, gsIdent, stats);
284             }
285         }
286     }
287
288     private void statGroupDescCommit(final Optional<TransactionCacheContainer<?>> txContainer, final ReadWriteTransaction tx,
289             final InstanceIdentifier<FlowCapableNode> fNodeIdent, final List<GroupKey> existGroupKeys) {
290
291         Preconditions.checkNotNull(existGroupKeys);
292         Preconditions.checkNotNull(txContainer);
293         Preconditions.checkNotNull(fNodeIdent);
294         Preconditions.checkNotNull(tx);
295
296         final List<? extends TransactionAware> cacheNotifs = txContainer.get().getNotifications();
297         for (final TransactionAware notif : cacheNotifs) {
298             if ( ! (notif instanceof GroupDescStatsUpdated)) {
299                 break;
300             }
301             final List<GroupDescStats> groupStats = ((GroupDescStatsUpdated) notif).getGroupDescStats();
302             if (groupStats == null) {
303                 break;
304             }
305             for (final GroupDescStats group : groupStats) {
306                 if (group.getGroupId() != null) {
307                     final GroupBuilder groupBuilder = new GroupBuilder(group);
308                     final GroupKey groupKey = new GroupKey(group.getGroupId());
309                     final InstanceIdentifier<Group> groupRef = fNodeIdent.child(Group.class,groupKey);
310
311                     final NodeGroupDescStatsBuilder groupDesc= new NodeGroupDescStatsBuilder();
312                     groupDesc.setGroupDesc(new GroupDescBuilder(group).build());
313                     //Update augmented data
314                     groupBuilder.addAugmentation(NodeGroupDescStats.class, groupDesc.build());
315                     existGroupKeys.remove(groupKey);
316                     tx.put(LogicalDatastoreType.OPERATIONAL, groupRef, groupBuilder.build());
317                 }
318             }
319         }
320     }
321
322     private void deleteAllNotPresentNode(final InstanceIdentifier<FlowCapableNode> fNodeIdent,
323             final ReadWriteTransaction trans, final List<GroupKey> deviceGroupKeys) {
324
325         Preconditions.checkNotNull(fNodeIdent);
326         Preconditions.checkNotNull(trans);
327
328         if (deviceGroupKeys == null) {
329             return;
330         }
331
332         for (final GroupKey key : deviceGroupKeys) {
333             final InstanceIdentifier<Group> delGroupIdent = fNodeIdent.child(Group.class, key);
334             LOG.trace("Group {} has to removed.", key);
335             Optional<Group> delGroup = Optional.absent();
336             try {
337                 delGroup = trans.read(LogicalDatastoreType.OPERATIONAL, delGroupIdent).checkedGet();
338             }
339             catch (final ReadFailedException e) {
340                 // NOOP - probably another transaction delete that node
341             }
342             if (delGroup.isPresent()) {
343                 trans.delete(LogicalDatastoreType.OPERATIONAL, delGroupIdent);
344             }
345         }
346     }
347 }
348

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.