Merge changes I1474351f,I2ddc5ffa
[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.HashMap;
11 import java.util.List;
12 import java.util.concurrent.ConcurrentMap;
13
14 import org.opendaylight.controller.sal.binding.api.data.DataModificationTransaction;
15 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNodeConnector;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowId;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.Table;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.TableBuilder;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.TableKey;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.Flow;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.FlowBuilder;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.FlowKey;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.AggregateFlowStatisticsData;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.AggregateFlowStatisticsDataBuilder;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.AggregateFlowStatisticsUpdate;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.FlowStatisticsData;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.FlowStatisticsDataBuilder;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.FlowStatisticsUpdated;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.FlowTableStatisticsUpdated;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.FlowsStatisticsUpdate;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.NodeConnectorStatisticsUpdated;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.OpendaylightFlowStatisticsListener;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.aggregate.flow.statistics.AggregateFlowStatisticsBuilder;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.flow.and.statistics.map.list.FlowAndStatisticsMapList;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.flow.and.statistics.map.list.FlowAndStatisticsMapListBuilder;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.flow.statistics.FlowStatisticsBuilder;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.table.statistics.rev131215.FlowTableStatisticsData;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.table.statistics.rev131215.FlowTableStatisticsDataBuilder;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.table.statistics.rev131215.FlowTableStatisticsUpdate;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.table.statistics.rev131215.OpendaylightFlowTableStatisticsListener;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.table.statistics.rev131215.flow.table.and.statistics.map.FlowTableAndStatisticsMap;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.table.statistics.rev131215.flow.table.statistics.FlowTableStatisticsBuilder;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.port.rev130925.queues.Queue;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.port.rev130925.queues.QueueBuilder;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.port.rev130925.queues.QueueKey;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.queue.rev130925.QueueId;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.statistics.rev131111.GroupDescStatsUpdated;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.statistics.rev131111.GroupFeaturesUpdated;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.statistics.rev131111.GroupStatisticsUpdated;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.statistics.rev131111.NodeGroupDescStats;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.statistics.rev131111.NodeGroupDescStatsBuilder;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.statistics.rev131111.NodeGroupFeatures;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.statistics.rev131111.NodeGroupFeaturesBuilder;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.statistics.rev131111.NodeGroupStatistics;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.statistics.rev131111.NodeGroupStatisticsBuilder;
57 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.statistics.rev131111.OpendaylightGroupStatisticsListener;
58 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.statistics.rev131111.group.desc.GroupDescBuilder;
59 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.statistics.rev131111.group.features.GroupFeaturesBuilder;
60 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.statistics.rev131111.group.statistics.GroupStatisticsBuilder;
61 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
62 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeRef;
63 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
64 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.node.NodeConnector;
65 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.node.NodeConnectorBuilder;
66 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.node.NodeConnectorKey;
67 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
68 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeBuilder;
69 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
70 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.MeterConfigStatsUpdated;
71 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.MeterFeaturesUpdated;
72 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.MeterStatisticsUpdated;
73 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.NodeMeterConfigStats;
74 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.NodeMeterConfigStatsBuilder;
75 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.NodeMeterFeatures;
76 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.NodeMeterFeaturesBuilder;
77 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.NodeMeterStatistics;
78 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.NodeMeterStatisticsBuilder;
79 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.OpendaylightMeterStatisticsListener;
80 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.nodes.node.MeterConfigStatsBuilder;
81 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.nodes.node.MeterFeaturesBuilder;
82 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.nodes.node.MeterStatisticsBuilder;
83 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.statistics.types.rev130925.GenericQueueStatistics;
84 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.statistics.types.rev130925.GenericStatistics;
85 import org.opendaylight.yang.gen.v1.urn.opendaylight.port.statistics.rev131214.FlowCapableNodeConnectorStatisticsData;
86 import org.opendaylight.yang.gen.v1.urn.opendaylight.port.statistics.rev131214.FlowCapableNodeConnectorStatisticsDataBuilder;
87 import org.opendaylight.yang.gen.v1.urn.opendaylight.port.statistics.rev131214.OpendaylightPortStatisticsListener;
88 import org.opendaylight.yang.gen.v1.urn.opendaylight.port.statistics.rev131214.PortStatisticsUpdate;
89 import org.opendaylight.yang.gen.v1.urn.opendaylight.port.statistics.rev131214.flow.capable.node.connector.statistics.FlowCapableNodeConnectorStatisticsBuilder;
90 import org.opendaylight.yang.gen.v1.urn.opendaylight.port.statistics.rev131214.node.connector.statistics.and.port.number.map.NodeConnectorStatisticsAndPortNumberMap;
91 import org.opendaylight.yang.gen.v1.urn.opendaylight.queue.statistics.rev131216.FlowCapableNodeConnectorQueueStatisticsData;
92 import org.opendaylight.yang.gen.v1.urn.opendaylight.queue.statistics.rev131216.FlowCapableNodeConnectorQueueStatisticsDataBuilder;
93 import org.opendaylight.yang.gen.v1.urn.opendaylight.queue.statistics.rev131216.OpendaylightQueueStatisticsListener;
94 import org.opendaylight.yang.gen.v1.urn.opendaylight.queue.statistics.rev131216.QueueStatisticsUpdate;
95 import org.opendaylight.yang.gen.v1.urn.opendaylight.queue.statistics.rev131216.flow.capable.node.connector.queue.statistics.FlowCapableNodeConnectorQueueStatisticsBuilder;
96 import org.opendaylight.yang.gen.v1.urn.opendaylight.queue.statistics.rev131216.queue.id.and.statistics.map.QueueIdAndStatisticsMap;
97 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
98 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier.InstanceIdentifierBuilder;
99 import org.slf4j.Logger;
100 import org.slf4j.LoggerFactory;
101
102 public class StatisticsUpdateCommiter implements OpendaylightGroupStatisticsListener,
103         OpendaylightMeterStatisticsListener, 
104         OpendaylightFlowStatisticsListener,
105         OpendaylightPortStatisticsListener,
106         OpendaylightFlowTableStatisticsListener,
107         OpendaylightQueueStatisticsListener{
108     
109     public final static Logger sucLogger = LoggerFactory.getLogger(StatisticsUpdateCommiter.class);
110
111     private final StatisticsProvider statisticsManager;
112     
113     private final int unaccountedFlowsCounter = 1;
114
115     public StatisticsUpdateCommiter(final StatisticsProvider manager){
116
117         this.statisticsManager = manager;
118     }
119     
120     public StatisticsProvider getStatisticsManager(){
121         return statisticsManager;
122     }
123    
124     @Override
125     public void onMeterConfigStatsUpdated(MeterConfigStatsUpdated notification) {
126
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()).setMeterConfigStats(notification.getMeterConfigStats());
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         NodeMeterConfigStatsBuilder meterConfig= new NodeMeterConfigStatsBuilder();
143         MeterConfigStatsBuilder stats = new MeterConfigStatsBuilder();
144         stats.setMeterConfigStats(notification.getMeterConfigStats());
145         meterConfig.setMeterConfigStats(stats.build());
146         
147         //Update augmented data
148         nodeData.addAugmentation(NodeMeterConfigStats.class, meterConfig.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 onMeterStatisticsUpdated(MeterStatisticsUpdated notification) {
158         //Add statistics to local cache
159         ConcurrentMap<NodeId, NodeStatistics> cache = this.statisticsManager.getStatisticsCache();
160         if(!cache.containsKey(notification.getId())){
161             cache.put(notification.getId(), new NodeStatistics());
162         }
163         cache.get(notification.getId()).setMeterStatistics(notification.getMeterStats());
164         
165         //Publish data to configuration data store
166         DataModificationTransaction it = this.statisticsManager.startChange();
167         NodeKey key = new NodeKey(notification.getId());
168         NodeRef ref = getNodeRef(key);
169         
170         final NodeBuilder nodeData = new NodeBuilder(); 
171         nodeData.setKey(key);
172         
173         NodeMeterStatisticsBuilder meterStats= new NodeMeterStatisticsBuilder();
174         MeterStatisticsBuilder stats = new MeterStatisticsBuilder();
175         stats.setMeterStats(notification.getMeterStats());
176         meterStats.setMeterStatistics(stats.build());
177         
178         //Update augmented data
179         nodeData.addAugmentation(NodeMeterStatistics.class, meterStats.build());
180         
181         InstanceIdentifier<? extends Object> refValue = ref.getValue();
182         it.putOperationalData(refValue, nodeData.build());
183         it.commit();
184
185     }
186
187     @Override
188     public void onGroupDescStatsUpdated(GroupDescStatsUpdated notification) {
189         //Add statistics to local cache
190         ConcurrentMap<NodeId, NodeStatistics> cache = this.statisticsManager.getStatisticsCache();
191         if(!cache.containsKey(notification.getId())){
192             cache.put(notification.getId(), new NodeStatistics());
193         }
194         cache.get(notification.getId()).setGroupDescStats(notification.getGroupDescStats());
195         
196         //Publish data to configuration data store
197         DataModificationTransaction it = this.statisticsManager.startChange();
198         NodeKey key = new NodeKey(notification.getId());
199         NodeRef ref = getNodeRef(key);
200         
201         final NodeBuilder nodeData = new NodeBuilder(); 
202         nodeData.setKey(key);
203         
204         NodeGroupDescStatsBuilder groupDesc= new NodeGroupDescStatsBuilder();
205         GroupDescBuilder stats = new GroupDescBuilder();
206         stats.setGroupDescStats(notification.getGroupDescStats());
207         groupDesc.setGroupDesc(stats.build());
208         
209         //Update augmented data
210         nodeData.addAugmentation(NodeGroupDescStats.class, groupDesc.build());
211
212         InstanceIdentifier<? extends Object> refValue = ref.getValue();
213         it.putOperationalData(refValue, nodeData.build());
214         it.commit();
215
216     }
217
218     @Override
219     public void onGroupStatisticsUpdated(GroupStatisticsUpdated notification) {
220         
221         //Add statistics to local cache
222         ConcurrentMap<NodeId, NodeStatistics> cache = this.statisticsManager.getStatisticsCache();
223         if(!cache.containsKey(notification.getId())){
224             cache.put(notification.getId(), new NodeStatistics());
225         }
226         cache.get(notification.getId()).setGroupStatistics(notification.getGroupStats());
227         
228         //Publish data to configuration data store
229         
230         DataModificationTransaction it = this.statisticsManager.startChange();
231         NodeKey key = new NodeKey(notification.getId());
232         NodeRef ref = getNodeRef(key);
233
234         final NodeBuilder nodeData = new NodeBuilder(); 
235         nodeData.setKey(key);
236         
237         NodeGroupStatisticsBuilder groupStats = new NodeGroupStatisticsBuilder();
238         GroupStatisticsBuilder stats = new GroupStatisticsBuilder();
239         stats.setGroupStats(notification.getGroupStats());
240         groupStats.setGroupStatistics(stats.build());
241                 
242         //Update augmented data
243         nodeData.addAugmentation(NodeGroupStatistics.class, groupStats.build());
244
245         InstanceIdentifier<? extends Object> refValue = ref.getValue();
246         it.putOperationalData(refValue, nodeData.build());
247         it.commit();
248
249 //        for (GroupStats groupstat : notification.getGroupStats()) {
250 //        
251 //            GroupStatsKey groupKey = groupstat.getKey();
252 //            InstanceIdentifier<? extends Object> id = InstanceIdentifier.builder(Nodes.class).child(Node.class, key).augmentation(NodeGroupStatistics.class).child(GroupStatistics.class).child(GroupStats.class,groupKey).toInstance();
253 //            it.putOperationalData(id, groupstat);
254 //            it.commit();
255 //        }
256     }
257     
258     @Override
259     public void onMeterFeaturesUpdated(MeterFeaturesUpdated notification) {
260
261         //Add statistics to local cache
262         ConcurrentMap<NodeId, NodeStatistics> cache = this.statisticsManager.getStatisticsCache();
263         if(!cache.containsKey(notification.getId())){
264             cache.put(notification.getId(), new NodeStatistics());
265         }
266         MeterFeaturesBuilder meterFeature = new MeterFeaturesBuilder();
267         meterFeature.setMeterBandSupported(notification.getMeterBandSupported());
268         meterFeature.setMeterCapabilitiesSupported(notification.getMeterCapabilitiesSupported());
269         meterFeature.setMaxBands(notification.getMaxBands());
270         meterFeature.setMaxColor(notification.getMaxColor());
271         meterFeature.setMaxMeter(notification.getMaxMeter());
272         
273         cache.get(notification.getId()).setMeterFeatures(meterFeature.build());
274         
275         //Publish data to configuration data store
276         DataModificationTransaction it = this.statisticsManager.startChange();
277         NodeKey key = new NodeKey(notification.getId());
278         NodeRef ref = getNodeRef(key);
279         
280         final NodeBuilder nodeData = new NodeBuilder(); 
281         nodeData.setKey(key);
282         
283         NodeMeterFeaturesBuilder nodeMeterFeatures= new NodeMeterFeaturesBuilder();
284         nodeMeterFeatures.setMeterFeatures(meterFeature.build());
285         
286         //Update augmented data
287         nodeData.addAugmentation(NodeMeterFeatures.class, nodeMeterFeatures.build());
288         
289         InstanceIdentifier<? extends Object> refValue = ref.getValue();
290         it.putOperationalData(refValue, nodeData.build());
291         it.commit();
292     }
293     
294     @Override
295     public void onGroupFeaturesUpdated(GroupFeaturesUpdated notification) {
296
297         //Add statistics to local cache
298         ConcurrentMap<NodeId, NodeStatistics> cache = this.statisticsManager.getStatisticsCache();
299         if(!cache.containsKey(notification.getId())){
300             cache.put(notification.getId(), new NodeStatistics());
301         }
302         
303         GroupFeaturesBuilder groupFeatures = new GroupFeaturesBuilder();
304         groupFeatures.setActions(notification.getActions());
305         groupFeatures.setGroupCapabilitiesSupported(notification.getGroupCapabilitiesSupported());
306         groupFeatures.setGroupTypesSupported(notification.getGroupTypesSupported());
307         groupFeatures.setMaxGroups(notification.getMaxGroups());
308         cache.get(notification.getId()).setGroupFeatures(groupFeatures.build());
309         
310         //Publish data to configuration data store
311         DataModificationTransaction it = this.statisticsManager.startChange();
312         NodeKey key = new NodeKey(notification.getId());
313         NodeRef ref = getNodeRef(key);
314         
315         final NodeBuilder nodeData = new NodeBuilder(); 
316         nodeData.setKey(key);
317         
318         NodeGroupFeaturesBuilder nodeGroupFeatures= new NodeGroupFeaturesBuilder();
319         nodeGroupFeatures.setGroupFeatures(groupFeatures.build());
320         
321         //Update augmented data
322         nodeData.addAugmentation(NodeGroupFeatures.class, nodeGroupFeatures.build());
323         
324         InstanceIdentifier<? extends Object> refValue = ref.getValue();
325         it.putOperationalData(refValue, nodeData.build());
326         it.commit();
327     }
328
329     @Override
330     public void onFlowsStatisticsUpdate(FlowsStatisticsUpdate notification) {
331         NodeKey key = new NodeKey(notification.getId());
332         sucLogger.info("Received flow stats update : {}",notification.toString());
333         
334         for(FlowAndStatisticsMapList map: notification.getFlowAndStatisticsMapList()){
335             short tableId = map.getTableId();
336             
337             DataModificationTransaction it = this.statisticsManager.startChange();
338
339             boolean foundOriginalFlow = false;
340
341             FlowBuilder flowBuilder = new FlowBuilder();
342
343             FlowStatisticsDataBuilder flowStatisticsData = new FlowStatisticsDataBuilder();
344
345             FlowBuilder flow = new FlowBuilder();
346             flow.setContainerName(map.getContainerName());
347             flow.setBufferId(map.getBufferId());
348             flow.setCookie(map.getCookie());
349             flow.setCookieMask(map.getCookieMask());
350             flow.setFlags(map.getFlags());
351             flow.setFlowName(map.getFlowName());
352             flow.setHardTimeout(map.getHardTimeout());
353             if(map.getFlowId() != null)
354                 flow.setId(new FlowId(map.getFlowId().getValue()));
355             flow.setIdleTimeout(map.getIdleTimeout());
356             flow.setInstallHw(map.isInstallHw());
357             flow.setInstructions(map.getInstructions());
358             if(map.getFlowId()!= null)
359                 flow.setKey(new FlowKey(new FlowId(map.getKey().getFlowId().getValue())));
360             flow.setMatch(map.getMatch());
361             flow.setOutGroup(map.getOutGroup());
362             flow.setOutPort(map.getOutPort());
363             flow.setPriority(map.getPriority());
364             flow.setStrict(map.isStrict());
365             flow.setTableId(tableId);
366                 
367             Flow flowRule = flow.build();
368                 
369             FlowAndStatisticsMapListBuilder stats = new FlowAndStatisticsMapListBuilder();
370             stats.setByteCount(map.getByteCount());
371             stats.setPacketCount(map.getPacketCount());
372             stats.setDuration(map.getDuration());
373                 
374             GenericStatistics flowStats = stats.build();
375                 
376             //Add statistics to local cache
377             ConcurrentMap<NodeId, NodeStatistics> cache = this.statisticsManager.getStatisticsCache();
378             if(!cache.containsKey(notification.getId())){
379                 cache.put(notification.getId(), new NodeStatistics());
380             }
381             if(!cache.get(notification.getId()).getFlowAndStatsMap().containsKey(tableId)){
382                 cache.get(notification.getId()).getFlowAndStatsMap().put(tableId, new HashMap<Flow,GenericStatistics>());
383             }
384             cache.get(notification.getId()).getFlowAndStatsMap().get(tableId).put(flowRule,flowStats);
385                 
386             //Augment the data to the flow node
387
388             FlowStatisticsBuilder flowStatistics = new FlowStatisticsBuilder();
389             flowStatistics.setByteCount(flowStats.getByteCount());
390             flowStatistics.setPacketCount(flowStats.getPacketCount());
391             flowStatistics.setDuration(flowStats.getDuration());
392             flowStatistics.setContainerName(map.getContainerName());
393             flowStatistics.setBufferId(map.getBufferId());
394             flowStatistics.setCookie(map.getCookie());
395             flowStatistics.setCookieMask(map.getCookieMask());
396             flowStatistics.setFlags(map.getFlags());
397             flowStatistics.setFlowName(map.getFlowName());
398             flowStatistics.setHardTimeout(map.getHardTimeout());
399             flowStatistics.setIdleTimeout(map.getIdleTimeout());
400             flowStatistics.setInstallHw(map.isInstallHw());
401             flowStatistics.setInstructions(map.getInstructions());
402             flowStatistics.setMatch(map.getMatch());
403             flowStatistics.setOutGroup(map.getOutGroup());
404             flowStatistics.setOutPort(map.getOutPort());
405             flowStatistics.setPriority(map.getPriority());
406             flowStatistics.setStrict(map.isStrict());
407             flowStatistics.setTableId(tableId);
408
409             flowStatisticsData.setFlowStatistics(flowStatistics.build());
410                 
411             sucLogger.debug("Flow : {}",flowRule.toString());
412             sucLogger.debug("Statistics to augment : {}",flowStatistics.build().toString());
413
414             InstanceIdentifier<Table> tableRef = InstanceIdentifier.builder(Nodes.class).child(Node.class, key)
415                     .augmentation(FlowCapableNode.class).child(Table.class, new TableKey(tableId)).toInstance();
416             
417             Table table= (Table)it.readConfigurationData(tableRef);
418
419             //TODO: Not a good way to do it, need to figure out better way.
420             //TODO: major issue in any alternate approach is that flow key is incrementally assigned 
421             //to the flows stored in data store.
422             if(table != null){
423
424                 for(Flow existingFlow : table.getFlow()){
425                     sucLogger.debug("Existing flow in data store : {}",existingFlow.toString());
426                     if(flowEquals(flowRule,existingFlow)){
427                         InstanceIdentifier<Flow> flowRef = InstanceIdentifier.builder(Nodes.class).child(Node.class, key)
428                                 .augmentation(FlowCapableNode.class)
429                                 .child(Table.class, new TableKey(tableId))
430                                 .child(Flow.class,existingFlow.getKey()).toInstance();
431                         flowBuilder.setKey(existingFlow.getKey());
432                         flowBuilder.addAugmentation(FlowStatisticsData.class, flowStatisticsData.build());
433                         sucLogger.debug("Found matching flow in the datastore, augmenting statistics");
434                         foundOriginalFlow = true;
435                         it.putOperationalData(flowRef, flowBuilder.build());
436                         it.commit();
437                         break;
438                     }
439                 }
440             }
441             
442             if(!foundOriginalFlow){
443                 sucLogger.info("Associated original flow is not found in data store. Augmenting flow in operational data st");
444                 //TODO: Temporary fix: format [ 0+tableid+0+unaccounted flow counter]
445                 long flowKey = Long.getLong(new String("0"+Short.toString(tableId)+"0"+Integer.toString(this.unaccountedFlowsCounter)));
446                 FlowKey newFlowKey = new FlowKey(new FlowId(flowKey));
447                 InstanceIdentifier<Flow> flowRef = InstanceIdentifier.builder(Nodes.class).child(Node.class, key)
448                         .augmentation(FlowCapableNode.class)
449                         .child(Table.class, new TableKey(tableId))
450                         .child(Flow.class,newFlowKey).toInstance();
451                 flowBuilder.setKey(newFlowKey);
452                 flowBuilder.addAugmentation(FlowStatisticsData.class, flowStatisticsData.build());
453                 sucLogger.debug("Flow was no present in data store, augmenting statistics as an unaccounted flow");
454                 it.putOperationalData(flowRef, flowBuilder.build());
455                 it.commit();
456             }
457         }
458     }
459
460     @Override
461     public void onAggregateFlowStatisticsUpdate(AggregateFlowStatisticsUpdate notification) {
462         NodeKey key = new NodeKey(notification.getId());
463         sucLogger.debug("Received aggregate flow statistics update : {}",notification.toString());
464         
465         Short tableId = this.statisticsManager.getMultipartMessageManager().getTableIdForTxId(notification.getTransactionId());
466         if(tableId != null){
467             
468             DataModificationTransaction it = this.statisticsManager.startChange();
469
470             InstanceIdentifier<Table> tableRef = InstanceIdentifier.builder(Nodes.class).child(Node.class, key)
471                     .augmentation(FlowCapableNode.class).child(Table.class, new TableKey(tableId)).toInstance();
472
473             AggregateFlowStatisticsDataBuilder aggregateFlowStatisticsDataBuilder = new AggregateFlowStatisticsDataBuilder();
474             AggregateFlowStatisticsBuilder aggregateFlowStatisticsBuilder = new AggregateFlowStatisticsBuilder();
475             aggregateFlowStatisticsBuilder.setByteCount(notification.getByteCount());
476             aggregateFlowStatisticsBuilder.setFlowCount(notification.getFlowCount());
477             aggregateFlowStatisticsBuilder.setPacketCount(notification.getPacketCount());
478             aggregateFlowStatisticsDataBuilder.setAggregateFlowStatistics(aggregateFlowStatisticsBuilder.build());
479             
480             ConcurrentMap<NodeId, NodeStatistics> cache = this.statisticsManager.getStatisticsCache();
481             if(!cache.containsKey(notification.getId())){
482                 cache.put(notification.getId(), new NodeStatistics());
483             }
484             cache.get(notification.getId()).getTableAndAggregateFlowStatsMap().put(tableId,aggregateFlowStatisticsBuilder.build());
485             
486             sucLogger.debug("Augment aggregate statistics: {} for table {} on Node {}",aggregateFlowStatisticsBuilder.build().toString(),tableId,key);
487
488             TableBuilder tableBuilder = new TableBuilder();
489             tableBuilder.setKey(new TableKey(tableId));
490             tableBuilder.addAugmentation(AggregateFlowStatisticsData.class, aggregateFlowStatisticsDataBuilder.build());
491             it.putOperationalData(tableRef, tableBuilder.build());
492             it.commit();
493
494         }
495     }
496
497     @Override
498     public void onPortStatisticsUpdate(PortStatisticsUpdate notification) {
499         NodeKey key = new NodeKey(notification.getId());
500         sucLogger.info("Received port stats update : {}",notification.toString());
501         
502         //Add statistics to local cache
503         ConcurrentMap<NodeId, NodeStatistics> cache = this.statisticsManager.getStatisticsCache();
504         if(!cache.containsKey(notification.getId())){
505             cache.put(notification.getId(), new NodeStatistics());
506         }
507
508
509         List<NodeConnectorStatisticsAndPortNumberMap> portsStats = notification.getNodeConnectorStatisticsAndPortNumberMap();
510         for(NodeConnectorStatisticsAndPortNumberMap portStats : portsStats){
511             
512             DataModificationTransaction it = this.statisticsManager.startChange();
513
514             FlowCapableNodeConnectorStatisticsBuilder statisticsBuilder 
515                                             = new FlowCapableNodeConnectorStatisticsBuilder();
516             statisticsBuilder.setBytes(portStats.getBytes());
517             statisticsBuilder.setCollisionCount(portStats.getCollisionCount());
518             statisticsBuilder.setDuration(portStats.getDuration());
519             statisticsBuilder.setPackets(portStats.getPackets());
520             statisticsBuilder.setReceiveCrcError(portStats.getReceiveCrcError());
521             statisticsBuilder.setReceiveDrops(portStats.getReceiveDrops());
522             statisticsBuilder.setReceiveErrors(portStats.getReceiveErrors());
523             statisticsBuilder.setReceiveFrameError(portStats.getReceiveFrameError());
524             statisticsBuilder.setReceiveOverRunError(portStats.getReceiveOverRunError());
525             statisticsBuilder.setTransmitDrops(portStats.getTransmitDrops());
526             statisticsBuilder.setTransmitErrors(portStats.getTransmitErrors());
527             
528             //Update data in the cache
529             cache.get(notification.getId()).getNodeConnectorStats().put(portStats.getNodeConnectorId(), statisticsBuilder.build());
530             
531             //Augment data to the node-connector
532             FlowCapableNodeConnectorStatisticsDataBuilder statisticsDataBuilder = 
533                     new FlowCapableNodeConnectorStatisticsDataBuilder();
534             
535             statisticsDataBuilder.setFlowCapableNodeConnectorStatistics(statisticsBuilder.build());
536             
537             InstanceIdentifier<NodeConnector> nodeConnectorRef = InstanceIdentifier.builder(Nodes.class).child(Node.class, key).child(NodeConnector.class, new NodeConnectorKey(portStats.getNodeConnectorId())).toInstance();
538             
539             NodeConnector nodeConnector = (NodeConnector)it.readOperationalData(nodeConnectorRef);
540             
541             if(nodeConnector != null){
542                 sucLogger.debug("Augmenting port statistics {} to port {}",statisticsDataBuilder.build().toString(),nodeConnectorRef.toString());
543                 NodeConnectorBuilder nodeConnectorBuilder = new NodeConnectorBuilder();
544                 nodeConnectorBuilder.addAugmentation(FlowCapableNodeConnectorStatisticsData.class, statisticsDataBuilder.build());
545                 it.putOperationalData(nodeConnectorRef, nodeConnectorBuilder.build());
546                 it.commit();
547             }
548         }
549     }
550
551     @Override
552     public void onFlowTableStatisticsUpdate(FlowTableStatisticsUpdate notification) {
553         NodeKey key = new NodeKey(notification.getId());
554         sucLogger.debug("Received flow table statistics update : {}",notification.toString());
555         
556         List<FlowTableAndStatisticsMap> flowTablesStatsList = notification.getFlowTableAndStatisticsMap();
557         for (FlowTableAndStatisticsMap ftStats : flowTablesStatsList){
558             
559             DataModificationTransaction it = this.statisticsManager.startChange();
560
561             InstanceIdentifier<Table> tableRef = InstanceIdentifier.builder(Nodes.class).child(Node.class, key)
562                     .augmentation(FlowCapableNode.class).child(Table.class, new TableKey(ftStats.getTableId().getValue())).toInstance();
563             
564             FlowTableStatisticsDataBuilder statisticsDataBuilder = new FlowTableStatisticsDataBuilder();
565             
566             FlowTableStatisticsBuilder statisticsBuilder = new FlowTableStatisticsBuilder();
567             statisticsBuilder.setActiveFlows(ftStats.getActiveFlows());
568             statisticsBuilder.setPacketsLookedUp(ftStats.getPacketsLookedUp());
569             statisticsBuilder.setPacketsMatched(ftStats.getPacketsMatched());
570             
571             statisticsDataBuilder.setFlowTableStatistics(statisticsBuilder.build());
572             
573             ConcurrentMap<NodeId, NodeStatistics> cache = this.statisticsManager.getStatisticsCache();
574             if(!cache.containsKey(notification.getId())){
575                 cache.put(notification.getId(), new NodeStatistics());
576             }
577             cache.get(notification.getId()).getFlowTableAndStatisticsMap().put(ftStats.getTableId().getValue(),statisticsBuilder.build());
578             
579             sucLogger.debug("Augment flow table statistics: {} for table {} on Node {}",statisticsBuilder.build().toString(),ftStats.getTableId(),key);
580             
581             TableBuilder tableBuilder = new TableBuilder();
582             tableBuilder.setKey(new TableKey(ftStats.getTableId().getValue()));
583             tableBuilder.addAugmentation(FlowTableStatisticsData.class, statisticsDataBuilder.build());
584             it.putOperationalData(tableRef, tableBuilder.build());
585             it.commit();
586         }
587     }
588
589     @Override
590     public void onQueueStatisticsUpdate(QueueStatisticsUpdate notification) {
591         NodeKey key = new NodeKey(notification.getId());
592         sucLogger.info("Received queue stats update : {}",notification.toString());
593         
594         //Add statistics to local cache
595         ConcurrentMap<NodeId, NodeStatistics> cache = this.statisticsManager.getStatisticsCache();
596         if(!cache.containsKey(notification.getId())){
597             cache.put(notification.getId(), new NodeStatistics());
598         }
599         
600         List<QueueIdAndStatisticsMap> queuesStats = notification.getQueueIdAndStatisticsMap();
601         for(QueueIdAndStatisticsMap swQueueStats : queuesStats){
602             
603             if(!cache.get(notification.getId()).getNodeConnectorAndQueuesStatsMap().containsKey(swQueueStats.getNodeConnectorId())){
604                 cache.get(notification.getId()).getNodeConnectorAndQueuesStatsMap().put(swQueueStats.getNodeConnectorId(), new HashMap<QueueId,GenericQueueStatistics>());
605             }
606             
607             FlowCapableNodeConnectorQueueStatisticsDataBuilder queueStatisticsDataBuilder = new FlowCapableNodeConnectorQueueStatisticsDataBuilder();
608             
609             FlowCapableNodeConnectorQueueStatisticsBuilder queueStatisticsBuilder = new FlowCapableNodeConnectorQueueStatisticsBuilder();
610             
611             queueStatisticsBuilder.fieldsFrom(swQueueStats);
612             
613             queueStatisticsDataBuilder.setFlowCapableNodeConnectorQueueStatistics(queueStatisticsBuilder.build());
614             
615             cache.get(notification.getId()).getNodeConnectorAndQueuesStatsMap()
616                                             .get(swQueueStats.getNodeConnectorId())
617                                             .put(swQueueStats.getQueueId(), queueStatisticsBuilder.build());
618             
619             
620             DataModificationTransaction it = this.statisticsManager.startChange();
621
622             InstanceIdentifier<Queue> queueRef 
623                     = InstanceIdentifier.builder(Nodes.class)
624                                         .child(Node.class, key)
625                                         .child(NodeConnector.class, new NodeConnectorKey(swQueueStats.getNodeConnectorId()))
626                                         .augmentation(FlowCapableNodeConnector.class)
627                                         .child(Queue.class, new QueueKey(swQueueStats.getQueueId())).toInstance();
628             
629             QueueBuilder queueBuilder = new QueueBuilder();
630             queueBuilder.addAugmentation(FlowCapableNodeConnectorQueueStatisticsData.class, queueStatisticsDataBuilder.build());
631             queueBuilder.setKey(new QueueKey(swQueueStats.getQueueId()));
632
633             sucLogger.info("Augmenting queue statistics {} of queue {} to port {}"
634                                         ,queueStatisticsDataBuilder.build().toString(),
635                                         swQueueStats.getQueueId(),
636                                         swQueueStats.getNodeConnectorId());
637             
638             it.putOperationalData(queueRef, queueBuilder.build());
639             it.commit();
640             
641         }
642         
643     }
644
645     @Override
646     public void onFlowStatisticsUpdated(FlowStatisticsUpdated notification) {
647         // TODO Auto-generated method stub
648         //TODO: Depricated, will clean it up once sal-compatibility is fixed.
649         //Sal-Compatibility code usage this notification event.
650         
651     }
652
653     @Override
654     public void onFlowTableStatisticsUpdated(FlowTableStatisticsUpdated notification) {
655         // TODO Auto-generated method stub
656         //TODO: Need to implement it yet
657         
658     }
659
660     @Override
661     public void onNodeConnectorStatisticsUpdated(NodeConnectorStatisticsUpdated notification) {
662         // TODO Auto-generated method stub
663         //TODO: Need to implement it yet
664         
665     }
666
667     private NodeRef getNodeRef(NodeKey nodeKey){
668         InstanceIdentifierBuilder<?> builder = InstanceIdentifier.builder(Nodes.class).child(Node.class, nodeKey);
669         return new NodeRef(builder.toInstance());
670     }
671     
672     public boolean flowEquals(Flow statsFlow, Flow storedFlow) {
673         if (statsFlow.getClass() != storedFlow.getClass()) {
674             return false;
675         }
676         if (statsFlow.getBufferId()== null) {
677             if (storedFlow.getBufferId() != null) {
678                 return false;
679             }
680         } else if(!statsFlow.getBufferId().equals(storedFlow.getBufferId())) {
681             return false;
682         }
683         if (statsFlow.getContainerName()== null) {
684             if (storedFlow.getContainerName()!= null) {
685                 return false;
686             }
687         } else if(!statsFlow.getContainerName().equals(storedFlow.getContainerName())) {
688             return false;
689         }
690         if (statsFlow.getCookie()== null) {
691             if (storedFlow.getCookie()!= null) {
692                 return false;
693             }
694         } else if(!statsFlow.getCookie().equals(storedFlow.getCookie())) {
695             return false;
696         }
697         if (statsFlow.getMatch()== null) {
698             if (storedFlow.getMatch() != null) {
699                 return false;
700             }
701         } else if(!statsFlow.getMatch().equals(storedFlow.getMatch())) {
702             return false;
703         }
704         if (statsFlow.getCookie()== null) {
705             if (storedFlow.getCookie()!= null) {
706                 return false;
707             }
708         } else if(!statsFlow.getCookie().equals(storedFlow.getCookie())) {
709             return false;
710         }
711         if (statsFlow.getHardTimeout() == null) {
712             if (storedFlow.getHardTimeout() != null) {
713                 return false;
714             }
715         } else if(!statsFlow.getHardTimeout().equals(storedFlow.getHardTimeout() )) {
716             return false;
717         }
718         if (statsFlow.getIdleTimeout()== null) {
719             if (storedFlow.getIdleTimeout() != null) {
720                 return false;
721             }
722         } else if(!statsFlow.getIdleTimeout().equals(storedFlow.getIdleTimeout())) {
723             return false;
724         }
725         if (statsFlow.getPriority() == null) {
726             if (storedFlow.getPriority() != null) {
727                 return false;
728             }
729         } else if(!statsFlow.getPriority().equals(storedFlow.getPriority())) {
730             return false;
731         }
732         if (statsFlow.getTableId() == null) {
733             if (storedFlow.getTableId() != null) {
734                 return false;
735             }
736         } else if(!statsFlow.getTableId().equals(storedFlow.getTableId())) {
737             return false;
738         }
739         return true;
740     }
741
742 }