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 | |
| 116 | |
alshabib | 7b79549 | 2014-09-16 14:38:39 -0700 | [diff] [blame] | 117 | /* |
| 118 | * Implementations of criteria. |
| 119 | */ |
| 120 | |
| 121 | public static final class PortCriterion implements Criterion { |
| 122 | private final PortNumber port; |
| 123 | |
| 124 | public PortCriterion(PortNumber port) { |
| 125 | this.port = port; |
| 126 | } |
| 127 | |
| 128 | @Override |
| 129 | public Type type() { |
| 130 | return Type.IN_PORT; |
| 131 | } |
| 132 | |
| 133 | public PortNumber port() { |
| 134 | return this.port; |
| 135 | } |
alshabib | 99b8fdc | 2014-09-25 14:30:22 -0700 | [diff] [blame] | 136 | |
| 137 | @Override |
| 138 | public String toString() { |
| 139 | return toStringHelper(type().toString()) |
| 140 | .add("port", port).toString(); |
| 141 | } |
alshabib | ba5ac48 | 2014-10-02 17:15:20 -0700 | [diff] [blame^] | 142 | |
| 143 | @Override |
| 144 | public int hashCode() { |
| 145 | return Objects.hash(port); |
| 146 | } |
| 147 | |
| 148 | @Override |
| 149 | public boolean equals(Object obj) { |
| 150 | if (this == obj) { |
| 151 | return true; |
| 152 | } |
| 153 | if (obj instanceof PortCriterion) { |
| 154 | PortCriterion that = (PortCriterion) obj; |
| 155 | return Objects.equals(port, that.port); |
| 156 | |
| 157 | } |
| 158 | return false; |
| 159 | } |
| 160 | |
alshabib | 7b79549 | 2014-09-16 14:38:39 -0700 | [diff] [blame] | 161 | } |
| 162 | |
| 163 | |
| 164 | public static final class EthCriterion implements Criterion { |
Ayaka Koshibe | a9c199f | 2014-09-16 16:21:40 -0700 | [diff] [blame] | 165 | private final MacAddress mac; |
alshabib | 7b79549 | 2014-09-16 14:38:39 -0700 | [diff] [blame] | 166 | private final Type type; |
| 167 | |
Ayaka Koshibe | a9c199f | 2014-09-16 16:21:40 -0700 | [diff] [blame] | 168 | public EthCriterion(MacAddress mac, Type type) { |
alshabib | 7b79549 | 2014-09-16 14:38:39 -0700 | [diff] [blame] | 169 | this.mac = mac; |
| 170 | this.type = type; |
| 171 | } |
| 172 | |
| 173 | @Override |
| 174 | public Type type() { |
| 175 | return this.type; |
| 176 | } |
| 177 | |
Ayaka Koshibe | a9c199f | 2014-09-16 16:21:40 -0700 | [diff] [blame] | 178 | public MacAddress mac() { |
alshabib | 7b79549 | 2014-09-16 14:38:39 -0700 | [diff] [blame] | 179 | return this.mac; |
| 180 | } |
alshabib | 99b8fdc | 2014-09-25 14:30:22 -0700 | [diff] [blame] | 181 | |
| 182 | @Override |
| 183 | public String toString() { |
| 184 | return toStringHelper(type().toString()) |
| 185 | .add("mac", mac).toString(); |
| 186 | } |
| 187 | |
alshabib | ba5ac48 | 2014-10-02 17:15:20 -0700 | [diff] [blame^] | 188 | @Override |
| 189 | public int hashCode() { |
| 190 | return Objects.hash(mac, type); |
| 191 | } |
| 192 | |
| 193 | @Override |
| 194 | public boolean equals(Object obj) { |
| 195 | if (this == obj) { |
| 196 | return true; |
| 197 | } |
| 198 | if (obj instanceof EthCriterion) { |
| 199 | EthCriterion that = (EthCriterion) obj; |
| 200 | return Objects.equals(mac, that.mac) && |
| 201 | Objects.equals(type, that.type); |
| 202 | |
| 203 | |
| 204 | } |
| 205 | return false; |
| 206 | } |
| 207 | |
| 208 | |
alshabib | 7b79549 | 2014-09-16 14:38:39 -0700 | [diff] [blame] | 209 | } |
| 210 | |
| 211 | public static final class EthTypeCriterion implements Criterion { |
| 212 | |
| 213 | private final Short ethType; |
| 214 | |
| 215 | public EthTypeCriterion(Short ethType) { |
| 216 | this.ethType = ethType; |
| 217 | } |
| 218 | |
| 219 | @Override |
| 220 | public Type type() { |
| 221 | return Type.ETH_TYPE; |
| 222 | } |
| 223 | |
| 224 | public Short ethType() { |
| 225 | return ethType; |
| 226 | } |
| 227 | |
alshabib | 99b8fdc | 2014-09-25 14:30:22 -0700 | [diff] [blame] | 228 | @Override |
| 229 | public String toString() { |
| 230 | return toStringHelper(type().toString()) |
| 231 | .add("ethType", Long.toHexString(ethType)).toString(); |
| 232 | } |
| 233 | |
alshabib | ba5ac48 | 2014-10-02 17:15:20 -0700 | [diff] [blame^] | 234 | @Override |
| 235 | public int hashCode() { |
| 236 | return Objects.hash(ethType); |
| 237 | } |
| 238 | |
| 239 | @Override |
| 240 | public boolean equals(Object obj) { |
| 241 | if (this == obj) { |
| 242 | return true; |
| 243 | } |
| 244 | if (obj instanceof EthTypeCriterion) { |
| 245 | EthTypeCriterion that = (EthTypeCriterion) obj; |
| 246 | return Objects.equals(ethType, that.ethType); |
| 247 | |
| 248 | |
| 249 | } |
| 250 | return false; |
| 251 | } |
| 252 | |
alshabib | 7b79549 | 2014-09-16 14:38:39 -0700 | [diff] [blame] | 253 | } |
| 254 | |
| 255 | |
| 256 | public static final class IPCriterion implements Criterion { |
| 257 | |
Ayaka Koshibe | 1d56fe4 | 2014-09-19 16:51:58 -0700 | [diff] [blame] | 258 | private final IpPrefix ip; |
alshabib | 7b79549 | 2014-09-16 14:38:39 -0700 | [diff] [blame] | 259 | private final Type type; |
| 260 | |
Ayaka Koshibe | 1d56fe4 | 2014-09-19 16:51:58 -0700 | [diff] [blame] | 261 | public IPCriterion(IpPrefix ip, Type type) { |
alshabib | 7b79549 | 2014-09-16 14:38:39 -0700 | [diff] [blame] | 262 | this.ip = ip; |
| 263 | this.type = type; |
| 264 | } |
| 265 | |
| 266 | @Override |
| 267 | public Type type() { |
| 268 | return this.type; |
| 269 | } |
| 270 | |
Ayaka Koshibe | 1d56fe4 | 2014-09-19 16:51:58 -0700 | [diff] [blame] | 271 | public IpPrefix ip() { |
alshabib | 7b79549 | 2014-09-16 14:38:39 -0700 | [diff] [blame] | 272 | return this.ip; |
| 273 | } |
| 274 | |
alshabib | 99b8fdc | 2014-09-25 14:30:22 -0700 | [diff] [blame] | 275 | @Override |
| 276 | public String toString() { |
| 277 | return toStringHelper(type().toString()) |
| 278 | .add("ip", ip).toString(); |
| 279 | } |
alshabib | 7b79549 | 2014-09-16 14:38:39 -0700 | [diff] [blame] | 280 | |
alshabib | ba5ac48 | 2014-10-02 17:15:20 -0700 | [diff] [blame^] | 281 | @Override |
| 282 | public int hashCode() { |
| 283 | return Objects.hash(ip, type); |
| 284 | } |
| 285 | |
| 286 | @Override |
| 287 | public boolean equals(Object obj) { |
| 288 | if (this == obj) { |
| 289 | return true; |
| 290 | } |
| 291 | if (obj instanceof IPCriterion) { |
| 292 | IPCriterion that = (IPCriterion) obj; |
| 293 | return Objects.equals(ip, that.ip) && |
| 294 | Objects.equals(type, that.type); |
| 295 | |
| 296 | |
| 297 | } |
| 298 | return false; |
| 299 | } |
| 300 | |
alshabib | 7b79549 | 2014-09-16 14:38:39 -0700 | [diff] [blame] | 301 | } |
| 302 | |
| 303 | |
| 304 | public static final class IPProtocolCriterion implements Criterion { |
| 305 | |
| 306 | private final Byte proto; |
| 307 | |
| 308 | public IPProtocolCriterion(Byte protocol) { |
| 309 | this.proto = protocol; |
| 310 | } |
| 311 | |
| 312 | @Override |
| 313 | public Type type() { |
| 314 | return Type.IP_PROTO; |
| 315 | } |
| 316 | |
| 317 | public Byte protocol() { |
| 318 | return proto; |
| 319 | } |
| 320 | |
alshabib | 99b8fdc | 2014-09-25 14:30:22 -0700 | [diff] [blame] | 321 | @Override |
| 322 | public String toString() { |
| 323 | return toStringHelper(type().toString()) |
| 324 | .add("protocol", Long.toHexString(proto)).toString(); |
| 325 | } |
| 326 | |
alshabib | ba5ac48 | 2014-10-02 17:15:20 -0700 | [diff] [blame^] | 327 | @Override |
| 328 | public int hashCode() { |
| 329 | return Objects.hash(proto); |
| 330 | } |
| 331 | |
| 332 | @Override |
| 333 | public boolean equals(Object obj) { |
| 334 | if (this == obj) { |
| 335 | return true; |
| 336 | } |
| 337 | if (obj instanceof IPProtocolCriterion) { |
| 338 | IPProtocolCriterion that = (IPProtocolCriterion) obj; |
| 339 | return Objects.equals(proto, that.proto); |
| 340 | |
| 341 | |
| 342 | } |
| 343 | return false; |
| 344 | } |
| 345 | |
alshabib | 7b79549 | 2014-09-16 14:38:39 -0700 | [diff] [blame] | 346 | } |
| 347 | |
| 348 | |
| 349 | public static final class VlanPcpCriterion implements Criterion { |
| 350 | |
| 351 | private final Byte vlanPcp; |
| 352 | |
| 353 | public VlanPcpCriterion(Byte vlanPcp) { |
| 354 | this.vlanPcp = vlanPcp; |
| 355 | } |
| 356 | |
| 357 | @Override |
| 358 | public Type type() { |
| 359 | return Type.VLAN_PCP; |
| 360 | } |
| 361 | |
alshabib | 35edb1a | 2014-09-16 17:44:44 -0700 | [diff] [blame] | 362 | public Byte priority() { |
alshabib | 7b79549 | 2014-09-16 14:38:39 -0700 | [diff] [blame] | 363 | return vlanPcp; |
| 364 | } |
| 365 | |
alshabib | 99b8fdc | 2014-09-25 14:30:22 -0700 | [diff] [blame] | 366 | @Override |
| 367 | public String toString() { |
| 368 | return toStringHelper(type().toString()) |
| 369 | .add("pcp", Long.toHexString(vlanPcp)).toString(); |
| 370 | } |
| 371 | |
alshabib | ba5ac48 | 2014-10-02 17:15:20 -0700 | [diff] [blame^] | 372 | @Override |
| 373 | public int hashCode() { |
| 374 | return Objects.hash(vlanPcp); |
| 375 | } |
| 376 | |
| 377 | @Override |
| 378 | public boolean equals(Object obj) { |
| 379 | if (this == obj) { |
| 380 | return true; |
| 381 | } |
| 382 | if (obj instanceof VlanPcpCriterion) { |
| 383 | VlanPcpCriterion that = (VlanPcpCriterion) obj; |
| 384 | return Objects.equals(vlanPcp, that.vlanPcp); |
| 385 | |
| 386 | |
| 387 | } |
| 388 | return false; |
| 389 | } |
| 390 | |
alshabib | 7b79549 | 2014-09-16 14:38:39 -0700 | [diff] [blame] | 391 | } |
| 392 | |
| 393 | |
| 394 | public static final class VlanIdCriterion implements Criterion { |
| 395 | |
| 396 | |
Ayaka Koshibe | a9c199f | 2014-09-16 16:21:40 -0700 | [diff] [blame] | 397 | private final VlanId vlanId; |
alshabib | 7b79549 | 2014-09-16 14:38:39 -0700 | [diff] [blame] | 398 | |
Ayaka Koshibe | a9c199f | 2014-09-16 16:21:40 -0700 | [diff] [blame] | 399 | public VlanIdCriterion(VlanId vlanId) { |
alshabib | 7b79549 | 2014-09-16 14:38:39 -0700 | [diff] [blame] | 400 | this.vlanId = vlanId; |
| 401 | } |
| 402 | |
| 403 | @Override |
| 404 | public Type type() { |
| 405 | return Type.VLAN_VID; |
| 406 | } |
| 407 | |
Ayaka Koshibe | a9c199f | 2014-09-16 16:21:40 -0700 | [diff] [blame] | 408 | public VlanId vlanId() { |
alshabib | 7b79549 | 2014-09-16 14:38:39 -0700 | [diff] [blame] | 409 | return vlanId; |
| 410 | } |
| 411 | |
alshabib | 99b8fdc | 2014-09-25 14:30:22 -0700 | [diff] [blame] | 412 | @Override |
| 413 | public String toString() { |
| 414 | return toStringHelper(type().toString()) |
| 415 | .add("id", vlanId).toString(); |
| 416 | } |
| 417 | |
alshabib | ba5ac48 | 2014-10-02 17:15:20 -0700 | [diff] [blame^] | 418 | @Override |
| 419 | public int hashCode() { |
| 420 | return Objects.hash(vlanId); |
| 421 | } |
| 422 | |
| 423 | @Override |
| 424 | public boolean equals(Object obj) { |
| 425 | if (this == obj) { |
| 426 | return true; |
| 427 | } |
| 428 | if (obj instanceof VlanIdCriterion) { |
| 429 | VlanIdCriterion that = (VlanIdCriterion) obj; |
| 430 | return Objects.equals(vlanId, that.vlanId); |
| 431 | |
| 432 | |
| 433 | } |
| 434 | return false; |
| 435 | } |
| 436 | |
alshabib | 7b79549 | 2014-09-16 14:38:39 -0700 | [diff] [blame] | 437 | } |
| 438 | |
| 439 | |
tom | 8bb1606 | 2014-09-12 14:47:46 -0700 | [diff] [blame] | 440 | } |