blob: 3cacfa5988828b0f3feff5d2d52aa9eeebfbc3eb [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;
22import org.onlab.packet.VlanId;
sangho8995ac52015-02-04 11:29:03 -080023import org.onosproject.core.GroupId;
Sho SHIMIZU2e7ac842015-05-05 15:45:38 -070024import org.onosproject.net.IndexedLambda;
25import org.onosproject.net.Lambda;
Sho SHIMIZUe9e12752015-05-05 14:45:40 -070026import org.onosproject.net.OchSignal;
Brian O'Connorabafb502014-12-02 22:26:20 -080027import org.onosproject.net.PortNumber;
28import org.onosproject.net.flow.instructions.L0ModificationInstruction.L0SubType;
29import org.onosproject.net.flow.instructions.L0ModificationInstruction.ModLambdaInstruction;
Sho SHIMIZU2e7ac842015-05-05 15:45:38 -070030import org.onosproject.net.flow.instructions.L0ModificationInstruction.ModOchSignalInstruction;
Brian O'Connorabafb502014-12-02 22:26:20 -080031import org.onosproject.net.flow.instructions.L3ModificationInstruction.L3SubType;
32import org.onosproject.net.flow.instructions.L3ModificationInstruction.ModIPInstruction;
Pavlin Radoslavovfebe82c2015-02-11 19:08:15 -080033import org.onosproject.net.flow.instructions.L3ModificationInstruction.ModIPv6FlowLabelInstruction;
sangho3f97a17d2015-01-29 22:56:29 -080034import org.onosproject.net.flow.instructions.L3ModificationInstruction.ModTtlInstruction;
Hyunsun Moonc8bd97c2015-07-18 22:47:33 -070035import org.onosproject.net.flow.instructions.L4ModificationInstruction.L4SubType;
36import org.onosproject.net.flow.instructions.L4ModificationInstruction.ModTransportPortInstruction;
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -080037
Jonathan Hart54b406b2015-03-06 16:24:14 -080038import java.util.Objects;
39
40import static com.google.common.base.MoreObjects.toStringHelper;
41import static com.google.common.base.Preconditions.checkNotNull;
alshabib64231f62014-09-16 17:58:36 -070042
alshabib55a55d92014-09-16 11:59:31 -070043/**
44 * Factory class for creating various traffic treatment instructions.
45 */
46public final class Instructions {
47
48
49 // Ban construction
50 private Instructions() {}
51
52 /**
53 * Creates an output instruction using the specified port number. This can
54 * include logical ports such as CONTROLLER, FLOOD, etc.
55 *
56 * @param number port number
57 * @return output instruction
58 */
59 public static OutputInstruction createOutput(final PortNumber number) {
60 checkNotNull(number, "PortNumber cannot be null");
61 return new OutputInstruction(number);
62 }
63
64 /**
65 * Creates a drop instruction.
Jonathan Hart54b406b2015-03-06 16:24:14 -080066 *
alshabib55a55d92014-09-16 11:59:31 -070067 * @return drop instruction
68 */
69 public static DropInstruction createDrop() {
70 return new DropInstruction();
71 }
72
73 /**
sangho8995ac52015-02-04 11:29:03 -080074 * Creates a group instruction.
75 *
76 * @param groupId Group Id
77 * @return group instruction
78 */
79 public static GroupInstruction createGroup(final GroupId groupId) {
80 checkNotNull(groupId, "GroupId cannot be null");
81 return new GroupInstruction(groupId);
82 }
83
84 /**
Marc De Leenheer49087752014-10-23 13:54:09 -070085 * Creates a l0 modification.
Jonathan Hart54b406b2015-03-06 16:24:14 -080086 *
87 * @param lambda the lambda to modify to
Marc De Leenheer49087752014-10-23 13:54:09 -070088 * @return a l0 modification
Sho SHIMIZU2e7ac842015-05-05 15:45:38 -070089 * @deprecated in Cardinal Release. Use {@link #modL0Lambda(Lambda)} instead.
Marc De Leenheer49087752014-10-23 13:54:09 -070090 */
Sho SHIMIZU2e7ac842015-05-05 15:45:38 -070091 @Deprecated
Marc De Leenheer49087752014-10-23 13:54:09 -070092 public static L0ModificationInstruction modL0Lambda(short lambda) {
93 checkNotNull(lambda, "L0 lambda cannot be null");
94 return new ModLambdaInstruction(L0SubType.LAMBDA, lambda);
95 }
96
97 /**
Sho SHIMIZUe9e12752015-05-05 14:45:40 -070098 * Creates an L0 modification with the specified OCh signal.
99 *
100 * @param lambda OCh signal
101 * @return an L0 modification
102 */
Sho SHIMIZU2e7ac842015-05-05 15:45:38 -0700103 public static L0ModificationInstruction modL0Lambda(Lambda lambda) {
Sho SHIMIZUe9e12752015-05-05 14:45:40 -0700104 checkNotNull(lambda, "L0 OCh signal cannot be null");
Sho SHIMIZU2e7ac842015-05-05 15:45:38 -0700105
106 if (lambda instanceof IndexedLambda) {
107 return new ModLambdaInstruction(L0SubType.LAMBDA, (short) ((IndexedLambda) lambda).index());
108 } else if (lambda instanceof OchSignal) {
109 return new ModOchSignalInstruction((OchSignal) lambda);
110 } else {
111 throw new UnsupportedOperationException(String.format("Unsupported type: %s", lambda));
112 }
Sho SHIMIZUe9e12752015-05-05 14:45:40 -0700113 }
114
115 /**
alshabib55a55d92014-09-16 11:59:31 -0700116 * Creates a l2 src modification.
Jonathan Hart54b406b2015-03-06 16:24:14 -0800117 *
118 * @param addr the mac address to modify to
alshabib55a55d92014-09-16 11:59:31 -0700119 * @return a l2 modification
120 */
Ayaka Koshibea9c199f2014-09-16 16:21:40 -0700121 public static L2ModificationInstruction modL2Src(MacAddress addr) {
alshabib55a55d92014-09-16 11:59:31 -0700122 checkNotNull(addr, "Src l2 address cannot be null");
alshabibd17abc22015-04-21 18:26:35 -0700123 return new L2ModificationInstruction.ModEtherInstruction(
124 L2ModificationInstruction.L2SubType.ETH_SRC, addr);
alshabib55a55d92014-09-16 11:59:31 -0700125 }
126
127 /**
128 * Creates a L2 dst modification.
Jonathan Hart54b406b2015-03-06 16:24:14 -0800129 *
130 * @param addr the mac address to modify to
alshabib55a55d92014-09-16 11:59:31 -0700131 * @return a L2 modification
132 */
Ayaka Koshibea9c199f2014-09-16 16:21:40 -0700133 public static L2ModificationInstruction modL2Dst(MacAddress addr) {
alshabib55a55d92014-09-16 11:59:31 -0700134 checkNotNull(addr, "Dst l2 address cannot be null");
alshabibd17abc22015-04-21 18:26:35 -0700135 return new L2ModificationInstruction.ModEtherInstruction(
136 L2ModificationInstruction.L2SubType.ETH_DST, addr);
alshabib55a55d92014-09-16 11:59:31 -0700137 }
138
139 /**
Jonathan Hart54b406b2015-03-06 16:24:14 -0800140 * Creates a VLAN ID modification.
141 *
142 * @param vlanId the VLAN ID to modify to
alshabib7410fea2014-09-16 13:48:39 -0700143 * @return a L2 modification
144 */
Ayaka Koshibea9c199f2014-09-16 16:21:40 -0700145 public static L2ModificationInstruction modVlanId(VlanId vlanId) {
alshabib55a55d92014-09-16 11:59:31 -0700146 checkNotNull(vlanId, "VLAN id cannot be null");
alshabibd17abc22015-04-21 18:26:35 -0700147 return new L2ModificationInstruction.ModVlanIdInstruction(vlanId);
alshabib55a55d92014-09-16 11:59:31 -0700148 }
149
alshabib7410fea2014-09-16 13:48:39 -0700150 /**
Jonathan Hart54b406b2015-03-06 16:24:14 -0800151 * Creates a VLAN PCP modification.
152 *
153 * @param vlanPcp the PCP to modify to
alshabib7410fea2014-09-16 13:48:39 -0700154 * @return a L2 modification
155 */
156 public static L2ModificationInstruction modVlanPcp(Byte vlanPcp) {
157 checkNotNull(vlanPcp, "VLAN Pcp cannot be null");
alshabibd17abc22015-04-21 18:26:35 -0700158 return new L2ModificationInstruction.ModVlanPcpInstruction(vlanPcp);
alshabib7410fea2014-09-16 13:48:39 -0700159 }
160
161 /**
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -0800162 * Creates a MPLS label modification.
Jonathan Hart54b406b2015-03-06 16:24:14 -0800163 *
164 * @param mplsLabel MPLS label to set
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -0800165 * @return a L2 Modification
166 */
Michele Santuari4b6019e2014-12-19 11:31:45 +0100167 public static L2ModificationInstruction modMplsLabel(MplsLabel mplsLabel) {
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -0800168 checkNotNull(mplsLabel, "MPLS label cannot be null");
alshabibd17abc22015-04-21 18:26:35 -0700169 return new L2ModificationInstruction.ModMplsLabelInstruction(mplsLabel);
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -0800170 }
sangho3f97a17d2015-01-29 22:56:29 -0800171
172 /**
Jonathan Hart54b406b2015-03-06 16:24:14 -0800173 * Creates a MPLS decrement TTL modification.
sangho3f97a17d2015-01-29 22:56:29 -0800174 *
175 * @return a L2 Modification
176 */
177 public static L2ModificationInstruction decMplsTtl() {
alshabibd17abc22015-04-21 18:26:35 -0700178 return new L2ModificationInstruction.ModMplsTtlInstruction();
sangho3f97a17d2015-01-29 22:56:29 -0800179 }
Pavlin Radoslavovfebe82c2015-02-11 19:08:15 -0800180
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -0800181 /**
Pavlin Radoslavovfebe82c2015-02-11 19:08:15 -0800182 * Creates a L3 IPv4 src modification.
183 *
184 * @param addr the IPv4 address to modify to
alshabib7410fea2014-09-16 13:48:39 -0700185 * @return a L3 modification
186 */
Pavlin Radoslavov855ea2d2014-10-30 15:32:39 -0700187 public static L3ModificationInstruction modL3Src(IpAddress addr) {
Pavlin Radoslavovfebe82c2015-02-11 19:08:15 -0800188 checkNotNull(addr, "Src l3 IPv4 address cannot be null");
189 return new ModIPInstruction(L3SubType.IPV4_SRC, addr);
alshabib7410fea2014-09-16 13:48:39 -0700190 }
191
192 /**
Pavlin Radoslavovfebe82c2015-02-11 19:08:15 -0800193 * Creates a L3 IPv4 dst modification.
194 *
195 * @param addr the IPv4 address to modify to
alshabib7410fea2014-09-16 13:48:39 -0700196 * @return a L3 modification
197 */
Pavlin Radoslavov855ea2d2014-10-30 15:32:39 -0700198 public static L3ModificationInstruction modL3Dst(IpAddress addr) {
Pavlin Radoslavovfebe82c2015-02-11 19:08:15 -0800199 checkNotNull(addr, "Dst l3 IPv4 address cannot be null");
200 return new ModIPInstruction(L3SubType.IPV4_DST, addr);
201 }
202
203 /**
204 * Creates a L3 IPv6 src modification.
205 *
206 * @param addr the IPv6 address to modify to
207 * @return a L3 modification
208 */
209 public static L3ModificationInstruction modL3IPv6Src(IpAddress addr) {
210 checkNotNull(addr, "Src l3 IPv6 address cannot be null");
211 return new ModIPInstruction(L3SubType.IPV6_SRC, addr);
212 }
213
214 /**
215 * Creates a L3 IPv6 dst modification.
216 *
217 * @param addr the IPv6 address to modify to
218 * @return a L3 modification
219 */
220 public static L3ModificationInstruction modL3IPv6Dst(IpAddress addr) {
221 checkNotNull(addr, "Dst l3 IPv6 address cannot be null");
222 return new ModIPInstruction(L3SubType.IPV6_DST, addr);
223 }
224
225 /**
226 * Creates a L3 IPv6 Flow Label modification.
227 *
228 * @param flowLabel the IPv6 flow label to modify to (20 bits)
229 * @return a L3 modification
230 */
231 public static L3ModificationInstruction modL3IPv6FlowLabel(int flowLabel) {
232 return new ModIPv6FlowLabelInstruction(flowLabel);
alshabib7410fea2014-09-16 13:48:39 -0700233 }
234
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -0800235 /**
Jonathan Hart54b406b2015-03-06 16:24:14 -0800236 * Creates a L3 decrement TTL modification.
237 *
sangho3f97a17d2015-01-29 22:56:29 -0800238 * @return a L3 modification
239 */
240 public static L3ModificationInstruction decNwTtl() {
241 return new ModTtlInstruction(L3SubType.DEC_TTL);
242 }
243
244 /**
Jonathan Hart54b406b2015-03-06 16:24:14 -0800245 * Creates a L3 copy TTL to outer header modification.
246 *
sangho3f97a17d2015-01-29 22:56:29 -0800247 * @return a L3 modification
248 */
249 public static L3ModificationInstruction copyTtlOut() {
250 return new ModTtlInstruction(L3SubType.TTL_OUT);
251 }
252
253 /**
Jonathan Hart54b406b2015-03-06 16:24:14 -0800254 * Creates a L3 copy TTL to inner header modification.
255 *
sangho3f97a17d2015-01-29 22:56:29 -0800256 * @return a L3 modification
257 */
258 public static L3ModificationInstruction copyTtlIn() {
259 return new ModTtlInstruction(L3SubType.TTL_IN);
260 }
261
262 /**
Jonathan Hart54b406b2015-03-06 16:24:14 -0800263 * Creates a push MPLS header instruction.
264 *
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -0800265 * @return a L2 modification.
266 */
267 public static Instruction pushMpls() {
alshabibd17abc22015-04-21 18:26:35 -0700268 return new L2ModificationInstruction.PushHeaderInstructions(
269 L2ModificationInstruction.L2SubType.MPLS_PUSH,
alshabib7b808c52015-06-26 14:22:24 -0700270 EthType.EtherType.MPLS_UNICAST.ethType());
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -0800271 }
272
273 /**
Jonathan Hart54b406b2015-03-06 16:24:14 -0800274 * Creates a pop MPLS header instruction.
275 *
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -0800276 * @return a L2 modification.
277 */
278 public static Instruction popMpls() {
alshabibd17abc22015-04-21 18:26:35 -0700279 return new L2ModificationInstruction.PushHeaderInstructions(
280 L2ModificationInstruction.L2SubType.MPLS_POP,
alshabib7b808c52015-06-26 14:22:24 -0700281 EthType.EtherType.MPLS_UNICAST.ethType());
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -0800282 }
alshabib7410fea2014-09-16 13:48:39 -0700283
sangho3f97a17d2015-01-29 22:56:29 -0800284 /**
Jonathan Hart54b406b2015-03-06 16:24:14 -0800285 * Creates a pop MPLS header instruction with a particular ethertype.
sangho3f97a17d2015-01-29 22:56:29 -0800286 *
287 * @param etherType Ethernet type to set
288 * @return a L2 modification.
Sho SHIMIZUbe63b232015-06-30 10:57:58 -0700289 * @deprecated in Cardinal Release
sangho3f97a17d2015-01-29 22:56:29 -0800290 */
alshabib7b808c52015-06-26 14:22:24 -0700291 @Deprecated
alshabib0ad43982015-05-07 13:43:13 -0700292 public static Instruction popMpls(int etherType) {
sangho3f97a17d2015-01-29 22:56:29 -0800293 checkNotNull(etherType, "Ethernet type cannot be null");
alshabibd17abc22015-04-21 18:26:35 -0700294 return new L2ModificationInstruction.PushHeaderInstructions(
alshabib7b808c52015-06-26 14:22:24 -0700295 L2ModificationInstruction.L2SubType.MPLS_POP, new EthType(etherType));
296 }
297
298
299 /**
300 * Creates a pop MPLS header instruction with a particular ethertype.
301 *
302 * @param etherType Ethernet type to set
303 * @return a L2 modification.
304 */
305 public static Instruction popMpls(EthType etherType) {
306 checkNotNull(etherType, "Ethernet type cannot be null");
307 return new L2ModificationInstruction.PushHeaderInstructions(
alshabibd17abc22015-04-21 18:26:35 -0700308 L2ModificationInstruction.L2SubType.MPLS_POP, etherType);
sangho3f97a17d2015-01-29 22:56:29 -0800309 }
310
Saurav Dasfbe25c52015-03-04 11:12:00 -0800311 /**
Jonathan Hart54b406b2015-03-06 16:24:14 -0800312 * Creates a pop VLAN header instruction.
313 *
314 * @return a L2 modification
Saurav Dasfbe25c52015-03-04 11:12:00 -0800315 */
316 public static Instruction popVlan() {
alshabibd17abc22015-04-21 18:26:35 -0700317 return new L2ModificationInstruction.PopVlanInstruction(
318 L2ModificationInstruction.L2SubType.VLAN_POP);
Saurav Dasfbe25c52015-03-04 11:12:00 -0800319 }
320
321 /**
Jonathan Hart54b406b2015-03-06 16:24:14 -0800322 * Creates a push VLAN header instruction.
323 *
324 * @return a L2 modification
325 */
326 public static Instruction pushVlan() {
alshabibd17abc22015-04-21 18:26:35 -0700327 return new L2ModificationInstruction.PushHeaderInstructions(
alshabib7b808c52015-06-26 14:22:24 -0700328 L2ModificationInstruction.L2SubType.VLAN_PUSH,
329 EthType.EtherType.VLAN.ethType());
Jonathan Hart54b406b2015-03-06 16:24:14 -0800330 }
331
332 /**
alshabibd17abc22015-04-21 18:26:35 -0700333 * Sends the packet to the table id.
Jonathan Hart54b406b2015-03-06 16:24:14 -0800334 *
alshabibd17abc22015-04-21 18:26:35 -0700335 * @param tableId flow rule table id
Thomas Vachuska3e2b6512015-03-05 09:25:03 -0800336 * @return table type transition instruction
Saurav Dasfbe25c52015-03-04 11:12:00 -0800337 */
alshabibd17abc22015-04-21 18:26:35 -0700338 public static Instruction transition(Integer tableId) {
339 checkNotNull(tableId, "Table id cannot be null");
340 return new TableTypeTransition(tableId);
alshabib9af70072015-02-09 14:34:16 -0800341 }
342
Yuta HIGUCHI6a479642015-02-08 01:28:50 -0800343 /**
Saurav Das86af8f12015-05-25 23:55:33 -0700344 * Writes metadata to associate with a packet.
345 *
346 * @param metadata the metadata value to write
347 * @param metadataMask the bits to mask for the metadata value
348 * @return metadata instruction
349 */
350 public static Instruction writeMetadata(long metadata, long metadataMask) {
351 return new MetadataInstruction(metadata, metadataMask);
352 }
353
354 /**
Hyunsun Moona08c5d02015-07-14 17:53:00 -0700355 * Creates a Tunnel ID modification.
356 *
357 * @param tunnelId the Tunnel ID to modify to
358 * @return a L2 modification
359 */
360 public static L2ModificationInstruction modTunnelId(long tunnelId) {
361 checkNotNull(tunnelId, "Tunnel id cannot be null");
362 return new L2ModificationInstruction.ModTunnelIdInstruction(tunnelId);
363 }
364
365 /**
Hyunsun Moonc8bd97c2015-07-18 22:47:33 -0700366 * Creates a TCP src modification.
367 *
368 * @param port the TCP port number to modify to
369 * @return a L4 modification
370 */
371 public static L4ModificationInstruction modTcpSrc(short port) {
372 checkNotNull(port, "Src TCP port cannot be null");
373 return new ModTransportPortInstruction(L4SubType.TCP_SRC, port);
374 }
375
376 /**
377 * Creates a TCP dst modification.
378 *
379 * @param port the TCP port number to modify to
380 * @return a L4 modification
381 */
382 public static L4ModificationInstruction modTcpDst(short port) {
383 checkNotNull(port, "Dst TCP port cannot be null");
384 return new ModTransportPortInstruction(L4SubType.TCP_DST, port);
385 }
386
387 /**
388 * Creates a UDP src modification.
389 *
390 * @param port the UDP port number to modify to
391 * @return a L4 modification
392 */
393 public static L4ModificationInstruction modUdpSrc(short port) {
394 checkNotNull(port, "Src UDP port cannot be null");
395 return new ModTransportPortInstruction(L4SubType.UDP_SRC, port);
396 }
397
398 /**
399 * Creates a UDP dst modification.
400 *
401 * @param port the UDP port number to modify to
402 * @return a L4 modification
403 */
404 public static L4ModificationInstruction modUdpDst(short port) {
405 checkNotNull(port, "Dst UDP port cannot be null");
406 return new ModTransportPortInstruction(L4SubType.UDP_DST, port);
407 }
408
409 /**
Yuta HIGUCHI6a479642015-02-08 01:28:50 -0800410 * Drop instruction.
alshabib55a55d92014-09-16 11:59:31 -0700411 */
alshabib55a55d92014-09-16 11:59:31 -0700412 public static final class DropInstruction implements Instruction {
Yuta HIGUCHI6a479642015-02-08 01:28:50 -0800413
414 private DropInstruction() {}
415
alshabib55a55d92014-09-16 11:59:31 -0700416 @Override
417 public Type type() {
418 return Type.DROP;
419 }
alshabib99b8fdc2014-09-25 14:30:22 -0700420
421 @Override
422 public String toString() {
alshabib346b5b32015-03-06 00:42:16 -0800423 return toStringHelper(type().toString()).toString();
alshabib99b8fdc2014-09-25 14:30:22 -0700424 }
alshabib8ca53902014-10-07 13:11:17 -0700425
426 @Override
427 public int hashCode() {
Thomas Vachuska6c8eff32015-05-27 16:11:44 -0700428 return Objects.hash(type().ordinal());
alshabib8ca53902014-10-07 13:11:17 -0700429 }
430
431 @Override
432 public boolean equals(Object obj) {
433 if (this == obj) {
434 return true;
435 }
436 if (obj instanceof DropInstruction) {
Yuta HIGUCHI6a479642015-02-08 01:28:50 -0800437 return true;
alshabib8ca53902014-10-07 13:11:17 -0700438 }
439 return false;
440 }
alshabib55a55d92014-09-16 11:59:31 -0700441 }
442
Yuta HIGUCHI6a479642015-02-08 01:28:50 -0800443 /**
444 * Output Instruction.
sangho8995ac52015-02-04 11:29:03 -0800445 */
alshabib55a55d92014-09-16 11:59:31 -0700446 public static final class OutputInstruction implements Instruction {
447 private final PortNumber port;
448
449 private OutputInstruction(PortNumber port) {
450 this.port = port;
451 }
452
453 public PortNumber port() {
454 return port;
455 }
456
457 @Override
458 public Type type() {
459 return Type.OUTPUT;
460 }
alshabib99b8fdc2014-09-25 14:30:22 -0700461 @Override
462 public String toString() {
463 return toStringHelper(type().toString())
464 .add("port", port).toString();
465 }
alshabib8ca53902014-10-07 13:11:17 -0700466
467 @Override
468 public int hashCode() {
Thomas Vachuska6c8eff32015-05-27 16:11:44 -0700469 return Objects.hash(type().ordinal(), port);
alshabib8ca53902014-10-07 13:11:17 -0700470 }
471
472 @Override
473 public boolean equals(Object obj) {
474 if (this == obj) {
475 return true;
476 }
477 if (obj instanceof OutputInstruction) {
478 OutputInstruction that = (OutputInstruction) obj;
Yuta HIGUCHI4ce65292014-11-01 12:09:55 -0700479 return Objects.equals(port, that.port);
alshabib8ca53902014-10-07 13:11:17 -0700480
481 }
482 return false;
483 }
alshabib55a55d92014-09-16 11:59:31 -0700484 }
485
Yuta HIGUCHI6a479642015-02-08 01:28:50 -0800486 /**
487 * Group Instruction.
sangho8995ac52015-02-04 11:29:03 -0800488 */
sangho8995ac52015-02-04 11:29:03 -0800489 public static final class GroupInstruction implements Instruction {
490 private final GroupId groupId;
491
492 private GroupInstruction(GroupId groupId) {
493 this.groupId = groupId;
494 }
495
496 public GroupId groupId() {
497 return groupId;
498 }
499
500 @Override
501 public Type type() {
502 return Type.GROUP;
503 }
alshabib9af70072015-02-09 14:34:16 -0800504
sangho8995ac52015-02-04 11:29:03 -0800505 @Override
506 public String toString() {
507 return toStringHelper(type().toString())
508 .add("group ID", groupId.id()).toString();
509 }
510
511 @Override
512 public int hashCode() {
Thomas Vachuska6c8eff32015-05-27 16:11:44 -0700513 return Objects.hash(type().ordinal(), groupId);
sangho8995ac52015-02-04 11:29:03 -0800514 }
515
516 @Override
517 public boolean equals(Object obj) {
518 if (this == obj) {
519 return true;
520 }
521 if (obj instanceof GroupInstruction) {
522 GroupInstruction that = (GroupInstruction) obj;
523 return Objects.equals(groupId, that.groupId);
524
525 }
526 return false;
527 }
528 }
529
Saurav Das86af8f12015-05-25 23:55:33 -0700530 /**
531 * Transition instruction.
532 */
alshabibd17abc22015-04-21 18:26:35 -0700533 public static class TableTypeTransition implements Instruction {
534 private final Integer tableId;
535
536 TableTypeTransition(Integer tableId) {
537 this.tableId = tableId;
alshabib9af70072015-02-09 14:34:16 -0800538 }
539
540 @Override
541 public Type type() {
542 return Type.TABLE;
543 }
544
alshabibd17abc22015-04-21 18:26:35 -0700545 public Integer tableId() {
546 return this.tableId;
alshabib9af70072015-02-09 14:34:16 -0800547 }
548
549 @Override
550 public String toString() {
551 return toStringHelper(type().toString())
alshabibd17abc22015-04-21 18:26:35 -0700552 .add("tableId", this.tableId).toString();
alshabib9af70072015-02-09 14:34:16 -0800553 }
554
555 @Override
556 public int hashCode() {
Thomas Vachuska6c8eff32015-05-27 16:11:44 -0700557 return Objects.hash(type().ordinal(), tableId);
alshabib9af70072015-02-09 14:34:16 -0800558 }
559
560 @Override
561 public boolean equals(Object obj) {
562 if (this == obj) {
563 return true;
564 }
565 if (obj instanceof TableTypeTransition) {
566 TableTypeTransition that = (TableTypeTransition) obj;
alshabibd17abc22015-04-21 18:26:35 -0700567 return Objects.equals(tableId, that.tableId);
alshabib9af70072015-02-09 14:34:16 -0800568
569 }
570 return false;
571 }
Saurav Das86af8f12015-05-25 23:55:33 -0700572 }
alshabib9af70072015-02-09 14:34:16 -0800573
Saurav Das86af8f12015-05-25 23:55:33 -0700574 /**
575 * Metadata instruction.
576 */
577 public static class MetadataInstruction implements Instruction {
578 private final long metadata;
579 private final long metadataMask;
580
581 MetadataInstruction(long metadata, long metadataMask) {
582 this.metadata = metadata;
583 this.metadataMask = metadataMask;
584 }
585
586 @Override
587 public Type type() {
588 return Type.METADATA;
589 }
590
591 public long metadata() {
592 return this.metadata;
593 }
594
595 public long metadataMask() {
596 return this.metadataMask;
597 }
598
599 @Override
600 public String toString() {
601 return toStringHelper(type().toString())
602 .add("metadata", Long.toHexString(this.metadata))
603 .add("metadata mask", Long.toHexString(this.metadataMask))
604 .toString();
605 }
606
607 @Override
608 public int hashCode() {
Thomas Vachuska6c8eff32015-05-27 16:11:44 -0700609 return Objects.hash(type().ordinal(), metadata, metadataMask);
Saurav Das86af8f12015-05-25 23:55:33 -0700610 }
611
612 @Override
613 public boolean equals(Object obj) {
614 if (this == obj) {
615 return true;
616 }
617 if (obj instanceof MetadataInstruction) {
618 MetadataInstruction that = (MetadataInstruction) obj;
619 return Objects.equals(metadata, that.metadata) &&
620 Objects.equals(metadataMask, that.metadataMask);
621
622 }
623 return false;
624 }
alshabib9af70072015-02-09 14:34:16 -0800625 }
alshabib55a55d92014-09-16 11:59:31 -0700626}
alshabib8ca53902014-10-07 13:11:17 -0700627
628