blob: 09ec53600c5b635931ff2369c9a9a828a356e533 [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;
alshabib55a55d92014-09-16 11:59:31 -070019import static com.google.common.base.Preconditions.checkNotNull;
Brian O'Connorabafb502014-12-02 22:26:20 -080020import static org.onosproject.net.flow.instructions.L2ModificationInstruction.*;
alshabib55a55d92014-09-16 11:59:31 -070021
alshabib8ca53902014-10-07 13:11:17 -070022import java.util.Objects;
23
sangho8995ac52015-02-04 11:29:03 -080024import org.onosproject.core.GroupId;
Brian O'Connorabafb502014-12-02 22:26:20 -080025import org.onosproject.net.PortNumber;
26import org.onosproject.net.flow.instructions.L0ModificationInstruction.L0SubType;
27import org.onosproject.net.flow.instructions.L0ModificationInstruction.ModLambdaInstruction;
28import org.onosproject.net.flow.instructions.L2ModificationInstruction.L2SubType;
29import org.onosproject.net.flow.instructions.L2ModificationInstruction.ModEtherInstruction;
30import org.onosproject.net.flow.instructions.L3ModificationInstruction.L3SubType;
31import org.onosproject.net.flow.instructions.L3ModificationInstruction.ModIPInstruction;
Pavlin Radoslavovfebe82c2015-02-11 19:08:15 -080032import org.onosproject.net.flow.instructions.L3ModificationInstruction.ModIPv6FlowLabelInstruction;
sangho3f97a17d2015-01-29 22:56:29 -080033import org.onosproject.net.flow.instructions.L3ModificationInstruction.ModTtlInstruction;
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -080034
35import org.onlab.packet.Ethernet;
Pavlin Radoslavov855ea2d2014-10-30 15:32:39 -070036import org.onlab.packet.IpAddress;
Ayaka Koshibea9c199f2014-09-16 16:21:40 -070037import org.onlab.packet.MacAddress;
Michele Santuari4b6019e2014-12-19 11:31:45 +010038import org.onlab.packet.MplsLabel;
Ayaka Koshibea9c199f2014-09-16 16:21:40 -070039import org.onlab.packet.VlanId;
alshabib64231f62014-09-16 17:58:36 -070040
alshabib55a55d92014-09-16 11:59:31 -070041/**
42 * Factory class for creating various traffic treatment instructions.
43 */
44public final class Instructions {
45
46
47 // Ban construction
48 private Instructions() {}
49
50 /**
51 * Creates an output instruction using the specified port number. This can
52 * include logical ports such as CONTROLLER, FLOOD, etc.
53 *
54 * @param number port number
55 * @return output instruction
56 */
57 public static OutputInstruction createOutput(final PortNumber number) {
58 checkNotNull(number, "PortNumber cannot be null");
59 return new OutputInstruction(number);
60 }
61
62 /**
63 * Creates a drop instruction.
64 * @return drop instruction
65 */
66 public static DropInstruction createDrop() {
67 return new DropInstruction();
68 }
69
70 /**
sangho8995ac52015-02-04 11:29:03 -080071 * Creates a group instruction.
72 *
73 * @param groupId Group Id
74 * @return group instruction
75 */
76 public static GroupInstruction createGroup(final GroupId groupId) {
77 checkNotNull(groupId, "GroupId cannot be null");
78 return new GroupInstruction(groupId);
79 }
80
81 /**
Marc De Leenheer49087752014-10-23 13:54:09 -070082 * Creates a l0 modification.
83 * @param lambda the lambda to modify to.
84 * @return a l0 modification
85 */
86 public static L0ModificationInstruction modL0Lambda(short lambda) {
87 checkNotNull(lambda, "L0 lambda cannot be null");
88 return new ModLambdaInstruction(L0SubType.LAMBDA, lambda);
89 }
90
91 /**
alshabib55a55d92014-09-16 11:59:31 -070092 * Creates a l2 src modification.
93 * @param addr the mac address to modify to.
94 * @return a l2 modification
95 */
Ayaka Koshibea9c199f2014-09-16 16:21:40 -070096 public static L2ModificationInstruction modL2Src(MacAddress addr) {
alshabib55a55d92014-09-16 11:59:31 -070097 checkNotNull(addr, "Src l2 address cannot be null");
alshabib99b8fdc2014-09-25 14:30:22 -070098 return new ModEtherInstruction(L2SubType.ETH_SRC, addr);
alshabib55a55d92014-09-16 11:59:31 -070099 }
100
101 /**
102 * Creates a L2 dst modification.
103 * @param addr the mac address to modify to.
104 * @return a L2 modification
105 */
Ayaka Koshibea9c199f2014-09-16 16:21:40 -0700106 public static L2ModificationInstruction modL2Dst(MacAddress addr) {
alshabib55a55d92014-09-16 11:59:31 -0700107 checkNotNull(addr, "Dst l2 address cannot be null");
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -0800108 return new ModEtherInstruction(L2SubType.ETH_DST, addr);
alshabib55a55d92014-09-16 11:59:31 -0700109 }
110
111 /**
alshabib7410fea2014-09-16 13:48:39 -0700112 * Creates a Vlan id modification.
113 * @param vlanId the vlan id to modify to.
114 * @return a L2 modification
115 */
Ayaka Koshibea9c199f2014-09-16 16:21:40 -0700116 public static L2ModificationInstruction modVlanId(VlanId vlanId) {
alshabib55a55d92014-09-16 11:59:31 -0700117 checkNotNull(vlanId, "VLAN id cannot be null");
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -0800118 return new ModVlanIdInstruction(vlanId);
alshabib55a55d92014-09-16 11:59:31 -0700119 }
120
alshabib7410fea2014-09-16 13:48:39 -0700121 /**
122 * Creates a Vlan pcp modification.
123 * @param vlanPcp the pcp to modify to.
124 * @return a L2 modification
125 */
126 public static L2ModificationInstruction modVlanPcp(Byte vlanPcp) {
127 checkNotNull(vlanPcp, "VLAN Pcp cannot be null");
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -0800128 return new ModVlanPcpInstruction(vlanPcp);
alshabib7410fea2014-09-16 13:48:39 -0700129 }
130
131 /**
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -0800132 * Creates a MPLS label modification.
133 * @param mplsLabel to set.
134 * @return a L2 Modification
135 */
Michele Santuari4b6019e2014-12-19 11:31:45 +0100136 public static L2ModificationInstruction modMplsLabel(MplsLabel mplsLabel) {
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -0800137 checkNotNull(mplsLabel, "MPLS label cannot be null");
138 return new ModMplsLabelInstruction(mplsLabel);
139 }
sangho3f97a17d2015-01-29 22:56:29 -0800140
141 /**
142 * Creates a MPLS TTL modification.
143 *
144 * @return a L2 Modification
145 */
146 public static L2ModificationInstruction decMplsTtl() {
147 return new ModMplsTtlInstruction();
148 }
Pavlin Radoslavovfebe82c2015-02-11 19:08:15 -0800149
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -0800150 /**
Pavlin Radoslavovfebe82c2015-02-11 19:08:15 -0800151 * Creates a L3 IPv4 src modification.
152 *
153 * @param addr the IPv4 address to modify to
alshabib7410fea2014-09-16 13:48:39 -0700154 * @return a L3 modification
155 */
Pavlin Radoslavov855ea2d2014-10-30 15:32:39 -0700156 public static L3ModificationInstruction modL3Src(IpAddress addr) {
Pavlin Radoslavovfebe82c2015-02-11 19:08:15 -0800157 checkNotNull(addr, "Src l3 IPv4 address cannot be null");
158 return new ModIPInstruction(L3SubType.IPV4_SRC, addr);
alshabib7410fea2014-09-16 13:48:39 -0700159 }
160
161 /**
Pavlin Radoslavovfebe82c2015-02-11 19:08:15 -0800162 * Creates a L3 IPv4 dst modification.
163 *
164 * @param addr the IPv4 address to modify to
alshabib7410fea2014-09-16 13:48:39 -0700165 * @return a L3 modification
166 */
Pavlin Radoslavov855ea2d2014-10-30 15:32:39 -0700167 public static L3ModificationInstruction modL3Dst(IpAddress addr) {
Pavlin Radoslavovfebe82c2015-02-11 19:08:15 -0800168 checkNotNull(addr, "Dst l3 IPv4 address cannot be null");
169 return new ModIPInstruction(L3SubType.IPV4_DST, addr);
170 }
171
172 /**
173 * Creates a L3 IPv6 src modification.
174 *
175 * @param addr the IPv6 address to modify to
176 * @return a L3 modification
177 */
178 public static L3ModificationInstruction modL3IPv6Src(IpAddress addr) {
179 checkNotNull(addr, "Src l3 IPv6 address cannot be null");
180 return new ModIPInstruction(L3SubType.IPV6_SRC, addr);
181 }
182
183 /**
184 * Creates a L3 IPv6 dst modification.
185 *
186 * @param addr the IPv6 address to modify to
187 * @return a L3 modification
188 */
189 public static L3ModificationInstruction modL3IPv6Dst(IpAddress addr) {
190 checkNotNull(addr, "Dst l3 IPv6 address cannot be null");
191 return new ModIPInstruction(L3SubType.IPV6_DST, addr);
192 }
193
194 /**
195 * Creates a L3 IPv6 Flow Label modification.
196 *
197 * @param flowLabel the IPv6 flow label to modify to (20 bits)
198 * @return a L3 modification
199 */
200 public static L3ModificationInstruction modL3IPv6FlowLabel(int flowLabel) {
201 return new ModIPv6FlowLabelInstruction(flowLabel);
alshabib7410fea2014-09-16 13:48:39 -0700202 }
203
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -0800204 /**
sangho3f97a17d2015-01-29 22:56:29 -0800205 * Creates a L3 TTL modification.
206 * @return a L3 modification
207 */
208 public static L3ModificationInstruction decNwTtl() {
209 return new ModTtlInstruction(L3SubType.DEC_TTL);
210 }
211
212 /**
213 * Creates a L3 TTL modification.
214 * @return a L3 modification
215 */
216 public static L3ModificationInstruction copyTtlOut() {
217 return new ModTtlInstruction(L3SubType.TTL_OUT);
218 }
219
220 /**
221 * Creates a L3 TTL modification.
222 * @return a L3 modification
223 */
224 public static L3ModificationInstruction copyTtlIn() {
225 return new ModTtlInstruction(L3SubType.TTL_IN);
226 }
227
228 /**
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -0800229 * Creates a mpls header instruction.
230 * @return a L2 modification.
231 */
232 public static Instruction pushMpls() {
233 return new PushHeaderInstructions(L2SubType.MPLS_PUSH,
Yuta HIGUCHI32a53c52015-02-08 01:25:40 -0800234 Ethernet.MPLS_UNICAST);
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -0800235 }
236
237 /**
238 * Creates a mpls header instruction.
239 * @return a L2 modification.
240 */
241 public static Instruction popMpls() {
242 return new PushHeaderInstructions(L2SubType.MPLS_POP,
Yuta HIGUCHI32a53c52015-02-08 01:25:40 -0800243 Ethernet.MPLS_UNICAST);
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -0800244 }
alshabib7410fea2014-09-16 13:48:39 -0700245
sangho3f97a17d2015-01-29 22:56:29 -0800246 /**
247 * Creates a mpls header instruction.
248 *
249 * @param etherType Ethernet type to set
250 * @return a L2 modification.
251 */
252 public static Instruction popMpls(Short etherType) {
253 checkNotNull(etherType, "Ethernet type cannot be null");
Yuta HIGUCHI32a53c52015-02-08 01:25:40 -0800254 return new PushHeaderInstructions(L2SubType.MPLS_POP, etherType);
sangho3f97a17d2015-01-29 22:56:29 -0800255 }
256
alshabib55a55d92014-09-16 11:59:31 -0700257 /*
sangho8995ac52015-02-04 11:29:03 -0800258 * Drop instructions
alshabib55a55d92014-09-16 11:59:31 -0700259 */
260
261 public static final class DropInstruction implements Instruction {
262 @Override
263 public Type type() {
264 return Type.DROP;
265 }
alshabib99b8fdc2014-09-25 14:30:22 -0700266
267 @Override
268 public String toString() {
269 return toStringHelper(type()).toString();
270
271 }
alshabib8ca53902014-10-07 13:11:17 -0700272
273 @Override
274 public int hashCode() {
275 return Objects.hash(type());
276 }
277
278 @Override
279 public boolean equals(Object obj) {
280 if (this == obj) {
281 return true;
282 }
283 if (obj instanceof DropInstruction) {
284 DropInstruction that = (DropInstruction) obj;
285 return Objects.equals(type(), that.type());
286
287 }
288 return false;
289 }
alshabib55a55d92014-09-16 11:59:31 -0700290 }
291
sangho8995ac52015-02-04 11:29:03 -0800292 /*
293 * Output Instruction
294 */
alshabib55a55d92014-09-16 11:59:31 -0700295
296 public static final class OutputInstruction implements Instruction {
297 private final PortNumber port;
298
299 private OutputInstruction(PortNumber port) {
300 this.port = port;
301 }
302
303 public PortNumber port() {
304 return port;
305 }
306
307 @Override
308 public Type type() {
309 return Type.OUTPUT;
310 }
alshabib99b8fdc2014-09-25 14:30:22 -0700311 @Override
312 public String toString() {
313 return toStringHelper(type().toString())
314 .add("port", port).toString();
315 }
alshabib8ca53902014-10-07 13:11:17 -0700316
317 @Override
318 public int hashCode() {
Thomas Vachuska9b2da212014-11-10 19:30:25 -0800319 return Objects.hash(type(), port);
alshabib8ca53902014-10-07 13:11:17 -0700320 }
321
322 @Override
323 public boolean equals(Object obj) {
324 if (this == obj) {
325 return true;
326 }
327 if (obj instanceof OutputInstruction) {
328 OutputInstruction that = (OutputInstruction) obj;
Yuta HIGUCHI4ce65292014-11-01 12:09:55 -0700329 return Objects.equals(port, that.port);
alshabib8ca53902014-10-07 13:11:17 -0700330
331 }
332 return false;
333 }
alshabib55a55d92014-09-16 11:59:31 -0700334 }
335
sangho8995ac52015-02-04 11:29:03 -0800336 /*
337 * Group Instruction
338 */
339
340 public static final class GroupInstruction implements Instruction {
341 private final GroupId groupId;
342
343 private GroupInstruction(GroupId groupId) {
344 this.groupId = groupId;
345 }
346
347 public GroupId groupId() {
348 return groupId;
349 }
350
351 @Override
352 public Type type() {
353 return Type.GROUP;
354 }
355 @Override
356 public String toString() {
357 return toStringHelper(type().toString())
358 .add("group ID", groupId.id()).toString();
359 }
360
361 @Override
362 public int hashCode() {
363 return Objects.hash(type(), groupId);
364 }
365
366 @Override
367 public boolean equals(Object obj) {
368 if (this == obj) {
369 return true;
370 }
371 if (obj instanceof GroupInstruction) {
372 GroupInstruction that = (GroupInstruction) obj;
373 return Objects.equals(groupId, that.groupId);
374
375 }
376 return false;
377 }
378 }
379
alshabib55a55d92014-09-16 11:59:31 -0700380}
alshabib8ca53902014-10-07 13:11:17 -0700381
382