blob: 470226636079581b487bd0467412b22e294f127c [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 /**
56 * MPLS Label modification.
57 */
58 MPLS_LABEL,
59
60 /**
61 * MPLS Push modification.
62 */
63 MPLS_PUSH,
64
65 /**
66 * MPLS Pop modification.
67 */
sangho3f97a17d2015-01-29 22:56:29 -080068 MPLS_POP,
69
70 /**
71 * MPLS TTL modification.
72 */
Saurav Dasfbe25c52015-03-04 11:12:00 -080073 DEC_MPLS_TTL,
sangho3f97a17d2015-01-29 22:56:29 -080074
Saurav Dasfbe25c52015-03-04 11:12:00 -080075 /**
76 * VLAN Pop modification.
77 */
78 VLAN_POP
alshabib55a55d92014-09-16 11:59:31 -070079 }
80
81 // TODO: Create factory class 'Instructions' that will have various factory
82 // to create specific instructions.
83
alshabib35edb1a2014-09-16 17:44:44 -070084 public abstract L2SubType subtype();
alshabib55a55d92014-09-16 11:59:31 -070085
86 @Override
Yuta HIGUCHI6a479642015-02-08 01:28:50 -080087 public final Type type() {
alshabib35edb1a2014-09-16 17:44:44 -070088 return Type.L2MODIFICATION;
alshabib55a55d92014-09-16 11:59:31 -070089 }
90
91 /**
92 * Represents a L2 src/dst modification instruction.
93 */
94 public static final class ModEtherInstruction extends L2ModificationInstruction {
95
alshabib35edb1a2014-09-16 17:44:44 -070096 private final L2SubType subtype;
Ayaka Koshibea9c199f2014-09-16 16:21:40 -070097 private final MacAddress mac;
alshabib55a55d92014-09-16 11:59:31 -070098
Yuta HIGUCHI6a479642015-02-08 01:28:50 -080099 ModEtherInstruction(L2SubType subType, MacAddress addr) {
alshabib64231f62014-09-16 17:58:36 -0700100
alshabib55a55d92014-09-16 11:59:31 -0700101 this.subtype = subType;
102 this.mac = addr;
103 }
104
105 @Override
alshabib35edb1a2014-09-16 17:44:44 -0700106 public L2SubType subtype() {
alshabib55a55d92014-09-16 11:59:31 -0700107 return this.subtype;
108 }
109
Ayaka Koshibea9c199f2014-09-16 16:21:40 -0700110 public MacAddress mac() {
alshabib55a55d92014-09-16 11:59:31 -0700111 return this.mac;
112 }
113
alshabib99b8fdc2014-09-25 14:30:22 -0700114 @Override
115 public String toString() {
116 return toStringHelper(subtype().toString())
117 .add("mac", mac).toString();
118 }
119
alshabib8ca53902014-10-07 13:11:17 -0700120 @Override
121 public int hashCode() {
Thomas Vachuska9b2da212014-11-10 19:30:25 -0800122 return Objects.hash(type(), subtype, mac);
alshabib8ca53902014-10-07 13:11:17 -0700123 }
124
125 @Override
126 public boolean equals(Object obj) {
127 if (this == obj) {
128 return true;
129 }
130 if (obj instanceof ModEtherInstruction) {
131 ModEtherInstruction that = (ModEtherInstruction) obj;
132 return Objects.equals(mac, that.mac) &&
133 Objects.equals(subtype, that.subtype);
alshabib8ca53902014-10-07 13:11:17 -0700134 }
135 return false;
136 }
alshabib55a55d92014-09-16 11:59:31 -0700137 }
138
Yuta HIGUCHI6a479642015-02-08 01:28:50 -0800139 // TODO This instruction is reused for Pop-Mpls. Consider renaming.
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -0800140 public static final class PushHeaderInstructions extends
141 L2ModificationInstruction {
142
143 private final L2SubType subtype;
Yuta HIGUCHI32a53c52015-02-08 01:25:40 -0800144 private final short ethernetType; // uint16_t
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -0800145
Yuta HIGUCHI32a53c52015-02-08 01:25:40 -0800146 PushHeaderInstructions(L2SubType subType, short ethernetType) {
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -0800147 this.subtype = subType;
148 this.ethernetType = ethernetType;
149 }
150
Yuta HIGUCHI32a53c52015-02-08 01:25:40 -0800151 public int ethernetType() {
152 return Short.toUnsignedInt(ethernetType);
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -0800153 }
154
155 @Override
156 public L2SubType subtype() {
157 return this.subtype;
158 }
159
160 @Override
161 public String toString() {
Yuta HIGUCHI32a53c52015-02-08 01:25:40 -0800162 return toStringHelper(subtype().toString())
163 .add("ethernetType", String.format("0x%04x", ethernetType()))
164 .toString();
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -0800165 }
166
167 @Override
168 public int hashCode() {
Yuta HIGUCHI32a53c52015-02-08 01:25:40 -0800169 return Objects.hash(type(), subtype, ethernetType);
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -0800170 }
171
172 @Override
173 public boolean equals(Object obj) {
174 if (this == obj) {
175 return true;
176 }
177 if (obj instanceof PushHeaderInstructions) {
178 PushHeaderInstructions that = (PushHeaderInstructions) obj;
Yuta HIGUCHI32a53c52015-02-08 01:25:40 -0800179 return Objects.equals(subtype, that.subtype) &&
180 Objects.equals(this.ethernetType, that.ethernetType);
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -0800181 }
182 return false;
183 }
184 }
185
186
187
alshabib55a55d92014-09-16 11:59:31 -0700188 /**
alshabib55a55d92014-09-16 11:59:31 -0700189 * Represents a VLAN id modification instruction.
190 */
191 public static final class ModVlanIdInstruction extends L2ModificationInstruction {
192
Ray Milkey78081052014-11-05 10:38:12 -0800193 private final VlanId vlanId;
alshabib55a55d92014-09-16 11:59:31 -0700194
Yuta HIGUCHI6a479642015-02-08 01:28:50 -0800195 ModVlanIdInstruction(VlanId vlanId) {
alshabib55a55d92014-09-16 11:59:31 -0700196 this.vlanId = vlanId;
197 }
198
199 @Override
alshabib35edb1a2014-09-16 17:44:44 -0700200 public L2SubType subtype() {
alshabib7410fea2014-09-16 13:48:39 -0700201 return L2SubType.VLAN_ID;
alshabib55a55d92014-09-16 11:59:31 -0700202 }
203
Ayaka Koshibea9c199f2014-09-16 16:21:40 -0700204 public VlanId vlanId() {
alshabib55a55d92014-09-16 11:59:31 -0700205 return this.vlanId;
206 }
207
alshabib99b8fdc2014-09-25 14:30:22 -0700208 @Override
209 public String toString() {
210 return toStringHelper(subtype().toString())
211 .add("id", vlanId).toString();
212 }
213
alshabib8ca53902014-10-07 13:11:17 -0700214 @Override
215 public int hashCode() {
Thomas Vachuska9b2da212014-11-10 19:30:25 -0800216 return Objects.hash(type(), subtype(), vlanId);
alshabib8ca53902014-10-07 13:11:17 -0700217 }
218
219 @Override
220 public boolean equals(Object obj) {
221 if (this == obj) {
222 return true;
223 }
224 if (obj instanceof ModVlanIdInstruction) {
225 ModVlanIdInstruction that = (ModVlanIdInstruction) obj;
Yuta HIGUCHI6a479642015-02-08 01:28:50 -0800226 return Objects.equals(vlanId, that.vlanId);
alshabib8ca53902014-10-07 13:11:17 -0700227 }
228 return false;
229 }
alshabib55a55d92014-09-16 11:59:31 -0700230 }
231
alshabib7410fea2014-09-16 13:48:39 -0700232 /**
233 * Represents a VLAN PCP modification instruction.
234 */
235 public static final class ModVlanPcpInstruction extends L2ModificationInstruction {
236
Ray Milkey78081052014-11-05 10:38:12 -0800237 private final Byte vlanPcp;
alshabib7410fea2014-09-16 13:48:39 -0700238
Yuta HIGUCHI6a479642015-02-08 01:28:50 -0800239 ModVlanPcpInstruction(Byte vlanPcp) {
alshabib7410fea2014-09-16 13:48:39 -0700240 this.vlanPcp = vlanPcp;
241 }
242
243 @Override
alshabib35edb1a2014-09-16 17:44:44 -0700244 public L2SubType subtype() {
alshabib7410fea2014-09-16 13:48:39 -0700245 return L2SubType.VLAN_PCP;
246 }
247
248 public Byte vlanPcp() {
249 return this.vlanPcp;
250 }
251
alshabib99b8fdc2014-09-25 14:30:22 -0700252 @Override
253 public String toString() {
254 return toStringHelper(subtype().toString())
255 .add("pcp", Long.toHexString(vlanPcp)).toString();
256 }
257
alshabib8ca53902014-10-07 13:11:17 -0700258 @Override
259 public int hashCode() {
Thomas Vachuska9b2da212014-11-10 19:30:25 -0800260 return Objects.hash(type(), subtype(), vlanPcp);
alshabib8ca53902014-10-07 13:11:17 -0700261 }
262
263 @Override
264 public boolean equals(Object obj) {
265 if (this == obj) {
266 return true;
267 }
268 if (obj instanceof ModVlanPcpInstruction) {
269 ModVlanPcpInstruction that = (ModVlanPcpInstruction) obj;
Yuta HIGUCHI6a479642015-02-08 01:28:50 -0800270 return Objects.equals(vlanPcp, that.vlanPcp);
alshabib8ca53902014-10-07 13:11:17 -0700271 }
272 return false;
273 }
alshabib7410fea2014-09-16 13:48:39 -0700274 }
275
Saurav Dasfbe25c52015-03-04 11:12:00 -0800276 /**
277 * Represents a VLAN POP modification instruction.
278 */
279 public static final class PopVlanInstruction extends L2ModificationInstruction {
280 private final L2SubType subtype;
281
282 PopVlanInstruction(L2SubType subType) {
283 this.subtype = subType;
284 }
285
286 @Override
287 public L2SubType subtype() {
288 return subtype;
289 }
290
291 @Override
292 public String toString() {
293 return toStringHelper(subtype().toString())
294 .toString();
295 }
296
297 @Override
298 public int hashCode() {
299 return Objects.hash(type(), subtype);
300 }
301
302 @Override
303 public boolean equals(Object obj) {
304 if (this == obj) {
305 return true;
306 }
307 if (obj instanceof PushHeaderInstructions) {
308 PushHeaderInstructions that = (PushHeaderInstructions) obj;
309 return Objects.equals(subtype, that.subtype);
310 }
311 return false;
312 }
313 }
alshabib55a55d92014-09-16 11:59:31 -0700314
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -0800315 /**
316 * Represents a MPLS label modification.
317 */
Yuta HIGUCHI6a479642015-02-08 01:28:50 -0800318 public static final class ModMplsLabelInstruction
319 extends L2ModificationInstruction {
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -0800320
Michele Santuari4b6019e2014-12-19 11:31:45 +0100321 private final MplsLabel mplsLabel;
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -0800322
Yuta HIGUCHI6a479642015-02-08 01:28:50 -0800323 ModMplsLabelInstruction(MplsLabel mplsLabel) {
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -0800324 this.mplsLabel = mplsLabel;
325 }
326
327 public Integer label() {
Michele Santuari4b6019e2014-12-19 11:31:45 +0100328 return mplsLabel.toInt();
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -0800329 }
330
331 @Override
332 public L2SubType subtype() {
333 return L2SubType.MPLS_LABEL;
334 }
335
336 @Override
337 public String toString() {
Pavlin Radoslavov3ebe1702015-02-17 09:53:17 -0800338 return toStringHelper(subtype().toString())
Michele Santuari4b6019e2014-12-19 11:31:45 +0100339 .add("mpls", mplsLabel).toString();
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -0800340 }
341
342 @Override
343 public int hashCode() {
Yuta HIGUCHIcca5e722015-02-05 14:00:41 -0800344 return Objects.hash(type(), subtype(), mplsLabel);
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -0800345 }
346
347 @Override
348 public boolean equals(Object obj) {
349 if (this == obj) {
350 return true;
351 }
352 if (obj instanceof ModMplsLabelInstruction) {
353 ModMplsLabelInstruction that = (ModMplsLabelInstruction) obj;
Yuta HIGUCHI6a479642015-02-08 01:28:50 -0800354 return Objects.equals(mplsLabel, that.mplsLabel);
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -0800355 }
356 return false;
357 }
358 }
sangho3f97a17d2015-01-29 22:56:29 -0800359
360 /**
Yuta HIGUCHIcca5e722015-02-05 14:00:41 -0800361 * Represents a MPLS TTL modification.
sangho3f97a17d2015-01-29 22:56:29 -0800362 */
Yuta HIGUCHI6a479642015-02-08 01:28:50 -0800363 public static final class ModMplsTtlInstruction
364 extends L2ModificationInstruction {
sangho3f97a17d2015-01-29 22:56:29 -0800365
Yuta HIGUCHI6a479642015-02-08 01:28:50 -0800366 ModMplsTtlInstruction() {
sangho3f97a17d2015-01-29 22:56:29 -0800367 }
368
369 @Override
370 public L2SubType subtype() {
371 return L2SubType.DEC_MPLS_TTL;
372 }
373
374 @Override
375 public String toString() {
Yuta HIGUCHIcca5e722015-02-05 14:00:41 -0800376 return toStringHelper(subtype().toString())
377 .toString();
sangho3f97a17d2015-01-29 22:56:29 -0800378 }
379
380 @Override
381 public int hashCode() {
Yuta HIGUCHIcca5e722015-02-05 14:00:41 -0800382 return Objects.hash(type(), subtype());
sangho3f97a17d2015-01-29 22:56:29 -0800383 }
384
385 @Override
386 public boolean equals(Object obj) {
387 if (this == obj) {
388 return true;
389 }
Yuta HIGUCHIcca5e722015-02-05 14:00:41 -0800390 if (obj instanceof ModMplsTtlInstruction) {
391 return true;
sangho3f97a17d2015-01-29 22:56:29 -0800392 }
393 return false;
394 }
395 }
alshabib55a55d92014-09-16 11:59:31 -0700396}