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