a7c00d45913ccb4d5c9c44504f252735d17340a1
[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.net.Inet4Address;
11 import java.net.InetAddress;
12 import java.net.UnknownHostException;
13 import java.util.List;
14 import java.util.concurrent.ConcurrentMap;
15
16 import org.opendaylight.controller.md.statistics.manager.NodeStatisticsAger.FlowEntry;
17 import org.opendaylight.controller.md.statistics.manager.NodeStatisticsAger.QueueEntry;
18 import org.opendaylight.controller.sal.binding.api.data.DataModificationTransaction;
19 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Prefix;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNodeConnector;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowId;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.meters.Meter;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.meters.MeterBuilder;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.meters.MeterKey;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.Table;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.TableBuilder;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.TableKey;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.Flow;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.FlowBuilder;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.FlowKey;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.AggregateFlowStatisticsData;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.AggregateFlowStatisticsDataBuilder;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.AggregateFlowStatisticsUpdate;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.FlowStatisticsData;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.FlowStatisticsDataBuilder;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.FlowsStatisticsUpdate;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.OpendaylightFlowStatisticsListener;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.aggregate.flow.statistics.AggregateFlowStatisticsBuilder;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.flow.and.statistics.map.list.FlowAndStatisticsMapList;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.flow.and.statistics.map.list.FlowAndStatisticsMapListBuilder;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.flow.statistics.FlowStatisticsBuilder;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.table.statistics.rev131215.FlowTableStatisticsData;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.table.statistics.rev131215.FlowTableStatisticsDataBuilder;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.table.statistics.rev131215.FlowTableStatisticsUpdate;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.table.statistics.rev131215.OpendaylightFlowTableStatisticsListener;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.table.statistics.rev131215.flow.table.and.statistics.map.FlowTableAndStatisticsMap;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.table.statistics.rev131215.flow.table.statistics.FlowTableStatisticsBuilder;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.port.rev130925.queues.Queue;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.port.rev130925.queues.QueueBuilder;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.port.rev130925.queues.QueueKey;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.Match;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.statistics.rev131111.GroupDescStatsUpdated;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.statistics.rev131111.GroupFeaturesUpdated;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.statistics.rev131111.GroupStatisticsUpdated;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.statistics.rev131111.NodeGroupDescStats;
57 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.statistics.rev131111.NodeGroupDescStatsBuilder;
58 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.statistics.rev131111.NodeGroupFeatures;
59 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.statistics.rev131111.NodeGroupFeaturesBuilder;
60 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.statistics.rev131111.NodeGroupStatistics;
61 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.statistics.rev131111.NodeGroupStatisticsBuilder;
62 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.statistics.rev131111.OpendaylightGroupStatisticsListener;
63 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.statistics.rev131111.group.desc.GroupDescBuilder;
64 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.statistics.rev131111.group.features.GroupFeaturesBuilder;
65 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.statistics.rev131111.group.statistics.GroupStatisticsBuilder;
66 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.group.desc.stats.reply.GroupDescStats;
67 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.group.statistics.reply.GroupStats;
68 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.groups.Group;
69 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.groups.GroupBuilder;
70 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.groups.GroupKey;
71 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
72 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeRef;
73 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
74 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.node.NodeConnector;
75 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.node.NodeConnectorBuilder;
76 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.node.NodeConnectorKey;
77 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
78 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeBuilder;
79 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
80 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.MeterConfigStatsUpdated;
81 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.MeterFeaturesUpdated;
82 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.MeterStatisticsUpdated;
83 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.NodeMeterConfigStats;
84 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.NodeMeterConfigStatsBuilder;
85 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.NodeMeterFeatures;
86 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.NodeMeterFeaturesBuilder;
87 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.NodeMeterStatistics;
88 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.NodeMeterStatisticsBuilder;
89 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.OpendaylightMeterStatisticsListener;
90 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.nodes.node.MeterFeaturesBuilder;
91 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.nodes.node.meter.MeterConfigStatsBuilder;
92 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.nodes.node.meter.MeterStatisticsBuilder;
93 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.types.rev130918.meter.config.stats.reply.MeterConfigStats;
94 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.types.rev130918.meter.statistics.reply.MeterStats;
95 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.Layer3Match;
96 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._3.match.Ipv4Match;
97 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.statistics.types.rev130925.GenericStatistics;
98 import org.opendaylight.yang.gen.v1.urn.opendaylight.port.statistics.rev131214.FlowCapableNodeConnectorStatisticsData;
99 import org.opendaylight.yang.gen.v1.urn.opendaylight.port.statistics.rev131214.FlowCapableNodeConnectorStatisticsDataBuilder;
100 import org.opendaylight.yang.gen.v1.urn.opendaylight.port.statistics.rev131214.NodeConnectorStatisticsUpdate;
101 import org.opendaylight.yang.gen.v1.urn.opendaylight.port.statistics.rev131214.OpendaylightPortStatisticsListener;
102 import org.opendaylight.yang.gen.v1.urn.opendaylight.port.statistics.rev131214.flow.capable.node.connector.statistics.FlowCapableNodeConnectorStatisticsBuilder;
103 import org.opendaylight.yang.gen.v1.urn.opendaylight.port.statistics.rev131214.node.connector.statistics.and.port.number.map.NodeConnectorStatisticsAndPortNumberMap;
104 import org.opendaylight.yang.gen.v1.urn.opendaylight.queue.statistics.rev131216.FlowCapableNodeConnectorQueueStatisticsData;
105 import org.opendaylight.yang.gen.v1.urn.opendaylight.queue.statistics.rev131216.FlowCapableNodeConnectorQueueStatisticsDataBuilder;
106 import org.opendaylight.yang.gen.v1.urn.opendaylight.queue.statistics.rev131216.OpendaylightQueueStatisticsListener;
107 import org.opendaylight.yang.gen.v1.urn.opendaylight.queue.statistics.rev131216.QueueStatisticsUpdate;
108 import org.opendaylight.yang.gen.v1.urn.opendaylight.queue.statistics.rev131216.flow.capable.node.connector.queue.statistics.FlowCapableNodeConnectorQueueStatisticsBuilder;
109 import org.opendaylight.yang.gen.v1.urn.opendaylight.queue.statistics.rev131216.queue.id.and.statistics.map.QueueIdAndStatisticsMap;
110 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
111 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier.InstanceIdentifierBuilder;
112 import org.slf4j.Logger;
113 import org.slf4j.LoggerFactory;
114
115 /**
116  * Class implement statistics manager related listener interface and augment all the 
117  * received statistics data to data stores.
118  * TODO: Need to add error message listener and clean-up the associated tx id 
119  * if it exists in the tx-id cache.
120  * @author vishnoianil
121  *
122  */
123 public class StatisticsUpdateCommiter implements OpendaylightGroupStatisticsListener,
124         OpendaylightMeterStatisticsListener, 
125         OpendaylightFlowStatisticsListener,
126         OpendaylightPortStatisticsListener,
127         OpendaylightFlowTableStatisticsListener,
128         OpendaylightQueueStatisticsListener{
129     
130     private final static Logger sucLogger = LoggerFactory.getLogger(StatisticsUpdateCommiter.class);
131
132     private final StatisticsProvider statisticsManager;
133     private final MultipartMessageManager messageManager;
134     
135     private int unaccountedFlowsCounter = 1;
136
137     /**
138      * default ctor
139      * @param manager
140      */
141     public StatisticsUpdateCommiter(final StatisticsProvider manager){
142
143         this.statisticsManager = manager;
144         this.messageManager = this.statisticsManager.getMultipartMessageManager();
145     }
146     
147     public StatisticsProvider getStatisticsManager(){
148         return statisticsManager;
149     }
150    
151     @Override
152     public void onMeterConfigStatsUpdated(MeterConfigStatsUpdated notification) {
153         //Check if response is for the request statistics-manager sent.
154         if(!messageManager.isRequestTxIdExist(notification.getId(),notification.getTransactionId(),notification.isMoreReplies()))
155             return;
156         
157         NodeKey key = new NodeKey(notification.getId());
158
159         //Add statistics to local cache
160         ConcurrentMap<NodeId, NodeStatisticsAger> cache = this.statisticsManager.getStatisticsCache();
161         if(!cache.containsKey(notification.getId())){
162             cache.put(notification.getId(), new NodeStatisticsAger(statisticsManager,key));
163         }
164         cache.get(notification.getId()).updateMeterConfigStats(notification.getMeterConfigStats());
165         
166         //Publish data to configuration data store
167         List<MeterConfigStats> meterConfigStatsList = notification.getMeterConfigStats();
168         
169         for(MeterConfigStats meterConfigStats : meterConfigStatsList){
170             DataModificationTransaction it = this.statisticsManager.startChange();
171             MeterBuilder meterBuilder = new MeterBuilder();
172             MeterKey meterKey = new MeterKey(meterConfigStats.getMeterId());
173             meterBuilder.setKey(meterKey);
174             
175             InstanceIdentifier<Meter> meterRef = InstanceIdentifier.builder(Nodes.class).child(Node.class,key)
176                                                                                         .augmentation(FlowCapableNode.class)
177                                                                                         .child(Meter.class,meterKey).toInstance();
178             
179             NodeMeterConfigStatsBuilder meterConfig= new NodeMeterConfigStatsBuilder();
180             MeterConfigStatsBuilder stats = new MeterConfigStatsBuilder();
181             stats.fieldsFrom(meterConfigStats);
182             meterConfig.setMeterConfigStats(stats.build());
183             
184             //Update augmented data
185             meterBuilder.addAugmentation(NodeMeterConfigStats.class, meterConfig.build());
186             it.putOperationalData(meterRef, meterBuilder.build());
187             it.commit();
188
189         }
190     }
191
192     @Override
193     public void onMeterStatisticsUpdated(MeterStatisticsUpdated notification) {
194         
195         //Check if response is for the request statistics-manager sent.
196         if(!messageManager.isRequestTxIdExist(notification.getId(),notification.getTransactionId(),notification.isMoreReplies()))
197             return;
198
199         NodeKey key = new NodeKey(notification.getId());
200         
201         List<MeterStats> meterStatsList = notification.getMeterStats();
202         
203         for(MeterStats meterStats : meterStatsList){
204
205             //Publish data to configuration data store
206             DataModificationTransaction it = this.statisticsManager.startChange();
207             MeterBuilder meterBuilder = new MeterBuilder();
208             MeterKey meterKey = new MeterKey(meterStats.getMeterId());
209             meterBuilder.setKey(meterKey);
210             
211             InstanceIdentifier<Meter> meterRef = InstanceIdentifier.builder(Nodes.class).child(Node.class,key)
212                                                                                         .augmentation(FlowCapableNode.class)
213                                                                                         .child(Meter.class,meterKey).toInstance();
214             
215             NodeMeterStatisticsBuilder meterStatsBuilder= new NodeMeterStatisticsBuilder();
216             MeterStatisticsBuilder stats = new MeterStatisticsBuilder();
217             stats.fieldsFrom(meterStats);
218             meterStatsBuilder.setMeterStatistics(stats.build());
219
220             //Update augmented data
221             meterBuilder.addAugmentation(NodeMeterStatistics.class, meterStatsBuilder.build());
222             it.putOperationalData(meterRef, meterBuilder.build());
223             it.commit();
224         }
225     }
226
227     @Override
228     public void onGroupDescStatsUpdated(GroupDescStatsUpdated notification) {
229         
230         //Check if response is for the request statistics-manager sent.
231         if(!messageManager.isRequestTxIdExist(notification.getId(),notification.getTransactionId(),notification.isMoreReplies()))
232             return;
233
234         NodeKey key = new NodeKey(notification.getId());
235
236         //Add statistics to local cache
237         ConcurrentMap<NodeId, NodeStatisticsAger> cache = this.statisticsManager.getStatisticsCache();
238         if(!cache.containsKey(notification.getId())){
239             cache.put(notification.getId(), new NodeStatisticsAger(statisticsManager,key));
240         }
241         cache.get(notification.getId()).updateGroupDescStats(notification.getGroupDescStats());
242         
243         //Publish data to configuration data store
244         List<GroupDescStats> groupDescStatsList = notification.getGroupDescStats();
245
246         for(GroupDescStats groupDescStats : groupDescStatsList){
247             DataModificationTransaction it = this.statisticsManager.startChange();
248             
249             GroupBuilder groupBuilder = new GroupBuilder();
250             GroupKey groupKey = new GroupKey(groupDescStats.getGroupId());
251             groupBuilder.setKey(groupKey);
252             
253             InstanceIdentifier<Group> groupRef = InstanceIdentifier.builder(Nodes.class).child(Node.class,key)
254                                                                                         .augmentation(FlowCapableNode.class)
255                                                                                         .child(Group.class,groupKey).toInstance();
256
257             NodeGroupDescStatsBuilder groupDesc= new NodeGroupDescStatsBuilder();
258             GroupDescBuilder stats = new GroupDescBuilder();
259             stats.fieldsFrom(groupDescStats);
260             groupDesc.setGroupDesc(stats.build());
261             
262             //Update augmented data
263             groupBuilder.addAugmentation(NodeGroupDescStats.class, groupDesc.build());
264
265             it.putOperationalData(groupRef, groupBuilder.build());
266             it.commit();
267         }
268     }
269
270     @Override
271     public void onGroupStatisticsUpdated(GroupStatisticsUpdated notification) {
272         
273         //Check if response is for the request statistics-manager sent.
274         if(!messageManager.isRequestTxIdExist(notification.getId(),notification.getTransactionId(),notification.isMoreReplies()))
275             return;
276
277         //Publish data to configuration data store
278         NodeKey key = new NodeKey(notification.getId());
279         List<GroupStats> groupStatsList = notification.getGroupStats();
280
281         for(GroupStats groupStats : groupStatsList){
282             DataModificationTransaction it = this.statisticsManager.startChange();
283             
284             GroupBuilder groupBuilder = new GroupBuilder();
285             GroupKey groupKey = new GroupKey(groupStats.getGroupId());
286             groupBuilder.setKey(groupKey);
287             
288             InstanceIdentifier<Group> groupRef = InstanceIdentifier.builder(Nodes.class).child(Node.class,key)
289                                                                                         .augmentation(FlowCapableNode.class)
290                                                                                         .child(Group.class,groupKey).toInstance();
291
292             NodeGroupStatisticsBuilder groupStatisticsBuilder= new NodeGroupStatisticsBuilder();
293             GroupStatisticsBuilder stats = new GroupStatisticsBuilder();
294             stats.fieldsFrom(groupStats);
295             groupStatisticsBuilder.setGroupStatistics(stats.build());
296             
297             //Update augmented data
298             groupBuilder.addAugmentation(NodeGroupStatistics.class, groupStatisticsBuilder.build());
299             it.putOperationalData(groupRef, groupBuilder.build());
300             it.commit();
301         }
302     }
303     
304     @Override
305     public void onMeterFeaturesUpdated(MeterFeaturesUpdated notification) {
306
307         MeterFeaturesBuilder meterFeature = new MeterFeaturesBuilder();
308         meterFeature.setMeterBandSupported(notification.getMeterBandSupported());
309         meterFeature.setMeterCapabilitiesSupported(notification.getMeterCapabilitiesSupported());
310         meterFeature.setMaxBands(notification.getMaxBands());
311         meterFeature.setMaxColor(notification.getMaxColor());
312         meterFeature.setMaxMeter(notification.getMaxMeter());
313         
314         //Publish data to configuration data store
315         DataModificationTransaction it = this.statisticsManager.startChange();
316         NodeKey key = new NodeKey(notification.getId());
317         NodeRef ref = getNodeRef(key);
318         
319         final NodeBuilder nodeData = new NodeBuilder(); 
320         nodeData.setKey(key);
321         
322         NodeMeterFeaturesBuilder nodeMeterFeatures= new NodeMeterFeaturesBuilder();
323         nodeMeterFeatures.setMeterFeatures(meterFeature.build());
324         
325         //Update augmented data
326         nodeData.addAugmentation(NodeMeterFeatures.class, nodeMeterFeatures.build());
327         
328         InstanceIdentifier<? extends Object> refValue = ref.getValue();
329         it.putOperationalData(refValue, nodeData.build());
330         it.commit();
331     }
332     
333     @Override
334     public void onGroupFeaturesUpdated(GroupFeaturesUpdated notification) {
335
336         GroupFeaturesBuilder groupFeatures = new GroupFeaturesBuilder();
337         groupFeatures.setActions(notification.getActions());
338         groupFeatures.setGroupCapabilitiesSupported(notification.getGroupCapabilitiesSupported());
339         groupFeatures.setGroupTypesSupported(notification.getGroupTypesSupported());
340         groupFeatures.setMaxGroups(notification.getMaxGroups());
341         
342         //Publish data to configuration data store
343         DataModificationTransaction it = this.statisticsManager.startChange();
344         NodeKey key = new NodeKey(notification.getId());
345         NodeRef ref = getNodeRef(key);
346         
347         final NodeBuilder nodeData = new NodeBuilder(); 
348         nodeData.setKey(key);
349         
350         NodeGroupFeaturesBuilder nodeGroupFeatures= new NodeGroupFeaturesBuilder();
351         nodeGroupFeatures.setGroupFeatures(groupFeatures.build());
352         
353         //Update augmented data
354         nodeData.addAugmentation(NodeGroupFeatures.class, nodeGroupFeatures.build());
355         
356         InstanceIdentifier<? extends Object> refValue = ref.getValue();
357         it.putOperationalData(refValue, nodeData.build());
358         it.commit();
359     }
360
361     @Override
362     public void onFlowsStatisticsUpdate(FlowsStatisticsUpdate notification) {
363         
364         //Check if response is for the request statistics-manager sent.
365         if(!messageManager.isRequestTxIdExist(notification.getId(),notification.getTransactionId(),notification.isMoreReplies()))
366             return;
367
368         NodeKey key = new NodeKey(notification.getId());
369         sucLogger.debug("Received flow stats update : {}",notification.toString());
370         
371         for(FlowAndStatisticsMapList map: notification.getFlowAndStatisticsMapList()){
372             short tableId = map.getTableId();
373             
374             DataModificationTransaction it = this.statisticsManager.startChange();
375
376             boolean foundOriginalFlow = false;
377
378             FlowBuilder flowBuilder = new FlowBuilder();
379
380             FlowStatisticsDataBuilder flowStatisticsData = new FlowStatisticsDataBuilder();
381
382             FlowBuilder flow = new FlowBuilder();
383             flow.setContainerName(map.getContainerName());
384             flow.setBufferId(map.getBufferId());
385             flow.setCookie(map.getCookie());
386             flow.setCookieMask(map.getCookieMask());
387             flow.setFlags(map.getFlags());
388             flow.setFlowName(map.getFlowName());
389             flow.setHardTimeout(map.getHardTimeout());
390             if(map.getFlowId() != null)
391                 flow.setId(new FlowId(map.getFlowId().getValue()));
392             flow.setIdleTimeout(map.getIdleTimeout());
393             flow.setInstallHw(map.isInstallHw());
394             flow.setInstructions(map.getInstructions());
395             if(map.getFlowId()!= null)
396                 flow.setKey(new FlowKey(new FlowId(map.getKey().getFlowId().getValue())));
397             flow.setMatch(map.getMatch());
398             flow.setOutGroup(map.getOutGroup());
399             flow.setOutPort(map.getOutPort());
400             flow.setPriority(map.getPriority());
401             flow.setStrict(map.isStrict());
402             flow.setTableId(tableId);
403                 
404             Flow flowRule = flow.build();
405                 
406             FlowAndStatisticsMapListBuilder stats = new FlowAndStatisticsMapListBuilder();
407             stats.setByteCount(map.getByteCount());
408             stats.setPacketCount(map.getPacketCount());
409             stats.setDuration(map.getDuration());
410                 
411             GenericStatistics flowStats = stats.build();
412                 
413             //Add statistics to local cache
414             ConcurrentMap<NodeId, NodeStatisticsAger> cache = this.statisticsManager.getStatisticsCache();
415             if(!cache.containsKey(notification.getId())){
416                 cache.put(notification.getId(), new NodeStatisticsAger(statisticsManager,key));
417             }
418             NodeStatisticsAger nsa = cache.get(notification.getId());
419                 
420             //Augment the data to the flow node
421
422             FlowStatisticsBuilder flowStatistics = new FlowStatisticsBuilder();
423             flowStatistics.setByteCount(flowStats.getByteCount());
424             flowStatistics.setPacketCount(flowStats.getPacketCount());
425             flowStatistics.setDuration(flowStats.getDuration());
426             flowStatistics.setContainerName(map.getContainerName());
427             flowStatistics.setBufferId(map.getBufferId());
428             flowStatistics.setCookie(map.getCookie());
429             flowStatistics.setCookieMask(map.getCookieMask());
430             flowStatistics.setFlags(map.getFlags());
431             flowStatistics.setFlowName(map.getFlowName());
432             flowStatistics.setHardTimeout(map.getHardTimeout());
433             flowStatistics.setIdleTimeout(map.getIdleTimeout());
434             flowStatistics.setInstallHw(map.isInstallHw());
435             flowStatistics.setInstructions(map.getInstructions());
436             flowStatistics.setMatch(map.getMatch());
437             flowStatistics.setOutGroup(map.getOutGroup());
438             flowStatistics.setOutPort(map.getOutPort());
439             flowStatistics.setPriority(map.getPriority());
440             flowStatistics.setStrict(map.isStrict());
441             flowStatistics.setTableId(tableId);
442
443             flowStatisticsData.setFlowStatistics(flowStatistics.build());
444                 
445             sucLogger.debug("Flow : {}",flowRule.toString());
446             sucLogger.debug("Statistics to augment : {}",flowStatistics.build().toString());
447
448             InstanceIdentifier<Table> tableRef = InstanceIdentifier.builder(Nodes.class).child(Node.class, key)
449                     .augmentation(FlowCapableNode.class).child(Table.class, new TableKey(tableId)).toInstance();
450             
451             Table table= (Table)it.readConfigurationData(tableRef);
452
453             //TODO: Not a good way to do it, need to figure out better way.
454             //TODO: major issue in any alternate approach is that flow key is incrementally assigned 
455             //to the flows stored in data store.
456             // Augment same statistics to all the matching masked flow
457             if(table != null){
458
459                 for(Flow existingFlow : table.getFlow()){
460                     sucLogger.debug("Existing flow in data store : {}",existingFlow.toString());
461                     if(flowEquals(flowRule,existingFlow)){
462                         it = this.statisticsManager.startChange();
463                         InstanceIdentifier<Flow> flowRef = InstanceIdentifier.builder(Nodes.class).child(Node.class, key)
464                                 .augmentation(FlowCapableNode.class)
465                                 .child(Table.class, new TableKey(tableId))
466                                 .child(Flow.class,existingFlow.getKey()).toInstance();
467                         flowBuilder.setKey(existingFlow.getKey());
468                         flowBuilder.addAugmentation(FlowStatisticsData.class, flowStatisticsData.build());
469                         sucLogger.debug("Found matching flow in the datastore, augmenting statistics");
470                         foundOriginalFlow = true;
471                         // Update entry with timestamp of latest response 
472                         flow.setKey(existingFlow.getKey());
473                         FlowEntry flowStatsEntry = nsa.new FlowEntry(tableId,flow.build());
474                         cache.get(notification.getId()).updateFlowStats(flowStatsEntry);
475
476                         it.putOperationalData(flowRef, flowBuilder.build());
477                         it.commit();
478                     }
479                 }
480             }
481             
482             table= (Table)it.readOperationalData(tableRef);
483             if(!foundOriginalFlow && table != null){
484
485                 for(Flow existingFlow : table.getFlow()){
486                     FlowStatisticsData augmentedflowStatisticsData = existingFlow.getAugmentation(FlowStatisticsData.class);
487                     if(augmentedflowStatisticsData != null){
488                         FlowBuilder existingOperationalFlow = new FlowBuilder();
489                         existingOperationalFlow.fieldsFrom(augmentedflowStatisticsData.getFlowStatistics());
490                         sucLogger.debug("Existing unaccounted flow in operational data store : {}",existingFlow.toString());
491                         if(flowEquals(flowRule,existingOperationalFlow.build())){
492                             InstanceIdentifier<Flow> flowRef = InstanceIdentifier.builder(Nodes.class).child(Node.class, key)
493                                     .augmentation(FlowCapableNode.class)
494                                     .child(Table.class, new TableKey(tableId))
495                                     .child(Flow.class,existingFlow.getKey()).toInstance();
496                             flowBuilder.setKey(existingFlow.getKey());
497                             flowBuilder.addAugmentation(FlowStatisticsData.class, flowStatisticsData.build());
498                             sucLogger.debug("Found matching unaccounted flow in the operational datastore, augmenting statistics");
499                             foundOriginalFlow = true;
500                             
501                             // Update entry with timestamp of latest response 
502                             flow.setKey(existingFlow.getKey());
503                             FlowEntry flowStatsEntry = nsa.new FlowEntry(tableId,flow.build());
504                             cache.get(notification.getId()).updateFlowStats(flowStatsEntry);
505
506                             it.putOperationalData(flowRef, flowBuilder.build());
507                             it.commit();
508                             break;
509                         }
510                     }
511                 }
512             }
513             if(!foundOriginalFlow){
514                 String flowKey = "#UF$TABLE*"+Short.toString(tableId)+"*"+Integer.toString(this.unaccountedFlowsCounter);
515                 this.unaccountedFlowsCounter++;
516                 FlowKey newFlowKey = new FlowKey(new FlowId(flowKey));
517                 InstanceIdentifier<Flow> flowRef = InstanceIdentifier.builder(Nodes.class).child(Node.class, key)
518                         .augmentation(FlowCapableNode.class)
519                         .child(Table.class, new TableKey(tableId))
520                         .child(Flow.class,newFlowKey).toInstance();
521                 flowBuilder.setKey(newFlowKey);
522                 flowBuilder.addAugmentation(FlowStatisticsData.class, flowStatisticsData.build());
523                 sucLogger.debug("Flow {} is not present in config data store, augmenting statistics as an unaccounted flow",flowBuilder.build());
524                 
525                 // Update entry with timestamp of latest response 
526                 flow.setKey(newFlowKey);
527                 FlowEntry flowStatsEntry = nsa.new FlowEntry(tableId,flow.build());
528                 cache.get(notification.getId()).updateFlowStats(flowStatsEntry);
529
530                 it.putOperationalData(flowRef, flowBuilder.build());
531                 it.commit();
532             }
533         }
534     }
535
536     @Override
537     public void onAggregateFlowStatisticsUpdate(AggregateFlowStatisticsUpdate notification) {
538         //Check if response is for the request statistics-manager sent.
539         if(!messageManager.isRequestTxIdExist(notification.getId(),notification.getTransactionId(),notification.isMoreReplies()))
540             return;
541
542         NodeKey key = new NodeKey(notification.getId());
543         
544         Short tableId = messageManager.getTableIdForTxId(notification.getId(),notification.getTransactionId());
545         if(tableId != null){
546             
547             DataModificationTransaction it = this.statisticsManager.startChange();
548
549             InstanceIdentifier<Table> tableRef = InstanceIdentifier.builder(Nodes.class).child(Node.class, key)
550                     .augmentation(FlowCapableNode.class).child(Table.class, new TableKey(tableId)).toInstance();
551
552             AggregateFlowStatisticsDataBuilder aggregateFlowStatisticsDataBuilder = new AggregateFlowStatisticsDataBuilder();
553             AggregateFlowStatisticsBuilder aggregateFlowStatisticsBuilder = new AggregateFlowStatisticsBuilder();
554             aggregateFlowStatisticsBuilder.setByteCount(notification.getByteCount());
555             aggregateFlowStatisticsBuilder.setFlowCount(notification.getFlowCount());
556             aggregateFlowStatisticsBuilder.setPacketCount(notification.getPacketCount());
557             aggregateFlowStatisticsDataBuilder.setAggregateFlowStatistics(aggregateFlowStatisticsBuilder.build());
558             
559             sucLogger.debug("Augment aggregate statistics: {} for table {} on Node {}",aggregateFlowStatisticsBuilder.build().toString(),tableId,key);
560
561             TableBuilder tableBuilder = new TableBuilder();
562             tableBuilder.setKey(new TableKey(tableId));
563             tableBuilder.addAugmentation(AggregateFlowStatisticsData.class, aggregateFlowStatisticsDataBuilder.build());
564             it.putOperationalData(tableRef, tableBuilder.build());
565             it.commit();
566
567         }
568     }
569
570     @Override
571     public void onNodeConnectorStatisticsUpdate(NodeConnectorStatisticsUpdate notification) {
572         //Check if response is for the request statistics-manager sent.
573         if(!messageManager.isRequestTxIdExist(notification.getId(),notification.getTransactionId(),notification.isMoreReplies()))
574             return;
575
576         NodeKey key = new NodeKey(notification.getId());
577         
578         List<NodeConnectorStatisticsAndPortNumberMap> portsStats = notification.getNodeConnectorStatisticsAndPortNumberMap();
579         for(NodeConnectorStatisticsAndPortNumberMap portStats : portsStats){
580             
581             DataModificationTransaction it = this.statisticsManager.startChange();
582
583             FlowCapableNodeConnectorStatisticsBuilder statisticsBuilder 
584                                             = new FlowCapableNodeConnectorStatisticsBuilder();
585             statisticsBuilder.setBytes(portStats.getBytes());
586             statisticsBuilder.setCollisionCount(portStats.getCollisionCount());
587             statisticsBuilder.setDuration(portStats.getDuration());
588             statisticsBuilder.setPackets(portStats.getPackets());
589             statisticsBuilder.setReceiveCrcError(portStats.getReceiveCrcError());
590             statisticsBuilder.setReceiveDrops(portStats.getReceiveDrops());
591             statisticsBuilder.setReceiveErrors(portStats.getReceiveErrors());
592             statisticsBuilder.setReceiveFrameError(portStats.getReceiveFrameError());
593             statisticsBuilder.setReceiveOverRunError(portStats.getReceiveOverRunError());
594             statisticsBuilder.setTransmitDrops(portStats.getTransmitDrops());
595             statisticsBuilder.setTransmitErrors(portStats.getTransmitErrors());
596             
597             //Augment data to the node-connector
598             FlowCapableNodeConnectorStatisticsDataBuilder statisticsDataBuilder = 
599                     new FlowCapableNodeConnectorStatisticsDataBuilder();
600             
601             statisticsDataBuilder.setFlowCapableNodeConnectorStatistics(statisticsBuilder.build());
602             
603             InstanceIdentifier<NodeConnector> nodeConnectorRef = InstanceIdentifier.builder(Nodes.class).child(Node.class, key).child(NodeConnector.class, new NodeConnectorKey(portStats.getNodeConnectorId())).toInstance();
604             
605             NodeConnector nodeConnector = (NodeConnector)it.readOperationalData(nodeConnectorRef);
606             
607             if(nodeConnector != null){
608                 sucLogger.debug("Augmenting port statistics {} to port {}",statisticsDataBuilder.build().toString(),nodeConnectorRef.toString());
609                 NodeConnectorBuilder nodeConnectorBuilder = new NodeConnectorBuilder();
610                 nodeConnectorBuilder.addAugmentation(FlowCapableNodeConnectorStatisticsData.class, statisticsDataBuilder.build());
611                 it.putOperationalData(nodeConnectorRef, nodeConnectorBuilder.build());
612                 it.commit();
613             }
614         }
615     }
616
617     @Override
618     public void onFlowTableStatisticsUpdate(FlowTableStatisticsUpdate notification) {
619         //Check if response is for the request statistics-manager sent.
620         if(!messageManager.isRequestTxIdExist(notification.getId(),notification.getTransactionId(),notification.isMoreReplies()))
621             return;
622
623         NodeKey key = new NodeKey(notification.getId());
624         
625         List<FlowTableAndStatisticsMap> flowTablesStatsList = notification.getFlowTableAndStatisticsMap();
626         for (FlowTableAndStatisticsMap ftStats : flowTablesStatsList){
627             
628             DataModificationTransaction it = this.statisticsManager.startChange();
629
630             InstanceIdentifier<Table> tableRef = InstanceIdentifier.builder(Nodes.class).child(Node.class, key)
631                     .augmentation(FlowCapableNode.class).child(Table.class, new TableKey(ftStats.getTableId().getValue())).toInstance();
632             
633             FlowTableStatisticsDataBuilder statisticsDataBuilder = new FlowTableStatisticsDataBuilder();
634             
635             FlowTableStatisticsBuilder statisticsBuilder = new FlowTableStatisticsBuilder();
636             statisticsBuilder.setActiveFlows(ftStats.getActiveFlows());
637             statisticsBuilder.setPacketsLookedUp(ftStats.getPacketsLookedUp());
638             statisticsBuilder.setPacketsMatched(ftStats.getPacketsMatched());
639             
640             statisticsDataBuilder.setFlowTableStatistics(statisticsBuilder.build());
641             
642             sucLogger.debug("Augment flow table statistics: {} for table {} on Node {}",statisticsBuilder.build().toString(),ftStats.getTableId(),key);
643             
644             TableBuilder tableBuilder = new TableBuilder();
645             tableBuilder.setKey(new TableKey(ftStats.getTableId().getValue()));
646             tableBuilder.addAugmentation(FlowTableStatisticsData.class, statisticsDataBuilder.build());
647             it.putOperationalData(tableRef, tableBuilder.build());
648             it.commit();
649         }
650     }
651
652     @Override
653     public void onQueueStatisticsUpdate(QueueStatisticsUpdate notification) {
654         
655         //Check if response is for the request statistics-manager sent.
656         if(!messageManager.isRequestTxIdExist(notification.getId(),notification.getTransactionId(),notification.isMoreReplies()))
657             return;
658
659         NodeKey key = new NodeKey(notification.getId());
660         
661         //Add statistics to local cache
662         ConcurrentMap<NodeId, NodeStatisticsAger> cache = this.statisticsManager.getStatisticsCache();
663         if(!cache.containsKey(notification.getId())){
664             cache.put(notification.getId(), new NodeStatisticsAger(statisticsManager,key));
665         }
666         
667         NodeStatisticsAger nsa = cache.get(notification.getId());
668         
669         List<QueueIdAndStatisticsMap> queuesStats = notification.getQueueIdAndStatisticsMap();
670         for(QueueIdAndStatisticsMap swQueueStats : queuesStats){
671             
672             QueueEntry queueEntry = nsa.new QueueEntry(swQueueStats.getNodeConnectorId(),swQueueStats.getQueueId());
673             nsa.updateQueueStats(queueEntry);
674             
675             FlowCapableNodeConnectorQueueStatisticsDataBuilder queueStatisticsDataBuilder = new FlowCapableNodeConnectorQueueStatisticsDataBuilder();
676             
677             FlowCapableNodeConnectorQueueStatisticsBuilder queueStatisticsBuilder = new FlowCapableNodeConnectorQueueStatisticsBuilder();
678             
679             queueStatisticsBuilder.fieldsFrom(swQueueStats);
680             
681             queueStatisticsDataBuilder.setFlowCapableNodeConnectorQueueStatistics(queueStatisticsBuilder.build());
682             
683             DataModificationTransaction it = this.statisticsManager.startChange();
684
685             InstanceIdentifier<Queue> queueRef 
686                     = InstanceIdentifier.builder(Nodes.class)
687                                         .child(Node.class, key)
688                                         .child(NodeConnector.class, new NodeConnectorKey(swQueueStats.getNodeConnectorId()))
689                                         .augmentation(FlowCapableNodeConnector.class)
690                                         .child(Queue.class, new QueueKey(swQueueStats.getQueueId())).toInstance();
691             
692             QueueBuilder queueBuilder = new QueueBuilder();
693             queueBuilder.addAugmentation(FlowCapableNodeConnectorQueueStatisticsData.class, queueStatisticsDataBuilder.build());
694             queueBuilder.setKey(new QueueKey(swQueueStats.getQueueId()));
695
696             sucLogger.debug("Augmenting queue statistics {} of queue {} to port {}"
697                                         ,queueStatisticsDataBuilder.build().toString(),
698                                         swQueueStats.getQueueId(),
699                                         swQueueStats.getNodeConnectorId());
700             
701             it.putOperationalData(queueRef, queueBuilder.build());
702             it.commit();
703             
704         }
705         
706     }
707
708     private static NodeRef getNodeRef(NodeKey nodeKey){
709         InstanceIdentifierBuilder<?> builder = InstanceIdentifier.builder(Nodes.class).child(Node.class, nodeKey);
710         return new NodeRef(builder.toInstance());
711     }
712    
713     public boolean flowEquals(Flow statsFlow, Flow storedFlow) {
714         if (statsFlow.getClass() != storedFlow.getClass()) {
715             return false;
716         }
717         if (statsFlow.getContainerName()== null) {
718             if (storedFlow.getContainerName()!= null) {
719                 return false;
720             }
721         } else if(!statsFlow.getContainerName().equals(storedFlow.getContainerName())) {
722             return false;
723         }
724         if (statsFlow.getMatch()== null) {
725             if (storedFlow.getMatch() != null) {
726                 return false;
727             }
728         } //else if(!statsFlow.getMatch().equals(storedFlow.getMatch())) {
729         else if(!matchEquals(statsFlow.getMatch(), storedFlow.getMatch())) {
730             return false;
731         }
732         if (storedFlow.getPriority() == null) {
733             if (statsFlow.getPriority() != null && statsFlow.getPriority()!= 0x8000) {
734                 return false;
735             }
736         } else if(!statsFlow.getPriority().equals(storedFlow.getPriority())) {
737             return false;
738         }
739         if (statsFlow.getTableId() == null) {
740             if (storedFlow.getTableId() != null) {
741                 return false;
742             }
743         } else if(!statsFlow.getTableId().equals(storedFlow.getTableId())) {
744             return false;
745         }
746         return true;
747     }
748     
749     /**
750      * Explicit equals method to compare the 'match' for flows stored in the data-stores and flow fetched from the switch.
751      * Flow installation process has three steps 
752      * 1) Store flow in config data store 
753      * 2) and send it to plugin for installation
754      * 3) Flow gets installed in switch
755      * 
756      * The flow user wants to install and what finally gets installed in switch can be slightly different. 
757      * E.g, If user installs flow with src/dst ip=10.0.0.1/24, when it get installed in the switch
758      * src/dst ip will be changes to 10.0.0.0/24 because of netmask of 24. When statistics manager fetch 
759      * stats it gets 10.0.0.0/24 rather then 10.0.0.1/24. Custom match takes care of by using masked ip
760      * while comparing two ip addresses.
761      * 
762      * Sometimes when user don't provide few values that is required by flow installation request, like 
763      * priority,hard timeout, idle timeout, cookies etc, plugin usages default values before sending 
764      * request to the switch. So when statistics manager gets flow statistics, it gets the default value.
765      * But the flow stored in config data store don't have those defaults value. I included those checks 
766      * in the customer flow/match equal function. 
767      * 
768      * 
769      * @param statsFlow
770      * @param storedFlow
771      * @return
772      */
773     
774     public boolean matchEquals(Match statsFlow, Match storedFlow) {
775         if (statsFlow == storedFlow) {
776             return true;
777         }
778         if (storedFlow.getClass() != statsFlow.getClass()) {
779             return false;
780         }
781         if (storedFlow.getEthernetMatch() == null) {
782             if (statsFlow.getEthernetMatch() != null) {
783                 return false;
784             }
785         } else if(!storedFlow.getEthernetMatch().equals(statsFlow.getEthernetMatch())) {
786             return false;
787         }
788         if (storedFlow.getIcmpv4Match()== null) {
789             if (statsFlow.getIcmpv4Match() != null) {
790                 return false;
791             }
792         } else if(!storedFlow.getIcmpv4Match().equals(statsFlow.getIcmpv4Match())) {
793             return false;
794         }
795         if (storedFlow.getIcmpv6Match() == null) {
796             if (statsFlow.getIcmpv6Match() != null) {
797                 return false;
798             }
799         } else if(!storedFlow.getIcmpv6Match().equals(statsFlow.getIcmpv6Match())) {
800             return false;
801         }
802         if (storedFlow.getInPhyPort() == null) {
803             if (statsFlow.getInPhyPort() != null) {
804                 return false;
805             }
806         } else if(!storedFlow.getInPhyPort().equals(statsFlow.getInPhyPort())) {
807             return false;
808         }
809         if (storedFlow.getInPort()== null) {
810             if (statsFlow.getInPort() != null) {
811                 return false;
812             }
813         } else if(!storedFlow.getInPort().equals(statsFlow.getInPort())) {
814             return false;
815         }
816         if (storedFlow.getIpMatch()== null) {
817             if (statsFlow.getIpMatch() != null) {
818                 return false;
819             }
820         } else if(!storedFlow.getIpMatch().equals(statsFlow.getIpMatch())) {
821             return false;
822         }
823         if (storedFlow.getLayer3Match()== null) {
824             if (statsFlow.getLayer3Match() != null) {
825                     return false;
826             }
827         } else if(!layer3MatchEquals(statsFlow.getLayer3Match(),storedFlow.getLayer3Match())) {
828             return false;
829         }
830         if (storedFlow.getLayer4Match()== null) {
831             if (statsFlow.getLayer4Match() != null) {
832                 return false;
833             }
834         } else if(!storedFlow.getLayer4Match().equals(statsFlow.getLayer4Match())) {
835             return false;
836         }
837         if (storedFlow.getMetadata() == null) {
838             if (statsFlow.getMetadata() != null) {
839                 return false;
840             }
841         } else if(!storedFlow.getMetadata().equals(statsFlow.getMetadata())) {
842             return false;
843         }
844         if (storedFlow.getProtocolMatchFields() == null) {
845             if (statsFlow.getProtocolMatchFields() != null) {
846                 return false;
847             }
848         } else if(!storedFlow.getProtocolMatchFields().equals(statsFlow.getProtocolMatchFields())) {
849             return false;
850         }
851         if (storedFlow.getTunnel()== null) {
852             if (statsFlow.getTunnel() != null) {
853                 return false;
854             }
855         } else if(!storedFlow.getTunnel().equals(statsFlow.getTunnel())) {
856             return false;
857         }
858         if (storedFlow.getVlanMatch()== null) {
859             if (statsFlow.getVlanMatch() != null) {
860                 return false;
861             }
862         } else if(!storedFlow.getVlanMatch().equals(statsFlow.getVlanMatch())) {
863             return false;
864         }
865         return true;
866     }
867
868     protected static boolean layer3MatchEquals(Layer3Match statsLayer3Match, Layer3Match storedLayer3Match){
869         boolean verdict = true; 
870         if(statsLayer3Match instanceof Ipv4Match && storedLayer3Match instanceof Ipv4Match){
871             Ipv4Match statsIpv4Match = (Ipv4Match)statsLayer3Match;
872             Ipv4Match storedIpv4Match = (Ipv4Match)storedLayer3Match;
873
874             if (verdict) {
875                 verdict = compareNullSafe(
876                         storedIpv4Match.getIpv4Destination(), statsIpv4Match.getIpv4Destination());
877             }
878             if (verdict) {
879                 verdict = compareNullSafe(
880                         statsIpv4Match.getIpv4Source(), storedIpv4Match.getIpv4Source());
881             }
882         } else {
883             Boolean nullCheckOut = checkNullValues(storedLayer3Match, statsLayer3Match);
884             if (nullCheckOut != null) {
885                 verdict = nullCheckOut;
886             } else {
887                 verdict = storedLayer3Match.equals(statsLayer3Match);
888             }
889         }
890         
891         return verdict;
892     }
893     
894     private static boolean compareNullSafe(Ipv4Prefix statsIpv4, Ipv4Prefix storedIpv4) {
895         boolean verdict = true;
896         Boolean checkDestNullValuesOut = checkNullValues(storedIpv4, statsIpv4);
897         if (checkDestNullValuesOut != null) {
898             verdict = checkDestNullValuesOut;
899         } else if(!IpAddressEquals(statsIpv4, storedIpv4)){
900             verdict = false;
901         }
902         
903         return verdict;
904     }
905     
906     private static Boolean checkNullValues(Object v1, Object v2) {
907         Boolean verdict = null;
908         if (v1 == null && v2 != null) {
909             verdict = Boolean.FALSE;
910         } else if (v1 != null && v2 == null) {
911             verdict = Boolean.FALSE;
912         } else if (v1 == null && v2 == null) {
913             verdict = Boolean.TRUE;
914         }
915         
916         return verdict;
917     }
918     
919     /**
920      * TODO: why don't we use the default Ipv4Prefix.equals()?
921      * 
922      * @param statsIpAddress
923      * @param storedIpAddress
924      * @return true if IPv4prefixes equals 
925      */
926     private static boolean IpAddressEquals(Ipv4Prefix statsIpAddress, Ipv4Prefix storedIpAddress) {
927         IntegerIpAddress statsIpAddressInt = StrIpToIntIp(statsIpAddress.getValue());
928         IntegerIpAddress storedIpAddressInt = StrIpToIntIp(storedIpAddress.getValue());
929
930         if(IpAndMaskBasedMatch(statsIpAddressInt,storedIpAddressInt)){
931             return true;
932         }
933         if(IpBasedMatch(statsIpAddressInt,storedIpAddressInt)){
934             return true;
935         }
936         return false;
937     }
938
939     private static boolean IpAndMaskBasedMatch(IntegerIpAddress statsIpAddressInt,IntegerIpAddress storedIpAddressInt){
940         return ((statsIpAddressInt.getIp() & statsIpAddressInt.getMask()) ==  (storedIpAddressInt.getIp() & storedIpAddressInt.getMask()));
941     }
942
943     private static boolean IpBasedMatch(IntegerIpAddress statsIpAddressInt,IntegerIpAddress storedIpAddressInt){
944         return (statsIpAddressInt.getIp() == storedIpAddressInt.getIp());
945     }
946     
947     /**
948      * Method return integer version of ip address. Converted int will be mask if 
949      * mask specified
950      */
951     private static IntegerIpAddress StrIpToIntIp(String ipAddresss){
952         
953         String[] parts = ipAddresss.split("/");
954         String ip = parts[0];
955         int prefix;
956
957         if (parts.length < 2) {
958             prefix = 32;
959         } else {
960             prefix = Integer.parseInt(parts[1]);
961         }
962
963         IntegerIpAddress integerIpAddress = null;
964         try {
965             Inet4Address addr = (Inet4Address) InetAddress.getByName(ip);
966             byte[] addrBytes = addr.getAddress();
967             int ipInt = ((addrBytes[0] & 0xFF) << 24) |
968                     ((addrBytes[1] & 0xFF) << 16) |
969                     ((addrBytes[2] & 0xFF) << 8)  |
970                     ((addrBytes[3] & 0xFF) << 0);
971             
972             int mask = 0xffffffff << 32 - prefix;
973             
974             integerIpAddress = new IntegerIpAddress(ipInt, mask);
975         } catch (UnknownHostException e){
976             sucLogger.error("Failed to determine host IP address by name: {}", e.getMessage(), e);
977         }
978
979         return integerIpAddress;
980     }
981     
982     static class IntegerIpAddress{
983         int ip;
984         int mask;
985         public IntegerIpAddress(int ip, int mask) {
986             this.ip = ip;
987             this.mask = mask;
988         }
989         public int getIp() {
990             return ip;
991         }
992         public int getMask() {
993             return mask;
994         }
995     }
996 }
997