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 / StatListenCommitQueue.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.Collections;
12 import java.util.HashMap;
13 import java.util.List;
14 import java.util.Map;
15 import java.util.Map.Entry;
16
17 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
18 import org.opendaylight.controller.md.sal.binding.api.ReadWriteTransaction;
19 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
20 import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
21 import org.opendaylight.controller.md.statistics.manager.StatRpcMsgManager.TransactionCacheContainer;
22 import org.opendaylight.controller.md.statistics.manager.StatisticsManager;
23 import org.opendaylight.controller.md.statistics.manager.StatisticsManager.StatDataStoreOperation;
24 import org.opendaylight.controller.sal.binding.api.NotificationProviderService;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNodeConnector;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.transaction.rev131103.TransactionAware;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.transaction.rev131103.TransactionId;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.port.rev130925.queues.Queue;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.port.rev130925.queues.QueueKey;
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.node.NodeConnector;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.node.NodeConnectorKey;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.queue.statistics.rev131216.FlowCapableNodeConnectorQueueStatisticsData;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.queue.statistics.rev131216.FlowCapableNodeConnectorQueueStatisticsDataBuilder;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.queue.statistics.rev131216.OpendaylightQueueStatisticsListener;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.queue.statistics.rev131216.QueueStatisticsUpdate;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.queue.statistics.rev131216.flow.capable.node.connector.queue.statistics.FlowCapableNodeConnectorQueueStatistics;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.queue.statistics.rev131216.flow.capable.node.connector.queue.statistics.FlowCapableNodeConnectorQueueStatisticsBuilder;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.queue.statistics.rev131216.queue.id.and.statistics.map.QueueIdAndStatisticsMap;
43 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
44 import org.slf4j.Logger;
45 import org.slf4j.LoggerFactory;
46
47 import com.google.common.base.Optional;
48 import com.google.common.base.Preconditions;
49
50 /**
51  * statistics-manager
52  * org.opendaylight.controller.md.statistics.manager.impl
53  *
54  * StatNotifyCommitQueue
55  * Class is a NotifyListner for Queues Statistics
56  * All expected (registered) queueStatistics will be builded and
57  * commit to Operational/DataStore
58  *
59  * @author <a href="mailto:vdemcak@cisco.com">Vaclav Demcak</a>
60  *
61  */
62 public class StatListenCommitQueue extends StatAbstractListenCommit<Queue, OpendaylightQueueStatisticsListener>
63                                         implements OpendaylightQueueStatisticsListener {
64
65     private final static Logger LOG = LoggerFactory.getLogger(StatListenCommitQueue.class);
66
67     public StatListenCommitQueue(final StatisticsManager manager, final DataBroker db,
68             final NotificationProviderService nps) {
69         super(manager, db, nps, Queue.class);
70     }
71
72     @Override
73     protected OpendaylightQueueStatisticsListener getStatNotificationListener() {
74         return this;
75     }
76
77     @Override
78     protected InstanceIdentifier<Queue> getWildCardedRegistrationPath() {
79         return InstanceIdentifier.create(Nodes.class).child(Node.class).child(NodeConnector.class)
80             .augmentation(FlowCapableNodeConnector.class).child(Queue.class);
81     }
82
83     @Override
84     public void onQueueStatisticsUpdate(final QueueStatisticsUpdate notification) {
85         final TransactionId transId = notification.getTransactionId();
86         final NodeId nodeId = notification.getId();
87         if ( ! isExpectedStatistics(transId, nodeId)) {
88             LOG.debug("STAT-MANAGER - QueueStatisticsUpdate: unregistred notification detect TransactionId {}", transId);
89             return;
90         }
91         manager.getRpcMsgManager().addNotification(notification, nodeId);
92         if (notification.isMoreReplies()) {
93             return;
94         }
95
96         /* Don't block RPC Notification thread */
97         manager.enqueue(new StatDataStoreOperation() {
98             @Override
99             public void applyOperation(final ReadWriteTransaction tx) {
100
101                 final InstanceIdentifier<Node> nodeIdent = InstanceIdentifier.create(Nodes.class)
102                         .child(Node.class, new NodeKey(nodeId));
103
104                 /* Validate exist Node */
105                 Optional<Node> fNode = Optional.absent();
106                 try {
107                     fNode = tx.read(LogicalDatastoreType.OPERATIONAL, nodeIdent).checkedGet();
108                 }
109                 catch (final ReadFailedException e) {
110                     LOG.debug("Read Operational/DS for Node fail! {}", nodeIdent, e);
111                 }
112                 if ( ! fNode.isPresent()) {
113                     LOG.trace("Read Operational/DS for Node fail! Node {} doesn't exist.", nodeIdent);
114                     return;
115                 }
116
117                 /* Get and Validate TransactionCacheContainer */
118                 final Optional<TransactionCacheContainer<?>> txContainer = getTransactionCacheContainer(transId, nodeId);
119                 if ( ! isTransactionCacheContainerValid(txContainer)) {
120                     return;
121                 }
122                 /* Prepare List actual Queues and not updated Queues will be removed */
123                 final List<NodeConnector> existConnectors = fNode.get().getNodeConnector() != null
124                         ? fNode.get().getNodeConnector() : Collections.<NodeConnector> emptyList();
125                 final Map<QueueKey, NodeConnectorKey> existQueueKeys = new HashMap<>();
126                 for (final NodeConnector connect : existConnectors) {
127                     final List<Queue> listQueues = connect.getAugmentation(FlowCapableNodeConnector.class).getQueue();
128                     if (listQueues != null) {
129                         for (final Queue queue : listQueues) {
130                             existQueueKeys.put(queue.getKey(), connect.getKey());
131                         }
132                     }
133                 }
134                 /* Queue processing */
135                 statQueueCommit(txContainer, tx, nodeIdent, existQueueKeys);
136                 /* Delete all not presented Group Nodes */
137                 deleteAllNotPresentedNodes(nodeIdent, tx, Collections.unmodifiableMap(existQueueKeys));
138                 /* Notification for continue collecting statistics */
139                 notifyToCollectNextStatistics(nodeIdent);
140             }
141         });
142     }
143
144     private void statQueueCommit(
145             final Optional<TransactionCacheContainer<?>> txContainer, final ReadWriteTransaction tx,
146             final InstanceIdentifier<Node> nodeIdent, final Map<QueueKey, NodeConnectorKey> existQueueKeys) {
147
148         Preconditions.checkNotNull(existQueueKeys);
149         Preconditions.checkNotNull(txContainer);
150         Preconditions.checkNotNull(nodeIdent);
151         Preconditions.checkNotNull(tx);
152
153         final List<? extends TransactionAware> cacheNotifs = txContainer.get().getNotifications();
154         for (final TransactionAware notif : cacheNotifs) {
155             if ( ! (notif instanceof QueueStatisticsUpdate)) {
156                 break;
157             }
158             final List<QueueIdAndStatisticsMap> queueStats = ((QueueStatisticsUpdate) notif).getQueueIdAndStatisticsMap();
159             if (queueStats == null) {
160                 break;
161             }
162             for (final QueueIdAndStatisticsMap queueStat : queueStats) {
163                 if (queueStat.getQueueId() != null) {
164                     final FlowCapableNodeConnectorQueueStatistics statChild =
165                             new FlowCapableNodeConnectorQueueStatisticsBuilder(queueStat).build();
166                     final FlowCapableNodeConnectorQueueStatisticsDataBuilder statBuild =
167                             new FlowCapableNodeConnectorQueueStatisticsDataBuilder();
168                     statBuild.setFlowCapableNodeConnectorQueueStatistics(statChild);
169                     final QueueKey qKey = new QueueKey(queueStat.getQueueId());
170                     final InstanceIdentifier<FlowCapableNodeConnectorQueueStatisticsData> queueStatIdent = nodeIdent
171                             .child(NodeConnector.class, new NodeConnectorKey(queueStat.getNodeConnectorId()))
172                             .augmentation(FlowCapableNodeConnector.class)
173                             .child(Queue.class, qKey).augmentation(FlowCapableNodeConnectorQueueStatisticsData.class);
174                     existQueueKeys.remove(qKey);
175                     tx.put(LogicalDatastoreType.OPERATIONAL, queueStatIdent, statBuild.build());
176                 }
177             }
178         }
179     }
180
181     private void deleteAllNotPresentedNodes(final InstanceIdentifier<Node> nodeIdent,
182             final ReadWriteTransaction tx, final Map<QueueKey, NodeConnectorKey> existQueueKeys) {
183
184         Preconditions.checkNotNull(nodeIdent);
185         Preconditions.checkNotNull(tx);
186
187         if (existQueueKeys == null) {
188             return;
189         }
190
191         for (final Entry<QueueKey, NodeConnectorKey> entry : existQueueKeys.entrySet()) {
192             final InstanceIdentifier<Queue> queueIdent = nodeIdent.child(NodeConnector.class, entry.getValue())
193                     .augmentation(FlowCapableNodeConnector.class).child(Queue.class, entry.getKey());
194             LOG.trace("Queue {} has to removed.", queueIdent);
195             Optional<Queue> delQueue = Optional.absent();
196             try {
197                 delQueue = tx.read(LogicalDatastoreType.OPERATIONAL, queueIdent).checkedGet();
198             }
199             catch (final ReadFailedException e) {
200                 // NOOP - probably another transaction delete that node
201             }
202             if (delQueue.isPresent()) {
203                 tx.delete(LogicalDatastoreType.OPERATIONAL, queueIdent);
204             }
205         }
206     }
207 }
208

©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.