blob: aba5680ea08c7d0ca3f3f1a4a1849ca79f13ce98 [file] [log] [blame]
Thomas Vachuska83e090e2014-10-22 14:25:35 -07001/*
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07002 * Copyright 2014 Open Networking Laboratory
Thomas Vachuska83e090e2014-10-22 14:25:35 -07003 *
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07004 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
Thomas Vachuska83e090e2014-10-22 14:25:35 -07007 *
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07008 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
Thomas Vachuska83e090e2014-10-22 14:25:35 -070015 */
alshabib7410fea2014-09-16 13:48:39 -070016package org.onlab.onos.net.flow.criteria;
tom8bb16062014-09-12 14:47:46 -070017
alshabib99b8fdc2014-09-25 14:30:22 -070018import static com.google.common.base.MoreObjects.toStringHelper;
19
alshabibba5ac482014-10-02 17:15:20 -070020import java.util.Objects;
21
alshabib7b795492014-09-16 14:38:39 -070022import org.onlab.onos.net.PortNumber;
23import org.onlab.onos.net.flow.criteria.Criterion.Type;
Ayaka Koshibe1d56fe42014-09-19 16:51:58 -070024import org.onlab.packet.IpPrefix;
Ayaka Koshibea9c199f2014-09-16 16:21:40 -070025import org.onlab.packet.MacAddress;
26import org.onlab.packet.VlanId;
alshabib7b795492014-09-16 14:38:39 -070027
tom8bb16062014-09-12 14:47:46 -070028/**
29 * Factory class to create various traffic selection criteria.
30 */
31public final class Criteria {
32
alshabib7b795492014-09-16 14:38:39 -070033 //TODO: incomplete type implementation. Need to implement complete list from Criterion
34
tom8bb16062014-09-12 14:47:46 -070035 // Ban construction
36 private Criteria() {
37 }
38
39 /**
alshabib7b795492014-09-16 14:38:39 -070040 * Creates a match on IN_PORT field using the specified value.
41 *
42 * @param port inport value
43 * @return match criterion
44 */
45 public static Criterion matchInPort(PortNumber port) {
46 return new PortCriterion(port);
47 }
48
49 /**
tom8bb16062014-09-12 14:47:46 -070050 * Creates a match on ETH_SRC field using the specified value. This value
51 * may be a wildcard mask.
52 *
tomc104d282014-09-19 10:57:55 -070053 * @param mac MAC address value or wildcard mask
tom8bb16062014-09-12 14:47:46 -070054 * @return match criterion
55 */
Ayaka Koshibea9c199f2014-09-16 16:21:40 -070056 public static Criterion matchEthSrc(MacAddress mac) {
alshabib7b795492014-09-16 14:38:39 -070057 return new EthCriterion(mac, Type.ETH_SRC);
tom8bb16062014-09-12 14:47:46 -070058 }
59
alshabib369d2942014-09-12 17:59:35 -070060 /**
61 * Creates a match on ETH_DST field using the specified value. This value
62 * may be a wildcard mask.
63 *
tomc104d282014-09-19 10:57:55 -070064 * @param mac MAC address value or wildcard mask
alshabib369d2942014-09-12 17:59:35 -070065 * @return match criterion
66 */
Ayaka Koshibea9c199f2014-09-16 16:21:40 -070067 public static Criterion matchEthDst(MacAddress mac) {
alshabib7b795492014-09-16 14:38:39 -070068 return new EthCriterion(mac, Type.ETH_DST);
69 }
70
71 /**
72 * Creates a match on ETH_TYPE field using the specified value.
73 *
74 * @param ethType eth type value
75 * @return match criterion
76 */
77 public static Criterion matchEthType(Short ethType) {
78 return new EthTypeCriterion(ethType);
79 }
80
81 /**
82 * Creates a match on VLAN ID field using the specified value.
83 *
84 * @param vlanId vlan id value
85 * @return match criterion
86 */
Ayaka Koshibea9c199f2014-09-16 16:21:40 -070087 public static Criterion matchVlanId(VlanId vlanId) {
alshabib7b795492014-09-16 14:38:39 -070088 return new VlanIdCriterion(vlanId);
89 }
90
91 /**
92 * Creates a match on VLAN PCP field using the specified value.
93 *
94 * @param vlanPcp vlan pcp value
95 * @return match criterion
96 */
alshabibb45d1962014-09-18 14:25:45 -070097 public static Criterion matchVlanPcp(Byte vlanPcp) {
alshabib7b795492014-09-16 14:38:39 -070098 return new VlanPcpCriterion(vlanPcp);
99 }
100
101 /**
102 * Creates a match on IP proto field using the specified value.
103 *
104 * @param proto ip protocol value
105 * @return match criterion
106 */
107 public static Criterion matchIPProtocol(Byte proto) {
108 return new IPProtocolCriterion(proto);
109 }
110
111 /**
112 * Creates a match on IP src field using the specified value.
113 *
114 * @param ip ip src value
115 * @return match criterion
116 */
Ayaka Koshibe1d56fe42014-09-19 16:51:58 -0700117 public static Criterion matchIPSrc(IpPrefix ip) {
alshabib7b795492014-09-16 14:38:39 -0700118 return new IPCriterion(ip, Type.IPV4_SRC);
119 }
120
121 /**
122 * Creates a match on IP dst field using the specified value.
123 *
124 * @param ip ip src value
125 * @return match criterion
126 */
Ayaka Koshibe1d56fe42014-09-19 16:51:58 -0700127 public static Criterion matchIPDst(IpPrefix ip) {
alshabib7b795492014-09-16 14:38:39 -0700128 return new IPCriterion(ip, Type.IPV4_DST);
alshabib369d2942014-09-12 17:59:35 -0700129 }
130
Toshio Koide9c44c9a2014-10-09 11:44:36 -0700131 /**
132 * Creates a match on TCP source port field using the specified value.
133 *
134 * @param tcpPort
135 * @return match criterion
136 */
137 public static Criterion matchTcpSrc(Short tcpPort) {
138 return new TcpPortCriterion(tcpPort, Type.TCP_SRC);
139 }
140
141 /**
142 * Creates a match on TCP destination port field using the specified value.
143 *
144 * @param tcpPort
145 * @return match criterion
146 */
147 public static Criterion matchTcpDst(Short tcpPort) {
148 return new TcpPortCriterion(tcpPort, Type.TCP_DST);
149 }
alshabib369d2942014-09-12 17:59:35 -0700150
Marc De Leenheer49087752014-10-23 13:54:09 -0700151 /**
152 * Creates a match on lambda field using the specified value.
153 *
154 * @param lambda
155 * @return match criterion
156 */
157 public static Criterion matchLambda(Short lambda) {
158 return new LambdaCriterion(lambda, Type.OCH_SIGID);
159 }
160
161 /**
Praseed Balakrishnan64369da2014-10-23 15:55:20 -0700162 * Creates a match on lambda field using the specified value.
163 *
164 * @param lambda
165 * @return match criterion
166 */
167 public static Criterion matchOpticalSignalType(Byte lambda) {
168 return new OpticalSignalTypeCriterion(lambda, Type.OCH_SIGTYPE);
169 }
170
171
172 /**
alshabib7b795492014-09-16 14:38:39 -0700173 * Implementations of criteria.
174 */
alshabib7b795492014-09-16 14:38:39 -0700175 public static final class PortCriterion implements Criterion {
176 private final PortNumber port;
177
178 public PortCriterion(PortNumber port) {
179 this.port = port;
180 }
181
182 @Override
183 public Type type() {
184 return Type.IN_PORT;
185 }
186
187 public PortNumber port() {
188 return this.port;
189 }
alshabib99b8fdc2014-09-25 14:30:22 -0700190
191 @Override
192 public String toString() {
193 return toStringHelper(type().toString())
194 .add("port", port).toString();
195 }
alshabibba5ac482014-10-02 17:15:20 -0700196
197 @Override
198 public int hashCode() {
alshabib2020b892014-10-20 15:47:08 -0700199 return Objects.hash(port, type());
alshabibba5ac482014-10-02 17:15:20 -0700200 }
201
202 @Override
203 public boolean equals(Object obj) {
204 if (this == obj) {
205 return true;
206 }
207 if (obj instanceof PortCriterion) {
208 PortCriterion that = (PortCriterion) obj;
alshabib2020b892014-10-20 15:47:08 -0700209 return Objects.equals(port, that.port) &&
210 Objects.equals(this.type(), that.type());
alshabibba5ac482014-10-02 17:15:20 -0700211
212 }
213 return false;
214 }
215
alshabib7b795492014-09-16 14:38:39 -0700216 }
217
218
219 public static final class EthCriterion implements Criterion {
Ayaka Koshibea9c199f2014-09-16 16:21:40 -0700220 private final MacAddress mac;
alshabib7b795492014-09-16 14:38:39 -0700221 private final Type type;
222
Ayaka Koshibea9c199f2014-09-16 16:21:40 -0700223 public EthCriterion(MacAddress mac, Type type) {
alshabib7b795492014-09-16 14:38:39 -0700224 this.mac = mac;
225 this.type = type;
226 }
227
228 @Override
229 public Type type() {
230 return this.type;
231 }
232
Ayaka Koshibea9c199f2014-09-16 16:21:40 -0700233 public MacAddress mac() {
alshabib7b795492014-09-16 14:38:39 -0700234 return this.mac;
235 }
alshabib99b8fdc2014-09-25 14:30:22 -0700236
237 @Override
238 public String toString() {
239 return toStringHelper(type().toString())
240 .add("mac", mac).toString();
241 }
242
alshabibba5ac482014-10-02 17:15:20 -0700243 @Override
244 public int hashCode() {
245 return Objects.hash(mac, type);
246 }
247
248 @Override
249 public boolean equals(Object obj) {
250 if (this == obj) {
251 return true;
252 }
253 if (obj instanceof EthCriterion) {
254 EthCriterion that = (EthCriterion) obj;
255 return Objects.equals(mac, that.mac) &&
256 Objects.equals(type, that.type);
257
258
259 }
260 return false;
261 }
262
263
alshabib7b795492014-09-16 14:38:39 -0700264 }
265
266 public static final class EthTypeCriterion implements Criterion {
267
268 private final Short ethType;
269
270 public EthTypeCriterion(Short ethType) {
271 this.ethType = ethType;
272 }
273
274 @Override
275 public Type type() {
276 return Type.ETH_TYPE;
277 }
278
279 public Short ethType() {
280 return ethType;
281 }
282
alshabib99b8fdc2014-09-25 14:30:22 -0700283 @Override
284 public String toString() {
285 return toStringHelper(type().toString())
286 .add("ethType", Long.toHexString(ethType)).toString();
287 }
288
alshabibba5ac482014-10-02 17:15:20 -0700289 @Override
290 public int hashCode() {
alshabib2020b892014-10-20 15:47:08 -0700291 return Objects.hash(ethType, type());
alshabibba5ac482014-10-02 17:15:20 -0700292 }
293
294 @Override
295 public boolean equals(Object obj) {
296 if (this == obj) {
297 return true;
298 }
299 if (obj instanceof EthTypeCriterion) {
300 EthTypeCriterion that = (EthTypeCriterion) obj;
alshabib2020b892014-10-20 15:47:08 -0700301 return Objects.equals(ethType, that.ethType) &&
302 Objects.equals(this.type(), that.type());
alshabibba5ac482014-10-02 17:15:20 -0700303
304
305 }
306 return false;
307 }
308
alshabib7b795492014-09-16 14:38:39 -0700309 }
310
311
312 public static final class IPCriterion implements Criterion {
313
Ayaka Koshibe1d56fe42014-09-19 16:51:58 -0700314 private final IpPrefix ip;
alshabib7b795492014-09-16 14:38:39 -0700315 private final Type type;
316
Ayaka Koshibe1d56fe42014-09-19 16:51:58 -0700317 public IPCriterion(IpPrefix ip, Type type) {
alshabib7b795492014-09-16 14:38:39 -0700318 this.ip = ip;
319 this.type = type;
320 }
321
322 @Override
323 public Type type() {
324 return this.type;
325 }
326
Ayaka Koshibe1d56fe42014-09-19 16:51:58 -0700327 public IpPrefix ip() {
alshabib7b795492014-09-16 14:38:39 -0700328 return this.ip;
329 }
330
alshabib99b8fdc2014-09-25 14:30:22 -0700331 @Override
332 public String toString() {
333 return toStringHelper(type().toString())
334 .add("ip", ip).toString();
335 }
alshabib7b795492014-09-16 14:38:39 -0700336
alshabibba5ac482014-10-02 17:15:20 -0700337 @Override
338 public int hashCode() {
339 return Objects.hash(ip, type);
340 }
341
342 @Override
343 public boolean equals(Object obj) {
344 if (this == obj) {
345 return true;
346 }
347 if (obj instanceof IPCriterion) {
348 IPCriterion that = (IPCriterion) obj;
349 return Objects.equals(ip, that.ip) &&
350 Objects.equals(type, that.type);
351
352
353 }
354 return false;
355 }
356
alshabib7b795492014-09-16 14:38:39 -0700357 }
358
359
360 public static final class IPProtocolCriterion implements Criterion {
361
362 private final Byte proto;
363
364 public IPProtocolCriterion(Byte protocol) {
365 this.proto = protocol;
366 }
367
368 @Override
369 public Type type() {
370 return Type.IP_PROTO;
371 }
372
373 public Byte protocol() {
374 return proto;
375 }
376
alshabib99b8fdc2014-09-25 14:30:22 -0700377 @Override
378 public String toString() {
379 return toStringHelper(type().toString())
380 .add("protocol", Long.toHexString(proto)).toString();
381 }
382
alshabibba5ac482014-10-02 17:15:20 -0700383 @Override
384 public int hashCode() {
alshabib2020b892014-10-20 15:47:08 -0700385 return Objects.hash(proto, type());
alshabibba5ac482014-10-02 17:15:20 -0700386 }
387
388 @Override
389 public boolean equals(Object obj) {
390 if (this == obj) {
391 return true;
392 }
393 if (obj instanceof IPProtocolCriterion) {
394 IPProtocolCriterion that = (IPProtocolCriterion) obj;
395 return Objects.equals(proto, that.proto);
396
397
398 }
399 return false;
400 }
401
alshabib7b795492014-09-16 14:38:39 -0700402 }
403
404
405 public static final class VlanPcpCriterion implements Criterion {
406
407 private final Byte vlanPcp;
408
409 public VlanPcpCriterion(Byte vlanPcp) {
410 this.vlanPcp = vlanPcp;
411 }
412
413 @Override
414 public Type type() {
415 return Type.VLAN_PCP;
416 }
417
alshabib35edb1a2014-09-16 17:44:44 -0700418 public Byte priority() {
alshabib7b795492014-09-16 14:38:39 -0700419 return vlanPcp;
420 }
421
alshabib99b8fdc2014-09-25 14:30:22 -0700422 @Override
423 public String toString() {
424 return toStringHelper(type().toString())
425 .add("pcp", Long.toHexString(vlanPcp)).toString();
426 }
427
alshabibba5ac482014-10-02 17:15:20 -0700428 @Override
429 public int hashCode() {
430 return Objects.hash(vlanPcp);
431 }
432
433 @Override
434 public boolean equals(Object obj) {
435 if (this == obj) {
436 return true;
437 }
438 if (obj instanceof VlanPcpCriterion) {
439 VlanPcpCriterion that = (VlanPcpCriterion) obj;
alshabib2020b892014-10-20 15:47:08 -0700440 return Objects.equals(vlanPcp, that.vlanPcp) &&
441 Objects.equals(this.type(), that.type());
alshabibba5ac482014-10-02 17:15:20 -0700442
443
444 }
445 return false;
446 }
447
alshabib7b795492014-09-16 14:38:39 -0700448 }
449
450
451 public static final class VlanIdCriterion implements Criterion {
452
453
Ayaka Koshibea9c199f2014-09-16 16:21:40 -0700454 private final VlanId vlanId;
alshabib7b795492014-09-16 14:38:39 -0700455
Ayaka Koshibea9c199f2014-09-16 16:21:40 -0700456 public VlanIdCriterion(VlanId vlanId) {
alshabib7b795492014-09-16 14:38:39 -0700457 this.vlanId = vlanId;
458 }
459
460 @Override
461 public Type type() {
462 return Type.VLAN_VID;
463 }
464
Ayaka Koshibea9c199f2014-09-16 16:21:40 -0700465 public VlanId vlanId() {
alshabib7b795492014-09-16 14:38:39 -0700466 return vlanId;
467 }
468
alshabib99b8fdc2014-09-25 14:30:22 -0700469 @Override
470 public String toString() {
471 return toStringHelper(type().toString())
472 .add("id", vlanId).toString();
473 }
474
alshabibba5ac482014-10-02 17:15:20 -0700475 @Override
476 public int hashCode() {
alshabib2020b892014-10-20 15:47:08 -0700477 return Objects.hash(vlanId, type());
alshabibba5ac482014-10-02 17:15:20 -0700478 }
479
480 @Override
481 public boolean equals(Object obj) {
482 if (this == obj) {
483 return true;
484 }
485 if (obj instanceof VlanIdCriterion) {
486 VlanIdCriterion that = (VlanIdCriterion) obj;
alshabib2020b892014-10-20 15:47:08 -0700487 return Objects.equals(vlanId, that.vlanId) &&
488 Objects.equals(this.type(), that.type());
alshabibba5ac482014-10-02 17:15:20 -0700489
490
491 }
492 return false;
493 }
494
alshabib7b795492014-09-16 14:38:39 -0700495 }
496
497
Toshio Koide9c44c9a2014-10-09 11:44:36 -0700498 public static final class TcpPortCriterion implements Criterion {
499
500 private final Short tcpPort;
501 private final Type type;
502
503 public TcpPortCriterion(Short tcpPort, Type type) {
504 this.tcpPort = tcpPort;
505 this.type = type;
506 }
507
508 @Override
509 public Type type() {
510 return this.type;
511 }
512
513 public Short tcpPort() {
514 return this.tcpPort;
515 }
516
517 @Override
518 public String toString() {
519 return toStringHelper(type().toString())
520 .add("tcpPort", tcpPort).toString();
521 }
522
523 @Override
524 public int hashCode() {
525 return Objects.hash(tcpPort, type);
526 }
527
528 @Override
529 public boolean equals(Object obj) {
530 if (this == obj) {
531 return true;
532 }
533 if (obj instanceof TcpPortCriterion) {
534 TcpPortCriterion that = (TcpPortCriterion) obj;
535 return Objects.equals(tcpPort, that.tcpPort) &&
536 Objects.equals(type, that.type);
537
538
539 }
540 return false;
541 }
542 }
Marc De Leenheer49087752014-10-23 13:54:09 -0700543
544 public static final class LambdaCriterion implements Criterion {
545
546 private final short lambda;
547 private final Type type;
548
549 public LambdaCriterion(short lambda, Type type) {
550 this.lambda = lambda;
551 this.type = type;
552 }
553
554 @Override
555 public Type type() {
556 return this.type;
557 }
558
559 public Short lambda() {
560 return this.lambda;
561 }
562
563 @Override
564 public String toString() {
565 return toStringHelper(type().toString())
566 .add("lambda", lambda).toString();
567 }
568
569 @Override
570 public int hashCode() {
571 return Objects.hash(lambda, type);
572 }
573
574 @Override
575 public boolean equals(Object obj) {
576 if (this == obj) {
577 return true;
578 }
579 if (obj instanceof LambdaCriterion) {
580 LambdaCriterion that = (LambdaCriterion) obj;
581 return Objects.equals(lambda, that.lambda) &&
582 Objects.equals(type, that.type);
583 }
584 return false;
585 }
586 }
587
Praseed Balakrishnan64369da2014-10-23 15:55:20 -0700588 public static final class OpticalSignalTypeCriterion implements Criterion {
589
590 private final byte signalType;
591 private final Type type;
592
593 public OpticalSignalTypeCriterion(byte signalType, Type type) {
594 this.signalType = signalType;
595 this.type = type;
596 }
597
598 @Override
599 public Type type() {
600 return this.type;
601 }
602
603 public Byte signalType() {
604 return this.signalType;
605 }
606
607 @Override
608 public String toString() {
609 return toStringHelper(type().toString())
610 .add("signalType", signalType).toString();
611 }
612
613 @Override
614 public int hashCode() {
615 return Objects.hash(signalType, type);
616 }
617
618 @Override
619 public boolean equals(Object obj) {
620 if (this == obj) {
621 return true;
622 }
623 if (obj instanceof OpticalSignalTypeCriterion) {
624 OpticalSignalTypeCriterion that = (OpticalSignalTypeCriterion) obj;
625 return Objects.equals(signalType, that.signalType) &&
626 Objects.equals(type, that.type);
627 }
628 return false;
629 }
630 }
631
tom8bb16062014-09-12 14:47:46 -0700632}