blob: fb5fb97bb87e7b0dbbc0b4ff030479f477f19e61 [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
alshabib7b795492014-09-16 14:38:39 -0700154 /*
155 * Implementations of criteria.
156 */
157
158 public static final class PortCriterion implements Criterion {
159 private final PortNumber port;
160
161 public PortCriterion(PortNumber port) {
162 this.port = port;
163 }
164
165 @Override
166 public Type type() {
167 return Type.IN_PORT;
168 }
169
170 public PortNumber port() {
171 return this.port;
172 }
alshabib99b8fdc2014-09-25 14:30:22 -0700173
174 @Override
175 public String toString() {
176 return toStringHelper(type().toString())
177 .add("port", port).toString();
178 }
alshabibba5ac482014-10-02 17:15:20 -0700179
180 @Override
181 public int hashCode() {
alshabib2020b892014-10-20 15:47:08 -0700182 return Objects.hash(port, type());
alshabibba5ac482014-10-02 17:15:20 -0700183 }
184
185 @Override
186 public boolean equals(Object obj) {
187 if (this == obj) {
188 return true;
189 }
190 if (obj instanceof PortCriterion) {
191 PortCriterion that = (PortCriterion) obj;
alshabib2020b892014-10-20 15:47:08 -0700192 return Objects.equals(port, that.port) &&
193 Objects.equals(this.type(), that.type());
alshabibba5ac482014-10-02 17:15:20 -0700194
195 }
196 return false;
197 }
198
alshabib7b795492014-09-16 14:38:39 -0700199 }
200
201
202 public static final class EthCriterion implements Criterion {
Ayaka Koshibea9c199f2014-09-16 16:21:40 -0700203 private final MacAddress mac;
alshabib7b795492014-09-16 14:38:39 -0700204 private final Type type;
205
Ayaka Koshibea9c199f2014-09-16 16:21:40 -0700206 public EthCriterion(MacAddress mac, Type type) {
alshabib7b795492014-09-16 14:38:39 -0700207 this.mac = mac;
208 this.type = type;
209 }
210
211 @Override
212 public Type type() {
213 return this.type;
214 }
215
Ayaka Koshibea9c199f2014-09-16 16:21:40 -0700216 public MacAddress mac() {
alshabib7b795492014-09-16 14:38:39 -0700217 return this.mac;
218 }
alshabib99b8fdc2014-09-25 14:30:22 -0700219
220 @Override
221 public String toString() {
222 return toStringHelper(type().toString())
223 .add("mac", mac).toString();
224 }
225
alshabibba5ac482014-10-02 17:15:20 -0700226 @Override
227 public int hashCode() {
228 return Objects.hash(mac, type);
229 }
230
231 @Override
232 public boolean equals(Object obj) {
233 if (this == obj) {
234 return true;
235 }
236 if (obj instanceof EthCriterion) {
237 EthCriterion that = (EthCriterion) obj;
238 return Objects.equals(mac, that.mac) &&
239 Objects.equals(type, that.type);
240
241
242 }
243 return false;
244 }
245
246
alshabib7b795492014-09-16 14:38:39 -0700247 }
248
249 public static final class EthTypeCriterion implements Criterion {
250
251 private final Short ethType;
252
253 public EthTypeCriterion(Short ethType) {
254 this.ethType = ethType;
255 }
256
257 @Override
258 public Type type() {
259 return Type.ETH_TYPE;
260 }
261
262 public Short ethType() {
263 return ethType;
264 }
265
alshabib99b8fdc2014-09-25 14:30:22 -0700266 @Override
267 public String toString() {
268 return toStringHelper(type().toString())
269 .add("ethType", Long.toHexString(ethType)).toString();
270 }
271
alshabibba5ac482014-10-02 17:15:20 -0700272 @Override
273 public int hashCode() {
alshabib2020b892014-10-20 15:47:08 -0700274 return Objects.hash(ethType, type());
alshabibba5ac482014-10-02 17:15:20 -0700275 }
276
277 @Override
278 public boolean equals(Object obj) {
279 if (this == obj) {
280 return true;
281 }
282 if (obj instanceof EthTypeCriterion) {
283 EthTypeCriterion that = (EthTypeCriterion) obj;
alshabib2020b892014-10-20 15:47:08 -0700284 return Objects.equals(ethType, that.ethType) &&
285 Objects.equals(this.type(), that.type());
alshabibba5ac482014-10-02 17:15:20 -0700286
287
288 }
289 return false;
290 }
291
alshabib7b795492014-09-16 14:38:39 -0700292 }
293
294
295 public static final class IPCriterion implements Criterion {
296
Ayaka Koshibe1d56fe42014-09-19 16:51:58 -0700297 private final IpPrefix ip;
alshabib7b795492014-09-16 14:38:39 -0700298 private final Type type;
299
Ayaka Koshibe1d56fe42014-09-19 16:51:58 -0700300 public IPCriterion(IpPrefix ip, Type type) {
alshabib7b795492014-09-16 14:38:39 -0700301 this.ip = ip;
302 this.type = type;
303 }
304
305 @Override
306 public Type type() {
307 return this.type;
308 }
309
Ayaka Koshibe1d56fe42014-09-19 16:51:58 -0700310 public IpPrefix ip() {
alshabib7b795492014-09-16 14:38:39 -0700311 return this.ip;
312 }
313
alshabib99b8fdc2014-09-25 14:30:22 -0700314 @Override
315 public String toString() {
316 return toStringHelper(type().toString())
317 .add("ip", ip).toString();
318 }
alshabib7b795492014-09-16 14:38:39 -0700319
alshabibba5ac482014-10-02 17:15:20 -0700320 @Override
321 public int hashCode() {
322 return Objects.hash(ip, type);
323 }
324
325 @Override
326 public boolean equals(Object obj) {
327 if (this == obj) {
328 return true;
329 }
330 if (obj instanceof IPCriterion) {
331 IPCriterion that = (IPCriterion) obj;
332 return Objects.equals(ip, that.ip) &&
333 Objects.equals(type, that.type);
334
335
336 }
337 return false;
338 }
339
alshabib7b795492014-09-16 14:38:39 -0700340 }
341
342
343 public static final class IPProtocolCriterion implements Criterion {
344
345 private final Byte proto;
346
347 public IPProtocolCriterion(Byte protocol) {
348 this.proto = protocol;
349 }
350
351 @Override
352 public Type type() {
353 return Type.IP_PROTO;
354 }
355
356 public Byte protocol() {
357 return proto;
358 }
359
alshabib99b8fdc2014-09-25 14:30:22 -0700360 @Override
361 public String toString() {
362 return toStringHelper(type().toString())
363 .add("protocol", Long.toHexString(proto)).toString();
364 }
365
alshabibba5ac482014-10-02 17:15:20 -0700366 @Override
367 public int hashCode() {
alshabib2020b892014-10-20 15:47:08 -0700368 return Objects.hash(proto, type());
alshabibba5ac482014-10-02 17:15:20 -0700369 }
370
371 @Override
372 public boolean equals(Object obj) {
373 if (this == obj) {
374 return true;
375 }
376 if (obj instanceof IPProtocolCriterion) {
377 IPProtocolCriterion that = (IPProtocolCriterion) obj;
378 return Objects.equals(proto, that.proto);
379
380
381 }
382 return false;
383 }
384
alshabib7b795492014-09-16 14:38:39 -0700385 }
386
387
388 public static final class VlanPcpCriterion implements Criterion {
389
390 private final Byte vlanPcp;
391
392 public VlanPcpCriterion(Byte vlanPcp) {
393 this.vlanPcp = vlanPcp;
394 }
395
396 @Override
397 public Type type() {
398 return Type.VLAN_PCP;
399 }
400
alshabib35edb1a2014-09-16 17:44:44 -0700401 public Byte priority() {
alshabib7b795492014-09-16 14:38:39 -0700402 return vlanPcp;
403 }
404
alshabib99b8fdc2014-09-25 14:30:22 -0700405 @Override
406 public String toString() {
407 return toStringHelper(type().toString())
408 .add("pcp", Long.toHexString(vlanPcp)).toString();
409 }
410
alshabibba5ac482014-10-02 17:15:20 -0700411 @Override
412 public int hashCode() {
413 return Objects.hash(vlanPcp);
414 }
415
416 @Override
417 public boolean equals(Object obj) {
418 if (this == obj) {
419 return true;
420 }
421 if (obj instanceof VlanPcpCriterion) {
422 VlanPcpCriterion that = (VlanPcpCriterion) obj;
alshabib2020b892014-10-20 15:47:08 -0700423 return Objects.equals(vlanPcp, that.vlanPcp) &&
424 Objects.equals(this.type(), that.type());
alshabibba5ac482014-10-02 17:15:20 -0700425
426
427 }
428 return false;
429 }
430
alshabib7b795492014-09-16 14:38:39 -0700431 }
432
433
434 public static final class VlanIdCriterion implements Criterion {
435
436
Ayaka Koshibea9c199f2014-09-16 16:21:40 -0700437 private final VlanId vlanId;
alshabib7b795492014-09-16 14:38:39 -0700438
Ayaka Koshibea9c199f2014-09-16 16:21:40 -0700439 public VlanIdCriterion(VlanId vlanId) {
alshabib7b795492014-09-16 14:38:39 -0700440 this.vlanId = vlanId;
441 }
442
443 @Override
444 public Type type() {
445 return Type.VLAN_VID;
446 }
447
Ayaka Koshibea9c199f2014-09-16 16:21:40 -0700448 public VlanId vlanId() {
alshabib7b795492014-09-16 14:38:39 -0700449 return vlanId;
450 }
451
alshabib99b8fdc2014-09-25 14:30:22 -0700452 @Override
453 public String toString() {
454 return toStringHelper(type().toString())
455 .add("id", vlanId).toString();
456 }
457
alshabibba5ac482014-10-02 17:15:20 -0700458 @Override
459 public int hashCode() {
alshabib2020b892014-10-20 15:47:08 -0700460 return Objects.hash(vlanId, type());
alshabibba5ac482014-10-02 17:15:20 -0700461 }
462
463 @Override
464 public boolean equals(Object obj) {
465 if (this == obj) {
466 return true;
467 }
468 if (obj instanceof VlanIdCriterion) {
469 VlanIdCriterion that = (VlanIdCriterion) obj;
alshabib2020b892014-10-20 15:47:08 -0700470 return Objects.equals(vlanId, that.vlanId) &&
471 Objects.equals(this.type(), that.type());
alshabibba5ac482014-10-02 17:15:20 -0700472
473
474 }
475 return false;
476 }
477
alshabib7b795492014-09-16 14:38:39 -0700478 }
479
480
Toshio Koide9c44c9a2014-10-09 11:44:36 -0700481 public static final class TcpPortCriterion implements Criterion {
482
483 private final Short tcpPort;
484 private final Type type;
485
486 public TcpPortCriterion(Short tcpPort, Type type) {
487 this.tcpPort = tcpPort;
488 this.type = type;
489 }
490
491 @Override
492 public Type type() {
493 return this.type;
494 }
495
496 public Short tcpPort() {
497 return this.tcpPort;
498 }
499
500 @Override
501 public String toString() {
502 return toStringHelper(type().toString())
503 .add("tcpPort", tcpPort).toString();
504 }
505
506 @Override
507 public int hashCode() {
508 return Objects.hash(tcpPort, type);
509 }
510
511 @Override
512 public boolean equals(Object obj) {
513 if (this == obj) {
514 return true;
515 }
516 if (obj instanceof TcpPortCriterion) {
517 TcpPortCriterion that = (TcpPortCriterion) obj;
518 return Objects.equals(tcpPort, that.tcpPort) &&
519 Objects.equals(type, that.type);
520
521
522 }
523 return false;
524 }
525 }
tom8bb16062014-09-12 14:47:46 -0700526}