2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
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
9 package org.opendaylight.controller.md.statistics.manager.impl;
11 import java.util.ArrayList;
12 import java.util.List;
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;
44 import com.google.common.base.Optional;
48 * org.opendaylight.controller.md.statistics.manager.impl
50 * StatNotifyCommitQueue
51 * Class is a NotifyListner for Queues Statistics
52 * All expected (registered) queueStatistics will be builded and
53 * commit to Operational/DataStore
55 * @author <a href="mailto:vdemcak@cisco.com">Vaclav Demcak</a>
58 public class StatListenCommitQueue extends StatAbstractListenCommit<Queue, OpendaylightQueueStatisticsListener>
59 implements OpendaylightQueueStatisticsListener {
61 private final static Logger LOG = LoggerFactory.getLogger(StatListenCommitQueue.class);
63 public StatListenCommitQueue(final StatisticsManager manager, final DataBroker db,
64 final NotificationProviderService nps) {
65 super(manager, db, nps, Queue.class);
69 protected OpendaylightQueueStatisticsListener getStatNotificationListener() {
74 protected InstanceIdentifier<Queue> getWildCardedRegistrationPath() {
75 return InstanceIdentifier.create(Nodes.class).child(Node.class).child(NodeConnector.class)
76 .augmentation(FlowCapableNodeConnector.class).child(Queue.class);
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);
87 if (notification.isMoreReplies()) {
88 manager.getRpcMsgManager().addNotification(notification, nodeId);
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());
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() {
109 public void applyOperation(final ReadWriteTransaction trans) {
110 /* Notification for continue */
111 notifyToCollectNextStatistics(nodeIdent);
112 statQueueCommit(queueStats, nodeIdent, trans);
117 private void statQueueCommit(final List<QueueIdAndStatisticsMap> queueStats,
118 final InstanceIdentifier<Node> nodeIdent, final ReadWriteTransaction trans) {
120 /* check exist FlowCapableNode and write statistics */
121 Optional<Node> fNode = Optional.absent();
123 fNode = trans.read(LogicalDatastoreType.OPERATIONAL, nodeIdent).checkedGet();
125 catch (final ReadFailedException e) {
126 LOG.debug("Read Operational/DS for Node fail! {}", nodeIdent, e);
129 if ( ! fNode.isPresent()) {
130 LOG.trace("Read Operational/DS for Node fail! Node {} doesn't exist.", nodeIdent);
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(), true);