blob: 2e177f7e75a4b775762b0af8d9b7467d54e56a32 [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 /**
alshabib7b795492014-09-16 14:38:39 -0700165 * Implementations of criteria.
166 */
alshabib7b795492014-09-16 14:38:39 -0700167 public static final class PortCriterion implements Criterion {
168 private final PortNumber port;
169
170 public PortCriterion(PortNumber port) {
171 this.port = port;
172 }
173
174 @Override
175 public Type type() {
176 return Type.IN_PORT;
177 }
178
179 public PortNumber port() {
180 return this.port;
181 }
alshabib99b8fdc2014-09-25 14:30:22 -0700182
183 @Override
184 public String toString() {
185 return toStringHelper(type().toString())
186 .add("port", port).toString();
187 }
alshabibba5ac482014-10-02 17:15:20 -0700188
189 @Override
190 public int hashCode() {
alshabib2020b892014-10-20 15:47:08 -0700191 return Objects.hash(port, type());
alshabibba5ac482014-10-02 17:15:20 -0700192 }
193
194 @Override
195 public boolean equals(Object obj) {
196 if (this == obj) {
197 return true;
198 }
199 if (obj instanceof PortCriterion) {
200 PortCriterion that = (PortCriterion) obj;
alshabib2020b892014-10-20 15:47:08 -0700201 return Objects.equals(port, that.port) &&
202 Objects.equals(this.type(), that.type());
alshabibba5ac482014-10-02 17:15:20 -0700203
204 }
205 return false;
206 }
207
alshabib7b795492014-09-16 14:38:39 -0700208 }
209
210
211 public static final class EthCriterion implements Criterion {
Ayaka Koshibea9c199f2014-09-16 16:21:40 -0700212 private final MacAddress mac;
alshabib7b795492014-09-16 14:38:39 -0700213 private final Type type;
214
Ayaka Koshibea9c199f2014-09-16 16:21:40 -0700215 public EthCriterion(MacAddress mac, Type type) {
alshabib7b795492014-09-16 14:38:39 -0700216 this.mac = mac;
217 this.type = type;
218 }
219
220 @Override
221 public Type type() {
222 return this.type;
223 }
224
Ayaka Koshibea9c199f2014-09-16 16:21:40 -0700225 public MacAddress mac() {
alshabib7b795492014-09-16 14:38:39 -0700226 return this.mac;
227 }
alshabib99b8fdc2014-09-25 14:30:22 -0700228
229 @Override
230 public String toString() {
231 return toStringHelper(type().toString())
232 .add("mac", mac).toString();
233 }
234
alshabibba5ac482014-10-02 17:15:20 -0700235 @Override
236 public int hashCode() {
237 return Objects.hash(mac, type);
238 }
239
240 @Override
241 public boolean equals(Object obj) {
242 if (this == obj) {
243 return true;
244 }
245 if (obj instanceof EthCriterion) {
246 EthCriterion that = (EthCriterion) obj;
247 return Objects.equals(mac, that.mac) &&
248 Objects.equals(type, that.type);
249
250
251 }
252 return false;
253 }
254
255
alshabib7b795492014-09-16 14:38:39 -0700256 }
257
258 public static final class EthTypeCriterion implements Criterion {
259
260 private final Short ethType;
261
262 public EthTypeCriterion(Short ethType) {
263 this.ethType = ethType;
264 }
265
266 @Override
267 public Type type() {
268 return Type.ETH_TYPE;
269 }
270
271 public Short ethType() {
272 return ethType;
273 }
274
alshabib99b8fdc2014-09-25 14:30:22 -0700275 @Override
276 public String toString() {
277 return toStringHelper(type().toString())
278 .add("ethType", Long.toHexString(ethType)).toString();
279 }
280
alshabibba5ac482014-10-02 17:15:20 -0700281 @Override
282 public int hashCode() {
alshabib2020b892014-10-20 15:47:08 -0700283 return Objects.hash(ethType, type());
alshabibba5ac482014-10-02 17:15:20 -0700284 }
285
286 @Override
287 public boolean equals(Object obj) {
288 if (this == obj) {
289 return true;
290 }
291 if (obj instanceof EthTypeCriterion) {
292 EthTypeCriterion that = (EthTypeCriterion) obj;
alshabib2020b892014-10-20 15:47:08 -0700293 return Objects.equals(ethType, that.ethType) &&
294 Objects.equals(this.type(), that.type());
alshabibba5ac482014-10-02 17:15:20 -0700295
296
297 }
298 return false;
299 }
300
alshabib7b795492014-09-16 14:38:39 -0700301 }
302
303
304 public static final class IPCriterion implements Criterion {
305
Ayaka Koshibe1d56fe42014-09-19 16:51:58 -0700306 private final IpPrefix ip;
alshabib7b795492014-09-16 14:38:39 -0700307 private final Type type;
308
Ayaka Koshibe1d56fe42014-09-19 16:51:58 -0700309 public IPCriterion(IpPrefix ip, Type type) {
alshabib7b795492014-09-16 14:38:39 -0700310 this.ip = ip;
311 this.type = type;
312 }
313
314 @Override
315 public Type type() {
316 return this.type;
317 }
318
Ayaka Koshibe1d56fe42014-09-19 16:51:58 -0700319 public IpPrefix ip() {
alshabib7b795492014-09-16 14:38:39 -0700320 return this.ip;
321 }
322
alshabib99b8fdc2014-09-25 14:30:22 -0700323 @Override
324 public String toString() {
325 return toStringHelper(type().toString())
326 .add("ip", ip).toString();
327 }
alshabib7b795492014-09-16 14:38:39 -0700328
alshabibba5ac482014-10-02 17:15:20 -0700329 @Override
330 public int hashCode() {
331 return Objects.hash(ip, type);
332 }
333
334 @Override
335 public boolean equals(Object obj) {
336 if (this == obj) {
337 return true;
338 }
339 if (obj instanceof IPCriterion) {
340 IPCriterion that = (IPCriterion) obj;
341 return Objects.equals(ip, that.ip) &&
342 Objects.equals(type, that.type);
343
344
345 }
346 return false;
347 }
348
alshabib7b795492014-09-16 14:38:39 -0700349 }
350
351
352 public static final class IPProtocolCriterion implements Criterion {
353
354 private final Byte proto;
355
356 public IPProtocolCriterion(Byte protocol) {
357 this.proto = protocol;
358 }
359
360 @Override
361 public Type type() {
362 return Type.IP_PROTO;
363 }
364
365 public Byte protocol() {
366 return proto;
367 }
368
alshabib99b8fdc2014-09-25 14:30:22 -0700369 @Override
370 public String toString() {
371 return toStringHelper(type().toString())
372 .add("protocol", Long.toHexString(proto)).toString();
373 }
374
alshabibba5ac482014-10-02 17:15:20 -0700375 @Override
376 public int hashCode() {
alshabib2020b892014-10-20 15:47:08 -0700377 return Objects.hash(proto, type());
alshabibba5ac482014-10-02 17:15:20 -0700378 }
379
380 @Override
381 public boolean equals(Object obj) {
382 if (this == obj) {
383 return true;
384 }
385 if (obj instanceof IPProtocolCriterion) {
386 IPProtocolCriterion that = (IPProtocolCriterion) obj;
387 return Objects.equals(proto, that.proto);
388
389
390 }
391 return false;
392 }
393
alshabib7b795492014-09-16 14:38:39 -0700394 }
395
396
397 public static final class VlanPcpCriterion implements Criterion {
398
399 private final Byte vlanPcp;
400
401 public VlanPcpCriterion(Byte vlanPcp) {
402 this.vlanPcp = vlanPcp;
403 }
404
405 @Override
406 public Type type() {
407 return Type.VLAN_PCP;
408 }
409
alshabib35edb1a2014-09-16 17:44:44 -0700410 public Byte priority() {
alshabib7b795492014-09-16 14:38:39 -0700411 return vlanPcp;
412 }
413
alshabib99b8fdc2014-09-25 14:30:22 -0700414 @Override
415 public String toString() {
416 return toStringHelper(type().toString())
417 .add("pcp", Long.toHexString(vlanPcp)).toString();
418 }
419
alshabibba5ac482014-10-02 17:15:20 -0700420 @Override
421 public int hashCode() {
422 return Objects.hash(vlanPcp);
423 }
424
425 @Override
426 public boolean equals(Object obj) {
427 if (this == obj) {
428 return true;
429 }
430 if (obj instanceof VlanPcpCriterion) {
431 VlanPcpCriterion that = (VlanPcpCriterion) obj;
alshabib2020b892014-10-20 15:47:08 -0700432 return Objects.equals(vlanPcp, that.vlanPcp) &&
433 Objects.equals(this.type(), that.type());
alshabibba5ac482014-10-02 17:15:20 -0700434
435
436 }
437 return false;
438 }
439
alshabib7b795492014-09-16 14:38:39 -0700440 }
441
442
443 public static final class VlanIdCriterion implements Criterion {
444
445
Ayaka Koshibea9c199f2014-09-16 16:21:40 -0700446 private final VlanId vlanId;
alshabib7b795492014-09-16 14:38:39 -0700447
Ayaka Koshibea9c199f2014-09-16 16:21:40 -0700448 public VlanIdCriterion(VlanId vlanId) {
alshabib7b795492014-09-16 14:38:39 -0700449 this.vlanId = vlanId;
450 }
451
452 @Override
453 public Type type() {
454 return Type.VLAN_VID;
455 }
456
Ayaka Koshibea9c199f2014-09-16 16:21:40 -0700457 public VlanId vlanId() {
alshabib7b795492014-09-16 14:38:39 -0700458 return vlanId;
459 }
460
alshabib99b8fdc2014-09-25 14:30:22 -0700461 @Override
462 public String toString() {
463 return toStringHelper(type().toString())
464 .add("id", vlanId).toString();
465 }
466
alshabibba5ac482014-10-02 17:15:20 -0700467 @Override
468 public int hashCode() {
alshabib2020b892014-10-20 15:47:08 -0700469 return Objects.hash(vlanId, type());
alshabibba5ac482014-10-02 17:15:20 -0700470 }
471
472 @Override
473 public boolean equals(Object obj) {
474 if (this == obj) {
475 return true;
476 }
477 if (obj instanceof VlanIdCriterion) {
478 VlanIdCriterion that = (VlanIdCriterion) obj;
alshabib2020b892014-10-20 15:47:08 -0700479 return Objects.equals(vlanId, that.vlanId) &&
480 Objects.equals(this.type(), that.type());
alshabibba5ac482014-10-02 17:15:20 -0700481
482
483 }
484 return false;
485 }
486
alshabib7b795492014-09-16 14:38:39 -0700487 }
488
489
Toshio Koide9c44c9a2014-10-09 11:44:36 -0700490 public static final class TcpPortCriterion implements Criterion {
491
492 private final Short tcpPort;
493 private final Type type;
494
495 public TcpPortCriterion(Short tcpPort, Type type) {
496 this.tcpPort = tcpPort;
497 this.type = type;
498 }
499
500 @Override
501 public Type type() {
502 return this.type;
503 }
504
505 public Short tcpPort() {
506 return this.tcpPort;
507 }
508
509 @Override
510 public String toString() {
511 return toStringHelper(type().toString())
512 .add("tcpPort", tcpPort).toString();
513 }
514
515 @Override
516 public int hashCode() {
517 return Objects.hash(tcpPort, type);
518 }
519
520 @Override
521 public boolean equals(Object obj) {
522 if (this == obj) {
523 return true;
524 }
525 if (obj instanceof TcpPortCriterion) {
526 TcpPortCriterion that = (TcpPortCriterion) obj;
527 return Objects.equals(tcpPort, that.tcpPort) &&
528 Objects.equals(type, that.type);
529
530
531 }
532 return false;
533 }
534 }
Marc De Leenheer49087752014-10-23 13:54:09 -0700535
536 public static final class LambdaCriterion implements Criterion {
537
538 private final short lambda;
539 private final Type type;
540
541 public LambdaCriterion(short lambda, Type type) {
542 this.lambda = lambda;
543 this.type = type;
544 }
545
546 @Override
547 public Type type() {
548 return this.type;
549 }
550
551 public Short lambda() {
552 return this.lambda;
553 }
554
555 @Override
556 public String toString() {
557 return toStringHelper(type().toString())
558 .add("lambda", lambda).toString();
559 }
560
561 @Override
562 public int hashCode() {
563 return Objects.hash(lambda, type);
564 }
565
566 @Override
567 public boolean equals(Object obj) {
568 if (this == obj) {
569 return true;
570 }
571 if (obj instanceof LambdaCriterion) {
572 LambdaCriterion that = (LambdaCriterion) obj;
573 return Objects.equals(lambda, that.lambda) &&
574 Objects.equals(type, that.type);
575 }
576 return false;
577 }
578 }
579
tom8bb16062014-09-12 14:47:46 -0700580}