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 / StatListenCommitMeter.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.inventory.rev130819.meters.Meter;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.meters.MeterBuilder;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.meters.MeterKey;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.transaction.rev131103.TransactionAware;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.transaction.rev131103.TransactionId;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.MeterConfigStatsUpdated;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.MeterFeaturesUpdated;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.MeterStatisticsUpdated;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.NodeMeterConfigStats;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.NodeMeterConfigStatsBuilder;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.NodeMeterFeatures;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.NodeMeterFeaturesBuilder;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.NodeMeterStatistics;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.NodeMeterStatisticsBuilder;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.OpendaylightMeterStatisticsListener;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.nodes.node.MeterFeatures;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.nodes.node.MeterFeaturesBuilder;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.nodes.node.meter.MeterConfigStatsBuilder;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.nodes.node.meter.MeterStatistics;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.nodes.node.meter.MeterStatisticsBuilder;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.types.rev130918.meter.config.stats.reply.MeterConfigStats;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.types.rev130918.meter.statistics.reply.MeterStats;
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  * StatListenCommitMeter
64  * Class is a NotifyListener for MeterStatistics and DataChangeListener for Config/DataStore for Meter node.
65  * All expected (registered) MeterStatistics will be builded and commit to Operational/DataStore.
66  * DataChangeEven should call create/delete Meter in Operational/DS
67  *
68  */
69 public class StatListenCommitMeter extends StatAbstractListenCommit<Meter, OpendaylightMeterStatisticsListener>
70                                             implements OpendaylightMeterStatisticsListener {
71
72     private static final Logger LOG = LoggerFactory.getLogger(StatListenCommitMeter.class);
73
74     public StatListenCommitMeter(final StatisticsManager manager, final DataBroker db,
75             final NotificationProviderService nps) {
76         super(manager, db, nps, Meter.class);
77     }
78
79     @Override
80     protected InstanceIdentifier<Meter> getWildCardedRegistrationPath() {
81         return InstanceIdentifier.create(Nodes.class).child(Node.class)
82                 .augmentation(FlowCapableNode.class).child(Meter.class);
83     }
84
85     @Override
86     protected OpendaylightMeterStatisticsListener getStatNotificationListener() {
87         return this;
88     }
89
90     @Override
91     public void onMeterConfigStatsUpdated(final MeterConfigStatsUpdated notification) {
92         final TransactionId transId = notification.getTransactionId();
93         final NodeId nodeId = notification.getId();
94         if ( ! isExpectedStatistics(transId, nodeId)) {
95             LOG.debug("STAT-MANAGER - MeterConfigStatsUpdated: unregistred notification detect TransactionId {}", transId);
96             return;
97         }
98         manager.getRpcMsgManager().addNotification(notification, nodeId);
99         if (notification.isMoreReplies()) {
100             return;
101         }
102
103         /* Don't block RPC Notification thread */
104         manager.enqueue(new StatDataStoreOperation() {
105             @Override
106             public void applyOperation(final ReadWriteTransaction tx) {
107
108                 final InstanceIdentifier<Node> nodeIdent = InstanceIdentifier
109                         .create(Nodes.class).child(Node.class, new NodeKey(nodeId));
110
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 Meters and not updated Meters will be removed */
129                 final List<Meter> existMeters = fNode.get().getMeter() != null
130                         ? fNode.get().getMeter() : Collections.<Meter> emptyList();
131                 final List<MeterKey> existMeterKeys = new ArrayList<>();
132                 for (final Meter meter : existMeters) {
133                     existMeterKeys.add(meter.getKey());
134                 }
135                 /* MeterConfig processing */
136                 comitConfMeterStats(txContainer, tx, fNodeIdent, existMeterKeys);
137                 /* Delete all not presented Meter Nodes */
138                 deleteAllNotPresentedNodes(fNodeIdent, tx, Collections.unmodifiableList(existMeterKeys));
139                 /* Notification for continue collecting statistics */
140                 notifyToCollectNextStatistics(nodeIdent);
141             }
142         });
143     }
144
145     @Override
146     public void onMeterFeaturesUpdated(final MeterFeaturesUpdated 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("STAT-MANAGER - MeterFeaturesUpdated: 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 MeterFeaturesUpdated)) {
175                         break;
176                     }
177                     final MeterFeatures stats = new MeterFeaturesBuilder((MeterFeaturesUpdated)notif).build();
178                     final InstanceIdentifier<NodeMeterFeatures> nodeMeterFeatureIdent =
179                             nodeIdent.augmentation(NodeMeterFeatures.class);
180                     final InstanceIdentifier<MeterFeatures> meterFeatureIdent = nodeMeterFeatureIdent
181                             .child(MeterFeatures.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, nodeMeterFeatureIdent, new NodeMeterFeaturesBuilder().build(), true);
191                         tx.put(LogicalDatastoreType.OPERATIONAL, meterFeatureIdent, stats);
192                         manager.registerAdditionalNodeFeature(nodeIdent, StatCapabTypes.METER_STATS);
193                     }
194                 }
195             }
196         });
197     }
198
199     @Override
200     public void onMeterStatisticsUpdated(final MeterStatisticsUpdated 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 - MeterStatisticsUpdated: 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<Meter> notifMeter = Optional.absent();
240                 final Optional<? extends DataObject> inputObj = txContainer.get().getConfInput();
241                 if (inputObj.isPresent() && inputObj.get() instanceof Meter) {
242                     notifMeter = Optional.<Meter> of((Meter)inputObj.get());
243                 }
244                 for (final TransactionAware notif : cacheNotifs) {
245                     if ( ! (notif instanceof MeterStatisticsUpdated)) {
246                         break;
247                     }
248                     statMeterCommit(((MeterStatisticsUpdated) notif).getMeterStats(), nodeIdent, tx);
249                 }
250                 if (notifMeter.isPresent()) {
251                     notifyToCollectNextStatistics(nodeIdent);
252                 }
253             }
254         });
255     }
256
257     private void statMeterCommit(final List<MeterStats> meterStats,
258             final InstanceIdentifier<Node> nodeIdent, final ReadWriteTransaction tx) {
259
260         Preconditions.checkNotNull(meterStats);
261         Preconditions.checkNotNull(nodeIdent);
262         Preconditions.checkNotNull(tx);
263
264         final InstanceIdentifier<FlowCapableNode> fNodeIdent = nodeIdent.augmentation(FlowCapableNode.class);
265
266         for (final MeterStats mStat : meterStats) {
267             final MeterStatistics stats = new MeterStatisticsBuilder(mStat).build();
268
269             final InstanceIdentifier<Meter> meterIdent = fNodeIdent.child(Meter.class, new MeterKey(mStat.getMeterId()));
270             final InstanceIdentifier<NodeMeterStatistics> nodeMeterStatIdent = meterIdent
271                     .augmentation(NodeMeterStatistics.class);
272             final InstanceIdentifier<MeterStatistics> msIdent = nodeMeterStatIdent.child(MeterStatistics.class);
273             /* Meter Statistics commit */
274             Optional<Meter> meter = Optional.absent();
275             try {
276                 meter = tx.read(LogicalDatastoreType.OPERATIONAL, meterIdent).checkedGet();
277             }
278             catch (final ReadFailedException e) {
279                 LOG.debug("Read Operational/DS for FlowCapableNode fail! {}", fNodeIdent, e);
280             }
281             if (meter.isPresent()) {
282                 tx.merge(LogicalDatastoreType.OPERATIONAL, nodeMeterStatIdent, new NodeMeterStatisticsBuilder().build(), true);
283                 tx.put(LogicalDatastoreType.OPERATIONAL, msIdent, stats);
284             }
285         }
286     }
287
288     private void comitConfMeterStats(final Optional<TransactionCacheContainer<?>> txContainer, final ReadWriteTransaction tx,
289             final InstanceIdentifier<FlowCapableNode> fNodeIdent, final List<MeterKey> existMeterKeys) {
290
291         Preconditions.checkNotNull(existMeterKeys);
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 MeterConfigStatsUpdated)) {
299                 break;
300             }
301             final List<MeterConfigStats> meterStats = ((MeterConfigStatsUpdated) notif).getMeterConfigStats();
302             if (meterStats == null) {
303                 break;
304             }
305             for (final MeterConfigStats meterStat : meterStats) {
306                 if (meterStat.getMeterId() != null) {
307                     final MeterBuilder meterBuilder = new MeterBuilder(meterStat);
308                     final MeterKey meterKey = new MeterKey(meterStat.getMeterId());
309                     final InstanceIdentifier<Meter> meterRef = fNodeIdent.child(Meter.class, meterKey);
310
311                     final NodeMeterConfigStatsBuilder meterConfig = new NodeMeterConfigStatsBuilder();
312                     meterConfig.setMeterConfigStats(new MeterConfigStatsBuilder(meterStat).build());
313                     //Update augmented data
314                     meterBuilder.addAugmentation(NodeMeterConfigStats.class, meterConfig.build());
315                     existMeterKeys.remove(meterKey);
316                     tx.put(LogicalDatastoreType.OPERATIONAL, meterRef, meterBuilder.build());
317                 }
318             }
319         }
320     }
321
322     private void deleteAllNotPresentedNodes(final InstanceIdentifier<FlowCapableNode> fNodeIdent,
323             final ReadWriteTransaction tx, final List<MeterKey> deviceMeterKeys) {
324
325         Preconditions.checkNotNull(fNodeIdent);
326         Preconditions.checkNotNull(tx);
327
328         if (deviceMeterKeys == null) {
329             return;
330         }
331
332         for (final MeterKey key : deviceMeterKeys) {
333             final InstanceIdentifier<Meter> delMeterIdent = fNodeIdent.child(Meter.class, key);
334             LOG.trace("Meter {} has to removed.", key);
335             Optional<Meter> delMeter = Optional.absent();
336             try {
337                 delMeter = tx.read(LogicalDatastoreType.OPERATIONAL, delMeterIdent).checkedGet();
338             }
339             catch (final ReadFailedException e) {
340                 // NOOP - probably another transaction delete that node
341             }
342             if (delMeter.isPresent()) {
343                 tx.delete(LogicalDatastoreType.OPERATIONAL, delMeterIdent);
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.