blob: 8ed882c880621c47aeeb4f8fda30f38a95ff649a [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
alshabib55a55d92014-09-16 11:59:31 -070057 // Ban construction
58 private Instructions() {}
59
60 /**
61 * Creates an output instruction using the specified port number. This can
62 * include logical ports such as CONTROLLER, FLOOD, etc.
63 *
64 * @param number port number
65 * @return output instruction
66 */
67 public static OutputInstruction createOutput(final PortNumber number) {
68 checkNotNull(number, "PortNumber cannot be null");
69 return new OutputInstruction(number);
70 }
71
72 /**
73 * Creates a drop instruction.
Jonathan Hart54b406b2015-03-06 16:24:14 -080074 *
alshabib55a55d92014-09-16 11:59:31 -070075 * @return drop instruction
76 */
Charles Chan7efabeb2015-09-28 15:12:19 -070077 @Deprecated
alshabib55a55d92014-09-16 11:59:31 -070078 public static DropInstruction createDrop() {
79 return new DropInstruction();
80 }
81
82 /**
Charles Chan7efabeb2015-09-28 15:12:19 -070083 * Creates a no action instruction.
84 *
85 * @return no action instruction
86 */
87 public static NoActionInstruction createNoAction() {
88 return new NoActionInstruction();
89 }
90
91 /**
sangho8995ac52015-02-04 11:29:03 -080092 * Creates a group instruction.
93 *
94 * @param groupId Group Id
95 * @return group instruction
96 */
97 public static GroupInstruction createGroup(final GroupId groupId) {
98 checkNotNull(groupId, "GroupId cannot be null");
99 return new GroupInstruction(groupId);
100 }
101
Steffen Gebertbbfdaaa2015-09-29 11:01:46 +0200102 /**
103 * Creates a set-queue instruction.
104 *
105 * @param queueId Queue Id
Steffen Gebertba2d3b72015-10-22 11:14:31 +0200106 * @param port Port number
Steffen Gebertbbfdaaa2015-09-29 11:01:46 +0200107 * @return set-queue instruction
108 */
Steffen Gebertba2d3b72015-10-22 11:14:31 +0200109 public static SetQueueInstruction setQueue(final long queueId, final PortNumber port) {
Steffen Gebertbbfdaaa2015-09-29 11:01:46 +0200110 checkNotNull(queueId, "queue ID cannot be null");
Steffen Gebertba2d3b72015-10-22 11:14:31 +0200111 return new SetQueueInstruction(queueId, port);
Steffen Gebertbbfdaaa2015-09-29 11:01:46 +0200112 }
113
alshabib10c810b2015-08-18 16:59:04 -0700114 public static MeterInstruction meterTraffic(final MeterId meterId) {
115 checkNotNull(meterId, "meter id cannot be null");
116 return new MeterInstruction(meterId);
117 }
118
sangho8995ac52015-02-04 11:29:03 -0800119 /**
Sho SHIMIZUe9e12752015-05-05 14:45:40 -0700120 * Creates an L0 modification with the specified OCh signal.
121 *
122 * @param lambda OCh signal
123 * @return an L0 modification
124 */
Sho SHIMIZU2e7ac842015-05-05 15:45:38 -0700125 public static L0ModificationInstruction modL0Lambda(Lambda lambda) {
Sho SHIMIZUe9e12752015-05-05 14:45:40 -0700126 checkNotNull(lambda, "L0 OCh signal cannot be null");
Sho SHIMIZU2e7ac842015-05-05 15:45:38 -0700127
128 if (lambda instanceof IndexedLambda) {
129 return new ModLambdaInstruction(L0SubType.LAMBDA, (short) ((IndexedLambda) lambda).index());
130 } else if (lambda instanceof OchSignal) {
131 return new ModOchSignalInstruction((OchSignal) lambda);
132 } else {
133 throw new UnsupportedOperationException(String.format("Unsupported type: %s", lambda));
134 }
Sho SHIMIZUe9e12752015-05-05 14:45:40 -0700135 }
136
137 /**
Yafit Hadar52d81552015-10-07 12:26:52 +0300138 * Creates an L1 modification with the specified ODU signal Id.
139 *
140 * @param oduSignalId ODU Signal Id
141 * @return a L1 modification
142 */
143 public static L1ModificationInstruction modL1OduSignalId(OduSignalId oduSignalId) {
144 checkNotNull(oduSignalId, "L1 ODU signal ID cannot be null");
145 return new ModOduSignalIdInstruction(oduSignalId);
146 }
147 /**
alshabib55a55d92014-09-16 11:59:31 -0700148 * Creates a l2 src modification.
Jonathan Hart54b406b2015-03-06 16:24:14 -0800149 *
150 * @param addr the mac address to modify to
alshabib55a55d92014-09-16 11:59:31 -0700151 * @return a l2 modification
152 */
Ayaka Koshibea9c199f2014-09-16 16:21:40 -0700153 public static L2ModificationInstruction modL2Src(MacAddress addr) {
alshabib55a55d92014-09-16 11:59:31 -0700154 checkNotNull(addr, "Src l2 address cannot be null");
alshabibd17abc22015-04-21 18:26:35 -0700155 return new L2ModificationInstruction.ModEtherInstruction(
156 L2ModificationInstruction.L2SubType.ETH_SRC, addr);
alshabib55a55d92014-09-16 11:59:31 -0700157 }
158
159 /**
160 * Creates a L2 dst modification.
Jonathan Hart54b406b2015-03-06 16:24:14 -0800161 *
162 * @param addr the mac address to modify to
alshabib55a55d92014-09-16 11:59:31 -0700163 * @return a L2 modification
164 */
Ayaka Koshibea9c199f2014-09-16 16:21:40 -0700165 public static L2ModificationInstruction modL2Dst(MacAddress addr) {
alshabib55a55d92014-09-16 11:59:31 -0700166 checkNotNull(addr, "Dst l2 address cannot be null");
alshabibd17abc22015-04-21 18:26:35 -0700167 return new L2ModificationInstruction.ModEtherInstruction(
168 L2ModificationInstruction.L2SubType.ETH_DST, addr);
alshabib55a55d92014-09-16 11:59:31 -0700169 }
170
171 /**
Jonathan Hart54b406b2015-03-06 16:24:14 -0800172 * Creates a VLAN ID modification.
173 *
174 * @param vlanId the VLAN ID to modify to
alshabib7410fea2014-09-16 13:48:39 -0700175 * @return a L2 modification
176 */
Ayaka Koshibea9c199f2014-09-16 16:21:40 -0700177 public static L2ModificationInstruction modVlanId(VlanId vlanId) {
alshabib55a55d92014-09-16 11:59:31 -0700178 checkNotNull(vlanId, "VLAN id cannot be null");
alshabibd17abc22015-04-21 18:26:35 -0700179 return new L2ModificationInstruction.ModVlanIdInstruction(vlanId);
alshabib55a55d92014-09-16 11:59:31 -0700180 }
181
alshabib7410fea2014-09-16 13:48:39 -0700182 /**
Jonathan Hart54b406b2015-03-06 16:24:14 -0800183 * Creates a VLAN PCP modification.
184 *
185 * @param vlanPcp the PCP to modify to
alshabib7410fea2014-09-16 13:48:39 -0700186 * @return a L2 modification
187 */
188 public static L2ModificationInstruction modVlanPcp(Byte vlanPcp) {
189 checkNotNull(vlanPcp, "VLAN Pcp cannot be null");
alshabibd17abc22015-04-21 18:26:35 -0700190 return new L2ModificationInstruction.ModVlanPcpInstruction(vlanPcp);
alshabib7410fea2014-09-16 13:48:39 -0700191 }
192
193 /**
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -0800194 * Creates a MPLS label modification.
Jonathan Hart54b406b2015-03-06 16:24:14 -0800195 *
196 * @param mplsLabel MPLS label to set
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -0800197 * @return a L2 Modification
198 */
Michele Santuari4b6019e2014-12-19 11:31:45 +0100199 public static L2ModificationInstruction modMplsLabel(MplsLabel mplsLabel) {
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -0800200 checkNotNull(mplsLabel, "MPLS label cannot be null");
alshabibd17abc22015-04-21 18:26:35 -0700201 return new L2ModificationInstruction.ModMplsLabelInstruction(mplsLabel);
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -0800202 }
sangho3f97a17d2015-01-29 22:56:29 -0800203
204 /**
Saurav Das73a7dd42015-08-19 22:20:31 -0700205 * Creates a MPLS BOS bit modification.
206 *
207 * @param mplsBos MPLS BOS bit to set (true) or unset (false)
208 * @return a L2 Modification
209 */
210 public static L2ModificationInstruction modMplsBos(boolean mplsBos) {
211 return new L2ModificationInstruction.ModMplsBosInstruction(mplsBos);
212 }
213
214 /**
Jonathan Hart54b406b2015-03-06 16:24:14 -0800215 * Creates a MPLS decrement TTL modification.
sangho3f97a17d2015-01-29 22:56:29 -0800216 *
217 * @return a L2 Modification
218 */
219 public static L2ModificationInstruction decMplsTtl() {
alshabibd17abc22015-04-21 18:26:35 -0700220 return new L2ModificationInstruction.ModMplsTtlInstruction();
sangho3f97a17d2015-01-29 22:56:29 -0800221 }
Pavlin Radoslavovfebe82c2015-02-11 19:08:15 -0800222
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -0800223 /**
Pavlin Radoslavovfebe82c2015-02-11 19:08:15 -0800224 * Creates a L3 IPv4 src modification.
225 *
226 * @param addr the IPv4 address to modify to
alshabib7410fea2014-09-16 13:48:39 -0700227 * @return a L3 modification
228 */
Pavlin Radoslavov855ea2d2014-10-30 15:32:39 -0700229 public static L3ModificationInstruction modL3Src(IpAddress addr) {
Pavlin Radoslavovfebe82c2015-02-11 19:08:15 -0800230 checkNotNull(addr, "Src l3 IPv4 address cannot be null");
231 return new ModIPInstruction(L3SubType.IPV4_SRC, addr);
alshabib7410fea2014-09-16 13:48:39 -0700232 }
233
234 /**
Pavlin Radoslavovfebe82c2015-02-11 19:08:15 -0800235 * Creates a L3 IPv4 dst modification.
236 *
237 * @param addr the IPv4 address to modify to
alshabib7410fea2014-09-16 13:48:39 -0700238 * @return a L3 modification
239 */
Pavlin Radoslavov855ea2d2014-10-30 15:32:39 -0700240 public static L3ModificationInstruction modL3Dst(IpAddress addr) {
Pavlin Radoslavovfebe82c2015-02-11 19:08:15 -0800241 checkNotNull(addr, "Dst l3 IPv4 address cannot be null");
242 return new ModIPInstruction(L3SubType.IPV4_DST, addr);
243 }
244
245 /**
246 * Creates a L3 IPv6 src modification.
247 *
248 * @param addr the IPv6 address to modify to
249 * @return a L3 modification
250 */
251 public static L3ModificationInstruction modL3IPv6Src(IpAddress addr) {
252 checkNotNull(addr, "Src l3 IPv6 address cannot be null");
253 return new ModIPInstruction(L3SubType.IPV6_SRC, addr);
254 }
255
256 /**
257 * Creates a L3 IPv6 dst modification.
258 *
259 * @param addr the IPv6 address to modify to
260 * @return a L3 modification
261 */
262 public static L3ModificationInstruction modL3IPv6Dst(IpAddress addr) {
263 checkNotNull(addr, "Dst l3 IPv6 address cannot be null");
264 return new ModIPInstruction(L3SubType.IPV6_DST, addr);
265 }
266
267 /**
268 * Creates a L3 IPv6 Flow Label modification.
269 *
270 * @param flowLabel the IPv6 flow label to modify to (20 bits)
271 * @return a L3 modification
272 */
273 public static L3ModificationInstruction modL3IPv6FlowLabel(int flowLabel) {
274 return new ModIPv6FlowLabelInstruction(flowLabel);
alshabib7410fea2014-09-16 13:48:39 -0700275 }
276
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -0800277 /**
Jonathan Hart54b406b2015-03-06 16:24:14 -0800278 * Creates a L3 decrement TTL modification.
279 *
sangho3f97a17d2015-01-29 22:56:29 -0800280 * @return a L3 modification
281 */
282 public static L3ModificationInstruction decNwTtl() {
283 return new ModTtlInstruction(L3SubType.DEC_TTL);
284 }
285
286 /**
Jonathan Hart54b406b2015-03-06 16:24:14 -0800287 * Creates a L3 copy TTL to outer header modification.
288 *
sangho3f97a17d2015-01-29 22:56:29 -0800289 * @return a L3 modification
290 */
291 public static L3ModificationInstruction copyTtlOut() {
292 return new ModTtlInstruction(L3SubType.TTL_OUT);
293 }
294
295 /**
Jonathan Hart54b406b2015-03-06 16:24:14 -0800296 * Creates a L3 copy TTL to inner header modification.
297 *
sangho3f97a17d2015-01-29 22:56:29 -0800298 * @return a L3 modification
299 */
300 public static L3ModificationInstruction copyTtlIn() {
301 return new ModTtlInstruction(L3SubType.TTL_IN);
302 }
303
304 /**
lishuai3cce60b2015-12-01 19:35:16 +0800305 * Creates a L3 ARP IP src modification.
306 *
307 * @param addr the ip address to modify to
308 * @return a L3 modification
309 */
310 public static L3ModificationInstruction modArpSpa(IpAddress addr) {
311 checkNotNull(addr, "Src l3 ARP IP address cannot be null");
312 return new ModArpIPInstruction(L3SubType.ARP_SPA, addr);
313 }
314
315 /**
316 * Creates a l3 ARP Ether src modification.
317 *
318 * @param addr the mac address to modify to
319 * @return a l3 modification
320 */
321 public static L3ModificationInstruction modArpSha(MacAddress addr) {
322 checkNotNull(addr, "Src l3 ARP address cannot be null");
323 return new ModArpEthInstruction(L3SubType.ARP_SHA, addr);
324 }
325
326 /**
327 * Creates a l3 ARP operation modification.
328 *
329 * @param op the ARP operation to modify to
330 * @return a l3 modification
331 */
332 public static L3ModificationInstruction modL3ArpOp(short op) {
333 checkNotNull(op, "Arp operation cannot be null");
334 return new ModArpOpInstruction(L3SubType.ARP_OP, op);
335 }
336
337 /**
Jonathan Hart54b406b2015-03-06 16:24:14 -0800338 * Creates a push MPLS header instruction.
339 *
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -0800340 * @return a L2 modification.
341 */
342 public static Instruction pushMpls() {
alshabibd17abc22015-04-21 18:26:35 -0700343 return new L2ModificationInstruction.PushHeaderInstructions(
344 L2ModificationInstruction.L2SubType.MPLS_PUSH,
alshabib7b808c52015-06-26 14:22:24 -0700345 EthType.EtherType.MPLS_UNICAST.ethType());
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -0800346 }
347
348 /**
Jonathan Hart54b406b2015-03-06 16:24:14 -0800349 * Creates a pop MPLS header instruction.
350 *
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -0800351 * @return a L2 modification.
352 */
353 public static Instruction popMpls() {
alshabibd17abc22015-04-21 18:26:35 -0700354 return new L2ModificationInstruction.PushHeaderInstructions(
355 L2ModificationInstruction.L2SubType.MPLS_POP,
alshabib7b808c52015-06-26 14:22:24 -0700356 EthType.EtherType.MPLS_UNICAST.ethType());
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -0800357 }
alshabib7410fea2014-09-16 13:48:39 -0700358
sangho3f97a17d2015-01-29 22:56:29 -0800359 /**
Jonathan Hart54b406b2015-03-06 16:24:14 -0800360 * Creates a pop MPLS header instruction with a particular ethertype.
sangho3f97a17d2015-01-29 22:56:29 -0800361 *
362 * @param etherType Ethernet type to set
363 * @return a L2 modification.
alshabib7b808c52015-06-26 14:22:24 -0700364 */
365 public static Instruction popMpls(EthType etherType) {
366 checkNotNull(etherType, "Ethernet type cannot be null");
367 return new L2ModificationInstruction.PushHeaderInstructions(
alshabibd17abc22015-04-21 18:26:35 -0700368 L2ModificationInstruction.L2SubType.MPLS_POP, etherType);
sangho3f97a17d2015-01-29 22:56:29 -0800369 }
370
Saurav Dasfbe25c52015-03-04 11:12:00 -0800371 /**
Jonathan Hart54b406b2015-03-06 16:24:14 -0800372 * Creates a pop VLAN header instruction.
373 *
374 * @return a L2 modification
Saurav Dasfbe25c52015-03-04 11:12:00 -0800375 */
376 public static Instruction popVlan() {
alshabibd17abc22015-04-21 18:26:35 -0700377 return new L2ModificationInstruction.PopVlanInstruction(
378 L2ModificationInstruction.L2SubType.VLAN_POP);
Saurav Dasfbe25c52015-03-04 11:12:00 -0800379 }
380
381 /**
Jonathan Hart54b406b2015-03-06 16:24:14 -0800382 * Creates a push VLAN header instruction.
383 *
384 * @return a L2 modification
385 */
386 public static Instruction pushVlan() {
alshabibd17abc22015-04-21 18:26:35 -0700387 return new L2ModificationInstruction.PushHeaderInstructions(
alshabib7b808c52015-06-26 14:22:24 -0700388 L2ModificationInstruction.L2SubType.VLAN_PUSH,
389 EthType.EtherType.VLAN.ethType());
Jonathan Hart54b406b2015-03-06 16:24:14 -0800390 }
391
392 /**
alshabibd17abc22015-04-21 18:26:35 -0700393 * Sends the packet to the table id.
Jonathan Hart54b406b2015-03-06 16:24:14 -0800394 *
alshabibd17abc22015-04-21 18:26:35 -0700395 * @param tableId flow rule table id
Thomas Vachuska3e2b6512015-03-05 09:25:03 -0800396 * @return table type transition instruction
Saurav Dasfbe25c52015-03-04 11:12:00 -0800397 */
alshabibd17abc22015-04-21 18:26:35 -0700398 public static Instruction transition(Integer tableId) {
399 checkNotNull(tableId, "Table id cannot be null");
400 return new TableTypeTransition(tableId);
alshabib9af70072015-02-09 14:34:16 -0800401 }
402
Yuta HIGUCHI6a479642015-02-08 01:28:50 -0800403 /**
Saurav Das86af8f12015-05-25 23:55:33 -0700404 * Writes metadata to associate with a packet.
405 *
406 * @param metadata the metadata value to write
407 * @param metadataMask the bits to mask for the metadata value
408 * @return metadata instruction
409 */
410 public static Instruction writeMetadata(long metadata, long metadataMask) {
411 return new MetadataInstruction(metadata, metadataMask);
412 }
413
414 /**
Hyunsun Moona08c5d02015-07-14 17:53:00 -0700415 * Creates a Tunnel ID modification.
416 *
417 * @param tunnelId the Tunnel ID to modify to
418 * @return a L2 modification
419 */
420 public static L2ModificationInstruction modTunnelId(long tunnelId) {
421 checkNotNull(tunnelId, "Tunnel id cannot be null");
422 return new L2ModificationInstruction.ModTunnelIdInstruction(tunnelId);
423 }
424
425 /**
Hyunsun Moonc8bd97c2015-07-18 22:47:33 -0700426 * Creates a TCP src modification.
427 *
428 * @param port the TCP port number to modify to
429 * @return a L4 modification
Hyunsun Mooncf732fb2015-08-22 21:04:23 -0700430 * @deprecated in Drake release
Hyunsun Moonc8bd97c2015-07-18 22:47:33 -0700431 */
Hyunsun Mooncf732fb2015-08-22 21:04:23 -0700432 @Deprecated
Hyunsun Moonc8bd97c2015-07-18 22:47:33 -0700433 public static L4ModificationInstruction modTcpSrc(short port) {
434 checkNotNull(port, "Src TCP port cannot be null");
Hyunsun Mooncf732fb2015-08-22 21:04:23 -0700435 return new ModTransportPortInstruction(L4SubType.TCP_SRC, TpPort.tpPort(port));
436 }
437
438 /**
439 * Creates a TCP src modification.
440 *
441 * @param port the TCP port number to modify to
442 * @return a L4 modification
443 */
444 public static L4ModificationInstruction modTcpSrc(TpPort port) {
445 checkNotNull(port, "Src TCP port cannot be null");
Hyunsun Moonc8bd97c2015-07-18 22:47:33 -0700446 return new ModTransportPortInstruction(L4SubType.TCP_SRC, port);
447 }
448
449 /**
450 * Creates a TCP dst modification.
451 *
452 * @param port the TCP port number to modify to
453 * @return a L4 modification
Hyunsun Mooncf732fb2015-08-22 21:04:23 -0700454 * @deprecated in Drake release
Hyunsun Moonc8bd97c2015-07-18 22:47:33 -0700455 */
Hyunsun Mooncf732fb2015-08-22 21:04:23 -0700456 @Deprecated
Hyunsun Moonc8bd97c2015-07-18 22:47:33 -0700457 public static L4ModificationInstruction modTcpDst(short port) {
458 checkNotNull(port, "Dst TCP port cannot be null");
Hyunsun Mooncf732fb2015-08-22 21:04:23 -0700459 return new ModTransportPortInstruction(L4SubType.TCP_DST, TpPort.tpPort(port));
460 }
461
462 /**
463 * Creates a TCP dst modification.
464 *
465 * @param port the TCP port number to modify to
466 * @return a L4 modification
467 */
468 public static L4ModificationInstruction modTcpDst(TpPort port) {
469 checkNotNull(port, "Dst TCP port cannot be null");
Hyunsun Moonc8bd97c2015-07-18 22:47:33 -0700470 return new ModTransportPortInstruction(L4SubType.TCP_DST, port);
471 }
472
473 /**
474 * Creates a UDP src modification.
475 *
476 * @param port the UDP port number to modify to
477 * @return a L4 modification
Hyunsun Mooncf732fb2015-08-22 21:04:23 -0700478 * @deprecated in Drake release
Hyunsun Moonc8bd97c2015-07-18 22:47:33 -0700479 */
Hyunsun Mooncf732fb2015-08-22 21:04:23 -0700480 @Deprecated
Hyunsun Moonc8bd97c2015-07-18 22:47:33 -0700481 public static L4ModificationInstruction modUdpSrc(short port) {
482 checkNotNull(port, "Src UDP port cannot be null");
Hyunsun Mooncf732fb2015-08-22 21:04:23 -0700483 return new ModTransportPortInstruction(L4SubType.UDP_SRC, TpPort.tpPort(port));
484 }
485
486 /**
487 * Creates a UDP src modification.
488 *
489 * @param port the UDP port number to modify to
490 * @return a L4 modification
491 */
492 public static L4ModificationInstruction modUdpSrc(TpPort port) {
493 checkNotNull(port, "Src UDP port cannot be null");
Hyunsun Moonc8bd97c2015-07-18 22:47:33 -0700494 return new ModTransportPortInstruction(L4SubType.UDP_SRC, port);
495 }
496
497 /**
498 * Creates a UDP dst modification.
499 *
500 * @param port the UDP port number to modify to
501 * @return a L4 modification
Hyunsun Mooncf732fb2015-08-22 21:04:23 -0700502 * @deprecated in Drake release
Hyunsun Moonc8bd97c2015-07-18 22:47:33 -0700503 */
Hyunsun Mooncf732fb2015-08-22 21:04:23 -0700504 @Deprecated
Hyunsun Moonc8bd97c2015-07-18 22:47:33 -0700505 public static L4ModificationInstruction modUdpDst(short port) {
506 checkNotNull(port, "Dst UDP port cannot be null");
Hyunsun Mooncf732fb2015-08-22 21:04:23 -0700507 return new ModTransportPortInstruction(L4SubType.UDP_DST, TpPort.tpPort(port));
508 }
509
510 /**
511 * Creates a UDP dst modification.
512 *
513 * @param port the UDP port number to modify to
514 * @return a L4 modification
515 */
516 public static L4ModificationInstruction modUdpDst(TpPort port) {
517 checkNotNull(port, "Dst UDP port cannot be null");
Hyunsun Moonc8bd97c2015-07-18 22:47:33 -0700518 return new ModTransportPortInstruction(L4SubType.UDP_DST, port);
519 }
520
521 /**
Jonathan Hart3c259162015-10-21 21:31:19 -0700522 * Creates an extension instruction.
523 *
524 * @param extension extension instruction
525 * @param deviceId device ID
526 * @return extension instruction
527 */
alshabib880b6442015-11-23 22:13:04 -0800528 public static ExtensionInstructionWrapper extension(ExtensionTreatment extension,
Jonathan Hart3c259162015-10-21 21:31:19 -0700529 DeviceId deviceId) {
530 checkNotNull(extension, "Extension instruction cannot be null");
531 checkNotNull(deviceId, "Device ID cannot be null");
532 return new ExtensionInstructionWrapper(extension, deviceId);
533 }
534
535 /**
Yuta HIGUCHI6a479642015-02-08 01:28:50 -0800536 * Drop instruction.
alshabib55a55d92014-09-16 11:59:31 -0700537 */
Charles Chan7efabeb2015-09-28 15:12:19 -0700538 @Deprecated
alshabib55a55d92014-09-16 11:59:31 -0700539 public static final class DropInstruction implements Instruction {
Yuta HIGUCHI6a479642015-02-08 01:28:50 -0800540
541 private DropInstruction() {}
542
alshabib55a55d92014-09-16 11:59:31 -0700543 @Override
544 public Type type() {
545 return Type.DROP;
546 }
alshabib99b8fdc2014-09-25 14:30:22 -0700547
548 @Override
549 public String toString() {
alshabib346b5b32015-03-06 00:42:16 -0800550 return toStringHelper(type().toString()).toString();
alshabib99b8fdc2014-09-25 14:30:22 -0700551 }
alshabib8ca53902014-10-07 13:11:17 -0700552
553 @Override
554 public int hashCode() {
HIGUCHI Yutaca9cc8e2015-10-29 23:26:51 -0700555 return type().ordinal();
alshabib8ca53902014-10-07 13:11:17 -0700556 }
557
558 @Override
559 public boolean equals(Object obj) {
560 if (this == obj) {
561 return true;
562 }
563 if (obj instanceof DropInstruction) {
Yuta HIGUCHI6a479642015-02-08 01:28:50 -0800564 return true;
alshabib8ca53902014-10-07 13:11:17 -0700565 }
566 return false;
567 }
alshabib55a55d92014-09-16 11:59:31 -0700568 }
569
Yuta HIGUCHI6a479642015-02-08 01:28:50 -0800570 /**
Charles Chan7efabeb2015-09-28 15:12:19 -0700571 * No Action instruction.
572 */
573 public static final class NoActionInstruction implements Instruction {
574
575 private NoActionInstruction() {}
576
577 @Override
578 public Type type() {
579 return Type.NOACTION;
580 }
581
582 @Override
583 public String toString() {
584 return toStringHelper(type().toString()).toString();
585 }
586
587 @Override
588 public int hashCode() {
HIGUCHI Yutaca9cc8e2015-10-29 23:26:51 -0700589 return type().ordinal();
Charles Chan7efabeb2015-09-28 15:12:19 -0700590 }
591
592 @Override
593 public boolean equals(Object obj) {
594 if (this == obj) {
595 return true;
596 }
597 if (obj instanceof NoActionInstruction) {
598 return true;
599 }
600 return false;
601 }
602 }
603
604 /**
Yuta HIGUCHI6a479642015-02-08 01:28:50 -0800605 * Output Instruction.
sangho8995ac52015-02-04 11:29:03 -0800606 */
alshabib55a55d92014-09-16 11:59:31 -0700607 public static final class OutputInstruction implements Instruction {
608 private final PortNumber port;
609
610 private OutputInstruction(PortNumber port) {
611 this.port = port;
612 }
613
614 public PortNumber port() {
615 return port;
616 }
617
618 @Override
619 public Type type() {
620 return Type.OUTPUT;
621 }
alshabib99b8fdc2014-09-25 14:30:22 -0700622 @Override
623 public String toString() {
624 return toStringHelper(type().toString())
625 .add("port", port).toString();
626 }
alshabib8ca53902014-10-07 13:11:17 -0700627
628 @Override
629 public int hashCode() {
Thomas Vachuska6c8eff32015-05-27 16:11:44 -0700630 return Objects.hash(type().ordinal(), port);
alshabib8ca53902014-10-07 13:11:17 -0700631 }
632
633 @Override
634 public boolean equals(Object obj) {
635 if (this == obj) {
636 return true;
637 }
638 if (obj instanceof OutputInstruction) {
639 OutputInstruction that = (OutputInstruction) obj;
Yuta HIGUCHI4ce65292014-11-01 12:09:55 -0700640 return Objects.equals(port, that.port);
alshabib8ca53902014-10-07 13:11:17 -0700641
642 }
643 return false;
644 }
alshabib55a55d92014-09-16 11:59:31 -0700645 }
646
Yuta HIGUCHI6a479642015-02-08 01:28:50 -0800647 /**
648 * Group Instruction.
sangho8995ac52015-02-04 11:29:03 -0800649 */
sangho8995ac52015-02-04 11:29:03 -0800650 public static final class GroupInstruction implements Instruction {
651 private final GroupId groupId;
652
653 private GroupInstruction(GroupId groupId) {
654 this.groupId = groupId;
655 }
656
657 public GroupId groupId() {
658 return groupId;
659 }
660
661 @Override
662 public Type type() {
663 return Type.GROUP;
664 }
alshabib9af70072015-02-09 14:34:16 -0800665
sangho8995ac52015-02-04 11:29:03 -0800666 @Override
667 public String toString() {
668 return toStringHelper(type().toString())
Saurav Das4f980082015-11-05 13:39:15 -0800669 .addValue("group ID=0x" + Integer.toHexString(groupId.id()))
670 .toString();
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() {
773 return toStringHelper(type().toString())
774 .add("meter ID", meterId.id()).toString();
775 }
776
777 @Override
778 public int hashCode() {
779 return Objects.hash(type().ordinal(), meterId);
780 }
781
782 @Override
783 public boolean equals(Object obj) {
784 if (this == obj) {
785 return true;
786 }
787 if (obj instanceof MeterInstruction) {
788 MeterInstruction that = (MeterInstruction) obj;
789 return Objects.equals(meterId, that.meterId);
790
791 }
792 return false;
793 }
794 }
795
796 /**
Saurav Das86af8f12015-05-25 23:55:33 -0700797 * Transition instruction.
798 */
alshabibd17abc22015-04-21 18:26:35 -0700799 public static class TableTypeTransition implements Instruction {
800 private final Integer tableId;
801
802 TableTypeTransition(Integer tableId) {
803 this.tableId = tableId;
alshabib9af70072015-02-09 14:34:16 -0800804 }
805
806 @Override
807 public Type type() {
808 return Type.TABLE;
809 }
810
alshabibd17abc22015-04-21 18:26:35 -0700811 public Integer tableId() {
812 return this.tableId;
alshabib9af70072015-02-09 14:34:16 -0800813 }
814
815 @Override
816 public String toString() {
817 return toStringHelper(type().toString())
alshabibd17abc22015-04-21 18:26:35 -0700818 .add("tableId", this.tableId).toString();
alshabib9af70072015-02-09 14:34:16 -0800819 }
820
821 @Override
822 public int hashCode() {
Thomas Vachuska6c8eff32015-05-27 16:11:44 -0700823 return Objects.hash(type().ordinal(), tableId);
alshabib9af70072015-02-09 14:34:16 -0800824 }
825
826 @Override
827 public boolean equals(Object obj) {
828 if (this == obj) {
829 return true;
830 }
831 if (obj instanceof TableTypeTransition) {
832 TableTypeTransition that = (TableTypeTransition) obj;
alshabibd17abc22015-04-21 18:26:35 -0700833 return Objects.equals(tableId, that.tableId);
alshabib9af70072015-02-09 14:34:16 -0800834
835 }
836 return false;
837 }
Saurav Das86af8f12015-05-25 23:55:33 -0700838 }
alshabib9af70072015-02-09 14:34:16 -0800839
Saurav Das86af8f12015-05-25 23:55:33 -0700840 /**
841 * Metadata instruction.
842 */
843 public static class MetadataInstruction implements Instruction {
844 private final long metadata;
845 private final long metadataMask;
846
847 MetadataInstruction(long metadata, long metadataMask) {
848 this.metadata = metadata;
849 this.metadataMask = metadataMask;
850 }
851
852 @Override
853 public Type type() {
854 return Type.METADATA;
855 }
856
857 public long metadata() {
858 return this.metadata;
859 }
860
861 public long metadataMask() {
862 return this.metadataMask;
863 }
864
865 @Override
866 public String toString() {
867 return toStringHelper(type().toString())
868 .add("metadata", Long.toHexString(this.metadata))
869 .add("metadata mask", Long.toHexString(this.metadataMask))
870 .toString();
871 }
872
873 @Override
874 public int hashCode() {
Thomas Vachuska6c8eff32015-05-27 16:11:44 -0700875 return Objects.hash(type().ordinal(), metadata, metadataMask);
Saurav Das86af8f12015-05-25 23:55:33 -0700876 }
877
878 @Override
879 public boolean equals(Object obj) {
880 if (this == obj) {
881 return true;
882 }
883 if (obj instanceof MetadataInstruction) {
884 MetadataInstruction that = (MetadataInstruction) obj;
885 return Objects.equals(metadata, that.metadata) &&
886 Objects.equals(metadataMask, that.metadataMask);
887
888 }
889 return false;
890 }
alshabib9af70072015-02-09 14:34:16 -0800891 }
Saurav Das73a7dd42015-08-19 22:20:31 -0700892
Jonathan Hart3c259162015-10-21 21:31:19 -0700893 /**
894 * Extension instruction.
895 */
896 public static class ExtensionInstructionWrapper implements Instruction {
alshabib880b6442015-11-23 22:13:04 -0800897 private final ExtensionTreatment extensionTreatment;
Jonathan Hart3c259162015-10-21 21:31:19 -0700898 private final DeviceId deviceId;
899
alshabib880b6442015-11-23 22:13:04 -0800900 ExtensionInstructionWrapper(ExtensionTreatment extension, DeviceId deviceId) {
901 extensionTreatment = extension;
Jonathan Hart3c259162015-10-21 21:31:19 -0700902 this.deviceId = deviceId;
903 }
904
alshabib880b6442015-11-23 22:13:04 -0800905 public ExtensionTreatment extensionInstruction() {
906 return extensionTreatment;
Jonathan Hart3c259162015-10-21 21:31:19 -0700907 }
908
909 public DeviceId deviceId() {
910 return deviceId;
911 }
912
913 @Override
914 public Type type() {
915 return Type.EXTENSION;
916 }
917
918 @Override
919 public String toString() {
920 return toStringHelper(type().toString())
alshabib880b6442015-11-23 22:13:04 -0800921 .add("extension", extensionTreatment)
Jonathan Hart3c259162015-10-21 21:31:19 -0700922 .add("deviceId", deviceId)
923 .toString();
924 }
925
926 @Override
927 public int hashCode() {
alshabib880b6442015-11-23 22:13:04 -0800928 return Objects.hash(type().ordinal(), extensionTreatment, deviceId);
Jonathan Hart3c259162015-10-21 21:31:19 -0700929 }
930
931 @Override
932 public boolean equals(Object obj) {
933 if (this == obj) {
934 return true;
935 }
936 if (obj instanceof ExtensionInstructionWrapper) {
937 ExtensionInstructionWrapper that = (ExtensionInstructionWrapper) obj;
alshabib880b6442015-11-23 22:13:04 -0800938 return Objects.equals(extensionTreatment, that.extensionTreatment)
Jonathan Hart3c259162015-10-21 21:31:19 -0700939 && Objects.equals(deviceId, that.deviceId);
940
941 }
942 return false;
943 }
944 }
945
alshabib55a55d92014-09-16 11:59:31 -0700946}
alshabib8ca53902014-10-07 13:11:17 -0700947
948