blob: 64527c5077ea5dd1c7ff699f03ef2d338dfaea4b [file] [log] [blame]
Pavlin Radoslavov5363c2a2013-02-18 09:55:42 -08001package net.floodlightcontroller.util;
2
3import net.floodlightcontroller.util.MACAddress;
4import net.floodlightcontroller.util.IPv4Net;
Pavlin Radoslavovad008e02013-02-21 18:42:42 -08005
6import org.codehaus.jackson.annotate.JsonProperty;
Pavlin Radoslavov5363c2a2013-02-18 09:55:42 -08007
8/**
9 * The class representing the Flow Entry Matching filter.
10 *
11 * The Flow Entry matching filter that is used to specify
12 * the network data that would be forwarded on the data path from
Pavlin Radoslavovede97582013-03-08 18:57:28 -080013 * the source to the destination. Examples: source or destination MAC address,
14 * IP prefix that includes the destination's IP address, etc.
Pavlin Radoslavov5363c2a2013-02-18 09:55:42 -080015 */
16public class FlowEntryMatch {
Pavlin Radoslavovede97582013-03-08 18:57:28 -080017 /**
18 * A class for storing a value to match.
19 */
20 class Field<T> {
21 /**
22 * Default constructor.
23 */
24 public Field() {
25 this.enabled = false;
26 }
27
28 /**
29 * Constructor for a given value to match.
30 */
31 public Field(T value) {
32 this.value = value;
33 this.enabled = true;
34 }
35
36 /**
37 * Get the value.
38 *
39 * @return the value.
40 */
41 public T value() { return this.value; }
42
43 /**
44 * Enable the matching for a given value.
45 *
46 * @param value the value to set.
47 */
48 public void enableMatch(T value) {
49 this.value = value;
50 this.enabled = true;
51 }
52
53 /**
54 * Disable the matching.
55 */
56 public void disableMatch() {
57 this.enabled = false;
58 }
59
60 /**
61 * Test whether matching is enabled.
62 *
63 * @return true if matching is enabled, otherwise false.
64 */
65 public boolean enabled() { return this.enabled; }
66
67 private T value; // The value to match
68 private boolean enabled; // Set to true, if matching is enabled
69 }
70
71 private Field<Port> inPort; // Matching input switch port
72 private Field<MACAddress> srcMac; // Matching source MAC address
73 private Field<MACAddress> dstMac; // Matching destination MAC address
74 private Field<Short> vlanId; // Matching VLAN ID
75 private Field<Byte> vlanPriority; // Matching VLAN priority
76 private Field<Short> ethernetFrameType; // Matching Ethernet frame type
77 private Field<Byte> ipToS; // Matching IP ToS (DSCP field, 6 bits)
78 private Field<Byte> ipProto; // Matching IP protocol
79 private Field<IPv4Net> srcIPv4Net; // Matching source IPv4 prefix
80 private Field<IPv4Net> dstIPv4Net; // Matching destination IPv4 prefix
81 private Field<Short> srcTcpUdpPort; // Matching source TCP/UDP port
82 private Field<Short> dstTcpUdpPort; // Matching destination TCP/UDP port
Pavlin Radoslavov5363c2a2013-02-18 09:55:42 -080083
84 /**
85 * Default constructor.
86 */
87 public FlowEntryMatch() {
88 }
89
90 /**
Pavlin Radoslavovede97582013-03-08 18:57:28 -080091 * Get the matching input switch port.
92 *
93 * @return the matching input switch port.
94 */
95 @JsonProperty("inPort")
96 public Port inPort() {
97 if (inPort != null)
98 return inPort.value();
99 return null;
100 }
101
102 /**
103 * Enable the matching on input switch port.
104 *
105 * @param inPort the input switch port value to enable for matching.
106 */
107 @JsonProperty("inPort")
108 public void enableInPort(Port inPort) {
109 this.inPort = new Field<Port>(inPort);
110 }
111
112 /**
113 * Disable the matching on input switch port.
114 */
115 public void disableInPort() {
116 this.inPort = null;
117 }
118
119 /**
120 * Test if matching on input switch port is enabled.
121 *
122 * @return true if matching on input switch port is enabled.
123 */
124 @JsonProperty("matchInPort")
125 public boolean matchInPort() {
126 if (inPort != null)
127 return inPort.enabled();
128 return false;
129 }
130
131 /**
Pavlin Radoslavov5363c2a2013-02-18 09:55:42 -0800132 * Get the matching source MAC address.
133 *
134 * @return the matching source MAC address.
135 */
Pavlin Radoslavov2013cbb2013-02-26 10:15:18 -0800136 @JsonProperty("srcMac")
Pavlin Radoslavovede97582013-03-08 18:57:28 -0800137 public MACAddress srcMac() {
138 if (srcMac != null)
139 return srcMac.value();
140 return null;
141 }
Pavlin Radoslavov5363c2a2013-02-18 09:55:42 -0800142
143 /**
Pavlin Radoslavovede97582013-03-08 18:57:28 -0800144 * Enable the matching on source MAC address.
Pavlin Radoslavov5363c2a2013-02-18 09:55:42 -0800145 *
Pavlin Radoslavovede97582013-03-08 18:57:28 -0800146 * @param srcMac the source MAC address value to enable for matching.
Pavlin Radoslavov5363c2a2013-02-18 09:55:42 -0800147 */
Pavlin Radoslavov2013cbb2013-02-26 10:15:18 -0800148 @JsonProperty("srcMac")
Pavlin Radoslavovede97582013-03-08 18:57:28 -0800149 public void enableSrcMac(MACAddress srcMac) {
150 this.srcMac = new Field<MACAddress>(srcMac);
151 }
152
153 /**
154 * Disable the matching on source MAC address.
155 */
156 public void disableSrcMac() {
157 this.srcMac = null;
158 }
159
160 /**
161 * Test if matching on source MAC address is enabled.
162 *
163 * @return true if matching on source MAC address is enabled.
164 */
165 @JsonProperty("matchSrcMac")
166 public boolean matchSrcMac() {
167 if (srcMac != null)
168 return srcMac.enabled();
169 return false;
Pavlin Radoslavov5363c2a2013-02-18 09:55:42 -0800170 }
171
172 /**
173 * Get the matching destination MAC address.
174 *
175 * @return the matching destination MAC address.
176 */
Pavlin Radoslavov2013cbb2013-02-26 10:15:18 -0800177 @JsonProperty("dstMac")
Pavlin Radoslavovede97582013-03-08 18:57:28 -0800178 public MACAddress dstMac() {
179 if (dstMac != null)
180 return dstMac.value();
181 return null;
182 }
Pavlin Radoslavov5363c2a2013-02-18 09:55:42 -0800183
184 /**
Pavlin Radoslavovede97582013-03-08 18:57:28 -0800185 * Enable the matching on destination MAC address.
Pavlin Radoslavov5363c2a2013-02-18 09:55:42 -0800186 *
Pavlin Radoslavovede97582013-03-08 18:57:28 -0800187 * @param dstMac the destination MAC address value to enable for matching.
Pavlin Radoslavov5363c2a2013-02-18 09:55:42 -0800188 */
Pavlin Radoslavov2013cbb2013-02-26 10:15:18 -0800189 @JsonProperty("dstMac")
Pavlin Radoslavovede97582013-03-08 18:57:28 -0800190 public void enableDstMac(MACAddress dstMac) {
191 this.dstMac = new Field<MACAddress>(dstMac);
192 }
193
194 /**
195 * Disable the matching on destination MAC address.
196 */
197 public void disableDstMac() {
198 this.dstMac = null;
199 }
200
201 /**
202 * Test if matching on destination MAC address is enabled.
203 *
204 * @return true if matching on destination MAC address is enabled.
205 */
206 @JsonProperty("matchDstMac")
207 public boolean matchDstMac() {
208 if (dstMac != null)
209 return dstMac.enabled();
210 return false;
211 }
212
213 /**
214 * Get the matching VLAN ID.
215 *
216 * @return the matching VLAN ID.
217 */
218 @JsonProperty("vlanId")
219 public Short vlanId() {
220 if (vlanId != null)
221 return vlanId.value();
222 return null;
223 }
224
225 /**
226 * Enable the matching on VLAN ID.
227 *
228 * @param vlanId the VLAN ID value to enable for matching.
229 */
230 @JsonProperty("vlanId")
231 public void enableVlanId(Short vlanId) {
232 this.vlanId = new Field<Short>(vlanId);
233 }
234
235 /**
236 * Disable the matching on VLAN ID.
237 */
238 public void disableVlanId() {
239 this.vlanId = null;
240 }
241
242 /**
243 * Test if matching on VLAN ID is enabled.
244 *
245 * @return true if matching on VLAN ID is enabled.
246 */
247 @JsonProperty("matchVlanId")
248 public boolean matchVlanId() {
249 if (vlanId != null)
250 return vlanId.enabled();
251 return false;
252 }
253
254 /**
255 * Get the matching VLAN priority.
256 *
257 * @return the matching VLAN priority.
258 */
259 @JsonProperty("vlanPriority")
260 public Byte vlanPriority() {
261 if (vlanPriority != null)
262 return vlanPriority.value();
263 return null;
264 }
265
266 /**
267 * Enable the matching on VLAN priority.
268 *
269 * @param vlanPriority the VLAN priority value to enable for matching.
270 */
271 @JsonProperty("vlanPriority")
272 public void enableVlanPriority(Byte vlanPriority) {
273 this.vlanPriority = new Field<Byte>(vlanPriority);
274 }
275
276 /**
277 * Disable the matching on VLAN priority.
278 */
279 public void disableVlanPriority() {
280 this.vlanPriority = null;
281 }
282
283 /**
284 * Test if matching on VLAN priority is enabled.
285 *
286 * @return true if matching on VLAN priority is enabled.
287 */
288 @JsonProperty("matchVlanPriority")
289 public boolean matchVlanPriority() {
290 if (vlanPriority != null)
291 return vlanPriority.enabled();
292 return false;
293 }
294
295 /**
296 * Get the matching Ethernet frame type.
297 *
298 * @return the matching Ethernet frame type.
299 */
300 @JsonProperty("ethernetFrameType")
301 public Short ethernetFrameType() {
302 if (ethernetFrameType != null)
303 return ethernetFrameType.value();
304 return null;
305 }
306
307 /**
308 * Enable the matching on Ethernet frame type.
309 *
310 * @param ethernetFrameType the Ethernet frame type value to enable for
311 * matching.
312 */
313 @JsonProperty("ethernetFrameType")
314 public void enableEthernetFrameType(Short ethernetFrameType) {
315 this.ethernetFrameType = new Field<Short>(ethernetFrameType);
316 }
317
318 /**
319 * Disable the matching on Ethernet frame type.
320 */
321 public void disableEthernetFrameType() {
322 this.ethernetFrameType = null;
323 }
324
325 /**
326 * Test if matching on Ethernet frame type is enabled.
327 *
328 * @return true if matching on Ethernet frame type is enabled.
329 */
330 @JsonProperty("matchEthernetFrameType")
331 public boolean matchEthernetFrameType() {
332 if (ethernetFrameType != null)
333 return ethernetFrameType.enabled();
334 return false;
335 }
336
337 /**
338 * Get the matching IP ToS (DSCP field, 6 bits)
339 *
340 * @return the matching IP ToS.
341 */
342 @JsonProperty("ipToS")
343 public Byte ipToS() {
344 if (ipToS != null)
345 return ipToS.value();
346 return null;
347 }
348
349 /**
350 * Enable the matching on IP ToS (DSCP field, 6 bits).
351 *
352 * @param ipToS the IP ToS value to enable for matching.
353 */
354 @JsonProperty("ipToS")
355 public void enableIpToS(Byte ipToS) {
356 this.ipToS = new Field<Byte>(ipToS);
357 }
358
359 /**
360 * Disable the matching on IP ToS (DSCP field, 6 bits).
361 */
362 public void disableIpToS() {
363 this.ipToS = null;
364 }
365
366 /**
367 * Test if matching on IP ToS (DSCP field, 6 bits) is enabled.
368 *
369 * @return true if matching on IP ToS is enabled.
370 */
371 @JsonProperty("matchIpToS")
372 public boolean matchIpToS() {
373 if (ipToS != null)
374 return ipToS.enabled();
375 return false;
376 }
377
378 /**
379 * Get the matching IP protocol.
380 *
381 * @return the matching IP protocol.
382 */
383 @JsonProperty("ipProto")
384 public Byte ipProto() {
385 if (ipProto != null)
386 return ipProto.value();
387 return null;
388 }
389
390 /**
391 * Enable the matching on IP protocol.
392 *
393 * @param ipProto the IP protocol value to enable for matching.
394 */
395 @JsonProperty("ipProto")
396 public void enableIpProto(Byte ipProto) {
397 this.ipProto = new Field<Byte>(ipProto);
398 }
399
400 /**
401 * Disable the matching on IP protocol.
402 */
403 public void disableIpProto() {
404 this.ipProto = null;
405 }
406
407 /**
408 * Test if matching on IP protocol is enabled.
409 *
410 * @return true if matching on IP protocol is enabled.
411 */
412 @JsonProperty("matchIpProto")
413 public boolean matchIpProto() {
414 if (ipProto != null)
415 return ipProto.enabled();
416 return false;
Pavlin Radoslavov5363c2a2013-02-18 09:55:42 -0800417 }
418
419 /**
420 * Get the matching source IPv4 prefix.
421 *
422 * @return the matching source IPv4 prefix.
423 */
Pavlin Radoslavov2013cbb2013-02-26 10:15:18 -0800424 @JsonProperty("srcIPv4Net")
Pavlin Radoslavovede97582013-03-08 18:57:28 -0800425 public IPv4Net srcIPv4Net() {
426 if (srcIPv4Net != null)
427 return srcIPv4Net.value();
428 return null;
429 }
Pavlin Radoslavov5363c2a2013-02-18 09:55:42 -0800430
431 /**
Pavlin Radoslavovede97582013-03-08 18:57:28 -0800432 * Enable the matching on source IPv4 prefix.
Pavlin Radoslavov5363c2a2013-02-18 09:55:42 -0800433 *
Pavlin Radoslavovede97582013-03-08 18:57:28 -0800434 * @param srcIPv4Net the source IPv4 prefix value to enable for matching.
Pavlin Radoslavov5363c2a2013-02-18 09:55:42 -0800435 */
Pavlin Radoslavov2013cbb2013-02-26 10:15:18 -0800436 @JsonProperty("srcIPv4Net")
Pavlin Radoslavovede97582013-03-08 18:57:28 -0800437 public void enableSrcIPv4Net(IPv4Net srcIPv4Net) {
438 this.srcIPv4Net = new Field<IPv4Net>(srcIPv4Net);
439 }
440
441 /**
442 * Disable the matching on source IPv4 prefix.
443 */
444 public void disableSrcIPv4Net() {
445 this.srcIPv4Net = null;
446 }
447
448 /**
449 * Test if matching on source IPv4 prefix is enabled.
450 *
451 * @return true if matching on source IPv4 prefix is enabled.
452 */
453 @JsonProperty("matchSrcIPv4Net")
454 public boolean matchSrcIPv4Net() {
455 if (srcIPv4Net != null)
456 return srcIPv4Net.enabled();
457 return false;
Pavlin Radoslavov5363c2a2013-02-18 09:55:42 -0800458 }
459
460 /**
461 * Get the matching destination IPv4 prefix.
462 *
463 * @return the matching destination IPv4 prefix.
464 */
Pavlin Radoslavov2013cbb2013-02-26 10:15:18 -0800465 @JsonProperty("dstIPv4Net")
Pavlin Radoslavovede97582013-03-08 18:57:28 -0800466 public IPv4Net dstIPv4Net() {
467 if (dstIPv4Net != null)
468 return dstIPv4Net.value();
469 return null;
470 }
Pavlin Radoslavov5363c2a2013-02-18 09:55:42 -0800471
472 /**
Pavlin Radoslavovede97582013-03-08 18:57:28 -0800473 * Enable the matching on destination IPv4 prefix.
Pavlin Radoslavov5363c2a2013-02-18 09:55:42 -0800474 *
Pavlin Radoslavovede97582013-03-08 18:57:28 -0800475 * @param dstIPv4Net the destination IPv4 prefix value to enable for
476 * matching.
Pavlin Radoslavov5363c2a2013-02-18 09:55:42 -0800477 */
Pavlin Radoslavov2013cbb2013-02-26 10:15:18 -0800478 @JsonProperty("dstIPv4Net")
Pavlin Radoslavovede97582013-03-08 18:57:28 -0800479 public void enableDstIPv4Net(IPv4Net dstIPv4Net) {
480 this.dstIPv4Net = new Field<IPv4Net>(dstIPv4Net);
481 }
482
483 /**
484 * Disable the matching on destination IPv4 prefix.
485 */
486 public void disableDstIPv4Net() {
487 this.dstIPv4Net = null;
488 }
489
490 /**
491 * Test if matching on destination IPv4 prefix is enabled.
492 *
493 * @return true if matching on destination IPv4 prefix is enabled.
494 */
495 @JsonProperty("matchDstIPv4Net")
496 public boolean matchDstIPv4Net() {
497 if (dstIPv4Net != null)
498 return dstIPv4Net.enabled();
499 return false;
500 }
501
502 /**
503 * Get the matching source TCP/UDP port.
504 *
505 * @return the matching source TCP/UDP port.
506 */
507 @JsonProperty("srcTcpUdpPort")
508 public Short srcTcpUdpPort() {
509 if (srcTcpUdpPort != null)
510 return srcTcpUdpPort.value();
511 return null;
512 }
513
514 /**
515 * Enable the matching on source TCP/UDP port.
516 *
517 * @param srcTcpUdpPort the source TCP/UDP port to enable for matching.
518 */
519 @JsonProperty("srcTcpUdpPort")
520 public void enableSrcTcpUdpPort(Short srcTcpUdpPort) {
521 this.srcTcpUdpPort = new Field<Short>(srcTcpUdpPort);
522 }
523
524 /**
525 * Disable the matching on source TCP/UDP port.
526 */
527 public void disableSrcTcpUdpPort() {
528 this.srcTcpUdpPort = null;
529 }
530
531 /**
532 * Test if matching on source TCP/UDP port is enabled.
533 *
534 * @return true if matching on source TCP/UDP port is enabled.
535 */
536 @JsonProperty("matchSrcTcpUdpPort")
537 public boolean matchSrcTcpUdpPort() {
538 if (srcTcpUdpPort != null)
539 return srcTcpUdpPort.enabled();
540 return false;
541 }
542
543 /**
544 * Get the matching destination TCP/UDP port.
545 *
546 * @return the matching destination TCP/UDP port.
547 */
548 @JsonProperty("dstTcpUdpPort")
549 public Short dstTcpUdpPort() {
550 if (dstTcpUdpPort != null)
551 return dstTcpUdpPort.value();
552 return null;
553 }
554
555 /**
556 * Enable the matching on destination TCP/UDP port.
557 *
558 * @param dstTcpUdpPort the destination TCP/UDP port to enable for
559 * matching.
560 */
561 @JsonProperty("dstTcpUdpPort")
562 public void enableDstTcpUdpPort(Short dstTcpUdpPort) {
563 this.dstTcpUdpPort = new Field<Short>(dstTcpUdpPort);
564 }
565
566 /**
567 * Disable the matching on destination TCP/UDP port.
568 */
569 public void disableDstTcpUdpPort() {
570 this.dstTcpUdpPort = null;
571 }
572
573 /**
574 * Test if matching on destination TCP/UDP port is enabled.
575 *
576 * @return true if matching on destination TCP/UDP port is enabled.
577 */
578 @JsonProperty("matchDstTcpUdpPort")
579 public boolean matchDstTcpUdpPort() {
580 if (dstTcpUdpPort != null)
581 return dstTcpUdpPort.enabled();
582 return false;
Pavlin Radoslavov5363c2a2013-02-18 09:55:42 -0800583 }
584
585 /**
586 * Convert the matching filter to a string.
587 *
Pavlin Radoslavovad008e02013-02-21 18:42:42 -0800588 * The string has the following form:
Pavlin Radoslavova10a9a82013-02-22 11:47:54 -0800589 * [srcMac=XXX dstMac=XXX srcIPv4Net=XXX dstIPv4Net=XXX]
Pavlin Radoslavovad008e02013-02-21 18:42:42 -0800590 *
Pavlin Radoslavov5363c2a2013-02-18 09:55:42 -0800591 * @return the matching filter as a string.
592 */
593 @Override
594 public String toString() {
Pavlin Radoslavovede97582013-03-08 18:57:28 -0800595 String ret = "[";
596 boolean addSpace = false;
597
598 //
599 // Conditionally add only those matching fields that are enabled
600 //
601 if (matchInPort()) {
602 if (addSpace)
603 ret += " ";
604 addSpace = true;
605 ret += "inPort=" + this.inPort().toString();
606 }
607 if (matchSrcMac()) {
608 if (addSpace)
609 ret += " ";
610 addSpace = true;
611 ret += "srcMac=" + this.srcMac().toString();
612 }
613 if (matchDstMac()) {
614 if (addSpace)
615 ret += " ";
616 addSpace = true;
617 ret += "dstMac=" + this.dstMac().toString();
618 }
619 if (matchVlanId()) {
620 if (addSpace)
621 ret += " ";
622 addSpace = true;
623 ret += "vlanId=" + this.vlanId().toString();
624 }
625 if (matchVlanPriority()) {
626 if (addSpace)
627 ret += " ";
628 addSpace = true;
629 ret += "vlanPriority=" + this.vlanPriority().toString();
630 }
631 if (matchEthernetFrameType()) {
632 if (addSpace)
633 ret += " ";
634 addSpace = true;
635 ret += "ethernetFrameType=" + this.ethernetFrameType().toString();
636 }
637 if (matchIpToS()) {
638 if (addSpace)
639 ret += " ";
640 addSpace = true;
641 ret += "ipToS=" + this.ipToS().toString();
642 }
643 if (matchIpProto()) {
644 if (addSpace)
645 ret += " ";
646 addSpace = true;
647 ret += "ipProto=" + this.ipProto().toString();
648 }
649 if (matchSrcIPv4Net()) {
650 if (addSpace)
651 ret += " ";
652 addSpace = true;
653 ret += "srcIPv4Net=" + this.srcIPv4Net().toString();
654 }
655 if (matchDstIPv4Net()) {
656 if (addSpace)
657 ret += " ";
658 addSpace = true;
659 ret += "dstIPv4Net=" + this.dstIPv4Net().toString();
660 }
661 if (matchSrcTcpUdpPort()) {
662 if (addSpace)
663 ret += " ";
664 addSpace = true;
665 ret += "srcTcpUdpPort=" + this.srcTcpUdpPort().toString();
666 }
667 if (matchDstTcpUdpPort()) {
668 if (addSpace)
669 ret += " ";
670 addSpace = true;
671 ret += "dstTcpUdpPort=" + this.dstTcpUdpPort().toString();
672 }
673
Pavlin Radoslavovad008e02013-02-21 18:42:42 -0800674 ret += "]";
Pavlin Radoslavovede97582013-03-08 18:57:28 -0800675
Pavlin Radoslavov5363c2a2013-02-18 09:55:42 -0800676 return ret;
677 }
678}