blob: b005bfcde68f57a3a86231da3506261b90f84e4d [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
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -080022import org.onlab.packet.Ethernet;
Ayaka Koshibea9c199f2014-09-16 16:21:40 -070023import org.onlab.packet.MacAddress;
24import 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 */
73 DEC_MPLS_TTL
74
alshabib55a55d92014-09-16 11:59:31 -070075 }
76
77 // TODO: Create factory class 'Instructions' that will have various factory
78 // to create specific instructions.
79
alshabib35edb1a2014-09-16 17:44:44 -070080 public abstract L2SubType subtype();
alshabib55a55d92014-09-16 11:59:31 -070081
82 @Override
83 public Type type() {
alshabib35edb1a2014-09-16 17:44:44 -070084 return Type.L2MODIFICATION;
alshabib55a55d92014-09-16 11:59:31 -070085 }
86
87 /**
88 * Represents a L2 src/dst modification instruction.
89 */
90 public static final class ModEtherInstruction extends L2ModificationInstruction {
91
alshabib35edb1a2014-09-16 17:44:44 -070092 private final L2SubType subtype;
Ayaka Koshibea9c199f2014-09-16 16:21:40 -070093 private final MacAddress mac;
alshabib55a55d92014-09-16 11:59:31 -070094
alshabib64231f62014-09-16 17:58:36 -070095 public ModEtherInstruction(L2SubType subType, MacAddress addr) {
96
alshabib55a55d92014-09-16 11:59:31 -070097 this.subtype = subType;
98 this.mac = addr;
99 }
100
101 @Override
alshabib35edb1a2014-09-16 17:44:44 -0700102 public L2SubType subtype() {
alshabib55a55d92014-09-16 11:59:31 -0700103 return this.subtype;
104 }
105
Ayaka Koshibea9c199f2014-09-16 16:21:40 -0700106 public MacAddress mac() {
alshabib55a55d92014-09-16 11:59:31 -0700107 return this.mac;
108 }
109
alshabib99b8fdc2014-09-25 14:30:22 -0700110 @Override
111 public String toString() {
112 return toStringHelper(subtype().toString())
113 .add("mac", mac).toString();
114 }
115
alshabib8ca53902014-10-07 13:11:17 -0700116 @Override
117 public int hashCode() {
Thomas Vachuska9b2da212014-11-10 19:30:25 -0800118 return Objects.hash(type(), subtype, mac);
alshabib8ca53902014-10-07 13:11:17 -0700119 }
120
121 @Override
122 public boolean equals(Object obj) {
123 if (this == obj) {
124 return true;
125 }
126 if (obj instanceof ModEtherInstruction) {
127 ModEtherInstruction that = (ModEtherInstruction) obj;
128 return Objects.equals(mac, that.mac) &&
alshabib2020b892014-10-20 15:47:08 -0700129 Objects.equals(this.type(), that.type()) &&
alshabib8ca53902014-10-07 13:11:17 -0700130 Objects.equals(subtype, that.subtype);
131
132 }
133 return false;
134 }
135
alshabib99b8fdc2014-09-25 14:30:22 -0700136
alshabib55a55d92014-09-16 11:59:31 -0700137 }
138
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -0800139 public static final class PushHeaderInstructions extends
140 L2ModificationInstruction {
141
142 private final L2SubType subtype;
143 private final Ethernet ethernetType;
144
145 public PushHeaderInstructions(L2SubType subType, Ethernet ethernetType) {
146 this.subtype = subType;
147 this.ethernetType = ethernetType;
148 }
149
150 public Ethernet ethernetType() {
151 return ethernetType;
152 }
153
154 @Override
155 public L2SubType subtype() {
156 return this.subtype;
157 }
158
159 @Override
160 public String toString() {
161 return toStringHelper(subtype().toString()).toString();
162 }
163
164 @Override
165 public int hashCode() {
166 return Objects.hash(type(), subtype);
167 }
168
169 @Override
170 public boolean equals(Object obj) {
171 if (this == obj) {
172 return true;
173 }
174 if (obj instanceof PushHeaderInstructions) {
175 PushHeaderInstructions that = (PushHeaderInstructions) obj;
176 return Objects.equals(this.type(), that.type()) &&
177 Objects.equals(subtype, that.subtype);
178
179 }
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() {
307 return toStringHelper(type().toString())
308 .add("mpls", mplsLabel.intValue()).toString();
309 }
310
311 @Override
312 public int hashCode() {
313 return Objects.hash(mplsLabel);
314 }
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 /**
333 * Represents a MPLS label modification.
334 */
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() {
348 return type().toString();
349 }
350
351 @Override
352 public int hashCode() {
353 return Objects.hash(type(), L2SubType.DEC_MPLS_TTL);
354 }
355
356 @Override
357 public boolean equals(Object obj) {
358 if (this == obj) {
359 return true;
360 }
361 if (obj instanceof ModMplsLabelInstruction) {
362 ModMplsTtlInstruction that = (ModMplsTtlInstruction) obj;
363 return Objects.equals(this.type(), that.type());
364 }
365 return false;
366 }
367 }
alshabib55a55d92014-09-16 11:59:31 -0700368}