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