blob: 758c51c4d5648c822d75bc9cc89a773c13e9aada [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
alshabib7b795492014-09-16 14:38:39 -07005import org.onlab.onos.net.PortNumber;
6import org.onlab.onos.net.flow.criteria.Criterion.Type;
Ayaka Koshibe1d56fe42014-09-19 16:51:58 -07007import org.onlab.packet.IpPrefix;
Ayaka Koshibea9c199f2014-09-16 16:21:40 -07008import org.onlab.packet.MacAddress;
9import org.onlab.packet.VlanId;
alshabib7b795492014-09-16 14:38:39 -070010
tom8bb16062014-09-12 14:47:46 -070011/**
12 * Factory class to create various traffic selection criteria.
13 */
14public final class Criteria {
15
alshabib7b795492014-09-16 14:38:39 -070016 //TODO: incomplete type implementation. Need to implement complete list from Criterion
17
tom8bb16062014-09-12 14:47:46 -070018 // Ban construction
19 private Criteria() {
20 }
21
22 /**
alshabib7b795492014-09-16 14:38:39 -070023 * Creates a match on IN_PORT field using the specified value.
24 *
25 * @param port inport value
26 * @return match criterion
27 */
28 public static Criterion matchInPort(PortNumber port) {
29 return new PortCriterion(port);
30 }
31
32 /**
tom8bb16062014-09-12 14:47:46 -070033 * Creates a match on ETH_SRC field using the specified value. This value
34 * may be a wildcard mask.
35 *
tomc104d282014-09-19 10:57:55 -070036 * @param mac MAC address value or wildcard mask
tom8bb16062014-09-12 14:47:46 -070037 * @return match criterion
38 */
Ayaka Koshibea9c199f2014-09-16 16:21:40 -070039 public static Criterion matchEthSrc(MacAddress mac) {
alshabib7b795492014-09-16 14:38:39 -070040 return new EthCriterion(mac, Type.ETH_SRC);
tom8bb16062014-09-12 14:47:46 -070041 }
42
alshabib369d2942014-09-12 17:59:35 -070043 /**
44 * Creates a match on ETH_DST field using the specified value. This value
45 * may be a wildcard mask.
46 *
tomc104d282014-09-19 10:57:55 -070047 * @param mac MAC address value or wildcard mask
alshabib369d2942014-09-12 17:59:35 -070048 * @return match criterion
49 */
Ayaka Koshibea9c199f2014-09-16 16:21:40 -070050 public static Criterion matchEthDst(MacAddress mac) {
alshabib7b795492014-09-16 14:38:39 -070051 return new EthCriterion(mac, Type.ETH_DST);
52 }
53
54 /**
55 * Creates a match on ETH_TYPE field using the specified value.
56 *
57 * @param ethType eth type value
58 * @return match criterion
59 */
60 public static Criterion matchEthType(Short ethType) {
61 return new EthTypeCriterion(ethType);
62 }
63
64 /**
65 * Creates a match on VLAN ID field using the specified value.
66 *
67 * @param vlanId vlan id value
68 * @return match criterion
69 */
Ayaka Koshibea9c199f2014-09-16 16:21:40 -070070 public static Criterion matchVlanId(VlanId vlanId) {
alshabib7b795492014-09-16 14:38:39 -070071 return new VlanIdCriterion(vlanId);
72 }
73
74 /**
75 * Creates a match on VLAN PCP field using the specified value.
76 *
77 * @param vlanPcp vlan pcp value
78 * @return match criterion
79 */
alshabibb45d1962014-09-18 14:25:45 -070080 public static Criterion matchVlanPcp(Byte vlanPcp) {
alshabib7b795492014-09-16 14:38:39 -070081 return new VlanPcpCriterion(vlanPcp);
82 }
83
84 /**
85 * Creates a match on IP proto field using the specified value.
86 *
87 * @param proto ip protocol value
88 * @return match criterion
89 */
90 public static Criterion matchIPProtocol(Byte proto) {
91 return new IPProtocolCriterion(proto);
92 }
93
94 /**
95 * Creates a match on IP src field using the specified value.
96 *
97 * @param ip ip src value
98 * @return match criterion
99 */
Ayaka Koshibe1d56fe42014-09-19 16:51:58 -0700100 public static Criterion matchIPSrc(IpPrefix ip) {
alshabib7b795492014-09-16 14:38:39 -0700101 return new IPCriterion(ip, Type.IPV4_SRC);
102 }
103
104 /**
105 * Creates a match on IP dst field using the specified value.
106 *
107 * @param ip ip src value
108 * @return match criterion
109 */
Ayaka Koshibe1d56fe42014-09-19 16:51:58 -0700110 public static Criterion matchIPDst(IpPrefix ip) {
alshabib7b795492014-09-16 14:38:39 -0700111 return new IPCriterion(ip, Type.IPV4_DST);
alshabib369d2942014-09-12 17:59:35 -0700112 }
113
114
alshabib7b795492014-09-16 14:38:39 -0700115 /*
116 * Implementations of criteria.
117 */
118
119 public static final class PortCriterion implements Criterion {
120 private final PortNumber port;
121
122 public PortCriterion(PortNumber port) {
123 this.port = port;
124 }
125
126 @Override
127 public Type type() {
128 return Type.IN_PORT;
129 }
130
131 public PortNumber port() {
132 return this.port;
133 }
alshabib99b8fdc2014-09-25 14:30:22 -0700134
135 @Override
136 public String toString() {
137 return toStringHelper(type().toString())
138 .add("port", port).toString();
139 }
alshabib7b795492014-09-16 14:38:39 -0700140 }
141
142
143 public static final class EthCriterion implements Criterion {
Ayaka Koshibea9c199f2014-09-16 16:21:40 -0700144 private final MacAddress mac;
alshabib7b795492014-09-16 14:38:39 -0700145 private final Type type;
146
Ayaka Koshibea9c199f2014-09-16 16:21:40 -0700147 public EthCriterion(MacAddress mac, Type type) {
alshabib7b795492014-09-16 14:38:39 -0700148 this.mac = mac;
149 this.type = type;
150 }
151
152 @Override
153 public Type type() {
154 return this.type;
155 }
156
Ayaka Koshibea9c199f2014-09-16 16:21:40 -0700157 public MacAddress mac() {
alshabib7b795492014-09-16 14:38:39 -0700158 return this.mac;
159 }
alshabib99b8fdc2014-09-25 14:30:22 -0700160
161 @Override
162 public String toString() {
163 return toStringHelper(type().toString())
164 .add("mac", mac).toString();
165 }
166
alshabib7b795492014-09-16 14:38:39 -0700167 }
168
169 public static final class EthTypeCriterion implements Criterion {
170
171 private final Short ethType;
172
173 public EthTypeCriterion(Short ethType) {
174 this.ethType = ethType;
175 }
176
177 @Override
178 public Type type() {
179 return Type.ETH_TYPE;
180 }
181
182 public Short ethType() {
183 return ethType;
184 }
185
alshabib99b8fdc2014-09-25 14:30:22 -0700186 @Override
187 public String toString() {
188 return toStringHelper(type().toString())
189 .add("ethType", Long.toHexString(ethType)).toString();
190 }
191
alshabib7b795492014-09-16 14:38:39 -0700192 }
193
194
195 public static final class IPCriterion implements Criterion {
196
Ayaka Koshibe1d56fe42014-09-19 16:51:58 -0700197 private final IpPrefix ip;
alshabib7b795492014-09-16 14:38:39 -0700198 private final Type type;
199
Ayaka Koshibe1d56fe42014-09-19 16:51:58 -0700200 public IPCriterion(IpPrefix ip, Type type) {
alshabib7b795492014-09-16 14:38:39 -0700201 this.ip = ip;
202 this.type = type;
203 }
204
205 @Override
206 public Type type() {
207 return this.type;
208 }
209
Ayaka Koshibe1d56fe42014-09-19 16:51:58 -0700210 public IpPrefix ip() {
alshabib7b795492014-09-16 14:38:39 -0700211 return this.ip;
212 }
213
alshabib99b8fdc2014-09-25 14:30:22 -0700214 @Override
215 public String toString() {
216 return toStringHelper(type().toString())
217 .add("ip", ip).toString();
218 }
alshabib7b795492014-09-16 14:38:39 -0700219
220 }
221
222
223 public static final class IPProtocolCriterion implements Criterion {
224
225 private final Byte proto;
226
227 public IPProtocolCriterion(Byte protocol) {
228 this.proto = protocol;
229 }
230
231 @Override
232 public Type type() {
233 return Type.IP_PROTO;
234 }
235
236 public Byte protocol() {
237 return proto;
238 }
239
alshabib99b8fdc2014-09-25 14:30:22 -0700240 @Override
241 public String toString() {
242 return toStringHelper(type().toString())
243 .add("protocol", Long.toHexString(proto)).toString();
244 }
245
alshabib7b795492014-09-16 14:38:39 -0700246 }
247
248
249 public static final class VlanPcpCriterion implements Criterion {
250
251 private final Byte vlanPcp;
252
253 public VlanPcpCriterion(Byte vlanPcp) {
254 this.vlanPcp = vlanPcp;
255 }
256
257 @Override
258 public Type type() {
259 return Type.VLAN_PCP;
260 }
261
alshabib35edb1a2014-09-16 17:44:44 -0700262 public Byte priority() {
alshabib7b795492014-09-16 14:38:39 -0700263 return vlanPcp;
264 }
265
alshabib99b8fdc2014-09-25 14:30:22 -0700266 @Override
267 public String toString() {
268 return toStringHelper(type().toString())
269 .add("pcp", Long.toHexString(vlanPcp)).toString();
270 }
271
alshabib7b795492014-09-16 14:38:39 -0700272 }
273
274
275 public static final class VlanIdCriterion implements Criterion {
276
277
Ayaka Koshibea9c199f2014-09-16 16:21:40 -0700278 private final VlanId vlanId;
alshabib7b795492014-09-16 14:38:39 -0700279
Ayaka Koshibea9c199f2014-09-16 16:21:40 -0700280 public VlanIdCriterion(VlanId vlanId) {
alshabib7b795492014-09-16 14:38:39 -0700281 this.vlanId = vlanId;
282 }
283
284 @Override
285 public Type type() {
286 return Type.VLAN_VID;
287 }
288
Ayaka Koshibea9c199f2014-09-16 16:21:40 -0700289 public VlanId vlanId() {
alshabib7b795492014-09-16 14:38:39 -0700290 return vlanId;
291 }
292
alshabib99b8fdc2014-09-25 14:30:22 -0700293 @Override
294 public String toString() {
295 return toStringHelper(type().toString())
296 .add("id", vlanId).toString();
297 }
298
alshabib7b795492014-09-16 14:38:39 -0700299 }
300
301
tom8bb16062014-09-12 14:47:46 -0700302}