blob: 3838cdf1509380c564cb4d48ca236792b7a8dee4 [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;
38import org.onlab.packet.VlanId;
alshabib64231f62014-09-16 17:58:36 -070039
alshabib55a55d92014-09-16 11:59:31 -070040/**
41 * Factory class for creating various traffic treatment instructions.
42 */
43public final class Instructions {
44
45
46 // Ban construction
47 private Instructions() {}
48
49 /**
50 * Creates an output instruction using the specified port number. This can
51 * include logical ports such as CONTROLLER, FLOOD, etc.
52 *
53 * @param number port number
54 * @return output instruction
55 */
56 public static OutputInstruction createOutput(final PortNumber number) {
57 checkNotNull(number, "PortNumber cannot be null");
58 return new OutputInstruction(number);
59 }
60
61 /**
62 * Creates a drop instruction.
63 * @return drop instruction
64 */
65 public static DropInstruction createDrop() {
66 return new DropInstruction();
67 }
68
69 /**
sangho8995ac52015-02-04 11:29:03 -080070 * Creates a group instruction.
71 *
72 * @param groupId Group Id
73 * @return group instruction
74 */
75 public static GroupInstruction createGroup(final GroupId groupId) {
76 checkNotNull(groupId, "GroupId cannot be null");
77 return new GroupInstruction(groupId);
78 }
79
80 /**
Marc De Leenheer49087752014-10-23 13:54:09 -070081 * Creates a l0 modification.
82 * @param lambda the lambda to modify to.
83 * @return a l0 modification
84 */
85 public static L0ModificationInstruction modL0Lambda(short lambda) {
86 checkNotNull(lambda, "L0 lambda cannot be null");
87 return new ModLambdaInstruction(L0SubType.LAMBDA, lambda);
88 }
89
90 /**
alshabib55a55d92014-09-16 11:59:31 -070091 * Creates a l2 src modification.
92 * @param addr the mac address to modify to.
93 * @return a l2 modification
94 */
Ayaka Koshibea9c199f2014-09-16 16:21:40 -070095 public static L2ModificationInstruction modL2Src(MacAddress addr) {
alshabib55a55d92014-09-16 11:59:31 -070096 checkNotNull(addr, "Src l2 address cannot be null");
alshabib99b8fdc2014-09-25 14:30:22 -070097 return new ModEtherInstruction(L2SubType.ETH_SRC, addr);
alshabib55a55d92014-09-16 11:59:31 -070098 }
99
100 /**
101 * Creates a L2 dst modification.
102 * @param addr the mac address to modify to.
103 * @return a L2 modification
104 */
Ayaka Koshibea9c199f2014-09-16 16:21:40 -0700105 public static L2ModificationInstruction modL2Dst(MacAddress addr) {
alshabib55a55d92014-09-16 11:59:31 -0700106 checkNotNull(addr, "Dst l2 address cannot be null");
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -0800107 return new ModEtherInstruction(L2SubType.ETH_DST, addr);
alshabib55a55d92014-09-16 11:59:31 -0700108 }
109
110 /**
alshabib7410fea2014-09-16 13:48:39 -0700111 * Creates a Vlan id modification.
112 * @param vlanId the vlan id to modify to.
113 * @return a L2 modification
114 */
Ayaka Koshibea9c199f2014-09-16 16:21:40 -0700115 public static L2ModificationInstruction modVlanId(VlanId vlanId) {
alshabib55a55d92014-09-16 11:59:31 -0700116 checkNotNull(vlanId, "VLAN id cannot be null");
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -0800117 return new ModVlanIdInstruction(vlanId);
alshabib55a55d92014-09-16 11:59:31 -0700118 }
119
alshabib7410fea2014-09-16 13:48:39 -0700120 /**
121 * Creates a Vlan pcp modification.
122 * @param vlanPcp the pcp to modify to.
123 * @return a L2 modification
124 */
125 public static L2ModificationInstruction modVlanPcp(Byte vlanPcp) {
126 checkNotNull(vlanPcp, "VLAN Pcp cannot be null");
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -0800127 return new ModVlanPcpInstruction(vlanPcp);
alshabib7410fea2014-09-16 13:48:39 -0700128 }
129
130 /**
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -0800131 * Creates a MPLS label modification.
132 * @param mplsLabel to set.
133 * @return a L2 Modification
134 */
135 public static L2ModificationInstruction modMplsLabel(Integer mplsLabel) {
136 checkNotNull(mplsLabel, "MPLS label cannot be null");
137 return new ModMplsLabelInstruction(mplsLabel);
138 }
sangho3f97a17d2015-01-29 22:56:29 -0800139
140 /**
141 * Creates a MPLS TTL modification.
142 *
143 * @return a L2 Modification
144 */
145 public static L2ModificationInstruction decMplsTtl() {
146 return new ModMplsTtlInstruction();
147 }
Pavlin Radoslavovfebe82c2015-02-11 19:08:15 -0800148
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -0800149 /**
Pavlin Radoslavovfebe82c2015-02-11 19:08:15 -0800150 * Creates a L3 IPv4 src modification.
151 *
152 * @param addr the IPv4 address to modify to
alshabib7410fea2014-09-16 13:48:39 -0700153 * @return a L3 modification
154 */
Pavlin Radoslavov855ea2d2014-10-30 15:32:39 -0700155 public static L3ModificationInstruction modL3Src(IpAddress addr) {
Pavlin Radoslavovfebe82c2015-02-11 19:08:15 -0800156 checkNotNull(addr, "Src l3 IPv4 address cannot be null");
157 return new ModIPInstruction(L3SubType.IPV4_SRC, addr);
alshabib7410fea2014-09-16 13:48:39 -0700158 }
159
160 /**
Pavlin Radoslavovfebe82c2015-02-11 19:08:15 -0800161 * Creates a L3 IPv4 dst modification.
162 *
163 * @param addr the IPv4 address to modify to
alshabib7410fea2014-09-16 13:48:39 -0700164 * @return a L3 modification
165 */
Pavlin Radoslavov855ea2d2014-10-30 15:32:39 -0700166 public static L3ModificationInstruction modL3Dst(IpAddress addr) {
Pavlin Radoslavovfebe82c2015-02-11 19:08:15 -0800167 checkNotNull(addr, "Dst l3 IPv4 address cannot be null");
168 return new ModIPInstruction(L3SubType.IPV4_DST, addr);
169 }
170
171 /**
172 * Creates a L3 IPv6 src modification.
173 *
174 * @param addr the IPv6 address to modify to
175 * @return a L3 modification
176 */
177 public static L3ModificationInstruction modL3IPv6Src(IpAddress addr) {
178 checkNotNull(addr, "Src l3 IPv6 address cannot be null");
179 return new ModIPInstruction(L3SubType.IPV6_SRC, addr);
180 }
181
182 /**
183 * Creates a L3 IPv6 dst modification.
184 *
185 * @param addr the IPv6 address to modify to
186 * @return a L3 modification
187 */
188 public static L3ModificationInstruction modL3IPv6Dst(IpAddress addr) {
189 checkNotNull(addr, "Dst l3 IPv6 address cannot be null");
190 return new ModIPInstruction(L3SubType.IPV6_DST, addr);
191 }
192
193 /**
194 * Creates a L3 IPv6 Flow Label modification.
195 *
196 * @param flowLabel the IPv6 flow label to modify to (20 bits)
197 * @return a L3 modification
198 */
199 public static L3ModificationInstruction modL3IPv6FlowLabel(int flowLabel) {
200 return new ModIPv6FlowLabelInstruction(flowLabel);
alshabib7410fea2014-09-16 13:48:39 -0700201 }
202
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -0800203 /**
sangho3f97a17d2015-01-29 22:56:29 -0800204 * Creates a L3 TTL modification.
205 * @return a L3 modification
206 */
207 public static L3ModificationInstruction decNwTtl() {
208 return new ModTtlInstruction(L3SubType.DEC_TTL);
209 }
210
211 /**
212 * Creates a L3 TTL modification.
213 * @return a L3 modification
214 */
215 public static L3ModificationInstruction copyTtlOut() {
216 return new ModTtlInstruction(L3SubType.TTL_OUT);
217 }
218
219 /**
220 * Creates a L3 TTL modification.
221 * @return a L3 modification
222 */
223 public static L3ModificationInstruction copyTtlIn() {
224 return new ModTtlInstruction(L3SubType.TTL_IN);
225 }
226
227 /**
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -0800228 * Creates a mpls header instruction.
229 * @return a L2 modification.
230 */
231 public static Instruction pushMpls() {
232 return new PushHeaderInstructions(L2SubType.MPLS_PUSH,
Yuta HIGUCHI32a53c52015-02-08 01:25:40 -0800233 Ethernet.MPLS_UNICAST);
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -0800234 }
235
236 /**
237 * Creates a mpls header instruction.
238 * @return a L2 modification.
239 */
240 public static Instruction popMpls() {
241 return new PushHeaderInstructions(L2SubType.MPLS_POP,
Yuta HIGUCHI32a53c52015-02-08 01:25:40 -0800242 Ethernet.MPLS_UNICAST);
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -0800243 }
alshabib7410fea2014-09-16 13:48:39 -0700244
sangho3f97a17d2015-01-29 22:56:29 -0800245 /**
246 * Creates a mpls header instruction.
247 *
248 * @param etherType Ethernet type to set
249 * @return a L2 modification.
250 */
251 public static Instruction popMpls(Short etherType) {
252 checkNotNull(etherType, "Ethernet type cannot be null");
Yuta HIGUCHI32a53c52015-02-08 01:25:40 -0800253 return new PushHeaderInstructions(L2SubType.MPLS_POP, etherType);
sangho3f97a17d2015-01-29 22:56:29 -0800254 }
255
alshabib55a55d92014-09-16 11:59:31 -0700256 /*
sangho8995ac52015-02-04 11:29:03 -0800257 * Drop instructions
alshabib55a55d92014-09-16 11:59:31 -0700258 */
259
260 public static final class DropInstruction implements Instruction {
261 @Override
262 public Type type() {
263 return Type.DROP;
264 }
alshabib99b8fdc2014-09-25 14:30:22 -0700265
266 @Override
267 public String toString() {
268 return toStringHelper(type()).toString();
269
270 }
alshabib8ca53902014-10-07 13:11:17 -0700271
272 @Override
273 public int hashCode() {
274 return Objects.hash(type());
275 }
276
277 @Override
278 public boolean equals(Object obj) {
279 if (this == obj) {
280 return true;
281 }
282 if (obj instanceof DropInstruction) {
283 DropInstruction that = (DropInstruction) obj;
284 return Objects.equals(type(), that.type());
285
286 }
287 return false;
288 }
alshabib55a55d92014-09-16 11:59:31 -0700289 }
290
sangho8995ac52015-02-04 11:29:03 -0800291 /*
292 * Output Instruction
293 */
alshabib55a55d92014-09-16 11:59:31 -0700294
295 public static final class OutputInstruction implements Instruction {
296 private final PortNumber port;
297
298 private OutputInstruction(PortNumber port) {
299 this.port = port;
300 }
301
302 public PortNumber port() {
303 return port;
304 }
305
306 @Override
307 public Type type() {
308 return Type.OUTPUT;
309 }
alshabib99b8fdc2014-09-25 14:30:22 -0700310 @Override
311 public String toString() {
312 return toStringHelper(type().toString())
313 .add("port", port).toString();
314 }
alshabib8ca53902014-10-07 13:11:17 -0700315
316 @Override
317 public int hashCode() {
Thomas Vachuska9b2da212014-11-10 19:30:25 -0800318 return Objects.hash(type(), port);
alshabib8ca53902014-10-07 13:11:17 -0700319 }
320
321 @Override
322 public boolean equals(Object obj) {
323 if (this == obj) {
324 return true;
325 }
326 if (obj instanceof OutputInstruction) {
327 OutputInstruction that = (OutputInstruction) obj;
Yuta HIGUCHI4ce65292014-11-01 12:09:55 -0700328 return Objects.equals(port, that.port);
alshabib8ca53902014-10-07 13:11:17 -0700329
330 }
331 return false;
332 }
alshabib55a55d92014-09-16 11:59:31 -0700333 }
334
sangho8995ac52015-02-04 11:29:03 -0800335 /*
336 * Group Instruction
337 */
338
339 public static final class GroupInstruction implements Instruction {
340 private final GroupId groupId;
341
342 private GroupInstruction(GroupId groupId) {
343 this.groupId = groupId;
344 }
345
346 public GroupId groupId() {
347 return groupId;
348 }
349
350 @Override
351 public Type type() {
352 return Type.GROUP;
353 }
354 @Override
355 public String toString() {
356 return toStringHelper(type().toString())
357 .add("group ID", groupId.id()).toString();
358 }
359
360 @Override
361 public int hashCode() {
362 return Objects.hash(type(), groupId);
363 }
364
365 @Override
366 public boolean equals(Object obj) {
367 if (this == obj) {
368 return true;
369 }
370 if (obj instanceof GroupInstruction) {
371 GroupInstruction that = (GroupInstruction) obj;
372 return Objects.equals(groupId, that.groupId);
373
374 }
375 return false;
376 }
377 }
378
alshabib55a55d92014-09-16 11:59:31 -0700379}
alshabib8ca53902014-10-07 13:11:17 -0700380
381