Thomas Vachuska | 83e090e | 2014-10-22 14:25:35 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2014-present 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 | |
Pavlin Radoslavov | 855ea2d | 2014-10-30 15:32:39 -0700 | [diff] [blame] | 18 | import org.onlab.packet.IpAddress; |
lishuai | 3cce60b | 2015-12-01 19:35:16 +0800 | [diff] [blame] | 19 | import org.onlab.packet.MacAddress; |
alshabib | 7410fea | 2014-09-16 13:48:39 -0700 | [diff] [blame] | 20 | |
Jonathan Hart | c7840bd | 2016-01-21 23:26:29 -0800 | [diff] [blame] | 21 | import java.util.Objects; |
| 22 | |
alshabib | 7410fea | 2014-09-16 13:48:39 -0700 | [diff] [blame] | 23 | /** |
| 24 | * Abstraction of a single traffic treatment step. |
alshabib | 7410fea | 2014-09-16 13:48:39 -0700 | [diff] [blame] | 25 | */ |
| 26 | public abstract class L3ModificationInstruction implements Instruction { |
| 27 | |
Jonathan Hart | c7840bd | 2016-01-21 23:26:29 -0800 | [diff] [blame] | 28 | private static final String SEPARATOR = ":"; |
| 29 | |
alshabib | 7410fea | 2014-09-16 13:48:39 -0700 | [diff] [blame] | 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 | |
| 90 | /** |
| 91 | * Returns the subtype of the modification instruction. |
| 92 | * @return type of instruction |
| 93 | */ |
alshabib | 35edb1a | 2014-09-16 17:44:44 -0700 | [diff] [blame] | 94 | public abstract L3SubType subtype(); |
alshabib | 7410fea | 2014-09-16 13:48:39 -0700 | [diff] [blame] | 95 | |
| 96 | @Override |
Yuta HIGUCHI | 6a47964 | 2015-02-08 01:28:50 -0800 | [diff] [blame] | 97 | public final Type type() { |
alshabib | 35edb1a | 2014-09-16 17:44:44 -0700 | [diff] [blame] | 98 | return Type.L3MODIFICATION; |
alshabib | 7410fea | 2014-09-16 13:48:39 -0700 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | /** |
| 102 | * Represents a L3 src/dst modification instruction. |
| 103 | */ |
| 104 | public static final class ModIPInstruction extends L3ModificationInstruction { |
| 105 | |
alshabib | 35edb1a | 2014-09-16 17:44:44 -0700 | [diff] [blame] | 106 | private final L3SubType subtype; |
Pavlin Radoslavov | 855ea2d | 2014-10-30 15:32:39 -0700 | [diff] [blame] | 107 | private final IpAddress ip; |
alshabib | 7410fea | 2014-09-16 13:48:39 -0700 | [diff] [blame] | 108 | |
Yuta HIGUCHI | 6a47964 | 2015-02-08 01:28:50 -0800 | [diff] [blame] | 109 | ModIPInstruction(L3SubType subType, IpAddress addr) { |
alshabib | 64231f6 | 2014-09-16 17:58:36 -0700 | [diff] [blame] | 110 | |
alshabib | 7410fea | 2014-09-16 13:48:39 -0700 | [diff] [blame] | 111 | this.subtype = subType; |
| 112 | this.ip = addr; |
| 113 | } |
| 114 | |
| 115 | @Override |
alshabib | 35edb1a | 2014-09-16 17:44:44 -0700 | [diff] [blame] | 116 | public L3SubType subtype() { |
alshabib | 7410fea | 2014-09-16 13:48:39 -0700 | [diff] [blame] | 117 | return this.subtype; |
| 118 | } |
| 119 | |
Pavlin Radoslavov | 855ea2d | 2014-10-30 15:32:39 -0700 | [diff] [blame] | 120 | public IpAddress ip() { |
alshabib | 7410fea | 2014-09-16 13:48:39 -0700 | [diff] [blame] | 121 | return this.ip; |
| 122 | } |
| 123 | |
alshabib | 99b8fdc | 2014-09-25 14:30:22 -0700 | [diff] [blame] | 124 | @Override |
| 125 | public String toString() { |
Jonathan Hart | c7840bd | 2016-01-21 23:26:29 -0800 | [diff] [blame] | 126 | return subtype().toString() + SEPARATOR + ip; |
alshabib | 99b8fdc | 2014-09-25 14:30:22 -0700 | [diff] [blame] | 127 | } |
| 128 | |
alshabib | 8ca5390 | 2014-10-07 13:11:17 -0700 | [diff] [blame] | 129 | @Override |
| 130 | public int hashCode() { |
Thomas Vachuska | 9b2da21 | 2014-11-10 19:30:25 -0800 | [diff] [blame] | 131 | return Objects.hash(type(), subtype(), ip); |
alshabib | 8ca5390 | 2014-10-07 13:11:17 -0700 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | @Override |
| 135 | public boolean equals(Object obj) { |
| 136 | if (this == obj) { |
| 137 | return true; |
| 138 | } |
| 139 | if (obj instanceof ModIPInstruction) { |
| 140 | ModIPInstruction that = (ModIPInstruction) obj; |
alshabib | 2020b89 | 2014-10-20 15:47:08 -0700 | [diff] [blame] | 141 | return Objects.equals(ip, that.ip) && |
alshabib | 2020b89 | 2014-10-20 15:47:08 -0700 | [diff] [blame] | 142 | Objects.equals(this.subtype(), that.subtype()); |
alshabib | 8ca5390 | 2014-10-07 13:11:17 -0700 | [diff] [blame] | 143 | } |
| 144 | return false; |
| 145 | } |
sangho | 3f97a17d | 2015-01-29 22:56:29 -0800 | [diff] [blame] | 146 | } |
alshabib | 8ca5390 | 2014-10-07 13:11:17 -0700 | [diff] [blame] | 147 | |
Yuta HIGUCHI | cca5e72 | 2015-02-05 14:00:41 -0800 | [diff] [blame] | 148 | /** |
lishuai | 3cce60b | 2015-12-01 19:35:16 +0800 | [diff] [blame] | 149 | * Represents a L3 ARP IP src/dst modification instruction. |
| 150 | */ |
| 151 | public static final class ModArpIPInstruction extends L3ModificationInstruction { |
| 152 | |
| 153 | private final L3SubType subtype; |
| 154 | private final IpAddress ip; |
| 155 | |
| 156 | ModArpIPInstruction(L3SubType subType, IpAddress addr) { |
| 157 | |
| 158 | this.subtype = subType; |
| 159 | this.ip = addr; |
| 160 | } |
| 161 | |
| 162 | @Override |
| 163 | public L3SubType subtype() { |
| 164 | return this.subtype; |
| 165 | } |
| 166 | |
| 167 | public IpAddress ip() { |
| 168 | return this.ip; |
| 169 | } |
| 170 | |
| 171 | @Override |
| 172 | public String toString() { |
Jonathan Hart | c7840bd | 2016-01-21 23:26:29 -0800 | [diff] [blame] | 173 | return subtype().toString() + SEPARATOR + ip; |
lishuai | 3cce60b | 2015-12-01 19:35:16 +0800 | [diff] [blame] | 174 | } |
| 175 | |
| 176 | @Override |
| 177 | public int hashCode() { |
| 178 | return Objects.hash(type(), subtype(), ip); |
| 179 | } |
| 180 | |
| 181 | @Override |
| 182 | public boolean equals(Object obj) { |
| 183 | if (this == obj) { |
| 184 | return true; |
| 185 | } |
| 186 | if (obj instanceof ModArpIPInstruction) { |
| 187 | ModArpIPInstruction that = (ModArpIPInstruction) obj; |
| 188 | return Objects.equals(ip, that.ip) && |
| 189 | Objects.equals(this.subtype(), that.subtype()); |
| 190 | } |
| 191 | return false; |
| 192 | } |
| 193 | } |
| 194 | |
| 195 | /** |
| 196 | * Represents a L3 ARP Ether src/dst modification instruction. |
| 197 | */ |
| 198 | public static final class ModArpEthInstruction extends L3ModificationInstruction { |
| 199 | |
| 200 | private final L3SubType subtype; |
| 201 | private final MacAddress mac; |
| 202 | |
| 203 | ModArpEthInstruction(L3SubType subType, MacAddress addr) { |
| 204 | |
| 205 | this.subtype = subType; |
| 206 | this.mac = addr; |
| 207 | } |
| 208 | |
| 209 | @Override |
| 210 | public L3SubType subtype() { |
| 211 | return this.subtype; |
| 212 | } |
| 213 | |
| 214 | public MacAddress mac() { |
| 215 | return this.mac; |
| 216 | } |
| 217 | |
| 218 | @Override |
| 219 | public String toString() { |
Jonathan Hart | c7840bd | 2016-01-21 23:26:29 -0800 | [diff] [blame] | 220 | return subtype().toString() + SEPARATOR + mac; |
lishuai | 3cce60b | 2015-12-01 19:35:16 +0800 | [diff] [blame] | 221 | } |
| 222 | |
| 223 | @Override |
| 224 | public int hashCode() { |
| 225 | return Objects.hash(type(), subtype(), mac); |
| 226 | } |
| 227 | |
| 228 | @Override |
| 229 | public boolean equals(Object obj) { |
| 230 | if (this == obj) { |
| 231 | return true; |
| 232 | } |
| 233 | if (obj instanceof ModArpEthInstruction) { |
| 234 | ModArpEthInstruction that = (ModArpEthInstruction) obj; |
| 235 | return Objects.equals(mac, that.mac) && |
| 236 | Objects.equals(this.subtype(), that.subtype()); |
| 237 | } |
| 238 | return false; |
| 239 | } |
| 240 | } |
| 241 | |
| 242 | /** |
| 243 | * Represents a L3 ARP operation modification instruction. |
| 244 | */ |
| 245 | public static final class ModArpOpInstruction extends L3ModificationInstruction { |
| 246 | |
| 247 | private final L3SubType subtype; |
| 248 | private final short op; |
| 249 | |
| 250 | ModArpOpInstruction(L3SubType subType, short op) { |
| 251 | |
| 252 | this.subtype = subType; |
| 253 | this.op = op; |
| 254 | } |
| 255 | |
| 256 | @Override |
| 257 | public L3SubType subtype() { |
| 258 | return this.subtype; |
| 259 | } |
| 260 | |
| 261 | public long op() { |
| 262 | return this.op; |
| 263 | } |
| 264 | |
| 265 | @Override |
| 266 | public String toString() { |
Jonathan Hart | c7840bd | 2016-01-21 23:26:29 -0800 | [diff] [blame] | 267 | return subtype().toString() + SEPARATOR + op; |
lishuai | 3cce60b | 2015-12-01 19:35:16 +0800 | [diff] [blame] | 268 | } |
| 269 | |
| 270 | @Override |
| 271 | public int hashCode() { |
| 272 | return Objects.hash(type(), subtype(), op); |
| 273 | } |
| 274 | |
| 275 | @Override |
| 276 | public boolean equals(Object obj) { |
| 277 | if (this == obj) { |
| 278 | return true; |
| 279 | } |
| 280 | if (obj instanceof ModArpOpInstruction) { |
| 281 | ModArpOpInstruction that = (ModArpOpInstruction) obj; |
| 282 | return Objects.equals(op, that.op) && |
| 283 | Objects.equals(this.subtype(), that.subtype()); |
| 284 | } |
| 285 | return false; |
| 286 | } |
| 287 | } |
| 288 | |
| 289 | /** |
Pavlin Radoslavov | febe82c | 2015-02-11 19:08:15 -0800 | [diff] [blame] | 290 | * Represents a L3 IPv6 Flow Label (RFC 6437) modification instruction |
| 291 | * (20 bits unsigned integer). |
| 292 | */ |
| 293 | public static final class ModIPv6FlowLabelInstruction |
| 294 | extends L3ModificationInstruction { |
| 295 | private static final int MASK = 0xfffff; |
| 296 | private final int flowLabel; // IPv6 flow label: 20 bits |
| 297 | |
| 298 | /** |
Thomas Vachuska | 8ab196c | 2015-02-17 23:53:48 -0800 | [diff] [blame] | 299 | * Creates a new flow mod instruction. |
Pavlin Radoslavov | febe82c | 2015-02-11 19:08:15 -0800 | [diff] [blame] | 300 | * |
Thomas Vachuska | 8ab196c | 2015-02-17 23:53:48 -0800 | [diff] [blame] | 301 | * @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] | 302 | */ |
Yuta HIGUCHI | 6a47964 | 2015-02-08 01:28:50 -0800 | [diff] [blame] | 303 | ModIPv6FlowLabelInstruction(int flowLabel) { |
Pavlin Radoslavov | febe82c | 2015-02-11 19:08:15 -0800 | [diff] [blame] | 304 | this.flowLabel = flowLabel & MASK; |
| 305 | } |
| 306 | |
| 307 | @Override |
| 308 | public L3SubType subtype() { |
| 309 | return L3SubType.IPV6_FLABEL; |
| 310 | } |
| 311 | |
| 312 | /** |
| 313 | * Gets the IPv6 flow label to set in the treatment. |
| 314 | * |
| 315 | * @return the IPv6 flow label to set in the treatment (20 bits) |
| 316 | */ |
| 317 | public int flowLabel() { |
| 318 | return this.flowLabel; |
| 319 | } |
| 320 | |
| 321 | @Override |
| 322 | public String toString() { |
Jonathan Hart | c7840bd | 2016-01-21 23:26:29 -0800 | [diff] [blame] | 323 | return subtype().toString() + SEPARATOR + Long.toHexString(flowLabel); |
Pavlin Radoslavov | febe82c | 2015-02-11 19:08:15 -0800 | [diff] [blame] | 324 | } |
| 325 | |
| 326 | @Override |
| 327 | public int hashCode() { |
| 328 | return Objects.hash(type(), subtype(), flowLabel); |
| 329 | } |
| 330 | |
| 331 | @Override |
| 332 | public boolean equals(Object obj) { |
| 333 | if (this == obj) { |
| 334 | return true; |
| 335 | } |
| 336 | if (obj instanceof ModIPv6FlowLabelInstruction) { |
| 337 | ModIPv6FlowLabelInstruction that = |
| 338 | (ModIPv6FlowLabelInstruction) obj; |
Yuta HIGUCHI | 6a47964 | 2015-02-08 01:28:50 -0800 | [diff] [blame] | 339 | return Objects.equals(flowLabel, that.flowLabel); |
Pavlin Radoslavov | febe82c | 2015-02-11 19:08:15 -0800 | [diff] [blame] | 340 | } |
| 341 | return false; |
| 342 | } |
| 343 | } |
| 344 | |
| 345 | /** |
Yuta HIGUCHI | cca5e72 | 2015-02-05 14:00:41 -0800 | [diff] [blame] | 346 | * Represents a L3 TTL modification instruction. |
| 347 | */ |
sangho | 3f97a17d | 2015-01-29 22:56:29 -0800 | [diff] [blame] | 348 | public static final class ModTtlInstruction extends L3ModificationInstruction { |
| 349 | |
| 350 | private final L3SubType subtype; |
| 351 | |
Yuta HIGUCHI | 6a47964 | 2015-02-08 01:28:50 -0800 | [diff] [blame] | 352 | ModTtlInstruction(L3SubType subtype) { |
sangho | 3f97a17d | 2015-01-29 22:56:29 -0800 | [diff] [blame] | 353 | this.subtype = subtype; |
| 354 | } |
| 355 | |
| 356 | @Override |
| 357 | public L3SubType subtype() { |
| 358 | return this.subtype; |
| 359 | } |
| 360 | |
| 361 | @Override |
| 362 | public String toString() { |
Jonathan Hart | c7840bd | 2016-01-21 23:26:29 -0800 | [diff] [blame] | 363 | return subtype().toString(); |
sangho | 3f97a17d | 2015-01-29 22:56:29 -0800 | [diff] [blame] | 364 | } |
| 365 | |
| 366 | @Override |
| 367 | public int hashCode() { |
| 368 | return Objects.hash(type(), subtype()); |
| 369 | } |
| 370 | |
| 371 | @Override |
| 372 | public boolean equals(Object obj) { |
| 373 | if (this == obj) { |
| 374 | return true; |
| 375 | } |
Yuta HIGUCHI | cca5e72 | 2015-02-05 14:00:41 -0800 | [diff] [blame] | 376 | if (obj instanceof ModTtlInstruction) { |
| 377 | ModTtlInstruction that = (ModTtlInstruction) obj; |
Yuta HIGUCHI | 6a47964 | 2015-02-08 01:28:50 -0800 | [diff] [blame] | 378 | return Objects.equals(this.subtype(), that.subtype()); |
sangho | 3f97a17d | 2015-01-29 22:56:29 -0800 | [diff] [blame] | 379 | } |
| 380 | return false; |
| 381 | } |
alshabib | 7410fea | 2014-09-16 13:48:39 -0700 | [diff] [blame] | 382 | } |
alshabib | 7410fea | 2014-09-16 13:48:39 -0700 | [diff] [blame] | 383 | } |