blob: 9b027d588ad45a415b6bde912520ea7ad5d56904 [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 */
alshabib55a55d92014-09-16 11:59:31 -070016package org.onlab.onos.net.flow.instructions;
17
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 */
68 MPLS_POP
alshabib55a55d92014-09-16 11:59:31 -070069 }
70
71 // TODO: Create factory class 'Instructions' that will have various factory
72 // to create specific instructions.
73
alshabib35edb1a2014-09-16 17:44:44 -070074 public abstract L2SubType subtype();
alshabib55a55d92014-09-16 11:59:31 -070075
76 @Override
77 public Type type() {
alshabib35edb1a2014-09-16 17:44:44 -070078 return Type.L2MODIFICATION;
alshabib55a55d92014-09-16 11:59:31 -070079 }
80
81 /**
82 * Represents a L2 src/dst modification instruction.
83 */
84 public static final class ModEtherInstruction extends L2ModificationInstruction {
85
alshabib35edb1a2014-09-16 17:44:44 -070086 private final L2SubType subtype;
Ayaka Koshibea9c199f2014-09-16 16:21:40 -070087 private final MacAddress mac;
alshabib55a55d92014-09-16 11:59:31 -070088
alshabib64231f62014-09-16 17:58:36 -070089 public ModEtherInstruction(L2SubType subType, MacAddress addr) {
90
alshabib55a55d92014-09-16 11:59:31 -070091 this.subtype = subType;
92 this.mac = addr;
93 }
94
95 @Override
alshabib35edb1a2014-09-16 17:44:44 -070096 public L2SubType subtype() {
alshabib55a55d92014-09-16 11:59:31 -070097 return this.subtype;
98 }
99
Ayaka Koshibea9c199f2014-09-16 16:21:40 -0700100 public MacAddress mac() {
alshabib55a55d92014-09-16 11:59:31 -0700101 return this.mac;
102 }
103
alshabib99b8fdc2014-09-25 14:30:22 -0700104 @Override
105 public String toString() {
106 return toStringHelper(subtype().toString())
107 .add("mac", mac).toString();
108 }
109
alshabib8ca53902014-10-07 13:11:17 -0700110 @Override
111 public int hashCode() {
Thomas Vachuska9b2da212014-11-10 19:30:25 -0800112 return Objects.hash(type(), subtype, mac);
alshabib8ca53902014-10-07 13:11:17 -0700113 }
114
115 @Override
116 public boolean equals(Object obj) {
117 if (this == obj) {
118 return true;
119 }
120 if (obj instanceof ModEtherInstruction) {
121 ModEtherInstruction that = (ModEtherInstruction) obj;
122 return Objects.equals(mac, that.mac) &&
alshabib2020b892014-10-20 15:47:08 -0700123 Objects.equals(this.type(), that.type()) &&
alshabib8ca53902014-10-07 13:11:17 -0700124 Objects.equals(subtype, that.subtype);
125
126 }
127 return false;
128 }
129
alshabib99b8fdc2014-09-25 14:30:22 -0700130
alshabib55a55d92014-09-16 11:59:31 -0700131 }
132
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -0800133 public static final class PushHeaderInstructions extends
134 L2ModificationInstruction {
135
136 private final L2SubType subtype;
137 private final Ethernet ethernetType;
138
139 public PushHeaderInstructions(L2SubType subType, Ethernet ethernetType) {
140 this.subtype = subType;
141 this.ethernetType = ethernetType;
142 }
143
144 public Ethernet ethernetType() {
145 return ethernetType;
146 }
147
148 @Override
149 public L2SubType subtype() {
150 return this.subtype;
151 }
152
153 @Override
154 public String toString() {
155 return toStringHelper(subtype().toString()).toString();
156 }
157
158 @Override
159 public int hashCode() {
160 return Objects.hash(type(), subtype);
161 }
162
163 @Override
164 public boolean equals(Object obj) {
165 if (this == obj) {
166 return true;
167 }
168 if (obj instanceof PushHeaderInstructions) {
169 PushHeaderInstructions that = (PushHeaderInstructions) obj;
170 return Objects.equals(this.type(), that.type()) &&
171 Objects.equals(subtype, that.subtype);
172
173 }
174 return false;
175 }
176 }
177
178
179
alshabib55a55d92014-09-16 11:59:31 -0700180 /**
alshabib55a55d92014-09-16 11:59:31 -0700181 * Represents a VLAN id modification instruction.
182 */
183 public static final class ModVlanIdInstruction extends L2ModificationInstruction {
184
Ray Milkey78081052014-11-05 10:38:12 -0800185 private final VlanId vlanId;
alshabib55a55d92014-09-16 11:59:31 -0700186
Ayaka Koshibea9c199f2014-09-16 16:21:40 -0700187 public ModVlanIdInstruction(VlanId vlanId) {
alshabib55a55d92014-09-16 11:59:31 -0700188 this.vlanId = vlanId;
189 }
190
191 @Override
alshabib35edb1a2014-09-16 17:44:44 -0700192 public L2SubType subtype() {
alshabib7410fea2014-09-16 13:48:39 -0700193 return L2SubType.VLAN_ID;
alshabib55a55d92014-09-16 11:59:31 -0700194 }
195
Ayaka Koshibea9c199f2014-09-16 16:21:40 -0700196 public VlanId vlanId() {
alshabib55a55d92014-09-16 11:59:31 -0700197 return this.vlanId;
198 }
199
alshabib99b8fdc2014-09-25 14:30:22 -0700200 @Override
201 public String toString() {
202 return toStringHelper(subtype().toString())
203 .add("id", vlanId).toString();
204 }
205
alshabib8ca53902014-10-07 13:11:17 -0700206 @Override
207 public int hashCode() {
Thomas Vachuska9b2da212014-11-10 19:30:25 -0800208 return Objects.hash(type(), subtype(), vlanId);
alshabib8ca53902014-10-07 13:11:17 -0700209 }
210
211 @Override
212 public boolean equals(Object obj) {
213 if (this == obj) {
214 return true;
215 }
216 if (obj instanceof ModVlanIdInstruction) {
217 ModVlanIdInstruction that = (ModVlanIdInstruction) obj;
alshabib2020b892014-10-20 15:47:08 -0700218 return Objects.equals(vlanId, that.vlanId) &&
219 Objects.equals(this.type(), that.type()) &&
220 Objects.equals(this.subtype(), that.subtype());
alshabib8ca53902014-10-07 13:11:17 -0700221
222 }
223 return false;
224 }
225
226
alshabib55a55d92014-09-16 11:59:31 -0700227 }
228
alshabib7410fea2014-09-16 13:48:39 -0700229 /**
230 * Represents a VLAN PCP modification instruction.
231 */
232 public static final class ModVlanPcpInstruction extends L2ModificationInstruction {
233
Ray Milkey78081052014-11-05 10:38:12 -0800234 private final Byte vlanPcp;
alshabib7410fea2014-09-16 13:48:39 -0700235
236 public ModVlanPcpInstruction(Byte vlanPcp) {
237 this.vlanPcp = vlanPcp;
238 }
239
240 @Override
alshabib35edb1a2014-09-16 17:44:44 -0700241 public L2SubType subtype() {
alshabib7410fea2014-09-16 13:48:39 -0700242 return L2SubType.VLAN_PCP;
243 }
244
245 public Byte vlanPcp() {
246 return this.vlanPcp;
247 }
248
alshabib99b8fdc2014-09-25 14:30:22 -0700249 @Override
250 public String toString() {
251 return toStringHelper(subtype().toString())
252 .add("pcp", Long.toHexString(vlanPcp)).toString();
253 }
254
alshabib8ca53902014-10-07 13:11:17 -0700255 @Override
256 public int hashCode() {
Thomas Vachuska9b2da212014-11-10 19:30:25 -0800257 return Objects.hash(type(), subtype(), vlanPcp);
alshabib8ca53902014-10-07 13:11:17 -0700258 }
259
260 @Override
261 public boolean equals(Object obj) {
262 if (this == obj) {
263 return true;
264 }
265 if (obj instanceof ModVlanPcpInstruction) {
266 ModVlanPcpInstruction that = (ModVlanPcpInstruction) obj;
alshabib2020b892014-10-20 15:47:08 -0700267 return Objects.equals(vlanPcp, that.vlanPcp) &&
268 Objects.equals(this.type(), that.type()) &&
269 Objects.equals(this.subtype(), that.subtype());
alshabib8ca53902014-10-07 13:11:17 -0700270
271 }
272 return false;
273 }
274
alshabib7410fea2014-09-16 13:48:39 -0700275 }
276
alshabib55a55d92014-09-16 11:59:31 -0700277
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -0800278 /**
279 * Represents a MPLS label modification.
280 */
281 public static final class ModMplsLabelInstruction extends
282 L2ModificationInstruction {
283
284 private final Integer mplsLabel;
285
286 public ModMplsLabelInstruction(Integer mplsLabel) {
287 this.mplsLabel = mplsLabel;
288 }
289
290 public Integer label() {
291 return mplsLabel;
292 }
293
294 @Override
295 public L2SubType subtype() {
296 return L2SubType.MPLS_LABEL;
297 }
298
299 @Override
300 public String toString() {
301 return toStringHelper(type().toString())
302 .add("mpls", mplsLabel.intValue()).toString();
303 }
304
305 @Override
306 public int hashCode() {
307 return Objects.hash(mplsLabel);
308 }
309
310 @Override
311 public boolean equals(Object obj) {
312 if (this == obj) {
313 return true;
314 }
315 if (obj instanceof ModMplsLabelInstruction) {
316 ModMplsLabelInstruction that = (ModMplsLabelInstruction) obj;
317 return Objects.equals(mplsLabel, that.mplsLabel) &&
318 Objects.equals(this.type(), that.type());
319
320
321 }
322 return false;
323 }
324 }
alshabib55a55d92014-09-16 11:59:31 -0700325}