blob: 513555722b22baadbde5af5cf6dbb4435d82ccfa [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 */
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;
Yuta HIGUCHI32a53c52015-02-08 01:25:40 -0800143 private final short ethernetType; // uint16_t
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -0800144
Yuta HIGUCHI32a53c52015-02-08 01:25:40 -0800145 PushHeaderInstructions(L2SubType subType, short ethernetType) {
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -0800146 this.subtype = subType;
147 this.ethernetType = ethernetType;
148 }
149
Yuta HIGUCHI32a53c52015-02-08 01:25:40 -0800150 public int ethernetType() {
151 return Short.toUnsignedInt(ethernetType);
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -0800152 }
153
154 @Override
155 public L2SubType subtype() {
156 return this.subtype;
157 }
158
159 @Override
160 public String toString() {
Yuta HIGUCHI32a53c52015-02-08 01:25:40 -0800161 return toStringHelper(subtype().toString())
162 .add("ethernetType", String.format("0x%04x", ethernetType()))
163 .toString();
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -0800164 }
165
166 @Override
167 public int hashCode() {
Yuta HIGUCHI32a53c52015-02-08 01:25:40 -0800168 return Objects.hash(type(), subtype, ethernetType);
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -0800169 }
170
171 @Override
172 public boolean equals(Object obj) {
173 if (this == obj) {
174 return true;
175 }
176 if (obj instanceof PushHeaderInstructions) {
177 PushHeaderInstructions that = (PushHeaderInstructions) obj;
Yuta HIGUCHI32a53c52015-02-08 01:25:40 -0800178 return Objects.equals(subtype, that.subtype) &&
179 Objects.equals(this.ethernetType, that.ethernetType);
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -0800180 }
181 return false;
182 }
183 }
184
185
186
alshabib55a55d92014-09-16 11:59:31 -0700187 /**
alshabib55a55d92014-09-16 11:59:31 -0700188 * Represents a VLAN id modification instruction.
189 */
190 public static final class ModVlanIdInstruction extends L2ModificationInstruction {
191
Ray Milkey78081052014-11-05 10:38:12 -0800192 private final VlanId vlanId;
alshabib55a55d92014-09-16 11:59:31 -0700193
Ayaka Koshibea9c199f2014-09-16 16:21:40 -0700194 public ModVlanIdInstruction(VlanId vlanId) {
alshabib55a55d92014-09-16 11:59:31 -0700195 this.vlanId = vlanId;
196 }
197
198 @Override
alshabib35edb1a2014-09-16 17:44:44 -0700199 public L2SubType subtype() {
alshabib7410fea2014-09-16 13:48:39 -0700200 return L2SubType.VLAN_ID;
alshabib55a55d92014-09-16 11:59:31 -0700201 }
202
Ayaka Koshibea9c199f2014-09-16 16:21:40 -0700203 public VlanId vlanId() {
alshabib55a55d92014-09-16 11:59:31 -0700204 return this.vlanId;
205 }
206
alshabib99b8fdc2014-09-25 14:30:22 -0700207 @Override
208 public String toString() {
209 return toStringHelper(subtype().toString())
210 .add("id", vlanId).toString();
211 }
212
alshabib8ca53902014-10-07 13:11:17 -0700213 @Override
214 public int hashCode() {
Thomas Vachuska9b2da212014-11-10 19:30:25 -0800215 return Objects.hash(type(), subtype(), vlanId);
alshabib8ca53902014-10-07 13:11:17 -0700216 }
217
218 @Override
219 public boolean equals(Object obj) {
220 if (this == obj) {
221 return true;
222 }
223 if (obj instanceof ModVlanIdInstruction) {
224 ModVlanIdInstruction that = (ModVlanIdInstruction) obj;
alshabib2020b892014-10-20 15:47:08 -0700225 return Objects.equals(vlanId, that.vlanId) &&
226 Objects.equals(this.type(), that.type()) &&
227 Objects.equals(this.subtype(), that.subtype());
alshabib8ca53902014-10-07 13:11:17 -0700228
229 }
230 return false;
231 }
232
233
alshabib55a55d92014-09-16 11:59:31 -0700234 }
235
alshabib7410fea2014-09-16 13:48:39 -0700236 /**
237 * Represents a VLAN PCP modification instruction.
238 */
239 public static final class ModVlanPcpInstruction extends L2ModificationInstruction {
240
Ray Milkey78081052014-11-05 10:38:12 -0800241 private final Byte vlanPcp;
alshabib7410fea2014-09-16 13:48:39 -0700242
243 public ModVlanPcpInstruction(Byte vlanPcp) {
244 this.vlanPcp = vlanPcp;
245 }
246
247 @Override
alshabib35edb1a2014-09-16 17:44:44 -0700248 public L2SubType subtype() {
alshabib7410fea2014-09-16 13:48:39 -0700249 return L2SubType.VLAN_PCP;
250 }
251
252 public Byte vlanPcp() {
253 return this.vlanPcp;
254 }
255
alshabib99b8fdc2014-09-25 14:30:22 -0700256 @Override
257 public String toString() {
258 return toStringHelper(subtype().toString())
259 .add("pcp", Long.toHexString(vlanPcp)).toString();
260 }
261
alshabib8ca53902014-10-07 13:11:17 -0700262 @Override
263 public int hashCode() {
Thomas Vachuska9b2da212014-11-10 19:30:25 -0800264 return Objects.hash(type(), subtype(), vlanPcp);
alshabib8ca53902014-10-07 13:11:17 -0700265 }
266
267 @Override
268 public boolean equals(Object obj) {
269 if (this == obj) {
270 return true;
271 }
272 if (obj instanceof ModVlanPcpInstruction) {
273 ModVlanPcpInstruction that = (ModVlanPcpInstruction) obj;
alshabib2020b892014-10-20 15:47:08 -0700274 return Objects.equals(vlanPcp, that.vlanPcp) &&
275 Objects.equals(this.type(), that.type()) &&
276 Objects.equals(this.subtype(), that.subtype());
alshabib8ca53902014-10-07 13:11:17 -0700277
278 }
279 return false;
280 }
281
alshabib7410fea2014-09-16 13:48:39 -0700282 }
283
alshabib55a55d92014-09-16 11:59:31 -0700284
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -0800285 /**
286 * Represents a MPLS label modification.
287 */
288 public static final class ModMplsLabelInstruction extends
289 L2ModificationInstruction {
290
Michele Santuari4b6019e2014-12-19 11:31:45 +0100291 private final MplsLabel mplsLabel;
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -0800292
Michele Santuari4b6019e2014-12-19 11:31:45 +0100293 public ModMplsLabelInstruction(MplsLabel mplsLabel) {
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -0800294 this.mplsLabel = mplsLabel;
295 }
296
297 public Integer label() {
Michele Santuari4b6019e2014-12-19 11:31:45 +0100298 return mplsLabel.toInt();
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -0800299 }
300
301 @Override
302 public L2SubType subtype() {
303 return L2SubType.MPLS_LABEL;
304 }
305
306 @Override
307 public String toString() {
Pavlin Radoslavov3ebe1702015-02-17 09:53:17 -0800308 return toStringHelper(subtype().toString())
Michele Santuari4b6019e2014-12-19 11:31:45 +0100309 .add("mpls", mplsLabel).toString();
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -0800310 }
311
312 @Override
313 public int hashCode() {
Yuta HIGUCHIcca5e722015-02-05 14:00:41 -0800314 return Objects.hash(type(), subtype(), mplsLabel);
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -0800315 }
316
317 @Override
318 public boolean equals(Object obj) {
319 if (this == obj) {
320 return true;
321 }
322 if (obj instanceof ModMplsLabelInstruction) {
323 ModMplsLabelInstruction that = (ModMplsLabelInstruction) obj;
324 return Objects.equals(mplsLabel, that.mplsLabel) &&
325 Objects.equals(this.type(), that.type());
326
327
328 }
329 return false;
330 }
331 }
sangho3f97a17d2015-01-29 22:56:29 -0800332
333 /**
Yuta HIGUCHIcca5e722015-02-05 14:00:41 -0800334 * Represents a MPLS TTL modification.
sangho3f97a17d2015-01-29 22:56:29 -0800335 */
336 public static final class ModMplsTtlInstruction extends
337 L2ModificationInstruction {
338
339 public ModMplsTtlInstruction() {
340 }
341
342 @Override
343 public L2SubType subtype() {
344 return L2SubType.DEC_MPLS_TTL;
345 }
346
347 @Override
348 public String toString() {
Yuta HIGUCHIcca5e722015-02-05 14:00:41 -0800349 return toStringHelper(subtype().toString())
350 .toString();
sangho3f97a17d2015-01-29 22:56:29 -0800351 }
352
353 @Override
354 public int hashCode() {
Yuta HIGUCHIcca5e722015-02-05 14:00:41 -0800355 return Objects.hash(type(), subtype());
sangho3f97a17d2015-01-29 22:56:29 -0800356 }
357
358 @Override
359 public boolean equals(Object obj) {
360 if (this == obj) {
361 return true;
362 }
Yuta HIGUCHIcca5e722015-02-05 14:00:41 -0800363 if (obj instanceof ModMplsTtlInstruction) {
364 return true;
sangho3f97a17d2015-01-29 22:56:29 -0800365 }
366 return false;
367 }
368 }
alshabib55a55d92014-09-16 11:59:31 -0700369}