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