blob: 8bd09608e56cda62bf3573754aa26354737daa60 [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() {
164 return Objects.hash(port);
165 }
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;
174 return Objects.equals(port, that.port);
175
176 }
177 return false;
178 }
179
alshabib7b795492014-09-16 14:38:39 -0700180 }
181
182
183 public static final class EthCriterion implements Criterion {
Ayaka Koshibea9c199f2014-09-16 16:21:40 -0700184 private final MacAddress mac;
alshabib7b795492014-09-16 14:38:39 -0700185 private final Type type;
186
Ayaka Koshibea9c199f2014-09-16 16:21:40 -0700187 public EthCriterion(MacAddress mac, Type type) {
alshabib7b795492014-09-16 14:38:39 -0700188 this.mac = mac;
189 this.type = type;
190 }
191
192 @Override
193 public Type type() {
194 return this.type;
195 }
196
Ayaka Koshibea9c199f2014-09-16 16:21:40 -0700197 public MacAddress mac() {
alshabib7b795492014-09-16 14:38:39 -0700198 return this.mac;
199 }
alshabib99b8fdc2014-09-25 14:30:22 -0700200
201 @Override
202 public String toString() {
203 return toStringHelper(type().toString())
204 .add("mac", mac).toString();
205 }
206
alshabibba5ac482014-10-02 17:15:20 -0700207 @Override
208 public int hashCode() {
209 return Objects.hash(mac, type);
210 }
211
212 @Override
213 public boolean equals(Object obj) {
214 if (this == obj) {
215 return true;
216 }
217 if (obj instanceof EthCriterion) {
218 EthCriterion that = (EthCriterion) obj;
219 return Objects.equals(mac, that.mac) &&
220 Objects.equals(type, that.type);
221
222
223 }
224 return false;
225 }
226
227
alshabib7b795492014-09-16 14:38:39 -0700228 }
229
230 public static final class EthTypeCriterion implements Criterion {
231
232 private final Short ethType;
233
234 public EthTypeCriterion(Short ethType) {
235 this.ethType = ethType;
236 }
237
238 @Override
239 public Type type() {
240 return Type.ETH_TYPE;
241 }
242
243 public Short ethType() {
244 return ethType;
245 }
246
alshabib99b8fdc2014-09-25 14:30:22 -0700247 @Override
248 public String toString() {
249 return toStringHelper(type().toString())
250 .add("ethType", Long.toHexString(ethType)).toString();
251 }
252
alshabibba5ac482014-10-02 17:15:20 -0700253 @Override
254 public int hashCode() {
255 return Objects.hash(ethType);
256 }
257
258 @Override
259 public boolean equals(Object obj) {
260 if (this == obj) {
261 return true;
262 }
263 if (obj instanceof EthTypeCriterion) {
264 EthTypeCriterion that = (EthTypeCriterion) obj;
265 return Objects.equals(ethType, that.ethType);
266
267
268 }
269 return false;
270 }
271
alshabib7b795492014-09-16 14:38:39 -0700272 }
273
274
275 public static final class IPCriterion implements Criterion {
276
Ayaka Koshibe1d56fe42014-09-19 16:51:58 -0700277 private final IpPrefix ip;
alshabib7b795492014-09-16 14:38:39 -0700278 private final Type type;
279
Ayaka Koshibe1d56fe42014-09-19 16:51:58 -0700280 public IPCriterion(IpPrefix ip, Type type) {
alshabib7b795492014-09-16 14:38:39 -0700281 this.ip = ip;
282 this.type = type;
283 }
284
285 @Override
286 public Type type() {
287 return this.type;
288 }
289
Ayaka Koshibe1d56fe42014-09-19 16:51:58 -0700290 public IpPrefix ip() {
alshabib7b795492014-09-16 14:38:39 -0700291 return this.ip;
292 }
293
alshabib99b8fdc2014-09-25 14:30:22 -0700294 @Override
295 public String toString() {
296 return toStringHelper(type().toString())
297 .add("ip", ip).toString();
298 }
alshabib7b795492014-09-16 14:38:39 -0700299
alshabibba5ac482014-10-02 17:15:20 -0700300 @Override
301 public int hashCode() {
302 return Objects.hash(ip, type);
303 }
304
305 @Override
306 public boolean equals(Object obj) {
307 if (this == obj) {
308 return true;
309 }
310 if (obj instanceof IPCriterion) {
311 IPCriterion that = (IPCriterion) obj;
312 return Objects.equals(ip, that.ip) &&
313 Objects.equals(type, that.type);
314
315
316 }
317 return false;
318 }
319
alshabib7b795492014-09-16 14:38:39 -0700320 }
321
322
323 public static final class IPProtocolCriterion implements Criterion {
324
325 private final Byte proto;
326
327 public IPProtocolCriterion(Byte protocol) {
328 this.proto = protocol;
329 }
330
331 @Override
332 public Type type() {
333 return Type.IP_PROTO;
334 }
335
336 public Byte protocol() {
337 return proto;
338 }
339
alshabib99b8fdc2014-09-25 14:30:22 -0700340 @Override
341 public String toString() {
342 return toStringHelper(type().toString())
343 .add("protocol", Long.toHexString(proto)).toString();
344 }
345
alshabibba5ac482014-10-02 17:15:20 -0700346 @Override
347 public int hashCode() {
348 return Objects.hash(proto);
349 }
350
351 @Override
352 public boolean equals(Object obj) {
353 if (this == obj) {
354 return true;
355 }
356 if (obj instanceof IPProtocolCriterion) {
357 IPProtocolCriterion that = (IPProtocolCriterion) obj;
358 return Objects.equals(proto, that.proto);
359
360
361 }
362 return false;
363 }
364
alshabib7b795492014-09-16 14:38:39 -0700365 }
366
367
368 public static final class VlanPcpCriterion implements Criterion {
369
370 private final Byte vlanPcp;
371
372 public VlanPcpCriterion(Byte vlanPcp) {
373 this.vlanPcp = vlanPcp;
374 }
375
376 @Override
377 public Type type() {
378 return Type.VLAN_PCP;
379 }
380
alshabib35edb1a2014-09-16 17:44:44 -0700381 public Byte priority() {
alshabib7b795492014-09-16 14:38:39 -0700382 return vlanPcp;
383 }
384
alshabib99b8fdc2014-09-25 14:30:22 -0700385 @Override
386 public String toString() {
387 return toStringHelper(type().toString())
388 .add("pcp", Long.toHexString(vlanPcp)).toString();
389 }
390
alshabibba5ac482014-10-02 17:15:20 -0700391 @Override
392 public int hashCode() {
393 return Objects.hash(vlanPcp);
394 }
395
396 @Override
397 public boolean equals(Object obj) {
398 if (this == obj) {
399 return true;
400 }
401 if (obj instanceof VlanPcpCriterion) {
402 VlanPcpCriterion that = (VlanPcpCriterion) obj;
403 return Objects.equals(vlanPcp, that.vlanPcp);
404
405
406 }
407 return false;
408 }
409
alshabib7b795492014-09-16 14:38:39 -0700410 }
411
412
413 public static final class VlanIdCriterion implements Criterion {
414
415
Ayaka Koshibea9c199f2014-09-16 16:21:40 -0700416 private final VlanId vlanId;
alshabib7b795492014-09-16 14:38:39 -0700417
Ayaka Koshibea9c199f2014-09-16 16:21:40 -0700418 public VlanIdCriterion(VlanId vlanId) {
alshabib7b795492014-09-16 14:38:39 -0700419 this.vlanId = vlanId;
420 }
421
422 @Override
423 public Type type() {
424 return Type.VLAN_VID;
425 }
426
Ayaka Koshibea9c199f2014-09-16 16:21:40 -0700427 public VlanId vlanId() {
alshabib7b795492014-09-16 14:38:39 -0700428 return vlanId;
429 }
430
alshabib99b8fdc2014-09-25 14:30:22 -0700431 @Override
432 public String toString() {
433 return toStringHelper(type().toString())
434 .add("id", vlanId).toString();
435 }
436
alshabibba5ac482014-10-02 17:15:20 -0700437 @Override
438 public int hashCode() {
439 return Objects.hash(vlanId);
440 }
441
442 @Override
443 public boolean equals(Object obj) {
444 if (this == obj) {
445 return true;
446 }
447 if (obj instanceof VlanIdCriterion) {
448 VlanIdCriterion that = (VlanIdCriterion) obj;
449 return Objects.equals(vlanId, that.vlanId);
450
451
452 }
453 return false;
454 }
455
alshabib7b795492014-09-16 14:38:39 -0700456 }
457
458
Toshio Koide9c44c9a2014-10-09 11:44:36 -0700459 public static final class TcpPortCriterion implements Criterion {
460
461 private final Short tcpPort;
462 private final Type type;
463
464 public TcpPortCriterion(Short tcpPort, Type type) {
465 this.tcpPort = tcpPort;
466 this.type = type;
467 }
468
469 @Override
470 public Type type() {
471 return this.type;
472 }
473
474 public Short tcpPort() {
475 return this.tcpPort;
476 }
477
478 @Override
479 public String toString() {
480 return toStringHelper(type().toString())
481 .add("tcpPort", tcpPort).toString();
482 }
483
484 @Override
485 public int hashCode() {
486 return Objects.hash(tcpPort, type);
487 }
488
489 @Override
490 public boolean equals(Object obj) {
491 if (this == obj) {
492 return true;
493 }
494 if (obj instanceof TcpPortCriterion) {
495 TcpPortCriterion that = (TcpPortCriterion) obj;
496 return Objects.equals(tcpPort, that.tcpPort) &&
497 Objects.equals(type, that.type);
498
499
500 }
501 return false;
502 }
503 }
tom8bb16062014-09-12 14:47:46 -0700504}