bug 537 - Node Reconciliation
[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.getClass() != storedFlow.getClass()) {
39             return false;
40         }
41         if (statsFlow.getContainerName()== null) {
42             if (storedFlow.getContainerName()!= null) {
43                 return false;
44             }
45         } else if(!statsFlow.getContainerName().equals(storedFlow.getContainerName())) {
46             return false;
47         }
48         if (storedFlow.getPriority() == null) {
49             if (statsFlow.getPriority() != null && statsFlow.getPriority()!= 0x8000) {
50                 return false;
51             }
52         } else if(!statsFlow.getPriority().equals(storedFlow.getPriority())) {
53             return false;
54         }
55         if (statsFlow.getMatch()== null) {
56             if (storedFlow.getMatch() != null) {
57                 return false;
58             }
59         } else if(!matchEquals(statsFlow.getMatch(), storedFlow.getMatch())) {
60             return false;
61         }
62         if (statsFlow.getTableId() == null) {
63             if (storedFlow.getTableId() != null) {
64                 return false;
65             }
66         } else if(!statsFlow.getTableId().equals(storedFlow.getTableId())) {
67             return false;
68         }
69         return true;
70     }
71
72     /**
73      * Explicit equals method to compare the 'match' for flows stored in the data-stores and flow fetched from the switch.
74      * Flow installation process has three steps
75      * 1) Store flow in config data store
76      * 2) and send it to plugin for installation
77      * 3) Flow gets installed in switch
78      *
79      * The flow user wants to install and what finally gets installed in switch can be slightly different.
80      * E.g, If user installs flow with src/dst ip=10.0.0.1/24, when it get installed in the switch
81      * src/dst ip will be changes to 10.0.0.0/24 because of netmask of 24. When statistics manager fetch
82      * stats it gets 10.0.0.0/24 rather then 10.0.0.1/24. Custom match takes care of by using masked ip
83      * while comparing two ip addresses.
84      *
85      * Sometimes when user don't provide few values that is required by flow installation request, like
86      * priority,hard timeout, idle timeout, cookies etc, plugin usages default values before sending
87      * request to the switch. So when statistics manager gets flow statistics, it gets the default value.
88      * But the flow stored in config data store don't have those defaults value. I included those checks
89      * in the customer flow/match equal function.
90      *
91      *
92      * @param statsFlow
93      * @param storedFlow
94      * @return
95      */
96     public static boolean matchEquals(Match statsFlow, Match storedFlow) {
97         if (statsFlow == storedFlow) {
98             return true;
99         }
100         if (storedFlow == null && statsFlow != null) return false;
101         if (statsFlow == null && storedFlow != null) return false;
102         if (storedFlow.getClass() != statsFlow.getClass()) {
103             return false;
104         }
105         if (storedFlow.getEthernetMatch() == null) {
106             if (statsFlow.getEthernetMatch() != null) {
107                 return false;
108             }
109         } else if(!storedFlow.getEthernetMatch().equals(statsFlow.getEthernetMatch())) {
110             return false;
111         }
112         if (storedFlow.getIcmpv4Match()== null) {
113             if (statsFlow.getIcmpv4Match() != null) {
114                 return false;
115             }
116         } else if(!storedFlow.getIcmpv4Match().equals(statsFlow.getIcmpv4Match())) {
117             return false;
118         }
119         if (storedFlow.getIcmpv6Match() == null) {
120             if (statsFlow.getIcmpv6Match() != null) {
121                 return false;
122             }
123         } else if(!storedFlow.getIcmpv6Match().equals(statsFlow.getIcmpv6Match())) {
124             return false;
125         }
126         if (storedFlow.getInPhyPort() == null) {
127             if (statsFlow.getInPhyPort() != null) {
128                 return false;
129             }
130         } else if(!storedFlow.getInPhyPort().equals(statsFlow.getInPhyPort())) {
131             return false;
132         }
133         if (storedFlow.getInPort()== null) {
134             if (statsFlow.getInPort() != null) {
135                 return false;
136             }
137         } else if(!storedFlow.getInPort().equals(statsFlow.getInPort())) {
138             return false;
139         }
140         if (storedFlow.getIpMatch()== null) {
141             if (statsFlow.getIpMatch() != null) {
142                 return false;
143             }
144         } else if(!storedFlow.getIpMatch().equals(statsFlow.getIpMatch())) {
145             return false;
146         }
147         if (storedFlow.getLayer3Match()== null) {
148             if (statsFlow.getLayer3Match() != null) {
149                     return false;
150             }
151         } else if(!layer3MatchEquals(statsFlow.getLayer3Match(),storedFlow.getLayer3Match())) {
152             return false;
153         }
154         if (storedFlow.getLayer4Match()== null) {
155             if (statsFlow.getLayer4Match() != null) {
156                 return false;
157             }
158         } else if(!storedFlow.getLayer4Match().equals(statsFlow.getLayer4Match())) {
159             return false;
160         }
161         if (storedFlow.getMetadata() == null) {
162             if (statsFlow.getMetadata() != null) {
163                 return false;
164             }
165         } else if(!storedFlow.getMetadata().equals(statsFlow.getMetadata())) {
166             return false;
167         }
168         if (storedFlow.getProtocolMatchFields() == null) {
169             if (statsFlow.getProtocolMatchFields() != null) {
170                 return false;
171             }
172         } else if(!storedFlow.getProtocolMatchFields().equals(statsFlow.getProtocolMatchFields())) {
173             return false;
174         }
175         if (storedFlow.getTunnel()== null) {
176             if (statsFlow.getTunnel() != null) {
177                 return false;
178             }
179         } else if(!storedFlow.getTunnel().equals(statsFlow.getTunnel())) {
180             return false;
181         }
182         if (storedFlow.getVlanMatch()== null) {
183             if (statsFlow.getVlanMatch() != null) {
184                 return false;
185             }
186         } else if(!storedFlow.getVlanMatch().equals(statsFlow.getVlanMatch())) {
187             return false;
188         }
189         return true;
190     }
191
192     @VisibleForTesting
193     static boolean layer3MatchEquals(Layer3Match statsLayer3Match, Layer3Match storedLayer3Match){
194         boolean verdict = true;
195         if(statsLayer3Match instanceof Ipv4Match && storedLayer3Match instanceof Ipv4Match){
196             Ipv4Match statsIpv4Match = (Ipv4Match)statsLayer3Match;
197             Ipv4Match storedIpv4Match = (Ipv4Match)storedLayer3Match;
198
199             if (verdict) {
200                 verdict = compareNullSafe(
201                         storedIpv4Match.getIpv4Destination(), statsIpv4Match.getIpv4Destination());
202             }
203             if (verdict) {
204                 verdict = compareNullSafe(
205                         statsIpv4Match.getIpv4Source(), storedIpv4Match.getIpv4Source());
206             }
207         } else {
208             Boolean nullCheckOut = checkNullValues(storedLayer3Match, statsLayer3Match);
209             if (nullCheckOut != null) {
210                 verdict = nullCheckOut;
211             } else {
212                 verdict = storedLayer3Match.equals(statsLayer3Match);
213             }
214         }
215
216         return verdict;
217     }
218
219     private static boolean compareNullSafe(Ipv4Prefix statsIpv4, Ipv4Prefix storedIpv4) {
220         boolean verdict = true;
221         Boolean checkDestNullValuesOut = checkNullValues(storedIpv4, statsIpv4);
222         if (checkDestNullValuesOut != null) {
223             verdict = checkDestNullValuesOut;
224         } else if(!IpAddressEquals(statsIpv4, storedIpv4)){
225             verdict = false;
226         }
227
228         return verdict;
229     }
230
231     private static Boolean checkNullValues(Object v1, Object v2) {
232         Boolean verdict = null;
233         if (v1 == null && v2 != null) {
234             verdict = Boolean.FALSE;
235         } else if (v1 != null && v2 == null) {
236             verdict = Boolean.FALSE;
237         } else if (v1 == null && v2 == null) {
238             verdict = Boolean.TRUE;
239         }
240
241         return verdict;
242     }
243
244     /**
245      * TODO: why don't we use the default Ipv4Prefix.equals()?
246      *
247      * @param statsIpAddress
248      * @param storedIpAddress
249      * @return true if IPv4prefixes equals
250      */
251     private static boolean IpAddressEquals(Ipv4Prefix statsIpAddress, Ipv4Prefix storedIpAddress) {
252         IntegerIpAddress statsIpAddressInt = StrIpToIntIp(statsIpAddress.getValue());
253         IntegerIpAddress storedIpAddressInt = StrIpToIntIp(storedIpAddress.getValue());
254
255         if(IpAndMaskBasedMatch(statsIpAddressInt,storedIpAddressInt)){
256             return true;
257         }
258         if(IpBasedMatch(statsIpAddressInt,storedIpAddressInt)){
259             return true;
260         }
261         return false;
262     }
263
264     private static boolean IpAndMaskBasedMatch(IntegerIpAddress statsIpAddressInt,IntegerIpAddress storedIpAddressInt){
265         return ((statsIpAddressInt.getIp() & statsIpAddressInt.getMask()) ==  (storedIpAddressInt.getIp() & storedIpAddressInt.getMask()));
266     }
267
268     private static boolean IpBasedMatch(IntegerIpAddress statsIpAddressInt,IntegerIpAddress storedIpAddressInt){
269         return (statsIpAddressInt.getIp() == storedIpAddressInt.getIp());
270     }
271
272     /**
273      * Method return integer version of ip address. Converted int will be mask if
274      * mask specified
275      */
276     private static IntegerIpAddress StrIpToIntIp(String ipAddresss){
277
278         String[] parts = ipAddresss.split("/");
279         String ip = parts[0];
280         int prefix;
281
282         if (parts.length < 2) {
283             prefix = 32;
284         } else {
285             prefix = Integer.parseInt(parts[1]);
286         }
287
288         IntegerIpAddress integerIpAddress = null;
289         try {
290             Inet4Address addr = (Inet4Address) InetAddress.getByName(ip);
291             byte[] addrBytes = addr.getAddress();
292             int ipInt = ((addrBytes[0] & 0xFF) << 24) |
293                     ((addrBytes[1] & 0xFF) << 16) |
294                     ((addrBytes[2] & 0xFF) << 8)  |
295                     ((addrBytes[3] & 0xFF) << 0);
296
297             int mask = 0xffffffff << 32 - prefix;
298
299             integerIpAddress = new IntegerIpAddress(ipInt, mask);
300         } catch (UnknownHostException e){
301             logger.error("Failed to determine host IP address by name: {}", e.getMessage(), e);
302         }
303
304         return integerIpAddress;
305     }
306
307     private static class IntegerIpAddress{
308         int ip;
309         int mask;
310         public IntegerIpAddress(int ip, int mask) {
311             this.ip = ip;
312             this.mask = mask;
313         }
314         public int getIp() {
315             return ip;
316         }
317         public int getMask() {
318             return mask;
319         }
320     }
321 }