blob: bfcc58892ba22233beef9c61c009b1fbe95a83ef [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;
alshabib55a55d92014-09-16 11:59:31 -070017
alshabib7b808c52015-06-26 14:22:24 -070018import org.onlab.packet.EthType;
Ayaka Koshibea9c199f2014-09-16 16:21:40 -070019import org.onlab.packet.MacAddress;
Michele Santuari4b6019e2014-12-19 11:31:45 +010020import org.onlab.packet.MplsLabel;
Ayaka Koshibea9c199f2014-09-16 16:21:40 -070021import org.onlab.packet.VlanId;
alshabib55a55d92014-09-16 11:59:31 -070022
Jonathan Hart54b406b2015-03-06 16:24:14 -080023import java.util.Objects;
24
alshabib55a55d92014-09-16 11:59:31 -070025/**
26 * Abstraction of a single traffic treatment step.
alshabib55a55d92014-09-16 11:59:31 -070027 */
28public abstract class L2ModificationInstruction implements Instruction {
29
Jonathan Hartc7840bd2016-01-21 23:26:29 -080030 private static final String SEPARATOR = ":";
31
alshabib55a55d92014-09-16 11:59:31 -070032 /**
33 * Represents the type of traffic treatment.
34 */
alshabib35edb1a2014-09-16 17:44:44 -070035 public enum L2SubType {
alshabib55a55d92014-09-16 11:59:31 -070036 /**
37 * Ether src modification.
38 */
alshabib99b8fdc2014-09-25 14:30:22 -070039 ETH_SRC,
alshabib55a55d92014-09-16 11:59:31 -070040
41 /**
42 * Ether dst modification.
43 */
alshabib99b8fdc2014-09-25 14:30:22 -070044 ETH_DST,
alshabib55a55d92014-09-16 11:59:31 -070045
46 /**
alshabib55a55d92014-09-16 11:59:31 -070047 * VLAN id modification.
48 */
49 VLAN_ID,
50
51 /**
52 * VLAN priority modification.
53 */
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -080054 VLAN_PCP,
55
56 /**
57 * MPLS Label modification.
58 */
59 MPLS_LABEL,
60
61 /**
62 * MPLS Push modification.
63 */
64 MPLS_PUSH,
65
66 /**
67 * MPLS Pop modification.
68 */
sangho3f97a17d2015-01-29 22:56:29 -080069 MPLS_POP,
70
71 /**
72 * MPLS TTL modification.
73 */
Saurav Dasfbe25c52015-03-04 11:12:00 -080074 DEC_MPLS_TTL,
sangho3f97a17d2015-01-29 22:56:29 -080075
Saurav Dasfbe25c52015-03-04 11:12:00 -080076 /**
77 * VLAN Pop modification.
78 */
Jonathan Hart54b406b2015-03-06 16:24:14 -080079 VLAN_POP,
80
81 /**
82 * VLAN Push modification.
83 */
Hyunsun Moona08c5d02015-07-14 17:53:00 -070084 VLAN_PUSH,
85
86 /**
Saurav Das73a7dd42015-08-19 22:20:31 -070087 * Tunnel id modification.
Hyunsun Moona08c5d02015-07-14 17:53:00 -070088 */
Saurav Das73a7dd42015-08-19 22:20:31 -070089 TUNNEL_ID,
90
91 /**
92 * MPLS BOS instruction.
93 */
94 MPLS_BOS
alshabib55a55d92014-09-16 11:59:31 -070095 }
96
alshabib35edb1a2014-09-16 17:44:44 -070097 public abstract L2SubType subtype();
alshabib55a55d92014-09-16 11:59:31 -070098
99 @Override
Yuta HIGUCHI6a479642015-02-08 01:28:50 -0800100 public final Type type() {
alshabib35edb1a2014-09-16 17:44:44 -0700101 return Type.L2MODIFICATION;
alshabib55a55d92014-09-16 11:59:31 -0700102 }
103
104 /**
105 * Represents a L2 src/dst modification instruction.
106 */
107 public static final class ModEtherInstruction extends L2ModificationInstruction {
108
alshabib35edb1a2014-09-16 17:44:44 -0700109 private final L2SubType subtype;
Ayaka Koshibea9c199f2014-09-16 16:21:40 -0700110 private final MacAddress mac;
alshabib55a55d92014-09-16 11:59:31 -0700111
Yuta HIGUCHI6a479642015-02-08 01:28:50 -0800112 ModEtherInstruction(L2SubType subType, MacAddress addr) {
alshabib64231f62014-09-16 17:58:36 -0700113
alshabib55a55d92014-09-16 11:59:31 -0700114 this.subtype = subType;
115 this.mac = addr;
116 }
117
118 @Override
alshabib35edb1a2014-09-16 17:44:44 -0700119 public L2SubType subtype() {
alshabib55a55d92014-09-16 11:59:31 -0700120 return this.subtype;
121 }
122
Ayaka Koshibea9c199f2014-09-16 16:21:40 -0700123 public MacAddress mac() {
alshabib55a55d92014-09-16 11:59:31 -0700124 return this.mac;
125 }
126
alshabib99b8fdc2014-09-25 14:30:22 -0700127 @Override
128 public String toString() {
Jonathan Hartc7840bd2016-01-21 23:26:29 -0800129 return subtype().toString() + SEPARATOR + mac;
alshabib99b8fdc2014-09-25 14:30:22 -0700130 }
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, mac);
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 ModEtherInstruction) {
143 ModEtherInstruction that = (ModEtherInstruction) obj;
144 return Objects.equals(mac, that.mac) &&
145 Objects.equals(subtype, that.subtype);
alshabib8ca53902014-10-07 13:11:17 -0700146 }
147 return false;
148 }
alshabib55a55d92014-09-16 11:59:31 -0700149 }
150
Jian Liddfd2eb2016-05-19 11:32:18 -0700151 /**
Jian Liddfd2eb2016-05-19 11:32:18 -0700152 * Represents a MPLS header modification instruction.
153 */
154 public static final class ModMplsHeaderInstruction extends L2ModificationInstruction {
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -0800155
Jian Liddfd2eb2016-05-19 11:32:18 -0700156 private final L2SubType subtype;
157 private final EthType ethernetType; // Ethernet type value: 16 bits
158
159 ModMplsHeaderInstruction(L2SubType subType, EthType ethernetType) {
160 this.subtype = subType;
161 this.ethernetType = ethernetType;
162 }
163
164 public EthType ethernetType() {
165 return ethernetType;
166 }
167
168 @Override
169 public L2SubType subtype() {
170 return subtype;
171 }
172
173 @Override
174 public String toString() {
175 return subtype().toString() + SEPARATOR + ethernetType;
176 }
177
178 @Override
179 public int hashCode() {
180 return Objects.hash(type(), subtype, ethernetType);
181 }
182
183 @Override
184 public boolean equals(Object obj) {
185 if (this == obj) {
186 return true;
187 }
188 if (obj instanceof ModMplsHeaderInstruction) {
189 ModMplsHeaderInstruction that = (ModMplsHeaderInstruction) obj;
190 return Objects.equals(subtype, that.subtype) &&
191 Objects.equals(this.ethernetType, that.ethernetType);
192 }
193 return false;
194 }
195 }
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -0800196
alshabib55a55d92014-09-16 11:59:31 -0700197 /**
alshabib55a55d92014-09-16 11:59:31 -0700198 * Represents a VLAN id modification instruction.
199 */
200 public static final class ModVlanIdInstruction extends L2ModificationInstruction {
201
Ray Milkey78081052014-11-05 10:38:12 -0800202 private final VlanId vlanId;
alshabib55a55d92014-09-16 11:59:31 -0700203
Yuta HIGUCHI6a479642015-02-08 01:28:50 -0800204 ModVlanIdInstruction(VlanId vlanId) {
alshabib55a55d92014-09-16 11:59:31 -0700205 this.vlanId = vlanId;
206 }
207
208 @Override
alshabib35edb1a2014-09-16 17:44:44 -0700209 public L2SubType subtype() {
alshabib7410fea2014-09-16 13:48:39 -0700210 return L2SubType.VLAN_ID;
alshabib55a55d92014-09-16 11:59:31 -0700211 }
212
Ayaka Koshibea9c199f2014-09-16 16:21:40 -0700213 public VlanId vlanId() {
alshabib55a55d92014-09-16 11:59:31 -0700214 return this.vlanId;
215 }
216
alshabib99b8fdc2014-09-25 14:30:22 -0700217 @Override
218 public String toString() {
Jonathan Hartc7840bd2016-01-21 23:26:29 -0800219 return subtype().toString() + SEPARATOR + vlanId;
alshabib99b8fdc2014-09-25 14:30:22 -0700220 }
221
alshabib8ca53902014-10-07 13:11:17 -0700222 @Override
223 public int hashCode() {
Thomas Vachuska9b2da212014-11-10 19:30:25 -0800224 return Objects.hash(type(), subtype(), vlanId);
alshabib8ca53902014-10-07 13:11:17 -0700225 }
226
227 @Override
228 public boolean equals(Object obj) {
229 if (this == obj) {
230 return true;
231 }
232 if (obj instanceof ModVlanIdInstruction) {
233 ModVlanIdInstruction that = (ModVlanIdInstruction) obj;
Yuta HIGUCHI6a479642015-02-08 01:28:50 -0800234 return Objects.equals(vlanId, that.vlanId);
alshabib8ca53902014-10-07 13:11:17 -0700235 }
236 return false;
237 }
alshabib55a55d92014-09-16 11:59:31 -0700238 }
239
alshabib7410fea2014-09-16 13:48:39 -0700240 /**
241 * Represents a VLAN PCP modification instruction.
242 */
243 public static final class ModVlanPcpInstruction extends L2ModificationInstruction {
244
alshabib0ad43982015-05-07 13:43:13 -0700245 private static final byte MASK = 0x7;
246 private final byte vlanPcp;
alshabib7410fea2014-09-16 13:48:39 -0700247
alshabib0ad43982015-05-07 13:43:13 -0700248 ModVlanPcpInstruction(byte vlanPcp) {
249 this.vlanPcp = (byte) (vlanPcp & MASK);
alshabib7410fea2014-09-16 13:48:39 -0700250 }
251
252 @Override
alshabib35edb1a2014-09-16 17:44:44 -0700253 public L2SubType subtype() {
alshabib7410fea2014-09-16 13:48:39 -0700254 return L2SubType.VLAN_PCP;
255 }
256
alshabib0ad43982015-05-07 13:43:13 -0700257 public byte vlanPcp() {
alshabib7410fea2014-09-16 13:48:39 -0700258 return this.vlanPcp;
259 }
260
alshabib99b8fdc2014-09-25 14:30:22 -0700261 @Override
262 public String toString() {
Jonathan Hartc7840bd2016-01-21 23:26:29 -0800263 return subtype().toString() + SEPARATOR + Long.toHexString(vlanPcp);
alshabib99b8fdc2014-09-25 14:30:22 -0700264 }
265
alshabib8ca53902014-10-07 13:11:17 -0700266 @Override
267 public int hashCode() {
Thomas Vachuska9b2da212014-11-10 19:30:25 -0800268 return Objects.hash(type(), subtype(), vlanPcp);
alshabib8ca53902014-10-07 13:11:17 -0700269 }
270
271 @Override
272 public boolean equals(Object obj) {
273 if (this == obj) {
274 return true;
275 }
276 if (obj instanceof ModVlanPcpInstruction) {
277 ModVlanPcpInstruction that = (ModVlanPcpInstruction) obj;
Yuta HIGUCHI6a479642015-02-08 01:28:50 -0800278 return Objects.equals(vlanPcp, that.vlanPcp);
alshabib8ca53902014-10-07 13:11:17 -0700279 }
280 return false;
281 }
alshabib7410fea2014-09-16 13:48:39 -0700282 }
283
Saurav Dasfbe25c52015-03-04 11:12:00 -0800284 /**
Jian Liddfd2eb2016-05-19 11:32:18 -0700285 * Represents a VLAN Header modification instruction.
286 */
287 public static final class ModVlanHeaderInstruction extends L2ModificationInstruction {
288
289 private final L2SubType subtype;
290 private EthType ethernetType; // Ethernet type value: 16 bits
291
292 ModVlanHeaderInstruction(L2SubType subType, EthType ethernetType) {
293 this.subtype = subType;
294 this.ethernetType = ethernetType;
295 }
296
297 ModVlanHeaderInstruction(L2SubType subType) {
298 this(subType, EthType.EtherType.UNKNOWN.ethType());
299 }
300
301 public EthType ethernetType() {
302 return ethernetType;
303 }
304
305 @Override
306 public L2SubType subtype() {
307 return subtype;
308 }
309
310 @Override
311 public String toString() {
312 return subtype().toString() + SEPARATOR + ethernetType;
313 }
314
315 @Override
316 public int hashCode() {
317 return Objects.hash(type(), subtype, ethernetType);
318 }
319
320 @Override
321 public boolean equals(Object obj) {
322 if (this == obj) {
323 return true;
324 }
325 if (obj instanceof ModVlanHeaderInstruction) {
326 ModVlanHeaderInstruction that = (ModVlanHeaderInstruction) obj;
327 return Objects.equals(subtype, that.subtype) &&
328 Objects.equals(this.ethernetType, that.ethernetType);
329 }
330 return false;
331 }
332 }
333
334 /**
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -0800335 * Represents a MPLS label modification.
336 */
Yuta HIGUCHI6a479642015-02-08 01:28:50 -0800337 public static final class ModMplsLabelInstruction
338 extends L2ModificationInstruction {
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -0800339
Michele Santuari4b6019e2014-12-19 11:31:45 +0100340 private final MplsLabel mplsLabel;
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -0800341
Yuta HIGUCHI6a479642015-02-08 01:28:50 -0800342 ModMplsLabelInstruction(MplsLabel mplsLabel) {
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -0800343 this.mplsLabel = mplsLabel;
344 }
345
Ray Milkey125572b2016-02-22 16:48:17 -0800346 public MplsLabel label() {
347 return mplsLabel;
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -0800348 }
349
Ray Milkey125572b2016-02-22 16:48:17 -0800350 /**
351 * Extracts the MPLS label from the instruction.
352 *
353 * @return MPLS label
354 * @deprecated deprecated in 1.5.0 Falcon
355 */
356 @Deprecated
HIGUCHI Yuta60db9772015-08-26 17:49:34 -0700357 public MplsLabel mplsLabel() {
Ray Milkey125572b2016-02-22 16:48:17 -0800358 return label();
HIGUCHI Yuta60db9772015-08-26 17:49:34 -0700359 }
360
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -0800361 @Override
362 public L2SubType subtype() {
363 return L2SubType.MPLS_LABEL;
364 }
365
366 @Override
367 public String toString() {
Jonathan Hartc7840bd2016-01-21 23:26:29 -0800368 return subtype().toString() + SEPARATOR + mplsLabel;
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -0800369 }
370
371 @Override
372 public int hashCode() {
Yuta HIGUCHIcca5e722015-02-05 14:00:41 -0800373 return Objects.hash(type(), subtype(), mplsLabel);
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -0800374 }
375
376 @Override
377 public boolean equals(Object obj) {
378 if (this == obj) {
379 return true;
380 }
381 if (obj instanceof ModMplsLabelInstruction) {
382 ModMplsLabelInstruction that = (ModMplsLabelInstruction) obj;
Yuta HIGUCHI6a479642015-02-08 01:28:50 -0800383 return Objects.equals(mplsLabel, that.mplsLabel);
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -0800384 }
385 return false;
386 }
387 }
sangho3f97a17d2015-01-29 22:56:29 -0800388
389 /**
Saurav Das73a7dd42015-08-19 22:20:31 -0700390 * Represents a MPLS BOS modification.
391 */
392 public static final class ModMplsBosInstruction
393 extends L2ModificationInstruction {
394
395 private final boolean mplsBos;
396
397 ModMplsBosInstruction(boolean mplsBos) {
398 this.mplsBos = mplsBos;
399 }
400
401 public boolean mplsBos() {
402 return mplsBos;
403 }
404
405 @Override
406 public L2SubType subtype() {
407 return L2SubType.MPLS_BOS;
408 }
409
410 @Override
411 public String toString() {
Jonathan Hartc7840bd2016-01-21 23:26:29 -0800412 return subtype().toString() + SEPARATOR + mplsBos;
Saurav Das73a7dd42015-08-19 22:20:31 -0700413 }
414
415 @Override
416 public int hashCode() {
417 return Objects.hash(type(), subtype(), mplsBos);
418 }
419
420 @Override
421 public boolean equals(Object obj) {
422 if (this == obj) {
423 return true;
424 }
425 if (obj instanceof ModMplsBosInstruction) {
426 ModMplsBosInstruction that = (ModMplsBosInstruction) obj;
427 return Objects.equals(mplsBos, that.mplsBos());
428 }
429 return false;
430 }
431 }
432
433 /**
Yuta HIGUCHIcca5e722015-02-05 14:00:41 -0800434 * Represents a MPLS TTL modification.
sangho3f97a17d2015-01-29 22:56:29 -0800435 */
Yuta HIGUCHI6a479642015-02-08 01:28:50 -0800436 public static final class ModMplsTtlInstruction
437 extends L2ModificationInstruction {
sangho3f97a17d2015-01-29 22:56:29 -0800438
Yuta HIGUCHI6a479642015-02-08 01:28:50 -0800439 ModMplsTtlInstruction() {
sangho3f97a17d2015-01-29 22:56:29 -0800440 }
441
442 @Override
443 public L2SubType subtype() {
444 return L2SubType.DEC_MPLS_TTL;
445 }
446
447 @Override
448 public String toString() {
Jonathan Hartc7840bd2016-01-21 23:26:29 -0800449 return subtype().toString();
sangho3f97a17d2015-01-29 22:56:29 -0800450 }
451
452 @Override
453 public int hashCode() {
Yuta HIGUCHIcca5e722015-02-05 14:00:41 -0800454 return Objects.hash(type(), subtype());
sangho3f97a17d2015-01-29 22:56:29 -0800455 }
456
457 @Override
458 public boolean equals(Object obj) {
459 if (this == obj) {
460 return true;
461 }
Yuta HIGUCHIcca5e722015-02-05 14:00:41 -0800462 if (obj instanceof ModMplsTtlInstruction) {
463 return true;
sangho3f97a17d2015-01-29 22:56:29 -0800464 }
465 return false;
466 }
467 }
Hyunsun Moona08c5d02015-07-14 17:53:00 -0700468
469 /**
470 * Represents a Tunnel id modification.
471 */
472 public static final class ModTunnelIdInstruction
473 extends L2ModificationInstruction {
474
475 private final long tunnelId;
476
477 ModTunnelIdInstruction(long tunnelId) {
478 this.tunnelId = tunnelId;
479 }
480
481 public long tunnelId() {
482 return this.tunnelId;
483 }
484
485 @Override
486 public L2SubType subtype() {
487 return L2SubType.TUNNEL_ID;
488 }
489
490 @Override
491 public String toString() {
Jonathan Hartc7840bd2016-01-21 23:26:29 -0800492 return subtype().toString() + SEPARATOR + Long.toHexString(tunnelId);
Hyunsun Moona08c5d02015-07-14 17:53:00 -0700493 }
494
495 @Override
496 public int hashCode() {
497 return Objects.hash(type(), subtype(), tunnelId);
498 }
499
500 @Override
501 public boolean equals(Object obj) {
502 if (this == obj) {
503 return true;
504 }
505 if (obj instanceof ModTunnelIdInstruction) {
506 ModTunnelIdInstruction that = (ModTunnelIdInstruction) obj;
507 return Objects.equals(tunnelId, that.tunnelId);
508 }
509 return false;
510 }
511 }
alshabib55a55d92014-09-16 11:59:31 -0700512}