blob: a819bd32cbd137f2d36a56d5089b3f6cc8e7607b [file] [log] [blame]
alshabib7410fea2014-09-16 13:48:39 -07001package org.onlab.onos.net.flow.criteria;
tom8bb16062014-09-12 14:47:46 -07002
alshabib99b8fdc2014-09-25 14:30:22 -07003import static com.google.common.base.MoreObjects.toStringHelper;
4
alshabibba5ac482014-10-02 17:15:20 -07005import java.util.Objects;
6
alshabib7b795492014-09-16 14:38:39 -07007import org.onlab.onos.net.PortNumber;
8import org.onlab.onos.net.flow.criteria.Criterion.Type;
Ayaka Koshibe1d56fe42014-09-19 16:51:58 -07009import org.onlab.packet.IpPrefix;
Ayaka Koshibea9c199f2014-09-16 16:21:40 -070010import org.onlab.packet.MacAddress;
11import org.onlab.packet.VlanId;
alshabib7b795492014-09-16 14:38:39 -070012
tom8bb16062014-09-12 14:47:46 -070013/**
14 * Factory class to create various traffic selection criteria.
15 */
16public final class Criteria {
17
alshabib7b795492014-09-16 14:38:39 -070018 //TODO: incomplete type implementation. Need to implement complete list from Criterion
19
tom8bb16062014-09-12 14:47:46 -070020 // Ban construction
21 private Criteria() {
22 }
23
24 /**
alshabib7b795492014-09-16 14:38:39 -070025 * Creates a match on IN_PORT field using the specified value.
26 *
27 * @param port inport value
28 * @return match criterion
29 */
30 public static Criterion matchInPort(PortNumber port) {
31 return new PortCriterion(port);
32 }
33
34 /**
tom8bb16062014-09-12 14:47:46 -070035 * Creates a match on ETH_SRC field using the specified value. This value
36 * may be a wildcard mask.
37 *
tomc104d282014-09-19 10:57:55 -070038 * @param mac MAC address value or wildcard mask
tom8bb16062014-09-12 14:47:46 -070039 * @return match criterion
40 */
Ayaka Koshibea9c199f2014-09-16 16:21:40 -070041 public static Criterion matchEthSrc(MacAddress mac) {
alshabib7b795492014-09-16 14:38:39 -070042 return new EthCriterion(mac, Type.ETH_SRC);
tom8bb16062014-09-12 14:47:46 -070043 }
44
alshabib369d2942014-09-12 17:59:35 -070045 /**
46 * Creates a match on ETH_DST field using the specified value. This value
47 * may be a wildcard mask.
48 *
tomc104d282014-09-19 10:57:55 -070049 * @param mac MAC address value or wildcard mask
alshabib369d2942014-09-12 17:59:35 -070050 * @return match criterion
51 */
Ayaka Koshibea9c199f2014-09-16 16:21:40 -070052 public static Criterion matchEthDst(MacAddress mac) {
alshabib7b795492014-09-16 14:38:39 -070053 return new EthCriterion(mac, Type.ETH_DST);
54 }
55
56 /**
57 * Creates a match on ETH_TYPE field using the specified value.
58 *
59 * @param ethType eth type value
60 * @return match criterion
61 */
62 public static Criterion matchEthType(Short ethType) {
63 return new EthTypeCriterion(ethType);
64 }
65
66 /**
67 * Creates a match on VLAN ID field using the specified value.
68 *
69 * @param vlanId vlan id value
70 * @return match criterion
71 */
Ayaka Koshibea9c199f2014-09-16 16:21:40 -070072 public static Criterion matchVlanId(VlanId vlanId) {
alshabib7b795492014-09-16 14:38:39 -070073 return new VlanIdCriterion(vlanId);
74 }
75
76 /**
77 * Creates a match on VLAN PCP field using the specified value.
78 *
79 * @param vlanPcp vlan pcp value
80 * @return match criterion
81 */
alshabibb45d1962014-09-18 14:25:45 -070082 public static Criterion matchVlanPcp(Byte vlanPcp) {
alshabib7b795492014-09-16 14:38:39 -070083 return new VlanPcpCriterion(vlanPcp);
84 }
85
86 /**
87 * Creates a match on IP proto field using the specified value.
88 *
89 * @param proto ip protocol value
90 * @return match criterion
91 */
92 public static Criterion matchIPProtocol(Byte proto) {
93 return new IPProtocolCriterion(proto);
94 }
95
96 /**
97 * Creates a match on IP src field using the specified value.
98 *
99 * @param ip ip src value
100 * @return match criterion
101 */
Ayaka Koshibe1d56fe42014-09-19 16:51:58 -0700102 public static Criterion matchIPSrc(IpPrefix ip) {
alshabib7b795492014-09-16 14:38:39 -0700103 return new IPCriterion(ip, Type.IPV4_SRC);
104 }
105
106 /**
107 * Creates a match on IP dst field using the specified value.
108 *
109 * @param ip ip src value
110 * @return match criterion
111 */
Ayaka Koshibe1d56fe42014-09-19 16:51:58 -0700112 public static Criterion matchIPDst(IpPrefix ip) {
alshabib7b795492014-09-16 14:38:39 -0700113 return new IPCriterion(ip, Type.IPV4_DST);
alshabib369d2942014-09-12 17:59:35 -0700114 }
115
116
alshabib7b795492014-09-16 14:38:39 -0700117 /*
118 * Implementations of criteria.
119 */
120
121 public static final class PortCriterion implements Criterion {
122 private final PortNumber port;
123
124 public PortCriterion(PortNumber port) {
125 this.port = port;
126 }
127
128 @Override
129 public Type type() {
130 return Type.IN_PORT;
131 }
132
133 public PortNumber port() {
134 return this.port;
135 }
alshabib99b8fdc2014-09-25 14:30:22 -0700136
137 @Override
138 public String toString() {
139 return toStringHelper(type().toString())
140 .add("port", port).toString();
141 }
alshabibba5ac482014-10-02 17:15:20 -0700142
143 @Override
144 public int hashCode() {
145 return Objects.hash(port);
146 }
147
148 @Override
149 public boolean equals(Object obj) {
150 if (this == obj) {
151 return true;
152 }
153 if (obj instanceof PortCriterion) {
154 PortCriterion that = (PortCriterion) obj;
155 return Objects.equals(port, that.port);
156
157 }
158 return false;
159 }
160
alshabib7b795492014-09-16 14:38:39 -0700161 }
162
163
164 public static final class EthCriterion implements Criterion {
Ayaka Koshibea9c199f2014-09-16 16:21:40 -0700165 private final MacAddress mac;
alshabib7b795492014-09-16 14:38:39 -0700166 private final Type type;
167
Ayaka Koshibea9c199f2014-09-16 16:21:40 -0700168 public EthCriterion(MacAddress mac, Type type) {
alshabib7b795492014-09-16 14:38:39 -0700169 this.mac = mac;
170 this.type = type;
171 }
172
173 @Override
174 public Type type() {
175 return this.type;
176 }
177
Ayaka Koshibea9c199f2014-09-16 16:21:40 -0700178 public MacAddress mac() {
alshabib7b795492014-09-16 14:38:39 -0700179 return this.mac;
180 }
alshabib99b8fdc2014-09-25 14:30:22 -0700181
182 @Override
183 public String toString() {
184 return toStringHelper(type().toString())
185 .add("mac", mac).toString();
186 }
187
alshabibba5ac482014-10-02 17:15:20 -0700188 @Override
189 public int hashCode() {
190 return Objects.hash(mac, type);
191 }
192
193 @Override
194 public boolean equals(Object obj) {
195 if (this == obj) {
196 return true;
197 }
198 if (obj instanceof EthCriterion) {
199 EthCriterion that = (EthCriterion) obj;
200 return Objects.equals(mac, that.mac) &&
201 Objects.equals(type, that.type);
202
203
204 }
205 return false;
206 }
207
208
alshabib7b795492014-09-16 14:38:39 -0700209 }
210
211 public static final class EthTypeCriterion implements Criterion {
212
213 private final Short ethType;
214
215 public EthTypeCriterion(Short ethType) {
216 this.ethType = ethType;
217 }
218
219 @Override
220 public Type type() {
221 return Type.ETH_TYPE;
222 }
223
224 public Short ethType() {
225 return ethType;
226 }
227
alshabib99b8fdc2014-09-25 14:30:22 -0700228 @Override
229 public String toString() {
230 return toStringHelper(type().toString())
231 .add("ethType", Long.toHexString(ethType)).toString();
232 }
233
alshabibba5ac482014-10-02 17:15:20 -0700234 @Override
235 public int hashCode() {
236 return Objects.hash(ethType);
237 }
238
239 @Override
240 public boolean equals(Object obj) {
241 if (this == obj) {
242 return true;
243 }
244 if (obj instanceof EthTypeCriterion) {
245 EthTypeCriterion that = (EthTypeCriterion) obj;
246 return Objects.equals(ethType, that.ethType);
247
248
249 }
250 return false;
251 }
252
alshabib7b795492014-09-16 14:38:39 -0700253 }
254
255
256 public static final class IPCriterion implements Criterion {
257
Ayaka Koshibe1d56fe42014-09-19 16:51:58 -0700258 private final IpPrefix ip;
alshabib7b795492014-09-16 14:38:39 -0700259 private final Type type;
260
Ayaka Koshibe1d56fe42014-09-19 16:51:58 -0700261 public IPCriterion(IpPrefix ip, Type type) {
alshabib7b795492014-09-16 14:38:39 -0700262 this.ip = ip;
263 this.type = type;
264 }
265
266 @Override
267 public Type type() {
268 return this.type;
269 }
270
Ayaka Koshibe1d56fe42014-09-19 16:51:58 -0700271 public IpPrefix ip() {
alshabib7b795492014-09-16 14:38:39 -0700272 return this.ip;
273 }
274
alshabib99b8fdc2014-09-25 14:30:22 -0700275 @Override
276 public String toString() {
277 return toStringHelper(type().toString())
278 .add("ip", ip).toString();
279 }
alshabib7b795492014-09-16 14:38:39 -0700280
alshabibba5ac482014-10-02 17:15:20 -0700281 @Override
282 public int hashCode() {
283 return Objects.hash(ip, type);
284 }
285
286 @Override
287 public boolean equals(Object obj) {
288 if (this == obj) {
289 return true;
290 }
291 if (obj instanceof IPCriterion) {
292 IPCriterion that = (IPCriterion) obj;
293 return Objects.equals(ip, that.ip) &&
294 Objects.equals(type, that.type);
295
296
297 }
298 return false;
299 }
300
alshabib7b795492014-09-16 14:38:39 -0700301 }
302
303
304 public static final class IPProtocolCriterion implements Criterion {
305
306 private final Byte proto;
307
308 public IPProtocolCriterion(Byte protocol) {
309 this.proto = protocol;
310 }
311
312 @Override
313 public Type type() {
314 return Type.IP_PROTO;
315 }
316
317 public Byte protocol() {
318 return proto;
319 }
320
alshabib99b8fdc2014-09-25 14:30:22 -0700321 @Override
322 public String toString() {
323 return toStringHelper(type().toString())
324 .add("protocol", Long.toHexString(proto)).toString();
325 }
326
alshabibba5ac482014-10-02 17:15:20 -0700327 @Override
328 public int hashCode() {
329 return Objects.hash(proto);
330 }
331
332 @Override
333 public boolean equals(Object obj) {
334 if (this == obj) {
335 return true;
336 }
337 if (obj instanceof IPProtocolCriterion) {
338 IPProtocolCriterion that = (IPProtocolCriterion) obj;
339 return Objects.equals(proto, that.proto);
340
341
342 }
343 return false;
344 }
345
alshabib7b795492014-09-16 14:38:39 -0700346 }
347
348
349 public static final class VlanPcpCriterion implements Criterion {
350
351 private final Byte vlanPcp;
352
353 public VlanPcpCriterion(Byte vlanPcp) {
354 this.vlanPcp = vlanPcp;
355 }
356
357 @Override
358 public Type type() {
359 return Type.VLAN_PCP;
360 }
361
alshabib35edb1a2014-09-16 17:44:44 -0700362 public Byte priority() {
alshabib7b795492014-09-16 14:38:39 -0700363 return vlanPcp;
364 }
365
alshabib99b8fdc2014-09-25 14:30:22 -0700366 @Override
367 public String toString() {
368 return toStringHelper(type().toString())
369 .add("pcp", Long.toHexString(vlanPcp)).toString();
370 }
371
alshabibba5ac482014-10-02 17:15:20 -0700372 @Override
373 public int hashCode() {
374 return Objects.hash(vlanPcp);
375 }
376
377 @Override
378 public boolean equals(Object obj) {
379 if (this == obj) {
380 return true;
381 }
382 if (obj instanceof VlanPcpCriterion) {
383 VlanPcpCriterion that = (VlanPcpCriterion) obj;
384 return Objects.equals(vlanPcp, that.vlanPcp);
385
386
387 }
388 return false;
389 }
390
alshabib7b795492014-09-16 14:38:39 -0700391 }
392
393
394 public static final class VlanIdCriterion implements Criterion {
395
396
Ayaka Koshibea9c199f2014-09-16 16:21:40 -0700397 private final VlanId vlanId;
alshabib7b795492014-09-16 14:38:39 -0700398
Ayaka Koshibea9c199f2014-09-16 16:21:40 -0700399 public VlanIdCriterion(VlanId vlanId) {
alshabib7b795492014-09-16 14:38:39 -0700400 this.vlanId = vlanId;
401 }
402
403 @Override
404 public Type type() {
405 return Type.VLAN_VID;
406 }
407
Ayaka Koshibea9c199f2014-09-16 16:21:40 -0700408 public VlanId vlanId() {
alshabib7b795492014-09-16 14:38:39 -0700409 return vlanId;
410 }
411
alshabib99b8fdc2014-09-25 14:30:22 -0700412 @Override
413 public String toString() {
414 return toStringHelper(type().toString())
415 .add("id", vlanId).toString();
416 }
417
alshabibba5ac482014-10-02 17:15:20 -0700418 @Override
419 public int hashCode() {
420 return Objects.hash(vlanId);
421 }
422
423 @Override
424 public boolean equals(Object obj) {
425 if (this == obj) {
426 return true;
427 }
428 if (obj instanceof VlanIdCriterion) {
429 VlanIdCriterion that = (VlanIdCriterion) obj;
430 return Objects.equals(vlanId, that.vlanId);
431
432
433 }
434 return false;
435 }
436
alshabib7b795492014-09-16 14:38:39 -0700437 }
438
439
tom8bb16062014-09-12 14:47:46 -0700440}