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.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;
22 import com.google.common.annotations.VisibleForTesting;
25 * Utility class for comparing flows.
27 final class FlowComparator {
28 private final static Logger logger = LoggerFactory.getLogger(FlowComparator.class);
30 private FlowComparator() {
34 public static boolean flowEquals(Flow statsFlow, Flow storedFlow) {
35 if (statsFlow.getClass() != storedFlow.getClass()) {
38 if (statsFlow.getContainerName()== null) {
39 if (storedFlow.getContainerName()!= null) {
42 } else if(!statsFlow.getContainerName().equals(storedFlow.getContainerName())) {
45 if (statsFlow.getMatch()== null) {
46 if (storedFlow.getMatch() != null) {
49 } //else if(!statsFlow.getMatch().equals(storedFlow.getMatch())) {
50 else if(!matchEquals(statsFlow.getMatch(), storedFlow.getMatch())) {
53 if (storedFlow.getPriority() == null) {
54 if (statsFlow.getPriority() != null && statsFlow.getPriority()!= 0x8000) {
57 } else if(!statsFlow.getPriority().equals(storedFlow.getPriority())) {
60 if (statsFlow.getTableId() == null) {
61 if (storedFlow.getTableId() != null) {
64 } else if(!statsFlow.getTableId().equals(storedFlow.getTableId())) {
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
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.
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.
94 public static boolean matchEquals(Match statsFlow, Match storedFlow) {
95 if (statsFlow == storedFlow) {
98 if (storedFlow == null && statsFlow != null) return false;
99 if (statsFlow == null && storedFlow != null) return false;
100 if (storedFlow.getClass() != statsFlow.getClass()) {
103 if (storedFlow.getEthernetMatch() == null) {
104 if (statsFlow.getEthernetMatch() != null) {
107 } else if(!storedFlow.getEthernetMatch().equals(statsFlow.getEthernetMatch())) {
110 if (storedFlow.getIcmpv4Match()== null) {
111 if (statsFlow.getIcmpv4Match() != null) {
114 } else if(!storedFlow.getIcmpv4Match().equals(statsFlow.getIcmpv4Match())) {
117 if (storedFlow.getIcmpv6Match() == null) {
118 if (statsFlow.getIcmpv6Match() != null) {
121 } else if(!storedFlow.getIcmpv6Match().equals(statsFlow.getIcmpv6Match())) {
124 if (storedFlow.getInPhyPort() == null) {
125 if (statsFlow.getInPhyPort() != null) {
128 } else if(!storedFlow.getInPhyPort().equals(statsFlow.getInPhyPort())) {
131 if (storedFlow.getInPort()== null) {
132 if (statsFlow.getInPort() != null) {
135 } else if(!storedFlow.getInPort().equals(statsFlow.getInPort())) {
138 if (storedFlow.getIpMatch()== null) {
139 if (statsFlow.getIpMatch() != null) {
142 } else if(!storedFlow.getIpMatch().equals(statsFlow.getIpMatch())) {
145 if (storedFlow.getLayer3Match()== null) {
146 if (statsFlow.getLayer3Match() != null) {
149 } else if(!layer3MatchEquals(statsFlow.getLayer3Match(),storedFlow.getLayer3Match())) {
152 if (storedFlow.getLayer4Match()== null) {
153 if (statsFlow.getLayer4Match() != null) {
156 } else if(!storedFlow.getLayer4Match().equals(statsFlow.getLayer4Match())) {
159 if (storedFlow.getMetadata() == null) {
160 if (statsFlow.getMetadata() != null) {
163 } else if(!storedFlow.getMetadata().equals(statsFlow.getMetadata())) {
166 if (storedFlow.getProtocolMatchFields() == null) {
167 if (statsFlow.getProtocolMatchFields() != null) {
170 } else if(!storedFlow.getProtocolMatchFields().equals(statsFlow.getProtocolMatchFields())) {
173 if (storedFlow.getTunnel()== null) {
174 if (statsFlow.getTunnel() != null) {
177 } else if(!storedFlow.getTunnel().equals(statsFlow.getTunnel())) {
180 if (storedFlow.getVlanMatch()== null) {
181 if (statsFlow.getVlanMatch() != null) {
184 } else if(!storedFlow.getVlanMatch().equals(statsFlow.getVlanMatch())) {
191 static boolean layer3MatchEquals(Layer3Match statsLayer3Match, Layer3Match storedLayer3Match){
192 boolean verdict = true;
193 if(statsLayer3Match instanceof Ipv4Match && storedLayer3Match instanceof Ipv4Match){
194 Ipv4Match statsIpv4Match = (Ipv4Match)statsLayer3Match;
195 Ipv4Match storedIpv4Match = (Ipv4Match)storedLayer3Match;
198 verdict = compareNullSafe(
199 storedIpv4Match.getIpv4Destination(), statsIpv4Match.getIpv4Destination());
202 verdict = compareNullSafe(
203 statsIpv4Match.getIpv4Source(), storedIpv4Match.getIpv4Source());
206 Boolean nullCheckOut = checkNullValues(storedLayer3Match, statsLayer3Match);
207 if (nullCheckOut != null) {
208 verdict = nullCheckOut;
210 verdict = storedLayer3Match.equals(statsLayer3Match);
217 private static boolean compareNullSafe(Ipv4Prefix statsIpv4, Ipv4Prefix storedIpv4) {
218 boolean verdict = true;
219 Boolean checkDestNullValuesOut = checkNullValues(storedIpv4, statsIpv4);
220 if (checkDestNullValuesOut != null) {
221 verdict = checkDestNullValuesOut;
222 } else if(!IpAddressEquals(statsIpv4, storedIpv4)){
229 private static Boolean checkNullValues(Object v1, Object v2) {
230 Boolean verdict = null;
231 if (v1 == null && v2 != null) {
232 verdict = Boolean.FALSE;
233 } else if (v1 != null && v2 == null) {
234 verdict = Boolean.FALSE;
235 } else if (v1 == null && v2 == null) {
236 verdict = Boolean.TRUE;
243 * TODO: why don't we use the default Ipv4Prefix.equals()?
245 * @param statsIpAddress
246 * @param storedIpAddress
247 * @return true if IPv4prefixes equals
249 private static boolean IpAddressEquals(Ipv4Prefix statsIpAddress, Ipv4Prefix storedIpAddress) {
250 IntegerIpAddress statsIpAddressInt = StrIpToIntIp(statsIpAddress.getValue());
251 IntegerIpAddress storedIpAddressInt = StrIpToIntIp(storedIpAddress.getValue());
253 if(IpAndMaskBasedMatch(statsIpAddressInt,storedIpAddressInt)){
256 if(IpBasedMatch(statsIpAddressInt,storedIpAddressInt)){
262 private static boolean IpAndMaskBasedMatch(IntegerIpAddress statsIpAddressInt,IntegerIpAddress storedIpAddressInt){
263 return ((statsIpAddressInt.getIp() & statsIpAddressInt.getMask()) == (storedIpAddressInt.getIp() & storedIpAddressInt.getMask()));
266 private static boolean IpBasedMatch(IntegerIpAddress statsIpAddressInt,IntegerIpAddress storedIpAddressInt){
267 return (statsIpAddressInt.getIp() == storedIpAddressInt.getIp());
271 * Method return integer version of ip address. Converted int will be mask if
274 private static IntegerIpAddress StrIpToIntIp(String ipAddresss){
276 String[] parts = ipAddresss.split("/");
277 String ip = parts[0];
280 if (parts.length < 2) {
283 prefix = Integer.parseInt(parts[1]);
286 IntegerIpAddress integerIpAddress = null;
288 Inet4Address addr = (Inet4Address) InetAddress.getByName(ip);
289 byte[] addrBytes = addr.getAddress();
290 int ipInt = ((addrBytes[0] & 0xFF) << 24) |
291 ((addrBytes[1] & 0xFF) << 16) |
292 ((addrBytes[2] & 0xFF) << 8) |
293 ((addrBytes[3] & 0xFF) << 0);
295 int mask = 0xffffffff << 32 - prefix;
297 integerIpAddress = new IntegerIpAddress(ipInt, mask);
298 } catch (UnknownHostException e){
299 logger.error("Failed to determine host IP address by name: {}", e.getMessage(), e);
302 return integerIpAddress;
305 private static class IntegerIpAddress{
308 public IntegerIpAddress(int ip, int mask) {
315 public int getMask() {