Merge "Fix for CLI- AddMDFlow having problem"
[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.group.desc.GroupDescBuilder;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.statistics.rev131111.group.features.GroupFeaturesBuilder;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.statistics.rev131111.group.statistics.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.putOperationalData(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.putOperationalData(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.putOperationalData(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.putOperationalData(refValue, nodeData.build());
185         it.commit();
186
187 //        for (GroupStats groupstat : notification.getGroupStats()) {
188 //        
189 //            GroupStatsKey groupKey = groupstat.getKey();
190 //            InstanceIdentifier<? extends Object> id = InstanceIdentifier.builder(Nodes.class).child(Node.class, key).augmentation(NodeGroupStatistics.class).child(GroupStatistics.class).child(GroupStats.class,groupKey).toInstance();
191 //            it.putOperationalData(id, groupstat);
192 //            it.commit();
193 //        }
194     }
195     
196     @Override
197     public void onMeterFeaturesUpdated(MeterFeaturesUpdated notification) {
198
199         //Add statistics to local cache
200         ConcurrentMap<NodeId, NodeStatistics> cache = this.statisticsManager.getStatisticsCache();
201         if(!cache.containsKey(notification.getId())){
202             cache.put(notification.getId(), new NodeStatistics());
203         }
204         MeterFeaturesBuilder meterFeature = new MeterFeaturesBuilder();
205         meterFeature.setMeterBandSupported(notification.getMeterBandSupported());
206         meterFeature.setMeterCapabilitiesSupported(notification.getMeterCapabilitiesSupported());
207         meterFeature.setMaxBands(notification.getMaxBands());
208         meterFeature.setMaxColor(notification.getMaxColor());
209         meterFeature.setMaxMeter(notification.getMaxMeter());
210         
211         cache.get(notification.getId()).setMeterFeatures(meterFeature.build());
212         
213         //Publish data to configuration data store
214         DataModificationTransaction it = this.statisticsManager.startChange();
215         NodeKey key = new NodeKey(notification.getId());
216         NodeRef ref = getNodeRef(key);
217         
218         final NodeBuilder nodeData = new NodeBuilder(); 
219         nodeData.setKey(key);
220         
221         NodeMeterFeaturesBuilder nodeMeterFeatures= new NodeMeterFeaturesBuilder();
222         nodeMeterFeatures.setMeterFeatures(meterFeature.build());
223         
224         //Update augmented data
225         nodeData.addAugmentation(NodeMeterFeatures.class, nodeMeterFeatures.build());
226         
227         InstanceIdentifier<? extends Object> refValue = ref.getValue();
228         it.putOperationalData(refValue, nodeData.build());
229         it.commit();
230     }
231     
232     @Override
233     public void onGroupFeaturesUpdated(GroupFeaturesUpdated notification) {
234
235         //Add statistics to local cache
236         ConcurrentMap<NodeId, NodeStatistics> cache = this.statisticsManager.getStatisticsCache();
237         if(!cache.containsKey(notification.getId())){
238             cache.put(notification.getId(), new NodeStatistics());
239         }
240         
241         GroupFeaturesBuilder groupFeatures = new GroupFeaturesBuilder();
242         groupFeatures.setActions(notification.getActions());
243         groupFeatures.setGroupCapabilitiesSupported(notification.getGroupCapabilitiesSupported());
244         groupFeatures.setGroupTypesSupported(notification.getGroupTypesSupported());
245         groupFeatures.setMaxGroups(notification.getMaxGroups());
246         cache.get(notification.getId()).setGroupFeatures(groupFeatures.build());
247         
248         //Publish data to configuration data store
249         DataModificationTransaction it = this.statisticsManager.startChange();
250         NodeKey key = new NodeKey(notification.getId());
251         NodeRef ref = getNodeRef(key);
252         
253         final NodeBuilder nodeData = new NodeBuilder(); 
254         nodeData.setKey(key);
255         
256         NodeGroupFeaturesBuilder nodeGroupFeatures= new NodeGroupFeaturesBuilder();
257         nodeGroupFeatures.setGroupFeatures(groupFeatures.build());
258         
259         //Update augmented data
260         nodeData.addAugmentation(NodeGroupFeatures.class, nodeGroupFeatures.build());
261         
262         InstanceIdentifier<? extends Object> refValue = ref.getValue();
263         it.putOperationalData(refValue, nodeData.build());
264         it.commit();
265     }
266
267     private NodeRef getNodeRef(NodeKey nodeKey){
268         InstanceIdentifierBuilder<?> builder = InstanceIdentifier.builder(Nodes.class).child(Node.class, nodeKey);
269         return new NodeRef(builder.toInstance());
270     }
271
272 }