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