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