3 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
5 * This program and the accompanying materials are made available under the
6 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
7 * and is available at http://www.eclipse.org/legal/epl-v10.html
10 package org.opendaylight.controller.sal.match.extensible;
12 import java.io.Serializable;
13 import java.net.Inet4Address;
14 import java.net.Inet6Address;
15 import java.net.InetAddress;
16 import java.util.ArrayList;
17 import java.util.Arrays;
18 import java.util.HashMap;
19 import java.util.HashSet;
20 import java.util.List;
22 import java.util.Map.Entry;
25 import javax.xml.bind.annotation.XmlAccessType;
26 import javax.xml.bind.annotation.XmlAccessorType;
27 import javax.xml.bind.annotation.XmlElement;
28 import javax.xml.bind.annotation.XmlRootElement;
31 import org.opendaylight.controller.sal.utils.NetUtils;
34 * Represents the generic match criteria for a network frame/packet/message
35 * It contains a collection of individual field match
39 @XmlAccessorType(XmlAccessType.NONE)
41 public class Match implements Cloneable, Serializable {
42 private static final long serialVersionUID = 1L;
43 private Map<String, MatchField<?>> fields;
46 fields = new HashMap<String, MatchField<?>>();
49 public Match(Match match) {
50 fields = new HashMap<String, MatchField<?>>(match.fields);
54 * Generic setter for frame/packet/message's header field against which to match
56 * @param field the fields parameters as MAtchField object
58 public void setField(MatchField<?> field) {
59 if (field.isValid()) {
60 fields.put(field.getType(), field);
65 * Generic method to clear a field from the match
67 public void clearField(String type) {
72 * Generic getter for fields against which the match is programmed
74 * @param type frame/packet/message's header field type
77 public MatchField<?> getField(String type) {
78 return fields.get(type);
82 * Returns the list of MatchType fields the match is set for
84 * @return List of individual MatchType fields.
86 public List<String> getMatchesList() {
87 return new ArrayList<String>(fields.keySet());
91 * Returns the list of MatchFields the match is set for
93 * @return List of individual MatchField values.
95 @XmlElement(name="matchField")
96 public List<MatchField<?>> getMatchFields() {
97 return new ArrayList<MatchField<?>>(fields.values());
101 * Returns whether this match is for an IPv6 flow
103 public boolean isIPv6() {
104 if (isPresent(DlType.TYPE)) {
105 for (MatchField<?> field : fields.values()) {
115 * Returns whether this match is for an IPv4 flow
117 public boolean isIPv4() {
122 * Returns whether for the specified field type the match is to be considered "any"
123 * Equivalent to say this match does not care about the value of the specified field
128 public boolean isAny(String type) {
129 return !fields.containsKey(type);
133 * Returns whether a match for the specified field type is configured
138 public boolean isPresent(String type) {
139 return (fields.get(type) != null);
142 public boolean isEmpty() {
143 return fields.isEmpty();
147 public Match clone() {
150 cloned = (Match) super.clone();
151 cloned.fields = new HashMap<String, MatchField<?>>();
152 for (Entry<String, MatchField<?>> entry : this.fields.entrySet()) {
153 cloned.fields.put(entry.getKey(), entry.getValue().clone());
155 } catch (CloneNotSupportedException e) {
156 throw new RuntimeException(e);
162 * Returns a reversed version of this match
163 * For example, in the reversed version the network source and destination
164 * addresses will be exchanged. Non symmetric match field will not be
165 * copied over into the reversed match version, like input port.
169 public Match reverse() {
170 Match reverse = new Match();
171 for (MatchField<?> field : fields.values()) {
172 reverse.setField(field.hasReverse()? field.getReverse() : field.clone());
175 // Reset asymmetric fields
176 reverse.clearField(InPort.TYPE);
182 * Check whether the current match conflicts with the passed filter match
183 * This match conflicts with the filter if for at least a MatchType defined
184 * in the filter match, the respective MatchFields differ or are not
187 * In other words the function returns true if the set of packets described
188 * by one match and the set of packets described by the other match are
189 * disjoint. Equivalently, if the intersection of the two sets of packets
190 * described by the two org.opendaylight.controller.sal.matches is an empty.
192 * For example, Let's suppose the filter has the following MatchFields:
194 * NW_DST = 172.20.30.110/24
196 * while this match has the following MatchFields:
198 * NW_DST = 172.20.30.45/24
201 * Then the function would return false as the two Match are not
204 * Note: the mask value is taken into account only for MatchType.NW_SRC and
208 * the Match describing the filter
209 * @return true if the set of packets described by one match and the set of
210 * packets described by the other match are disjoint, false
213 public boolean conflictWithFilter(Match filter) {
214 return !this.intersetcs(filter);
218 * Merge the current Match fields with the fields of the filter Match. A
219 * check is first run to see if this Match is compatible with the filter
220 * Match. If it is not, the merge is not attempted.
222 * The result is the match object representing the intersection of the set
223 * of packets described by this match with the set of packets described by
224 * the filter match. If the intersection of the two sets is empty, the
225 * return match will be null.
228 * the match with which attempting the merge
229 * @return a new Match object describing the set of packets represented by
230 * the intersection of this and the filter org.opendaylight.controller.sal.matches. null if the
231 * intersection is empty.
233 public Match mergeWithFilter(Match filter) {
234 return this.getIntersection(filter);
238 * Return the match representing the intersection of the set of packets
239 * described by this match with the set of packets described by the other
240 * match. Such as m.getIntersection(m) == m, m.getIntersection(u) == m and
241 * m.getIntersection(o) == o where u is an empty match (universal set, all
242 * packets) and o is the null match (empty set).
245 * the match with which computing the intersection
246 * @return a new Match object representing the intersection of the set of
247 * packets described by this match with the set of packets described
248 * by the other match. null when the intersection is the empty set.
250 public Match getIntersection(Match other) {
251 // If no intersection, return the empty set
252 if (!this.intersetcs(other)) {
255 // Check if any of the two is the universal match
256 if (this.isEmpty()) {
257 return other.clone();
259 if (other.isEmpty()) {
262 // Get all the match types for both filters
263 Set<String> allTypes = new HashSet<String>(this.fields.keySet());
264 allTypes.addAll(new HashSet<String>(other.fields.keySet()));
265 // Derive the intersection
266 Match intersection = new Match();
267 for (String type : allTypes) {
268 if (this.isAny(type) && other.isAny(type)) {
271 if (this.isAny(type)) {
272 intersection.setField(other.getField(type).clone());
274 } else if (other.isAny(type)) {
275 intersection.setField(this.getField(type).clone());
278 // Either they are equal or it is about IP address
280 // When it is about IP address, take the wider prefix address
284 MatchField<?> thisField = this.getField(type);
285 MatchField<?> otherField = other.getField(type);
286 InetAddress thisAddress = (InetAddress) thisField.getValue();
287 InetAddress otherAddress = (InetAddress) otherField.getValue();
288 InetAddress thisMask = (InetAddress) thisField.getMask();
289 InetAddress otherMask = (InetAddress) otherField.getMask();
291 int thisMaskLen = (thisMask == null) ? ((thisAddress instanceof Inet4Address) ? 32 : 128) : NetUtils
292 .getSubnetMaskLength(thisMask);
293 int otherMaskLen = (otherMask == null) ? ((otherAddress instanceof Inet4Address) ? 32 : 128) : NetUtils
294 .getSubnetMaskLength(otherMask);
296 InetAddress subnetPrefix = null;
297 InetAddress subnetMask = null;
298 if (thisMaskLen < otherMaskLen) {
299 subnetPrefix = NetUtils.getSubnetPrefix(otherAddress, otherMaskLen);
300 subnetMask = otherMask;
302 subnetPrefix = NetUtils.getSubnetPrefix(thisAddress, thisMaskLen);
303 subnetMask = thisMask;
305 MatchField<?> field = (type.equals(NwSrc.TYPE)) ? new NwSrc(subnetPrefix, subnetMask) : new NwDst(
306 subnetPrefix, subnetMask);
307 intersection.setField(field);
310 // this and other match field are equal for this type, pick this
312 intersection.setField(this.getField(type).clone());
319 * Checks whether the intersection of the set of packets described by this
320 * match with the set of packets described by the other match is non empty
322 * For example, if this match is: DL_SRC = 00:cc:bb:aa:11:22
324 * and the other match is: DL_TYPE = 0x800 NW_SRC = 1.2.3.4
326 * then their respective matching packets set intersection is non empty:
327 * DL_SRC = 00:cc:bb:aa:11:22 DL_TYPE = 0x800 NW_SRC = 1.2.3.4
330 * the other match with which testing the intersection
331 * @return true if the intersection of the respective matching packets sets
334 public boolean intersetcs(Match other) {
335 // No intersection with the empty set
339 // Always intersection with the universal set
340 if (this.isEmpty() || other.isEmpty()) {
344 // Get all the match types for both filters
345 Set<String> allTypes = new HashSet<String>(this.fields.keySet());
346 allTypes.addAll(new HashSet<String>(other.fields.keySet()));
348 // Iterate through all the match types defined in the two filters
349 for (String type : allTypes) {
350 if (this.isAny(type) || other.isAny(type)) {
354 MatchField<?> thisField = this.getField(type);
355 MatchField<?> otherField = other.getField(type);
360 if (!Arrays.equals((byte[]) thisField.getValue(), (byte[]) otherField.getValue())) {
366 InetAddress thisAddress = (InetAddress) thisField.getValue();
367 InetAddress otherAddress = (InetAddress) otherField.getValue();
369 if (thisAddress instanceof Inet4Address && otherAddress instanceof Inet6Address
370 || thisAddress instanceof Inet6Address && otherAddress instanceof Inet4Address) {
373 InetAddress thisMask = (InetAddress) thisField.getMask();
374 InetAddress otherMask = (InetAddress) otherField.getMask();
375 if (NetUtils.inetAddressConflict(thisAddress, otherAddress, thisMask, otherMask)
376 && NetUtils.inetAddressConflict(otherAddress, thisAddress, otherMask, thisMask)) {
381 if (!thisField.getValue().equals(otherField.getValue())) {
390 public int hashCode() {
391 final int prime = 31;
393 result = prime * result + ((fields == null) ? 0 : fields.hashCode());
398 public boolean equals(Object obj) {
405 if (!(obj instanceof Match)) {
408 Match other = (Match) obj;
409 if (fields == null) {
410 if (other.fields != null) {
413 } else if (!fields.equals(other.fields)) {
420 public String toString() {
421 return "Match[" + fields.values() + "]";