blob: 9d8ab7bd2cc24269ad39523c919dd3a76ca462cd [file] [log] [blame]
Thomas Vachuska83e090e2014-10-22 14:25:35 -07001/*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
alshabib7410fea2014-09-16 13:48:39 -070019package org.onlab.onos.net.flow.criteria;
tom8bb16062014-09-12 14:47:46 -070020
alshabib99b8fdc2014-09-25 14:30:22 -070021import static com.google.common.base.MoreObjects.toStringHelper;
22
alshabibba5ac482014-10-02 17:15:20 -070023import java.util.Objects;
24
alshabib7b795492014-09-16 14:38:39 -070025import org.onlab.onos.net.PortNumber;
26import org.onlab.onos.net.flow.criteria.Criterion.Type;
Ayaka Koshibe1d56fe42014-09-19 16:51:58 -070027import org.onlab.packet.IpPrefix;
Ayaka Koshibea9c199f2014-09-16 16:21:40 -070028import org.onlab.packet.MacAddress;
29import org.onlab.packet.VlanId;
alshabib7b795492014-09-16 14:38:39 -070030
tom8bb16062014-09-12 14:47:46 -070031/**
32 * Factory class to create various traffic selection criteria.
33 */
34public final class Criteria {
35
alshabib7b795492014-09-16 14:38:39 -070036 //TODO: incomplete type implementation. Need to implement complete list from Criterion
37
tom8bb16062014-09-12 14:47:46 -070038 // Ban construction
39 private Criteria() {
40 }
41
42 /**
alshabib7b795492014-09-16 14:38:39 -070043 * Creates a match on IN_PORT field using the specified value.
44 *
45 * @param port inport value
46 * @return match criterion
47 */
48 public static Criterion matchInPort(PortNumber port) {
49 return new PortCriterion(port);
50 }
51
52 /**
tom8bb16062014-09-12 14:47:46 -070053 * Creates a match on ETH_SRC field using the specified value. This value
54 * may be a wildcard mask.
55 *
tomc104d282014-09-19 10:57:55 -070056 * @param mac MAC address value or wildcard mask
tom8bb16062014-09-12 14:47:46 -070057 * @return match criterion
58 */
Ayaka Koshibea9c199f2014-09-16 16:21:40 -070059 public static Criterion matchEthSrc(MacAddress mac) {
alshabib7b795492014-09-16 14:38:39 -070060 return new EthCriterion(mac, Type.ETH_SRC);
tom8bb16062014-09-12 14:47:46 -070061 }
62
alshabib369d2942014-09-12 17:59:35 -070063 /**
64 * Creates a match on ETH_DST field using the specified value. This value
65 * may be a wildcard mask.
66 *
tomc104d282014-09-19 10:57:55 -070067 * @param mac MAC address value or wildcard mask
alshabib369d2942014-09-12 17:59:35 -070068 * @return match criterion
69 */
Ayaka Koshibea9c199f2014-09-16 16:21:40 -070070 public static Criterion matchEthDst(MacAddress mac) {
alshabib7b795492014-09-16 14:38:39 -070071 return new EthCriterion(mac, Type.ETH_DST);
72 }
73
74 /**
75 * Creates a match on ETH_TYPE field using the specified value.
76 *
77 * @param ethType eth type value
78 * @return match criterion
79 */
80 public static Criterion matchEthType(Short ethType) {
81 return new EthTypeCriterion(ethType);
82 }
83
84 /**
85 * Creates a match on VLAN ID field using the specified value.
86 *
87 * @param vlanId vlan id value
88 * @return match criterion
89 */
Ayaka Koshibea9c199f2014-09-16 16:21:40 -070090 public static Criterion matchVlanId(VlanId vlanId) {
alshabib7b795492014-09-16 14:38:39 -070091 return new VlanIdCriterion(vlanId);
92 }
93
94 /**
95 * Creates a match on VLAN PCP field using the specified value.
96 *
97 * @param vlanPcp vlan pcp value
98 * @return match criterion
99 */
alshabibb45d1962014-09-18 14:25:45 -0700100 public static Criterion matchVlanPcp(Byte vlanPcp) {
alshabib7b795492014-09-16 14:38:39 -0700101 return new VlanPcpCriterion(vlanPcp);
102 }
103
104 /**
105 * Creates a match on IP proto field using the specified value.
106 *
107 * @param proto ip protocol value
108 * @return match criterion
109 */
110 public static Criterion matchIPProtocol(Byte proto) {
111 return new IPProtocolCriterion(proto);
112 }
113
114 /**
115 * Creates a match on IP src field using the specified value.
116 *
117 * @param ip ip src value
118 * @return match criterion
119 */
Ayaka Koshibe1d56fe42014-09-19 16:51:58 -0700120 public static Criterion matchIPSrc(IpPrefix ip) {
alshabib7b795492014-09-16 14:38:39 -0700121 return new IPCriterion(ip, Type.IPV4_SRC);
122 }
123
124 /**
125 * Creates a match on IP dst field using the specified value.
126 *
127 * @param ip ip src value
128 * @return match criterion
129 */
Ayaka Koshibe1d56fe42014-09-19 16:51:58 -0700130 public static Criterion matchIPDst(IpPrefix ip) {
alshabib7b795492014-09-16 14:38:39 -0700131 return new IPCriterion(ip, Type.IPV4_DST);
alshabib369d2942014-09-12 17:59:35 -0700132 }
133
Toshio Koide9c44c9a2014-10-09 11:44:36 -0700134 /**
135 * Creates a match on TCP source port field using the specified value.
136 *
137 * @param tcpPort
138 * @return match criterion
139 */
140 public static Criterion matchTcpSrc(Short tcpPort) {
141 return new TcpPortCriterion(tcpPort, Type.TCP_SRC);
142 }
143
144 /**
145 * Creates a match on TCP destination port field using the specified value.
146 *
147 * @param tcpPort
148 * @return match criterion
149 */
150 public static Criterion matchTcpDst(Short tcpPort) {
151 return new TcpPortCriterion(tcpPort, Type.TCP_DST);
152 }
alshabib369d2942014-09-12 17:59:35 -0700153
Marc De Leenheer49087752014-10-23 13:54:09 -0700154 /**
155 * Creates a match on lambda field using the specified value.
156 *
157 * @param lambda
158 * @return match criterion
159 */
160 public static Criterion matchLambda(Short lambda) {
161 return new LambdaCriterion(lambda, Type.OCH_SIGID);
162 }
163
164 /**
Praseed Balakrishnan64369da2014-10-23 15:55:20 -0700165 * Creates a match on lambda field using the specified value.
166 *
167 * @param lambda
168 * @return match criterion
169 */
170 public static Criterion matchOpticalSignalType(Byte lambda) {
171 return new OpticalSignalTypeCriterion(lambda, Type.OCH_SIGTYPE);
172 }
173
174
175 /**
alshabib7b795492014-09-16 14:38:39 -0700176 * Implementations of criteria.
177 */
alshabib7b795492014-09-16 14:38:39 -0700178 public static final class PortCriterion implements Criterion {
179 private final PortNumber port;
180
181 public PortCriterion(PortNumber port) {
182 this.port = port;
183 }
184
185 @Override
186 public Type type() {
187 return Type.IN_PORT;
188 }
189
190 public PortNumber port() {
191 return this.port;
192 }
alshabib99b8fdc2014-09-25 14:30:22 -0700193
194 @Override
195 public String toString() {
196 return toStringHelper(type().toString())
197 .add("port", port).toString();
198 }
alshabibba5ac482014-10-02 17:15:20 -0700199
200 @Override
201 public int hashCode() {
alshabib2020b892014-10-20 15:47:08 -0700202 return Objects.hash(port, type());
alshabibba5ac482014-10-02 17:15:20 -0700203 }
204
205 @Override
206 public boolean equals(Object obj) {
207 if (this == obj) {
208 return true;
209 }
210 if (obj instanceof PortCriterion) {
211 PortCriterion that = (PortCriterion) obj;
alshabib2020b892014-10-20 15:47:08 -0700212 return Objects.equals(port, that.port) &&
213 Objects.equals(this.type(), that.type());
alshabibba5ac482014-10-02 17:15:20 -0700214
215 }
216 return false;
217 }
218
alshabib7b795492014-09-16 14:38:39 -0700219 }
220
221
222 public static final class EthCriterion implements Criterion {
Ayaka Koshibea9c199f2014-09-16 16:21:40 -0700223 private final MacAddress mac;
alshabib7b795492014-09-16 14:38:39 -0700224 private final Type type;
225
Ayaka Koshibea9c199f2014-09-16 16:21:40 -0700226 public EthCriterion(MacAddress mac, Type type) {
alshabib7b795492014-09-16 14:38:39 -0700227 this.mac = mac;
228 this.type = type;
229 }
230
231 @Override
232 public Type type() {
233 return this.type;
234 }
235
Ayaka Koshibea9c199f2014-09-16 16:21:40 -0700236 public MacAddress mac() {
alshabib7b795492014-09-16 14:38:39 -0700237 return this.mac;
238 }
alshabib99b8fdc2014-09-25 14:30:22 -0700239
240 @Override
241 public String toString() {
242 return toStringHelper(type().toString())
243 .add("mac", mac).toString();
244 }
245
alshabibba5ac482014-10-02 17:15:20 -0700246 @Override
247 public int hashCode() {
248 return Objects.hash(mac, type);
249 }
250
251 @Override
252 public boolean equals(Object obj) {
253 if (this == obj) {
254 return true;
255 }
256 if (obj instanceof EthCriterion) {
257 EthCriterion that = (EthCriterion) obj;
258 return Objects.equals(mac, that.mac) &&
259 Objects.equals(type, that.type);
260
261
262 }
263 return false;
264 }
265
266
alshabib7b795492014-09-16 14:38:39 -0700267 }
268
269 public static final class EthTypeCriterion implements Criterion {
270
271 private final Short ethType;
272
273 public EthTypeCriterion(Short ethType) {
274 this.ethType = ethType;
275 }
276
277 @Override
278 public Type type() {
279 return Type.ETH_TYPE;
280 }
281
282 public Short ethType() {
283 return ethType;
284 }
285
alshabib99b8fdc2014-09-25 14:30:22 -0700286 @Override
287 public String toString() {
288 return toStringHelper(type().toString())
289 .add("ethType", Long.toHexString(ethType)).toString();
290 }
291
alshabibba5ac482014-10-02 17:15:20 -0700292 @Override
293 public int hashCode() {
alshabib2020b892014-10-20 15:47:08 -0700294 return Objects.hash(ethType, type());
alshabibba5ac482014-10-02 17:15:20 -0700295 }
296
297 @Override
298 public boolean equals(Object obj) {
299 if (this == obj) {
300 return true;
301 }
302 if (obj instanceof EthTypeCriterion) {
303 EthTypeCriterion that = (EthTypeCriterion) obj;
alshabib2020b892014-10-20 15:47:08 -0700304 return Objects.equals(ethType, that.ethType) &&
305 Objects.equals(this.type(), that.type());
alshabibba5ac482014-10-02 17:15:20 -0700306
307
308 }
309 return false;
310 }
311
alshabib7b795492014-09-16 14:38:39 -0700312 }
313
314
315 public static final class IPCriterion implements Criterion {
316
Ayaka Koshibe1d56fe42014-09-19 16:51:58 -0700317 private final IpPrefix ip;
alshabib7b795492014-09-16 14:38:39 -0700318 private final Type type;
319
Ayaka Koshibe1d56fe42014-09-19 16:51:58 -0700320 public IPCriterion(IpPrefix ip, Type type) {
alshabib7b795492014-09-16 14:38:39 -0700321 this.ip = ip;
322 this.type = type;
323 }
324
325 @Override
326 public Type type() {
327 return this.type;
328 }
329
Ayaka Koshibe1d56fe42014-09-19 16:51:58 -0700330 public IpPrefix ip() {
alshabib7b795492014-09-16 14:38:39 -0700331 return this.ip;
332 }
333
alshabib99b8fdc2014-09-25 14:30:22 -0700334 @Override
335 public String toString() {
336 return toStringHelper(type().toString())
337 .add("ip", ip).toString();
338 }
alshabib7b795492014-09-16 14:38:39 -0700339
alshabibba5ac482014-10-02 17:15:20 -0700340 @Override
341 public int hashCode() {
342 return Objects.hash(ip, type);
343 }
344
345 @Override
346 public boolean equals(Object obj) {
347 if (this == obj) {
348 return true;
349 }
350 if (obj instanceof IPCriterion) {
351 IPCriterion that = (IPCriterion) obj;
352 return Objects.equals(ip, that.ip) &&
353 Objects.equals(type, that.type);
354
355
356 }
357 return false;
358 }
359
alshabib7b795492014-09-16 14:38:39 -0700360 }
361
362
363 public static final class IPProtocolCriterion implements Criterion {
364
365 private final Byte proto;
366
367 public IPProtocolCriterion(Byte protocol) {
368 this.proto = protocol;
369 }
370
371 @Override
372 public Type type() {
373 return Type.IP_PROTO;
374 }
375
376 public Byte protocol() {
377 return proto;
378 }
379
alshabib99b8fdc2014-09-25 14:30:22 -0700380 @Override
381 public String toString() {
382 return toStringHelper(type().toString())
383 .add("protocol", Long.toHexString(proto)).toString();
384 }
385
alshabibba5ac482014-10-02 17:15:20 -0700386 @Override
387 public int hashCode() {
alshabib2020b892014-10-20 15:47:08 -0700388 return Objects.hash(proto, type());
alshabibba5ac482014-10-02 17:15:20 -0700389 }
390
391 @Override
392 public boolean equals(Object obj) {
393 if (this == obj) {
394 return true;
395 }
396 if (obj instanceof IPProtocolCriterion) {
397 IPProtocolCriterion that = (IPProtocolCriterion) obj;
398 return Objects.equals(proto, that.proto);
399
400
401 }
402 return false;
403 }
404
alshabib7b795492014-09-16 14:38:39 -0700405 }
406
407
408 public static final class VlanPcpCriterion implements Criterion {
409
410 private final Byte vlanPcp;
411
412 public VlanPcpCriterion(Byte vlanPcp) {
413 this.vlanPcp = vlanPcp;
414 }
415
416 @Override
417 public Type type() {
418 return Type.VLAN_PCP;
419 }
420
alshabib35edb1a2014-09-16 17:44:44 -0700421 public Byte priority() {
alshabib7b795492014-09-16 14:38:39 -0700422 return vlanPcp;
423 }
424
alshabib99b8fdc2014-09-25 14:30:22 -0700425 @Override
426 public String toString() {
427 return toStringHelper(type().toString())
428 .add("pcp", Long.toHexString(vlanPcp)).toString();
429 }
430
alshabibba5ac482014-10-02 17:15:20 -0700431 @Override
432 public int hashCode() {
433 return Objects.hash(vlanPcp);
434 }
435
436 @Override
437 public boolean equals(Object obj) {
438 if (this == obj) {
439 return true;
440 }
441 if (obj instanceof VlanPcpCriterion) {
442 VlanPcpCriterion that = (VlanPcpCriterion) obj;
alshabib2020b892014-10-20 15:47:08 -0700443 return Objects.equals(vlanPcp, that.vlanPcp) &&
444 Objects.equals(this.type(), that.type());
alshabibba5ac482014-10-02 17:15:20 -0700445
446
447 }
448 return false;
449 }
450
alshabib7b795492014-09-16 14:38:39 -0700451 }
452
453
454 public static final class VlanIdCriterion implements Criterion {
455
456
Ayaka Koshibea9c199f2014-09-16 16:21:40 -0700457 private final VlanId vlanId;
alshabib7b795492014-09-16 14:38:39 -0700458
Ayaka Koshibea9c199f2014-09-16 16:21:40 -0700459 public VlanIdCriterion(VlanId vlanId) {
alshabib7b795492014-09-16 14:38:39 -0700460 this.vlanId = vlanId;
461 }
462
463 @Override
464 public Type type() {
465 return Type.VLAN_VID;
466 }
467
Ayaka Koshibea9c199f2014-09-16 16:21:40 -0700468 public VlanId vlanId() {
alshabib7b795492014-09-16 14:38:39 -0700469 return vlanId;
470 }
471
alshabib99b8fdc2014-09-25 14:30:22 -0700472 @Override
473 public String toString() {
474 return toStringHelper(type().toString())
475 .add("id", vlanId).toString();
476 }
477
alshabibba5ac482014-10-02 17:15:20 -0700478 @Override
479 public int hashCode() {
alshabib2020b892014-10-20 15:47:08 -0700480 return Objects.hash(vlanId, type());
alshabibba5ac482014-10-02 17:15:20 -0700481 }
482
483 @Override
484 public boolean equals(Object obj) {
485 if (this == obj) {
486 return true;
487 }
488 if (obj instanceof VlanIdCriterion) {
489 VlanIdCriterion that = (VlanIdCriterion) obj;
alshabib2020b892014-10-20 15:47:08 -0700490 return Objects.equals(vlanId, that.vlanId) &&
491 Objects.equals(this.type(), that.type());
alshabibba5ac482014-10-02 17:15:20 -0700492
493
494 }
495 return false;
496 }
497
alshabib7b795492014-09-16 14:38:39 -0700498 }
499
500
Toshio Koide9c44c9a2014-10-09 11:44:36 -0700501 public static final class TcpPortCriterion implements Criterion {
502
503 private final Short tcpPort;
504 private final Type type;
505
506 public TcpPortCriterion(Short tcpPort, Type type) {
507 this.tcpPort = tcpPort;
508 this.type = type;
509 }
510
511 @Override
512 public Type type() {
513 return this.type;
514 }
515
516 public Short tcpPort() {
517 return this.tcpPort;
518 }
519
520 @Override
521 public String toString() {
522 return toStringHelper(type().toString())
523 .add("tcpPort", tcpPort).toString();
524 }
525
526 @Override
527 public int hashCode() {
528 return Objects.hash(tcpPort, type);
529 }
530
531 @Override
532 public boolean equals(Object obj) {
533 if (this == obj) {
534 return true;
535 }
536 if (obj instanceof TcpPortCriterion) {
537 TcpPortCriterion that = (TcpPortCriterion) obj;
538 return Objects.equals(tcpPort, that.tcpPort) &&
539 Objects.equals(type, that.type);
540
541
542 }
543 return false;
544 }
545 }
Marc De Leenheer49087752014-10-23 13:54:09 -0700546
547 public static final class LambdaCriterion implements Criterion {
548
549 private final short lambda;
550 private final Type type;
551
552 public LambdaCriterion(short lambda, Type type) {
553 this.lambda = lambda;
554 this.type = type;
555 }
556
557 @Override
558 public Type type() {
559 return this.type;
560 }
561
562 public Short lambda() {
563 return this.lambda;
564 }
565
566 @Override
567 public String toString() {
568 return toStringHelper(type().toString())
569 .add("lambda", lambda).toString();
570 }
571
572 @Override
573 public int hashCode() {
574 return Objects.hash(lambda, type);
575 }
576
577 @Override
578 public boolean equals(Object obj) {
579 if (this == obj) {
580 return true;
581 }
582 if (obj instanceof LambdaCriterion) {
583 LambdaCriterion that = (LambdaCriterion) obj;
584 return Objects.equals(lambda, that.lambda) &&
585 Objects.equals(type, that.type);
586 }
587 return false;
588 }
589 }
590
Praseed Balakrishnan64369da2014-10-23 15:55:20 -0700591 public static final class OpticalSignalTypeCriterion implements Criterion {
592
593 private final byte signalType;
594 private final Type type;
595
596 public OpticalSignalTypeCriterion(byte signalType, Type type) {
597 this.signalType = signalType;
598 this.type = type;
599 }
600
601 @Override
602 public Type type() {
603 return this.type;
604 }
605
606 public Byte signalType() {
607 return this.signalType;
608 }
609
610 @Override
611 public String toString() {
612 return toStringHelper(type().toString())
613 .add("signalType", signalType).toString();
614 }
615
616 @Override
617 public int hashCode() {
618 return Objects.hash(signalType, type);
619 }
620
621 @Override
622 public boolean equals(Object obj) {
623 if (this == obj) {
624 return true;
625 }
626 if (obj instanceof OpticalSignalTypeCriterion) {
627 OpticalSignalTypeCriterion that = (OpticalSignalTypeCriterion) obj;
628 return Objects.equals(signalType, that.signalType) &&
629 Objects.equals(type, that.type);
630 }
631 return false;
632 }
633 }
634
tom8bb16062014-09-12 14:47:46 -0700635}