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