2 * Copyright IBM Corporation, 2013. All rights reserved.
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
8 package org.opendaylight.controller.md.statistics.manager;
10 import java.net.Inet4Address;
11 import java.net.InetAddress;
12 import java.net.UnknownHostException;
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;
25 import com.google.common.annotations.VisibleForTesting;
28 * Utility class for comparing flows.
30 final class FlowComparator {
31 private final static Logger logger = LoggerFactory.getLogger(FlowComparator.class);
33 private FlowComparator() {
37 public static boolean flowEquals(Flow statsFlow, Flow storedFlow) {
38 if (statsFlow == null || storedFlow == null) {
41 if (statsFlow.getContainerName()== null) {
42 if (storedFlow.getContainerName()!= null) {
45 } else if(!statsFlow.getContainerName().equals(storedFlow.getContainerName())) {
48 if (storedFlow.getPriority() == null) {
49 if (statsFlow.getPriority() != null && statsFlow.getPriority()!= 0x8000) {
52 } else if(!statsFlow.getPriority().equals(storedFlow.getPriority())) {
55 if (statsFlow.getMatch()== null) {
56 if (storedFlow.getMatch() != null) {
59 } else if(!matchEquals(statsFlow.getMatch(), storedFlow.getMatch())) {
62 if (statsFlow.getTableId() == null) {
63 if (storedFlow.getTableId() != null) {
66 } else if(!statsFlow.getTableId().equals(storedFlow.getTableId())) {
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
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.
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.
96 public static boolean matchEquals(Match statsFlow, Match storedFlow) {
97 if (statsFlow == storedFlow) {
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) {
106 } else if(!ethernetMatchEquals(statsFlow.getEthernetMatch(),storedFlow.getEthernetMatch())) {
109 if (storedFlow.getIcmpv4Match()== null) {
110 if (statsFlow.getIcmpv4Match() != null) {
113 } else if(!storedFlow.getIcmpv4Match().equals(statsFlow.getIcmpv4Match())) {
116 if (storedFlow.getIcmpv6Match() == null) {
117 if (statsFlow.getIcmpv6Match() != null) {
120 } else if(!storedFlow.getIcmpv6Match().equals(statsFlow.getIcmpv6Match())) {
123 if (storedFlow.getInPhyPort() == null) {
124 if (statsFlow.getInPhyPort() != null) {
127 } else if(!storedFlow.getInPhyPort().equals(statsFlow.getInPhyPort())) {
130 if (storedFlow.getInPort()== null) {
131 if (statsFlow.getInPort() != null) {
134 } else if(!storedFlow.getInPort().equals(statsFlow.getInPort())) {
137 if (storedFlow.getIpMatch()== null) {
138 if (statsFlow.getIpMatch() != null) {
141 } else if(!storedFlow.getIpMatch().equals(statsFlow.getIpMatch())) {
144 if (storedFlow.getLayer3Match()== null) {
145 if (statsFlow.getLayer3Match() != null) {
148 } else if(!layer3MatchEquals(statsFlow.getLayer3Match(),storedFlow.getLayer3Match())) {
151 if (storedFlow.getLayer4Match()== null) {
152 if (statsFlow.getLayer4Match() != null) {
155 } else if(!storedFlow.getLayer4Match().equals(statsFlow.getLayer4Match())) {
158 if (storedFlow.getMetadata() == null) {
159 if (statsFlow.getMetadata() != null) {
162 } else if(!storedFlow.getMetadata().equals(statsFlow.getMetadata())) {
165 if (storedFlow.getProtocolMatchFields() == null) {
166 if (statsFlow.getProtocolMatchFields() != null) {
169 } else if(!storedFlow.getProtocolMatchFields().equals(statsFlow.getProtocolMatchFields())) {
172 if (storedFlow.getTunnel()== null) {
173 if (statsFlow.getTunnel() != null) {
176 } else if(!storedFlow.getTunnel().equals(statsFlow.getTunnel())) {
179 if (storedFlow.getVlanMatch()== null) {
180 if (statsFlow.getVlanMatch() != null) {
183 } else if(!storedFlow.getVlanMatch().equals(statsFlow.getVlanMatch())) {
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.
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;
204 verdict = ethernetMatchFieldsEquals(statsEthernetMatch.getEthernetSource(),storedEthernetMatch.getEthernetSource());
207 verdict = ethernetMatchFieldsEquals(statsEthernetMatch.getEthernetDestination(),storedEthernetMatch.getEthernetDestination());
210 if(statsEthernetMatch.getEthernetType() == null){
211 if(storedEthernetMatch.getEthernetType() != null){
215 verdict = statsEthernetMatch.getEthernetType().equals(storedEthernetMatch.getEthernetType());
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;
230 verdict = macAddressEquals(statsEthernetMatchFields.getAddress(),storedEthernetMatchFields.getAddress());
233 verdict = macAddressEquals(statsEthernetMatchFields.getMask(),storedEthernetMatchFields.getMask());
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;
245 verdict = statsMacAddress.getValue().equalsIgnoreCase(storedMacAddress.getValue());
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;
258 verdict = compareNullSafe(
259 storedIpv4Match.getIpv4Destination(), statsIpv4Match.getIpv4Destination());
262 verdict = compareNullSafe(
263 statsIpv4Match.getIpv4Source(), storedIpv4Match.getIpv4Source());
266 Boolean nullCheckOut = checkNullValues(storedLayer3Match, statsLayer3Match);
267 if (nullCheckOut != null) {
268 verdict = nullCheckOut;
270 verdict = storedLayer3Match.equals(statsLayer3Match);
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)){
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;
303 * TODO: why don't we use the default Ipv4Prefix.equals()?
305 * @param statsIpAddress
306 * @param storedIpAddress
307 * @return true if IPv4prefixes equals
309 private static boolean IpAddressEquals(Ipv4Prefix statsIpAddress, Ipv4Prefix storedIpAddress) {
310 IntegerIpAddress statsIpAddressInt = StrIpToIntIp(statsIpAddress.getValue());
311 IntegerIpAddress storedIpAddressInt = StrIpToIntIp(storedIpAddress.getValue());
313 if(IpAndMaskBasedMatch(statsIpAddressInt,storedIpAddressInt)){
316 if(IpBasedMatch(statsIpAddressInt,storedIpAddressInt)){
322 private static boolean IpAndMaskBasedMatch(IntegerIpAddress statsIpAddressInt,IntegerIpAddress storedIpAddressInt){
323 return ((statsIpAddressInt.getIp() & statsIpAddressInt.getMask()) == (storedIpAddressInt.getIp() & storedIpAddressInt.getMask()));
326 private static boolean IpBasedMatch(IntegerIpAddress statsIpAddressInt,IntegerIpAddress storedIpAddressInt){
327 return (statsIpAddressInt.getIp() == storedIpAddressInt.getIp());
331 * Method return integer version of ip address. Converted int will be mask if
334 private static IntegerIpAddress StrIpToIntIp(String ipAddresss){
336 String[] parts = ipAddresss.split("/");
337 String ip = parts[0];
340 if (parts.length < 2) {
343 prefix = Integer.parseInt(parts[1]);
346 IntegerIpAddress integerIpAddress = null;
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);
355 int mask = 0xffffffff << 32 - prefix;
357 integerIpAddress = new IntegerIpAddress(ipInt, mask);
358 } catch (UnknownHostException e){
359 logger.error("Failed to determine host IP address by name: {}", e.getMessage(), e);
362 return integerIpAddress;
365 private static class IntegerIpAddress{
368 public IntegerIpAddress(int ip, int mask) {
375 public int getMask() {