BUG-2637: migration consequence - fix unit test
[controller.git] / opendaylight / md-sal / statistics-manager / src / main / java / org / opendaylight / controller / md / statistics / manager / impl / helper / FlowComparator.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.impl.helper;
9
10 import com.google.common.annotations.VisibleForTesting;
11 import com.google.common.net.InetAddresses;
12 import java.net.Inet4Address;
13 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Prefix;
14 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev100924.MacAddress;
15 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.Flow;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.Match;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.MacAddressFilter;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.EthernetMatch;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.Layer3Match;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._3.match.Ipv4Match;
21 import org.slf4j.Logger;
22 import org.slf4j.LoggerFactory;
23
24 /**
25  * Utility class for comparing flows.
26  */
27 public final class FlowComparator {
28     private final static Logger LOG = LoggerFactory.getLogger(FlowComparator.class);
29
30     private FlowComparator() {
31         throw new UnsupportedOperationException("Utilities class should not be instantiated");
32     }
33
34     public static boolean flowEquals(final Flow statsFlow, final Flow storedFlow) {
35         if (statsFlow == null || storedFlow == null) {
36             return false;
37         }
38         if (statsFlow.getContainerName()== null) {
39             if (storedFlow.getContainerName()!= null) {
40                 return false;
41             }
42         } else if(!statsFlow.getContainerName().equals(storedFlow.getContainerName())) {
43             return false;
44         }
45         if (storedFlow.getPriority() == null) {
46             if (statsFlow.getPriority() != null && statsFlow.getPriority()!= 0x8000) {
47                 return false;
48             }
49         } else if(!statsFlow.getPriority().equals(storedFlow.getPriority())) {
50             return false;
51         }
52         if (statsFlow.getMatch()== null) {
53             if (storedFlow.getMatch() != null) {
54                 return false;
55             }
56         } else if(!matchEquals(statsFlow.getMatch(), storedFlow.getMatch())) {
57             return false;
58         }
59         if (statsFlow.getTableId() == null) {
60             if (storedFlow.getTableId() != null) {
61                 return false;
62             }
63         } else if(!statsFlow.getTableId().equals(storedFlow.getTableId())) {
64             return false;
65         }
66         return true;
67     }
68
69     /**
70      * Explicit equals method to compare the 'match' for flows stored in the data-stores and flow fetched from the switch.
71      * Flow installation process has three steps
72      * 1) Store flow in config data store
73      * 2) and send it to plugin for installation
74      * 3) Flow gets installed in switch
75      *
76      * The flow user wants to install and what finally gets installed in switch can be slightly different.
77      * E.g, If user installs flow with src/dst ip=10.0.0.1/24, when it get installed in the switch
78      * src/dst ip will be changes to 10.0.0.0/24 because of netmask of 24. When statistics manager fetch
79      * stats it gets 10.0.0.0/24 rather then 10.0.0.1/24. Custom match takes care of by using masked ip
80      * while comparing two ip addresses.
81      *
82      * Sometimes when user don't provide few values that is required by flow installation request, like
83      * priority,hard timeout, idle timeout, cookies etc, plugin usages default values before sending
84      * request to the switch. So when statistics manager gets flow statistics, it gets the default value.
85      * But the flow stored in config data store don't have those defaults value. I included those checks
86      * in the customer flow/match equal function.
87      *
88      *
89      * @param statsFlow
90      * @param storedFlow
91      * @return
92      */
93     public static boolean matchEquals(final Match statsFlow, final Match storedFlow) {
94         if (statsFlow == storedFlow) {
95             return true;
96         }
97         if (storedFlow == null && statsFlow != null) {
98             return false;
99         }
100         if (statsFlow == null && storedFlow != null) {
101             return false;
102         }
103         if (storedFlow.getEthernetMatch() == null) {
104             if (statsFlow.getEthernetMatch() != null) {
105                 return false;
106             }
107         } else if(!ethernetMatchEquals(statsFlow.getEthernetMatch(),storedFlow.getEthernetMatch())) {
108             return false;
109         }
110         if (storedFlow.getIcmpv4Match()== null) {
111             if (statsFlow.getIcmpv4Match() != null) {
112                 return false;
113             }
114         } else if(!storedFlow.getIcmpv4Match().equals(statsFlow.getIcmpv4Match())) {
115             return false;
116         }
117         if (storedFlow.getIcmpv6Match() == null) {
118             if (statsFlow.getIcmpv6Match() != null) {
119                 return false;
120             }
121         } else if(!storedFlow.getIcmpv6Match().equals(statsFlow.getIcmpv6Match())) {
122             return false;
123         }
124         if (storedFlow.getInPhyPort() == null) {
125             if (statsFlow.getInPhyPort() != null) {
126                 return false;
127             }
128         } else if(!storedFlow.getInPhyPort().equals(statsFlow.getInPhyPort())) {
129             return false;
130         }
131         if (storedFlow.getInPort()== null) {
132             if (statsFlow.getInPort() != null) {
133                 return false;
134             }
135         } else if(!storedFlow.getInPort().equals(statsFlow.getInPort())) {
136             return false;
137         }
138         if (storedFlow.getIpMatch()== null) {
139             if (statsFlow.getIpMatch() != null) {
140                 return false;
141             }
142         } else if(!storedFlow.getIpMatch().equals(statsFlow.getIpMatch())) {
143             return false;
144         }
145         if (storedFlow.getLayer3Match()== null) {
146             if (statsFlow.getLayer3Match() != null) {
147                     return false;
148             }
149         } else if(!layer3MatchEquals(statsFlow.getLayer3Match(),storedFlow.getLayer3Match())) {
150             return false;
151         }
152         if (storedFlow.getLayer4Match()== null) {
153             if (statsFlow.getLayer4Match() != null) {
154                 return false;
155             }
156         } else if(!storedFlow.getLayer4Match().equals(statsFlow.getLayer4Match())) {
157             return false;
158         }
159         if (storedFlow.getMetadata() == null) {
160             if (statsFlow.getMetadata() != null) {
161                 return false;
162             }
163         } else if(!storedFlow.getMetadata().equals(statsFlow.getMetadata())) {
164             return false;
165         }
166         if (storedFlow.getProtocolMatchFields() == null) {
167             if (statsFlow.getProtocolMatchFields() != null) {
168                 return false;
169             }
170         } else if(!storedFlow.getProtocolMatchFields().equals(statsFlow.getProtocolMatchFields())) {
171             return false;
172         }
173         if (storedFlow.getTunnel()== null) {
174             if (statsFlow.getTunnel() != null) {
175                 return false;
176             }
177         } else if(!storedFlow.getTunnel().equals(statsFlow.getTunnel())) {
178             return false;
179         }
180         if (storedFlow.getVlanMatch()== null) {
181             if (statsFlow.getVlanMatch() != null) {
182                 return false;
183             }
184         } else if(!storedFlow.getVlanMatch().equals(statsFlow.getVlanMatch())) {
185             return false;
186         }
187         return true;
188     }
189
190     /*
191      * Custom EthernetMatch is required because mac address string provided by user in EthernetMatch can be in
192      * any case (upper or lower or mix). Ethernet Match which controller receives from switch is always
193      * an upper case string. Default EthernetMatch equals doesn't use equalsIgnoreCase() and hence it fails.
194      * E.g User provided mac address string in flow match is aa:bb:cc:dd:ee:ff and when controller fetch
195      * statistic data, openflow driver library returns AA:BB:CC:DD:EE:FF and default eqauls fails here.
196      */
197     @VisibleForTesting
198     static boolean ethernetMatchEquals(final EthernetMatch statsEthernetMatch, final EthernetMatch storedEthernetMatch){
199         boolean verdict = true;
200         final Boolean checkNullValues = checkNullValues(statsEthernetMatch, storedEthernetMatch);
201         if (checkNullValues != null) {
202             verdict = checkNullValues;
203         } else {
204             if(verdict){
205                 verdict = ethernetMatchFieldsEquals(statsEthernetMatch.getEthernetSource(),storedEthernetMatch.getEthernetSource());
206             }
207             if(verdict){
208                 verdict = ethernetMatchFieldsEquals(statsEthernetMatch.getEthernetDestination(),storedEthernetMatch.getEthernetDestination());
209             }
210             if(verdict){
211                 if(statsEthernetMatch.getEthernetType() == null){
212                     if(storedEthernetMatch.getEthernetType() != null){
213                         verdict = false;
214                     }
215                 }else{
216                     verdict = statsEthernetMatch.getEthernetType().equals(storedEthernetMatch.getEthernetType());
217                 }
218             }
219         }
220         return verdict;
221     }
222
223     private static boolean ethernetMatchFieldsEquals(final MacAddressFilter statsEthernetMatchFields,
224                                                         final MacAddressFilter storedEthernetMatchFields){
225         boolean verdict = true;
226         final Boolean checkNullValues = checkNullValues(statsEthernetMatchFields, storedEthernetMatchFields);
227         if (checkNullValues != null) {
228             verdict = checkNullValues;
229         } else {
230             if(verdict){
231                 verdict = macAddressEquals(statsEthernetMatchFields.getAddress(),storedEthernetMatchFields.getAddress());
232             }
233             if(verdict){
234                 verdict = macAddressEquals(statsEthernetMatchFields.getMask(),storedEthernetMatchFields.getMask());
235             }
236         }
237         return verdict;
238     }
239
240     private static boolean macAddressEquals(final MacAddress statsMacAddress, final MacAddress storedMacAddress){
241         boolean verdict = true;
242         final Boolean checkNullValues = checkNullValues(statsMacAddress, storedMacAddress);
243         if (checkNullValues != null) {
244             verdict = checkNullValues;
245         } else {
246             verdict = statsMacAddress.getValue().equalsIgnoreCase(storedMacAddress.getValue());
247         }
248         return verdict;
249     }
250
251     @VisibleForTesting
252     static boolean layer3MatchEquals(final Layer3Match statsLayer3Match, final Layer3Match storedLayer3Match){
253         boolean verdict = true;
254         if(statsLayer3Match instanceof Ipv4Match && storedLayer3Match instanceof Ipv4Match){
255             final Ipv4Match statsIpv4Match = (Ipv4Match)statsLayer3Match;
256             final Ipv4Match storedIpv4Match = (Ipv4Match)storedLayer3Match;
257
258             if (verdict) {
259                 verdict = compareNullSafe(
260                         storedIpv4Match.getIpv4Destination(), statsIpv4Match.getIpv4Destination());
261             }
262             if (verdict) {
263                 verdict = compareNullSafe(
264                         statsIpv4Match.getIpv4Source(), storedIpv4Match.getIpv4Source());
265             }
266         } else {
267             final Boolean nullCheckOut = checkNullValues(storedLayer3Match, statsLayer3Match);
268             if (nullCheckOut != null) {
269                 verdict = nullCheckOut;
270             } else {
271                 verdict = storedLayer3Match.equals(statsLayer3Match);
272             }
273         }
274
275         return verdict;
276     }
277
278     private static boolean compareNullSafe(final Ipv4Prefix statsIpv4, final Ipv4Prefix storedIpv4) {
279         boolean verdict = true;
280         final Boolean checkDestNullValuesOut = checkNullValues(storedIpv4, statsIpv4);
281         if (checkDestNullValuesOut != null) {
282             verdict = checkDestNullValuesOut;
283         } else if(!IpAddressEquals(statsIpv4, storedIpv4)){
284             verdict = false;
285         }
286
287         return verdict;
288     }
289
290     private static Boolean checkNullValues(final Object v1, final Object v2) {
291         Boolean verdict = null;
292         if (v1 == null && v2 != null) {
293             verdict = Boolean.FALSE;
294         } else if (v1 != null && v2 == null) {
295             verdict = Boolean.FALSE;
296         } else if (v1 == null && v2 == null) {
297             verdict = Boolean.TRUE;
298         }
299
300         return verdict;
301     }
302
303     /**
304      * TODO: why don't we use the default Ipv4Prefix.equals()?
305      *
306      * @param statsIpAddress
307      * @param storedIpAddress
308      * @return true if IPv4prefixes equals
309      */
310     private static boolean IpAddressEquals(final Ipv4Prefix statsIpAddress, final Ipv4Prefix storedIpAddress) {
311         final IntegerIpAddress statsIpAddressInt = StrIpToIntIp(statsIpAddress.getValue());
312         final IntegerIpAddress storedIpAddressInt = StrIpToIntIp(storedIpAddress.getValue());
313
314         if(IpAndMaskBasedMatch(statsIpAddressInt,storedIpAddressInt)){
315             return true;
316         }
317         if(IpBasedMatch(statsIpAddressInt,storedIpAddressInt)){
318             return true;
319         }
320         return false;
321     }
322
323     private static boolean IpAndMaskBasedMatch(final IntegerIpAddress statsIpAddressInt,final IntegerIpAddress storedIpAddressInt){
324         return ((statsIpAddressInt.getIp() & statsIpAddressInt.getMask()) ==  (storedIpAddressInt.getIp() & storedIpAddressInt.getMask()));
325     }
326
327     private static boolean IpBasedMatch(final IntegerIpAddress statsIpAddressInt,final IntegerIpAddress storedIpAddressInt){
328         return (statsIpAddressInt.getIp() == storedIpAddressInt.getIp());
329     }
330
331     /**
332      * Method return integer version of ip address. Converted int will be mask if
333      * mask specified
334      */
335     private static IntegerIpAddress StrIpToIntIp(final String ipAddresss){
336
337         final String[] parts = ipAddresss.split("/");
338         final String ip = parts[0];
339         int prefix;
340
341         if (parts.length < 2) {
342             prefix = 32;
343         } else {
344             prefix = Integer.parseInt(parts[1]);
345         }
346
347         IntegerIpAddress integerIpAddress = null;
348
349             final Inet4Address addr = ((Inet4Address) InetAddresses.forString(ip));
350             final byte[] addrBytes = addr.getAddress();
351             final int ipInt = ((addrBytes[0] & 0xFF) << 24) |
352                     ((addrBytes[1] & 0xFF) << 16) |
353                     ((addrBytes[2] & 0xFF) << 8)  |
354                     ((addrBytes[3] & 0xFF) << 0);
355
356             // FIXME: Is this valid?
357             final int mask = 0xffffffff << 32 - prefix;
358
359             integerIpAddress = new IntegerIpAddress(ipInt, mask);
360
361
362         return integerIpAddress;
363     }
364
365     private static class IntegerIpAddress{
366         int ip;
367         int mask;
368         public IntegerIpAddress(final int ip, final int mask) {
369             this.ip = ip;
370             this.mask = mask;
371         }
372         public int getIp() {
373             return ip;
374         }
375         public int getMask() {
376             return mask;
377         }
378     }
379 }