blob: 0efe9a77371a020126d35cb402f8f01e46aa7c69 [file] [log] [blame]
Thomas Vachuska83e090e2014-10-22 14:25:35 -07001/*
Ray Milkey34c95902015-04-15 09:47:53 -07002 * Copyright 2014-2015 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
alshabib99b8fdc2014-09-25 14:30:22 -070018import static com.google.common.base.MoreObjects.toStringHelper;
19
alshabib8ca53902014-10-07 13:11:17 -070020import java.util.Objects;
21
Pavlin Radoslavov855ea2d2014-10-30 15:32:39 -070022import org.onlab.packet.IpAddress;
lishuai3cce60b2015-12-01 19:35:16 +080023import org.onlab.packet.MacAddress;
alshabib7410fea2014-09-16 13:48:39 -070024
25/**
26 * Abstraction of a single traffic treatment step.
alshabib7410fea2014-09-16 13:48:39 -070027 */
28public abstract class L3ModificationInstruction implements Instruction {
29
30 /**
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 //TODO: remaining types
90 }
91
92 /**
93 * Returns the subtype of the modification instruction.
94 * @return type of instruction
95 */
alshabib35edb1a2014-09-16 17:44:44 -070096 public abstract L3SubType subtype();
alshabib7410fea2014-09-16 13:48:39 -070097
98 @Override
Yuta HIGUCHI6a479642015-02-08 01:28:50 -080099 public final Type type() {
alshabib35edb1a2014-09-16 17:44:44 -0700100 return Type.L3MODIFICATION;
alshabib7410fea2014-09-16 13:48:39 -0700101 }
102
103 /**
104 * Represents a L3 src/dst modification instruction.
105 */
106 public static final class ModIPInstruction extends L3ModificationInstruction {
107
alshabib35edb1a2014-09-16 17:44:44 -0700108 private final L3SubType subtype;
Pavlin Radoslavov855ea2d2014-10-30 15:32:39 -0700109 private final IpAddress ip;
alshabib7410fea2014-09-16 13:48:39 -0700110
Yuta HIGUCHI6a479642015-02-08 01:28:50 -0800111 ModIPInstruction(L3SubType subType, IpAddress addr) {
alshabib64231f62014-09-16 17:58:36 -0700112
alshabib7410fea2014-09-16 13:48:39 -0700113 this.subtype = subType;
114 this.ip = addr;
115 }
116
117 @Override
alshabib35edb1a2014-09-16 17:44:44 -0700118 public L3SubType subtype() {
alshabib7410fea2014-09-16 13:48:39 -0700119 return this.subtype;
120 }
121
Pavlin Radoslavov855ea2d2014-10-30 15:32:39 -0700122 public IpAddress ip() {
alshabib7410fea2014-09-16 13:48:39 -0700123 return this.ip;
124 }
125
alshabib99b8fdc2014-09-25 14:30:22 -0700126 @Override
127 public String toString() {
128 return toStringHelper(subtype().toString())
129 .add("ip", ip).toString();
130 }
131
alshabib8ca53902014-10-07 13:11:17 -0700132 @Override
133 public int hashCode() {
Thomas Vachuska9b2da212014-11-10 19:30:25 -0800134 return Objects.hash(type(), subtype(), ip);
alshabib8ca53902014-10-07 13:11:17 -0700135 }
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;
alshabib2020b892014-10-20 15:47:08 -0700144 return Objects.equals(ip, that.ip) &&
alshabib2020b892014-10-20 15:47:08 -0700145 Objects.equals(this.subtype(), that.subtype());
alshabib8ca53902014-10-07 13:11:17 -0700146 }
147 return false;
148 }
sangho3f97a17d2015-01-29 22:56:29 -0800149 }
alshabib8ca53902014-10-07 13:11:17 -0700150
Yuta HIGUCHIcca5e722015-02-05 14:00:41 -0800151 /**
lishuai3cce60b2015-12-01 19:35:16 +0800152 * 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 Radoslavovfebe82c2015-02-11 19:08:15 -0800296 * 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 Vachuska8ab196c2015-02-17 23:53:48 -0800305 * Creates a new flow mod instruction.
Pavlin Radoslavovfebe82c2015-02-11 19:08:15 -0800306 *
Thomas Vachuska8ab196c2015-02-17 23:53:48 -0800307 * @param flowLabel the IPv6 flow label to set in the treatment (20 bits)
Pavlin Radoslavovfebe82c2015-02-11 19:08:15 -0800308 */
Yuta HIGUCHI6a479642015-02-08 01:28:50 -0800309 ModIPv6FlowLabelInstruction(int flowLabel) {
Pavlin Radoslavovfebe82c2015-02-11 19:08:15 -0800310 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 HIGUCHI6a479642015-02-08 01:28:50 -0800346 return Objects.equals(flowLabel, that.flowLabel);
Pavlin Radoslavovfebe82c2015-02-11 19:08:15 -0800347 }
348 return false;
349 }
350 }
351
352 /**
Yuta HIGUCHIcca5e722015-02-05 14:00:41 -0800353 * Represents a L3 TTL modification instruction.
354 */
sangho3f97a17d2015-01-29 22:56:29 -0800355 public static final class ModTtlInstruction extends L3ModificationInstruction {
356
357 private final L3SubType subtype;
358
Yuta HIGUCHI6a479642015-02-08 01:28:50 -0800359 ModTtlInstruction(L3SubType subtype) {
sangho3f97a17d2015-01-29 22:56:29 -0800360 this.subtype = subtype;
361 }
362
363 @Override
364 public L3SubType subtype() {
365 return this.subtype;
366 }
367
368 @Override
369 public String toString() {
Yuta HIGUCHIcca5e722015-02-05 14:00:41 -0800370 return toStringHelper(subtype().toString())
371 .toString();
sangho3f97a17d2015-01-29 22:56:29 -0800372 }
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 HIGUCHIcca5e722015-02-05 14:00:41 -0800384 if (obj instanceof ModTtlInstruction) {
385 ModTtlInstruction that = (ModTtlInstruction) obj;
Yuta HIGUCHI6a479642015-02-08 01:28:50 -0800386 return Objects.equals(this.subtype(), that.subtype());
sangho3f97a17d2015-01-29 22:56:29 -0800387 }
388 return false;
389 }
alshabib7410fea2014-09-16 13:48:39 -0700390 }
alshabib7410fea2014-09-16 13:48:39 -0700391}