Thomas Vachuska | 83e090e | 2014-10-22 14:25:35 -0700 | [diff] [blame] | 1 | /* |
Ray Milkey | 34c9590 | 2015-04-15 09:47:53 -0700 | [diff] [blame] | 2 | * Copyright 2014-2015 Open Networking Laboratory |
Thomas Vachuska | 83e090e | 2014-10-22 14:25:35 -0700 | [diff] [blame] | 3 | * |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
Thomas Vachuska | 83e090e | 2014-10-22 14:25:35 -0700 | [diff] [blame] | 7 | * |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
Thomas Vachuska | 83e090e | 2014-10-22 14:25:35 -0700 | [diff] [blame] | 15 | */ |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 16 | package org.onosproject.net.flow.instructions; |
alshabib | 7410fea | 2014-09-16 13:48:39 -0700 | [diff] [blame] | 17 | |
alshabib | 99b8fdc | 2014-09-25 14:30:22 -0700 | [diff] [blame] | 18 | import static com.google.common.base.MoreObjects.toStringHelper; |
| 19 | |
alshabib | 8ca5390 | 2014-10-07 13:11:17 -0700 | [diff] [blame] | 20 | import java.util.Objects; |
| 21 | |
Pavlin Radoslavov | 855ea2d | 2014-10-30 15:32:39 -0700 | [diff] [blame] | 22 | import org.onlab.packet.IpAddress; |
lishuai | 3cce60b | 2015-12-01 19:35:16 +0800 | [diff] [blame] | 23 | import org.onlab.packet.MacAddress; |
alshabib | 7410fea | 2014-09-16 13:48:39 -0700 | [diff] [blame] | 24 | |
| 25 | /** |
| 26 | * Abstraction of a single traffic treatment step. |
alshabib | 7410fea | 2014-09-16 13:48:39 -0700 | [diff] [blame] | 27 | */ |
| 28 | public abstract class L3ModificationInstruction implements Instruction { |
| 29 | |
| 30 | /** |
| 31 | * Represents the type of traffic treatment. |
| 32 | */ |
alshabib | 35edb1a | 2014-09-16 17:44:44 -0700 | [diff] [blame] | 33 | public enum L3SubType { |
alshabib | 7410fea | 2014-09-16 13:48:39 -0700 | [diff] [blame] | 34 | /** |
Pavlin Radoslavov | febe82c | 2015-02-11 19:08:15 -0800 | [diff] [blame] | 35 | * IPv4 src modification. |
alshabib | 7410fea | 2014-09-16 13:48:39 -0700 | [diff] [blame] | 36 | */ |
Pavlin Radoslavov | febe82c | 2015-02-11 19:08:15 -0800 | [diff] [blame] | 37 | IPV4_SRC, |
alshabib | 7410fea | 2014-09-16 13:48:39 -0700 | [diff] [blame] | 38 | |
| 39 | /** |
Pavlin Radoslavov | febe82c | 2015-02-11 19:08:15 -0800 | [diff] [blame] | 40 | * IPv4 dst modification. |
alshabib | 7410fea | 2014-09-16 13:48:39 -0700 | [diff] [blame] | 41 | */ |
Pavlin Radoslavov | febe82c | 2015-02-11 19:08:15 -0800 | [diff] [blame] | 42 | IPV4_DST, |
sangho | 3f97a17d | 2015-01-29 22:56:29 -0800 | [diff] [blame] | 43 | |
| 44 | /** |
Pavlin Radoslavov | febe82c | 2015-02-11 19:08:15 -0800 | [diff] [blame] | 45 | * IPv6 src modification. |
| 46 | */ |
| 47 | IPV6_SRC, |
| 48 | |
| 49 | /** |
| 50 | * IPv6 dst modification. |
| 51 | */ |
| 52 | IPV6_DST, |
| 53 | |
| 54 | /** |
| 55 | * IPv6 flow label modification. |
| 56 | */ |
| 57 | IPV6_FLABEL, |
| 58 | |
| 59 | /** |
| 60 | * Decrement TTL. |
sangho | 3f97a17d | 2015-01-29 22:56:29 -0800 | [diff] [blame] | 61 | */ |
| 62 | DEC_TTL, |
| 63 | |
| 64 | /** |
| 65 | * Copy TTL out. |
| 66 | */ |
| 67 | TTL_OUT, |
| 68 | |
| 69 | /** |
| 70 | * Copy TTL in. |
| 71 | */ |
lishuai | 3cce60b | 2015-12-01 19:35:16 +0800 | [diff] [blame] | 72 | TTL_IN, |
| 73 | |
| 74 | /** |
| 75 | * ARP IP src modification. |
| 76 | */ |
| 77 | ARP_SPA, |
| 78 | |
| 79 | /** |
| 80 | * ARP Ether src modification. |
| 81 | */ |
| 82 | ARP_SHA, |
| 83 | |
| 84 | /** |
| 85 | * Arp operation modification. |
| 86 | */ |
| 87 | ARP_OP |
alshabib | 7410fea | 2014-09-16 13:48:39 -0700 | [diff] [blame] | 88 | |
| 89 | //TODO: remaining types |
| 90 | } |
| 91 | |
| 92 | /** |
| 93 | * Returns the subtype of the modification instruction. |
| 94 | * @return type of instruction |
| 95 | */ |
alshabib | 35edb1a | 2014-09-16 17:44:44 -0700 | [diff] [blame] | 96 | public abstract L3SubType subtype(); |
alshabib | 7410fea | 2014-09-16 13:48:39 -0700 | [diff] [blame] | 97 | |
| 98 | @Override |
Yuta HIGUCHI | 6a47964 | 2015-02-08 01:28:50 -0800 | [diff] [blame] | 99 | public final Type type() { |
alshabib | 35edb1a | 2014-09-16 17:44:44 -0700 | [diff] [blame] | 100 | return Type.L3MODIFICATION; |
alshabib | 7410fea | 2014-09-16 13:48:39 -0700 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | /** |
| 104 | * Represents a L3 src/dst modification instruction. |
| 105 | */ |
| 106 | public static final class ModIPInstruction extends L3ModificationInstruction { |
| 107 | |
alshabib | 35edb1a | 2014-09-16 17:44:44 -0700 | [diff] [blame] | 108 | private final L3SubType subtype; |
Pavlin Radoslavov | 855ea2d | 2014-10-30 15:32:39 -0700 | [diff] [blame] | 109 | private final IpAddress ip; |
alshabib | 7410fea | 2014-09-16 13:48:39 -0700 | [diff] [blame] | 110 | |
Yuta HIGUCHI | 6a47964 | 2015-02-08 01:28:50 -0800 | [diff] [blame] | 111 | ModIPInstruction(L3SubType subType, IpAddress addr) { |
alshabib | 64231f6 | 2014-09-16 17:58:36 -0700 | [diff] [blame] | 112 | |
alshabib | 7410fea | 2014-09-16 13:48:39 -0700 | [diff] [blame] | 113 | this.subtype = subType; |
| 114 | this.ip = addr; |
| 115 | } |
| 116 | |
| 117 | @Override |
alshabib | 35edb1a | 2014-09-16 17:44:44 -0700 | [diff] [blame] | 118 | public L3SubType subtype() { |
alshabib | 7410fea | 2014-09-16 13:48:39 -0700 | [diff] [blame] | 119 | return this.subtype; |
| 120 | } |
| 121 | |
Pavlin Radoslavov | 855ea2d | 2014-10-30 15:32:39 -0700 | [diff] [blame] | 122 | public IpAddress ip() { |
alshabib | 7410fea | 2014-09-16 13:48:39 -0700 | [diff] [blame] | 123 | return this.ip; |
| 124 | } |
| 125 | |
alshabib | 99b8fdc | 2014-09-25 14:30:22 -0700 | [diff] [blame] | 126 | @Override |
| 127 | public String toString() { |
| 128 | return toStringHelper(subtype().toString()) |
| 129 | .add("ip", ip).toString(); |
| 130 | } |
| 131 | |
alshabib | 8ca5390 | 2014-10-07 13:11:17 -0700 | [diff] [blame] | 132 | @Override |
| 133 | public int hashCode() { |
Thomas Vachuska | 9b2da21 | 2014-11-10 19:30:25 -0800 | [diff] [blame] | 134 | return Objects.hash(type(), subtype(), ip); |
alshabib | 8ca5390 | 2014-10-07 13:11:17 -0700 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | @Override |
| 138 | public boolean equals(Object obj) { |
| 139 | if (this == obj) { |
| 140 | return true; |
| 141 | } |
| 142 | if (obj instanceof ModIPInstruction) { |
| 143 | ModIPInstruction that = (ModIPInstruction) obj; |
alshabib | 2020b89 | 2014-10-20 15:47:08 -0700 | [diff] [blame] | 144 | return Objects.equals(ip, that.ip) && |
alshabib | 2020b89 | 2014-10-20 15:47:08 -0700 | [diff] [blame] | 145 | Objects.equals(this.subtype(), that.subtype()); |
alshabib | 8ca5390 | 2014-10-07 13:11:17 -0700 | [diff] [blame] | 146 | } |
| 147 | return false; |
| 148 | } |
sangho | 3f97a17d | 2015-01-29 22:56:29 -0800 | [diff] [blame] | 149 | } |
alshabib | 8ca5390 | 2014-10-07 13:11:17 -0700 | [diff] [blame] | 150 | |
Yuta HIGUCHI | cca5e72 | 2015-02-05 14:00:41 -0800 | [diff] [blame] | 151 | /** |
lishuai | 3cce60b | 2015-12-01 19:35:16 +0800 | [diff] [blame] | 152 | * Represents a L3 ARP IP src/dst modification instruction. |
| 153 | */ |
| 154 | public static final class ModArpIPInstruction extends L3ModificationInstruction { |
| 155 | |
| 156 | private final L3SubType subtype; |
| 157 | private final IpAddress ip; |
| 158 | |
| 159 | ModArpIPInstruction(L3SubType subType, IpAddress addr) { |
| 160 | |
| 161 | this.subtype = subType; |
| 162 | this.ip = addr; |
| 163 | } |
| 164 | |
| 165 | @Override |
| 166 | public L3SubType subtype() { |
| 167 | return this.subtype; |
| 168 | } |
| 169 | |
| 170 | public IpAddress ip() { |
| 171 | return this.ip; |
| 172 | } |
| 173 | |
| 174 | @Override |
| 175 | public String toString() { |
| 176 | return toStringHelper(subtype().toString()) |
| 177 | .add("ip", ip).toString(); |
| 178 | } |
| 179 | |
| 180 | @Override |
| 181 | public int hashCode() { |
| 182 | return Objects.hash(type(), subtype(), ip); |
| 183 | } |
| 184 | |
| 185 | @Override |
| 186 | public boolean equals(Object obj) { |
| 187 | if (this == obj) { |
| 188 | return true; |
| 189 | } |
| 190 | if (obj instanceof ModArpIPInstruction) { |
| 191 | ModArpIPInstruction that = (ModArpIPInstruction) obj; |
| 192 | return Objects.equals(ip, that.ip) && |
| 193 | Objects.equals(this.subtype(), that.subtype()); |
| 194 | } |
| 195 | return false; |
| 196 | } |
| 197 | } |
| 198 | |
| 199 | /** |
| 200 | * Represents a L3 ARP Ether src/dst modification instruction. |
| 201 | */ |
| 202 | public static final class ModArpEthInstruction extends L3ModificationInstruction { |
| 203 | |
| 204 | private final L3SubType subtype; |
| 205 | private final MacAddress mac; |
| 206 | |
| 207 | ModArpEthInstruction(L3SubType subType, MacAddress addr) { |
| 208 | |
| 209 | this.subtype = subType; |
| 210 | this.mac = addr; |
| 211 | } |
| 212 | |
| 213 | @Override |
| 214 | public L3SubType subtype() { |
| 215 | return this.subtype; |
| 216 | } |
| 217 | |
| 218 | public MacAddress mac() { |
| 219 | return this.mac; |
| 220 | } |
| 221 | |
| 222 | @Override |
| 223 | public String toString() { |
| 224 | return toStringHelper(subtype().toString()) |
| 225 | .add("mac", mac).toString(); |
| 226 | } |
| 227 | |
| 228 | @Override |
| 229 | public int hashCode() { |
| 230 | return Objects.hash(type(), subtype(), mac); |
| 231 | } |
| 232 | |
| 233 | @Override |
| 234 | public boolean equals(Object obj) { |
| 235 | if (this == obj) { |
| 236 | return true; |
| 237 | } |
| 238 | if (obj instanceof ModArpEthInstruction) { |
| 239 | ModArpEthInstruction that = (ModArpEthInstruction) obj; |
| 240 | return Objects.equals(mac, that.mac) && |
| 241 | Objects.equals(this.subtype(), that.subtype()); |
| 242 | } |
| 243 | return false; |
| 244 | } |
| 245 | } |
| 246 | |
| 247 | /** |
| 248 | * Represents a L3 ARP operation modification instruction. |
| 249 | */ |
| 250 | public static final class ModArpOpInstruction extends L3ModificationInstruction { |
| 251 | |
| 252 | private final L3SubType subtype; |
| 253 | private final short op; |
| 254 | |
| 255 | ModArpOpInstruction(L3SubType subType, short op) { |
| 256 | |
| 257 | this.subtype = subType; |
| 258 | this.op = op; |
| 259 | } |
| 260 | |
| 261 | @Override |
| 262 | public L3SubType subtype() { |
| 263 | return this.subtype; |
| 264 | } |
| 265 | |
| 266 | public long op() { |
| 267 | return this.op; |
| 268 | } |
| 269 | |
| 270 | @Override |
| 271 | public String toString() { |
| 272 | return toStringHelper(subtype().toString()) |
| 273 | .add("op", op).toString(); |
| 274 | } |
| 275 | |
| 276 | @Override |
| 277 | public int hashCode() { |
| 278 | return Objects.hash(type(), subtype(), op); |
| 279 | } |
| 280 | |
| 281 | @Override |
| 282 | public boolean equals(Object obj) { |
| 283 | if (this == obj) { |
| 284 | return true; |
| 285 | } |
| 286 | if (obj instanceof ModArpOpInstruction) { |
| 287 | ModArpOpInstruction that = (ModArpOpInstruction) obj; |
| 288 | return Objects.equals(op, that.op) && |
| 289 | Objects.equals(this.subtype(), that.subtype()); |
| 290 | } |
| 291 | return false; |
| 292 | } |
| 293 | } |
| 294 | |
| 295 | /** |
Pavlin Radoslavov | febe82c | 2015-02-11 19:08:15 -0800 | [diff] [blame] | 296 | * Represents a L3 IPv6 Flow Label (RFC 6437) modification instruction |
| 297 | * (20 bits unsigned integer). |
| 298 | */ |
| 299 | public static final class ModIPv6FlowLabelInstruction |
| 300 | extends L3ModificationInstruction { |
| 301 | private static final int MASK = 0xfffff; |
| 302 | private final int flowLabel; // IPv6 flow label: 20 bits |
| 303 | |
| 304 | /** |
Thomas Vachuska | 8ab196c | 2015-02-17 23:53:48 -0800 | [diff] [blame] | 305 | * Creates a new flow mod instruction. |
Pavlin Radoslavov | febe82c | 2015-02-11 19:08:15 -0800 | [diff] [blame] | 306 | * |
Thomas Vachuska | 8ab196c | 2015-02-17 23:53:48 -0800 | [diff] [blame] | 307 | * @param flowLabel the IPv6 flow label to set in the treatment (20 bits) |
Pavlin Radoslavov | febe82c | 2015-02-11 19:08:15 -0800 | [diff] [blame] | 308 | */ |
Yuta HIGUCHI | 6a47964 | 2015-02-08 01:28:50 -0800 | [diff] [blame] | 309 | ModIPv6FlowLabelInstruction(int flowLabel) { |
Pavlin Radoslavov | febe82c | 2015-02-11 19:08:15 -0800 | [diff] [blame] | 310 | this.flowLabel = flowLabel & MASK; |
| 311 | } |
| 312 | |
| 313 | @Override |
| 314 | public L3SubType subtype() { |
| 315 | return L3SubType.IPV6_FLABEL; |
| 316 | } |
| 317 | |
| 318 | /** |
| 319 | * Gets the IPv6 flow label to set in the treatment. |
| 320 | * |
| 321 | * @return the IPv6 flow label to set in the treatment (20 bits) |
| 322 | */ |
| 323 | public int flowLabel() { |
| 324 | return this.flowLabel; |
| 325 | } |
| 326 | |
| 327 | @Override |
| 328 | public String toString() { |
| 329 | return toStringHelper(subtype().toString()) |
| 330 | .add("flowLabel", Long.toHexString(flowLabel)).toString(); |
| 331 | } |
| 332 | |
| 333 | @Override |
| 334 | public int hashCode() { |
| 335 | return Objects.hash(type(), subtype(), flowLabel); |
| 336 | } |
| 337 | |
| 338 | @Override |
| 339 | public boolean equals(Object obj) { |
| 340 | if (this == obj) { |
| 341 | return true; |
| 342 | } |
| 343 | if (obj instanceof ModIPv6FlowLabelInstruction) { |
| 344 | ModIPv6FlowLabelInstruction that = |
| 345 | (ModIPv6FlowLabelInstruction) obj; |
Yuta HIGUCHI | 6a47964 | 2015-02-08 01:28:50 -0800 | [diff] [blame] | 346 | return Objects.equals(flowLabel, that.flowLabel); |
Pavlin Radoslavov | febe82c | 2015-02-11 19:08:15 -0800 | [diff] [blame] | 347 | } |
| 348 | return false; |
| 349 | } |
| 350 | } |
| 351 | |
| 352 | /** |
Yuta HIGUCHI | cca5e72 | 2015-02-05 14:00:41 -0800 | [diff] [blame] | 353 | * Represents a L3 TTL modification instruction. |
| 354 | */ |
sangho | 3f97a17d | 2015-01-29 22:56:29 -0800 | [diff] [blame] | 355 | public static final class ModTtlInstruction extends L3ModificationInstruction { |
| 356 | |
| 357 | private final L3SubType subtype; |
| 358 | |
Yuta HIGUCHI | 6a47964 | 2015-02-08 01:28:50 -0800 | [diff] [blame] | 359 | ModTtlInstruction(L3SubType subtype) { |
sangho | 3f97a17d | 2015-01-29 22:56:29 -0800 | [diff] [blame] | 360 | this.subtype = subtype; |
| 361 | } |
| 362 | |
| 363 | @Override |
| 364 | public L3SubType subtype() { |
| 365 | return this.subtype; |
| 366 | } |
| 367 | |
| 368 | @Override |
| 369 | public String toString() { |
Yuta HIGUCHI | cca5e72 | 2015-02-05 14:00:41 -0800 | [diff] [blame] | 370 | return toStringHelper(subtype().toString()) |
| 371 | .toString(); |
sangho | 3f97a17d | 2015-01-29 22:56:29 -0800 | [diff] [blame] | 372 | } |
| 373 | |
| 374 | @Override |
| 375 | public int hashCode() { |
| 376 | return Objects.hash(type(), subtype()); |
| 377 | } |
| 378 | |
| 379 | @Override |
| 380 | public boolean equals(Object obj) { |
| 381 | if (this == obj) { |
| 382 | return true; |
| 383 | } |
Yuta HIGUCHI | cca5e72 | 2015-02-05 14:00:41 -0800 | [diff] [blame] | 384 | if (obj instanceof ModTtlInstruction) { |
| 385 | ModTtlInstruction that = (ModTtlInstruction) obj; |
Yuta HIGUCHI | 6a47964 | 2015-02-08 01:28:50 -0800 | [diff] [blame] | 386 | return Objects.equals(this.subtype(), that.subtype()); |
sangho | 3f97a17d | 2015-01-29 22:56:29 -0800 | [diff] [blame] | 387 | } |
| 388 | return false; |
| 389 | } |
alshabib | 7410fea | 2014-09-16 13:48:39 -0700 | [diff] [blame] | 390 | } |
alshabib | 7410fea | 2014-09-16 13:48:39 -0700 | [diff] [blame] | 391 | } |