blob: 723bbe61208476cc2c9615f7bb3d8a37a5547c4b [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;
23import org.onlab.packet.VlanId;
alshabib55a55d92014-09-16 11:59:31 -070024
25/**
26 * Abstraction of a single traffic treatment step.
alshabib55a55d92014-09-16 11:59:31 -070027 */
28public abstract class L2ModificationInstruction implements Instruction {
29
30 /**
31 * Represents the type of traffic treatment.
32 */
alshabib35edb1a2014-09-16 17:44:44 -070033 public enum L2SubType {
alshabib55a55d92014-09-16 11:59:31 -070034 /**
35 * Ether src modification.
36 */
alshabib99b8fdc2014-09-25 14:30:22 -070037 ETH_SRC,
alshabib55a55d92014-09-16 11:59:31 -070038
39 /**
40 * Ether dst modification.
41 */
alshabib99b8fdc2014-09-25 14:30:22 -070042 ETH_DST,
alshabib55a55d92014-09-16 11:59:31 -070043
44 /**
alshabib55a55d92014-09-16 11:59:31 -070045 * VLAN id modification.
46 */
47 VLAN_ID,
48
49 /**
50 * VLAN priority modification.
51 */
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -080052 VLAN_PCP,
53
54 /**
55 * MPLS Label modification.
56 */
57 MPLS_LABEL,
58
59 /**
60 * MPLS Push modification.
61 */
62 MPLS_PUSH,
63
64 /**
65 * MPLS Pop modification.
66 */
sangho3f97a17d2015-01-29 22:56:29 -080067 MPLS_POP,
68
69 /**
70 * MPLS TTL modification.
71 */
72 DEC_MPLS_TTL
73
alshabib55a55d92014-09-16 11:59:31 -070074 }
75
76 // TODO: Create factory class 'Instructions' that will have various factory
77 // to create specific instructions.
78
alshabib35edb1a2014-09-16 17:44:44 -070079 public abstract L2SubType subtype();
alshabib55a55d92014-09-16 11:59:31 -070080
81 @Override
82 public Type type() {
alshabib35edb1a2014-09-16 17:44:44 -070083 return Type.L2MODIFICATION;
alshabib55a55d92014-09-16 11:59:31 -070084 }
85
86 /**
87 * Represents a L2 src/dst modification instruction.
88 */
89 public static final class ModEtherInstruction extends L2ModificationInstruction {
90
alshabib35edb1a2014-09-16 17:44:44 -070091 private final L2SubType subtype;
Ayaka Koshibea9c199f2014-09-16 16:21:40 -070092 private final MacAddress mac;
alshabib55a55d92014-09-16 11:59:31 -070093
alshabib64231f62014-09-16 17:58:36 -070094 public ModEtherInstruction(L2SubType subType, MacAddress addr) {
95
alshabib55a55d92014-09-16 11:59:31 -070096 this.subtype = subType;
97 this.mac = addr;
98 }
99
100 @Override
alshabib35edb1a2014-09-16 17:44:44 -0700101 public L2SubType subtype() {
alshabib55a55d92014-09-16 11:59:31 -0700102 return this.subtype;
103 }
104
Ayaka Koshibea9c199f2014-09-16 16:21:40 -0700105 public MacAddress mac() {
alshabib55a55d92014-09-16 11:59:31 -0700106 return this.mac;
107 }
108
alshabib99b8fdc2014-09-25 14:30:22 -0700109 @Override
110 public String toString() {
111 return toStringHelper(subtype().toString())
112 .add("mac", mac).toString();
113 }
114
alshabib8ca53902014-10-07 13:11:17 -0700115 @Override
116 public int hashCode() {
Thomas Vachuska9b2da212014-11-10 19:30:25 -0800117 return Objects.hash(type(), subtype, mac);
alshabib8ca53902014-10-07 13:11:17 -0700118 }
119
120 @Override
121 public boolean equals(Object obj) {
122 if (this == obj) {
123 return true;
124 }
125 if (obj instanceof ModEtherInstruction) {
126 ModEtherInstruction that = (ModEtherInstruction) obj;
127 return Objects.equals(mac, that.mac) &&
alshabib2020b892014-10-20 15:47:08 -0700128 Objects.equals(this.type(), that.type()) &&
alshabib8ca53902014-10-07 13:11:17 -0700129 Objects.equals(subtype, that.subtype);
130
131 }
132 return false;
133 }
134
alshabib99b8fdc2014-09-25 14:30:22 -0700135
alshabib55a55d92014-09-16 11:59:31 -0700136 }
137
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -0800138 public static final class PushHeaderInstructions extends
139 L2ModificationInstruction {
140
141 private final L2SubType subtype;
Yuta HIGUCHI32a53c52015-02-08 01:25:40 -0800142 private final short ethernetType; // uint16_t
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -0800143
Yuta HIGUCHI32a53c52015-02-08 01:25:40 -0800144 PushHeaderInstructions(L2SubType subType, short ethernetType) {
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -0800145 this.subtype = subType;
146 this.ethernetType = ethernetType;
147 }
148
Yuta HIGUCHI32a53c52015-02-08 01:25:40 -0800149 public int ethernetType() {
150 return Short.toUnsignedInt(ethernetType);
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -0800151 }
152
153 @Override
154 public L2SubType subtype() {
155 return this.subtype;
156 }
157
158 @Override
159 public String toString() {
Yuta HIGUCHI32a53c52015-02-08 01:25:40 -0800160 return toStringHelper(subtype().toString())
161 .add("ethernetType", String.format("0x%04x", ethernetType()))
162 .toString();
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -0800163 }
164
165 @Override
166 public int hashCode() {
Yuta HIGUCHI32a53c52015-02-08 01:25:40 -0800167 return Objects.hash(type(), subtype, ethernetType);
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -0800168 }
169
170 @Override
171 public boolean equals(Object obj) {
172 if (this == obj) {
173 return true;
174 }
175 if (obj instanceof PushHeaderInstructions) {
176 PushHeaderInstructions that = (PushHeaderInstructions) obj;
Yuta HIGUCHI32a53c52015-02-08 01:25:40 -0800177 return Objects.equals(subtype, that.subtype) &&
178 Objects.equals(this.ethernetType, that.ethernetType);
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -0800179 }
180 return false;
181 }
182 }
183
184
185
alshabib55a55d92014-09-16 11:59:31 -0700186 /**
alshabib55a55d92014-09-16 11:59:31 -0700187 * Represents a VLAN id modification instruction.
188 */
189 public static final class ModVlanIdInstruction extends L2ModificationInstruction {
190
Ray Milkey78081052014-11-05 10:38:12 -0800191 private final VlanId vlanId;
alshabib55a55d92014-09-16 11:59:31 -0700192
Ayaka Koshibea9c199f2014-09-16 16:21:40 -0700193 public ModVlanIdInstruction(VlanId vlanId) {
alshabib55a55d92014-09-16 11:59:31 -0700194 this.vlanId = vlanId;
195 }
196
197 @Override
alshabib35edb1a2014-09-16 17:44:44 -0700198 public L2SubType subtype() {
alshabib7410fea2014-09-16 13:48:39 -0700199 return L2SubType.VLAN_ID;
alshabib55a55d92014-09-16 11:59:31 -0700200 }
201
Ayaka Koshibea9c199f2014-09-16 16:21:40 -0700202 public VlanId vlanId() {
alshabib55a55d92014-09-16 11:59:31 -0700203 return this.vlanId;
204 }
205
alshabib99b8fdc2014-09-25 14:30:22 -0700206 @Override
207 public String toString() {
208 return toStringHelper(subtype().toString())
209 .add("id", vlanId).toString();
210 }
211
alshabib8ca53902014-10-07 13:11:17 -0700212 @Override
213 public int hashCode() {
Thomas Vachuska9b2da212014-11-10 19:30:25 -0800214 return Objects.hash(type(), subtype(), vlanId);
alshabib8ca53902014-10-07 13:11:17 -0700215 }
216
217 @Override
218 public boolean equals(Object obj) {
219 if (this == obj) {
220 return true;
221 }
222 if (obj instanceof ModVlanIdInstruction) {
223 ModVlanIdInstruction that = (ModVlanIdInstruction) obj;
alshabib2020b892014-10-20 15:47:08 -0700224 return Objects.equals(vlanId, that.vlanId) &&
225 Objects.equals(this.type(), that.type()) &&
226 Objects.equals(this.subtype(), that.subtype());
alshabib8ca53902014-10-07 13:11:17 -0700227
228 }
229 return false;
230 }
231
232
alshabib55a55d92014-09-16 11:59:31 -0700233 }
234
alshabib7410fea2014-09-16 13:48:39 -0700235 /**
236 * Represents a VLAN PCP modification instruction.
237 */
238 public static final class ModVlanPcpInstruction extends L2ModificationInstruction {
239
Ray Milkey78081052014-11-05 10:38:12 -0800240 private final Byte vlanPcp;
alshabib7410fea2014-09-16 13:48:39 -0700241
242 public ModVlanPcpInstruction(Byte vlanPcp) {
243 this.vlanPcp = vlanPcp;
244 }
245
246 @Override
alshabib35edb1a2014-09-16 17:44:44 -0700247 public L2SubType subtype() {
alshabib7410fea2014-09-16 13:48:39 -0700248 return L2SubType.VLAN_PCP;
249 }
250
251 public Byte vlanPcp() {
252 return this.vlanPcp;
253 }
254
alshabib99b8fdc2014-09-25 14:30:22 -0700255 @Override
256 public String toString() {
257 return toStringHelper(subtype().toString())
258 .add("pcp", Long.toHexString(vlanPcp)).toString();
259 }
260
alshabib8ca53902014-10-07 13:11:17 -0700261 @Override
262 public int hashCode() {
Thomas Vachuska9b2da212014-11-10 19:30:25 -0800263 return Objects.hash(type(), subtype(), vlanPcp);
alshabib8ca53902014-10-07 13:11:17 -0700264 }
265
266 @Override
267 public boolean equals(Object obj) {
268 if (this == obj) {
269 return true;
270 }
271 if (obj instanceof ModVlanPcpInstruction) {
272 ModVlanPcpInstruction that = (ModVlanPcpInstruction) obj;
alshabib2020b892014-10-20 15:47:08 -0700273 return Objects.equals(vlanPcp, that.vlanPcp) &&
274 Objects.equals(this.type(), that.type()) &&
275 Objects.equals(this.subtype(), that.subtype());
alshabib8ca53902014-10-07 13:11:17 -0700276
277 }
278 return false;
279 }
280
alshabib7410fea2014-09-16 13:48:39 -0700281 }
282
alshabib55a55d92014-09-16 11:59:31 -0700283
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -0800284 /**
285 * Represents a MPLS label modification.
286 */
287 public static final class ModMplsLabelInstruction extends
288 L2ModificationInstruction {
289
290 private final Integer mplsLabel;
291
292 public ModMplsLabelInstruction(Integer mplsLabel) {
293 this.mplsLabel = mplsLabel;
294 }
295
296 public Integer label() {
297 return mplsLabel;
298 }
299
300 @Override
301 public L2SubType subtype() {
302 return L2SubType.MPLS_LABEL;
303 }
304
305 @Override
306 public String toString() {
Yuta HIGUCHIcca5e722015-02-05 14:00:41 -0800307 return toStringHelper(subtype().toString())
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -0800308 .add("mpls", mplsLabel.intValue()).toString();
309 }
310
311 @Override
312 public int hashCode() {
Yuta HIGUCHIcca5e722015-02-05 14:00:41 -0800313 return Objects.hash(type(), subtype(), mplsLabel);
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -0800314 }
315
316 @Override
317 public boolean equals(Object obj) {
318 if (this == obj) {
319 return true;
320 }
321 if (obj instanceof ModMplsLabelInstruction) {
322 ModMplsLabelInstruction that = (ModMplsLabelInstruction) obj;
323 return Objects.equals(mplsLabel, that.mplsLabel) &&
324 Objects.equals(this.type(), that.type());
325
326
327 }
328 return false;
329 }
330 }
sangho3f97a17d2015-01-29 22:56:29 -0800331
332 /**
Yuta HIGUCHIcca5e722015-02-05 14:00:41 -0800333 * Represents a MPLS TTL modification.
sangho3f97a17d2015-01-29 22:56:29 -0800334 */
335 public static final class ModMplsTtlInstruction extends
336 L2ModificationInstruction {
337
338 public ModMplsTtlInstruction() {
339 }
340
341 @Override
342 public L2SubType subtype() {
343 return L2SubType.DEC_MPLS_TTL;
344 }
345
346 @Override
347 public String toString() {
Yuta HIGUCHIcca5e722015-02-05 14:00:41 -0800348 return toStringHelper(subtype().toString())
349 .toString();
sangho3f97a17d2015-01-29 22:56:29 -0800350 }
351
352 @Override
353 public int hashCode() {
Yuta HIGUCHIcca5e722015-02-05 14:00:41 -0800354 return Objects.hash(type(), subtype());
sangho3f97a17d2015-01-29 22:56:29 -0800355 }
356
357 @Override
358 public boolean equals(Object obj) {
359 if (this == obj) {
360 return true;
361 }
Yuta HIGUCHIcca5e722015-02-05 14:00:41 -0800362 if (obj instanceof ModMplsTtlInstruction) {
363 return true;
sangho3f97a17d2015-01-29 22:56:29 -0800364 }
365 return false;
366 }
367 }
alshabib55a55d92014-09-16 11:59:31 -0700368}