Toshio Koide

Add TCP src/dst port traffic selection criteria to TrafficSelector

...@@ -140,6 +140,16 @@ public final class DefaultTrafficSelector implements TrafficSelector { ...@@ -140,6 +140,16 @@ public final class DefaultTrafficSelector implements TrafficSelector {
140 } 140 }
141 141
142 @Override 142 @Override
143 + public Builder matchTcpSrc(Short tcpPort) {
144 + return add(Criteria.matchTcpSrc(tcpPort));
145 + }
146 +
147 + @Override
148 + public Builder matchTcpDst(Short tcpPort) {
149 + return add(Criteria.matchTcpDst(tcpPort));
150 + }
151 +
152 + @Override
143 public TrafficSelector build() { 153 public TrafficSelector build() {
144 return new DefaultTrafficSelector(ImmutableSet.copyOf(selector.values())); 154 return new DefaultTrafficSelector(ImmutableSet.copyOf(selector.values()));
145 } 155 }
......
...@@ -98,6 +98,20 @@ public interface TrafficSelector { ...@@ -98,6 +98,20 @@ public interface TrafficSelector {
98 public Builder matchIPDst(IpPrefix ip); 98 public Builder matchIPDst(IpPrefix ip);
99 99
100 /** 100 /**
101 + * Matches a TCP source port number.
102 + * @param tcpPort a TCP source port number
103 + * @return a selection builder
104 + */
105 + public Builder matchTcpSrc(Short tcpPort);
106 +
107 + /**
108 + * Matches a TCP destination port number.
109 + * @param tcpPort a TCP destination port number
110 + * @return a selection builder
111 + */
112 + public Builder matchTcpDst(Short tcpPort);
113 +
114 + /**
101 * Builds an immutable traffic selector. 115 * Builds an immutable traffic selector.
102 * 116 *
103 * @return traffic selector 117 * @return traffic selector
......
...@@ -113,6 +113,25 @@ public final class Criteria { ...@@ -113,6 +113,25 @@ public final class Criteria {
113 return new IPCriterion(ip, Type.IPV4_DST); 113 return new IPCriterion(ip, Type.IPV4_DST);
114 } 114 }
115 115
116 + /**
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 + }
116 135
117 /* 136 /*
118 * Implementations of criteria. 137 * Implementations of criteria.
...@@ -437,4 +456,49 @@ public final class Criteria { ...@@ -437,4 +456,49 @@ public final class Criteria {
437 } 456 }
438 457
439 458
459 + 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 + }
440 } 504 }
......