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