blob: dcb137e0b2be520eb9d1234e680a0744131fdaa8 [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
Ayaka Koshibea9c199f2014-09-16 16:21:40 -070018import org.onlab.packet.MacAddress;
Michele Santuari4b6019e2014-12-19 11:31:45 +010019import org.onlab.packet.MplsLabel;
Ayaka Koshibea9c199f2014-09-16 16:21:40 -070020import org.onlab.packet.VlanId;
alshabib55a55d92014-09-16 11:59:31 -070021
Jonathan Hart54b406b2015-03-06 16:24:14 -080022import java.util.Objects;
23
24import static com.google.common.base.MoreObjects.toStringHelper;
25
alshabib55a55d92014-09-16 11:59:31 -070026/**
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 */
Jonathan Hart54b406b2015-03-06 16:24:14 -080083 VLAN_POP,
84
85 /**
86 * VLAN Push modification.
87 */
88 VLAN_PUSH
alshabib55a55d92014-09-16 11:59:31 -070089 }
90
91 // TODO: Create factory class 'Instructions' that will have various factory
92 // to create specific instructions.
93
alshabib35edb1a2014-09-16 17:44:44 -070094 public abstract L2SubType subtype();
alshabib55a55d92014-09-16 11:59:31 -070095
96 @Override
Yuta HIGUCHI6a479642015-02-08 01:28:50 -080097 public final Type type() {
alshabib35edb1a2014-09-16 17:44:44 -070098 return Type.L2MODIFICATION;
alshabib55a55d92014-09-16 11:59:31 -070099 }
100
101 /**
102 * Represents a L2 src/dst modification instruction.
103 */
104 public static final class ModEtherInstruction extends L2ModificationInstruction {
105
alshabib35edb1a2014-09-16 17:44:44 -0700106 private final L2SubType subtype;
Ayaka Koshibea9c199f2014-09-16 16:21:40 -0700107 private final MacAddress mac;
alshabib55a55d92014-09-16 11:59:31 -0700108
Yuta HIGUCHI6a479642015-02-08 01:28:50 -0800109 ModEtherInstruction(L2SubType subType, MacAddress addr) {
alshabib64231f62014-09-16 17:58:36 -0700110
alshabib55a55d92014-09-16 11:59:31 -0700111 this.subtype = subType;
112 this.mac = addr;
113 }
114
115 @Override
alshabib35edb1a2014-09-16 17:44:44 -0700116 public L2SubType subtype() {
alshabib55a55d92014-09-16 11:59:31 -0700117 return this.subtype;
118 }
119
Ayaka Koshibea9c199f2014-09-16 16:21:40 -0700120 public MacAddress mac() {
alshabib55a55d92014-09-16 11:59:31 -0700121 return this.mac;
122 }
123
alshabib99b8fdc2014-09-25 14:30:22 -0700124 @Override
125 public String toString() {
126 return toStringHelper(subtype().toString())
127 .add("mac", mac).toString();
128 }
129
alshabib8ca53902014-10-07 13:11:17 -0700130 @Override
131 public int hashCode() {
Thomas Vachuska9b2da212014-11-10 19:30:25 -0800132 return Objects.hash(type(), subtype, mac);
alshabib8ca53902014-10-07 13:11:17 -0700133 }
134
135 @Override
136 public boolean equals(Object obj) {
137 if (this == obj) {
138 return true;
139 }
140 if (obj instanceof ModEtherInstruction) {
141 ModEtherInstruction that = (ModEtherInstruction) obj;
142 return Objects.equals(mac, that.mac) &&
143 Objects.equals(subtype, that.subtype);
alshabib8ca53902014-10-07 13:11:17 -0700144 }
145 return false;
146 }
alshabib55a55d92014-09-16 11:59:31 -0700147 }
148
Yuta HIGUCHI6a479642015-02-08 01:28:50 -0800149 // TODO This instruction is reused for Pop-Mpls. Consider renaming.
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -0800150 public static final class PushHeaderInstructions extends
151 L2ModificationInstruction {
152
153 private final L2SubType subtype;
Yuta HIGUCHI32a53c52015-02-08 01:25:40 -0800154 private final short ethernetType; // uint16_t
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -0800155
Yuta HIGUCHI32a53c52015-02-08 01:25:40 -0800156 PushHeaderInstructions(L2SubType subType, short ethernetType) {
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -0800157 this.subtype = subType;
158 this.ethernetType = ethernetType;
159 }
160
Yuta HIGUCHI32a53c52015-02-08 01:25:40 -0800161 public int ethernetType() {
162 return Short.toUnsignedInt(ethernetType);
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -0800163 }
164
165 @Override
166 public L2SubType subtype() {
167 return this.subtype;
168 }
169
170 @Override
171 public String toString() {
Yuta HIGUCHI32a53c52015-02-08 01:25:40 -0800172 return toStringHelper(subtype().toString())
173 .add("ethernetType", String.format("0x%04x", ethernetType()))
174 .toString();
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -0800175 }
176
177 @Override
178 public int hashCode() {
Yuta HIGUCHI32a53c52015-02-08 01:25:40 -0800179 return Objects.hash(type(), subtype, ethernetType);
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -0800180 }
181
182 @Override
183 public boolean equals(Object obj) {
184 if (this == obj) {
185 return true;
186 }
187 if (obj instanceof PushHeaderInstructions) {
188 PushHeaderInstructions that = (PushHeaderInstructions) obj;
Yuta HIGUCHI32a53c52015-02-08 01:25:40 -0800189 return Objects.equals(subtype, that.subtype) &&
190 Objects.equals(this.ethernetType, that.ethernetType);
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -0800191 }
192 return false;
193 }
194 }
195
196
197
alshabib55a55d92014-09-16 11:59:31 -0700198 /**
alshabib55a55d92014-09-16 11:59:31 -0700199 * Represents a VLAN id modification instruction.
200 */
201 public static final class ModVlanIdInstruction extends L2ModificationInstruction {
202
Ray Milkey78081052014-11-05 10:38:12 -0800203 private final VlanId vlanId;
alshabib55a55d92014-09-16 11:59:31 -0700204
Yuta HIGUCHI6a479642015-02-08 01:28:50 -0800205 ModVlanIdInstruction(VlanId vlanId) {
alshabib55a55d92014-09-16 11:59:31 -0700206 this.vlanId = vlanId;
207 }
208
209 @Override
alshabib35edb1a2014-09-16 17:44:44 -0700210 public L2SubType subtype() {
alshabib7410fea2014-09-16 13:48:39 -0700211 return L2SubType.VLAN_ID;
alshabib55a55d92014-09-16 11:59:31 -0700212 }
213
Ayaka Koshibea9c199f2014-09-16 16:21:40 -0700214 public VlanId vlanId() {
alshabib55a55d92014-09-16 11:59:31 -0700215 return this.vlanId;
216 }
217
alshabib99b8fdc2014-09-25 14:30:22 -0700218 @Override
219 public String toString() {
220 return toStringHelper(subtype().toString())
221 .add("id", vlanId).toString();
222 }
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
Ray Milkey78081052014-11-05 10:38:12 -0800247 private final Byte vlanPcp;
alshabib7410fea2014-09-16 13:48:39 -0700248
Yuta HIGUCHI6a479642015-02-08 01:28:50 -0800249 ModVlanPcpInstruction(Byte vlanPcp) {
alshabib7410fea2014-09-16 13:48:39 -0700250 this.vlanPcp = vlanPcp;
251 }
252
253 @Override
alshabib35edb1a2014-09-16 17:44:44 -0700254 public L2SubType subtype() {
alshabib7410fea2014-09-16 13:48:39 -0700255 return L2SubType.VLAN_PCP;
256 }
257
258 public Byte vlanPcp() {
259 return this.vlanPcp;
260 }
261
alshabib99b8fdc2014-09-25 14:30:22 -0700262 @Override
263 public String toString() {
264 return toStringHelper(subtype().toString())
265 .add("pcp", Long.toHexString(vlanPcp)).toString();
266 }
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
alshabibab21b2d2015-03-04 18:35:33 -0800286 public static final class StripVlanInstruction extends L2ModificationInstruction {
287
HIGUCHI Yuta7bb8b3f2015-03-13 23:21:37 -0700288 StripVlanInstruction() {}
289
alshabibab21b2d2015-03-04 18:35:33 -0800290 @Override
291 public L2SubType subtype() {
292 return L2SubType.STRIP_VLAN;
293 }
294
295 @Override
296 public String toString() {
HIGUCHI Yuta7bb8b3f2015-03-13 23:21:37 -0700297 return toStringHelper(subtype().toString())
298 .toString();
alshabibab21b2d2015-03-04 18:35:33 -0800299 }
300
301 @Override
302 public int hashCode() {
303 return Objects.hash(type(), subtype());
304 }
305
306 @Override
307 public boolean equals(Object obj) {
308 if (this == obj) {
309 return true;
310 }
HIGUCHI Yuta7bb8b3f2015-03-13 23:21:37 -0700311 if (obj instanceof StripVlanInstruction) {
312 return true;
313 }
alshabibab21b2d2015-03-04 18:35:33 -0800314 return false;
315 }
316 }
317
Saurav Dasfbe25c52015-03-04 11:12:00 -0800318 /**
319 * Represents a VLAN POP modification instruction.
320 */
321 public static final class PopVlanInstruction extends L2ModificationInstruction {
322 private final L2SubType subtype;
323
324 PopVlanInstruction(L2SubType subType) {
325 this.subtype = subType;
326 }
327
328 @Override
329 public L2SubType subtype() {
330 return subtype;
331 }
332
333 @Override
334 public String toString() {
335 return toStringHelper(subtype().toString())
336 .toString();
337 }
338
339 @Override
340 public int hashCode() {
341 return Objects.hash(type(), subtype);
342 }
343
344 @Override
345 public boolean equals(Object obj) {
346 if (this == obj) {
347 return true;
348 }
HIGUCHI Yuta7bb8b3f2015-03-13 23:21:37 -0700349 if (obj instanceof PopVlanInstruction) {
350 PopVlanInstruction that = (PopVlanInstruction) obj;
Saurav Dasfbe25c52015-03-04 11:12:00 -0800351 return Objects.equals(subtype, that.subtype);
352 }
353 return false;
354 }
355 }
alshabib55a55d92014-09-16 11:59:31 -0700356
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -0800357 /**
358 * Represents a MPLS label modification.
359 */
Yuta HIGUCHI6a479642015-02-08 01:28:50 -0800360 public static final class ModMplsLabelInstruction
361 extends L2ModificationInstruction {
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -0800362
Michele Santuari4b6019e2014-12-19 11:31:45 +0100363 private final MplsLabel mplsLabel;
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -0800364
Yuta HIGUCHI6a479642015-02-08 01:28:50 -0800365 ModMplsLabelInstruction(MplsLabel mplsLabel) {
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -0800366 this.mplsLabel = mplsLabel;
367 }
368
369 public Integer label() {
Michele Santuari4b6019e2014-12-19 11:31:45 +0100370 return mplsLabel.toInt();
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -0800371 }
372
373 @Override
374 public L2SubType subtype() {
375 return L2SubType.MPLS_LABEL;
376 }
377
378 @Override
379 public String toString() {
Pavlin Radoslavov3ebe1702015-02-17 09:53:17 -0800380 return toStringHelper(subtype().toString())
Michele Santuari4b6019e2014-12-19 11:31:45 +0100381 .add("mpls", mplsLabel).toString();
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -0800382 }
383
384 @Override
385 public int hashCode() {
Yuta HIGUCHIcca5e722015-02-05 14:00:41 -0800386 return Objects.hash(type(), subtype(), mplsLabel);
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -0800387 }
388
389 @Override
390 public boolean equals(Object obj) {
391 if (this == obj) {
392 return true;
393 }
394 if (obj instanceof ModMplsLabelInstruction) {
395 ModMplsLabelInstruction that = (ModMplsLabelInstruction) obj;
Yuta HIGUCHI6a479642015-02-08 01:28:50 -0800396 return Objects.equals(mplsLabel, that.mplsLabel);
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -0800397 }
398 return false;
399 }
400 }
sangho3f97a17d2015-01-29 22:56:29 -0800401
402 /**
Yuta HIGUCHIcca5e722015-02-05 14:00:41 -0800403 * Represents a MPLS TTL modification.
sangho3f97a17d2015-01-29 22:56:29 -0800404 */
Yuta HIGUCHI6a479642015-02-08 01:28:50 -0800405 public static final class ModMplsTtlInstruction
406 extends L2ModificationInstruction {
sangho3f97a17d2015-01-29 22:56:29 -0800407
Yuta HIGUCHI6a479642015-02-08 01:28:50 -0800408 ModMplsTtlInstruction() {
sangho3f97a17d2015-01-29 22:56:29 -0800409 }
410
411 @Override
412 public L2SubType subtype() {
413 return L2SubType.DEC_MPLS_TTL;
414 }
415
416 @Override
417 public String toString() {
Yuta HIGUCHIcca5e722015-02-05 14:00:41 -0800418 return toStringHelper(subtype().toString())
419 .toString();
sangho3f97a17d2015-01-29 22:56:29 -0800420 }
421
422 @Override
423 public int hashCode() {
Yuta HIGUCHIcca5e722015-02-05 14:00:41 -0800424 return Objects.hash(type(), subtype());
sangho3f97a17d2015-01-29 22:56:29 -0800425 }
426
427 @Override
428 public boolean equals(Object obj) {
429 if (this == obj) {
430 return true;
431 }
Yuta HIGUCHIcca5e722015-02-05 14:00:41 -0800432 if (obj instanceof ModMplsTtlInstruction) {
433 return true;
sangho3f97a17d2015-01-29 22:56:29 -0800434 }
435 return false;
436 }
437 }
alshabib55a55d92014-09-16 11:59:31 -0700438}