blob: 22888984bda8da9ac4c9cd2aa3e3edc9b6858689 [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
Yuta HIGUCHI6a479642015-02-08 01:28:50 -080083 public final 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
Yuta HIGUCHI6a479642015-02-08 01:28:50 -080095 ModEtherInstruction(L2SubType subType, MacAddress addr) {
alshabib64231f62014-09-16 17:58:36 -070096
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) &&
129 Objects.equals(subtype, that.subtype);
alshabib8ca53902014-10-07 13:11:17 -0700130 }
131 return false;
132 }
alshabib55a55d92014-09-16 11:59:31 -0700133 }
134
Yuta HIGUCHI6a479642015-02-08 01:28:50 -0800135 // TODO This instruction is reused for Pop-Mpls. Consider renaming.
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -0800136 public static final class PushHeaderInstructions extends
137 L2ModificationInstruction {
138
139 private final L2SubType subtype;
Yuta HIGUCHI32a53c52015-02-08 01:25:40 -0800140 private final short ethernetType; // uint16_t
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -0800141
Yuta HIGUCHI32a53c52015-02-08 01:25:40 -0800142 PushHeaderInstructions(L2SubType subType, short ethernetType) {
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -0800143 this.subtype = subType;
144 this.ethernetType = ethernetType;
145 }
146
Yuta HIGUCHI32a53c52015-02-08 01:25:40 -0800147 public int ethernetType() {
148 return Short.toUnsignedInt(ethernetType);
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -0800149 }
150
151 @Override
152 public L2SubType subtype() {
153 return this.subtype;
154 }
155
156 @Override
157 public String toString() {
Yuta HIGUCHI32a53c52015-02-08 01:25:40 -0800158 return toStringHelper(subtype().toString())
159 .add("ethernetType", String.format("0x%04x", ethernetType()))
160 .toString();
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -0800161 }
162
163 @Override
164 public int hashCode() {
Yuta HIGUCHI32a53c52015-02-08 01:25:40 -0800165 return Objects.hash(type(), subtype, ethernetType);
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -0800166 }
167
168 @Override
169 public boolean equals(Object obj) {
170 if (this == obj) {
171 return true;
172 }
173 if (obj instanceof PushHeaderInstructions) {
174 PushHeaderInstructions that = (PushHeaderInstructions) obj;
Yuta HIGUCHI32a53c52015-02-08 01:25:40 -0800175 return Objects.equals(subtype, that.subtype) &&
176 Objects.equals(this.ethernetType, that.ethernetType);
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -0800177 }
178 return false;
179 }
180 }
181
182
183
alshabib55a55d92014-09-16 11:59:31 -0700184 /**
alshabib55a55d92014-09-16 11:59:31 -0700185 * Represents a VLAN id modification instruction.
186 */
187 public static final class ModVlanIdInstruction extends L2ModificationInstruction {
188
Ray Milkey78081052014-11-05 10:38:12 -0800189 private final VlanId vlanId;
alshabib55a55d92014-09-16 11:59:31 -0700190
Yuta HIGUCHI6a479642015-02-08 01:28:50 -0800191 ModVlanIdInstruction(VlanId vlanId) {
alshabib55a55d92014-09-16 11:59:31 -0700192 this.vlanId = vlanId;
193 }
194
195 @Override
alshabib35edb1a2014-09-16 17:44:44 -0700196 public L2SubType subtype() {
alshabib7410fea2014-09-16 13:48:39 -0700197 return L2SubType.VLAN_ID;
alshabib55a55d92014-09-16 11:59:31 -0700198 }
199
Ayaka Koshibea9c199f2014-09-16 16:21:40 -0700200 public VlanId vlanId() {
alshabib55a55d92014-09-16 11:59:31 -0700201 return this.vlanId;
202 }
203
alshabib99b8fdc2014-09-25 14:30:22 -0700204 @Override
205 public String toString() {
206 return toStringHelper(subtype().toString())
207 .add("id", vlanId).toString();
208 }
209
alshabib8ca53902014-10-07 13:11:17 -0700210 @Override
211 public int hashCode() {
Thomas Vachuska9b2da212014-11-10 19:30:25 -0800212 return Objects.hash(type(), subtype(), vlanId);
alshabib8ca53902014-10-07 13:11:17 -0700213 }
214
215 @Override
216 public boolean equals(Object obj) {
217 if (this == obj) {
218 return true;
219 }
220 if (obj instanceof ModVlanIdInstruction) {
221 ModVlanIdInstruction that = (ModVlanIdInstruction) obj;
Yuta HIGUCHI6a479642015-02-08 01:28:50 -0800222 return Objects.equals(vlanId, that.vlanId);
alshabib8ca53902014-10-07 13:11:17 -0700223 }
224 return false;
225 }
alshabib55a55d92014-09-16 11:59:31 -0700226 }
227
alshabib7410fea2014-09-16 13:48:39 -0700228 /**
229 * Represents a VLAN PCP modification instruction.
230 */
231 public static final class ModVlanPcpInstruction extends L2ModificationInstruction {
232
Ray Milkey78081052014-11-05 10:38:12 -0800233 private final Byte vlanPcp;
alshabib7410fea2014-09-16 13:48:39 -0700234
Yuta HIGUCHI6a479642015-02-08 01:28:50 -0800235 ModVlanPcpInstruction(Byte vlanPcp) {
alshabib7410fea2014-09-16 13:48:39 -0700236 this.vlanPcp = vlanPcp;
237 }
238
239 @Override
alshabib35edb1a2014-09-16 17:44:44 -0700240 public L2SubType subtype() {
alshabib7410fea2014-09-16 13:48:39 -0700241 return L2SubType.VLAN_PCP;
242 }
243
244 public Byte vlanPcp() {
245 return this.vlanPcp;
246 }
247
alshabib99b8fdc2014-09-25 14:30:22 -0700248 @Override
249 public String toString() {
250 return toStringHelper(subtype().toString())
251 .add("pcp", Long.toHexString(vlanPcp)).toString();
252 }
253
alshabib8ca53902014-10-07 13:11:17 -0700254 @Override
255 public int hashCode() {
Thomas Vachuska9b2da212014-11-10 19:30:25 -0800256 return Objects.hash(type(), subtype(), vlanPcp);
alshabib8ca53902014-10-07 13:11:17 -0700257 }
258
259 @Override
260 public boolean equals(Object obj) {
261 if (this == obj) {
262 return true;
263 }
264 if (obj instanceof ModVlanPcpInstruction) {
265 ModVlanPcpInstruction that = (ModVlanPcpInstruction) obj;
Yuta HIGUCHI6a479642015-02-08 01:28:50 -0800266 return Objects.equals(vlanPcp, that.vlanPcp);
alshabib8ca53902014-10-07 13:11:17 -0700267 }
268 return false;
269 }
alshabib7410fea2014-09-16 13:48:39 -0700270 }
271
alshabib55a55d92014-09-16 11:59:31 -0700272
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -0800273 /**
274 * Represents a MPLS label modification.
275 */
Yuta HIGUCHI6a479642015-02-08 01:28:50 -0800276 public static final class ModMplsLabelInstruction
277 extends L2ModificationInstruction {
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -0800278
Michele Santuari4b6019e2014-12-19 11:31:45 +0100279 private final MplsLabel mplsLabel;
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -0800280
Yuta HIGUCHI6a479642015-02-08 01:28:50 -0800281 ModMplsLabelInstruction(MplsLabel mplsLabel) {
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -0800282 this.mplsLabel = mplsLabel;
283 }
284
285 public Integer label() {
Michele Santuari4b6019e2014-12-19 11:31:45 +0100286 return mplsLabel.toInt();
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -0800287 }
288
289 @Override
290 public L2SubType subtype() {
291 return L2SubType.MPLS_LABEL;
292 }
293
294 @Override
295 public String toString() {
Pavlin Radoslavov3ebe1702015-02-17 09:53:17 -0800296 return toStringHelper(subtype().toString())
Michele Santuari4b6019e2014-12-19 11:31:45 +0100297 .add("mpls", mplsLabel).toString();
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -0800298 }
299
300 @Override
301 public int hashCode() {
Yuta HIGUCHIcca5e722015-02-05 14:00:41 -0800302 return Objects.hash(type(), subtype(), mplsLabel);
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -0800303 }
304
305 @Override
306 public boolean equals(Object obj) {
307 if (this == obj) {
308 return true;
309 }
310 if (obj instanceof ModMplsLabelInstruction) {
311 ModMplsLabelInstruction that = (ModMplsLabelInstruction) obj;
Yuta HIGUCHI6a479642015-02-08 01:28:50 -0800312 return Objects.equals(mplsLabel, that.mplsLabel);
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -0800313 }
314 return false;
315 }
316 }
sangho3f97a17d2015-01-29 22:56:29 -0800317
318 /**
Yuta HIGUCHIcca5e722015-02-05 14:00:41 -0800319 * Represents a MPLS TTL modification.
sangho3f97a17d2015-01-29 22:56:29 -0800320 */
Yuta HIGUCHI6a479642015-02-08 01:28:50 -0800321 public static final class ModMplsTtlInstruction
322 extends L2ModificationInstruction {
sangho3f97a17d2015-01-29 22:56:29 -0800323
Yuta HIGUCHI6a479642015-02-08 01:28:50 -0800324 ModMplsTtlInstruction() {
sangho3f97a17d2015-01-29 22:56:29 -0800325 }
326
327 @Override
328 public L2SubType subtype() {
329 return L2SubType.DEC_MPLS_TTL;
330 }
331
332 @Override
333 public String toString() {
Yuta HIGUCHIcca5e722015-02-05 14:00:41 -0800334 return toStringHelper(subtype().toString())
335 .toString();
sangho3f97a17d2015-01-29 22:56:29 -0800336 }
337
338 @Override
339 public int hashCode() {
Yuta HIGUCHIcca5e722015-02-05 14:00:41 -0800340 return Objects.hash(type(), subtype());
sangho3f97a17d2015-01-29 22:56:29 -0800341 }
342
343 @Override
344 public boolean equals(Object obj) {
345 if (this == obj) {
346 return true;
347 }
Yuta HIGUCHIcca5e722015-02-05 14:00:41 -0800348 if (obj instanceof ModMplsTtlInstruction) {
349 return true;
sangho3f97a17d2015-01-29 22:56:29 -0800350 }
351 return false;
352 }
353 }
alshabib55a55d92014-09-16 11:59:31 -0700354}