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.openflowplugin.applications.statistics.manager.impl;
11 import java.util.ArrayList;
12 import java.util.List;
14 import java.util.UUID;
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.sal.binding.api.NotificationProviderService;
19 import org.opendaylight.openflowplugin.applications.statistics.manager.StatNodeRegistration;
20 import org.opendaylight.openflowplugin.applications.statistics.manager.StatRpcMsgManager.TransactionCacheContainer;
21 import org.opendaylight.openflowplugin.applications.statistics.manager.StatisticsManager;
22 import org.opendaylight.openflowplugin.applications.statistics.manager.StatisticsManager.StatDataStoreOperation;
23 import org.opendaylight.openflowplugin.applications.statistics.manager.StatisticsManager.StatDataStoreOperation.StatsManagerOperationType;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.Table;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.TableBuilder;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.TableKey;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.table.statistics.rev131215.FlowTableStatisticsData;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.table.statistics.rev131215.FlowTableStatisticsDataBuilder;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.table.statistics.rev131215.FlowTableStatisticsUpdate;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.table.statistics.rev131215.OpendaylightFlowTableStatisticsListener;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.table.statistics.rev131215.flow.table.and.statistics.map.FlowTableAndStatisticsMap;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.table.statistics.rev131215.flow.table.statistics.FlowTableStatistics;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.table.statistics.rev131215.flow.table.statistics.FlowTableStatisticsBuilder;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.transaction.rev150304.TransactionAware;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.transaction.rev150304.TransactionId;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
41 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
42 import org.slf4j.Logger;
43 import org.slf4j.LoggerFactory;
45 import com.google.common.base.Optional;
49 * org.opendaylight.openflowplugin.applications.statistics.manager.impl
51 * StatNotifyCommitTable
52 * Class is a NotifyListener for TableStatistics
53 * All expected (registered) tableStatistics will be builded and
54 * commit to Operational/DataStore
56 * @author <a href="mailto:vdemcak@cisco.com">Vaclav Demcak</a>
59 public class StatNotifyCommitTable extends StatAbstractNotifyCommit<OpendaylightFlowTableStatisticsListener>
60 implements OpendaylightFlowTableStatisticsListener {
62 private static final Logger LOG = LoggerFactory.getLogger(StatNotifyCommitTable.class);
64 public StatNotifyCommitTable(final StatisticsManager manager,
65 final NotificationProviderService nps,
66 final StatNodeRegistration nrm) {
67 super(manager, nps, nrm);
71 protected OpendaylightFlowTableStatisticsListener getStatNotificationListener() {
76 public void onFlowTableStatisticsUpdate(final FlowTableStatisticsUpdate notification) {
77 final TransactionId transId = notification.getTransactionId();
78 final NodeId nodeId = notification.getId();
79 if ( ! isExpectedStatistics(transId, nodeId)) {
80 LOG.debug("STAT-MANAGER - FlowTableStatisticsUpdate: unregistred notification detect TransactionId {}", transId);
83 manager.getRpcMsgManager().addNotification(notification, nodeId);
84 if (notification.isMoreReplies()) {
87 /* Don't block RPC Notification thread */
88 manager.enqueue(new StatDataStoreOperation(StatsManagerOperationType.DATA_COMMIT_OPER_DS,nodeId) {
90 public void applyOperation(final ReadWriteTransaction trans) {
91 final List<FlowTableAndStatisticsMap> tableStats = new ArrayList<FlowTableAndStatisticsMap>(10);
92 final Optional<TransactionCacheContainer<?>> txContainer = getTransactionCacheContainer(transId, nodeId);
93 final InstanceIdentifier<Node> nodeIdent = InstanceIdentifier.create(Nodes.class)
94 .child(Node.class, new NodeKey(nodeId));
95 if (( ! txContainer.isPresent()) || txContainer.get().getNodeId() == null) {
99 if(!nodeRegistrationManager.isFlowCapableNodeOwner(nodeId)) { return; }
101 final List<? extends TransactionAware> cachedNotifs = txContainer.get().getNotifications();
102 for (final TransactionAware notif : cachedNotifs) {
103 if (notif instanceof FlowTableStatisticsUpdate) {
104 final List<FlowTableAndStatisticsMap> statNotif =
105 ((FlowTableStatisticsUpdate) notif).getFlowTableAndStatisticsMap();
106 if (statNotif != null) {
107 tableStats.addAll(statNotif);
111 /* write stat to trans */
112 statTableCommit(tableStats, nodeIdent, trans);
113 /* Notification for continue collecting statistics - Tables statistics are still same size
114 * and they are small - don't need to wait to whole apply operation */
115 notifyToCollectNextStatistics(nodeIdent, transId);
119 public UUID generatedUUIDForNode() {
120 return manager.getGeneratedUUIDForNode(getNodeIdentifier());
125 private void statTableCommit(final List<FlowTableAndStatisticsMap> tableStats, final InstanceIdentifier<Node> nodeIdent,
126 final ReadWriteTransaction trans) {
127 final InstanceIdentifier<FlowCapableNode> fNodeIdent = nodeIdent.augmentation(FlowCapableNode.class);
128 /* check flow Capable Node and write statistics */
129 Optional<FlowCapableNode> fNode = Optional.absent();
131 fNode = trans.read(LogicalDatastoreType.OPERATIONAL, fNodeIdent).checkedGet();
133 catch (final ReadFailedException e) {
134 LOG.debug("Read Operational/DS for FlowCapableNode fail! {}", fNodeIdent, e);
137 if ( ! fNode.isPresent()) {
138 LOG.trace("Read Operational/DS for FlowCapableNode fail! Node {} doesn't exist.", fNodeIdent);
141 for (final FlowTableAndStatisticsMap tableStat : tableStats) {
142 final InstanceIdentifier<Table> tableIdent = fNodeIdent
143 .child(Table.class, new TableKey(tableStat.getTableId().getValue()));
144 final Table table = new TableBuilder().setId(tableStat.getTableId().getValue()).build();
145 trans.merge(LogicalDatastoreType.OPERATIONAL, tableIdent, table);
146 final InstanceIdentifier<FlowTableStatisticsData> tableStatIdent = tableIdent
147 .augmentation(FlowTableStatisticsData.class);
148 trans.merge(LogicalDatastoreType.OPERATIONAL, tableStatIdent, new FlowTableStatisticsDataBuilder().build());
150 final FlowTableStatistics stats = new FlowTableStatisticsBuilder(tableStat).build();
151 final InstanceIdentifier<FlowTableStatistics> tStatIdent = tableStatIdent.child(FlowTableStatistics.class);
152 trans.put(LogicalDatastoreType.OPERATIONAL, tStatIdent, stats);