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