blob: f06e9e1673ac4b8e414548bd56d08568d905bb9f [file] [log] [blame]
Thomas Vachuska83e090e2014-10-22 14:25:35 -07001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2014-present Open Networking Laboratory
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 */
87 ARP_OP
alshabib7410fea2014-09-16 13:48:39 -070088 }
89
90 /**
91 * Returns the subtype of the modification instruction.
92 * @return type of instruction
93 */
alshabib35edb1a2014-09-16 17:44:44 -070094 public abstract L3SubType subtype();
alshabib7410fea2014-09-16 13:48:39 -070095
96 @Override
Yuta HIGUCHI6a479642015-02-08 01:28:50 -080097 public final Type type() {
alshabib35edb1a2014-09-16 17:44:44 -070098 return Type.L3MODIFICATION;
alshabib7410fea2014-09-16 13:48:39 -070099 }
100
101 /**
102 * Represents a L3 src/dst modification instruction.
103 */
104 public static final class ModIPInstruction extends L3ModificationInstruction {
105
alshabib35edb1a2014-09-16 17:44:44 -0700106 private final L3SubType subtype;
Pavlin Radoslavov855ea2d2014-10-30 15:32:39 -0700107 private final IpAddress ip;
alshabib7410fea2014-09-16 13:48:39 -0700108
Yuta HIGUCHI6a479642015-02-08 01:28:50 -0800109 ModIPInstruction(L3SubType subType, IpAddress addr) {
alshabib64231f62014-09-16 17:58:36 -0700110
alshabib7410fea2014-09-16 13:48:39 -0700111 this.subtype = subType;
112 this.ip = addr;
113 }
114
115 @Override
alshabib35edb1a2014-09-16 17:44:44 -0700116 public L3SubType subtype() {
alshabib7410fea2014-09-16 13:48:39 -0700117 return this.subtype;
118 }
119
Pavlin Radoslavov855ea2d2014-10-30 15:32:39 -0700120 public IpAddress ip() {
alshabib7410fea2014-09-16 13:48:39 -0700121 return this.ip;
122 }
123
alshabib99b8fdc2014-09-25 14:30:22 -0700124 @Override
125 public String toString() {
Jonathan Hartc7840bd2016-01-21 23:26:29 -0800126 return subtype().toString() + SEPARATOR + ip;
alshabib99b8fdc2014-09-25 14:30:22 -0700127 }
128
alshabib8ca53902014-10-07 13:11:17 -0700129 @Override
130 public int hashCode() {
Thomas Vachuska9b2da212014-11-10 19:30:25 -0800131 return Objects.hash(type(), subtype(), ip);
alshabib8ca53902014-10-07 13:11:17 -0700132 }
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;
alshabib2020b892014-10-20 15:47:08 -0700141 return Objects.equals(ip, that.ip) &&
alshabib2020b892014-10-20 15:47:08 -0700142 Objects.equals(this.subtype(), that.subtype());
alshabib8ca53902014-10-07 13:11:17 -0700143 }
144 return false;
145 }
sangho3f97a17d2015-01-29 22:56:29 -0800146 }
alshabib8ca53902014-10-07 13:11:17 -0700147
Yuta HIGUCHIcca5e722015-02-05 14:00:41 -0800148 /**
lishuai3cce60b2015-12-01 19:35:16 +0800149 * 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 Hartc7840bd2016-01-21 23:26:29 -0800173 return subtype().toString() + SEPARATOR + ip;
lishuai3cce60b2015-12-01 19:35:16 +0800174 }
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 Hartc7840bd2016-01-21 23:26:29 -0800220 return subtype().toString() + SEPARATOR + mac;
lishuai3cce60b2015-12-01 19:35:16 +0800221 }
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 Hartc7840bd2016-01-21 23:26:29 -0800267 return subtype().toString() + SEPARATOR + op;
lishuai3cce60b2015-12-01 19:35:16 +0800268 }
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 Radoslavovfebe82c2015-02-11 19:08:15 -0800290 * 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 Vachuska8ab196c2015-02-17 23:53:48 -0800299 * Creates a new flow mod instruction.
Pavlin Radoslavovfebe82c2015-02-11 19:08:15 -0800300 *
Thomas Vachuska8ab196c2015-02-17 23:53:48 -0800301 * @param flowLabel the IPv6 flow label to set in the treatment (20 bits)
Pavlin Radoslavovfebe82c2015-02-11 19:08:15 -0800302 */
Yuta HIGUCHI6a479642015-02-08 01:28:50 -0800303 ModIPv6FlowLabelInstruction(int flowLabel) {
Pavlin Radoslavovfebe82c2015-02-11 19:08:15 -0800304 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 Hartc7840bd2016-01-21 23:26:29 -0800323 return subtype().toString() + SEPARATOR + Long.toHexString(flowLabel);
Pavlin Radoslavovfebe82c2015-02-11 19:08:15 -0800324 }
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 HIGUCHI6a479642015-02-08 01:28:50 -0800339 return Objects.equals(flowLabel, that.flowLabel);
Pavlin Radoslavovfebe82c2015-02-11 19:08:15 -0800340 }
341 return false;
342 }
343 }
344
345 /**
Yuta HIGUCHIcca5e722015-02-05 14:00:41 -0800346 * Represents a L3 TTL modification instruction.
347 */
sangho3f97a17d2015-01-29 22:56:29 -0800348 public static final class ModTtlInstruction extends L3ModificationInstruction {
349
350 private final L3SubType subtype;
351
Yuta HIGUCHI6a479642015-02-08 01:28:50 -0800352 ModTtlInstruction(L3SubType subtype) {
sangho3f97a17d2015-01-29 22:56:29 -0800353 this.subtype = subtype;
354 }
355
356 @Override
357 public L3SubType subtype() {
358 return this.subtype;
359 }
360
361 @Override
362 public String toString() {
Jonathan Hartc7840bd2016-01-21 23:26:29 -0800363 return subtype().toString();
sangho3f97a17d2015-01-29 22:56:29 -0800364 }
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 HIGUCHIcca5e722015-02-05 14:00:41 -0800376 if (obj instanceof ModTtlInstruction) {
377 ModTtlInstruction that = (ModTtlInstruction) obj;
Yuta HIGUCHI6a479642015-02-08 01:28:50 -0800378 return Objects.equals(this.subtype(), that.subtype());
sangho3f97a17d2015-01-29 22:56:29 -0800379 }
380 return false;
381 }
alshabib7410fea2014-09-16 13:48:39 -0700382 }
alshabib7410fea2014-09-16 13:48:39 -0700383}