Merge "Bug 2551 - Statistics collection of random node fails when large number if...
[controller.git] / opendaylight / md-sal / statistics-manager / src / main / java / org / opendaylight / controller / md / statistics / manager / impl / StatNotifyCommitTable.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.ReadWriteTransaction;
15 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
16 import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
17 import org.opendaylight.controller.md.statistics.manager.StatRpcMsgManager.TransactionCacheContainer;
18 import org.opendaylight.controller.md.statistics.manager.StatisticsManager;
19 import org.opendaylight.controller.md.statistics.manager.StatisticsManager.StatDataStoreOperation;
20 import org.opendaylight.controller.md.statistics.manager.StatisticsManager.StatDataStoreOperation.StatsManagerOperationType;
21 import org.opendaylight.controller.sal.binding.api.NotificationProviderService;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.Table;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.TableBuilder;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.TableKey;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.table.statistics.rev131215.FlowTableStatisticsData;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.table.statistics.rev131215.FlowTableStatisticsDataBuilder;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.table.statistics.rev131215.FlowTableStatisticsUpdate;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.table.statistics.rev131215.OpendaylightFlowTableStatisticsListener;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.table.statistics.rev131215.flow.table.and.statistics.map.FlowTableAndStatisticsMap;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.table.statistics.rev131215.flow.table.statistics.FlowTableStatistics;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.table.statistics.rev131215.flow.table.statistics.FlowTableStatisticsBuilder;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.transaction.rev131103.TransactionAware;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.transaction.rev131103.TransactionId;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
39 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
40 import org.slf4j.Logger;
41 import org.slf4j.LoggerFactory;
42
43 import com.google.common.base.Optional;
44
45 /**
46  * statistics-manager
47  * org.opendaylight.controller.md.statistics.manager.impl
48  *
49  * StatNotifyCommitTable
50  * Class is a NotifyListener for TableStatistics
51  * All expected (registered) tableStatistics will be builded and
52  * commit to Operational/DataStore
53  *
54  * @author <a href="mailto:vdemcak@cisco.com">Vaclav Demcak</a>
55  *
56  */
57 public class StatNotifyCommitTable extends StatAbstractNotifyCommit<OpendaylightFlowTableStatisticsListener>
58                                         implements OpendaylightFlowTableStatisticsListener {
59
60     private final static Logger LOG = LoggerFactory.getLogger(StatNotifyCommitTable.class);
61
62     public StatNotifyCommitTable(final StatisticsManager manager,
63             final NotificationProviderService nps) {
64         super(manager, nps);
65     }
66
67     @Override
68     protected OpendaylightFlowTableStatisticsListener getStatNotificationListener() {
69         return this;
70     }
71
72     @Override
73     public void onFlowTableStatisticsUpdate(final FlowTableStatisticsUpdate notification) {
74         final TransactionId transId = notification.getTransactionId();
75         final NodeId nodeId = notification.getId();
76         if ( ! isExpectedStatistics(transId, nodeId)) {
77             LOG.debug("STAT-MANAGER - FlowTableStatisticsUpdate: unregistred notification detect TransactionId {}", transId);
78             return;
79         }
80         manager.getRpcMsgManager().addNotification(notification, nodeId);
81         if (notification.isMoreReplies()) {
82             return;
83         }
84         /* Don't block RPC Notification thread */
85         manager.enqueue(new StatDataStoreOperation(StatsManagerOperationType.DATA_COMMIT_OPER_DS,nodeId) {
86             @Override
87             public void applyOperation(final ReadWriteTransaction trans) {
88                 final List<FlowTableAndStatisticsMap> tableStats = new ArrayList<FlowTableAndStatisticsMap>(10);
89                 final Optional<TransactionCacheContainer<?>> txContainer = getTransactionCacheContainer(transId, nodeId);
90                 final InstanceIdentifier<Node> nodeIdent = InstanceIdentifier.create(Nodes.class)
91                         .child(Node.class, new NodeKey(nodeId));
92                 if (( ! txContainer.isPresent()) || txContainer.get().getNodeId() == null) {
93                     return;
94                 }
95                 final List<? extends TransactionAware> cachedNotifs = txContainer.get().getNotifications();
96                 for (final TransactionAware notif : cachedNotifs) {
97                     if (notif instanceof FlowTableStatisticsUpdate) {
98                         final List<FlowTableAndStatisticsMap> statNotif =
99                                 ((FlowTableStatisticsUpdate) notif).getFlowTableAndStatisticsMap();
100                         if (statNotif != null) {
101                             tableStats.addAll(statNotif);
102                         }
103                     }
104                 }
105                 /* write stat to trans */
106                 statTableCommit(tableStats, nodeIdent, trans);
107                 /* Notification for continue collecting statistics - Tables statistics are still same size
108                  * and they are small - don't need to wait to whole apply operation */
109                 notifyToCollectNextStatistics(nodeIdent, transId);
110             }
111         });
112     }
113
114     private void statTableCommit(final List<FlowTableAndStatisticsMap> tableStats, final InstanceIdentifier<Node> nodeIdent,
115             final ReadWriteTransaction trans) {
116         final InstanceIdentifier<FlowCapableNode> fNodeIdent = nodeIdent.augmentation(FlowCapableNode.class);
117         /* check flow Capable Node and write statistics */
118         Optional<FlowCapableNode> fNode = Optional.absent();
119         try {
120             fNode = trans.read(LogicalDatastoreType.OPERATIONAL, fNodeIdent).checkedGet();
121         }
122         catch (final ReadFailedException e) {
123             LOG.debug("Read Operational/DS for FlowCapableNode fail! {}", fNodeIdent, e);
124             return;
125         }
126         if ( ! fNode.isPresent()) {
127             LOG.trace("Read Operational/DS for FlowCapableNode fail! Node {} doesn't exist.", fNodeIdent);
128             return;
129         }
130         for (final FlowTableAndStatisticsMap tableStat : tableStats) {
131             final InstanceIdentifier<Table> tableIdent = fNodeIdent
132                     .child(Table.class, new TableKey(tableStat.getTableId().getValue()));
133             final Table table = new TableBuilder().setId(tableStat.getTableId().getValue()).build();
134             trans.merge(LogicalDatastoreType.OPERATIONAL, tableIdent, table);
135             final InstanceIdentifier<FlowTableStatisticsData> tableStatIdent = tableIdent
136                     .augmentation(FlowTableStatisticsData.class);
137             trans.merge(LogicalDatastoreType.OPERATIONAL, tableStatIdent, new FlowTableStatisticsDataBuilder().build());
138
139             final FlowTableStatistics stats = new FlowTableStatisticsBuilder(tableStat).build();
140             final InstanceIdentifier<FlowTableStatistics> tStatIdent = tableStatIdent.child(FlowTableStatistics.class);
141             trans.put(LogicalDatastoreType.OPERATIONAL, tStatIdent, stats);
142         }
143     }
144 }
145