alshabib | 7410fea | 2014-09-16 13:48:39 -0700 | [diff] [blame] | 1 | package org.onlab.onos.net.flow.criteria; |
tom | 8bb1606 | 2014-09-12 14:47:46 -0700 | [diff] [blame] | 2 | |
alshabib | 99b8fdc | 2014-09-25 14:30:22 -0700 | [diff] [blame] | 3 | import static com.google.common.base.MoreObjects.toStringHelper; |
| 4 | |
alshabib | ba5ac48 | 2014-10-02 17:15:20 -0700 | [diff] [blame] | 5 | import java.util.Objects; |
| 6 | |
alshabib | 7b79549 | 2014-09-16 14:38:39 -0700 | [diff] [blame] | 7 | import org.onlab.onos.net.PortNumber; |
| 8 | import org.onlab.onos.net.flow.criteria.Criterion.Type; |
Ayaka Koshibe | 1d56fe4 | 2014-09-19 16:51:58 -0700 | [diff] [blame] | 9 | import org.onlab.packet.IpPrefix; |
Ayaka Koshibe | a9c199f | 2014-09-16 16:21:40 -0700 | [diff] [blame] | 10 | import org.onlab.packet.MacAddress; |
| 11 | import org.onlab.packet.VlanId; |
alshabib | 7b79549 | 2014-09-16 14:38:39 -0700 | [diff] [blame] | 12 | |
tom | 8bb1606 | 2014-09-12 14:47:46 -0700 | [diff] [blame] | 13 | /** |
| 14 | * Factory class to create various traffic selection criteria. |
| 15 | */ |
| 16 | public final class Criteria { |
| 17 | |
alshabib | 7b79549 | 2014-09-16 14:38:39 -0700 | [diff] [blame] | 18 | //TODO: incomplete type implementation. Need to implement complete list from Criterion |
| 19 | |
tom | 8bb1606 | 2014-09-12 14:47:46 -0700 | [diff] [blame] | 20 | // Ban construction |
| 21 | private Criteria() { |
| 22 | } |
| 23 | |
| 24 | /** |
alshabib | 7b79549 | 2014-09-16 14:38:39 -0700 | [diff] [blame] | 25 | * 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 | /** |
tom | 8bb1606 | 2014-09-12 14:47:46 -0700 | [diff] [blame] | 35 | * Creates a match on ETH_SRC field using the specified value. This value |
| 36 | * may be a wildcard mask. |
| 37 | * |
tom | c104d28 | 2014-09-19 10:57:55 -0700 | [diff] [blame] | 38 | * @param mac MAC address value or wildcard mask |
tom | 8bb1606 | 2014-09-12 14:47:46 -0700 | [diff] [blame] | 39 | * @return match criterion |
| 40 | */ |
Ayaka Koshibe | a9c199f | 2014-09-16 16:21:40 -0700 | [diff] [blame] | 41 | public static Criterion matchEthSrc(MacAddress mac) { |
alshabib | 7b79549 | 2014-09-16 14:38:39 -0700 | [diff] [blame] | 42 | return new EthCriterion(mac, Type.ETH_SRC); |
tom | 8bb1606 | 2014-09-12 14:47:46 -0700 | [diff] [blame] | 43 | } |
| 44 | |
alshabib | 369d294 | 2014-09-12 17:59:35 -0700 | [diff] [blame] | 45 | /** |
| 46 | * Creates a match on ETH_DST field using the specified value. This value |
| 47 | * may be a wildcard mask. |
| 48 | * |
tom | c104d28 | 2014-09-19 10:57:55 -0700 | [diff] [blame] | 49 | * @param mac MAC address value or wildcard mask |
alshabib | 369d294 | 2014-09-12 17:59:35 -0700 | [diff] [blame] | 50 | * @return match criterion |
| 51 | */ |
Ayaka Koshibe | a9c199f | 2014-09-16 16:21:40 -0700 | [diff] [blame] | 52 | public static Criterion matchEthDst(MacAddress mac) { |
alshabib | 7b79549 | 2014-09-16 14:38:39 -0700 | [diff] [blame] | 53 | 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 Koshibe | a9c199f | 2014-09-16 16:21:40 -0700 | [diff] [blame] | 72 | public static Criterion matchVlanId(VlanId vlanId) { |
alshabib | 7b79549 | 2014-09-16 14:38:39 -0700 | [diff] [blame] | 73 | 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 | */ |
alshabib | b45d196 | 2014-09-18 14:25:45 -0700 | [diff] [blame] | 82 | public static Criterion matchVlanPcp(Byte vlanPcp) { |
alshabib | 7b79549 | 2014-09-16 14:38:39 -0700 | [diff] [blame] | 83 | 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 Koshibe | 1d56fe4 | 2014-09-19 16:51:58 -0700 | [diff] [blame] | 102 | public static Criterion matchIPSrc(IpPrefix ip) { |
alshabib | 7b79549 | 2014-09-16 14:38:39 -0700 | [diff] [blame] | 103 | 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 Koshibe | 1d56fe4 | 2014-09-19 16:51:58 -0700 | [diff] [blame] | 112 | public static Criterion matchIPDst(IpPrefix ip) { |
alshabib | 7b79549 | 2014-09-16 14:38:39 -0700 | [diff] [blame] | 113 | return new IPCriterion(ip, Type.IPV4_DST); |
alshabib | 369d294 | 2014-09-12 17:59:35 -0700 | [diff] [blame] | 114 | } |
| 115 | |
Toshio Koide | 9c44c9a | 2014-10-09 11:44:36 -0700 | [diff] [blame^] | 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 | } |
alshabib | 369d294 | 2014-09-12 17:59:35 -0700 | [diff] [blame] | 135 | |
alshabib | 7b79549 | 2014-09-16 14:38:39 -0700 | [diff] [blame] | 136 | /* |
| 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 | } |
alshabib | 99b8fdc | 2014-09-25 14:30:22 -0700 | [diff] [blame] | 155 | |
| 156 | @Override |
| 157 | public String toString() { |
| 158 | return toStringHelper(type().toString()) |
| 159 | .add("port", port).toString(); |
| 160 | } |
alshabib | ba5ac48 | 2014-10-02 17:15:20 -0700 | [diff] [blame] | 161 | |
| 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 | |
alshabib | 7b79549 | 2014-09-16 14:38:39 -0700 | [diff] [blame] | 180 | } |
| 181 | |
| 182 | |
| 183 | public static final class EthCriterion implements Criterion { |
Ayaka Koshibe | a9c199f | 2014-09-16 16:21:40 -0700 | [diff] [blame] | 184 | private final MacAddress mac; |
alshabib | 7b79549 | 2014-09-16 14:38:39 -0700 | [diff] [blame] | 185 | private final Type type; |
| 186 | |
Ayaka Koshibe | a9c199f | 2014-09-16 16:21:40 -0700 | [diff] [blame] | 187 | public EthCriterion(MacAddress mac, Type type) { |
alshabib | 7b79549 | 2014-09-16 14:38:39 -0700 | [diff] [blame] | 188 | this.mac = mac; |
| 189 | this.type = type; |
| 190 | } |
| 191 | |
| 192 | @Override |
| 193 | public Type type() { |
| 194 | return this.type; |
| 195 | } |
| 196 | |
Ayaka Koshibe | a9c199f | 2014-09-16 16:21:40 -0700 | [diff] [blame] | 197 | public MacAddress mac() { |
alshabib | 7b79549 | 2014-09-16 14:38:39 -0700 | [diff] [blame] | 198 | return this.mac; |
| 199 | } |
alshabib | 99b8fdc | 2014-09-25 14:30:22 -0700 | [diff] [blame] | 200 | |
| 201 | @Override |
| 202 | public String toString() { |
| 203 | return toStringHelper(type().toString()) |
| 204 | .add("mac", mac).toString(); |
| 205 | } |
| 206 | |
alshabib | ba5ac48 | 2014-10-02 17:15:20 -0700 | [diff] [blame] | 207 | @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 | |
alshabib | 7b79549 | 2014-09-16 14:38:39 -0700 | [diff] [blame] | 228 | } |
| 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 | |
alshabib | 99b8fdc | 2014-09-25 14:30:22 -0700 | [diff] [blame] | 247 | @Override |
| 248 | public String toString() { |
| 249 | return toStringHelper(type().toString()) |
| 250 | .add("ethType", Long.toHexString(ethType)).toString(); |
| 251 | } |
| 252 | |
alshabib | ba5ac48 | 2014-10-02 17:15:20 -0700 | [diff] [blame] | 253 | @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 | |
alshabib | 7b79549 | 2014-09-16 14:38:39 -0700 | [diff] [blame] | 272 | } |
| 273 | |
| 274 | |
| 275 | public static final class IPCriterion implements Criterion { |
| 276 | |
Ayaka Koshibe | 1d56fe4 | 2014-09-19 16:51:58 -0700 | [diff] [blame] | 277 | private final IpPrefix ip; |
alshabib | 7b79549 | 2014-09-16 14:38:39 -0700 | [diff] [blame] | 278 | private final Type type; |
| 279 | |
Ayaka Koshibe | 1d56fe4 | 2014-09-19 16:51:58 -0700 | [diff] [blame] | 280 | public IPCriterion(IpPrefix ip, Type type) { |
alshabib | 7b79549 | 2014-09-16 14:38:39 -0700 | [diff] [blame] | 281 | this.ip = ip; |
| 282 | this.type = type; |
| 283 | } |
| 284 | |
| 285 | @Override |
| 286 | public Type type() { |
| 287 | return this.type; |
| 288 | } |
| 289 | |
Ayaka Koshibe | 1d56fe4 | 2014-09-19 16:51:58 -0700 | [diff] [blame] | 290 | public IpPrefix ip() { |
alshabib | 7b79549 | 2014-09-16 14:38:39 -0700 | [diff] [blame] | 291 | return this.ip; |
| 292 | } |
| 293 | |
alshabib | 99b8fdc | 2014-09-25 14:30:22 -0700 | [diff] [blame] | 294 | @Override |
| 295 | public String toString() { |
| 296 | return toStringHelper(type().toString()) |
| 297 | .add("ip", ip).toString(); |
| 298 | } |
alshabib | 7b79549 | 2014-09-16 14:38:39 -0700 | [diff] [blame] | 299 | |
alshabib | ba5ac48 | 2014-10-02 17:15:20 -0700 | [diff] [blame] | 300 | @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 | |
alshabib | 7b79549 | 2014-09-16 14:38:39 -0700 | [diff] [blame] | 320 | } |
| 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 | |
alshabib | 99b8fdc | 2014-09-25 14:30:22 -0700 | [diff] [blame] | 340 | @Override |
| 341 | public String toString() { |
| 342 | return toStringHelper(type().toString()) |
| 343 | .add("protocol", Long.toHexString(proto)).toString(); |
| 344 | } |
| 345 | |
alshabib | ba5ac48 | 2014-10-02 17:15:20 -0700 | [diff] [blame] | 346 | @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 | |
alshabib | 7b79549 | 2014-09-16 14:38:39 -0700 | [diff] [blame] | 365 | } |
| 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 | |
alshabib | 35edb1a | 2014-09-16 17:44:44 -0700 | [diff] [blame] | 381 | public Byte priority() { |
alshabib | 7b79549 | 2014-09-16 14:38:39 -0700 | [diff] [blame] | 382 | return vlanPcp; |
| 383 | } |
| 384 | |
alshabib | 99b8fdc | 2014-09-25 14:30:22 -0700 | [diff] [blame] | 385 | @Override |
| 386 | public String toString() { |
| 387 | return toStringHelper(type().toString()) |
| 388 | .add("pcp", Long.toHexString(vlanPcp)).toString(); |
| 389 | } |
| 390 | |
alshabib | ba5ac48 | 2014-10-02 17:15:20 -0700 | [diff] [blame] | 391 | @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 | |
alshabib | 7b79549 | 2014-09-16 14:38:39 -0700 | [diff] [blame] | 410 | } |
| 411 | |
| 412 | |
| 413 | public static final class VlanIdCriterion implements Criterion { |
| 414 | |
| 415 | |
Ayaka Koshibe | a9c199f | 2014-09-16 16:21:40 -0700 | [diff] [blame] | 416 | private final VlanId vlanId; |
alshabib | 7b79549 | 2014-09-16 14:38:39 -0700 | [diff] [blame] | 417 | |
Ayaka Koshibe | a9c199f | 2014-09-16 16:21:40 -0700 | [diff] [blame] | 418 | public VlanIdCriterion(VlanId vlanId) { |
alshabib | 7b79549 | 2014-09-16 14:38:39 -0700 | [diff] [blame] | 419 | this.vlanId = vlanId; |
| 420 | } |
| 421 | |
| 422 | @Override |
| 423 | public Type type() { |
| 424 | return Type.VLAN_VID; |
| 425 | } |
| 426 | |
Ayaka Koshibe | a9c199f | 2014-09-16 16:21:40 -0700 | [diff] [blame] | 427 | public VlanId vlanId() { |
alshabib | 7b79549 | 2014-09-16 14:38:39 -0700 | [diff] [blame] | 428 | return vlanId; |
| 429 | } |
| 430 | |
alshabib | 99b8fdc | 2014-09-25 14:30:22 -0700 | [diff] [blame] | 431 | @Override |
| 432 | public String toString() { |
| 433 | return toStringHelper(type().toString()) |
| 434 | .add("id", vlanId).toString(); |
| 435 | } |
| 436 | |
alshabib | ba5ac48 | 2014-10-02 17:15:20 -0700 | [diff] [blame] | 437 | @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 | |
alshabib | 7b79549 | 2014-09-16 14:38:39 -0700 | [diff] [blame] | 456 | } |
| 457 | |
| 458 | |
Toshio Koide | 9c44c9a | 2014-10-09 11:44:36 -0700 | [diff] [blame^] | 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 | } |
tom | 8bb1606 | 2014-09-12 14:47:46 -0700 | [diff] [blame] | 504 | } |