BUG 1839 - HTTP delete of non existing data
[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.ietf.params.xml.ns.yang.ietf.yang.types.rev100924.MacAddress;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.Flow;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.Match;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.MacAddressFilter;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.EthernetMatch;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.Layer3Match;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._3.match.Ipv4Match;
22 import org.slf4j.Logger;
23 import org.slf4j.LoggerFactory;
24
25 import com.google.common.annotations.VisibleForTesting;
26
27 /**
28  * Utility class for comparing flows.
29  */
30 final class FlowComparator {
31     private final static Logger logger = LoggerFactory.getLogger(FlowComparator.class);
32
33     private FlowComparator() {
34
35     }
36
37     public static boolean flowEquals(Flow statsFlow, Flow storedFlow) {
38         if (statsFlow == null || storedFlow == null) {
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.getEthernetMatch() == null) {
103             if (statsFlow.getEthernetMatch() != null) {
104                 return false;
105             }
106         } else if(!ethernetMatchEquals(statsFlow.getEthernetMatch(),storedFlow.getEthernetMatch())) {
107             return false;
108         }
109         if (storedFlow.getIcmpv4Match()== null) {
110             if (statsFlow.getIcmpv4Match() != null) {
111                 return false;
112             }
113         } else if(!storedFlow.getIcmpv4Match().equals(statsFlow.getIcmpv4Match())) {
114             return false;
115         }
116         if (storedFlow.getIcmpv6Match() == null) {
117             if (statsFlow.getIcmpv6Match() != null) {
118                 return false;
119             }
120         } else if(!storedFlow.getIcmpv6Match().equals(statsFlow.getIcmpv6Match())) {
121             return false;
122         }
123         if (storedFlow.getInPhyPort() == null) {
124             if (statsFlow.getInPhyPort() != null) {
125                 return false;
126             }
127         } else if(!storedFlow.getInPhyPort().equals(statsFlow.getInPhyPort())) {
128             return false;
129         }
130         if (storedFlow.getInPort()== null) {
131             if (statsFlow.getInPort() != null) {
132                 return false;
133             }
134         } else if(!storedFlow.getInPort().equals(statsFlow.getInPort())) {
135             return false;
136         }
137         if (storedFlow.getIpMatch()== null) {
138             if (statsFlow.getIpMatch() != null) {
139                 return false;
140             }
141         } else if(!storedFlow.getIpMatch().equals(statsFlow.getIpMatch())) {
142             return false;
143         }
144         if (storedFlow.getLayer3Match()== null) {
145             if (statsFlow.getLayer3Match() != null) {
146                     return false;
147             }
148         } else if(!layer3MatchEquals(statsFlow.getLayer3Match(),storedFlow.getLayer3Match())) {
149             return false;
150         }
151         if (storedFlow.getLayer4Match()== null) {
152             if (statsFlow.getLayer4Match() != null) {
153                 return false;
154             }
155         } else if(!storedFlow.getLayer4Match().equals(statsFlow.getLayer4Match())) {
156             return false;
157         }
158         if (storedFlow.getMetadata() == null) {
159             if (statsFlow.getMetadata() != null) {
160                 return false;
161             }
162         } else if(!storedFlow.getMetadata().equals(statsFlow.getMetadata())) {
163             return false;
164         }
165         if (storedFlow.getProtocolMatchFields() == null) {
166             if (statsFlow.getProtocolMatchFields() != null) {
167                 return false;
168             }
169         } else if(!storedFlow.getProtocolMatchFields().equals(statsFlow.getProtocolMatchFields())) {
170             return false;
171         }
172         if (storedFlow.getTunnel()== null) {
173             if (statsFlow.getTunnel() != null) {
174                 return false;
175             }
176         } else if(!storedFlow.getTunnel().equals(statsFlow.getTunnel())) {
177             return false;
178         }
179         if (storedFlow.getVlanMatch()== null) {
180             if (statsFlow.getVlanMatch() != null) {
181                 return false;
182             }
183         } else if(!storedFlow.getVlanMatch().equals(statsFlow.getVlanMatch())) {
184             return false;
185         }
186         return true;
187     }
188
189     /*
190      * Custom EthernetMatch is required because mac address string provided by user in EthernetMatch can be in
191      * any case (upper or lower or mix). Ethernet Match which controller receives from switch is always
192      * an upper case string. Default EthernetMatch equals doesn't use equalsIgnoreCase() and hence it fails.
193      * E.g User provided mac address string in flow match is aa:bb:cc:dd:ee:ff and when controller fetch
194      * statistic data, openflow driver library returns AA:BB:CC:DD:EE:FF and default eqauls fails here.
195      */
196     @VisibleForTesting
197     static boolean ethernetMatchEquals(EthernetMatch statsEthernetMatch, EthernetMatch storedEthernetMatch){
198         boolean verdict = true;
199         Boolean checkNullValues = checkNullValues(statsEthernetMatch, storedEthernetMatch);
200         if (checkNullValues != null) {
201             verdict = checkNullValues;
202         } else {
203             if(verdict){
204                 verdict = ethernetMatchFieldsEquals(statsEthernetMatch.getEthernetSource(),storedEthernetMatch.getEthernetSource());
205             }
206             if(verdict){
207                 verdict = ethernetMatchFieldsEquals(statsEthernetMatch.getEthernetDestination(),storedEthernetMatch.getEthernetDestination());
208             }
209             if(verdict){
210                 if(statsEthernetMatch.getEthernetType() == null){
211                     if(storedEthernetMatch.getEthernetType() != null){
212                         verdict = false;
213                     }
214                 }else{
215                     verdict = statsEthernetMatch.getEthernetType().equals(storedEthernetMatch.getEthernetType());
216                 }
217             }
218         }
219         return verdict;
220     }
221
222     private static boolean ethernetMatchFieldsEquals(MacAddressFilter statsEthernetMatchFields,
223                                                         MacAddressFilter storedEthernetMatchFields){
224         boolean verdict = true;
225         Boolean checkNullValues = checkNullValues(statsEthernetMatchFields, storedEthernetMatchFields);
226         if (checkNullValues != null) {
227             verdict = checkNullValues;
228         } else {
229             if(verdict){
230                 verdict = macAddressEquals(statsEthernetMatchFields.getAddress(),storedEthernetMatchFields.getAddress());
231             }
232             if(verdict){
233                 verdict = macAddressEquals(statsEthernetMatchFields.getMask(),storedEthernetMatchFields.getMask());
234             }
235         }
236         return verdict;
237     }
238
239     private static boolean macAddressEquals(MacAddress statsMacAddress, MacAddress storedMacAddress){
240         boolean verdict = true;
241         Boolean checkNullValues = checkNullValues(statsMacAddress, storedMacAddress);
242         if (checkNullValues != null) {
243             verdict = checkNullValues;
244         } else {
245             verdict = statsMacAddress.getValue().equalsIgnoreCase(storedMacAddress.getValue());
246         }
247         return verdict;
248     }
249
250     @VisibleForTesting
251     static boolean layer3MatchEquals(Layer3Match statsLayer3Match, Layer3Match storedLayer3Match){
252         boolean verdict = true;
253         if(statsLayer3Match instanceof Ipv4Match && storedLayer3Match instanceof Ipv4Match){
254             Ipv4Match statsIpv4Match = (Ipv4Match)statsLayer3Match;
255             Ipv4Match storedIpv4Match = (Ipv4Match)storedLayer3Match;
256
257             if (verdict) {
258                 verdict = compareNullSafe(
259                         storedIpv4Match.getIpv4Destination(), statsIpv4Match.getIpv4Destination());
260             }
261             if (verdict) {
262                 verdict = compareNullSafe(
263                         statsIpv4Match.getIpv4Source(), storedIpv4Match.getIpv4Source());
264             }
265         } else {
266             Boolean nullCheckOut = checkNullValues(storedLayer3Match, statsLayer3Match);
267             if (nullCheckOut != null) {
268                 verdict = nullCheckOut;
269             } else {
270                 verdict = storedLayer3Match.equals(statsLayer3Match);
271             }
272         }
273
274         return verdict;
275     }
276
277     private static boolean compareNullSafe(Ipv4Prefix statsIpv4, Ipv4Prefix storedIpv4) {
278         boolean verdict = true;
279         Boolean checkDestNullValuesOut = checkNullValues(storedIpv4, statsIpv4);
280         if (checkDestNullValuesOut != null) {
281             verdict = checkDestNullValuesOut;
282         } else if(!IpAddressEquals(statsIpv4, storedIpv4)){
283             verdict = false;
284         }
285
286         return verdict;
287     }
288
289     private static Boolean checkNullValues(Object v1, Object v2) {
290         Boolean verdict = null;
291         if (v1 == null && v2 != null) {
292             verdict = Boolean.FALSE;
293         } else if (v1 != null && v2 == null) {
294             verdict = Boolean.FALSE;
295         } else if (v1 == null && v2 == null) {
296             verdict = Boolean.TRUE;
297         }
298
299         return verdict;
300     }
301
302     /**
303      * TODO: why don't we use the default Ipv4Prefix.equals()?
304      *
305      * @param statsIpAddress
306      * @param storedIpAddress
307      * @return true if IPv4prefixes equals
308      */
309     private static boolean IpAddressEquals(Ipv4Prefix statsIpAddress, Ipv4Prefix storedIpAddress) {
310         IntegerIpAddress statsIpAddressInt = StrIpToIntIp(statsIpAddress.getValue());
311         IntegerIpAddress storedIpAddressInt = StrIpToIntIp(storedIpAddress.getValue());
312
313         if(IpAndMaskBasedMatch(statsIpAddressInt,storedIpAddressInt)){
314             return true;
315         }
316         if(IpBasedMatch(statsIpAddressInt,storedIpAddressInt)){
317             return true;
318         }
319         return false;
320     }
321
322     private static boolean IpAndMaskBasedMatch(IntegerIpAddress statsIpAddressInt,IntegerIpAddress storedIpAddressInt){
323         return ((statsIpAddressInt.getIp() & statsIpAddressInt.getMask()) ==  (storedIpAddressInt.getIp() & storedIpAddressInt.getMask()));
324     }
325
326     private static boolean IpBasedMatch(IntegerIpAddress statsIpAddressInt,IntegerIpAddress storedIpAddressInt){
327         return (statsIpAddressInt.getIp() == storedIpAddressInt.getIp());
328     }
329
330     /**
331      * Method return integer version of ip address. Converted int will be mask if
332      * mask specified
333      */
334     private static IntegerIpAddress StrIpToIntIp(String ipAddresss){
335
336         String[] parts = ipAddresss.split("/");
337         String ip = parts[0];
338         int prefix;
339
340         if (parts.length < 2) {
341             prefix = 32;
342         } else {
343             prefix = Integer.parseInt(parts[1]);
344         }
345
346         IntegerIpAddress integerIpAddress = null;
347         try {
348             Inet4Address addr = (Inet4Address) InetAddress.getByName(ip);
349             byte[] addrBytes = addr.getAddress();
350             int ipInt = ((addrBytes[0] & 0xFF) << 24) |
351                     ((addrBytes[1] & 0xFF) << 16) |
352                     ((addrBytes[2] & 0xFF) << 8)  |
353                     ((addrBytes[3] & 0xFF) << 0);
354
355             int mask = 0xffffffff << 32 - prefix;
356
357             integerIpAddress = new IntegerIpAddress(ipInt, mask);
358         } catch (UnknownHostException e){
359             logger.error("Failed to determine host IP address by name: {}", e.getMessage(), e);
360         }
361
362         return integerIpAddress;
363     }
364
365     private static class IntegerIpAddress{
366         int ip;
367         int mask;
368         public IntegerIpAddress(int ip, 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 }