Merge "Fix for possible NPE if Bundle is stopped."
[controller.git] / opendaylight / md-sal / statistics-manager / src / main / java / org / opendaylight / controller / md / statistics / manager / 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;
9
10 import java.net.Inet4Address;
11 import java.net.InetAddress;
12 import java.net.UnknownHostException;
13
14 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Prefix;
15 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.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.match.Layer3Match;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._3.match.Ipv4Match;
19 import org.slf4j.Logger;
20 import org.slf4j.LoggerFactory;
21
22 import com.google.common.annotations.VisibleForTesting;
23
24 /**
25  * Utility class for comparing flows.
26  */
27 final class FlowComparator {
28     private final static Logger logger = LoggerFactory.getLogger(FlowComparator.class);
29
30     private FlowComparator() {
31
32     }
33
34     public static boolean flowEquals(Flow statsFlow, 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(Match statsFlow, Match storedFlow) {
94         if (statsFlow == storedFlow) {
95             return true;
96         }
97         if (storedFlow == null && statsFlow != null) return false;
98         if (statsFlow == null && storedFlow != null) return false;
99         if (storedFlow.getEthernetMatch() == null) {
100             if (statsFlow.getEthernetMatch() != null) {
101                 return false;
102             }
103         } else if(!storedFlow.getEthernetMatch().equals(statsFlow.getEthernetMatch())) {
104             return false;
105         }
106         if (storedFlow.getIcmpv4Match()== null) {
107             if (statsFlow.getIcmpv4Match() != null) {
108                 return false;
109             }
110         } else if(!storedFlow.getIcmpv4Match().equals(statsFlow.getIcmpv4Match())) {
111             return false;
112         }
113         if (storedFlow.getIcmpv6Match() == null) {
114             if (statsFlow.getIcmpv6Match() != null) {
115                 return false;
116             }
117         } else if(!storedFlow.getIcmpv6Match().equals(statsFlow.getIcmpv6Match())) {
118             return false;
119         }
120         if (storedFlow.getInPhyPort() == null) {
121             if (statsFlow.getInPhyPort() != null) {
122                 return false;
123             }
124         } else if(!storedFlow.getInPhyPort().equals(statsFlow.getInPhyPort())) {
125             return false;
126         }
127         if (storedFlow.getInPort()== null) {
128             if (statsFlow.getInPort() != null) {
129                 return false;
130             }
131         } else if(!storedFlow.getInPort().equals(statsFlow.getInPort())) {
132             return false;
133         }
134         if (storedFlow.getIpMatch()== null) {
135             if (statsFlow.getIpMatch() != null) {
136                 return false;
137             }
138         } else if(!storedFlow.getIpMatch().equals(statsFlow.getIpMatch())) {
139             return false;
140         }
141         if (storedFlow.getLayer3Match()== null) {
142             if (statsFlow.getLayer3Match() != null) {
143                     return false;
144             }
145         } else if(!layer3MatchEquals(statsFlow.getLayer3Match(),storedFlow.getLayer3Match())) {
146             return false;
147         }
148         if (storedFlow.getLayer4Match()== null) {
149             if (statsFlow.getLayer4Match() != null) {
150                 return false;
151             }
152         } else if(!storedFlow.getLayer4Match().equals(statsFlow.getLayer4Match())) {
153             return false;
154         }
155         if (storedFlow.getMetadata() == null) {
156             if (statsFlow.getMetadata() != null) {
157                 return false;
158             }
159         } else if(!storedFlow.getMetadata().equals(statsFlow.getMetadata())) {
160             return false;
161         }
162         if (storedFlow.getProtocolMatchFields() == null) {
163             if (statsFlow.getProtocolMatchFields() != null) {
164                 return false;
165             }
166         } else if(!storedFlow.getProtocolMatchFields().equals(statsFlow.getProtocolMatchFields())) {
167             return false;
168         }
169         if (storedFlow.getTunnel()== null) {
170             if (statsFlow.getTunnel() != null) {
171                 return false;
172             }
173         } else if(!storedFlow.getTunnel().equals(statsFlow.getTunnel())) {
174             return false;
175         }
176         if (storedFlow.getVlanMatch()== null) {
177             if (statsFlow.getVlanMatch() != null) {
178                 return false;
179             }
180         } else if(!storedFlow.getVlanMatch().equals(statsFlow.getVlanMatch())) {
181             return false;
182         }
183         return true;
184     }
185
186     @VisibleForTesting
187     static boolean layer3MatchEquals(Layer3Match statsLayer3Match, Layer3Match storedLayer3Match){
188         boolean verdict = true;
189         if(statsLayer3Match instanceof Ipv4Match && storedLayer3Match instanceof Ipv4Match){
190             Ipv4Match statsIpv4Match = (Ipv4Match)statsLayer3Match;
191             Ipv4Match storedIpv4Match = (Ipv4Match)storedLayer3Match;
192
193             if (verdict) {
194                 verdict = compareNullSafe(
195                         storedIpv4Match.getIpv4Destination(), statsIpv4Match.getIpv4Destination());
196             }
197             if (verdict) {
198                 verdict = compareNullSafe(
199                         statsIpv4Match.getIpv4Source(), storedIpv4Match.getIpv4Source());
200             }
201         } else {
202             Boolean nullCheckOut = checkNullValues(storedLayer3Match, statsLayer3Match);
203             if (nullCheckOut != null) {
204                 verdict = nullCheckOut;
205             } else {
206                 verdict = storedLayer3Match.equals(statsLayer3Match);
207             }
208         }
209
210         return verdict;
211     }
212
213     private static boolean compareNullSafe(Ipv4Prefix statsIpv4, Ipv4Prefix storedIpv4) {
214         boolean verdict = true;
215         Boolean checkDestNullValuesOut = checkNullValues(storedIpv4, statsIpv4);
216         if (checkDestNullValuesOut != null) {
217             verdict = checkDestNullValuesOut;
218         } else if(!IpAddressEquals(statsIpv4, storedIpv4)){
219             verdict = false;
220         }
221
222         return verdict;
223     }
224
225     private static Boolean checkNullValues(Object v1, Object v2) {
226         Boolean verdict = null;
227         if (v1 == null && v2 != null) {
228             verdict = Boolean.FALSE;
229         } else if (v1 != null && v2 == null) {
230             verdict = Boolean.FALSE;
231         } else if (v1 == null && v2 == null) {
232             verdict = Boolean.TRUE;
233         }
234
235         return verdict;
236     }
237
238     /**
239      * TODO: why don't we use the default Ipv4Prefix.equals()?
240      *
241      * @param statsIpAddress
242      * @param storedIpAddress
243      * @return true if IPv4prefixes equals
244      */
245     private static boolean IpAddressEquals(Ipv4Prefix statsIpAddress, Ipv4Prefix storedIpAddress) {
246         IntegerIpAddress statsIpAddressInt = StrIpToIntIp(statsIpAddress.getValue());
247         IntegerIpAddress storedIpAddressInt = StrIpToIntIp(storedIpAddress.getValue());
248
249         if(IpAndMaskBasedMatch(statsIpAddressInt,storedIpAddressInt)){
250             return true;
251         }
252         if(IpBasedMatch(statsIpAddressInt,storedIpAddressInt)){
253             return true;
254         }
255         return false;
256     }
257
258     private static boolean IpAndMaskBasedMatch(IntegerIpAddress statsIpAddressInt,IntegerIpAddress storedIpAddressInt){
259         return ((statsIpAddressInt.getIp() & statsIpAddressInt.getMask()) ==  (storedIpAddressInt.getIp() & storedIpAddressInt.getMask()));
260     }
261
262     private static boolean IpBasedMatch(IntegerIpAddress statsIpAddressInt,IntegerIpAddress storedIpAddressInt){
263         return (statsIpAddressInt.getIp() == storedIpAddressInt.getIp());
264     }
265
266     /**
267      * Method return integer version of ip address. Converted int will be mask if
268      * mask specified
269      */
270     private static IntegerIpAddress StrIpToIntIp(String ipAddresss){
271
272         String[] parts = ipAddresss.split("/");
273         String ip = parts[0];
274         int prefix;
275
276         if (parts.length < 2) {
277             prefix = 32;
278         } else {
279             prefix = Integer.parseInt(parts[1]);
280         }
281
282         IntegerIpAddress integerIpAddress = null;
283         try {
284             Inet4Address addr = (Inet4Address) InetAddress.getByName(ip);
285             byte[] addrBytes = addr.getAddress();
286             int ipInt = ((addrBytes[0] & 0xFF) << 24) |
287                     ((addrBytes[1] & 0xFF) << 16) |
288                     ((addrBytes[2] & 0xFF) << 8)  |
289                     ((addrBytes[3] & 0xFF) << 0);
290
291             int mask = 0xffffffff << 32 - prefix;
292
293             integerIpAddress = new IntegerIpAddress(ipInt, mask);
294         } catch (UnknownHostException e){
295             logger.error("Failed to determine host IP address by name: {}", e.getMessage(), e);
296         }
297
298         return integerIpAddress;
299     }
300
301     private static class IntegerIpAddress{
302         int ip;
303         int mask;
304         public IntegerIpAddress(int ip, int mask) {
305             this.ip = ip;
306             this.mask = mask;
307         }
308         public int getIp() {
309             return ip;
310         }
311         public int getMask() {
312             return mask;
313         }
314     }
315 }