blob: 2b11584fe263e0a3963d48d67f67fe476c166807 [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
Yuta HIGUCHI6a479642015-02-08 01:28:50 -0800151 // TODO This instruction is reused for Pop-Mpls. Consider renaming.
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -0800152 public static final class PushHeaderInstructions extends
153 L2ModificationInstruction {
154
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -0800155
alshabib7b808c52015-06-26 14:22:24 -0700156 private final L2SubType subtype;
157 private final EthType ethernetType; // Ethernet type value: 16 bits
158
159 PushHeaderInstructions(L2SubType subType, EthType ethernetType) {
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -0800160 this.subtype = subType;
alshabib7b808c52015-06-26 14:22:24 -0700161 this.ethernetType = ethernetType;
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -0800162 }
163
alshabib7b808c52015-06-26 14:22:24 -0700164 public EthType ethernetType() {
alshabib0ad43982015-05-07 13:43:13 -0700165 return ethernetType;
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -0800166 }
167
168 @Override
169 public L2SubType subtype() {
170 return this.subtype;
171 }
172
173 @Override
174 public String toString() {
Jonathan Hartc7840bd2016-01-21 23:26:29 -0800175 return subtype().toString() + SEPARATOR + ethernetType;
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -0800176 }
177
178 @Override
179 public int hashCode() {
Yuta HIGUCHI32a53c52015-02-08 01:25:40 -0800180 return Objects.hash(type(), subtype, ethernetType);
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -0800181 }
182
183 @Override
184 public boolean equals(Object obj) {
185 if (this == obj) {
186 return true;
187 }
188 if (obj instanceof PushHeaderInstructions) {
189 PushHeaderInstructions that = (PushHeaderInstructions) obj;
Yuta HIGUCHI32a53c52015-02-08 01:25:40 -0800190 return Objects.equals(subtype, that.subtype) &&
191 Objects.equals(this.ethernetType, that.ethernetType);
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -0800192 }
193 return false;
194 }
195 }
196
197
198
alshabib55a55d92014-09-16 11:59:31 -0700199 /**
alshabib55a55d92014-09-16 11:59:31 -0700200 * Represents a VLAN id modification instruction.
201 */
202 public static final class ModVlanIdInstruction extends L2ModificationInstruction {
203
Ray Milkey78081052014-11-05 10:38:12 -0800204 private final VlanId vlanId;
alshabib55a55d92014-09-16 11:59:31 -0700205
Yuta HIGUCHI6a479642015-02-08 01:28:50 -0800206 ModVlanIdInstruction(VlanId vlanId) {
alshabib55a55d92014-09-16 11:59:31 -0700207 this.vlanId = vlanId;
208 }
209
210 @Override
alshabib35edb1a2014-09-16 17:44:44 -0700211 public L2SubType subtype() {
alshabib7410fea2014-09-16 13:48:39 -0700212 return L2SubType.VLAN_ID;
alshabib55a55d92014-09-16 11:59:31 -0700213 }
214
Ayaka Koshibea9c199f2014-09-16 16:21:40 -0700215 public VlanId vlanId() {
alshabib55a55d92014-09-16 11:59:31 -0700216 return this.vlanId;
217 }
218
alshabib99b8fdc2014-09-25 14:30:22 -0700219 @Override
220 public String toString() {
Jonathan Hartc7840bd2016-01-21 23:26:29 -0800221 return subtype().toString() + SEPARATOR + vlanId;
alshabib99b8fdc2014-09-25 14:30:22 -0700222 }
223
alshabib8ca53902014-10-07 13:11:17 -0700224 @Override
225 public int hashCode() {
Thomas Vachuska9b2da212014-11-10 19:30:25 -0800226 return Objects.hash(type(), subtype(), vlanId);
alshabib8ca53902014-10-07 13:11:17 -0700227 }
228
229 @Override
230 public boolean equals(Object obj) {
231 if (this == obj) {
232 return true;
233 }
234 if (obj instanceof ModVlanIdInstruction) {
235 ModVlanIdInstruction that = (ModVlanIdInstruction) obj;
Yuta HIGUCHI6a479642015-02-08 01:28:50 -0800236 return Objects.equals(vlanId, that.vlanId);
alshabib8ca53902014-10-07 13:11:17 -0700237 }
238 return false;
239 }
alshabib55a55d92014-09-16 11:59:31 -0700240 }
241
alshabib7410fea2014-09-16 13:48:39 -0700242 /**
243 * Represents a VLAN PCP modification instruction.
244 */
245 public static final class ModVlanPcpInstruction extends L2ModificationInstruction {
246
alshabib0ad43982015-05-07 13:43:13 -0700247 private static final byte MASK = 0x7;
248 private final byte vlanPcp;
alshabib7410fea2014-09-16 13:48:39 -0700249
alshabib0ad43982015-05-07 13:43:13 -0700250 ModVlanPcpInstruction(byte vlanPcp) {
251 this.vlanPcp = (byte) (vlanPcp & MASK);
alshabib7410fea2014-09-16 13:48:39 -0700252 }
253
254 @Override
alshabib35edb1a2014-09-16 17:44:44 -0700255 public L2SubType subtype() {
alshabib7410fea2014-09-16 13:48:39 -0700256 return L2SubType.VLAN_PCP;
257 }
258
alshabib0ad43982015-05-07 13:43:13 -0700259 public byte vlanPcp() {
alshabib7410fea2014-09-16 13:48:39 -0700260 return this.vlanPcp;
261 }
262
alshabib99b8fdc2014-09-25 14:30:22 -0700263 @Override
264 public String toString() {
Jonathan Hartc7840bd2016-01-21 23:26:29 -0800265 return subtype().toString() + SEPARATOR + Long.toHexString(vlanPcp);
alshabib99b8fdc2014-09-25 14:30:22 -0700266 }
267
alshabib8ca53902014-10-07 13:11:17 -0700268 @Override
269 public int hashCode() {
Thomas Vachuska9b2da212014-11-10 19:30:25 -0800270 return Objects.hash(type(), subtype(), vlanPcp);
alshabib8ca53902014-10-07 13:11:17 -0700271 }
272
273 @Override
274 public boolean equals(Object obj) {
275 if (this == obj) {
276 return true;
277 }
278 if (obj instanceof ModVlanPcpInstruction) {
279 ModVlanPcpInstruction that = (ModVlanPcpInstruction) obj;
Yuta HIGUCHI6a479642015-02-08 01:28:50 -0800280 return Objects.equals(vlanPcp, that.vlanPcp);
alshabib8ca53902014-10-07 13:11:17 -0700281 }
282 return false;
283 }
alshabib7410fea2014-09-16 13:48:39 -0700284 }
285
Saurav Dasfbe25c52015-03-04 11:12:00 -0800286 /**
287 * Represents a VLAN POP modification instruction.
288 */
289 public static final class PopVlanInstruction extends L2ModificationInstruction {
290 private final L2SubType subtype;
291
292 PopVlanInstruction(L2SubType subType) {
293 this.subtype = subType;
294 }
295
296 @Override
297 public L2SubType subtype() {
298 return subtype;
299 }
300
301 @Override
302 public String toString() {
Jonathan Hartc7840bd2016-01-21 23:26:29 -0800303 return subtype().toString();
Saurav Dasfbe25c52015-03-04 11:12:00 -0800304 }
305
306 @Override
307 public int hashCode() {
308 return Objects.hash(type(), subtype);
309 }
310
311 @Override
312 public boolean equals(Object obj) {
313 if (this == obj) {
314 return true;
315 }
HIGUCHI Yuta7bb8b3f2015-03-13 23:21:37 -0700316 if (obj instanceof PopVlanInstruction) {
317 PopVlanInstruction that = (PopVlanInstruction) obj;
Saurav Dasfbe25c52015-03-04 11:12:00 -0800318 return Objects.equals(subtype, that.subtype);
319 }
320 return false;
321 }
322 }
alshabib55a55d92014-09-16 11:59:31 -0700323
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -0800324 /**
325 * Represents a MPLS label modification.
326 */
Yuta HIGUCHI6a479642015-02-08 01:28:50 -0800327 public static final class ModMplsLabelInstruction
328 extends L2ModificationInstruction {
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -0800329
Michele Santuari4b6019e2014-12-19 11:31:45 +0100330 private final MplsLabel mplsLabel;
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -0800331
Yuta HIGUCHI6a479642015-02-08 01:28:50 -0800332 ModMplsLabelInstruction(MplsLabel mplsLabel) {
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -0800333 this.mplsLabel = mplsLabel;
334 }
335
Ray Milkey125572b2016-02-22 16:48:17 -0800336 public MplsLabel label() {
337 return mplsLabel;
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -0800338 }
339
Ray Milkey125572b2016-02-22 16:48:17 -0800340 /**
341 * Extracts the MPLS label from the instruction.
342 *
343 * @return MPLS label
344 * @deprecated deprecated in 1.5.0 Falcon
345 */
346 @Deprecated
HIGUCHI Yuta60db9772015-08-26 17:49:34 -0700347 public MplsLabel mplsLabel() {
Ray Milkey125572b2016-02-22 16:48:17 -0800348 return label();
HIGUCHI Yuta60db9772015-08-26 17:49:34 -0700349 }
350
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -0800351 @Override
352 public L2SubType subtype() {
353 return L2SubType.MPLS_LABEL;
354 }
355
356 @Override
357 public String toString() {
Jonathan Hartc7840bd2016-01-21 23:26:29 -0800358 return subtype().toString() + SEPARATOR + mplsLabel;
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -0800359 }
360
361 @Override
362 public int hashCode() {
Yuta HIGUCHIcca5e722015-02-05 14:00:41 -0800363 return Objects.hash(type(), subtype(), mplsLabel);
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -0800364 }
365
366 @Override
367 public boolean equals(Object obj) {
368 if (this == obj) {
369 return true;
370 }
371 if (obj instanceof ModMplsLabelInstruction) {
372 ModMplsLabelInstruction that = (ModMplsLabelInstruction) obj;
Yuta HIGUCHI6a479642015-02-08 01:28:50 -0800373 return Objects.equals(mplsLabel, that.mplsLabel);
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -0800374 }
375 return false;
376 }
377 }
sangho3f97a17d2015-01-29 22:56:29 -0800378
379 /**
Saurav Das73a7dd42015-08-19 22:20:31 -0700380 * Represents a MPLS BOS modification.
381 */
382 public static final class ModMplsBosInstruction
383 extends L2ModificationInstruction {
384
385 private final boolean mplsBos;
386
387 ModMplsBosInstruction(boolean mplsBos) {
388 this.mplsBos = mplsBos;
389 }
390
391 public boolean mplsBos() {
392 return mplsBos;
393 }
394
395 @Override
396 public L2SubType subtype() {
397 return L2SubType.MPLS_BOS;
398 }
399
400 @Override
401 public String toString() {
Jonathan Hartc7840bd2016-01-21 23:26:29 -0800402 return subtype().toString() + SEPARATOR + mplsBos;
Saurav Das73a7dd42015-08-19 22:20:31 -0700403 }
404
405 @Override
406 public int hashCode() {
407 return Objects.hash(type(), subtype(), mplsBos);
408 }
409
410 @Override
411 public boolean equals(Object obj) {
412 if (this == obj) {
413 return true;
414 }
415 if (obj instanceof ModMplsBosInstruction) {
416 ModMplsBosInstruction that = (ModMplsBosInstruction) obj;
417 return Objects.equals(mplsBos, that.mplsBos());
418 }
419 return false;
420 }
421 }
422
423 /**
Yuta HIGUCHIcca5e722015-02-05 14:00:41 -0800424 * Represents a MPLS TTL modification.
sangho3f97a17d2015-01-29 22:56:29 -0800425 */
Yuta HIGUCHI6a479642015-02-08 01:28:50 -0800426 public static final class ModMplsTtlInstruction
427 extends L2ModificationInstruction {
sangho3f97a17d2015-01-29 22:56:29 -0800428
Yuta HIGUCHI6a479642015-02-08 01:28:50 -0800429 ModMplsTtlInstruction() {
sangho3f97a17d2015-01-29 22:56:29 -0800430 }
431
432 @Override
433 public L2SubType subtype() {
434 return L2SubType.DEC_MPLS_TTL;
435 }
436
437 @Override
438 public String toString() {
Jonathan Hartc7840bd2016-01-21 23:26:29 -0800439 return subtype().toString();
sangho3f97a17d2015-01-29 22:56:29 -0800440 }
441
442 @Override
443 public int hashCode() {
Yuta HIGUCHIcca5e722015-02-05 14:00:41 -0800444 return Objects.hash(type(), subtype());
sangho3f97a17d2015-01-29 22:56:29 -0800445 }
446
447 @Override
448 public boolean equals(Object obj) {
449 if (this == obj) {
450 return true;
451 }
Yuta HIGUCHIcca5e722015-02-05 14:00:41 -0800452 if (obj instanceof ModMplsTtlInstruction) {
453 return true;
sangho3f97a17d2015-01-29 22:56:29 -0800454 }
455 return false;
456 }
457 }
Hyunsun Moona08c5d02015-07-14 17:53:00 -0700458
459 /**
460 * Represents a Tunnel id modification.
461 */
462 public static final class ModTunnelIdInstruction
463 extends L2ModificationInstruction {
464
465 private final long tunnelId;
466
467 ModTunnelIdInstruction(long tunnelId) {
468 this.tunnelId = tunnelId;
469 }
470
471 public long tunnelId() {
472 return this.tunnelId;
473 }
474
475 @Override
476 public L2SubType subtype() {
477 return L2SubType.TUNNEL_ID;
478 }
479
480 @Override
481 public String toString() {
Jonathan Hartc7840bd2016-01-21 23:26:29 -0800482 return subtype().toString() + SEPARATOR + Long.toHexString(tunnelId);
Hyunsun Moona08c5d02015-07-14 17:53:00 -0700483 }
484
485 @Override
486 public int hashCode() {
487 return Objects.hash(type(), subtype(), tunnelId);
488 }
489
490 @Override
491 public boolean equals(Object obj) {
492 if (this == obj) {
493 return true;
494 }
495 if (obj instanceof ModTunnelIdInstruction) {
496 ModTunnelIdInstruction that = (ModTunnelIdInstruction) obj;
497 return Objects.equals(tunnelId, that.tunnelId);
498 }
499 return false;
500 }
501 }
alshabib55a55d92014-09-16 11:59:31 -0700502}