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 | 7b79549 | 2014-09-16 14:38:39 -0700 | [diff] [blame] | 5 | import org.onlab.onos.net.PortNumber; |
| 6 | import org.onlab.onos.net.flow.criteria.Criterion.Type; |
Ayaka Koshibe | 1d56fe4 | 2014-09-19 16:51:58 -0700 | [diff] [blame] | 7 | import org.onlab.packet.IpPrefix; |
Ayaka Koshibe | a9c199f | 2014-09-16 16:21:40 -0700 | [diff] [blame] | 8 | import org.onlab.packet.MacAddress; |
| 9 | import org.onlab.packet.VlanId; |
alshabib | 7b79549 | 2014-09-16 14:38:39 -0700 | [diff] [blame] | 10 | |
tom | 8bb1606 | 2014-09-12 14:47:46 -0700 | [diff] [blame] | 11 | /** |
| 12 | * Factory class to create various traffic selection criteria. |
| 13 | */ |
| 14 | public final class Criteria { |
| 15 | |
alshabib | 7b79549 | 2014-09-16 14:38:39 -0700 | [diff] [blame] | 16 | //TODO: incomplete type implementation. Need to implement complete list from Criterion |
| 17 | |
tom | 8bb1606 | 2014-09-12 14:47:46 -0700 | [diff] [blame] | 18 | // Ban construction |
| 19 | private Criteria() { |
| 20 | } |
| 21 | |
| 22 | /** |
alshabib | 7b79549 | 2014-09-16 14:38:39 -0700 | [diff] [blame] | 23 | * 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 | /** |
tom | 8bb1606 | 2014-09-12 14:47:46 -0700 | [diff] [blame] | 33 | * Creates a match on ETH_SRC field using the specified value. This value |
| 34 | * may be a wildcard mask. |
| 35 | * |
tom | c104d28 | 2014-09-19 10:57:55 -0700 | [diff] [blame] | 36 | * @param mac MAC address value or wildcard mask |
tom | 8bb1606 | 2014-09-12 14:47:46 -0700 | [diff] [blame] | 37 | * @return match criterion |
| 38 | */ |
Ayaka Koshibe | a9c199f | 2014-09-16 16:21:40 -0700 | [diff] [blame] | 39 | public static Criterion matchEthSrc(MacAddress mac) { |
alshabib | 7b79549 | 2014-09-16 14:38:39 -0700 | [diff] [blame] | 40 | return new EthCriterion(mac, Type.ETH_SRC); |
tom | 8bb1606 | 2014-09-12 14:47:46 -0700 | [diff] [blame] | 41 | } |
| 42 | |
alshabib | 369d294 | 2014-09-12 17:59:35 -0700 | [diff] [blame] | 43 | /** |
| 44 | * Creates a match on ETH_DST field using the specified value. This value |
| 45 | * may be a wildcard mask. |
| 46 | * |
tom | c104d28 | 2014-09-19 10:57:55 -0700 | [diff] [blame] | 47 | * @param mac MAC address value or wildcard mask |
alshabib | 369d294 | 2014-09-12 17:59:35 -0700 | [diff] [blame] | 48 | * @return match criterion |
| 49 | */ |
Ayaka Koshibe | a9c199f | 2014-09-16 16:21:40 -0700 | [diff] [blame] | 50 | public static Criterion matchEthDst(MacAddress mac) { |
alshabib | 7b79549 | 2014-09-16 14:38:39 -0700 | [diff] [blame] | 51 | 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 Koshibe | a9c199f | 2014-09-16 16:21:40 -0700 | [diff] [blame] | 70 | public static Criterion matchVlanId(VlanId vlanId) { |
alshabib | 7b79549 | 2014-09-16 14:38:39 -0700 | [diff] [blame] | 71 | 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 | */ |
alshabib | b45d196 | 2014-09-18 14:25:45 -0700 | [diff] [blame] | 80 | public static Criterion matchVlanPcp(Byte vlanPcp) { |
alshabib | 7b79549 | 2014-09-16 14:38:39 -0700 | [diff] [blame] | 81 | 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 Koshibe | 1d56fe4 | 2014-09-19 16:51:58 -0700 | [diff] [blame] | 100 | public static Criterion matchIPSrc(IpPrefix ip) { |
alshabib | 7b79549 | 2014-09-16 14:38:39 -0700 | [diff] [blame] | 101 | 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 Koshibe | 1d56fe4 | 2014-09-19 16:51:58 -0700 | [diff] [blame] | 110 | public static Criterion matchIPDst(IpPrefix ip) { |
alshabib | 7b79549 | 2014-09-16 14:38:39 -0700 | [diff] [blame] | 111 | return new IPCriterion(ip, Type.IPV4_DST); |
alshabib | 369d294 | 2014-09-12 17:59:35 -0700 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | |
alshabib | 7b79549 | 2014-09-16 14:38:39 -0700 | [diff] [blame] | 115 | /* |
| 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 | } |
alshabib | 99b8fdc | 2014-09-25 14:30:22 -0700 | [diff] [blame^] | 134 | |
| 135 | @Override |
| 136 | public String toString() { |
| 137 | return toStringHelper(type().toString()) |
| 138 | .add("port", port).toString(); |
| 139 | } |
alshabib | 7b79549 | 2014-09-16 14:38:39 -0700 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | |
| 143 | public static final class EthCriterion implements Criterion { |
Ayaka Koshibe | a9c199f | 2014-09-16 16:21:40 -0700 | [diff] [blame] | 144 | private final MacAddress mac; |
alshabib | 7b79549 | 2014-09-16 14:38:39 -0700 | [diff] [blame] | 145 | private final Type type; |
| 146 | |
Ayaka Koshibe | a9c199f | 2014-09-16 16:21:40 -0700 | [diff] [blame] | 147 | public EthCriterion(MacAddress mac, Type type) { |
alshabib | 7b79549 | 2014-09-16 14:38:39 -0700 | [diff] [blame] | 148 | this.mac = mac; |
| 149 | this.type = type; |
| 150 | } |
| 151 | |
| 152 | @Override |
| 153 | public Type type() { |
| 154 | return this.type; |
| 155 | } |
| 156 | |
Ayaka Koshibe | a9c199f | 2014-09-16 16:21:40 -0700 | [diff] [blame] | 157 | public MacAddress mac() { |
alshabib | 7b79549 | 2014-09-16 14:38:39 -0700 | [diff] [blame] | 158 | return this.mac; |
| 159 | } |
alshabib | 99b8fdc | 2014-09-25 14:30:22 -0700 | [diff] [blame^] | 160 | |
| 161 | @Override |
| 162 | public String toString() { |
| 163 | return toStringHelper(type().toString()) |
| 164 | .add("mac", mac).toString(); |
| 165 | } |
| 166 | |
alshabib | 7b79549 | 2014-09-16 14:38:39 -0700 | [diff] [blame] | 167 | } |
| 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 | |
alshabib | 99b8fdc | 2014-09-25 14:30:22 -0700 | [diff] [blame^] | 186 | @Override |
| 187 | public String toString() { |
| 188 | return toStringHelper(type().toString()) |
| 189 | .add("ethType", Long.toHexString(ethType)).toString(); |
| 190 | } |
| 191 | |
alshabib | 7b79549 | 2014-09-16 14:38:39 -0700 | [diff] [blame] | 192 | } |
| 193 | |
| 194 | |
| 195 | public static final class IPCriterion implements Criterion { |
| 196 | |
Ayaka Koshibe | 1d56fe4 | 2014-09-19 16:51:58 -0700 | [diff] [blame] | 197 | private final IpPrefix ip; |
alshabib | 7b79549 | 2014-09-16 14:38:39 -0700 | [diff] [blame] | 198 | private final Type type; |
| 199 | |
Ayaka Koshibe | 1d56fe4 | 2014-09-19 16:51:58 -0700 | [diff] [blame] | 200 | public IPCriterion(IpPrefix ip, Type type) { |
alshabib | 7b79549 | 2014-09-16 14:38:39 -0700 | [diff] [blame] | 201 | this.ip = ip; |
| 202 | this.type = type; |
| 203 | } |
| 204 | |
| 205 | @Override |
| 206 | public Type type() { |
| 207 | return this.type; |
| 208 | } |
| 209 | |
Ayaka Koshibe | 1d56fe4 | 2014-09-19 16:51:58 -0700 | [diff] [blame] | 210 | public IpPrefix ip() { |
alshabib | 7b79549 | 2014-09-16 14:38:39 -0700 | [diff] [blame] | 211 | return this.ip; |
| 212 | } |
| 213 | |
alshabib | 99b8fdc | 2014-09-25 14:30:22 -0700 | [diff] [blame^] | 214 | @Override |
| 215 | public String toString() { |
| 216 | return toStringHelper(type().toString()) |
| 217 | .add("ip", ip).toString(); |
| 218 | } |
alshabib | 7b79549 | 2014-09-16 14:38:39 -0700 | [diff] [blame] | 219 | |
| 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 | |
alshabib | 99b8fdc | 2014-09-25 14:30:22 -0700 | [diff] [blame^] | 240 | @Override |
| 241 | public String toString() { |
| 242 | return toStringHelper(type().toString()) |
| 243 | .add("protocol", Long.toHexString(proto)).toString(); |
| 244 | } |
| 245 | |
alshabib | 7b79549 | 2014-09-16 14:38:39 -0700 | [diff] [blame] | 246 | } |
| 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 | |
alshabib | 35edb1a | 2014-09-16 17:44:44 -0700 | [diff] [blame] | 262 | public Byte priority() { |
alshabib | 7b79549 | 2014-09-16 14:38:39 -0700 | [diff] [blame] | 263 | return vlanPcp; |
| 264 | } |
| 265 | |
alshabib | 99b8fdc | 2014-09-25 14:30:22 -0700 | [diff] [blame^] | 266 | @Override |
| 267 | public String toString() { |
| 268 | return toStringHelper(type().toString()) |
| 269 | .add("pcp", Long.toHexString(vlanPcp)).toString(); |
| 270 | } |
| 271 | |
alshabib | 7b79549 | 2014-09-16 14:38:39 -0700 | [diff] [blame] | 272 | } |
| 273 | |
| 274 | |
| 275 | public static final class VlanIdCriterion implements Criterion { |
| 276 | |
| 277 | |
Ayaka Koshibe | a9c199f | 2014-09-16 16:21:40 -0700 | [diff] [blame] | 278 | private final VlanId vlanId; |
alshabib | 7b79549 | 2014-09-16 14:38:39 -0700 | [diff] [blame] | 279 | |
Ayaka Koshibe | a9c199f | 2014-09-16 16:21:40 -0700 | [diff] [blame] | 280 | public VlanIdCriterion(VlanId vlanId) { |
alshabib | 7b79549 | 2014-09-16 14:38:39 -0700 | [diff] [blame] | 281 | this.vlanId = vlanId; |
| 282 | } |
| 283 | |
| 284 | @Override |
| 285 | public Type type() { |
| 286 | return Type.VLAN_VID; |
| 287 | } |
| 288 | |
Ayaka Koshibe | a9c199f | 2014-09-16 16:21:40 -0700 | [diff] [blame] | 289 | public VlanId vlanId() { |
alshabib | 7b79549 | 2014-09-16 14:38:39 -0700 | [diff] [blame] | 290 | return vlanId; |
| 291 | } |
| 292 | |
alshabib | 99b8fdc | 2014-09-25 14:30:22 -0700 | [diff] [blame^] | 293 | @Override |
| 294 | public String toString() { |
| 295 | return toStringHelper(type().toString()) |
| 296 | .add("id", vlanId).toString(); |
| 297 | } |
| 298 | |
alshabib | 7b79549 | 2014-09-16 14:38:39 -0700 | [diff] [blame] | 299 | } |
| 300 | |
| 301 | |
tom | 8bb1606 | 2014-09-12 14:47:46 -0700 | [diff] [blame] | 302 | } |