blob: cb6e20031cc1676490668e9faa2eadeb82cbc5a5 [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
Steffen Gebertba2d3b72015-10-22 11:14:31 +020018import com.google.common.base.MoreObjects;
alshabib7b808c52015-06-26 14:22:24 -070019import org.onlab.packet.EthType;
Jonathan Hart54b406b2015-03-06 16:24:14 -080020import org.onlab.packet.IpAddress;
21import org.onlab.packet.MacAddress;
22import org.onlab.packet.MplsLabel;
Hyunsun Mooncf732fb2015-08-22 21:04:23 -070023import org.onlab.packet.TpPort;
Jonathan Hart54b406b2015-03-06 16:24:14 -080024import org.onlab.packet.VlanId;
sangho8995ac52015-02-04 11:29:03 -080025import org.onosproject.core.GroupId;
Jonathan Hart3c259162015-10-21 21:31:19 -070026import org.onosproject.net.DeviceId;
Sho SHIMIZU2e7ac842015-05-05 15:45:38 -070027import org.onosproject.net.IndexedLambda;
28import org.onosproject.net.Lambda;
Sho SHIMIZUe9e12752015-05-05 14:45:40 -070029import org.onosproject.net.OchSignal;
Yafit Hadar52d81552015-10-07 12:26:52 +030030import org.onosproject.net.OduSignalId;
Brian O'Connorabafb502014-12-02 22:26:20 -080031import org.onosproject.net.PortNumber;
32import org.onosproject.net.flow.instructions.L0ModificationInstruction.L0SubType;
33import org.onosproject.net.flow.instructions.L0ModificationInstruction.ModLambdaInstruction;
Sho SHIMIZU2e7ac842015-05-05 15:45:38 -070034import org.onosproject.net.flow.instructions.L0ModificationInstruction.ModOchSignalInstruction;
Yafit Hadar52d81552015-10-07 12:26:52 +030035import org.onosproject.net.flow.instructions.L1ModificationInstruction.ModOduSignalIdInstruction;
Brian O'Connorabafb502014-12-02 22:26:20 -080036import org.onosproject.net.flow.instructions.L3ModificationInstruction.L3SubType;
37import org.onosproject.net.flow.instructions.L3ModificationInstruction.ModIPInstruction;
lishuai3cce60b2015-12-01 19:35:16 +080038import org.onosproject.net.flow.instructions.L3ModificationInstruction.ModArpIPInstruction;
39import org.onosproject.net.flow.instructions.L3ModificationInstruction.ModArpEthInstruction;
40import org.onosproject.net.flow.instructions.L3ModificationInstruction.ModArpOpInstruction;
Pavlin Radoslavovfebe82c2015-02-11 19:08:15 -080041import org.onosproject.net.flow.instructions.L3ModificationInstruction.ModIPv6FlowLabelInstruction;
sangho3f97a17d2015-01-29 22:56:29 -080042import org.onosproject.net.flow.instructions.L3ModificationInstruction.ModTtlInstruction;
Hyunsun Moonc8bd97c2015-07-18 22:47:33 -070043import org.onosproject.net.flow.instructions.L4ModificationInstruction.L4SubType;
44import org.onosproject.net.flow.instructions.L4ModificationInstruction.ModTransportPortInstruction;
alshabib10c810b2015-08-18 16:59:04 -070045import org.onosproject.net.meter.MeterId;
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -080046
Jonathan Hart54b406b2015-03-06 16:24:14 -080047import java.util.Objects;
48
49import static com.google.common.base.MoreObjects.toStringHelper;
50import static com.google.common.base.Preconditions.checkNotNull;
alshabib64231f62014-09-16 17:58:36 -070051
alshabib55a55d92014-09-16 11:59:31 -070052/**
53 * Factory class for creating various traffic treatment instructions.
54 */
55public final class Instructions {
56
Jonathan Hartc7840bd2016-01-21 23:26:29 -080057 private static final String SEPARATOR = ":";
58
alshabib55a55d92014-09-16 11:59:31 -070059 // Ban construction
60 private Instructions() {}
61
62 /**
63 * Creates an output instruction using the specified port number. This can
64 * include logical ports such as CONTROLLER, FLOOD, etc.
65 *
66 * @param number port number
67 * @return output instruction
68 */
69 public static OutputInstruction createOutput(final PortNumber number) {
70 checkNotNull(number, "PortNumber cannot be null");
71 return new OutputInstruction(number);
72 }
73
74 /**
75 * Creates a drop instruction.
Jonathan Hart54b406b2015-03-06 16:24:14 -080076 *
alshabib55a55d92014-09-16 11:59:31 -070077 * @return drop instruction
78 */
Charles Chan7efabeb2015-09-28 15:12:19 -070079 @Deprecated
alshabib55a55d92014-09-16 11:59:31 -070080 public static DropInstruction createDrop() {
81 return new DropInstruction();
82 }
83
84 /**
Charles Chan7efabeb2015-09-28 15:12:19 -070085 * Creates a no action instruction.
86 *
87 * @return no action instruction
88 */
89 public static NoActionInstruction createNoAction() {
90 return new NoActionInstruction();
91 }
92
93 /**
sangho8995ac52015-02-04 11:29:03 -080094 * Creates a group instruction.
95 *
96 * @param groupId Group Id
97 * @return group instruction
98 */
99 public static GroupInstruction createGroup(final GroupId groupId) {
100 checkNotNull(groupId, "GroupId cannot be null");
101 return new GroupInstruction(groupId);
102 }
103
Steffen Gebertbbfdaaa2015-09-29 11:01:46 +0200104 /**
105 * Creates a set-queue instruction.
106 *
107 * @param queueId Queue Id
Steffen Gebertba2d3b72015-10-22 11:14:31 +0200108 * @param port Port number
Steffen Gebertbbfdaaa2015-09-29 11:01:46 +0200109 * @return set-queue instruction
110 */
Steffen Gebertba2d3b72015-10-22 11:14:31 +0200111 public static SetQueueInstruction setQueue(final long queueId, final PortNumber port) {
Steffen Gebertbbfdaaa2015-09-29 11:01:46 +0200112 checkNotNull(queueId, "queue ID cannot be null");
Steffen Gebertba2d3b72015-10-22 11:14:31 +0200113 return new SetQueueInstruction(queueId, port);
Steffen Gebertbbfdaaa2015-09-29 11:01:46 +0200114 }
115
alshabib10c810b2015-08-18 16:59:04 -0700116 public static MeterInstruction meterTraffic(final MeterId meterId) {
117 checkNotNull(meterId, "meter id cannot be null");
118 return new MeterInstruction(meterId);
119 }
120
sangho8995ac52015-02-04 11:29:03 -0800121 /**
Sho SHIMIZUe9e12752015-05-05 14:45:40 -0700122 * Creates an L0 modification with the specified OCh signal.
123 *
124 * @param lambda OCh signal
125 * @return an L0 modification
126 */
Sho SHIMIZU2e7ac842015-05-05 15:45:38 -0700127 public static L0ModificationInstruction modL0Lambda(Lambda lambda) {
Sho SHIMIZUe9e12752015-05-05 14:45:40 -0700128 checkNotNull(lambda, "L0 OCh signal cannot be null");
Sho SHIMIZU2e7ac842015-05-05 15:45:38 -0700129
130 if (lambda instanceof IndexedLambda) {
131 return new ModLambdaInstruction(L0SubType.LAMBDA, (short) ((IndexedLambda) lambda).index());
132 } else if (lambda instanceof OchSignal) {
133 return new ModOchSignalInstruction((OchSignal) lambda);
134 } else {
135 throw new UnsupportedOperationException(String.format("Unsupported type: %s", lambda));
136 }
Sho SHIMIZUe9e12752015-05-05 14:45:40 -0700137 }
138
139 /**
Yafit Hadar52d81552015-10-07 12:26:52 +0300140 * Creates an L1 modification with the specified ODU signal Id.
141 *
142 * @param oduSignalId ODU Signal Id
143 * @return a L1 modification
144 */
145 public static L1ModificationInstruction modL1OduSignalId(OduSignalId oduSignalId) {
146 checkNotNull(oduSignalId, "L1 ODU signal ID cannot be null");
147 return new ModOduSignalIdInstruction(oduSignalId);
148 }
149 /**
alshabib55a55d92014-09-16 11:59:31 -0700150 * Creates a l2 src modification.
Jonathan Hart54b406b2015-03-06 16:24:14 -0800151 *
152 * @param addr the mac address to modify to
alshabib55a55d92014-09-16 11:59:31 -0700153 * @return a l2 modification
154 */
Ayaka Koshibea9c199f2014-09-16 16:21:40 -0700155 public static L2ModificationInstruction modL2Src(MacAddress addr) {
alshabib55a55d92014-09-16 11:59:31 -0700156 checkNotNull(addr, "Src l2 address cannot be null");
alshabibd17abc22015-04-21 18:26:35 -0700157 return new L2ModificationInstruction.ModEtherInstruction(
158 L2ModificationInstruction.L2SubType.ETH_SRC, addr);
alshabib55a55d92014-09-16 11:59:31 -0700159 }
160
161 /**
162 * Creates a L2 dst modification.
Jonathan Hart54b406b2015-03-06 16:24:14 -0800163 *
164 * @param addr the mac address to modify to
alshabib55a55d92014-09-16 11:59:31 -0700165 * @return a L2 modification
166 */
Ayaka Koshibea9c199f2014-09-16 16:21:40 -0700167 public static L2ModificationInstruction modL2Dst(MacAddress addr) {
alshabib55a55d92014-09-16 11:59:31 -0700168 checkNotNull(addr, "Dst l2 address cannot be null");
alshabibd17abc22015-04-21 18:26:35 -0700169 return new L2ModificationInstruction.ModEtherInstruction(
170 L2ModificationInstruction.L2SubType.ETH_DST, addr);
alshabib55a55d92014-09-16 11:59:31 -0700171 }
172
173 /**
Jonathan Hart54b406b2015-03-06 16:24:14 -0800174 * Creates a VLAN ID modification.
175 *
176 * @param vlanId the VLAN ID to modify to
alshabib7410fea2014-09-16 13:48:39 -0700177 * @return a L2 modification
178 */
Ayaka Koshibea9c199f2014-09-16 16:21:40 -0700179 public static L2ModificationInstruction modVlanId(VlanId vlanId) {
alshabib55a55d92014-09-16 11:59:31 -0700180 checkNotNull(vlanId, "VLAN id cannot be null");
alshabibd17abc22015-04-21 18:26:35 -0700181 return new L2ModificationInstruction.ModVlanIdInstruction(vlanId);
alshabib55a55d92014-09-16 11:59:31 -0700182 }
183
alshabib7410fea2014-09-16 13:48:39 -0700184 /**
Jonathan Hart54b406b2015-03-06 16:24:14 -0800185 * Creates a VLAN PCP modification.
186 *
187 * @param vlanPcp the PCP to modify to
alshabib7410fea2014-09-16 13:48:39 -0700188 * @return a L2 modification
189 */
190 public static L2ModificationInstruction modVlanPcp(Byte vlanPcp) {
191 checkNotNull(vlanPcp, "VLAN Pcp cannot be null");
alshabibd17abc22015-04-21 18:26:35 -0700192 return new L2ModificationInstruction.ModVlanPcpInstruction(vlanPcp);
alshabib7410fea2014-09-16 13:48:39 -0700193 }
194
195 /**
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -0800196 * Creates a MPLS label modification.
Jonathan Hart54b406b2015-03-06 16:24:14 -0800197 *
198 * @param mplsLabel MPLS label to set
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -0800199 * @return a L2 Modification
200 */
Michele Santuari4b6019e2014-12-19 11:31:45 +0100201 public static L2ModificationInstruction modMplsLabel(MplsLabel mplsLabel) {
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -0800202 checkNotNull(mplsLabel, "MPLS label cannot be null");
alshabibd17abc22015-04-21 18:26:35 -0700203 return new L2ModificationInstruction.ModMplsLabelInstruction(mplsLabel);
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -0800204 }
sangho3f97a17d2015-01-29 22:56:29 -0800205
206 /**
Saurav Das73a7dd42015-08-19 22:20:31 -0700207 * Creates a MPLS BOS bit modification.
208 *
209 * @param mplsBos MPLS BOS bit to set (true) or unset (false)
210 * @return a L2 Modification
211 */
212 public static L2ModificationInstruction modMplsBos(boolean mplsBos) {
213 return new L2ModificationInstruction.ModMplsBosInstruction(mplsBos);
214 }
215
216 /**
Jonathan Hart54b406b2015-03-06 16:24:14 -0800217 * Creates a MPLS decrement TTL modification.
sangho3f97a17d2015-01-29 22:56:29 -0800218 *
219 * @return a L2 Modification
220 */
221 public static L2ModificationInstruction decMplsTtl() {
alshabibd17abc22015-04-21 18:26:35 -0700222 return new L2ModificationInstruction.ModMplsTtlInstruction();
sangho3f97a17d2015-01-29 22:56:29 -0800223 }
Pavlin Radoslavovfebe82c2015-02-11 19:08:15 -0800224
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -0800225 /**
Pavlin Radoslavovfebe82c2015-02-11 19:08:15 -0800226 * Creates a L3 IPv4 src modification.
227 *
228 * @param addr the IPv4 address to modify to
alshabib7410fea2014-09-16 13:48:39 -0700229 * @return a L3 modification
230 */
Pavlin Radoslavov855ea2d2014-10-30 15:32:39 -0700231 public static L3ModificationInstruction modL3Src(IpAddress addr) {
Pavlin Radoslavovfebe82c2015-02-11 19:08:15 -0800232 checkNotNull(addr, "Src l3 IPv4 address cannot be null");
233 return new ModIPInstruction(L3SubType.IPV4_SRC, addr);
alshabib7410fea2014-09-16 13:48:39 -0700234 }
235
236 /**
Pavlin Radoslavovfebe82c2015-02-11 19:08:15 -0800237 * Creates a L3 IPv4 dst modification.
238 *
239 * @param addr the IPv4 address to modify to
alshabib7410fea2014-09-16 13:48:39 -0700240 * @return a L3 modification
241 */
Pavlin Radoslavov855ea2d2014-10-30 15:32:39 -0700242 public static L3ModificationInstruction modL3Dst(IpAddress addr) {
Pavlin Radoslavovfebe82c2015-02-11 19:08:15 -0800243 checkNotNull(addr, "Dst l3 IPv4 address cannot be null");
244 return new ModIPInstruction(L3SubType.IPV4_DST, addr);
245 }
246
247 /**
248 * Creates a L3 IPv6 src modification.
249 *
250 * @param addr the IPv6 address to modify to
251 * @return a L3 modification
252 */
253 public static L3ModificationInstruction modL3IPv6Src(IpAddress addr) {
254 checkNotNull(addr, "Src l3 IPv6 address cannot be null");
255 return new ModIPInstruction(L3SubType.IPV6_SRC, addr);
256 }
257
258 /**
259 * Creates a L3 IPv6 dst modification.
260 *
261 * @param addr the IPv6 address to modify to
262 * @return a L3 modification
263 */
264 public static L3ModificationInstruction modL3IPv6Dst(IpAddress addr) {
265 checkNotNull(addr, "Dst l3 IPv6 address cannot be null");
266 return new ModIPInstruction(L3SubType.IPV6_DST, addr);
267 }
268
269 /**
270 * Creates a L3 IPv6 Flow Label modification.
271 *
272 * @param flowLabel the IPv6 flow label to modify to (20 bits)
273 * @return a L3 modification
274 */
275 public static L3ModificationInstruction modL3IPv6FlowLabel(int flowLabel) {
276 return new ModIPv6FlowLabelInstruction(flowLabel);
alshabib7410fea2014-09-16 13:48:39 -0700277 }
278
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -0800279 /**
Jonathan Hart54b406b2015-03-06 16:24:14 -0800280 * Creates a L3 decrement TTL modification.
281 *
sangho3f97a17d2015-01-29 22:56:29 -0800282 * @return a L3 modification
283 */
284 public static L3ModificationInstruction decNwTtl() {
285 return new ModTtlInstruction(L3SubType.DEC_TTL);
286 }
287
288 /**
Jonathan Hart54b406b2015-03-06 16:24:14 -0800289 * Creates a L3 copy TTL to outer header modification.
290 *
sangho3f97a17d2015-01-29 22:56:29 -0800291 * @return a L3 modification
292 */
293 public static L3ModificationInstruction copyTtlOut() {
294 return new ModTtlInstruction(L3SubType.TTL_OUT);
295 }
296
297 /**
Jonathan Hart54b406b2015-03-06 16:24:14 -0800298 * Creates a L3 copy TTL to inner header modification.
299 *
sangho3f97a17d2015-01-29 22:56:29 -0800300 * @return a L3 modification
301 */
302 public static L3ModificationInstruction copyTtlIn() {
303 return new ModTtlInstruction(L3SubType.TTL_IN);
304 }
305
306 /**
lishuai3cce60b2015-12-01 19:35:16 +0800307 * Creates a L3 ARP IP src modification.
308 *
309 * @param addr the ip address to modify to
310 * @return a L3 modification
311 */
312 public static L3ModificationInstruction modArpSpa(IpAddress addr) {
313 checkNotNull(addr, "Src l3 ARP IP address cannot be null");
314 return new ModArpIPInstruction(L3SubType.ARP_SPA, addr);
315 }
316
317 /**
318 * Creates a l3 ARP Ether src modification.
319 *
320 * @param addr the mac address to modify to
321 * @return a l3 modification
322 */
323 public static L3ModificationInstruction modArpSha(MacAddress addr) {
324 checkNotNull(addr, "Src l3 ARP address cannot be null");
325 return new ModArpEthInstruction(L3SubType.ARP_SHA, addr);
326 }
327
328 /**
329 * Creates a l3 ARP operation modification.
330 *
331 * @param op the ARP operation to modify to
332 * @return a l3 modification
333 */
334 public static L3ModificationInstruction modL3ArpOp(short op) {
335 checkNotNull(op, "Arp operation cannot be null");
336 return new ModArpOpInstruction(L3SubType.ARP_OP, op);
337 }
338
339 /**
Jonathan Hart54b406b2015-03-06 16:24:14 -0800340 * Creates a push MPLS header instruction.
341 *
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -0800342 * @return a L2 modification.
343 */
344 public static Instruction pushMpls() {
alshabibd17abc22015-04-21 18:26:35 -0700345 return new L2ModificationInstruction.PushHeaderInstructions(
346 L2ModificationInstruction.L2SubType.MPLS_PUSH,
alshabib7b808c52015-06-26 14:22:24 -0700347 EthType.EtherType.MPLS_UNICAST.ethType());
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -0800348 }
349
350 /**
Jonathan Hart54b406b2015-03-06 16:24:14 -0800351 * Creates a pop MPLS header instruction.
352 *
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -0800353 * @return a L2 modification.
354 */
355 public static Instruction popMpls() {
alshabibd17abc22015-04-21 18:26:35 -0700356 return new L2ModificationInstruction.PushHeaderInstructions(
357 L2ModificationInstruction.L2SubType.MPLS_POP,
alshabib7b808c52015-06-26 14:22:24 -0700358 EthType.EtherType.MPLS_UNICAST.ethType());
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -0800359 }
alshabib7410fea2014-09-16 13:48:39 -0700360
sangho3f97a17d2015-01-29 22:56:29 -0800361 /**
Jonathan Hart54b406b2015-03-06 16:24:14 -0800362 * Creates a pop MPLS header instruction with a particular ethertype.
sangho3f97a17d2015-01-29 22:56:29 -0800363 *
364 * @param etherType Ethernet type to set
365 * @return a L2 modification.
alshabib7b808c52015-06-26 14:22:24 -0700366 */
367 public static Instruction popMpls(EthType etherType) {
368 checkNotNull(etherType, "Ethernet type cannot be null");
369 return new L2ModificationInstruction.PushHeaderInstructions(
alshabibd17abc22015-04-21 18:26:35 -0700370 L2ModificationInstruction.L2SubType.MPLS_POP, etherType);
sangho3f97a17d2015-01-29 22:56:29 -0800371 }
372
Saurav Dasfbe25c52015-03-04 11:12:00 -0800373 /**
Jonathan Hart54b406b2015-03-06 16:24:14 -0800374 * Creates a pop VLAN header instruction.
375 *
376 * @return a L2 modification
Saurav Dasfbe25c52015-03-04 11:12:00 -0800377 */
378 public static Instruction popVlan() {
alshabibd17abc22015-04-21 18:26:35 -0700379 return new L2ModificationInstruction.PopVlanInstruction(
380 L2ModificationInstruction.L2SubType.VLAN_POP);
Saurav Dasfbe25c52015-03-04 11:12:00 -0800381 }
382
383 /**
Jonathan Hart54b406b2015-03-06 16:24:14 -0800384 * Creates a push VLAN header instruction.
385 *
386 * @return a L2 modification
387 */
388 public static Instruction pushVlan() {
alshabibd17abc22015-04-21 18:26:35 -0700389 return new L2ModificationInstruction.PushHeaderInstructions(
alshabib7b808c52015-06-26 14:22:24 -0700390 L2ModificationInstruction.L2SubType.VLAN_PUSH,
391 EthType.EtherType.VLAN.ethType());
Jonathan Hart54b406b2015-03-06 16:24:14 -0800392 }
393
394 /**
alshabibd17abc22015-04-21 18:26:35 -0700395 * Sends the packet to the table id.
Jonathan Hart54b406b2015-03-06 16:24:14 -0800396 *
alshabibd17abc22015-04-21 18:26:35 -0700397 * @param tableId flow rule table id
Thomas Vachuska3e2b6512015-03-05 09:25:03 -0800398 * @return table type transition instruction
Saurav Dasfbe25c52015-03-04 11:12:00 -0800399 */
alshabibd17abc22015-04-21 18:26:35 -0700400 public static Instruction transition(Integer tableId) {
401 checkNotNull(tableId, "Table id cannot be null");
402 return new TableTypeTransition(tableId);
alshabib9af70072015-02-09 14:34:16 -0800403 }
404
Yuta HIGUCHI6a479642015-02-08 01:28:50 -0800405 /**
Saurav Das86af8f12015-05-25 23:55:33 -0700406 * Writes metadata to associate with a packet.
407 *
408 * @param metadata the metadata value to write
409 * @param metadataMask the bits to mask for the metadata value
410 * @return metadata instruction
411 */
412 public static Instruction writeMetadata(long metadata, long metadataMask) {
413 return new MetadataInstruction(metadata, metadataMask);
414 }
415
416 /**
Hyunsun Moona08c5d02015-07-14 17:53:00 -0700417 * Creates a Tunnel ID modification.
418 *
419 * @param tunnelId the Tunnel ID to modify to
420 * @return a L2 modification
421 */
422 public static L2ModificationInstruction modTunnelId(long tunnelId) {
423 checkNotNull(tunnelId, "Tunnel id cannot be null");
424 return new L2ModificationInstruction.ModTunnelIdInstruction(tunnelId);
425 }
426
427 /**
Hyunsun Moonc8bd97c2015-07-18 22:47:33 -0700428 * Creates a TCP src modification.
429 *
430 * @param port the TCP port number to modify to
431 * @return a L4 modification
Hyunsun Mooncf732fb2015-08-22 21:04:23 -0700432 * @deprecated in Drake release
Hyunsun Moonc8bd97c2015-07-18 22:47:33 -0700433 */
Hyunsun Mooncf732fb2015-08-22 21:04:23 -0700434 @Deprecated
Hyunsun Moonc8bd97c2015-07-18 22:47:33 -0700435 public static L4ModificationInstruction modTcpSrc(short port) {
436 checkNotNull(port, "Src TCP port cannot be null");
Hyunsun Mooncf732fb2015-08-22 21:04:23 -0700437 return new ModTransportPortInstruction(L4SubType.TCP_SRC, TpPort.tpPort(port));
438 }
439
440 /**
441 * Creates a TCP src modification.
442 *
443 * @param port the TCP port number to modify to
444 * @return a L4 modification
445 */
446 public static L4ModificationInstruction modTcpSrc(TpPort port) {
447 checkNotNull(port, "Src TCP port cannot be null");
Hyunsun Moonc8bd97c2015-07-18 22:47:33 -0700448 return new ModTransportPortInstruction(L4SubType.TCP_SRC, port);
449 }
450
451 /**
452 * Creates a TCP dst modification.
453 *
454 * @param port the TCP port number to modify to
455 * @return a L4 modification
Hyunsun Mooncf732fb2015-08-22 21:04:23 -0700456 * @deprecated in Drake release
Hyunsun Moonc8bd97c2015-07-18 22:47:33 -0700457 */
Hyunsun Mooncf732fb2015-08-22 21:04:23 -0700458 @Deprecated
Hyunsun Moonc8bd97c2015-07-18 22:47:33 -0700459 public static L4ModificationInstruction modTcpDst(short port) {
460 checkNotNull(port, "Dst TCP port cannot be null");
Hyunsun Mooncf732fb2015-08-22 21:04:23 -0700461 return new ModTransportPortInstruction(L4SubType.TCP_DST, TpPort.tpPort(port));
462 }
463
464 /**
465 * Creates a TCP dst modification.
466 *
467 * @param port the TCP port number to modify to
468 * @return a L4 modification
469 */
470 public static L4ModificationInstruction modTcpDst(TpPort port) {
471 checkNotNull(port, "Dst TCP port cannot be null");
Hyunsun Moonc8bd97c2015-07-18 22:47:33 -0700472 return new ModTransportPortInstruction(L4SubType.TCP_DST, port);
473 }
474
475 /**
476 * Creates a UDP src modification.
477 *
478 * @param port the UDP port number to modify to
479 * @return a L4 modification
Hyunsun Mooncf732fb2015-08-22 21:04:23 -0700480 * @deprecated in Drake release
Hyunsun Moonc8bd97c2015-07-18 22:47:33 -0700481 */
Hyunsun Mooncf732fb2015-08-22 21:04:23 -0700482 @Deprecated
Hyunsun Moonc8bd97c2015-07-18 22:47:33 -0700483 public static L4ModificationInstruction modUdpSrc(short port) {
484 checkNotNull(port, "Src UDP port cannot be null");
Hyunsun Mooncf732fb2015-08-22 21:04:23 -0700485 return new ModTransportPortInstruction(L4SubType.UDP_SRC, TpPort.tpPort(port));
486 }
487
488 /**
489 * Creates a UDP src modification.
490 *
491 * @param port the UDP port number to modify to
492 * @return a L4 modification
493 */
494 public static L4ModificationInstruction modUdpSrc(TpPort port) {
495 checkNotNull(port, "Src UDP port cannot be null");
Hyunsun Moonc8bd97c2015-07-18 22:47:33 -0700496 return new ModTransportPortInstruction(L4SubType.UDP_SRC, port);
497 }
498
499 /**
500 * Creates a UDP dst modification.
501 *
502 * @param port the UDP port number to modify to
503 * @return a L4 modification
Hyunsun Mooncf732fb2015-08-22 21:04:23 -0700504 * @deprecated in Drake release
Hyunsun Moonc8bd97c2015-07-18 22:47:33 -0700505 */
Hyunsun Mooncf732fb2015-08-22 21:04:23 -0700506 @Deprecated
Hyunsun Moonc8bd97c2015-07-18 22:47:33 -0700507 public static L4ModificationInstruction modUdpDst(short port) {
508 checkNotNull(port, "Dst UDP port cannot be null");
Hyunsun Mooncf732fb2015-08-22 21:04:23 -0700509 return new ModTransportPortInstruction(L4SubType.UDP_DST, TpPort.tpPort(port));
510 }
511
512 /**
513 * Creates a UDP dst modification.
514 *
515 * @param port the UDP port number to modify to
516 * @return a L4 modification
517 */
518 public static L4ModificationInstruction modUdpDst(TpPort port) {
519 checkNotNull(port, "Dst UDP port cannot be null");
Hyunsun Moonc8bd97c2015-07-18 22:47:33 -0700520 return new ModTransportPortInstruction(L4SubType.UDP_DST, port);
521 }
522
523 /**
Jonathan Hart3c259162015-10-21 21:31:19 -0700524 * Creates an extension instruction.
525 *
526 * @param extension extension instruction
527 * @param deviceId device ID
528 * @return extension instruction
529 */
alshabib880b6442015-11-23 22:13:04 -0800530 public static ExtensionInstructionWrapper extension(ExtensionTreatment extension,
Jonathan Hart3c259162015-10-21 21:31:19 -0700531 DeviceId deviceId) {
532 checkNotNull(extension, "Extension instruction cannot be null");
533 checkNotNull(deviceId, "Device ID cannot be null");
534 return new ExtensionInstructionWrapper(extension, deviceId);
535 }
536
537 /**
Yuta HIGUCHI6a479642015-02-08 01:28:50 -0800538 * Drop instruction.
alshabib55a55d92014-09-16 11:59:31 -0700539 */
Charles Chan7efabeb2015-09-28 15:12:19 -0700540 @Deprecated
alshabib55a55d92014-09-16 11:59:31 -0700541 public static final class DropInstruction implements Instruction {
Yuta HIGUCHI6a479642015-02-08 01:28:50 -0800542
543 private DropInstruction() {}
544
alshabib55a55d92014-09-16 11:59:31 -0700545 @Override
546 public Type type() {
547 return Type.DROP;
548 }
alshabib99b8fdc2014-09-25 14:30:22 -0700549
550 @Override
551 public String toString() {
Jonathan Hartc7840bd2016-01-21 23:26:29 -0800552 return type().toString();
alshabib99b8fdc2014-09-25 14:30:22 -0700553 }
alshabib8ca53902014-10-07 13:11:17 -0700554
555 @Override
556 public int hashCode() {
HIGUCHI Yutaca9cc8e2015-10-29 23:26:51 -0700557 return type().ordinal();
alshabib8ca53902014-10-07 13:11:17 -0700558 }
559
560 @Override
561 public boolean equals(Object obj) {
562 if (this == obj) {
563 return true;
564 }
565 if (obj instanceof DropInstruction) {
Yuta HIGUCHI6a479642015-02-08 01:28:50 -0800566 return true;
alshabib8ca53902014-10-07 13:11:17 -0700567 }
568 return false;
569 }
alshabib55a55d92014-09-16 11:59:31 -0700570 }
571
Yuta HIGUCHI6a479642015-02-08 01:28:50 -0800572 /**
Charles Chan7efabeb2015-09-28 15:12:19 -0700573 * No Action instruction.
574 */
575 public static final class NoActionInstruction implements Instruction {
576
577 private NoActionInstruction() {}
578
579 @Override
580 public Type type() {
581 return Type.NOACTION;
582 }
583
584 @Override
585 public String toString() {
Jonathan Hartc7840bd2016-01-21 23:26:29 -0800586 return type().toString();
Charles Chan7efabeb2015-09-28 15:12:19 -0700587 }
588
589 @Override
590 public int hashCode() {
HIGUCHI Yutaca9cc8e2015-10-29 23:26:51 -0700591 return type().ordinal();
Charles Chan7efabeb2015-09-28 15:12:19 -0700592 }
593
594 @Override
595 public boolean equals(Object obj) {
596 if (this == obj) {
597 return true;
598 }
599 if (obj instanceof NoActionInstruction) {
600 return true;
601 }
602 return false;
603 }
604 }
605
606 /**
Yuta HIGUCHI6a479642015-02-08 01:28:50 -0800607 * Output Instruction.
sangho8995ac52015-02-04 11:29:03 -0800608 */
alshabib55a55d92014-09-16 11:59:31 -0700609 public static final class OutputInstruction implements Instruction {
610 private final PortNumber port;
611
612 private OutputInstruction(PortNumber port) {
613 this.port = port;
614 }
615
616 public PortNumber port() {
617 return port;
618 }
619
620 @Override
621 public Type type() {
622 return Type.OUTPUT;
623 }
Jonathan Hartc7840bd2016-01-21 23:26:29 -0800624
alshabib99b8fdc2014-09-25 14:30:22 -0700625 @Override
626 public String toString() {
Jonathan Hartc7840bd2016-01-21 23:26:29 -0800627 return type().toString() + SEPARATOR + port.toString();
alshabib99b8fdc2014-09-25 14:30:22 -0700628 }
alshabib8ca53902014-10-07 13:11:17 -0700629
630 @Override
631 public int hashCode() {
Thomas Vachuska6c8eff32015-05-27 16:11:44 -0700632 return Objects.hash(type().ordinal(), port);
alshabib8ca53902014-10-07 13:11:17 -0700633 }
634
635 @Override
636 public boolean equals(Object obj) {
637 if (this == obj) {
638 return true;
639 }
640 if (obj instanceof OutputInstruction) {
641 OutputInstruction that = (OutputInstruction) obj;
Yuta HIGUCHI4ce65292014-11-01 12:09:55 -0700642 return Objects.equals(port, that.port);
alshabib8ca53902014-10-07 13:11:17 -0700643
644 }
645 return false;
646 }
alshabib55a55d92014-09-16 11:59:31 -0700647 }
648
Yuta HIGUCHI6a479642015-02-08 01:28:50 -0800649 /**
650 * Group Instruction.
sangho8995ac52015-02-04 11:29:03 -0800651 */
sangho8995ac52015-02-04 11:29:03 -0800652 public static final class GroupInstruction implements Instruction {
653 private final GroupId groupId;
654
655 private GroupInstruction(GroupId groupId) {
656 this.groupId = groupId;
657 }
658
659 public GroupId groupId() {
660 return groupId;
661 }
662
663 @Override
664 public Type type() {
665 return Type.GROUP;
666 }
alshabib9af70072015-02-09 14:34:16 -0800667
sangho8995ac52015-02-04 11:29:03 -0800668 @Override
669 public String toString() {
Jonathan Hartc7840bd2016-01-21 23:26:29 -0800670 return type().toString() + SEPARATOR + Integer.toHexString(groupId.id());
sangho8995ac52015-02-04 11:29:03 -0800671 }
672
673 @Override
674 public int hashCode() {
Thomas Vachuska6c8eff32015-05-27 16:11:44 -0700675 return Objects.hash(type().ordinal(), groupId);
sangho8995ac52015-02-04 11:29:03 -0800676 }
677
678 @Override
679 public boolean equals(Object obj) {
680 if (this == obj) {
681 return true;
682 }
683 if (obj instanceof GroupInstruction) {
684 GroupInstruction that = (GroupInstruction) obj;
685 return Objects.equals(groupId, that.groupId);
686
687 }
688 return false;
689 }
690 }
691
Saurav Das86af8f12015-05-25 23:55:33 -0700692 /**
Steffen Gebertbbfdaaa2015-09-29 11:01:46 +0200693 * Set-Queue Instruction.
694 */
695 public static final class SetQueueInstruction implements Instruction {
696 private final long queueId;
Steffen Gebertba2d3b72015-10-22 11:14:31 +0200697 private final PortNumber port;
Steffen Gebertbbfdaaa2015-09-29 11:01:46 +0200698
699 private SetQueueInstruction(long queueId) {
700 this.queueId = queueId;
Steffen Gebertba2d3b72015-10-22 11:14:31 +0200701 this.port = null;
702 }
703
704 private SetQueueInstruction(long queueId, PortNumber port) {
705 this.queueId = queueId;
706 this.port = port;
Steffen Gebertbbfdaaa2015-09-29 11:01:46 +0200707 }
708
709 public long queueId() {
710 return queueId;
711 }
712
Steffen Gebertba2d3b72015-10-22 11:14:31 +0200713 public PortNumber port() {
714 return port;
715 }
716
Steffen Gebertbbfdaaa2015-09-29 11:01:46 +0200717 @Override
718 public Type type() {
719 return Type.QUEUE;
720 }
721
722 @Override
723 public String toString() {
Steffen Gebertba2d3b72015-10-22 11:14:31 +0200724 MoreObjects.ToStringHelper toStringHelper = toStringHelper(type().toString());
725 toStringHelper.add("queueId", queueId);
726
727 if (port() != null) {
728 toStringHelper.add("port", port);
729 }
730 return toStringHelper.toString();
Steffen Gebertbbfdaaa2015-09-29 11:01:46 +0200731 }
732
733 @Override
734 public int hashCode() {
Steffen Gebertba2d3b72015-10-22 11:14:31 +0200735 return Objects.hash(type().ordinal(), queueId, port);
Steffen Gebertbbfdaaa2015-09-29 11:01:46 +0200736 }
737
738 @Override
739 public boolean equals(Object obj) {
740 if (this == obj) {
741 return true;
742 }
743 if (obj instanceof SetQueueInstruction) {
744 SetQueueInstruction that = (SetQueueInstruction) obj;
Steffen Gebertba2d3b72015-10-22 11:14:31 +0200745 return Objects.equals(queueId, that.queueId) && Objects.equals(port, that.port);
Steffen Gebertbbfdaaa2015-09-29 11:01:46 +0200746
747 }
748 return false;
749 }
750 }
751
752 /**
alshabib10c810b2015-08-18 16:59:04 -0700753 * A meter instruction.
754 */
755 public static final class MeterInstruction implements Instruction {
756 private final MeterId meterId;
757
758 private MeterInstruction(MeterId meterId) {
759 this.meterId = meterId;
760 }
761
762 public MeterId meterId() {
763 return meterId;
764 }
765
766 @Override
767 public Type type() {
768 return Type.METER;
769 }
770
771 @Override
772 public String toString() {
Jonathan Hartc7840bd2016-01-21 23:26:29 -0800773 return type().toString() + SEPARATOR + meterId.id();
alshabib10c810b2015-08-18 16:59:04 -0700774 }
775
776 @Override
777 public int hashCode() {
778 return Objects.hash(type().ordinal(), meterId);
779 }
780
781 @Override
782 public boolean equals(Object obj) {
783 if (this == obj) {
784 return true;
785 }
786 if (obj instanceof MeterInstruction) {
787 MeterInstruction that = (MeterInstruction) obj;
788 return Objects.equals(meterId, that.meterId);
789
790 }
791 return false;
792 }
793 }
794
795 /**
Saurav Das86af8f12015-05-25 23:55:33 -0700796 * Transition instruction.
797 */
alshabibd17abc22015-04-21 18:26:35 -0700798 public static class TableTypeTransition implements Instruction {
799 private final Integer tableId;
800
801 TableTypeTransition(Integer tableId) {
802 this.tableId = tableId;
alshabib9af70072015-02-09 14:34:16 -0800803 }
804
805 @Override
806 public Type type() {
807 return Type.TABLE;
808 }
809
alshabibd17abc22015-04-21 18:26:35 -0700810 public Integer tableId() {
811 return this.tableId;
alshabib9af70072015-02-09 14:34:16 -0800812 }
813
814 @Override
815 public String toString() {
Jonathan Hartc7840bd2016-01-21 23:26:29 -0800816 return type().toString() + SEPARATOR + this.tableId;
alshabib9af70072015-02-09 14:34:16 -0800817 }
818
819 @Override
820 public int hashCode() {
Thomas Vachuska6c8eff32015-05-27 16:11:44 -0700821 return Objects.hash(type().ordinal(), tableId);
alshabib9af70072015-02-09 14:34:16 -0800822 }
823
824 @Override
825 public boolean equals(Object obj) {
826 if (this == obj) {
827 return true;
828 }
829 if (obj instanceof TableTypeTransition) {
830 TableTypeTransition that = (TableTypeTransition) obj;
alshabibd17abc22015-04-21 18:26:35 -0700831 return Objects.equals(tableId, that.tableId);
alshabib9af70072015-02-09 14:34:16 -0800832
833 }
834 return false;
835 }
Saurav Das86af8f12015-05-25 23:55:33 -0700836 }
alshabib9af70072015-02-09 14:34:16 -0800837
Saurav Das86af8f12015-05-25 23:55:33 -0700838 /**
839 * Metadata instruction.
840 */
841 public static class MetadataInstruction implements Instruction {
842 private final long metadata;
843 private final long metadataMask;
844
845 MetadataInstruction(long metadata, long metadataMask) {
846 this.metadata = metadata;
847 this.metadataMask = metadataMask;
848 }
849
850 @Override
851 public Type type() {
852 return Type.METADATA;
853 }
854
855 public long metadata() {
856 return this.metadata;
857 }
858
859 public long metadataMask() {
860 return this.metadataMask;
861 }
862
863 @Override
864 public String toString() {
Jonathan Hartc7840bd2016-01-21 23:26:29 -0800865 return type().toString() + SEPARATOR +
866 Long.toHexString(this.metadata) + "/" +
867 Long.toHexString(this.metadataMask);
Saurav Das86af8f12015-05-25 23:55:33 -0700868 }
869
870 @Override
871 public int hashCode() {
Thomas Vachuska6c8eff32015-05-27 16:11:44 -0700872 return Objects.hash(type().ordinal(), metadata, metadataMask);
Saurav Das86af8f12015-05-25 23:55:33 -0700873 }
874
875 @Override
876 public boolean equals(Object obj) {
877 if (this == obj) {
878 return true;
879 }
880 if (obj instanceof MetadataInstruction) {
881 MetadataInstruction that = (MetadataInstruction) obj;
882 return Objects.equals(metadata, that.metadata) &&
883 Objects.equals(metadataMask, that.metadataMask);
884
885 }
886 return false;
887 }
alshabib9af70072015-02-09 14:34:16 -0800888 }
Saurav Das73a7dd42015-08-19 22:20:31 -0700889
Jonathan Hart3c259162015-10-21 21:31:19 -0700890 /**
891 * Extension instruction.
892 */
893 public static class ExtensionInstructionWrapper implements Instruction {
alshabib880b6442015-11-23 22:13:04 -0800894 private final ExtensionTreatment extensionTreatment;
Jonathan Hart3c259162015-10-21 21:31:19 -0700895 private final DeviceId deviceId;
896
alshabib880b6442015-11-23 22:13:04 -0800897 ExtensionInstructionWrapper(ExtensionTreatment extension, DeviceId deviceId) {
898 extensionTreatment = extension;
Jonathan Hart3c259162015-10-21 21:31:19 -0700899 this.deviceId = deviceId;
900 }
901
alshabib880b6442015-11-23 22:13:04 -0800902 public ExtensionTreatment extensionInstruction() {
903 return extensionTreatment;
Jonathan Hart3c259162015-10-21 21:31:19 -0700904 }
905
906 public DeviceId deviceId() {
907 return deviceId;
908 }
909
910 @Override
911 public Type type() {
912 return Type.EXTENSION;
913 }
914
915 @Override
916 public String toString() {
Jonathan Hartc7840bd2016-01-21 23:26:29 -0800917 return type().toString() + SEPARATOR + deviceId + "/" + extensionTreatment;
Jonathan Hart3c259162015-10-21 21:31:19 -0700918 }
919
920 @Override
921 public int hashCode() {
alshabib880b6442015-11-23 22:13:04 -0800922 return Objects.hash(type().ordinal(), extensionTreatment, deviceId);
Jonathan Hart3c259162015-10-21 21:31:19 -0700923 }
924
925 @Override
926 public boolean equals(Object obj) {
927 if (this == obj) {
928 return true;
929 }
930 if (obj instanceof ExtensionInstructionWrapper) {
931 ExtensionInstructionWrapper that = (ExtensionInstructionWrapper) obj;
alshabib880b6442015-11-23 22:13:04 -0800932 return Objects.equals(extensionTreatment, that.extensionTreatment)
Jonathan Hart3c259162015-10-21 21:31:19 -0700933 && Objects.equals(deviceId, that.deviceId);
934
935 }
936 return false;
937 }
938 }
939
alshabib55a55d92014-09-16 11:59:31 -0700940}
alshabib8ca53902014-10-07 13:11:17 -0700941
942