Mark AD-SAL interfaces as deprecated
[controller.git] / opendaylight / adsal / sal / api / src / main / java / org / opendaylight / controller / sal / match / extensible / Match.java
1
2 /*
3  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
4  *
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
8  */
9
10 package org.opendaylight.controller.sal.match.extensible;
11
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;
21 import java.util.Map;
22 import java.util.Map.Entry;
23 import java.util.Set;
24
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;
29
30
31 import org.opendaylight.controller.sal.utils.NetUtils;
32
33 /**
34  * Represents the generic match criteria for a network frame/packet/message
35  * It contains a collection of individual field match
36  *
37  */
38 @XmlRootElement
39 @XmlAccessorType(XmlAccessType.NONE)
40 @Deprecated
41 public class Match implements Cloneable, Serializable {
42         private static final long serialVersionUID = 1L;
43     private Map<String, MatchField<?>> fields;
44
45     public Match() {
46         fields = new HashMap<String, MatchField<?>>();
47     }
48
49     public Match(Match match) {
50         fields = new HashMap<String, MatchField<?>>(match.fields);
51     }
52
53     /**
54      * Generic setter for frame/packet/message's header field against which to match
55      *
56      * @param field the fields parameters as MAtchField object
57      */
58     public void setField(MatchField<?> field) {
59         if (field.isValid()) {
60             fields.put(field.getType(), field);
61         }
62     }
63
64     /**
65      * Generic method to clear a field from the match
66      */
67     public void clearField(String type) {
68         fields.remove(type);
69     }
70
71     /**
72      * Generic getter for fields against which the match is programmed
73      *
74      * @param type  frame/packet/message's header field type
75      * @return
76      */
77     public MatchField<?> getField(String type) {
78         return fields.get(type);
79     }
80
81     /**
82      * Returns the list of MatchType fields the match is set for
83      *
84      * @return List of individual MatchType fields.
85      */
86     public List<String> getMatchesList() {
87         return new ArrayList<String>(fields.keySet());
88     }
89
90     /**
91      * Returns the list of MatchFields the match is set for
92      *
93      * @return List of individual MatchField values.
94      */
95     @XmlElement(name="matchField")
96     public List<MatchField<?>> getMatchFields() {
97         return new ArrayList<MatchField<?>>(fields.values());
98     }
99
100     /**
101      * Returns whether this match is for an IPv6 flow
102      */
103     public boolean isIPv6() {
104         if (isPresent(DlType.TYPE)) {
105             for (MatchField<?> field : fields.values()) {
106                 if (!field.isV6()) {
107                     return false;
108                 }
109             }
110         }
111         return true;
112     }
113
114     /**
115      * Returns whether this match is for an IPv4 flow
116      */
117     public boolean isIPv4() {
118         return !isIPv6();
119     }
120
121     /**
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
124      *
125      * @param type
126      * @return
127      */
128     public boolean isAny(String type) {
129         return !fields.containsKey(type);
130     }
131
132     /**
133      * Returns whether a match for the specified field type is configured
134      *
135      * @param type
136      * @return
137      */
138     public boolean isPresent(String type) {
139         return (fields.get(type) != null);
140     }
141
142     public boolean isEmpty() {
143         return fields.isEmpty();
144     }
145
146     @Override
147     public Match clone() {
148         Match cloned = null;
149         try {
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());
154             }
155         } catch (CloneNotSupportedException e) {
156             throw new RuntimeException(e);
157         }
158         return cloned;
159     }
160
161     /**
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.
166      *
167      * @return
168      */
169     public Match reverse() {
170         Match reverse = new Match();
171         for (MatchField<?> field : fields.values()) {
172             reverse.setField(field.hasReverse()? field.getReverse() : field.clone());
173         }
174
175         // Reset asymmetric fields
176         reverse.clearField(InPort.TYPE);
177
178         return reverse;
179     }
180
181     /**
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
185      * compatible
186      *
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.
191      *
192      * For example, Let's suppose the filter has the following MatchFields:
193      * DL_TYPE = 0x800
194      * NW_DST = 172.20.30.110/24
195      *
196      * while this match has the following MatchFields:
197      * DL_TYPE = 0x800
198      * NW_DST = 172.20.30.45/24
199      * TP_DST = 80
200      *
201      * Then the function would return false as the two Match are not
202      * conflicting.
203      *
204      * Note: the mask value is taken into account only for MatchType.NW_SRC and
205      * MatchType.NW_DST
206      *
207      * @param match
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
211      *         otherwise
212      */
213     public boolean conflictWithFilter(Match filter) {
214         return !this.intersetcs(filter);
215     }
216
217     /**
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.
221      *
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.
226      *
227      * @param filter
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.
232      */
233     public Match mergeWithFilter(Match filter) {
234         return this.getIntersection(filter);
235     }
236
237     /**
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).
243      *
244      * @param other
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.
249      */
250     public Match getIntersection(Match other) {
251         // If no intersection, return the empty set
252         if (!this.intersetcs(other)) {
253             return null;
254         }
255         // Check if any of the two is the universal match
256         if (this.isEmpty()) {
257             return other.clone();
258         }
259         if (other.isEmpty()) {
260             return this.clone();
261         }
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)) {
269                 continue;
270             }
271             if (this.isAny(type)) {
272                 intersection.setField(other.getField(type).clone());
273                 continue;
274             } else if (other.isAny(type)) {
275                 intersection.setField(this.getField(type).clone());
276                 continue;
277             }
278             // Either they are equal or it is about IP address
279             switch (type) {
280             // When it is about IP address, take the wider prefix address
281             // between the twos
282             case NwSrc.TYPE:
283             case NwDst.TYPE:
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();
290
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);
295
296                 InetAddress subnetPrefix = null;
297                 InetAddress subnetMask = null;
298                 if (thisMaskLen < otherMaskLen) {
299                     subnetPrefix = NetUtils.getSubnetPrefix(otherAddress, otherMaskLen);
300                     subnetMask = otherMask;
301                 } else {
302                     subnetPrefix = NetUtils.getSubnetPrefix(thisAddress, thisMaskLen);
303                     subnetMask = thisMask;
304                 }
305                 MatchField<?> field = (type.equals(NwSrc.TYPE)) ? new NwSrc(subnetPrefix, subnetMask) : new NwDst(
306                         subnetPrefix, subnetMask);
307                 intersection.setField(field);
308                 break;
309             default:
310                 // this and other match field are equal for this type, pick this
311                 // match field
312                 intersection.setField(this.getField(type).clone());
313             }
314         }
315         return intersection;
316     }
317
318     /**
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
321      *
322      * For example, if this match is: DL_SRC = 00:cc:bb:aa:11:22
323      *
324      * and the other match is: DL_TYPE = 0x800 NW_SRC = 1.2.3.4
325      *
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
328      *
329      * @param other
330      *            the other match with which testing the intersection
331      * @return true if the intersection of the respective matching packets sets
332      *         is non empty
333      */
334     public boolean intersetcs(Match other) {
335         // No intersection with the empty set
336         if (other == null) {
337             return false;
338         }
339         // Always intersection with the universal set
340         if (this.isEmpty() || other.isEmpty()) {
341             return true;
342         }
343
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()));
347
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)) {
351                 continue;
352             }
353
354             MatchField<?> thisField = this.getField(type);
355             MatchField<?> otherField = other.getField(type);
356
357             switch (type) {
358             case DlSrc.TYPE:
359             case DlDst.TYPE:
360                 if (!Arrays.equals((byte[]) thisField.getValue(), (byte[]) otherField.getValue())) {
361                     return false;
362                 }
363                 break;
364             case NwSrc.TYPE:
365             case NwDst.TYPE:
366                 InetAddress thisAddress = (InetAddress) thisField.getValue();
367                 InetAddress otherAddress = (InetAddress) otherField.getValue();
368                 // Validity check
369                 if (thisAddress instanceof Inet4Address && otherAddress instanceof Inet6Address
370                         || thisAddress instanceof Inet6Address && otherAddress instanceof Inet4Address) {
371                     return false;
372                 }
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)) {
377                     return false;
378                 }
379                 break;
380             default:
381                 if (!thisField.getValue().equals(otherField.getValue())) {
382                     return false;
383                 }
384             }
385         }
386         return true;
387     }
388
389     @Override
390     public int hashCode() {
391         final int prime = 31;
392         int result = 1;
393         result = prime * result + ((fields == null) ? 0 : fields.hashCode());
394         return result;
395     }
396
397     @Override
398     public boolean equals(Object obj) {
399         if (this == obj) {
400             return true;
401         }
402         if (obj == null) {
403             return false;
404         }
405         if (!(obj instanceof Match)) {
406             return false;
407         }
408         Match other = (Match) obj;
409         if (fields == null) {
410             if (other.fields != null) {
411                 return false;
412             }
413         } else if (!fields.equals(other.fields)) {
414             return false;
415         }
416         return true;
417     }
418
419     @Override
420     public String toString() {
421         return "Match[" + fields.values() + "]";
422     }
423 }