blob: e29ffb90867c841402f90132db649f433fc2aeef [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
288 @Override
289 public L2SubType subtype() {
290 return L2SubType.STRIP_VLAN;
291 }
292
293 @Override
294 public String toString() {
295 return subtype().toString();
296 }
297
298 @Override
299 public int hashCode() {
300 return Objects.hash(type(), subtype());
301 }
302
303 @Override
304 public boolean equals(Object obj) {
305 if (this == obj) {
306 return true;
307 }
308
309 return false;
310 }
311 }
312
Saurav Dasfbe25c52015-03-04 11:12:00 -0800313 /**
314 * Represents a VLAN POP modification instruction.
315 */
316 public static final class PopVlanInstruction extends L2ModificationInstruction {
317 private final L2SubType subtype;
318
319 PopVlanInstruction(L2SubType subType) {
320 this.subtype = subType;
321 }
322
323 @Override
324 public L2SubType subtype() {
325 return subtype;
326 }
327
328 @Override
329 public String toString() {
330 return toStringHelper(subtype().toString())
331 .toString();
332 }
333
334 @Override
335 public int hashCode() {
336 return Objects.hash(type(), subtype);
337 }
338
339 @Override
340 public boolean equals(Object obj) {
341 if (this == obj) {
342 return true;
343 }
344 if (obj instanceof PushHeaderInstructions) {
345 PushHeaderInstructions that = (PushHeaderInstructions) obj;
346 return Objects.equals(subtype, that.subtype);
347 }
348 return false;
349 }
350 }
alshabib55a55d92014-09-16 11:59:31 -0700351
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -0800352 /**
353 * Represents a MPLS label modification.
354 */
Yuta HIGUCHI6a479642015-02-08 01:28:50 -0800355 public static final class ModMplsLabelInstruction
356 extends L2ModificationInstruction {
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -0800357
Michele Santuari4b6019e2014-12-19 11:31:45 +0100358 private final MplsLabel mplsLabel;
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -0800359
Yuta HIGUCHI6a479642015-02-08 01:28:50 -0800360 ModMplsLabelInstruction(MplsLabel mplsLabel) {
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -0800361 this.mplsLabel = mplsLabel;
362 }
363
364 public Integer label() {
Michele Santuari4b6019e2014-12-19 11:31:45 +0100365 return mplsLabel.toInt();
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -0800366 }
367
368 @Override
369 public L2SubType subtype() {
370 return L2SubType.MPLS_LABEL;
371 }
372
373 @Override
374 public String toString() {
Pavlin Radoslavov3ebe1702015-02-17 09:53:17 -0800375 return toStringHelper(subtype().toString())
Michele Santuari4b6019e2014-12-19 11:31:45 +0100376 .add("mpls", mplsLabel).toString();
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -0800377 }
378
379 @Override
380 public int hashCode() {
Yuta HIGUCHIcca5e722015-02-05 14:00:41 -0800381 return Objects.hash(type(), subtype(), mplsLabel);
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -0800382 }
383
384 @Override
385 public boolean equals(Object obj) {
386 if (this == obj) {
387 return true;
388 }
389 if (obj instanceof ModMplsLabelInstruction) {
390 ModMplsLabelInstruction that = (ModMplsLabelInstruction) obj;
Yuta HIGUCHI6a479642015-02-08 01:28:50 -0800391 return Objects.equals(mplsLabel, that.mplsLabel);
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -0800392 }
393 return false;
394 }
395 }
sangho3f97a17d2015-01-29 22:56:29 -0800396
397 /**
Yuta HIGUCHIcca5e722015-02-05 14:00:41 -0800398 * Represents a MPLS TTL modification.
sangho3f97a17d2015-01-29 22:56:29 -0800399 */
Yuta HIGUCHI6a479642015-02-08 01:28:50 -0800400 public static final class ModMplsTtlInstruction
401 extends L2ModificationInstruction {
sangho3f97a17d2015-01-29 22:56:29 -0800402
Yuta HIGUCHI6a479642015-02-08 01:28:50 -0800403 ModMplsTtlInstruction() {
sangho3f97a17d2015-01-29 22:56:29 -0800404 }
405
406 @Override
407 public L2SubType subtype() {
408 return L2SubType.DEC_MPLS_TTL;
409 }
410
411 @Override
412 public String toString() {
Yuta HIGUCHIcca5e722015-02-05 14:00:41 -0800413 return toStringHelper(subtype().toString())
414 .toString();
sangho3f97a17d2015-01-29 22:56:29 -0800415 }
416
417 @Override
418 public int hashCode() {
Yuta HIGUCHIcca5e722015-02-05 14:00:41 -0800419 return Objects.hash(type(), subtype());
sangho3f97a17d2015-01-29 22:56:29 -0800420 }
421
422 @Override
423 public boolean equals(Object obj) {
424 if (this == obj) {
425 return true;
426 }
Yuta HIGUCHIcca5e722015-02-05 14:00:41 -0800427 if (obj instanceof ModMplsTtlInstruction) {
428 return true;
sangho3f97a17d2015-01-29 22:56:29 -0800429 }
430 return false;
431 }
432 }
alshabib55a55d92014-09-16 11:59:31 -0700433}