blob: d2ea8627c2a2e296421e26233008e9336502f2e8 [file] [log] [blame]
Thomas Vachuska83e090e2014-10-22 14:25:35 -07001/*
Ray Milkey34c95902015-04-15 09:47:53 -07002 * Copyright 2014-2015 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 /**
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 */
Jonathan Hart54b406b2015-03-06 16:24:14 -080078 VLAN_POP,
79
80 /**
81 * VLAN Push modification.
82 */
83 VLAN_PUSH
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
Saurav Dasfbe25c52015-03-04 11:12:00 -0800281 /**
282 * Represents a VLAN POP modification instruction.
283 */
284 public static final class PopVlanInstruction extends L2ModificationInstruction {
285 private final L2SubType subtype;
286
287 PopVlanInstruction(L2SubType subType) {
288 this.subtype = subType;
289 }
290
291 @Override
292 public L2SubType subtype() {
293 return subtype;
294 }
295
296 @Override
297 public String toString() {
298 return toStringHelper(subtype().toString())
299 .toString();
300 }
301
302 @Override
303 public int hashCode() {
304 return Objects.hash(type(), subtype);
305 }
306
307 @Override
308 public boolean equals(Object obj) {
309 if (this == obj) {
310 return true;
311 }
HIGUCHI Yuta7bb8b3f2015-03-13 23:21:37 -0700312 if (obj instanceof PopVlanInstruction) {
313 PopVlanInstruction that = (PopVlanInstruction) obj;
Saurav Dasfbe25c52015-03-04 11:12:00 -0800314 return Objects.equals(subtype, that.subtype);
315 }
316 return false;
317 }
318 }
alshabib55a55d92014-09-16 11:59:31 -0700319
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -0800320 /**
321 * Represents a MPLS label modification.
322 */
Yuta HIGUCHI6a479642015-02-08 01:28:50 -0800323 public static final class ModMplsLabelInstruction
324 extends L2ModificationInstruction {
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -0800325
Michele Santuari4b6019e2014-12-19 11:31:45 +0100326 private final MplsLabel mplsLabel;
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -0800327
Yuta HIGUCHI6a479642015-02-08 01:28:50 -0800328 ModMplsLabelInstruction(MplsLabel mplsLabel) {
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -0800329 this.mplsLabel = mplsLabel;
330 }
331
332 public Integer label() {
Michele Santuari4b6019e2014-12-19 11:31:45 +0100333 return mplsLabel.toInt();
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -0800334 }
335
336 @Override
337 public L2SubType subtype() {
338 return L2SubType.MPLS_LABEL;
339 }
340
341 @Override
342 public String toString() {
Pavlin Radoslavov3ebe1702015-02-17 09:53:17 -0800343 return toStringHelper(subtype().toString())
Michele Santuari4b6019e2014-12-19 11:31:45 +0100344 .add("mpls", mplsLabel).toString();
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -0800345 }
346
347 @Override
348 public int hashCode() {
Yuta HIGUCHIcca5e722015-02-05 14:00:41 -0800349 return Objects.hash(type(), subtype(), mplsLabel);
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -0800350 }
351
352 @Override
353 public boolean equals(Object obj) {
354 if (this == obj) {
355 return true;
356 }
357 if (obj instanceof ModMplsLabelInstruction) {
358 ModMplsLabelInstruction that = (ModMplsLabelInstruction) obj;
Yuta HIGUCHI6a479642015-02-08 01:28:50 -0800359 return Objects.equals(mplsLabel, that.mplsLabel);
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -0800360 }
361 return false;
362 }
363 }
sangho3f97a17d2015-01-29 22:56:29 -0800364
365 /**
Yuta HIGUCHIcca5e722015-02-05 14:00:41 -0800366 * Represents a MPLS TTL modification.
sangho3f97a17d2015-01-29 22:56:29 -0800367 */
Yuta HIGUCHI6a479642015-02-08 01:28:50 -0800368 public static final class ModMplsTtlInstruction
369 extends L2ModificationInstruction {
sangho3f97a17d2015-01-29 22:56:29 -0800370
Yuta HIGUCHI6a479642015-02-08 01:28:50 -0800371 ModMplsTtlInstruction() {
sangho3f97a17d2015-01-29 22:56:29 -0800372 }
373
374 @Override
375 public L2SubType subtype() {
376 return L2SubType.DEC_MPLS_TTL;
377 }
378
379 @Override
380 public String toString() {
Yuta HIGUCHIcca5e722015-02-05 14:00:41 -0800381 return toStringHelper(subtype().toString())
382 .toString();
sangho3f97a17d2015-01-29 22:56:29 -0800383 }
384
385 @Override
386 public int hashCode() {
Yuta HIGUCHIcca5e722015-02-05 14:00:41 -0800387 return Objects.hash(type(), subtype());
sangho3f97a17d2015-01-29 22:56:29 -0800388 }
389
390 @Override
391 public boolean equals(Object obj) {
392 if (this == obj) {
393 return true;
394 }
Yuta HIGUCHIcca5e722015-02-05 14:00:41 -0800395 if (obj instanceof ModMplsTtlInstruction) {
396 return true;
sangho3f97a17d2015-01-29 22:56:29 -0800397 }
398 return false;
399 }
400 }
alshabib55a55d92014-09-16 11:59:31 -0700401}