blob: c5358a296d9174a91dd44850973b92d26a633c12 [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
alshabib7b808c52015-06-26 14:22:24 -070018import org.onlab.packet.EthType;
Jonathan Hart54b406b2015-03-06 16:24:14 -080019import org.onlab.packet.IpAddress;
20import org.onlab.packet.MacAddress;
21import org.onlab.packet.MplsLabel;
Hyunsun Mooncf732fb2015-08-22 21:04:23 -070022import org.onlab.packet.TpPort;
Jonathan Hart54b406b2015-03-06 16:24:14 -080023import org.onlab.packet.VlanId;
sangho8995ac52015-02-04 11:29:03 -080024import org.onosproject.core.GroupId;
Sho SHIMIZU2e7ac842015-05-05 15:45:38 -070025import org.onosproject.net.IndexedLambda;
26import org.onosproject.net.Lambda;
Sho SHIMIZUe9e12752015-05-05 14:45:40 -070027import org.onosproject.net.OchSignal;
Brian O'Connorabafb502014-12-02 22:26:20 -080028import org.onosproject.net.PortNumber;
29import org.onosproject.net.flow.instructions.L0ModificationInstruction.L0SubType;
30import org.onosproject.net.flow.instructions.L0ModificationInstruction.ModLambdaInstruction;
Sho SHIMIZU2e7ac842015-05-05 15:45:38 -070031import org.onosproject.net.flow.instructions.L0ModificationInstruction.ModOchSignalInstruction;
Brian O'Connorabafb502014-12-02 22:26:20 -080032import org.onosproject.net.flow.instructions.L3ModificationInstruction.L3SubType;
33import org.onosproject.net.flow.instructions.L3ModificationInstruction.ModIPInstruction;
Pavlin Radoslavovfebe82c2015-02-11 19:08:15 -080034import org.onosproject.net.flow.instructions.L3ModificationInstruction.ModIPv6FlowLabelInstruction;
sangho3f97a17d2015-01-29 22:56:29 -080035import org.onosproject.net.flow.instructions.L3ModificationInstruction.ModTtlInstruction;
Hyunsun Moonc8bd97c2015-07-18 22:47:33 -070036import org.onosproject.net.flow.instructions.L4ModificationInstruction.L4SubType;
37import org.onosproject.net.flow.instructions.L4ModificationInstruction.ModTransportPortInstruction;
alshabib10c810b2015-08-18 16:59:04 -070038import org.onosproject.net.meter.MeterId;
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -080039
Jonathan Hart54b406b2015-03-06 16:24:14 -080040import java.util.Objects;
41
42import static com.google.common.base.MoreObjects.toStringHelper;
43import static com.google.common.base.Preconditions.checkNotNull;
alshabib64231f62014-09-16 17:58:36 -070044
alshabib55a55d92014-09-16 11:59:31 -070045/**
46 * Factory class for creating various traffic treatment instructions.
47 */
48public final class Instructions {
49
50
51 // Ban construction
52 private Instructions() {}
53
54 /**
55 * Creates an output instruction using the specified port number. This can
56 * include logical ports such as CONTROLLER, FLOOD, etc.
57 *
58 * @param number port number
59 * @return output instruction
60 */
61 public static OutputInstruction createOutput(final PortNumber number) {
62 checkNotNull(number, "PortNumber cannot be null");
63 return new OutputInstruction(number);
64 }
65
66 /**
67 * Creates a drop instruction.
Jonathan Hart54b406b2015-03-06 16:24:14 -080068 *
alshabib55a55d92014-09-16 11:59:31 -070069 * @return drop instruction
70 */
71 public static DropInstruction createDrop() {
72 return new DropInstruction();
73 }
74
75 /**
sangho8995ac52015-02-04 11:29:03 -080076 * Creates a group instruction.
77 *
78 * @param groupId Group Id
79 * @return group instruction
80 */
81 public static GroupInstruction createGroup(final GroupId groupId) {
82 checkNotNull(groupId, "GroupId cannot be null");
83 return new GroupInstruction(groupId);
84 }
85
alshabib10c810b2015-08-18 16:59:04 -070086 public static MeterInstruction meterTraffic(final MeterId meterId) {
87 checkNotNull(meterId, "meter id cannot be null");
88 return new MeterInstruction(meterId);
89 }
90
sangho8995ac52015-02-04 11:29:03 -080091 /**
Marc De Leenheer49087752014-10-23 13:54:09 -070092 * Creates a l0 modification.
Jonathan Hart54b406b2015-03-06 16:24:14 -080093 *
94 * @param lambda the lambda to modify to
Marc De Leenheer49087752014-10-23 13:54:09 -070095 * @return a l0 modification
Sho SHIMIZU2e7ac842015-05-05 15:45:38 -070096 * @deprecated in Cardinal Release. Use {@link #modL0Lambda(Lambda)} instead.
Marc De Leenheer49087752014-10-23 13:54:09 -070097 */
Sho SHIMIZU2e7ac842015-05-05 15:45:38 -070098 @Deprecated
Marc De Leenheer49087752014-10-23 13:54:09 -070099 public static L0ModificationInstruction modL0Lambda(short lambda) {
100 checkNotNull(lambda, "L0 lambda cannot be null");
101 return new ModLambdaInstruction(L0SubType.LAMBDA, lambda);
102 }
103
104 /**
Sho SHIMIZUe9e12752015-05-05 14:45:40 -0700105 * Creates an L0 modification with the specified OCh signal.
106 *
107 * @param lambda OCh signal
108 * @return an L0 modification
109 */
Sho SHIMIZU2e7ac842015-05-05 15:45:38 -0700110 public static L0ModificationInstruction modL0Lambda(Lambda lambda) {
Sho SHIMIZUe9e12752015-05-05 14:45:40 -0700111 checkNotNull(lambda, "L0 OCh signal cannot be null");
Sho SHIMIZU2e7ac842015-05-05 15:45:38 -0700112
113 if (lambda instanceof IndexedLambda) {
114 return new ModLambdaInstruction(L0SubType.LAMBDA, (short) ((IndexedLambda) lambda).index());
115 } else if (lambda instanceof OchSignal) {
116 return new ModOchSignalInstruction((OchSignal) lambda);
117 } else {
118 throw new UnsupportedOperationException(String.format("Unsupported type: %s", lambda));
119 }
Sho SHIMIZUe9e12752015-05-05 14:45:40 -0700120 }
121
122 /**
alshabib55a55d92014-09-16 11:59:31 -0700123 * Creates a l2 src modification.
Jonathan Hart54b406b2015-03-06 16:24:14 -0800124 *
125 * @param addr the mac address to modify to
alshabib55a55d92014-09-16 11:59:31 -0700126 * @return a l2 modification
127 */
Ayaka Koshibea9c199f2014-09-16 16:21:40 -0700128 public static L2ModificationInstruction modL2Src(MacAddress addr) {
alshabib55a55d92014-09-16 11:59:31 -0700129 checkNotNull(addr, "Src l2 address cannot be null");
alshabibd17abc22015-04-21 18:26:35 -0700130 return new L2ModificationInstruction.ModEtherInstruction(
131 L2ModificationInstruction.L2SubType.ETH_SRC, addr);
alshabib55a55d92014-09-16 11:59:31 -0700132 }
133
134 /**
135 * Creates a L2 dst modification.
Jonathan Hart54b406b2015-03-06 16:24:14 -0800136 *
137 * @param addr the mac address to modify to
alshabib55a55d92014-09-16 11:59:31 -0700138 * @return a L2 modification
139 */
Ayaka Koshibea9c199f2014-09-16 16:21:40 -0700140 public static L2ModificationInstruction modL2Dst(MacAddress addr) {
alshabib55a55d92014-09-16 11:59:31 -0700141 checkNotNull(addr, "Dst l2 address cannot be null");
alshabibd17abc22015-04-21 18:26:35 -0700142 return new L2ModificationInstruction.ModEtherInstruction(
143 L2ModificationInstruction.L2SubType.ETH_DST, addr);
alshabib55a55d92014-09-16 11:59:31 -0700144 }
145
146 /**
Jonathan Hart54b406b2015-03-06 16:24:14 -0800147 * Creates a VLAN ID modification.
148 *
149 * @param vlanId the VLAN ID to modify to
alshabib7410fea2014-09-16 13:48:39 -0700150 * @return a L2 modification
151 */
Ayaka Koshibea9c199f2014-09-16 16:21:40 -0700152 public static L2ModificationInstruction modVlanId(VlanId vlanId) {
alshabib55a55d92014-09-16 11:59:31 -0700153 checkNotNull(vlanId, "VLAN id cannot be null");
alshabibd17abc22015-04-21 18:26:35 -0700154 return new L2ModificationInstruction.ModVlanIdInstruction(vlanId);
alshabib55a55d92014-09-16 11:59:31 -0700155 }
156
alshabib7410fea2014-09-16 13:48:39 -0700157 /**
Jonathan Hart54b406b2015-03-06 16:24:14 -0800158 * Creates a VLAN PCP modification.
159 *
160 * @param vlanPcp the PCP to modify to
alshabib7410fea2014-09-16 13:48:39 -0700161 * @return a L2 modification
162 */
163 public static L2ModificationInstruction modVlanPcp(Byte vlanPcp) {
164 checkNotNull(vlanPcp, "VLAN Pcp cannot be null");
alshabibd17abc22015-04-21 18:26:35 -0700165 return new L2ModificationInstruction.ModVlanPcpInstruction(vlanPcp);
alshabib7410fea2014-09-16 13:48:39 -0700166 }
167
168 /**
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -0800169 * Creates a MPLS label modification.
Jonathan Hart54b406b2015-03-06 16:24:14 -0800170 *
171 * @param mplsLabel MPLS label to set
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -0800172 * @return a L2 Modification
173 */
Michele Santuari4b6019e2014-12-19 11:31:45 +0100174 public static L2ModificationInstruction modMplsLabel(MplsLabel mplsLabel) {
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -0800175 checkNotNull(mplsLabel, "MPLS label cannot be null");
alshabibd17abc22015-04-21 18:26:35 -0700176 return new L2ModificationInstruction.ModMplsLabelInstruction(mplsLabel);
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -0800177 }
sangho3f97a17d2015-01-29 22:56:29 -0800178
179 /**
Saurav Das73a7dd42015-08-19 22:20:31 -0700180 * Creates a MPLS BOS bit modification.
181 *
182 * @param mplsBos MPLS BOS bit to set (true) or unset (false)
183 * @return a L2 Modification
184 */
185 public static L2ModificationInstruction modMplsBos(boolean mplsBos) {
186 return new L2ModificationInstruction.ModMplsBosInstruction(mplsBos);
187 }
188
189 /**
Jonathan Hart54b406b2015-03-06 16:24:14 -0800190 * Creates a MPLS decrement TTL modification.
sangho3f97a17d2015-01-29 22:56:29 -0800191 *
192 * @return a L2 Modification
193 */
194 public static L2ModificationInstruction decMplsTtl() {
alshabibd17abc22015-04-21 18:26:35 -0700195 return new L2ModificationInstruction.ModMplsTtlInstruction();
sangho3f97a17d2015-01-29 22:56:29 -0800196 }
Pavlin Radoslavovfebe82c2015-02-11 19:08:15 -0800197
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -0800198 /**
Pavlin Radoslavovfebe82c2015-02-11 19:08:15 -0800199 * Creates a L3 IPv4 src modification.
200 *
201 * @param addr the IPv4 address to modify to
alshabib7410fea2014-09-16 13:48:39 -0700202 * @return a L3 modification
203 */
Pavlin Radoslavov855ea2d2014-10-30 15:32:39 -0700204 public static L3ModificationInstruction modL3Src(IpAddress addr) {
Pavlin Radoslavovfebe82c2015-02-11 19:08:15 -0800205 checkNotNull(addr, "Src l3 IPv4 address cannot be null");
206 return new ModIPInstruction(L3SubType.IPV4_SRC, addr);
alshabib7410fea2014-09-16 13:48:39 -0700207 }
208
209 /**
Pavlin Radoslavovfebe82c2015-02-11 19:08:15 -0800210 * Creates a L3 IPv4 dst modification.
211 *
212 * @param addr the IPv4 address to modify to
alshabib7410fea2014-09-16 13:48:39 -0700213 * @return a L3 modification
214 */
Pavlin Radoslavov855ea2d2014-10-30 15:32:39 -0700215 public static L3ModificationInstruction modL3Dst(IpAddress addr) {
Pavlin Radoslavovfebe82c2015-02-11 19:08:15 -0800216 checkNotNull(addr, "Dst l3 IPv4 address cannot be null");
217 return new ModIPInstruction(L3SubType.IPV4_DST, addr);
218 }
219
220 /**
221 * Creates a L3 IPv6 src modification.
222 *
223 * @param addr the IPv6 address to modify to
224 * @return a L3 modification
225 */
226 public static L3ModificationInstruction modL3IPv6Src(IpAddress addr) {
227 checkNotNull(addr, "Src l3 IPv6 address cannot be null");
228 return new ModIPInstruction(L3SubType.IPV6_SRC, addr);
229 }
230
231 /**
232 * Creates a L3 IPv6 dst modification.
233 *
234 * @param addr the IPv6 address to modify to
235 * @return a L3 modification
236 */
237 public static L3ModificationInstruction modL3IPv6Dst(IpAddress addr) {
238 checkNotNull(addr, "Dst l3 IPv6 address cannot be null");
239 return new ModIPInstruction(L3SubType.IPV6_DST, addr);
240 }
241
242 /**
243 * Creates a L3 IPv6 Flow Label modification.
244 *
245 * @param flowLabel the IPv6 flow label to modify to (20 bits)
246 * @return a L3 modification
247 */
248 public static L3ModificationInstruction modL3IPv6FlowLabel(int flowLabel) {
249 return new ModIPv6FlowLabelInstruction(flowLabel);
alshabib7410fea2014-09-16 13:48:39 -0700250 }
251
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -0800252 /**
Jonathan Hart54b406b2015-03-06 16:24:14 -0800253 * Creates a L3 decrement TTL modification.
254 *
sangho3f97a17d2015-01-29 22:56:29 -0800255 * @return a L3 modification
256 */
257 public static L3ModificationInstruction decNwTtl() {
258 return new ModTtlInstruction(L3SubType.DEC_TTL);
259 }
260
261 /**
Jonathan Hart54b406b2015-03-06 16:24:14 -0800262 * Creates a L3 copy TTL to outer header modification.
263 *
sangho3f97a17d2015-01-29 22:56:29 -0800264 * @return a L3 modification
265 */
266 public static L3ModificationInstruction copyTtlOut() {
267 return new ModTtlInstruction(L3SubType.TTL_OUT);
268 }
269
270 /**
Jonathan Hart54b406b2015-03-06 16:24:14 -0800271 * Creates a L3 copy TTL to inner header modification.
272 *
sangho3f97a17d2015-01-29 22:56:29 -0800273 * @return a L3 modification
274 */
275 public static L3ModificationInstruction copyTtlIn() {
276 return new ModTtlInstruction(L3SubType.TTL_IN);
277 }
278
279 /**
Jonathan Hart54b406b2015-03-06 16:24:14 -0800280 * Creates a push MPLS header instruction.
281 *
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -0800282 * @return a L2 modification.
283 */
284 public static Instruction pushMpls() {
alshabibd17abc22015-04-21 18:26:35 -0700285 return new L2ModificationInstruction.PushHeaderInstructions(
286 L2ModificationInstruction.L2SubType.MPLS_PUSH,
alshabib7b808c52015-06-26 14:22:24 -0700287 EthType.EtherType.MPLS_UNICAST.ethType());
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -0800288 }
289
290 /**
Jonathan Hart54b406b2015-03-06 16:24:14 -0800291 * Creates a pop MPLS header instruction.
292 *
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -0800293 * @return a L2 modification.
294 */
295 public static Instruction popMpls() {
alshabibd17abc22015-04-21 18:26:35 -0700296 return new L2ModificationInstruction.PushHeaderInstructions(
297 L2ModificationInstruction.L2SubType.MPLS_POP,
alshabib7b808c52015-06-26 14:22:24 -0700298 EthType.EtherType.MPLS_UNICAST.ethType());
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -0800299 }
alshabib7410fea2014-09-16 13:48:39 -0700300
sangho3f97a17d2015-01-29 22:56:29 -0800301 /**
Jonathan Hart54b406b2015-03-06 16:24:14 -0800302 * Creates a pop MPLS header instruction with a particular ethertype.
sangho3f97a17d2015-01-29 22:56:29 -0800303 *
304 * @param etherType Ethernet type to set
305 * @return a L2 modification.
Sho SHIMIZUbe63b232015-06-30 10:57:58 -0700306 * @deprecated in Cardinal Release
sangho3f97a17d2015-01-29 22:56:29 -0800307 */
alshabib7b808c52015-06-26 14:22:24 -0700308 @Deprecated
alshabib0ad43982015-05-07 13:43:13 -0700309 public static Instruction popMpls(int etherType) {
sangho3f97a17d2015-01-29 22:56:29 -0800310 checkNotNull(etherType, "Ethernet type cannot be null");
alshabibd17abc22015-04-21 18:26:35 -0700311 return new L2ModificationInstruction.PushHeaderInstructions(
alshabib7b808c52015-06-26 14:22:24 -0700312 L2ModificationInstruction.L2SubType.MPLS_POP, new EthType(etherType));
313 }
314
315
316 /**
317 * Creates a pop MPLS header instruction with a particular ethertype.
318 *
319 * @param etherType Ethernet type to set
320 * @return a L2 modification.
321 */
322 public static Instruction popMpls(EthType etherType) {
323 checkNotNull(etherType, "Ethernet type cannot be null");
324 return new L2ModificationInstruction.PushHeaderInstructions(
alshabibd17abc22015-04-21 18:26:35 -0700325 L2ModificationInstruction.L2SubType.MPLS_POP, etherType);
sangho3f97a17d2015-01-29 22:56:29 -0800326 }
327
Saurav Dasfbe25c52015-03-04 11:12:00 -0800328 /**
Jonathan Hart54b406b2015-03-06 16:24:14 -0800329 * Creates a pop VLAN header instruction.
330 *
331 * @return a L2 modification
Saurav Dasfbe25c52015-03-04 11:12:00 -0800332 */
333 public static Instruction popVlan() {
alshabibd17abc22015-04-21 18:26:35 -0700334 return new L2ModificationInstruction.PopVlanInstruction(
335 L2ModificationInstruction.L2SubType.VLAN_POP);
Saurav Dasfbe25c52015-03-04 11:12:00 -0800336 }
337
338 /**
Jonathan Hart54b406b2015-03-06 16:24:14 -0800339 * Creates a push VLAN header instruction.
340 *
341 * @return a L2 modification
342 */
343 public static Instruction pushVlan() {
alshabibd17abc22015-04-21 18:26:35 -0700344 return new L2ModificationInstruction.PushHeaderInstructions(
alshabib7b808c52015-06-26 14:22:24 -0700345 L2ModificationInstruction.L2SubType.VLAN_PUSH,
346 EthType.EtherType.VLAN.ethType());
Jonathan Hart54b406b2015-03-06 16:24:14 -0800347 }
348
349 /**
alshabibd17abc22015-04-21 18:26:35 -0700350 * Sends the packet to the table id.
Jonathan Hart54b406b2015-03-06 16:24:14 -0800351 *
alshabibd17abc22015-04-21 18:26:35 -0700352 * @param tableId flow rule table id
Thomas Vachuska3e2b6512015-03-05 09:25:03 -0800353 * @return table type transition instruction
Saurav Dasfbe25c52015-03-04 11:12:00 -0800354 */
alshabibd17abc22015-04-21 18:26:35 -0700355 public static Instruction transition(Integer tableId) {
356 checkNotNull(tableId, "Table id cannot be null");
357 return new TableTypeTransition(tableId);
alshabib9af70072015-02-09 14:34:16 -0800358 }
359
Yuta HIGUCHI6a479642015-02-08 01:28:50 -0800360 /**
Saurav Das86af8f12015-05-25 23:55:33 -0700361 * Writes metadata to associate with a packet.
362 *
363 * @param metadata the metadata value to write
364 * @param metadataMask the bits to mask for the metadata value
365 * @return metadata instruction
366 */
367 public static Instruction writeMetadata(long metadata, long metadataMask) {
368 return new MetadataInstruction(metadata, metadataMask);
369 }
370
371 /**
Hyunsun Moona08c5d02015-07-14 17:53:00 -0700372 * Creates a Tunnel ID modification.
373 *
374 * @param tunnelId the Tunnel ID to modify to
375 * @return a L2 modification
376 */
377 public static L2ModificationInstruction modTunnelId(long tunnelId) {
378 checkNotNull(tunnelId, "Tunnel id cannot be null");
379 return new L2ModificationInstruction.ModTunnelIdInstruction(tunnelId);
380 }
381
382 /**
Hyunsun Moonc8bd97c2015-07-18 22:47:33 -0700383 * Creates a TCP src modification.
384 *
385 * @param port the TCP port number to modify to
386 * @return a L4 modification
Hyunsun Mooncf732fb2015-08-22 21:04:23 -0700387 * @deprecated in Drake release
Hyunsun Moonc8bd97c2015-07-18 22:47:33 -0700388 */
Hyunsun Mooncf732fb2015-08-22 21:04:23 -0700389 @Deprecated
Hyunsun Moonc8bd97c2015-07-18 22:47:33 -0700390 public static L4ModificationInstruction modTcpSrc(short port) {
391 checkNotNull(port, "Src TCP port cannot be null");
Hyunsun Mooncf732fb2015-08-22 21:04:23 -0700392 return new ModTransportPortInstruction(L4SubType.TCP_SRC, TpPort.tpPort(port));
393 }
394
395 /**
396 * Creates a TCP src modification.
397 *
398 * @param port the TCP port number to modify to
399 * @return a L4 modification
400 */
401 public static L4ModificationInstruction modTcpSrc(TpPort port) {
402 checkNotNull(port, "Src TCP port cannot be null");
Hyunsun Moonc8bd97c2015-07-18 22:47:33 -0700403 return new ModTransportPortInstruction(L4SubType.TCP_SRC, port);
404 }
405
406 /**
407 * Creates a TCP dst modification.
408 *
409 * @param port the TCP port number to modify to
410 * @return a L4 modification
Hyunsun Mooncf732fb2015-08-22 21:04:23 -0700411 * @deprecated in Drake release
Hyunsun Moonc8bd97c2015-07-18 22:47:33 -0700412 */
Hyunsun Mooncf732fb2015-08-22 21:04:23 -0700413 @Deprecated
Hyunsun Moonc8bd97c2015-07-18 22:47:33 -0700414 public static L4ModificationInstruction modTcpDst(short port) {
415 checkNotNull(port, "Dst TCP port cannot be null");
Hyunsun Mooncf732fb2015-08-22 21:04:23 -0700416 return new ModTransportPortInstruction(L4SubType.TCP_DST, TpPort.tpPort(port));
417 }
418
419 /**
420 * Creates a TCP dst modification.
421 *
422 * @param port the TCP port number to modify to
423 * @return a L4 modification
424 */
425 public static L4ModificationInstruction modTcpDst(TpPort port) {
426 checkNotNull(port, "Dst TCP port cannot be null");
Hyunsun Moonc8bd97c2015-07-18 22:47:33 -0700427 return new ModTransportPortInstruction(L4SubType.TCP_DST, port);
428 }
429
430 /**
431 * Creates a UDP src modification.
432 *
433 * @param port the UDP port number to modify to
434 * @return a L4 modification
Hyunsun Mooncf732fb2015-08-22 21:04:23 -0700435 * @deprecated in Drake release
Hyunsun Moonc8bd97c2015-07-18 22:47:33 -0700436 */
Hyunsun Mooncf732fb2015-08-22 21:04:23 -0700437 @Deprecated
Hyunsun Moonc8bd97c2015-07-18 22:47:33 -0700438 public static L4ModificationInstruction modUdpSrc(short port) {
439 checkNotNull(port, "Src UDP port cannot be null");
Hyunsun Mooncf732fb2015-08-22 21:04:23 -0700440 return new ModTransportPortInstruction(L4SubType.UDP_SRC, TpPort.tpPort(port));
441 }
442
443 /**
444 * Creates a UDP src modification.
445 *
446 * @param port the UDP port number to modify to
447 * @return a L4 modification
448 */
449 public static L4ModificationInstruction modUdpSrc(TpPort port) {
450 checkNotNull(port, "Src UDP port cannot be null");
Hyunsun Moonc8bd97c2015-07-18 22:47:33 -0700451 return new ModTransportPortInstruction(L4SubType.UDP_SRC, port);
452 }
453
454 /**
455 * Creates a UDP dst modification.
456 *
457 * @param port the UDP port number to modify to
458 * @return a L4 modification
Hyunsun Mooncf732fb2015-08-22 21:04:23 -0700459 * @deprecated in Drake release
Hyunsun Moonc8bd97c2015-07-18 22:47:33 -0700460 */
Hyunsun Mooncf732fb2015-08-22 21:04:23 -0700461 @Deprecated
Hyunsun Moonc8bd97c2015-07-18 22:47:33 -0700462 public static L4ModificationInstruction modUdpDst(short port) {
463 checkNotNull(port, "Dst UDP port cannot be null");
Hyunsun Mooncf732fb2015-08-22 21:04:23 -0700464 return new ModTransportPortInstruction(L4SubType.UDP_DST, TpPort.tpPort(port));
465 }
466
467 /**
468 * Creates a UDP dst modification.
469 *
470 * @param port the UDP port number to modify to
471 * @return a L4 modification
472 */
473 public static L4ModificationInstruction modUdpDst(TpPort port) {
474 checkNotNull(port, "Dst UDP port cannot be null");
Hyunsun Moonc8bd97c2015-07-18 22:47:33 -0700475 return new ModTransportPortInstruction(L4SubType.UDP_DST, port);
476 }
477
478 /**
Yuta HIGUCHI6a479642015-02-08 01:28:50 -0800479 * Drop instruction.
alshabib55a55d92014-09-16 11:59:31 -0700480 */
alshabib55a55d92014-09-16 11:59:31 -0700481 public static final class DropInstruction implements Instruction {
Yuta HIGUCHI6a479642015-02-08 01:28:50 -0800482
483 private DropInstruction() {}
484
alshabib55a55d92014-09-16 11:59:31 -0700485 @Override
486 public Type type() {
487 return Type.DROP;
488 }
alshabib99b8fdc2014-09-25 14:30:22 -0700489
490 @Override
491 public String toString() {
alshabib346b5b32015-03-06 00:42:16 -0800492 return toStringHelper(type().toString()).toString();
alshabib99b8fdc2014-09-25 14:30:22 -0700493 }
alshabib8ca53902014-10-07 13:11:17 -0700494
495 @Override
496 public int hashCode() {
Thomas Vachuska6c8eff32015-05-27 16:11:44 -0700497 return Objects.hash(type().ordinal());
alshabib8ca53902014-10-07 13:11:17 -0700498 }
499
500 @Override
501 public boolean equals(Object obj) {
502 if (this == obj) {
503 return true;
504 }
505 if (obj instanceof DropInstruction) {
Yuta HIGUCHI6a479642015-02-08 01:28:50 -0800506 return true;
alshabib8ca53902014-10-07 13:11:17 -0700507 }
508 return false;
509 }
alshabib55a55d92014-09-16 11:59:31 -0700510 }
511
Yuta HIGUCHI6a479642015-02-08 01:28:50 -0800512 /**
513 * Output Instruction.
sangho8995ac52015-02-04 11:29:03 -0800514 */
alshabib55a55d92014-09-16 11:59:31 -0700515 public static final class OutputInstruction implements Instruction {
516 private final PortNumber port;
517
518 private OutputInstruction(PortNumber port) {
519 this.port = port;
520 }
521
522 public PortNumber port() {
523 return port;
524 }
525
526 @Override
527 public Type type() {
528 return Type.OUTPUT;
529 }
alshabib99b8fdc2014-09-25 14:30:22 -0700530 @Override
531 public String toString() {
532 return toStringHelper(type().toString())
533 .add("port", port).toString();
534 }
alshabib8ca53902014-10-07 13:11:17 -0700535
536 @Override
537 public int hashCode() {
Thomas Vachuska6c8eff32015-05-27 16:11:44 -0700538 return Objects.hash(type().ordinal(), port);
alshabib8ca53902014-10-07 13:11:17 -0700539 }
540
541 @Override
542 public boolean equals(Object obj) {
543 if (this == obj) {
544 return true;
545 }
546 if (obj instanceof OutputInstruction) {
547 OutputInstruction that = (OutputInstruction) obj;
Yuta HIGUCHI4ce65292014-11-01 12:09:55 -0700548 return Objects.equals(port, that.port);
alshabib8ca53902014-10-07 13:11:17 -0700549
550 }
551 return false;
552 }
alshabib55a55d92014-09-16 11:59:31 -0700553 }
554
Yuta HIGUCHI6a479642015-02-08 01:28:50 -0800555 /**
556 * Group Instruction.
sangho8995ac52015-02-04 11:29:03 -0800557 */
sangho8995ac52015-02-04 11:29:03 -0800558 public static final class GroupInstruction implements Instruction {
559 private final GroupId groupId;
560
561 private GroupInstruction(GroupId groupId) {
562 this.groupId = groupId;
563 }
564
565 public GroupId groupId() {
566 return groupId;
567 }
568
569 @Override
570 public Type type() {
571 return Type.GROUP;
572 }
alshabib9af70072015-02-09 14:34:16 -0800573
sangho8995ac52015-02-04 11:29:03 -0800574 @Override
575 public String toString() {
576 return toStringHelper(type().toString())
577 .add("group ID", groupId.id()).toString();
578 }
579
580 @Override
581 public int hashCode() {
Thomas Vachuska6c8eff32015-05-27 16:11:44 -0700582 return Objects.hash(type().ordinal(), groupId);
sangho8995ac52015-02-04 11:29:03 -0800583 }
584
585 @Override
586 public boolean equals(Object obj) {
587 if (this == obj) {
588 return true;
589 }
590 if (obj instanceof GroupInstruction) {
591 GroupInstruction that = (GroupInstruction) obj;
592 return Objects.equals(groupId, that.groupId);
593
594 }
595 return false;
596 }
597 }
598
Saurav Das86af8f12015-05-25 23:55:33 -0700599 /**
alshabib10c810b2015-08-18 16:59:04 -0700600 * A meter instruction.
601 */
602 public static final class MeterInstruction implements Instruction {
603 private final MeterId meterId;
604
605 private MeterInstruction(MeterId meterId) {
606 this.meterId = meterId;
607 }
608
609 public MeterId meterId() {
610 return meterId;
611 }
612
613 @Override
614 public Type type() {
615 return Type.METER;
616 }
617
618 @Override
619 public String toString() {
620 return toStringHelper(type().toString())
621 .add("meter ID", meterId.id()).toString();
622 }
623
624 @Override
625 public int hashCode() {
626 return Objects.hash(type().ordinal(), meterId);
627 }
628
629 @Override
630 public boolean equals(Object obj) {
631 if (this == obj) {
632 return true;
633 }
634 if (obj instanceof MeterInstruction) {
635 MeterInstruction that = (MeterInstruction) obj;
636 return Objects.equals(meterId, that.meterId);
637
638 }
639 return false;
640 }
641 }
642
643 /**
Saurav Das86af8f12015-05-25 23:55:33 -0700644 * Transition instruction.
645 */
alshabibd17abc22015-04-21 18:26:35 -0700646 public static class TableTypeTransition implements Instruction {
647 private final Integer tableId;
648
649 TableTypeTransition(Integer tableId) {
650 this.tableId = tableId;
alshabib9af70072015-02-09 14:34:16 -0800651 }
652
653 @Override
654 public Type type() {
655 return Type.TABLE;
656 }
657
alshabibd17abc22015-04-21 18:26:35 -0700658 public Integer tableId() {
659 return this.tableId;
alshabib9af70072015-02-09 14:34:16 -0800660 }
661
662 @Override
663 public String toString() {
664 return toStringHelper(type().toString())
alshabibd17abc22015-04-21 18:26:35 -0700665 .add("tableId", this.tableId).toString();
alshabib9af70072015-02-09 14:34:16 -0800666 }
667
668 @Override
669 public int hashCode() {
Thomas Vachuska6c8eff32015-05-27 16:11:44 -0700670 return Objects.hash(type().ordinal(), tableId);
alshabib9af70072015-02-09 14:34:16 -0800671 }
672
673 @Override
674 public boolean equals(Object obj) {
675 if (this == obj) {
676 return true;
677 }
678 if (obj instanceof TableTypeTransition) {
679 TableTypeTransition that = (TableTypeTransition) obj;
alshabibd17abc22015-04-21 18:26:35 -0700680 return Objects.equals(tableId, that.tableId);
alshabib9af70072015-02-09 14:34:16 -0800681
682 }
683 return false;
684 }
Saurav Das86af8f12015-05-25 23:55:33 -0700685 }
alshabib9af70072015-02-09 14:34:16 -0800686
Saurav Das86af8f12015-05-25 23:55:33 -0700687 /**
688 * Metadata instruction.
689 */
690 public static class MetadataInstruction implements Instruction {
691 private final long metadata;
692 private final long metadataMask;
693
694 MetadataInstruction(long metadata, long metadataMask) {
695 this.metadata = metadata;
696 this.metadataMask = metadataMask;
697 }
698
699 @Override
700 public Type type() {
701 return Type.METADATA;
702 }
703
704 public long metadata() {
705 return this.metadata;
706 }
707
708 public long metadataMask() {
709 return this.metadataMask;
710 }
711
712 @Override
713 public String toString() {
714 return toStringHelper(type().toString())
715 .add("metadata", Long.toHexString(this.metadata))
716 .add("metadata mask", Long.toHexString(this.metadataMask))
717 .toString();
718 }
719
720 @Override
721 public int hashCode() {
Thomas Vachuska6c8eff32015-05-27 16:11:44 -0700722 return Objects.hash(type().ordinal(), metadata, metadataMask);
Saurav Das86af8f12015-05-25 23:55:33 -0700723 }
724
725 @Override
726 public boolean equals(Object obj) {
727 if (this == obj) {
728 return true;
729 }
730 if (obj instanceof MetadataInstruction) {
731 MetadataInstruction that = (MetadataInstruction) obj;
732 return Objects.equals(metadata, that.metadata) &&
733 Objects.equals(metadataMask, that.metadataMask);
734
735 }
736 return false;
737 }
alshabib9af70072015-02-09 14:34:16 -0800738 }
Saurav Das73a7dd42015-08-19 22:20:31 -0700739
alshabib55a55d92014-09-16 11:59:31 -0700740}
alshabib8ca53902014-10-07 13:11:17 -0700741
742