Merge "Prepare for transitioning NodeStatisticsAger"
[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
14 import org.opendaylight.controller.md.statistics.manager.NodeStatisticsAger.FlowEntry;
15 import org.opendaylight.controller.sal.binding.api.data.DataModificationTransaction;
16 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Prefix;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode;
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.tables.Table;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.TableKey;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.Flow;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.FlowBuilder;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.FlowKey;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.AggregateFlowStatisticsUpdate;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.FlowStatisticsData;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.FlowStatisticsDataBuilder;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.FlowsStatisticsUpdate;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.OpendaylightFlowStatisticsListener;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.flow.and.statistics.map.list.FlowAndStatisticsMapList;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.flow.and.statistics.map.list.FlowAndStatisticsMapListBuilder;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.flow.statistics.FlowStatisticsBuilder;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.table.statistics.rev131215.FlowTableStatisticsUpdate;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.table.statistics.rev131215.OpendaylightFlowTableStatisticsListener;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.Match;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.statistics.rev131111.GroupDescStatsUpdated;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.statistics.rev131111.GroupFeaturesUpdated;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.statistics.rev131111.GroupStatisticsUpdated;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.statistics.rev131111.OpendaylightGroupStatisticsListener;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.MeterConfigStatsUpdated;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.MeterFeaturesUpdated;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.MeterStatisticsUpdated;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.OpendaylightMeterStatisticsListener;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.Layer3Match;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._3.match.Ipv4Match;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.statistics.types.rev130925.GenericStatistics;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.port.statistics.rev131214.NodeConnectorStatisticsUpdate;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.port.statistics.rev131214.OpendaylightPortStatisticsListener;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.queue.statistics.rev131216.OpendaylightQueueStatisticsListener;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.queue.statistics.rev131216.QueueStatisticsUpdate;
53 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
54 import org.slf4j.Logger;
55 import org.slf4j.LoggerFactory;
56
57 /**
58  * Class implement statistics manager related listener interface and augment all the
59  * received statistics data to data stores.
60  * TODO: Need to add error message listener and clean-up the associated tx id
61  * if it exists in the tx-id cache.
62  * @author vishnoianil
63  *
64  */
65 public class StatisticsUpdateCommiter implements OpendaylightGroupStatisticsListener,
66         OpendaylightMeterStatisticsListener,
67         OpendaylightFlowStatisticsListener,
68         OpendaylightPortStatisticsListener,
69         OpendaylightFlowTableStatisticsListener,
70         OpendaylightQueueStatisticsListener{
71
72     private final static Logger sucLogger = LoggerFactory.getLogger(StatisticsUpdateCommiter.class);
73
74     private final StatisticsProvider statisticsManager;
75     private final MultipartMessageManager messageManager;
76
77     private int unaccountedFlowsCounter = 1;
78
79     /**
80      * default ctor
81      * @param manager
82      */
83     public StatisticsUpdateCommiter(final StatisticsProvider manager){
84         this.statisticsManager = manager;
85         this.messageManager = this.statisticsManager.getMultipartMessageManager();
86     }
87
88     @Override
89     public void onMeterConfigStatsUpdated(final MeterConfigStatsUpdated notification) {
90         //Check if response is for the request statistics-manager sent.
91         if(!messageManager.isRequestTxIdExist(notification.getId(),notification.getTransactionId(),notification.isMoreReplies()))
92             return;
93
94         //Add statistics to local cache
95         final NodeStatisticsAger sna = this.statisticsManager.getStatisticsHandler(notification.getId());
96         if (sna != null) {
97             sna.updateMeterConfigStats(notification.getMeterConfigStats());
98         }
99     }
100
101     @Override
102     public void onMeterStatisticsUpdated(MeterStatisticsUpdated notification) {
103         //Check if response is for the request statistics-manager sent.
104         if(!messageManager.isRequestTxIdExist(notification.getId(),notification.getTransactionId(),notification.isMoreReplies()))
105             return;
106
107         //Add statistics to local cache
108         final NodeStatisticsAger nsa = this.statisticsManager.getStatisticsHandler(notification.getId());
109         if (nsa != null) {
110             nsa.updateMeterStats(notification.getMeterStats());
111         }
112     }
113
114     @Override
115     public void onGroupDescStatsUpdated(GroupDescStatsUpdated notification) {
116         //Check if response is for the request statistics-manager sent.
117         if(!messageManager.isRequestTxIdExist(notification.getId(),notification.getTransactionId(),notification.isMoreReplies()))
118             return;
119
120         final NodeStatisticsAger nsa = statisticsManager.getStatisticsHandler(notification.getId());
121         if (nsa != null) {
122             nsa.updateGroupDescStats(notification.getGroupDescStats());
123         }
124     }
125
126     @Override
127     public void onGroupStatisticsUpdated(GroupStatisticsUpdated notification) {
128         //Check if response is for the request statistics-manager sent.
129         if(!messageManager.isRequestTxIdExist(notification.getId(),notification.getTransactionId(),notification.isMoreReplies()))
130             return;
131
132         final NodeStatisticsAger nsa = statisticsManager.getStatisticsHandler(notification.getId());
133         if (nsa != null) {
134             nsa.updateGroupStats(notification.getGroupStats());
135         }
136     }
137
138     @Override
139     public void onMeterFeaturesUpdated(MeterFeaturesUpdated notification) {
140         final NodeStatisticsAger sna = this.statisticsManager.getStatisticsHandler(notification.getId());
141         if (sna != null) {
142             sna.updateMeterFeatures(notification);
143         }
144     }
145
146     @Override
147     public void onGroupFeaturesUpdated(GroupFeaturesUpdated notification) {
148         final NodeStatisticsAger sna = this.statisticsManager.getStatisticsHandler(notification.getId());
149         if (sna != null) {
150             sna.updateGroupFeatures(notification);
151         }
152     }
153
154     @Override
155     public void onFlowsStatisticsUpdate(final FlowsStatisticsUpdate notification) {
156
157         //Check if response is for the request statistics-manager sent.
158         if(!messageManager.isRequestTxIdExist(notification.getId(),notification.getTransactionId(),notification.isMoreReplies()))
159             return;
160
161         sucLogger.debug("Received flow stats update : {}",notification.toString());
162
163         final NodeKey key = new NodeKey(notification.getId());
164         final NodeStatisticsAger nsa =  this.statisticsManager.getStatisticsHandler(key.getId());
165         DataModificationTransaction it = this.statisticsManager.startChange();
166
167         for(FlowAndStatisticsMapList map: notification.getFlowAndStatisticsMapList()){
168             short tableId = map.getTableId();
169
170
171             boolean foundOriginalFlow = false;
172
173             FlowBuilder flowBuilder = new FlowBuilder();
174
175             FlowStatisticsDataBuilder flowStatisticsData = new FlowStatisticsDataBuilder();
176
177             FlowBuilder flow = new FlowBuilder();
178             flow.setContainerName(map.getContainerName());
179             flow.setBufferId(map.getBufferId());
180             flow.setCookie(map.getCookie());
181             flow.setCookieMask(map.getCookieMask());
182             flow.setFlags(map.getFlags());
183             flow.setFlowName(map.getFlowName());
184             flow.setHardTimeout(map.getHardTimeout());
185             if(map.getFlowId() != null)
186                 flow.setId(new FlowId(map.getFlowId().getValue()));
187             flow.setIdleTimeout(map.getIdleTimeout());
188             flow.setInstallHw(map.isInstallHw());
189             flow.setInstructions(map.getInstructions());
190             if(map.getFlowId()!= null)
191                 flow.setKey(new FlowKey(new FlowId(map.getKey().getFlowId().getValue())));
192             flow.setMatch(map.getMatch());
193             flow.setOutGroup(map.getOutGroup());
194             flow.setOutPort(map.getOutPort());
195             flow.setPriority(map.getPriority());
196             flow.setStrict(map.isStrict());
197             flow.setTableId(tableId);
198
199             Flow flowRule = flow.build();
200
201             FlowAndStatisticsMapListBuilder stats = new FlowAndStatisticsMapListBuilder();
202             stats.setByteCount(map.getByteCount());
203             stats.setPacketCount(map.getPacketCount());
204             stats.setDuration(map.getDuration());
205
206             GenericStatistics flowStats = stats.build();
207
208             //Augment the data to the flow node
209
210             FlowStatisticsBuilder flowStatistics = new FlowStatisticsBuilder();
211             flowStatistics.setByteCount(flowStats.getByteCount());
212             flowStatistics.setPacketCount(flowStats.getPacketCount());
213             flowStatistics.setDuration(flowStats.getDuration());
214             flowStatistics.setContainerName(map.getContainerName());
215             flowStatistics.setBufferId(map.getBufferId());
216             flowStatistics.setCookie(map.getCookie());
217             flowStatistics.setCookieMask(map.getCookieMask());
218             flowStatistics.setFlags(map.getFlags());
219             flowStatistics.setFlowName(map.getFlowName());
220             flowStatistics.setHardTimeout(map.getHardTimeout());
221             flowStatistics.setIdleTimeout(map.getIdleTimeout());
222             flowStatistics.setInstallHw(map.isInstallHw());
223             flowStatistics.setInstructions(map.getInstructions());
224             flowStatistics.setMatch(map.getMatch());
225             flowStatistics.setOutGroup(map.getOutGroup());
226             flowStatistics.setOutPort(map.getOutPort());
227             flowStatistics.setPriority(map.getPriority());
228             flowStatistics.setStrict(map.isStrict());
229             flowStatistics.setTableId(tableId);
230
231             flowStatisticsData.setFlowStatistics(flowStatistics.build());
232
233             sucLogger.debug("Flow : {}",flowRule.toString());
234             sucLogger.debug("Statistics to augment : {}",flowStatistics.build().toString());
235
236             InstanceIdentifier<Table> tableRef = InstanceIdentifier.builder(Nodes.class).child(Node.class, key)
237                     .augmentation(FlowCapableNode.class).child(Table.class, new TableKey(tableId)).toInstance();
238
239             Table table= (Table)it.readConfigurationData(tableRef);
240
241             //TODO: Not a good way to do it, need to figure out better way.
242             //TODO: major issue in any alternate approach is that flow key is incrementally assigned
243             //to the flows stored in data store.
244             // Augment same statistics to all the matching masked flow
245             if(table != null){
246
247                 for(Flow existingFlow : table.getFlow()){
248                     sucLogger.debug("Existing flow in data store : {}",existingFlow.toString());
249                     if(flowEquals(flowRule,existingFlow)){
250                         InstanceIdentifier<Flow> flowRef = InstanceIdentifier.builder(Nodes.class).child(Node.class, key)
251                                 .augmentation(FlowCapableNode.class)
252                                 .child(Table.class, new TableKey(tableId))
253                                 .child(Flow.class,existingFlow.getKey()).toInstance();
254                         flowBuilder.setKey(existingFlow.getKey());
255                         flowBuilder.addAugmentation(FlowStatisticsData.class, flowStatisticsData.build());
256                         sucLogger.debug("Found matching flow in the datastore, augmenting statistics");
257                         foundOriginalFlow = true;
258                         // Update entry with timestamp of latest response
259                         flow.setKey(existingFlow.getKey());
260                         FlowEntry flowStatsEntry = nsa.new FlowEntry(tableId,flow.build());
261                         nsa.updateFlowStats(flowStatsEntry);
262
263                         it.putOperationalData(flowRef, flowBuilder.build());
264                     }
265                 }
266             }
267
268             table= (Table)it.readOperationalData(tableRef);
269             if(!foundOriginalFlow && table != null){
270
271                 for(Flow existingFlow : table.getFlow()){
272                     FlowStatisticsData augmentedflowStatisticsData = existingFlow.getAugmentation(FlowStatisticsData.class);
273                     if(augmentedflowStatisticsData != null){
274                         FlowBuilder existingOperationalFlow = new FlowBuilder();
275                         existingOperationalFlow.fieldsFrom(augmentedflowStatisticsData.getFlowStatistics());
276                         sucLogger.debug("Existing unaccounted flow in operational data store : {}",existingFlow.toString());
277                         if(flowEquals(flowRule,existingOperationalFlow.build())){
278                             InstanceIdentifier<Flow> flowRef = InstanceIdentifier.builder(Nodes.class).child(Node.class, key)
279                                     .augmentation(FlowCapableNode.class)
280                                     .child(Table.class, new TableKey(tableId))
281                                     .child(Flow.class,existingFlow.getKey()).toInstance();
282                             flowBuilder.setKey(existingFlow.getKey());
283                             flowBuilder.addAugmentation(FlowStatisticsData.class, flowStatisticsData.build());
284                             sucLogger.debug("Found matching unaccounted flow in the operational datastore, augmenting statistics");
285                             foundOriginalFlow = true;
286
287                             // Update entry with timestamp of latest response
288                             flow.setKey(existingFlow.getKey());
289                             FlowEntry flowStatsEntry = nsa.new FlowEntry(tableId,flow.build());
290                             nsa.updateFlowStats(flowStatsEntry);
291
292                             it.putOperationalData(flowRef, flowBuilder.build());
293                             break;
294                         }
295                     }
296                 }
297             }
298             if(!foundOriginalFlow){
299                 String flowKey = "#UF$TABLE*"+Short.toString(tableId)+"*"+Integer.toString(this.unaccountedFlowsCounter);
300                 this.unaccountedFlowsCounter++;
301                 FlowKey newFlowKey = new FlowKey(new FlowId(flowKey));
302                 InstanceIdentifier<Flow> flowRef = InstanceIdentifier.builder(Nodes.class).child(Node.class, key)
303                         .augmentation(FlowCapableNode.class)
304                         .child(Table.class, new TableKey(tableId))
305                         .child(Flow.class,newFlowKey).toInstance();
306                 flowBuilder.setKey(newFlowKey);
307                 flowBuilder.addAugmentation(FlowStatisticsData.class, flowStatisticsData.build());
308                 sucLogger.debug("Flow {} is not present in config data store, augmenting statistics as an unaccounted flow",flowBuilder.build());
309
310                 // Update entry with timestamp of latest response
311                 flow.setKey(newFlowKey);
312                 FlowEntry flowStatsEntry = nsa.new FlowEntry(tableId,flow.build());
313                 nsa.updateFlowStats(flowStatsEntry);
314
315                 it.putOperationalData(flowRef, flowBuilder.build());
316             }
317         }
318         it.commit();
319     }
320
321     @Override
322     public void onAggregateFlowStatisticsUpdate(AggregateFlowStatisticsUpdate notification) {
323         //Check if response is for the request statistics-manager sent.
324         if(!messageManager.isRequestTxIdExist(notification.getId(),notification.getTransactionId(),notification.isMoreReplies()))
325             return;
326
327         final NodeStatisticsAger nsa = this.statisticsManager.getStatisticsHandler(notification.getId());
328         if (nsa != null) {
329             final Short tableId = messageManager.getTableIdForTxId(notification.getId(),notification.getTransactionId());
330             nsa.updateAggregateFlowStats(tableId, notification);
331         }
332     }
333
334     @Override
335     public void onNodeConnectorStatisticsUpdate(NodeConnectorStatisticsUpdate notification) {
336         //Check if response is for the request statistics-manager sent.
337         if(!messageManager.isRequestTxIdExist(notification.getId(),notification.getTransactionId(),notification.isMoreReplies()))
338             return;
339
340         final NodeStatisticsAger nsa = this.statisticsManager.getStatisticsHandler(notification.getId());
341         if (nsa != null) {
342             nsa.updateNodeConnectorStats(notification.getNodeConnectorStatisticsAndPortNumberMap());
343         }
344     }
345
346     @Override
347     public void onFlowTableStatisticsUpdate(FlowTableStatisticsUpdate notification) {
348         //Check if response is for the request statistics-manager sent.
349         if(!messageManager.isRequestTxIdExist(notification.getId(),notification.getTransactionId(),notification.isMoreReplies()))
350             return;
351
352         final NodeStatisticsAger nsa = this.statisticsManager.getStatisticsHandler(notification.getId());
353         if (nsa != null) {
354             nsa.updateFlowTableStats(notification.getFlowTableAndStatisticsMap());
355         }
356     }
357
358     @Override
359     public void onQueueStatisticsUpdate(QueueStatisticsUpdate notification) {
360
361         //Check if response is for the request statistics-manager sent.
362         if(!messageManager.isRequestTxIdExist(notification.getId(),notification.getTransactionId(),notification.isMoreReplies()))
363             return;
364
365         //Add statistics to local cache
366         final NodeStatisticsAger nsa = this.statisticsManager.getStatisticsHandler(notification.getId());
367         if (nsa != null) {
368             nsa.updateQueueStats(notification.getQueueIdAndStatisticsMap());
369         }
370     }
371
372     public static boolean flowEquals(Flow statsFlow, Flow storedFlow) {
373         if (statsFlow.getClass() != storedFlow.getClass()) {
374             return false;
375         }
376         if (statsFlow.getContainerName()== null) {
377             if (storedFlow.getContainerName()!= null) {
378                 return false;
379             }
380         } else if(!statsFlow.getContainerName().equals(storedFlow.getContainerName())) {
381             return false;
382         }
383         if (statsFlow.getMatch()== null) {
384             if (storedFlow.getMatch() != null) {
385                 return false;
386             }
387         } //else if(!statsFlow.getMatch().equals(storedFlow.getMatch())) {
388         else if(!matchEquals(statsFlow.getMatch(), storedFlow.getMatch())) {
389             return false;
390         }
391         if (storedFlow.getPriority() == null) {
392             if (statsFlow.getPriority() != null && statsFlow.getPriority()!= 0x8000) {
393                 return false;
394             }
395         } else if(!statsFlow.getPriority().equals(storedFlow.getPriority())) {
396             return false;
397         }
398         if (statsFlow.getTableId() == null) {
399             if (storedFlow.getTableId() != null) {
400                 return false;
401             }
402         } else if(!statsFlow.getTableId().equals(storedFlow.getTableId())) {
403             return false;
404         }
405         return true;
406     }
407
408     /**
409      * Explicit equals method to compare the 'match' for flows stored in the data-stores and flow fetched from the switch.
410      * Flow installation process has three steps
411      * 1) Store flow in config data store
412      * 2) and send it to plugin for installation
413      * 3) Flow gets installed in switch
414      *
415      * The flow user wants to install and what finally gets installed in switch can be slightly different.
416      * E.g, If user installs flow with src/dst ip=10.0.0.1/24, when it get installed in the switch
417      * src/dst ip will be changes to 10.0.0.0/24 because of netmask of 24. When statistics manager fetch
418      * stats it gets 10.0.0.0/24 rather then 10.0.0.1/24. Custom match takes care of by using masked ip
419      * while comparing two ip addresses.
420      *
421      * Sometimes when user don't provide few values that is required by flow installation request, like
422      * priority,hard timeout, idle timeout, cookies etc, plugin usages default values before sending
423      * request to the switch. So when statistics manager gets flow statistics, it gets the default value.
424      * But the flow stored in config data store don't have those defaults value. I included those checks
425      * in the customer flow/match equal function.
426      *
427      *
428      * @param statsFlow
429      * @param storedFlow
430      * @return
431      */
432
433     public static boolean matchEquals(Match statsFlow, Match storedFlow) {
434         if (statsFlow == storedFlow) {
435             return true;
436         }
437         if (storedFlow.getClass() != statsFlow.getClass()) {
438             return false;
439         }
440         if (storedFlow.getEthernetMatch() == null) {
441             if (statsFlow.getEthernetMatch() != null) {
442                 return false;
443             }
444         } else if(!storedFlow.getEthernetMatch().equals(statsFlow.getEthernetMatch())) {
445             return false;
446         }
447         if (storedFlow.getIcmpv4Match()== null) {
448             if (statsFlow.getIcmpv4Match() != null) {
449                 return false;
450             }
451         } else if(!storedFlow.getIcmpv4Match().equals(statsFlow.getIcmpv4Match())) {
452             return false;
453         }
454         if (storedFlow.getIcmpv6Match() == null) {
455             if (statsFlow.getIcmpv6Match() != null) {
456                 return false;
457             }
458         } else if(!storedFlow.getIcmpv6Match().equals(statsFlow.getIcmpv6Match())) {
459             return false;
460         }
461         if (storedFlow.getInPhyPort() == null) {
462             if (statsFlow.getInPhyPort() != null) {
463                 return false;
464             }
465         } else if(!storedFlow.getInPhyPort().equals(statsFlow.getInPhyPort())) {
466             return false;
467         }
468         if (storedFlow.getInPort()== null) {
469             if (statsFlow.getInPort() != null) {
470                 return false;
471             }
472         } else if(!storedFlow.getInPort().equals(statsFlow.getInPort())) {
473             return false;
474         }
475         if (storedFlow.getIpMatch()== null) {
476             if (statsFlow.getIpMatch() != null) {
477                 return false;
478             }
479         } else if(!storedFlow.getIpMatch().equals(statsFlow.getIpMatch())) {
480             return false;
481         }
482         if (storedFlow.getLayer3Match()== null) {
483             if (statsFlow.getLayer3Match() != null) {
484                     return false;
485             }
486         } else if(!layer3MatchEquals(statsFlow.getLayer3Match(),storedFlow.getLayer3Match())) {
487             return false;
488         }
489         if (storedFlow.getLayer4Match()== null) {
490             if (statsFlow.getLayer4Match() != null) {
491                 return false;
492             }
493         } else if(!storedFlow.getLayer4Match().equals(statsFlow.getLayer4Match())) {
494             return false;
495         }
496         if (storedFlow.getMetadata() == null) {
497             if (statsFlow.getMetadata() != null) {
498                 return false;
499             }
500         } else if(!storedFlow.getMetadata().equals(statsFlow.getMetadata())) {
501             return false;
502         }
503         if (storedFlow.getProtocolMatchFields() == null) {
504             if (statsFlow.getProtocolMatchFields() != null) {
505                 return false;
506             }
507         } else if(!storedFlow.getProtocolMatchFields().equals(statsFlow.getProtocolMatchFields())) {
508             return false;
509         }
510         if (storedFlow.getTunnel()== null) {
511             if (statsFlow.getTunnel() != null) {
512                 return false;
513             }
514         } else if(!storedFlow.getTunnel().equals(statsFlow.getTunnel())) {
515             return false;
516         }
517         if (storedFlow.getVlanMatch()== null) {
518             if (statsFlow.getVlanMatch() != null) {
519                 return false;
520             }
521         } else if(!storedFlow.getVlanMatch().equals(statsFlow.getVlanMatch())) {
522             return false;
523         }
524         return true;
525     }
526
527     protected static boolean layer3MatchEquals(Layer3Match statsLayer3Match, Layer3Match storedLayer3Match){
528         boolean verdict = true;
529         if(statsLayer3Match instanceof Ipv4Match && storedLayer3Match instanceof Ipv4Match){
530             Ipv4Match statsIpv4Match = (Ipv4Match)statsLayer3Match;
531             Ipv4Match storedIpv4Match = (Ipv4Match)storedLayer3Match;
532
533             if (verdict) {
534                 verdict = compareNullSafe(
535                         storedIpv4Match.getIpv4Destination(), statsIpv4Match.getIpv4Destination());
536             }
537             if (verdict) {
538                 verdict = compareNullSafe(
539                         statsIpv4Match.getIpv4Source(), storedIpv4Match.getIpv4Source());
540             }
541         } else {
542             Boolean nullCheckOut = checkNullValues(storedLayer3Match, statsLayer3Match);
543             if (nullCheckOut != null) {
544                 verdict = nullCheckOut;
545             } else {
546                 verdict = storedLayer3Match.equals(statsLayer3Match);
547             }
548         }
549
550         return verdict;
551     }
552
553     private static boolean compareNullSafe(Ipv4Prefix statsIpv4, Ipv4Prefix storedIpv4) {
554         boolean verdict = true;
555         Boolean checkDestNullValuesOut = checkNullValues(storedIpv4, statsIpv4);
556         if (checkDestNullValuesOut != null) {
557             verdict = checkDestNullValuesOut;
558         } else if(!IpAddressEquals(statsIpv4, storedIpv4)){
559             verdict = false;
560         }
561
562         return verdict;
563     }
564
565     private static Boolean checkNullValues(Object v1, Object v2) {
566         Boolean verdict = null;
567         if (v1 == null && v2 != null) {
568             verdict = Boolean.FALSE;
569         } else if (v1 != null && v2 == null) {
570             verdict = Boolean.FALSE;
571         } else if (v1 == null && v2 == null) {
572             verdict = Boolean.TRUE;
573         }
574
575         return verdict;
576     }
577
578     /**
579      * TODO: why don't we use the default Ipv4Prefix.equals()?
580      *
581      * @param statsIpAddress
582      * @param storedIpAddress
583      * @return true if IPv4prefixes equals
584      */
585     private static boolean IpAddressEquals(Ipv4Prefix statsIpAddress, Ipv4Prefix storedIpAddress) {
586         IntegerIpAddress statsIpAddressInt = StrIpToIntIp(statsIpAddress.getValue());
587         IntegerIpAddress storedIpAddressInt = StrIpToIntIp(storedIpAddress.getValue());
588
589         if(IpAndMaskBasedMatch(statsIpAddressInt,storedIpAddressInt)){
590             return true;
591         }
592         if(IpBasedMatch(statsIpAddressInt,storedIpAddressInt)){
593             return true;
594         }
595         return false;
596     }
597
598     private static boolean IpAndMaskBasedMatch(IntegerIpAddress statsIpAddressInt,IntegerIpAddress storedIpAddressInt){
599         return ((statsIpAddressInt.getIp() & statsIpAddressInt.getMask()) ==  (storedIpAddressInt.getIp() & storedIpAddressInt.getMask()));
600     }
601
602     private static boolean IpBasedMatch(IntegerIpAddress statsIpAddressInt,IntegerIpAddress storedIpAddressInt){
603         return (statsIpAddressInt.getIp() == storedIpAddressInt.getIp());
604     }
605
606     /**
607      * Method return integer version of ip address. Converted int will be mask if
608      * mask specified
609      */
610     private static IntegerIpAddress StrIpToIntIp(String ipAddresss){
611
612         String[] parts = ipAddresss.split("/");
613         String ip = parts[0];
614         int prefix;
615
616         if (parts.length < 2) {
617             prefix = 32;
618         } else {
619             prefix = Integer.parseInt(parts[1]);
620         }
621
622         IntegerIpAddress integerIpAddress = null;
623         try {
624             Inet4Address addr = (Inet4Address) InetAddress.getByName(ip);
625             byte[] addrBytes = addr.getAddress();
626             int ipInt = ((addrBytes[0] & 0xFF) << 24) |
627                     ((addrBytes[1] & 0xFF) << 16) |
628                     ((addrBytes[2] & 0xFF) << 8)  |
629                     ((addrBytes[3] & 0xFF) << 0);
630
631             int mask = 0xffffffff << 32 - prefix;
632
633             integerIpAddress = new IntegerIpAddress(ipInt, mask);
634         } catch (UnknownHostException e){
635             sucLogger.error("Failed to determine host IP address by name: {}", e.getMessage(), e);
636         }
637
638         return integerIpAddress;
639     }
640
641     static class IntegerIpAddress{
642         int ip;
643         int mask;
644         public IntegerIpAddress(int ip, int mask) {
645             this.ip = ip;
646             this.mask = mask;
647         }
648         public int getIp() {
649             return ip;
650         }
651         public int getMask() {
652             return mask;
653         }
654     }
655 }
656