Merge "Bug fixes for netconf southbound plugin."
[controller.git] / opendaylight / md-sal / statistics-manager / src / main / java / org / opendaylight / controller / md / statistics / manager / StatisticsUpdateHandler.java
1 /*
2  * Copyright IBM Corporation, 2013.  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 package org.opendaylight.controller.md.statistics.manager;
9
10 import java.util.Map;
11 import java.util.Set;
12 import java.util.concurrent.ExecutionException;
13
14 import org.opendaylight.controller.md.sal.common.api.data.DataChangeEvent;
15 import org.opendaylight.controller.sal.binding.api.data.DataChangeListener;
16 import org.opendaylight.controller.sal.binding.api.data.DataModificationTransaction;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.meters.Meter;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.Flow;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.FlowStatisticsData;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.port.rev130925.queues.Queue;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.statistics.rev131111.NodeGroupDescStats;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.statistics.rev131111.NodeGroupStatistics;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.groups.Group;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.node.NodeConnector;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.node.NodeConnectorKey;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.NodeMeterConfigStats;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.NodeMeterStatistics;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.queue.statistics.rev131216.FlowCapableNodeConnectorQueueStatisticsData;
31 import org.opendaylight.yangtools.yang.binding.DataObject;
32 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
35
36 /**
37  * Following are two main responsibilities of the class
38  * 1) Listen for the create changes in config data store for tree nodes (Flow,Group,Meter,Queue)
39  * and send statistics request to the switch to fetch the statistics
40  *
41  * 2)Listen for the remove changes in config data store for tree nodes (Flow,Group,Meter,Queue)
42  * and remove the relative statistics data from operational data store.
43  *
44  * @author avishnoi@in.ibm.com
45  *
46  */
47 public class StatisticsUpdateHandler implements DataChangeListener {
48
49     private static final Logger suhLogger = LoggerFactory.getLogger(StatisticsUpdateHandler.class);
50     private final StatisticsProvider statisticsManager;
51
52     public StatisticsUpdateHandler(final StatisticsProvider manager){
53         this.statisticsManager = manager;
54     }
55
56     @SuppressWarnings("unchecked")
57     @Override
58     public void onDataChanged(DataChangeEvent<InstanceIdentifier<?>, DataObject> change) {
59         Map<InstanceIdentifier<?>, DataObject> additions = change.getCreatedConfigurationData();
60         for (InstanceIdentifier<? extends DataObject> dataObjectInstance : additions.keySet()) {
61             DataObject dataObject = additions.get(dataObjectInstance);
62             NodeKey nodeII = dataObjectInstance.firstKeyOf(Node.class, NodeKey.class);
63             if(dataObject instanceof Flow){
64                 Flow flow = (Flow) dataObject;
65                 try {
66                     this.statisticsManager.sendFlowStatsFromTableRequest(nodeII, flow);
67                 } catch (InterruptedException | ExecutionException e) {
68                     suhLogger.warn("Following exception occured while sending flow statistics request newly added flow: {}", e);
69                 }
70             }
71             if(dataObject instanceof Meter){
72                 try {
73                     this.statisticsManager.sendMeterConfigStatisticsRequest(nodeII);
74                 } catch (InterruptedException | ExecutionException e) {
75                     suhLogger.warn("Following exception occured while sending meter statistics request for newly added meter: {}", e);
76                 }
77             }
78             if(dataObject instanceof Group){
79                 try {
80                     this.statisticsManager.sendGroupDescriptionRequest(nodeII);
81                 } catch (InterruptedException | ExecutionException e) {
82                     suhLogger.warn("Following exception occured while sending group description request for newly added group: {}", e);
83                 }
84             }
85             if(dataObject instanceof Queue){
86                 Queue queue = (Queue) dataObject;
87                 InstanceIdentifier<NodeConnector> nodeConnectorII = dataObjectInstance.firstIdentifierOf(NodeConnector.class);
88                 NodeConnectorKey nodeConnectorKey = InstanceIdentifier.keyOf(nodeConnectorII);
89                 try {
90                     this.statisticsManager.sendQueueStatsFromGivenNodeConnector(nodeII,
91                             nodeConnectorKey.getId(), queue.getQueueId());
92                 } catch (InterruptedException | ExecutionException e) {
93                     suhLogger.warn("Following exception occured while sending queue statistics request for newly added group: {}", e);
94                 }
95             }
96         }
97
98         DataModificationTransaction it = this.statisticsManager.startChange();
99         Set<InstanceIdentifier<? extends DataObject>> removals = change.getRemovedConfigurationData();
100         for (InstanceIdentifier<? extends DataObject> dataObjectInstance : removals) {
101             DataObject dataObject = change.getOriginalConfigurationData().get(dataObjectInstance);
102
103             if(dataObject instanceof Flow){
104                 InstanceIdentifier<Flow> flowII = (InstanceIdentifier<Flow>)dataObjectInstance;
105                 InstanceIdentifier<?> flowAugmentation =
106                         InstanceIdentifier.builder(flowII).augmentation(FlowStatisticsData.class).toInstance();
107                 it.removeOperationalData(flowAugmentation);
108             }
109             if(dataObject instanceof Meter){
110                 InstanceIdentifier<Meter> meterII = (InstanceIdentifier<Meter>)dataObjectInstance;
111
112                 InstanceIdentifier<?> nodeMeterConfigStatsAugmentation =
113                         InstanceIdentifier.builder(meterII).augmentation(NodeMeterConfigStats.class).toInstance();
114                 it.removeOperationalData(nodeMeterConfigStatsAugmentation);
115
116                 InstanceIdentifier<?> nodeMeterStatisticsAugmentation =
117                         InstanceIdentifier.builder(meterII).augmentation(NodeMeterStatistics.class).toInstance();
118                 it.removeOperationalData(nodeMeterStatisticsAugmentation);
119             }
120
121             if(dataObject instanceof Group){
122                 InstanceIdentifier<Group> groupII = (InstanceIdentifier<Group>)dataObjectInstance;
123
124                 InstanceIdentifier<?> nodeGroupDescStatsAugmentation =
125                         InstanceIdentifier.builder(groupII).augmentation(NodeGroupDescStats.class).toInstance();
126                 it.removeOperationalData(nodeGroupDescStatsAugmentation);
127
128                 InstanceIdentifier<?> nodeGroupStatisticsAugmentation =
129                         InstanceIdentifier.builder(groupII).augmentation(NodeGroupStatistics.class).toInstance();
130                 it.removeOperationalData(nodeGroupStatisticsAugmentation);
131             }
132
133             if(dataObject instanceof Queue){
134                 InstanceIdentifier<Queue> queueII = (InstanceIdentifier<Queue>)dataObjectInstance;
135
136                 InstanceIdentifier<?> nodeConnectorQueueStatisticsDataAugmentation =
137                         InstanceIdentifier.builder(queueII).augmentation(FlowCapableNodeConnectorQueueStatisticsData.class).toInstance();
138                 it.removeOperationalData(nodeConnectorQueueStatisticsDataAugmentation);
139             }
140         }
141         it.commit();
142     }
143 }