Checkstyle enforcer
[controller.git] / opendaylight / sal / api / src / main / java / org / opendaylight / controller / sal / match / 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;
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.Collections;
19 import java.util.HashMap;
20 import java.util.List;
21 import java.util.Map;
22 import java.util.Map.Entry;
23
24 import javax.xml.bind.annotation.XmlAccessType;
25 import javax.xml.bind.annotation.XmlAccessorType;
26 import javax.xml.bind.annotation.XmlElement;
27 import javax.xml.bind.annotation.XmlRootElement;
28
29 import org.opendaylight.controller.sal.utils.EtherTypes;
30 import org.opendaylight.controller.sal.utils.IPProtocols;
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 public class Match implements Cloneable, Serializable {
41         private static final long serialVersionUID = 1L;
42         private static final Map<MatchType, MatchType> reversableMatches;
43     static {
44         Map<MatchType, MatchType> map = new HashMap<MatchType, MatchType>();
45         map.put(MatchType.DL_SRC, MatchType.DL_DST);
46         map.put(MatchType.DL_DST, MatchType.DL_SRC);
47         map.put(MatchType.NW_SRC, MatchType.NW_DST);
48         map.put(MatchType.NW_DST, MatchType.NW_SRC);
49         map.put(MatchType.TP_SRC, MatchType.TP_DST);
50         map.put(MatchType.TP_DST, MatchType.TP_SRC);
51         reversableMatches = Collections.unmodifiableMap(map);
52     }
53     private Map<MatchType, MatchField> fields;
54     private int matches; // concise way to tell which fields the match is set for (may remove if not needed)
55
56     public Match() {
57         fields = new HashMap<MatchType, MatchField>();
58         matches = 0;
59     }
60
61     public Match(Match match) {
62         fields = new HashMap<MatchType, MatchField>(match.fields);
63         matches = match.matches;
64     }
65
66     /**
67      * Generic setter for frame/packet/message's header fields against which to match
68      * Note: For MAC addresses, please pass the cloned value to this function
69      *
70      * @param type      packet's header field type
71      * @param value     field's value to assign to the match
72      * @param mask      field's bitmask to apply to the match (has to be of the same class type of value)
73      */
74     public void setField(MatchType type, Object value, Object mask) {
75         MatchField field = new MatchField(type, value, mask);
76         if (field.isValid()) {
77             fields.put(type, field);
78             matches |= type.getIndex();
79         }
80     }
81
82     /**
83      * Generic setter for frame/packet/message's header fields against which to match
84      * Note: For MAC addresses, please pass the cloned value to this function
85      *
86      * @param type      packet's header field type
87      * @param value     field's value to assign to the match
88      */
89     public void setField(MatchType type, Object value) {
90         MatchField field = new MatchField(type, value);
91         if (field.isValid()) {
92             fields.put(type, field);
93             matches |= type.getIndex();
94         }
95     }
96
97     /**
98      * Generic setter for frame/packet/message's header field against which to match
99      *
100      * @param field the fields parameters as MAtchField object
101      */
102     public void setField(MatchField field) {
103         if (field.isValid()) {
104             fields.put(field.getType(), field);
105             matches |= field.getType().getIndex();
106         }
107     }
108
109     /**
110      * Generic method to clear a field from the match
111      */
112     public void clearField(MatchType type) {
113         fields.remove(type);
114         matches &= ~type.getIndex();
115     }
116
117     /**
118      * Generic getter for fields against which the match is programmed
119      *
120      * @param type  frame/packet/message's header field type
121      * @return
122      */
123     public MatchField getField(MatchType type) {
124         return fields.get(type);
125     }
126
127     /**
128      * Returns the fields the match is set for in a bitmask fashion
129      * Each bit represents a field the match is configured for
130      *
131      * @return the 32 bit long mask (Refer to {@code}org.opendaylight.controller.sal.match.MatchElement)
132      */
133     public int getMatches() {
134         return matches;
135     }
136
137     /**
138      * Returns the list of MatchType fields the match is set for
139      *
140      * @return List of individual MatchType fields.
141      */
142     public List<MatchType> getMatchesList() {
143         return new ArrayList<MatchType>(fields.keySet());
144     }
145
146     /**
147      * Returns the list of MatchFields the match is set for
148      *
149      * @return List of individual MatchField values.
150      */
151     @XmlElement(name="matchField")
152     public List<MatchField> getMatchFields() {
153         return new ArrayList<MatchField>(fields.values());
154     }
155
156     /**
157      * Returns whether this match is for an IPv6 flow
158      */
159     public boolean isIPv6() {
160         return (isPresent(MatchType.DL_TYPE)
161                 && ((Short) getField(MatchType.DL_TYPE).getValue())
162                         .equals(EtherTypes.IPv6.shortValue())
163                 || isPresent(MatchType.NW_PROTO)
164                 && ((Byte) getField(MatchType.NW_PROTO).getValue())
165                         .equals(IPProtocols.IPV6ICMP.byteValue())
166                 || isPresent(MatchType.NW_SRC)
167                 && getField(MatchType.NW_SRC).getValue() instanceof Inet6Address || isPresent(MatchType.NW_DST)
168                 && getField(MatchType.NW_DST).getValue() instanceof Inet6Address);
169     }
170
171     /**
172      * Returns whether this match is for an IPv4 flow
173      */
174     public boolean isIPv4() {
175         return !isIPv6();
176     }
177
178     /**
179      * Returns whether for the specified field type the match is to be considered "any"
180      * Equivalent to say this match does not care about the value of the specified field
181      *
182      * @param type
183      * @return
184      */
185     public boolean isAny(MatchType type) {
186         //return ((fields.get(type) == null) || (fields.get(type).getBitMask() == 0L));
187         return !fields.containsKey(type);
188     }
189
190     /**
191      * Returns whether a match for the specified field type is configured
192      *
193      * @param type
194      * @return
195      */
196     public boolean isPresent(MatchType type) {
197         return (fields.get(type) != null);
198     }
199
200     @Override
201     public Match clone() {
202         Match cloned = null;
203         try {
204             cloned = (Match) super.clone();
205             cloned.fields = new HashMap<MatchType, MatchField>();
206             for (Entry<MatchType, MatchField> entry : this.fields.entrySet()) {
207                 cloned.fields.put(entry.getKey(), entry.getValue().clone());
208             }
209         } catch (CloneNotSupportedException e) {
210             throw new RuntimeException(e);
211         }
212         return cloned;
213     }
214
215     /**
216      * Returns a reversed version of this match
217      * For example, in the reversed version the network source and destination
218      * addresses will be exchanged. Non symmetric match field will not be
219      * copied over into the reversed match version, like input port.
220      *
221      * @return
222      */
223     public Match reverse() {
224         // Copy over all fields
225         Match reverse = this.clone();
226
227         // Flip symmetric fields
228         for (Map.Entry<MatchType, MatchType> entry : Match.reversableMatches
229                 .entrySet()) {
230             MatchType from = entry.getKey();
231             MatchType to = entry.getValue();
232             if (this.isPresent(from)) {
233                 reverse.setField(to, this.getField(from).getValue(), this
234                         .getField(from).getMask());
235             }
236         }
237
238         // Reset asymmetric fields
239         reverse.clearField(MatchType.IN_PORT);
240
241         return reverse;
242     }
243
244     /**
245      * Check whether the current match conflicts with the passed filter match
246      * This match conflicts with the filter if for at least a MatchType defined
247      * in the filter match, the respective MatchFields differ or are not compatible
248      *
249      * For example, Let's suppose the filter has the following MatchFields:
250      * DL_TYPE = 0x800
251      * NW_DST =  172.20.30.110/24
252      *
253      * while this match has the following MatchFields:
254      * NW_DST = 172.20.30.45/24
255      * TP_DST = 80
256      *
257      * Then the function would return false as the two Match are not conflicting
258      *
259      * Note: the mask value is taken into account only for MatchType.NW_SRC and MatchType.NW_DST
260      *
261      * @param match the MAtch describing the filter
262      * @return true if the match is conflicting with the filter, false otherwise
263      */
264     public boolean conflictWithFilter(Match filter) {
265         // Iterate through the MatchType defined in the filter
266         for (MatchType type : filter.getMatchesList()) {
267             if (this.isAny(type)) {
268                 continue;
269             }
270
271             MatchField thisField = this.getField(type);
272             MatchField filterField = filter.getField(type);
273
274             switch (type) {
275             case DL_SRC:
276             case DL_DST:
277                 if (Arrays.equals((byte[]) thisField.getValue(),
278                         (byte[]) filterField.getValue())) {
279                     return false;
280                 }
281                 break;
282             case NW_SRC:
283             case NW_DST:
284                 InetAddress thisAddress = (InetAddress) thisField.getValue();
285                 InetAddress filterAddress = (InetAddress) filterField
286                         .getValue();
287                 // Validity check
288                 if (thisAddress instanceof Inet4Address
289                         && filterAddress instanceof Inet6Address
290                         || thisAddress instanceof Inet6Address
291                         && filterAddress instanceof Inet4Address) {
292                     return true;
293                 }
294                 InetAddress thisMask = (InetAddress) thisField.getMask();
295                 InetAddress filterMask = (InetAddress) filterField.getMask();
296                 // thisAddress has to be in same subnet of filterAddress
297                 if (NetUtils.inetAddressConflict(thisAddress, filterAddress,
298                         thisMask, filterMask)) {
299                     return true;
300                 }
301                 break;
302             default:
303                 if (!thisField.getValue().equals(filterField.getValue())) {
304                     return true;
305                 }
306             }
307             //TODO: check v4 v6 incompatibility
308         }
309         return false;
310     }
311
312     /**
313      * Merge the current Match fields with the fields of the filter Match
314      * A check is first run to see if this Match is compatible with the
315      * filter Match. If it is not, the merge is not attempted.
316      *
317      *
318      * @param filter
319      * @return
320      */
321     public Match mergeWithFilter(Match filter) {
322         if (!this.conflictWithFilter(filter)) {
323             /*
324              * No conflict with the filter
325              * We can copy over the fields which this match does not have
326              */
327             for (MatchType type : filter.getMatchesList()) {
328                 if (this.isAny(type)) {
329                     this.setField(filter.getField(type).clone());
330                 }
331             }
332         }
333         return this;
334     }
335
336     @Override
337     public int hashCode() {
338         final int prime = 31;
339         int result = 1;
340         result = prime * result + ((fields == null) ? 0 : fields.hashCode());
341         result = prime * result + matches;
342         return result;
343     }
344
345     @Override
346     public boolean equals(Object obj) {
347         if (this == obj)
348             return true;
349         if (obj == null)
350             return false;
351         if (getClass() != obj.getClass())
352             return false;
353         Match other = (Match) obj;
354         if (fields == null) {
355             if (other.fields != null)
356                 return false;
357         } else if (!fields.equals(other.fields))
358             return false;
359         if (matches != other.matches)
360             return false;
361         return true;
362     }
363
364     @Override
365     public String toString() {
366         return "Match[" + fields.values() + "]";
367     }
368 }