e336f01874e25f48b2a13d1221b06147bc7c9df0
[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.ArrayList;
12 import java.util.List;
13
14 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
15 import org.opendaylight.controller.md.sal.binding.api.ReadWriteTransaction;
16 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
17 import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
18 import org.opendaylight.controller.md.statistics.manager.StatRpcMsgManager.TransactionCacheContainer;
19 import org.opendaylight.controller.md.statistics.manager.StatisticsManager;
20 import org.opendaylight.controller.md.statistics.manager.StatisticsManager.StatDataStoreOperation;
21 import org.opendaylight.controller.sal.binding.api.NotificationProviderService;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNodeConnector;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.transaction.rev131103.TransactionAware;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.transaction.rev131103.TransactionId;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.port.rev130925.queues.Queue;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.port.rev130925.queues.QueueKey;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.node.NodeConnector;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.node.NodeConnectorKey;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.queue.statistics.rev131216.FlowCapableNodeConnectorQueueStatisticsData;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.queue.statistics.rev131216.FlowCapableNodeConnectorQueueStatisticsDataBuilder;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.queue.statistics.rev131216.OpendaylightQueueStatisticsListener;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.queue.statistics.rev131216.QueueStatisticsUpdate;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.queue.statistics.rev131216.flow.capable.node.connector.queue.statistics.FlowCapableNodeConnectorQueueStatistics;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.queue.statistics.rev131216.flow.capable.node.connector.queue.statistics.FlowCapableNodeConnectorQueueStatisticsBuilder;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.queue.statistics.rev131216.queue.id.and.statistics.map.QueueIdAndStatisticsMap;
40 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
41 import org.slf4j.Logger;
42 import org.slf4j.LoggerFactory;
43
44 import com.google.common.base.Optional;
45
46 /**
47  * statistics-manager
48  * org.opendaylight.controller.md.statistics.manager.impl
49  *
50  * StatNotifyCommitQueue
51  * Class is a NotifyListner for Queues Statistics
52  * All expected (registered) queueStatistics will be builded and
53  * commit to Operational/DataStore
54  *
55  * @author <a href="mailto:vdemcak@cisco.com">Vaclav Demcak</a>
56  *
57  */
58 public class StatListenCommitQueue extends StatAbstractListenCommit<Queue, OpendaylightQueueStatisticsListener>
59                                         implements OpendaylightQueueStatisticsListener {
60
61     private final static Logger LOG = LoggerFactory.getLogger(StatListenCommitQueue.class);
62
63     public StatListenCommitQueue(final StatisticsManager manager, final DataBroker db,
64             final NotificationProviderService nps) {
65         super(manager, db, nps, Queue.class);
66     }
67
68     @Override
69     protected OpendaylightQueueStatisticsListener getStatNotificationListener() {
70         return this;
71     }
72
73     @Override
74     protected InstanceIdentifier<Queue> getWildCardedRegistrationPath() {
75         return InstanceIdentifier.create(Nodes.class).child(Node.class).child(NodeConnector.class)
76             .augmentation(FlowCapableNodeConnector.class).child(Queue.class);
77     }
78
79     @Override
80     public void onQueueStatisticsUpdate(final QueueStatisticsUpdate notification) {
81         final TransactionId transId = notification.getTransactionId();
82         final NodeId nodeId = notification.getId();
83         if ( ! isExpectedStatistics(transId, nodeId)) {
84             LOG.debug("STAT-MANAGER - QueueStatisticsUpdate: unregistred notification detect TransactionId {}", transId);
85             return;
86         }
87         if (notification.isMoreReplies()) {
88             manager.getRpcMsgManager().addNotification(notification, nodeId);
89             return;
90         }
91         final List<QueueIdAndStatisticsMap> queueStats = notification.getQueueIdAndStatisticsMap() != null
92                 ? new ArrayList<>(notification.getQueueIdAndStatisticsMap()) : new ArrayList<QueueIdAndStatisticsMap>(10);
93         final Optional<TransactionCacheContainer<?>> txContainer = getTransactionCacheContainer(transId, nodeId);
94         if (txContainer.isPresent()) {
95             final List<? extends TransactionAware> cachedNotifs =
96                     txContainer.get().getNotifications();
97             for (final TransactionAware notif : cachedNotifs) {
98                 if (notif instanceof QueueStatisticsUpdate) {
99                     queueStats.addAll(((QueueStatisticsUpdate) notif).getQueueIdAndStatisticsMap());
100                 }
101             }
102         }
103         final InstanceIdentifier<Node> nodeIdent = InstanceIdentifier.create(Nodes.class)
104                 .child(Node.class, new NodeKey(nodeId));
105         /* Queue statistics are small size and we are not able to change for OF cross controller
106          * - don't need to make are atomic */
107         manager.enqueue(new StatDataStoreOperation() {
108             @Override
109             public void applyOperation(final ReadWriteTransaction trans) {
110                 /* Notification for continue */
111                 notifyToCollectNextStatistics(nodeIdent);
112                 statQueueCommit(queueStats, nodeIdent, trans);
113             }
114         });
115     }
116
117     private void statQueueCommit(final List<QueueIdAndStatisticsMap> queueStats,
118             final InstanceIdentifier<Node> nodeIdent, final ReadWriteTransaction trans) {
119
120         /* check exist FlowCapableNode and write statistics */
121         Optional<Node> fNode = Optional.absent();
122         try {
123             fNode = trans.read(LogicalDatastoreType.OPERATIONAL, nodeIdent).checkedGet();
124         }
125         catch (final ReadFailedException e) {
126             LOG.debug("Read Operational/DS for Node fail! {}", nodeIdent, e);
127             return;
128         }
129         if ( ! fNode.isPresent()) {
130             LOG.trace("Read Operational/DS for Node fail! Node {} doesn't exist.", nodeIdent);
131             return;
132         }
133
134         for (final QueueIdAndStatisticsMap queueEntry : queueStats) {
135             final FlowCapableNodeConnectorQueueStatistics statChild =
136                     new FlowCapableNodeConnectorQueueStatisticsBuilder(queueEntry).build();
137             final FlowCapableNodeConnectorQueueStatisticsDataBuilder statBuild =
138                     new FlowCapableNodeConnectorQueueStatisticsDataBuilder();
139             statBuild.setFlowCapableNodeConnectorQueueStatistics(statChild);
140             final QueueKey qKey = new QueueKey(queueEntry.getQueueId());
141             final InstanceIdentifier<FlowCapableNodeConnectorQueueStatisticsData> queueStatIdent = nodeIdent
142                     .child(NodeConnector.class, new NodeConnectorKey(queueEntry.getNodeConnectorId()))
143                     .augmentation(FlowCapableNodeConnector.class)
144                     .child(Queue.class, qKey).augmentation(FlowCapableNodeConnectorQueueStatisticsData.class);
145             trans.put(LogicalDatastoreType.OPERATIONAL, queueStatIdent, statBuild.build());
146         }
147     }
148 }
149