blob: 2a93b4515948f330b703ebed9d7272ce5eab2ee4 [file] [log] [blame]
Thomas Vachuska83e090e2014-10-22 14:25:35 -07001/*
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07002 * Copyright 2014 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
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
Ayaka Koshibea9c199f2014-09-16 16:21:40 -070022import org.onlab.packet.MacAddress;
Michele Santuari4b6019e2014-12-19 11:31:45 +010023import org.onlab.packet.MplsLabel;
Ayaka Koshibea9c199f2014-09-16 16:21:40 -070024import org.onlab.packet.VlanId;
alshabib55a55d92014-09-16 11:59:31 -070025
26/**
27 * Abstraction of a single traffic treatment step.
alshabib55a55d92014-09-16 11:59:31 -070028 */
29public abstract class L2ModificationInstruction implements Instruction {
30
31 /**
32 * Represents the type of traffic treatment.
33 */
alshabib35edb1a2014-09-16 17:44:44 -070034 public enum L2SubType {
alshabib55a55d92014-09-16 11:59:31 -070035 /**
36 * Ether src modification.
37 */
alshabib99b8fdc2014-09-25 14:30:22 -070038 ETH_SRC,
alshabib55a55d92014-09-16 11:59:31 -070039
40 /**
41 * Ether dst modification.
42 */
alshabib99b8fdc2014-09-25 14:30:22 -070043 ETH_DST,
alshabib55a55d92014-09-16 11:59:31 -070044
45 /**
alshabib55a55d92014-09-16 11:59:31 -070046 * VLAN id modification.
47 */
48 VLAN_ID,
49
50 /**
51 * VLAN priority modification.
52 */
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -080053 VLAN_PCP,
54
55 /**
alshabibab21b2d2015-03-04 18:35:33 -080056 * Strips the vlan.
57 */
58 STRIP_VLAN,
59
60 /**
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -080061 * MPLS Label modification.
62 */
63 MPLS_LABEL,
64
65 /**
66 * MPLS Push modification.
67 */
68 MPLS_PUSH,
69
70 /**
71 * MPLS Pop modification.
72 */
sangho3f97a17d2015-01-29 22:56:29 -080073 MPLS_POP,
74
75 /**
76 * MPLS TTL modification.
77 */
Saurav Dasfbe25c52015-03-04 11:12:00 -080078 DEC_MPLS_TTL,
sangho3f97a17d2015-01-29 22:56:29 -080079
Saurav Dasfbe25c52015-03-04 11:12:00 -080080 /**
81 * VLAN Pop modification.
82 */
83 VLAN_POP
alshabib55a55d92014-09-16 11:59:31 -070084 }
85
86 // TODO: Create factory class 'Instructions' that will have various factory
87 // to create specific instructions.
88
alshabib35edb1a2014-09-16 17:44:44 -070089 public abstract L2SubType subtype();
alshabib55a55d92014-09-16 11:59:31 -070090
91 @Override
Yuta HIGUCHI6a479642015-02-08 01:28:50 -080092 public final Type type() {
alshabib35edb1a2014-09-16 17:44:44 -070093 return Type.L2MODIFICATION;
alshabib55a55d92014-09-16 11:59:31 -070094 }
95
96 /**
97 * Represents a L2 src/dst modification instruction.
98 */
99 public static final class ModEtherInstruction extends L2ModificationInstruction {
100
alshabib35edb1a2014-09-16 17:44:44 -0700101 private final L2SubType subtype;
Ayaka Koshibea9c199f2014-09-16 16:21:40 -0700102 private final MacAddress mac;
alshabib55a55d92014-09-16 11:59:31 -0700103
Yuta HIGUCHI6a479642015-02-08 01:28:50 -0800104 ModEtherInstruction(L2SubType subType, MacAddress addr) {
alshabib64231f62014-09-16 17:58:36 -0700105
alshabib55a55d92014-09-16 11:59:31 -0700106 this.subtype = subType;
107 this.mac = addr;
108 }
109
110 @Override
alshabib35edb1a2014-09-16 17:44:44 -0700111 public L2SubType subtype() {
alshabib55a55d92014-09-16 11:59:31 -0700112 return this.subtype;
113 }
114
Ayaka Koshibea9c199f2014-09-16 16:21:40 -0700115 public MacAddress mac() {
alshabib55a55d92014-09-16 11:59:31 -0700116 return this.mac;
117 }
118
alshabib99b8fdc2014-09-25 14:30:22 -0700119 @Override
120 public String toString() {
121 return toStringHelper(subtype().toString())
122 .add("mac", mac).toString();
123 }
124
alshabib8ca53902014-10-07 13:11:17 -0700125 @Override
126 public int hashCode() {
Thomas Vachuska9b2da212014-11-10 19:30:25 -0800127 return Objects.hash(type(), subtype, mac);
alshabib8ca53902014-10-07 13:11:17 -0700128 }
129
130 @Override
131 public boolean equals(Object obj) {
132 if (this == obj) {
133 return true;
134 }
135 if (obj instanceof ModEtherInstruction) {
136 ModEtherInstruction that = (ModEtherInstruction) obj;
137 return Objects.equals(mac, that.mac) &&
138 Objects.equals(subtype, that.subtype);
alshabib8ca53902014-10-07 13:11:17 -0700139 }
140 return false;
141 }
alshabib55a55d92014-09-16 11:59:31 -0700142 }
143
Yuta HIGUCHI6a479642015-02-08 01:28:50 -0800144 // TODO This instruction is reused for Pop-Mpls. Consider renaming.
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -0800145 public static final class PushHeaderInstructions extends
146 L2ModificationInstruction {
147
148 private final L2SubType subtype;
Yuta HIGUCHI32a53c52015-02-08 01:25:40 -0800149 private final short ethernetType; // uint16_t
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -0800150
Yuta HIGUCHI32a53c52015-02-08 01:25:40 -0800151 PushHeaderInstructions(L2SubType subType, short ethernetType) {
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -0800152 this.subtype = subType;
153 this.ethernetType = ethernetType;
154 }
155
Yuta HIGUCHI32a53c52015-02-08 01:25:40 -0800156 public int ethernetType() {
157 return Short.toUnsignedInt(ethernetType);
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -0800158 }
159
160 @Override
161 public L2SubType subtype() {
162 return this.subtype;
163 }
164
165 @Override
166 public String toString() {
Yuta HIGUCHI32a53c52015-02-08 01:25:40 -0800167 return toStringHelper(subtype().toString())
168 .add("ethernetType", String.format("0x%04x", ethernetType()))
169 .toString();
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -0800170 }
171
172 @Override
173 public int hashCode() {
Yuta HIGUCHI32a53c52015-02-08 01:25:40 -0800174 return Objects.hash(type(), subtype, ethernetType);
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -0800175 }
176
177 @Override
178 public boolean equals(Object obj) {
179 if (this == obj) {
180 return true;
181 }
182 if (obj instanceof PushHeaderInstructions) {
183 PushHeaderInstructions that = (PushHeaderInstructions) obj;
Yuta HIGUCHI32a53c52015-02-08 01:25:40 -0800184 return Objects.equals(subtype, that.subtype) &&
185 Objects.equals(this.ethernetType, that.ethernetType);
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -0800186 }
187 return false;
188 }
189 }
190
191
192
alshabib55a55d92014-09-16 11:59:31 -0700193 /**
alshabib55a55d92014-09-16 11:59:31 -0700194 * Represents a VLAN id modification instruction.
195 */
196 public static final class ModVlanIdInstruction extends L2ModificationInstruction {
197
Ray Milkey78081052014-11-05 10:38:12 -0800198 private final VlanId vlanId;
alshabib55a55d92014-09-16 11:59:31 -0700199
Yuta HIGUCHI6a479642015-02-08 01:28:50 -0800200 ModVlanIdInstruction(VlanId vlanId) {
alshabib55a55d92014-09-16 11:59:31 -0700201 this.vlanId = vlanId;
202 }
203
204 @Override
alshabib35edb1a2014-09-16 17:44:44 -0700205 public L2SubType subtype() {
alshabib7410fea2014-09-16 13:48:39 -0700206 return L2SubType.VLAN_ID;
alshabib55a55d92014-09-16 11:59:31 -0700207 }
208
Ayaka Koshibea9c199f2014-09-16 16:21:40 -0700209 public VlanId vlanId() {
alshabib55a55d92014-09-16 11:59:31 -0700210 return this.vlanId;
211 }
212
alshabib99b8fdc2014-09-25 14:30:22 -0700213 @Override
214 public String toString() {
215 return toStringHelper(subtype().toString())
216 .add("id", vlanId).toString();
217 }
218
alshabib8ca53902014-10-07 13:11:17 -0700219 @Override
220 public int hashCode() {
Thomas Vachuska9b2da212014-11-10 19:30:25 -0800221 return Objects.hash(type(), subtype(), vlanId);
alshabib8ca53902014-10-07 13:11:17 -0700222 }
223
224 @Override
225 public boolean equals(Object obj) {
226 if (this == obj) {
227 return true;
228 }
229 if (obj instanceof ModVlanIdInstruction) {
230 ModVlanIdInstruction that = (ModVlanIdInstruction) obj;
Yuta HIGUCHI6a479642015-02-08 01:28:50 -0800231 return Objects.equals(vlanId, that.vlanId);
alshabib8ca53902014-10-07 13:11:17 -0700232 }
233 return false;
234 }
alshabib55a55d92014-09-16 11:59:31 -0700235 }
236
alshabib7410fea2014-09-16 13:48:39 -0700237 /**
238 * Represents a VLAN PCP modification instruction.
239 */
240 public static final class ModVlanPcpInstruction extends L2ModificationInstruction {
241
Ray Milkey78081052014-11-05 10:38:12 -0800242 private final Byte vlanPcp;
alshabib7410fea2014-09-16 13:48:39 -0700243
Yuta HIGUCHI6a479642015-02-08 01:28:50 -0800244 ModVlanPcpInstruction(Byte vlanPcp) {
alshabib7410fea2014-09-16 13:48:39 -0700245 this.vlanPcp = vlanPcp;
246 }
247
248 @Override
alshabib35edb1a2014-09-16 17:44:44 -0700249 public L2SubType subtype() {
alshabib7410fea2014-09-16 13:48:39 -0700250 return L2SubType.VLAN_PCP;
251 }
252
253 public Byte vlanPcp() {
254 return this.vlanPcp;
255 }
256
alshabib99b8fdc2014-09-25 14:30:22 -0700257 @Override
258 public String toString() {
259 return toStringHelper(subtype().toString())
260 .add("pcp", Long.toHexString(vlanPcp)).toString();
261 }
262
alshabib8ca53902014-10-07 13:11:17 -0700263 @Override
264 public int hashCode() {
Thomas Vachuska9b2da212014-11-10 19:30:25 -0800265 return Objects.hash(type(), subtype(), vlanPcp);
alshabib8ca53902014-10-07 13:11:17 -0700266 }
267
268 @Override
269 public boolean equals(Object obj) {
270 if (this == obj) {
271 return true;
272 }
273 if (obj instanceof ModVlanPcpInstruction) {
274 ModVlanPcpInstruction that = (ModVlanPcpInstruction) obj;
Yuta HIGUCHI6a479642015-02-08 01:28:50 -0800275 return Objects.equals(vlanPcp, that.vlanPcp);
alshabib8ca53902014-10-07 13:11:17 -0700276 }
277 return false;
278 }
alshabib7410fea2014-09-16 13:48:39 -0700279 }
280
alshabibab21b2d2015-03-04 18:35:33 -0800281 public static final class StripVlanInstruction extends L2ModificationInstruction {
282
283 @Override
284 public L2SubType subtype() {
285 return L2SubType.STRIP_VLAN;
286 }
287
288 @Override
289 public String toString() {
290 return subtype().toString();
291 }
292
293 @Override
294 public int hashCode() {
295 return Objects.hash(type(), subtype());
296 }
297
298 @Override
299 public boolean equals(Object obj) {
300 if (this == obj) {
301 return true;
302 }
303
304 return false;
305 }
306 }
307
Saurav Dasfbe25c52015-03-04 11:12:00 -0800308 /**
309 * Represents a VLAN POP modification instruction.
310 */
311 public static final class PopVlanInstruction extends L2ModificationInstruction {
312 private final L2SubType subtype;
313
314 PopVlanInstruction(L2SubType subType) {
315 this.subtype = subType;
316 }
317
318 @Override
319 public L2SubType subtype() {
320 return subtype;
321 }
322
323 @Override
324 public String toString() {
325 return toStringHelper(subtype().toString())
326 .toString();
327 }
328
329 @Override
330 public int hashCode() {
331 return Objects.hash(type(), subtype);
332 }
333
334 @Override
335 public boolean equals(Object obj) {
336 if (this == obj) {
337 return true;
338 }
339 if (obj instanceof PushHeaderInstructions) {
340 PushHeaderInstructions that = (PushHeaderInstructions) obj;
341 return Objects.equals(subtype, that.subtype);
342 }
343 return false;
344 }
345 }
alshabib55a55d92014-09-16 11:59:31 -0700346
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -0800347 /**
348 * Represents a MPLS label modification.
349 */
Yuta HIGUCHI6a479642015-02-08 01:28:50 -0800350 public static final class ModMplsLabelInstruction
351 extends L2ModificationInstruction {
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -0800352
Michele Santuari4b6019e2014-12-19 11:31:45 +0100353 private final MplsLabel mplsLabel;
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -0800354
Yuta HIGUCHI6a479642015-02-08 01:28:50 -0800355 ModMplsLabelInstruction(MplsLabel mplsLabel) {
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -0800356 this.mplsLabel = mplsLabel;
357 }
358
359 public Integer label() {
Michele Santuari4b6019e2014-12-19 11:31:45 +0100360 return mplsLabel.toInt();
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -0800361 }
362
363 @Override
364 public L2SubType subtype() {
365 return L2SubType.MPLS_LABEL;
366 }
367
368 @Override
369 public String toString() {
Pavlin Radoslavov3ebe1702015-02-17 09:53:17 -0800370 return toStringHelper(subtype().toString())
Michele Santuari4b6019e2014-12-19 11:31:45 +0100371 .add("mpls", mplsLabel).toString();
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -0800372 }
373
374 @Override
375 public int hashCode() {
Yuta HIGUCHIcca5e722015-02-05 14:00:41 -0800376 return Objects.hash(type(), subtype(), mplsLabel);
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -0800377 }
378
379 @Override
380 public boolean equals(Object obj) {
381 if (this == obj) {
382 return true;
383 }
384 if (obj instanceof ModMplsLabelInstruction) {
385 ModMplsLabelInstruction that = (ModMplsLabelInstruction) obj;
Yuta HIGUCHI6a479642015-02-08 01:28:50 -0800386 return Objects.equals(mplsLabel, that.mplsLabel);
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -0800387 }
388 return false;
389 }
390 }
sangho3f97a17d2015-01-29 22:56:29 -0800391
392 /**
Yuta HIGUCHIcca5e722015-02-05 14:00:41 -0800393 * Represents a MPLS TTL modification.
sangho3f97a17d2015-01-29 22:56:29 -0800394 */
Yuta HIGUCHI6a479642015-02-08 01:28:50 -0800395 public static final class ModMplsTtlInstruction
396 extends L2ModificationInstruction {
sangho3f97a17d2015-01-29 22:56:29 -0800397
Yuta HIGUCHI6a479642015-02-08 01:28:50 -0800398 ModMplsTtlInstruction() {
sangho3f97a17d2015-01-29 22:56:29 -0800399 }
400
401 @Override
402 public L2SubType subtype() {
403 return L2SubType.DEC_MPLS_TTL;
404 }
405
406 @Override
407 public String toString() {
Yuta HIGUCHIcca5e722015-02-05 14:00:41 -0800408 return toStringHelper(subtype().toString())
409 .toString();
sangho3f97a17d2015-01-29 22:56:29 -0800410 }
411
412 @Override
413 public int hashCode() {
Yuta HIGUCHIcca5e722015-02-05 14:00:41 -0800414 return Objects.hash(type(), subtype());
sangho3f97a17d2015-01-29 22:56:29 -0800415 }
416
417 @Override
418 public boolean equals(Object obj) {
419 if (this == obj) {
420 return true;
421 }
Yuta HIGUCHIcca5e722015-02-05 14:00:41 -0800422 if (obj instanceof ModMplsTtlInstruction) {
423 return true;
sangho3f97a17d2015-01-29 22:56:29 -0800424 }
425 return false;
426 }
427 }
alshabib55a55d92014-09-16 11:59:31 -0700428}