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.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.sal.binding.api.NotificationProviderService;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.Table;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.TableBuilder;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.TableKey;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.table.statistics.rev131215.FlowTableStatisticsData;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.table.statistics.rev131215.FlowTableStatisticsDataBuilder;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.table.statistics.rev131215.FlowTableStatisticsUpdate;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.table.statistics.rev131215.OpendaylightFlowTableStatisticsListener;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.table.statistics.rev131215.flow.table.and.statistics.map.FlowTableAndStatisticsMap;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.table.statistics.rev131215.flow.table.statistics.FlowTableStatistics;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.table.statistics.rev131215.flow.table.statistics.FlowTableStatisticsBuilder;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.transaction.rev131103.TransactionAware;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.transaction.rev131103.TransactionId;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
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.yangtools.yang.binding.InstanceIdentifier;
39 import org.slf4j.Logger;
40 import org.slf4j.LoggerFactory;
42 import com.google.common.base.Optional;
46 * org.opendaylight.controller.md.statistics.manager.impl
48 * StatNotifyCommitTable
49 * Class is a NotifyListener for TableStatistics
50 * All expected (registered) tableStatistics will be builded and
51 * commit to Operational/DataStore
53 * @author <a href="mailto:vdemcak@cisco.com">Vaclav Demcak</a>
56 public class StatNotifyCommitTable extends StatAbstractNotifyCommit<OpendaylightFlowTableStatisticsListener>
57 implements OpendaylightFlowTableStatisticsListener {
59 private final static Logger LOG = LoggerFactory.getLogger(StatNotifyCommitTable.class);
61 public StatNotifyCommitTable(final StatisticsManager manager,
62 final NotificationProviderService nps) {
67 protected OpendaylightFlowTableStatisticsListener getStatNotificationListener() {
72 public void onFlowTableStatisticsUpdate(final FlowTableStatisticsUpdate notification) {
73 final TransactionId transId = notification.getTransactionId();
74 final NodeId nodeId = notification.getId();
75 if ( ! isExpectedStatistics(transId, nodeId)) {
76 LOG.debug("STAT-MANAGER - FlowTableStatisticsUpdate: unregistred notification detect TransactionId {}", transId);
79 manager.getRpcMsgManager().addNotification(notification, nodeId);
80 if (notification.isMoreReplies()) {
83 /* Don't block RPC Notification thread */
84 manager.enqueue(new StatDataStoreOperation() {
86 public void applyOperation(final ReadWriteTransaction trans) {
87 final List<FlowTableAndStatisticsMap> tableStats = new ArrayList<FlowTableAndStatisticsMap>(10);
88 final Optional<TransactionCacheContainer<?>> txContainer = getTransactionCacheContainer(transId, nodeId);
89 final InstanceIdentifier<Node> nodeIdent = InstanceIdentifier.create(Nodes.class)
90 .child(Node.class, new NodeKey(nodeId));
91 if (( ! txContainer.isPresent()) || txContainer.get().getNodeId() == null) {
94 final List<? extends TransactionAware> cachedNotifs = txContainer.get().getNotifications();
95 for (final TransactionAware notif : cachedNotifs) {
96 if (notif instanceof FlowTableStatisticsUpdate) {
97 final List<FlowTableAndStatisticsMap> statNotif =
98 ((FlowTableStatisticsUpdate) notif).getFlowTableAndStatisticsMap();
99 if (statNotif != null) {
100 tableStats.addAll(statNotif);
104 /* write stat to trans */
105 statTableCommit(tableStats, nodeIdent, trans);
106 /* Notification for continue collecting statistics - Tables statistics are still same size
107 * and they are small - don't need to wait to whole apply operation */
108 notifyToCollectNextStatistics(nodeIdent);
113 private void statTableCommit(final List<FlowTableAndStatisticsMap> tableStats, final InstanceIdentifier<Node> nodeIdent,
114 final ReadWriteTransaction trans) {
115 final InstanceIdentifier<FlowCapableNode> fNodeIdent = nodeIdent.augmentation(FlowCapableNode.class);
116 /* check flow Capable Node and write statistics */
117 Optional<FlowCapableNode> fNode = Optional.absent();
119 fNode = trans.read(LogicalDatastoreType.OPERATIONAL, fNodeIdent).checkedGet();
121 catch (final ReadFailedException e) {
122 LOG.debug("Read Operational/DS for FlowCapableNode fail! {}", fNodeIdent, e);
125 if ( ! fNode.isPresent()) {
126 LOG.trace("Read Operational/DS for FlowCapableNode fail! Node {} doesn't exist.", fNodeIdent);
129 for (final FlowTableAndStatisticsMap tableStat : tableStats) {
130 final InstanceIdentifier<Table> tableIdent = fNodeIdent
131 .child(Table.class, new TableKey(tableStat.getTableId().getValue()));
132 final Table table = new TableBuilder().setId(tableStat.getTableId().getValue()).build();
133 trans.merge(LogicalDatastoreType.OPERATIONAL, tableIdent, table);
134 final InstanceIdentifier<FlowTableStatisticsData> tableStatIdent = tableIdent
135 .augmentation(FlowTableStatisticsData.class);
136 trans.merge(LogicalDatastoreType.OPERATIONAL, tableStatIdent, new FlowTableStatisticsDataBuilder().build());
138 final FlowTableStatistics stats = new FlowTableStatisticsBuilder(tableStat).build();
139 final InstanceIdentifier<FlowTableStatistics> tStatIdent = tableStatIdent.child(FlowTableStatistics.class);
140 trans.put(LogicalDatastoreType.OPERATIONAL, tStatIdent, stats);