blob: ebd672c6e78f925eef3e083736b025204e1f5ad0 [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
Toshio Koide9c44c9a2014-10-09 11:44:36 -0700116 /**
117 * Creates a match on TCP source port field using the specified value.
118 *
119 * @param tcpPort
120 * @return match criterion
121 */
122 public static Criterion matchTcpSrc(Short tcpPort) {
123 return new TcpPortCriterion(tcpPort, Type.TCP_SRC);
124 }
125
126 /**
127 * Creates a match on TCP destination port field using the specified value.
128 *
129 * @param tcpPort
130 * @return match criterion
131 */
132 public static Criterion matchTcpDst(Short tcpPort) {
133 return new TcpPortCriterion(tcpPort, Type.TCP_DST);
134 }
alshabib369d2942014-09-12 17:59:35 -0700135
alshabib7b795492014-09-16 14:38:39 -0700136 /*
137 * Implementations of criteria.
138 */
139
140 public static final class PortCriterion implements Criterion {
141 private final PortNumber port;
142
143 public PortCriterion(PortNumber port) {
144 this.port = port;
145 }
146
147 @Override
148 public Type type() {
149 return Type.IN_PORT;
150 }
151
152 public PortNumber port() {
153 return this.port;
154 }
alshabib99b8fdc2014-09-25 14:30:22 -0700155
156 @Override
157 public String toString() {
158 return toStringHelper(type().toString())
159 .add("port", port).toString();
160 }
alshabibba5ac482014-10-02 17:15:20 -0700161
162 @Override
163 public int hashCode() {
alshabib2020b892014-10-20 15:47:08 -0700164 return Objects.hash(port, type());
alshabibba5ac482014-10-02 17:15:20 -0700165 }
166
167 @Override
168 public boolean equals(Object obj) {
169 if (this == obj) {
170 return true;
171 }
172 if (obj instanceof PortCriterion) {
173 PortCriterion that = (PortCriterion) obj;
alshabib2020b892014-10-20 15:47:08 -0700174 return Objects.equals(port, that.port) &&
175 Objects.equals(this.type(), that.type());
alshabibba5ac482014-10-02 17:15:20 -0700176
177 }
178 return false;
179 }
180
alshabib7b795492014-09-16 14:38:39 -0700181 }
182
183
184 public static final class EthCriterion implements Criterion {
Ayaka Koshibea9c199f2014-09-16 16:21:40 -0700185 private final MacAddress mac;
alshabib7b795492014-09-16 14:38:39 -0700186 private final Type type;
187
Ayaka Koshibea9c199f2014-09-16 16:21:40 -0700188 public EthCriterion(MacAddress mac, Type type) {
alshabib7b795492014-09-16 14:38:39 -0700189 this.mac = mac;
190 this.type = type;
191 }
192
193 @Override
194 public Type type() {
195 return this.type;
196 }
197
Ayaka Koshibea9c199f2014-09-16 16:21:40 -0700198 public MacAddress mac() {
alshabib7b795492014-09-16 14:38:39 -0700199 return this.mac;
200 }
alshabib99b8fdc2014-09-25 14:30:22 -0700201
202 @Override
203 public String toString() {
204 return toStringHelper(type().toString())
205 .add("mac", mac).toString();
206 }
207
alshabibba5ac482014-10-02 17:15:20 -0700208 @Override
209 public int hashCode() {
210 return Objects.hash(mac, type);
211 }
212
213 @Override
214 public boolean equals(Object obj) {
215 if (this == obj) {
216 return true;
217 }
218 if (obj instanceof EthCriterion) {
219 EthCriterion that = (EthCriterion) obj;
220 return Objects.equals(mac, that.mac) &&
221 Objects.equals(type, that.type);
222
223
224 }
225 return false;
226 }
227
228
alshabib7b795492014-09-16 14:38:39 -0700229 }
230
231 public static final class EthTypeCriterion implements Criterion {
232
233 private final Short ethType;
234
235 public EthTypeCriterion(Short ethType) {
236 this.ethType = ethType;
237 }
238
239 @Override
240 public Type type() {
241 return Type.ETH_TYPE;
242 }
243
244 public Short ethType() {
245 return ethType;
246 }
247
alshabib99b8fdc2014-09-25 14:30:22 -0700248 @Override
249 public String toString() {
250 return toStringHelper(type().toString())
251 .add("ethType", Long.toHexString(ethType)).toString();
252 }
253
alshabibba5ac482014-10-02 17:15:20 -0700254 @Override
255 public int hashCode() {
alshabib2020b892014-10-20 15:47:08 -0700256 return Objects.hash(ethType, type());
alshabibba5ac482014-10-02 17:15:20 -0700257 }
258
259 @Override
260 public boolean equals(Object obj) {
261 if (this == obj) {
262 return true;
263 }
264 if (obj instanceof EthTypeCriterion) {
265 EthTypeCriterion that = (EthTypeCriterion) obj;
alshabib2020b892014-10-20 15:47:08 -0700266 return Objects.equals(ethType, that.ethType) &&
267 Objects.equals(this.type(), that.type());
alshabibba5ac482014-10-02 17:15:20 -0700268
269
270 }
271 return false;
272 }
273
alshabib7b795492014-09-16 14:38:39 -0700274 }
275
276
277 public static final class IPCriterion implements Criterion {
278
Ayaka Koshibe1d56fe42014-09-19 16:51:58 -0700279 private final IpPrefix ip;
alshabib7b795492014-09-16 14:38:39 -0700280 private final Type type;
281
Ayaka Koshibe1d56fe42014-09-19 16:51:58 -0700282 public IPCriterion(IpPrefix ip, Type type) {
alshabib7b795492014-09-16 14:38:39 -0700283 this.ip = ip;
284 this.type = type;
285 }
286
287 @Override
288 public Type type() {
289 return this.type;
290 }
291
Ayaka Koshibe1d56fe42014-09-19 16:51:58 -0700292 public IpPrefix ip() {
alshabib7b795492014-09-16 14:38:39 -0700293 return this.ip;
294 }
295
alshabib99b8fdc2014-09-25 14:30:22 -0700296 @Override
297 public String toString() {
298 return toStringHelper(type().toString())
299 .add("ip", ip).toString();
300 }
alshabib7b795492014-09-16 14:38:39 -0700301
alshabibba5ac482014-10-02 17:15:20 -0700302 @Override
303 public int hashCode() {
304 return Objects.hash(ip, type);
305 }
306
307 @Override
308 public boolean equals(Object obj) {
309 if (this == obj) {
310 return true;
311 }
312 if (obj instanceof IPCriterion) {
313 IPCriterion that = (IPCriterion) obj;
314 return Objects.equals(ip, that.ip) &&
315 Objects.equals(type, that.type);
316
317
318 }
319 return false;
320 }
321
alshabib7b795492014-09-16 14:38:39 -0700322 }
323
324
325 public static final class IPProtocolCriterion implements Criterion {
326
327 private final Byte proto;
328
329 public IPProtocolCriterion(Byte protocol) {
330 this.proto = protocol;
331 }
332
333 @Override
334 public Type type() {
335 return Type.IP_PROTO;
336 }
337
338 public Byte protocol() {
339 return proto;
340 }
341
alshabib99b8fdc2014-09-25 14:30:22 -0700342 @Override
343 public String toString() {
344 return toStringHelper(type().toString())
345 .add("protocol", Long.toHexString(proto)).toString();
346 }
347
alshabibba5ac482014-10-02 17:15:20 -0700348 @Override
349 public int hashCode() {
alshabib2020b892014-10-20 15:47:08 -0700350 return Objects.hash(proto, type());
alshabibba5ac482014-10-02 17:15:20 -0700351 }
352
353 @Override
354 public boolean equals(Object obj) {
355 if (this == obj) {
356 return true;
357 }
358 if (obj instanceof IPProtocolCriterion) {
359 IPProtocolCriterion that = (IPProtocolCriterion) obj;
360 return Objects.equals(proto, that.proto);
361
362
363 }
364 return false;
365 }
366
alshabib7b795492014-09-16 14:38:39 -0700367 }
368
369
370 public static final class VlanPcpCriterion implements Criterion {
371
372 private final Byte vlanPcp;
373
374 public VlanPcpCriterion(Byte vlanPcp) {
375 this.vlanPcp = vlanPcp;
376 }
377
378 @Override
379 public Type type() {
380 return Type.VLAN_PCP;
381 }
382
alshabib35edb1a2014-09-16 17:44:44 -0700383 public Byte priority() {
alshabib7b795492014-09-16 14:38:39 -0700384 return vlanPcp;
385 }
386
alshabib99b8fdc2014-09-25 14:30:22 -0700387 @Override
388 public String toString() {
389 return toStringHelper(type().toString())
390 .add("pcp", Long.toHexString(vlanPcp)).toString();
391 }
392
alshabibba5ac482014-10-02 17:15:20 -0700393 @Override
394 public int hashCode() {
395 return Objects.hash(vlanPcp);
396 }
397
398 @Override
399 public boolean equals(Object obj) {
400 if (this == obj) {
401 return true;
402 }
403 if (obj instanceof VlanPcpCriterion) {
404 VlanPcpCriterion that = (VlanPcpCriterion) obj;
alshabib2020b892014-10-20 15:47:08 -0700405 return Objects.equals(vlanPcp, that.vlanPcp) &&
406 Objects.equals(this.type(), that.type());
alshabibba5ac482014-10-02 17:15:20 -0700407
408
409 }
410 return false;
411 }
412
alshabib7b795492014-09-16 14:38:39 -0700413 }
414
415
416 public static final class VlanIdCriterion implements Criterion {
417
418
Ayaka Koshibea9c199f2014-09-16 16:21:40 -0700419 private final VlanId vlanId;
alshabib7b795492014-09-16 14:38:39 -0700420
Ayaka Koshibea9c199f2014-09-16 16:21:40 -0700421 public VlanIdCriterion(VlanId vlanId) {
alshabib7b795492014-09-16 14:38:39 -0700422 this.vlanId = vlanId;
423 }
424
425 @Override
426 public Type type() {
427 return Type.VLAN_VID;
428 }
429
Ayaka Koshibea9c199f2014-09-16 16:21:40 -0700430 public VlanId vlanId() {
alshabib7b795492014-09-16 14:38:39 -0700431 return vlanId;
432 }
433
alshabib99b8fdc2014-09-25 14:30:22 -0700434 @Override
435 public String toString() {
436 return toStringHelper(type().toString())
437 .add("id", vlanId).toString();
438 }
439
alshabibba5ac482014-10-02 17:15:20 -0700440 @Override
441 public int hashCode() {
alshabib2020b892014-10-20 15:47:08 -0700442 return Objects.hash(vlanId, type());
alshabibba5ac482014-10-02 17:15:20 -0700443 }
444
445 @Override
446 public boolean equals(Object obj) {
447 if (this == obj) {
448 return true;
449 }
450 if (obj instanceof VlanIdCriterion) {
451 VlanIdCriterion that = (VlanIdCriterion) obj;
alshabib2020b892014-10-20 15:47:08 -0700452 return Objects.equals(vlanId, that.vlanId) &&
453 Objects.equals(this.type(), that.type());
alshabibba5ac482014-10-02 17:15:20 -0700454
455
456 }
457 return false;
458 }
459
alshabib7b795492014-09-16 14:38:39 -0700460 }
461
462
Toshio Koide9c44c9a2014-10-09 11:44:36 -0700463 public static final class TcpPortCriterion implements Criterion {
464
465 private final Short tcpPort;
466 private final Type type;
467
468 public TcpPortCriterion(Short tcpPort, Type type) {
469 this.tcpPort = tcpPort;
470 this.type = type;
471 }
472
473 @Override
474 public Type type() {
475 return this.type;
476 }
477
478 public Short tcpPort() {
479 return this.tcpPort;
480 }
481
482 @Override
483 public String toString() {
484 return toStringHelper(type().toString())
485 .add("tcpPort", tcpPort).toString();
486 }
487
488 @Override
489 public int hashCode() {
490 return Objects.hash(tcpPort, type);
491 }
492
493 @Override
494 public boolean equals(Object obj) {
495 if (this == obj) {
496 return true;
497 }
498 if (obj instanceof TcpPortCriterion) {
499 TcpPortCriterion that = (TcpPortCriterion) obj;
500 return Objects.equals(tcpPort, that.tcpPort) &&
501 Objects.equals(type, that.type);
502
503
504 }
505 return false;
506 }
507 }
tom8bb16062014-09-12 14:47:46 -0700508}