blob: f9894dabbe085f4d45ca1cc8fadd8375770494de [file] [log] [blame]
Thomas Vachuska83e090e2014-10-22 14:25:35 -07001/*
Ray Milkey34c95902015-04-15 09:47:53 -07002 * Copyright 2014-2015 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
Jonathan Hart54b406b2015-03-06 16:24:14 -080018import org.onlab.packet.Ethernet;
19import org.onlab.packet.IpAddress;
20import org.onlab.packet.MacAddress;
21import org.onlab.packet.MplsLabel;
22import org.onlab.packet.VlanId;
sangho8995ac52015-02-04 11:29:03 -080023import org.onosproject.core.GroupId;
Sho SHIMIZUe9e12752015-05-05 14:45:40 -070024import org.onosproject.net.OchSignal;
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;
Brian O'Connorabafb502014-12-02 22:26:20 -080028import org.onosproject.net.flow.instructions.L3ModificationInstruction.L3SubType;
29import org.onosproject.net.flow.instructions.L3ModificationInstruction.ModIPInstruction;
Pavlin Radoslavovfebe82c2015-02-11 19:08:15 -080030import org.onosproject.net.flow.instructions.L3ModificationInstruction.ModIPv6FlowLabelInstruction;
sangho3f97a17d2015-01-29 22:56:29 -080031import org.onosproject.net.flow.instructions.L3ModificationInstruction.ModTtlInstruction;
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -080032
Jonathan Hart54b406b2015-03-06 16:24:14 -080033import java.util.Objects;
34
35import static com.google.common.base.MoreObjects.toStringHelper;
36import static com.google.common.base.Preconditions.checkNotNull;
alshabib64231f62014-09-16 17:58:36 -070037
alshabib55a55d92014-09-16 11:59:31 -070038/**
39 * Factory class for creating various traffic treatment instructions.
40 */
41public final class Instructions {
42
43
44 // Ban construction
45 private Instructions() {}
46
47 /**
48 * Creates an output instruction using the specified port number. This can
49 * include logical ports such as CONTROLLER, FLOOD, etc.
50 *
51 * @param number port number
52 * @return output instruction
53 */
54 public static OutputInstruction createOutput(final PortNumber number) {
55 checkNotNull(number, "PortNumber cannot be null");
56 return new OutputInstruction(number);
57 }
58
59 /**
60 * Creates a drop instruction.
Jonathan Hart54b406b2015-03-06 16:24:14 -080061 *
alshabib55a55d92014-09-16 11:59:31 -070062 * @return drop instruction
63 */
64 public static DropInstruction createDrop() {
65 return new DropInstruction();
66 }
67
68 /**
sangho8995ac52015-02-04 11:29:03 -080069 * Creates a group instruction.
70 *
71 * @param groupId Group Id
72 * @return group instruction
73 */
74 public static GroupInstruction createGroup(final GroupId groupId) {
75 checkNotNull(groupId, "GroupId cannot be null");
76 return new GroupInstruction(groupId);
77 }
78
79 /**
Marc De Leenheer49087752014-10-23 13:54:09 -070080 * Creates a l0 modification.
Jonathan Hart54b406b2015-03-06 16:24:14 -080081 *
82 * @param lambda the lambda to modify to
Marc De Leenheer49087752014-10-23 13:54:09 -070083 * @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 /**
Sho SHIMIZUe9e12752015-05-05 14:45:40 -070091 * Creates an L0 modification with the specified OCh signal.
92 *
93 * @param lambda OCh signal
94 * @return an L0 modification
95 */
96 public static L0ModificationInstruction modL0OchSignal(OchSignal lambda) {
97 checkNotNull(lambda, "L0 OCh signal cannot be null");
98 return new L0ModificationInstruction.ModOchSignalInstruction(lambda);
99 }
100
101 /**
alshabib55a55d92014-09-16 11:59:31 -0700102 * Creates a l2 src modification.
Jonathan Hart54b406b2015-03-06 16:24:14 -0800103 *
104 * @param addr the mac address to modify to
alshabib55a55d92014-09-16 11:59:31 -0700105 * @return a l2 modification
106 */
Ayaka Koshibea9c199f2014-09-16 16:21:40 -0700107 public static L2ModificationInstruction modL2Src(MacAddress addr) {
alshabib55a55d92014-09-16 11:59:31 -0700108 checkNotNull(addr, "Src l2 address cannot be null");
alshabibd17abc22015-04-21 18:26:35 -0700109 return new L2ModificationInstruction.ModEtherInstruction(
110 L2ModificationInstruction.L2SubType.ETH_SRC, addr);
alshabib55a55d92014-09-16 11:59:31 -0700111 }
112
113 /**
114 * Creates a L2 dst modification.
Jonathan Hart54b406b2015-03-06 16:24:14 -0800115 *
116 * @param addr the mac address to modify to
alshabib55a55d92014-09-16 11:59:31 -0700117 * @return a L2 modification
118 */
Ayaka Koshibea9c199f2014-09-16 16:21:40 -0700119 public static L2ModificationInstruction modL2Dst(MacAddress addr) {
alshabib55a55d92014-09-16 11:59:31 -0700120 checkNotNull(addr, "Dst l2 address cannot be null");
alshabibd17abc22015-04-21 18:26:35 -0700121 return new L2ModificationInstruction.ModEtherInstruction(
122 L2ModificationInstruction.L2SubType.ETH_DST, addr);
alshabib55a55d92014-09-16 11:59:31 -0700123 }
124
125 /**
Jonathan Hart54b406b2015-03-06 16:24:14 -0800126 * Creates a VLAN ID modification.
127 *
128 * @param vlanId the VLAN ID to modify to
alshabib7410fea2014-09-16 13:48:39 -0700129 * @return a L2 modification
130 */
Ayaka Koshibea9c199f2014-09-16 16:21:40 -0700131 public static L2ModificationInstruction modVlanId(VlanId vlanId) {
alshabib55a55d92014-09-16 11:59:31 -0700132 checkNotNull(vlanId, "VLAN id cannot be null");
alshabibd17abc22015-04-21 18:26:35 -0700133 return new L2ModificationInstruction.ModVlanIdInstruction(vlanId);
alshabib55a55d92014-09-16 11:59:31 -0700134 }
135
alshabib7410fea2014-09-16 13:48:39 -0700136 /**
Jonathan Hart54b406b2015-03-06 16:24:14 -0800137 * Creates a VLAN PCP modification.
138 *
139 * @param vlanPcp the PCP to modify to
alshabib7410fea2014-09-16 13:48:39 -0700140 * @return a L2 modification
141 */
142 public static L2ModificationInstruction modVlanPcp(Byte vlanPcp) {
143 checkNotNull(vlanPcp, "VLAN Pcp cannot be null");
alshabibd17abc22015-04-21 18:26:35 -0700144 return new L2ModificationInstruction.ModVlanPcpInstruction(vlanPcp);
alshabib7410fea2014-09-16 13:48:39 -0700145 }
146
147 /**
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -0800148 * Creates a MPLS label modification.
Jonathan Hart54b406b2015-03-06 16:24:14 -0800149 *
150 * @param mplsLabel MPLS label to set
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -0800151 * @return a L2 Modification
152 */
Michele Santuari4b6019e2014-12-19 11:31:45 +0100153 public static L2ModificationInstruction modMplsLabel(MplsLabel mplsLabel) {
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -0800154 checkNotNull(mplsLabel, "MPLS label cannot be null");
alshabibd17abc22015-04-21 18:26:35 -0700155 return new L2ModificationInstruction.ModMplsLabelInstruction(mplsLabel);
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -0800156 }
sangho3f97a17d2015-01-29 22:56:29 -0800157
158 /**
Jonathan Hart54b406b2015-03-06 16:24:14 -0800159 * Creates a MPLS decrement TTL modification.
sangho3f97a17d2015-01-29 22:56:29 -0800160 *
161 * @return a L2 Modification
162 */
163 public static L2ModificationInstruction decMplsTtl() {
alshabibd17abc22015-04-21 18:26:35 -0700164 return new L2ModificationInstruction.ModMplsTtlInstruction();
sangho3f97a17d2015-01-29 22:56:29 -0800165 }
Pavlin Radoslavovfebe82c2015-02-11 19:08:15 -0800166
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -0800167 /**
Pavlin Radoslavovfebe82c2015-02-11 19:08:15 -0800168 * Creates a L3 IPv4 src modification.
169 *
170 * @param addr the IPv4 address to modify to
alshabib7410fea2014-09-16 13:48:39 -0700171 * @return a L3 modification
172 */
Pavlin Radoslavov855ea2d2014-10-30 15:32:39 -0700173 public static L3ModificationInstruction modL3Src(IpAddress addr) {
Pavlin Radoslavovfebe82c2015-02-11 19:08:15 -0800174 checkNotNull(addr, "Src l3 IPv4 address cannot be null");
175 return new ModIPInstruction(L3SubType.IPV4_SRC, addr);
alshabib7410fea2014-09-16 13:48:39 -0700176 }
177
178 /**
Pavlin Radoslavovfebe82c2015-02-11 19:08:15 -0800179 * Creates a L3 IPv4 dst modification.
180 *
181 * @param addr the IPv4 address to modify to
alshabib7410fea2014-09-16 13:48:39 -0700182 * @return a L3 modification
183 */
Pavlin Radoslavov855ea2d2014-10-30 15:32:39 -0700184 public static L3ModificationInstruction modL3Dst(IpAddress addr) {
Pavlin Radoslavovfebe82c2015-02-11 19:08:15 -0800185 checkNotNull(addr, "Dst l3 IPv4 address cannot be null");
186 return new ModIPInstruction(L3SubType.IPV4_DST, addr);
187 }
188
189 /**
190 * Creates a L3 IPv6 src modification.
191 *
192 * @param addr the IPv6 address to modify to
193 * @return a L3 modification
194 */
195 public static L3ModificationInstruction modL3IPv6Src(IpAddress addr) {
196 checkNotNull(addr, "Src l3 IPv6 address cannot be null");
197 return new ModIPInstruction(L3SubType.IPV6_SRC, addr);
198 }
199
200 /**
201 * Creates a L3 IPv6 dst modification.
202 *
203 * @param addr the IPv6 address to modify to
204 * @return a L3 modification
205 */
206 public static L3ModificationInstruction modL3IPv6Dst(IpAddress addr) {
207 checkNotNull(addr, "Dst l3 IPv6 address cannot be null");
208 return new ModIPInstruction(L3SubType.IPV6_DST, addr);
209 }
210
211 /**
212 * Creates a L3 IPv6 Flow Label modification.
213 *
214 * @param flowLabel the IPv6 flow label to modify to (20 bits)
215 * @return a L3 modification
216 */
217 public static L3ModificationInstruction modL3IPv6FlowLabel(int flowLabel) {
218 return new ModIPv6FlowLabelInstruction(flowLabel);
alshabib7410fea2014-09-16 13:48:39 -0700219 }
220
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -0800221 /**
Jonathan Hart54b406b2015-03-06 16:24:14 -0800222 * Creates a L3 decrement TTL modification.
223 *
sangho3f97a17d2015-01-29 22:56:29 -0800224 * @return a L3 modification
225 */
226 public static L3ModificationInstruction decNwTtl() {
227 return new ModTtlInstruction(L3SubType.DEC_TTL);
228 }
229
230 /**
Jonathan Hart54b406b2015-03-06 16:24:14 -0800231 * Creates a L3 copy TTL to outer header modification.
232 *
sangho3f97a17d2015-01-29 22:56:29 -0800233 * @return a L3 modification
234 */
235 public static L3ModificationInstruction copyTtlOut() {
236 return new ModTtlInstruction(L3SubType.TTL_OUT);
237 }
238
239 /**
Jonathan Hart54b406b2015-03-06 16:24:14 -0800240 * Creates a L3 copy TTL to inner header modification.
241 *
sangho3f97a17d2015-01-29 22:56:29 -0800242 * @return a L3 modification
243 */
244 public static L3ModificationInstruction copyTtlIn() {
245 return new ModTtlInstruction(L3SubType.TTL_IN);
246 }
247
248 /**
Jonathan Hart54b406b2015-03-06 16:24:14 -0800249 * Creates a push MPLS header instruction.
250 *
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -0800251 * @return a L2 modification.
252 */
253 public static Instruction pushMpls() {
alshabibd17abc22015-04-21 18:26:35 -0700254 return new L2ModificationInstruction.PushHeaderInstructions(
255 L2ModificationInstruction.L2SubType.MPLS_PUSH,
Yuta HIGUCHI32a53c52015-02-08 01:25:40 -0800256 Ethernet.MPLS_UNICAST);
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -0800257 }
258
259 /**
Jonathan Hart54b406b2015-03-06 16:24:14 -0800260 * Creates a pop MPLS header instruction.
261 *
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -0800262 * @return a L2 modification.
263 */
264 public static Instruction popMpls() {
alshabibd17abc22015-04-21 18:26:35 -0700265 return new L2ModificationInstruction.PushHeaderInstructions(
266 L2ModificationInstruction.L2SubType.MPLS_POP,
Yuta HIGUCHI32a53c52015-02-08 01:25:40 -0800267 Ethernet.MPLS_UNICAST);
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -0800268 }
alshabib7410fea2014-09-16 13:48:39 -0700269
sangho3f97a17d2015-01-29 22:56:29 -0800270 /**
Jonathan Hart54b406b2015-03-06 16:24:14 -0800271 * Creates a pop MPLS header instruction with a particular ethertype.
sangho3f97a17d2015-01-29 22:56:29 -0800272 *
273 * @param etherType Ethernet type to set
274 * @return a L2 modification.
275 */
276 public static Instruction popMpls(Short etherType) {
277 checkNotNull(etherType, "Ethernet type cannot be null");
alshabibd17abc22015-04-21 18:26:35 -0700278 return new L2ModificationInstruction.PushHeaderInstructions(
279 L2ModificationInstruction.L2SubType.MPLS_POP, etherType);
sangho3f97a17d2015-01-29 22:56:29 -0800280 }
281
Saurav Dasfbe25c52015-03-04 11:12:00 -0800282 /**
Jonathan Hart54b406b2015-03-06 16:24:14 -0800283 * Creates a pop VLAN header instruction.
284 *
285 * @return a L2 modification
Saurav Dasfbe25c52015-03-04 11:12:00 -0800286 */
287 public static Instruction popVlan() {
alshabibd17abc22015-04-21 18:26:35 -0700288 return new L2ModificationInstruction.PopVlanInstruction(
289 L2ModificationInstruction.L2SubType.VLAN_POP);
Saurav Dasfbe25c52015-03-04 11:12:00 -0800290 }
291
292 /**
Jonathan Hart54b406b2015-03-06 16:24:14 -0800293 * Creates a push VLAN header instruction.
294 *
295 * @return a L2 modification
296 */
297 public static Instruction pushVlan() {
alshabibd17abc22015-04-21 18:26:35 -0700298 return new L2ModificationInstruction.PushHeaderInstructions(
299 L2ModificationInstruction.L2SubType.VLAN_PUSH, Ethernet.TYPE_VLAN);
Jonathan Hart54b406b2015-03-06 16:24:14 -0800300 }
301
302 /**
alshabibd17abc22015-04-21 18:26:35 -0700303 * Sends the packet to the table id.
Jonathan Hart54b406b2015-03-06 16:24:14 -0800304 *
alshabibd17abc22015-04-21 18:26:35 -0700305 * @param tableId flow rule table id
Thomas Vachuska3e2b6512015-03-05 09:25:03 -0800306 * @return table type transition instruction
Saurav Dasfbe25c52015-03-04 11:12:00 -0800307 */
alshabibd17abc22015-04-21 18:26:35 -0700308 public static Instruction transition(Integer tableId) {
309 checkNotNull(tableId, "Table id cannot be null");
310 return new TableTypeTransition(tableId);
alshabib9af70072015-02-09 14:34:16 -0800311 }
312
Yuta HIGUCHI6a479642015-02-08 01:28:50 -0800313 /**
314 * Drop instruction.
alshabib55a55d92014-09-16 11:59:31 -0700315 */
alshabib55a55d92014-09-16 11:59:31 -0700316 public static final class DropInstruction implements Instruction {
Yuta HIGUCHI6a479642015-02-08 01:28:50 -0800317
318 private DropInstruction() {}
319
alshabib55a55d92014-09-16 11:59:31 -0700320 @Override
321 public Type type() {
322 return Type.DROP;
323 }
alshabib99b8fdc2014-09-25 14:30:22 -0700324
325 @Override
326 public String toString() {
alshabib346b5b32015-03-06 00:42:16 -0800327 return toStringHelper(type().toString()).toString();
alshabib99b8fdc2014-09-25 14:30:22 -0700328 }
alshabib8ca53902014-10-07 13:11:17 -0700329
330 @Override
331 public int hashCode() {
332 return Objects.hash(type());
333 }
334
335 @Override
336 public boolean equals(Object obj) {
337 if (this == obj) {
338 return true;
339 }
340 if (obj instanceof DropInstruction) {
Yuta HIGUCHI6a479642015-02-08 01:28:50 -0800341 return true;
alshabib8ca53902014-10-07 13:11:17 -0700342 }
343 return false;
344 }
alshabib55a55d92014-09-16 11:59:31 -0700345 }
346
Yuta HIGUCHI6a479642015-02-08 01:28:50 -0800347 /**
348 * Output Instruction.
sangho8995ac52015-02-04 11:29:03 -0800349 */
alshabib55a55d92014-09-16 11:59:31 -0700350 public static final class OutputInstruction implements Instruction {
351 private final PortNumber port;
352
353 private OutputInstruction(PortNumber port) {
354 this.port = port;
355 }
356
357 public PortNumber port() {
358 return port;
359 }
360
361 @Override
362 public Type type() {
363 return Type.OUTPUT;
364 }
alshabib99b8fdc2014-09-25 14:30:22 -0700365 @Override
366 public String toString() {
367 return toStringHelper(type().toString())
368 .add("port", port).toString();
369 }
alshabib8ca53902014-10-07 13:11:17 -0700370
371 @Override
372 public int hashCode() {
Thomas Vachuska9b2da212014-11-10 19:30:25 -0800373 return Objects.hash(type(), port);
alshabib8ca53902014-10-07 13:11:17 -0700374 }
375
376 @Override
377 public boolean equals(Object obj) {
378 if (this == obj) {
379 return true;
380 }
381 if (obj instanceof OutputInstruction) {
382 OutputInstruction that = (OutputInstruction) obj;
Yuta HIGUCHI4ce65292014-11-01 12:09:55 -0700383 return Objects.equals(port, that.port);
alshabib8ca53902014-10-07 13:11:17 -0700384
385 }
386 return false;
387 }
alshabib55a55d92014-09-16 11:59:31 -0700388 }
389
Yuta HIGUCHI6a479642015-02-08 01:28:50 -0800390 /**
391 * Group Instruction.
sangho8995ac52015-02-04 11:29:03 -0800392 */
sangho8995ac52015-02-04 11:29:03 -0800393 public static final class GroupInstruction implements Instruction {
394 private final GroupId groupId;
395
396 private GroupInstruction(GroupId groupId) {
397 this.groupId = groupId;
398 }
399
400 public GroupId groupId() {
401 return groupId;
402 }
403
404 @Override
405 public Type type() {
406 return Type.GROUP;
407 }
alshabib9af70072015-02-09 14:34:16 -0800408
sangho8995ac52015-02-04 11:29:03 -0800409 @Override
410 public String toString() {
411 return toStringHelper(type().toString())
412 .add("group ID", groupId.id()).toString();
413 }
414
415 @Override
416 public int hashCode() {
417 return Objects.hash(type(), groupId);
418 }
419
420 @Override
421 public boolean equals(Object obj) {
422 if (this == obj) {
423 return true;
424 }
425 if (obj instanceof GroupInstruction) {
426 GroupInstruction that = (GroupInstruction) obj;
427 return Objects.equals(groupId, that.groupId);
428
429 }
430 return false;
431 }
432 }
433
alshabib9af70072015-02-09 14:34:16 -0800434
alshabibd17abc22015-04-21 18:26:35 -0700435 public static class TableTypeTransition implements Instruction {
436 private final Integer tableId;
437
438 TableTypeTransition(Integer tableId) {
439 this.tableId = tableId;
alshabib9af70072015-02-09 14:34:16 -0800440 }
441
442 @Override
443 public Type type() {
444 return Type.TABLE;
445 }
446
alshabibd17abc22015-04-21 18:26:35 -0700447 public Integer tableId() {
448 return this.tableId;
alshabib9af70072015-02-09 14:34:16 -0800449 }
450
451 @Override
452 public String toString() {
453 return toStringHelper(type().toString())
alshabibd17abc22015-04-21 18:26:35 -0700454 .add("tableId", this.tableId).toString();
alshabib9af70072015-02-09 14:34:16 -0800455 }
456
457 @Override
458 public int hashCode() {
alshabibd17abc22015-04-21 18:26:35 -0700459 return Objects.hash(type(), tableId);
alshabib9af70072015-02-09 14:34:16 -0800460 }
461
462 @Override
463 public boolean equals(Object obj) {
464 if (this == obj) {
465 return true;
466 }
467 if (obj instanceof TableTypeTransition) {
468 TableTypeTransition that = (TableTypeTransition) obj;
alshabibd17abc22015-04-21 18:26:35 -0700469 return Objects.equals(tableId, that.tableId);
alshabib9af70072015-02-09 14:34:16 -0800470
471 }
472 return false;
473 }
474
475 }
alshabib55a55d92014-09-16 11:59:31 -0700476}
alshabib8ca53902014-10-07 13:11:17 -0700477
478