Merge changes Ia09a1107,I2c30f3b6
[controller.git] / opendaylight / md-sal / statistics-manager / src / main / java / org / opendaylight / controller / md / statistics / manager / StatisticsUpdateCommiter.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.concurrent.ConcurrentMap;
11
12 import org.opendaylight.controller.sal.binding.api.data.DataModificationTransaction;
13 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.statistics.rev131111.GroupDescStatsUpdated;
14 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.statistics.rev131111.GroupFeaturesUpdated;
15 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.statistics.rev131111.GroupStatisticsUpdated;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.statistics.rev131111.NodeGroupDescStats;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.statistics.rev131111.NodeGroupDescStatsBuilder;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.statistics.rev131111.NodeGroupFeatures;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.statistics.rev131111.NodeGroupFeaturesBuilder;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.statistics.rev131111.NodeGroupStatistics;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.statistics.rev131111.NodeGroupStatisticsBuilder;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.statistics.rev131111.OpendaylightGroupStatisticsListener;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.statistics.rev131111.nodes.node.GroupDescBuilder;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.statistics.rev131111.nodes.node.GroupFeaturesBuilder;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.statistics.rev131111.nodes.node.GroupStatisticsBuilder;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeRef;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeBuilder;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.MeterConfigStatsUpdated;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.MeterFeaturesUpdated;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.MeterStatisticsUpdated;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.NodeMeterConfigStats;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.NodeMeterConfigStatsBuilder;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.NodeMeterFeatures;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.NodeMeterFeaturesBuilder;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.NodeMeterStatistics;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.NodeMeterStatisticsBuilder;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.OpendaylightMeterStatisticsListener;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.nodes.node.MeterConfigStatsBuilder;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.nodes.node.MeterFeaturesBuilder;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.nodes.node.MeterStatisticsBuilder;
45 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
46 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier.InstanceIdentifierBuilder;
47
48 public class StatisticsUpdateCommiter implements OpendaylightGroupStatisticsListener,
49         OpendaylightMeterStatisticsListener {
50     
51     private final StatisticsProvider statisticsManager;
52
53     public StatisticsUpdateCommiter(final StatisticsProvider manager){
54
55         this.statisticsManager = manager;
56     }
57     
58     public StatisticsProvider getStatisticsManager(){
59         return statisticsManager;
60     }
61    
62     @Override
63     public void onMeterConfigStatsUpdated(MeterConfigStatsUpdated notification) {
64
65         //Add statistics to local cache
66         ConcurrentMap<NodeId, NodeStatistics> cache = this.statisticsManager.getStatisticsCache();
67         if(!cache.containsKey(notification.getId())){
68             cache.put(notification.getId(), new NodeStatistics());
69         }
70         cache.get(notification.getId()).setMeterConfigStats(notification.getMeterConfigStats());
71         
72         //Publish data to configuration data store
73         DataModificationTransaction it = this.statisticsManager.startChange();
74         NodeKey key = new NodeKey(notification.getId());
75         NodeRef ref = getNodeRef(key);
76         
77         final NodeBuilder nodeData = new NodeBuilder(); 
78         nodeData.setKey(key);
79         
80         NodeMeterConfigStatsBuilder meterConfig= new NodeMeterConfigStatsBuilder();
81         MeterConfigStatsBuilder stats = new MeterConfigStatsBuilder();
82         stats.setMeterConfigStats(notification.getMeterConfigStats());
83         meterConfig.setMeterConfigStats(stats.build());
84         
85         //Update augmented data
86         nodeData.addAugmentation(NodeMeterConfigStats.class, meterConfig.build());
87         
88         InstanceIdentifier<? extends Object> refValue = ref.getValue();
89         it.putRuntimeData(refValue, nodeData.build());
90         it.commit();
91
92     }
93
94     @Override
95     public void onMeterStatisticsUpdated(MeterStatisticsUpdated notification) {
96         //Add statistics to local cache
97         ConcurrentMap<NodeId, NodeStatistics> cache = this.statisticsManager.getStatisticsCache();
98         if(!cache.containsKey(notification.getId())){
99             cache.put(notification.getId(), new NodeStatistics());
100         }
101         cache.get(notification.getId()).setMeterStatistics(notification.getMeterStats());
102         
103         //Publish data to configuration data store
104         DataModificationTransaction it = this.statisticsManager.startChange();
105         NodeKey key = new NodeKey(notification.getId());
106         NodeRef ref = getNodeRef(key);
107         
108         final NodeBuilder nodeData = new NodeBuilder(); 
109         nodeData.setKey(key);
110         
111         NodeMeterStatisticsBuilder meterStats= new NodeMeterStatisticsBuilder();
112         MeterStatisticsBuilder stats = new MeterStatisticsBuilder();
113         stats.setMeterStats(notification.getMeterStats());
114         meterStats.setMeterStatistics(stats.build());
115         
116         //Update augmented data
117         nodeData.addAugmentation(NodeMeterStatistics.class, meterStats.build());
118         
119         InstanceIdentifier<? extends Object> refValue = ref.getValue();
120         it.putRuntimeData(refValue, nodeData.build());
121         it.commit();
122
123     }
124
125     @Override
126     public void onGroupDescStatsUpdated(GroupDescStatsUpdated notification) {
127         //Add statistics to local cache
128         ConcurrentMap<NodeId, NodeStatistics> cache = this.statisticsManager.getStatisticsCache();
129         if(!cache.containsKey(notification.getId())){
130             cache.put(notification.getId(), new NodeStatistics());
131         }
132         cache.get(notification.getId()).setGroupDescStats(notification.getGroupDescStats());
133         
134         //Publish data to configuration data store
135         DataModificationTransaction it = this.statisticsManager.startChange();
136         NodeKey key = new NodeKey(notification.getId());
137         NodeRef ref = getNodeRef(key);
138         
139         final NodeBuilder nodeData = new NodeBuilder(); 
140         nodeData.setKey(key);
141         
142         NodeGroupDescStatsBuilder groupDesc= new NodeGroupDescStatsBuilder();
143         GroupDescBuilder stats = new GroupDescBuilder();
144         stats.setGroupDescStats(notification.getGroupDescStats());
145         groupDesc.setGroupDesc(stats.build());
146         
147         //Update augmented data
148         nodeData.addAugmentation(NodeGroupDescStats.class, groupDesc.build());
149         
150         InstanceIdentifier<? extends Object> refValue = ref.getValue();
151         it.putRuntimeData(refValue, nodeData.build());
152         it.commit();
153
154     }
155
156     @Override
157     public void onGroupStatisticsUpdated(GroupStatisticsUpdated notification) {
158         
159         //Add statistics to local cache
160         ConcurrentMap<NodeId, NodeStatistics> cache = this.statisticsManager.getStatisticsCache();
161         if(!cache.containsKey(notification.getId())){
162             cache.put(notification.getId(), new NodeStatistics());
163         }
164         cache.get(notification.getId()).setGroupStatistics(notification.getGroupStats());
165         
166         //Publish data to configuration data store
167         
168         DataModificationTransaction it = this.statisticsManager.startChange();
169         NodeKey key = new NodeKey(notification.getId());
170         NodeRef ref = getNodeRef(key);
171         
172         final NodeBuilder nodeData = new NodeBuilder(); 
173         nodeData.setKey(key);
174         
175         NodeGroupStatisticsBuilder groupStats = new NodeGroupStatisticsBuilder();
176         GroupStatisticsBuilder stats = new GroupStatisticsBuilder();
177         stats.setGroupStats(notification.getGroupStats());
178         groupStats.setGroupStatistics(stats.build());
179         
180         //Update augmented data
181         nodeData.addAugmentation(NodeGroupStatistics.class, groupStats.build());
182         
183         InstanceIdentifier<? extends Object> refValue = ref.getValue();
184         it.putRuntimeData(refValue, nodeData.build());
185         it.commit();
186     }
187     
188     @Override
189     public void onMeterFeaturesUpdated(MeterFeaturesUpdated notification) {
190
191         //Add statistics to local cache
192         ConcurrentMap<NodeId, NodeStatistics> cache = this.statisticsManager.getStatisticsCache();
193         if(!cache.containsKey(notification.getId())){
194             cache.put(notification.getId(), new NodeStatistics());
195         }
196         MeterFeaturesBuilder meterFeature = new MeterFeaturesBuilder();
197         meterFeature.setBandTypes(notification.getBandTypes());
198         meterFeature.setCapabilities(notification.getCapabilities());
199         meterFeature.setMaxBands(notification.getMaxBands());
200         meterFeature.setMaxColor(notification.getMaxColor());
201         meterFeature.setMaxMeter(notification.getMaxMeter());
202         
203         cache.get(notification.getId()).setMeterFeatures(meterFeature.build());
204         
205         //Publish data to configuration data store
206         DataModificationTransaction it = this.statisticsManager.startChange();
207         NodeKey key = new NodeKey(notification.getId());
208         NodeRef ref = getNodeRef(key);
209         
210         final NodeBuilder nodeData = new NodeBuilder(); 
211         nodeData.setKey(key);
212         
213         NodeMeterFeaturesBuilder nodeMeterFeatures= new NodeMeterFeaturesBuilder();
214         nodeMeterFeatures.setMeterFeatures(meterFeature.build());
215         
216         //Update augmented data
217         nodeData.addAugmentation(NodeMeterFeatures.class, nodeMeterFeatures.build());
218         
219         InstanceIdentifier<? extends Object> refValue = ref.getValue();
220         it.putRuntimeData(refValue, nodeData.build());
221         it.commit();
222     }
223     
224     @Override
225     public void onGroupFeaturesUpdated(GroupFeaturesUpdated notification) {
226         //Add statistics to local cache
227         ConcurrentMap<NodeId, NodeStatistics> cache = this.statisticsManager.getStatisticsCache();
228         if(!cache.containsKey(notification.getId())){
229             cache.put(notification.getId(), new NodeStatistics());
230         }
231         
232         GroupFeaturesBuilder groupFeatures = new GroupFeaturesBuilder();
233         groupFeatures.setActions(notification.getActions());
234         groupFeatures.setCapabilities(notification.getCapabilities());
235         groupFeatures.setTypes(notification.getTypes());
236         groupFeatures.setMaxGroups(notification.getMaxGroups());
237         cache.get(notification.getId()).setGroupFeatures(groupFeatures.build());
238         
239         //Publish data to configuration data store
240         DataModificationTransaction it = this.statisticsManager.startChange();
241         NodeKey key = new NodeKey(notification.getId());
242         NodeRef ref = getNodeRef(key);
243         
244         final NodeBuilder nodeData = new NodeBuilder(); 
245         nodeData.setKey(key);
246         
247         NodeGroupFeaturesBuilder nodeGroupFeatures= new NodeGroupFeaturesBuilder();
248         nodeGroupFeatures.setGroupFeatures(groupFeatures.build());
249         
250         //Update augmented data
251         nodeData.addAugmentation(NodeGroupFeatures.class, nodeGroupFeatures.build());
252         
253         InstanceIdentifier<? extends Object> refValue = ref.getValue();
254         it.putRuntimeData(refValue, nodeData.build());
255         it.commit();
256     }
257
258     private NodeRef getNodeRef(NodeKey nodeKey){
259         InstanceIdentifierBuilder<?> builder = InstanceIdentifier.builder().node(Nodes.class);
260         return new NodeRef(builder.node(Node.class,nodeKey).toInstance());
261     }
262
263 }