blob: cc15360109c7660b007dc8ff6f4d4356f77ae2bd [file] [log] [blame]
Thomas Vachuska83e090e2014-10-22 14:25:35 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2014-present Open Networking Foundation
Thomas Vachuska83e090e2014-10-22 14:25:35 -07003 *
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07004 * 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 Vachuska83e090e2014-10-22 14:25:35 -07007 *
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07008 * 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 Vachuska83e090e2014-10-22 14:25:35 -070015 */
Brian O'Connorabafb502014-12-02 22:26:20 -080016package org.onosproject.net.flow.instructions;
alshabib7410fea2014-09-16 13:48:39 -070017
Pavlin Radoslavov855ea2d2014-10-30 15:32:39 -070018import org.onlab.packet.IpAddress;
lishuai3cce60b2015-12-01 19:35:16 +080019import org.onlab.packet.MacAddress;
alshabib7410fea2014-09-16 13:48:39 -070020
Jonathan Hartc7840bd2016-01-21 23:26:29 -080021import java.util.Objects;
22
alshabib7410fea2014-09-16 13:48:39 -070023/**
24 * Abstraction of a single traffic treatment step.
alshabib7410fea2014-09-16 13:48:39 -070025 */
26public abstract class L3ModificationInstruction implements Instruction {
27
Jonathan Hartc7840bd2016-01-21 23:26:29 -080028 private static final String SEPARATOR = ":";
29
alshabib7410fea2014-09-16 13:48:39 -070030 /**
31 * Represents the type of traffic treatment.
32 */
alshabib35edb1a2014-09-16 17:44:44 -070033 public enum L3SubType {
alshabib7410fea2014-09-16 13:48:39 -070034 /**
Pavlin Radoslavovfebe82c2015-02-11 19:08:15 -080035 * IPv4 src modification.
alshabib7410fea2014-09-16 13:48:39 -070036 */
Pavlin Radoslavovfebe82c2015-02-11 19:08:15 -080037 IPV4_SRC,
alshabib7410fea2014-09-16 13:48:39 -070038
39 /**
Pavlin Radoslavovfebe82c2015-02-11 19:08:15 -080040 * IPv4 dst modification.
alshabib7410fea2014-09-16 13:48:39 -070041 */
Pavlin Radoslavovfebe82c2015-02-11 19:08:15 -080042 IPV4_DST,
sangho3f97a17d2015-01-29 22:56:29 -080043
44 /**
Pavlin Radoslavovfebe82c2015-02-11 19:08:15 -080045 * 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.
sangho3f97a17d2015-01-29 22:56:29 -080061 */
62 DEC_TTL,
63
64 /**
65 * Copy TTL out.
66 */
67 TTL_OUT,
68
69 /**
70 * Copy TTL in.
71 */
lishuai3cce60b2015-12-01 19:35:16 +080072 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 */
Thiago Santos4a69ef82018-08-21 21:18:05 -070087 ARP_OP,
88
89 /**
90 * IP DSCP operation modification.
91 */
92 IP_DSCP
alshabib7410fea2014-09-16 13:48:39 -070093 }
94
95 /**
96 * Returns the subtype of the modification instruction.
97 * @return type of instruction
98 */
alshabib35edb1a2014-09-16 17:44:44 -070099 public abstract L3SubType subtype();
alshabib7410fea2014-09-16 13:48:39 -0700100
101 @Override
Yuta HIGUCHI6a479642015-02-08 01:28:50 -0800102 public final Type type() {
alshabib35edb1a2014-09-16 17:44:44 -0700103 return Type.L3MODIFICATION;
alshabib7410fea2014-09-16 13:48:39 -0700104 }
105
106 /**
107 * Represents a L3 src/dst modification instruction.
108 */
109 public static final class ModIPInstruction extends L3ModificationInstruction {
110
alshabib35edb1a2014-09-16 17:44:44 -0700111 private final L3SubType subtype;
Pavlin Radoslavov855ea2d2014-10-30 15:32:39 -0700112 private final IpAddress ip;
alshabib7410fea2014-09-16 13:48:39 -0700113
Yuta HIGUCHI6a479642015-02-08 01:28:50 -0800114 ModIPInstruction(L3SubType subType, IpAddress addr) {
alshabib64231f62014-09-16 17:58:36 -0700115
alshabib7410fea2014-09-16 13:48:39 -0700116 this.subtype = subType;
117 this.ip = addr;
118 }
119
120 @Override
alshabib35edb1a2014-09-16 17:44:44 -0700121 public L3SubType subtype() {
alshabib7410fea2014-09-16 13:48:39 -0700122 return this.subtype;
123 }
124
Pavlin Radoslavov855ea2d2014-10-30 15:32:39 -0700125 public IpAddress ip() {
alshabib7410fea2014-09-16 13:48:39 -0700126 return this.ip;
127 }
128
alshabib99b8fdc2014-09-25 14:30:22 -0700129 @Override
130 public String toString() {
Jonathan Hartc7840bd2016-01-21 23:26:29 -0800131 return subtype().toString() + SEPARATOR + ip;
alshabib99b8fdc2014-09-25 14:30:22 -0700132 }
133
alshabib8ca53902014-10-07 13:11:17 -0700134 @Override
135 public int hashCode() {
Thomas Vachuska9b2da212014-11-10 19:30:25 -0800136 return Objects.hash(type(), subtype(), ip);
alshabib8ca53902014-10-07 13:11:17 -0700137 }
138
139 @Override
140 public boolean equals(Object obj) {
141 if (this == obj) {
142 return true;
143 }
144 if (obj instanceof ModIPInstruction) {
145 ModIPInstruction that = (ModIPInstruction) obj;
alshabib2020b892014-10-20 15:47:08 -0700146 return Objects.equals(ip, that.ip) &&
alshabib2020b892014-10-20 15:47:08 -0700147 Objects.equals(this.subtype(), that.subtype());
alshabib8ca53902014-10-07 13:11:17 -0700148 }
149 return false;
150 }
sangho3f97a17d2015-01-29 22:56:29 -0800151 }
alshabib8ca53902014-10-07 13:11:17 -0700152
Yuta HIGUCHIcca5e722015-02-05 14:00:41 -0800153 /**
lishuai3cce60b2015-12-01 19:35:16 +0800154 * Represents a L3 ARP IP src/dst modification instruction.
155 */
156 public static final class ModArpIPInstruction extends L3ModificationInstruction {
157
158 private final L3SubType subtype;
159 private final IpAddress ip;
160
161 ModArpIPInstruction(L3SubType subType, IpAddress addr) {
162
163 this.subtype = subType;
164 this.ip = addr;
165 }
166
167 @Override
168 public L3SubType subtype() {
169 return this.subtype;
170 }
171
172 public IpAddress ip() {
173 return this.ip;
174 }
175
176 @Override
177 public String toString() {
Jonathan Hartc7840bd2016-01-21 23:26:29 -0800178 return subtype().toString() + SEPARATOR + ip;
lishuai3cce60b2015-12-01 19:35:16 +0800179 }
180
181 @Override
182 public int hashCode() {
183 return Objects.hash(type(), subtype(), ip);
184 }
185
186 @Override
187 public boolean equals(Object obj) {
188 if (this == obj) {
189 return true;
190 }
191 if (obj instanceof ModArpIPInstruction) {
192 ModArpIPInstruction that = (ModArpIPInstruction) obj;
193 return Objects.equals(ip, that.ip) &&
194 Objects.equals(this.subtype(), that.subtype());
195 }
196 return false;
197 }
198 }
199
200 /**
201 * Represents a L3 ARP Ether src/dst modification instruction.
202 */
203 public static final class ModArpEthInstruction extends L3ModificationInstruction {
204
205 private final L3SubType subtype;
206 private final MacAddress mac;
207
208 ModArpEthInstruction(L3SubType subType, MacAddress addr) {
209
210 this.subtype = subType;
211 this.mac = addr;
212 }
213
214 @Override
215 public L3SubType subtype() {
216 return this.subtype;
217 }
218
219 public MacAddress mac() {
220 return this.mac;
221 }
222
223 @Override
224 public String toString() {
Jonathan Hartc7840bd2016-01-21 23:26:29 -0800225 return subtype().toString() + SEPARATOR + mac;
lishuai3cce60b2015-12-01 19:35:16 +0800226 }
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() {
Jonathan Hartc7840bd2016-01-21 23:26:29 -0800272 return subtype().toString() + SEPARATOR + op;
lishuai3cce60b2015-12-01 19:35:16 +0800273 }
274
275 @Override
276 public int hashCode() {
277 return Objects.hash(type(), subtype(), op);
278 }
279
280 @Override
281 public boolean equals(Object obj) {
282 if (this == obj) {
283 return true;
284 }
285 if (obj instanceof ModArpOpInstruction) {
286 ModArpOpInstruction that = (ModArpOpInstruction) obj;
287 return Objects.equals(op, that.op) &&
288 Objects.equals(this.subtype(), that.subtype());
289 }
290 return false;
291 }
292 }
293
294 /**
Pavlin Radoslavovfebe82c2015-02-11 19:08:15 -0800295 * Represents a L3 IPv6 Flow Label (RFC 6437) modification instruction
296 * (20 bits unsigned integer).
297 */
298 public static final class ModIPv6FlowLabelInstruction
299 extends L3ModificationInstruction {
300 private static final int MASK = 0xfffff;
301 private final int flowLabel; // IPv6 flow label: 20 bits
302
303 /**
Thomas Vachuska8ab196c2015-02-17 23:53:48 -0800304 * Creates a new flow mod instruction.
Pavlin Radoslavovfebe82c2015-02-11 19:08:15 -0800305 *
Thomas Vachuska8ab196c2015-02-17 23:53:48 -0800306 * @param flowLabel the IPv6 flow label to set in the treatment (20 bits)
Pavlin Radoslavovfebe82c2015-02-11 19:08:15 -0800307 */
Yuta HIGUCHI6a479642015-02-08 01:28:50 -0800308 ModIPv6FlowLabelInstruction(int flowLabel) {
Pavlin Radoslavovfebe82c2015-02-11 19:08:15 -0800309 this.flowLabel = flowLabel & MASK;
310 }
311
312 @Override
313 public L3SubType subtype() {
314 return L3SubType.IPV6_FLABEL;
315 }
316
317 /**
318 * Gets the IPv6 flow label to set in the treatment.
319 *
320 * @return the IPv6 flow label to set in the treatment (20 bits)
321 */
322 public int flowLabel() {
323 return this.flowLabel;
324 }
325
326 @Override
327 public String toString() {
Jonathan Hartc7840bd2016-01-21 23:26:29 -0800328 return subtype().toString() + SEPARATOR + Long.toHexString(flowLabel);
Pavlin Radoslavovfebe82c2015-02-11 19:08:15 -0800329 }
330
331 @Override
332 public int hashCode() {
333 return Objects.hash(type(), subtype(), flowLabel);
334 }
335
336 @Override
337 public boolean equals(Object obj) {
338 if (this == obj) {
339 return true;
340 }
341 if (obj instanceof ModIPv6FlowLabelInstruction) {
342 ModIPv6FlowLabelInstruction that =
343 (ModIPv6FlowLabelInstruction) obj;
Yuta HIGUCHI6a479642015-02-08 01:28:50 -0800344 return Objects.equals(flowLabel, that.flowLabel);
Pavlin Radoslavovfebe82c2015-02-11 19:08:15 -0800345 }
346 return false;
347 }
348 }
349
350 /**
Yuta HIGUCHIcca5e722015-02-05 14:00:41 -0800351 * Represents a L3 TTL modification instruction.
352 */
sangho3f97a17d2015-01-29 22:56:29 -0800353 public static final class ModTtlInstruction extends L3ModificationInstruction {
354
355 private final L3SubType subtype;
356
Yuta HIGUCHI6a479642015-02-08 01:28:50 -0800357 ModTtlInstruction(L3SubType subtype) {
sangho3f97a17d2015-01-29 22:56:29 -0800358 this.subtype = subtype;
359 }
360
361 @Override
362 public L3SubType subtype() {
363 return this.subtype;
364 }
365
366 @Override
367 public String toString() {
Jonathan Hartc7840bd2016-01-21 23:26:29 -0800368 return subtype().toString();
sangho3f97a17d2015-01-29 22:56:29 -0800369 }
370
371 @Override
372 public int hashCode() {
373 return Objects.hash(type(), subtype());
374 }
375
376 @Override
377 public boolean equals(Object obj) {
378 if (this == obj) {
379 return true;
380 }
Yuta HIGUCHIcca5e722015-02-05 14:00:41 -0800381 if (obj instanceof ModTtlInstruction) {
382 ModTtlInstruction that = (ModTtlInstruction) obj;
Yuta HIGUCHI6a479642015-02-08 01:28:50 -0800383 return Objects.equals(this.subtype(), that.subtype());
sangho3f97a17d2015-01-29 22:56:29 -0800384 }
385 return false;
386 }
alshabib7410fea2014-09-16 13:48:39 -0700387 }
Thiago Santos4a69ef82018-08-21 21:18:05 -0700388
389 /**
390 * Represents a L3 DSCP modification instruction.
391 */
392 public static final class ModDscpInstruction extends L3ModificationInstruction {
393
394 private final L3SubType subtype;
395 private final byte dscp;
396
397 ModDscpInstruction(L3SubType subtype, byte dscp) {
398 this.subtype = subtype;
399 this.dscp = dscp;
400 }
401
402 @Override
403 public L3SubType subtype() {
404 return this.subtype;
405 }
406
407 @Override
408 public String toString() {
409 return subtype().toString() + SEPARATOR + dscp();
410 }
411
412 @Override
413 public int hashCode() {
414 return Objects.hash(type(), subtype(), dscp());
415 }
416
417 public byte dscp() {
418 return this.dscp;
419 }
420
421 @Override
422 public boolean equals(Object obj) {
423 if (this == obj) {
424 return true;
425 }
426 if (obj instanceof ModDscpInstruction) {
427 ModDscpInstruction that = (ModDscpInstruction) obj;
428 return Objects.equals(this.subtype(), that.subtype()) &&
429 Objects.equals(this.dscp(), that.dscp());
430 }
431 return false;
432 }
433 }
alshabib7410fea2014-09-16 13:48:39 -0700434}