Thomas Vachuska | 83e090e | 2014-10-22 14:25:35 -0700 | [diff] [blame] | 1 | /* |
Ray Milkey | 34c9590 | 2015-04-15 09:47:53 -0700 | [diff] [blame] | 2 | * Copyright 2014-2015 Open Networking Laboratory |
Thomas Vachuska | 83e090e | 2014-10-22 14:25:35 -0700 | [diff] [blame] | 3 | * |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 4 | * 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 Vachuska | 83e090e | 2014-10-22 14:25:35 -0700 | [diff] [blame] | 7 | * |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 8 | * 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 Vachuska | 83e090e | 2014-10-22 14:25:35 -0700 | [diff] [blame] | 15 | */ |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 16 | package org.onosproject.net.flow.instructions; |
alshabib | 55a55d9 | 2014-09-16 11:59:31 -0700 | [diff] [blame] | 17 | |
alshabib | 7b808c5 | 2015-06-26 14:22:24 -0700 | [diff] [blame] | 18 | import org.onlab.packet.EthType; |
Jonathan Hart | 54b406b | 2015-03-06 16:24:14 -0800 | [diff] [blame] | 19 | import org.onlab.packet.IpAddress; |
| 20 | import org.onlab.packet.MacAddress; |
| 21 | import org.onlab.packet.MplsLabel; |
| 22 | import org.onlab.packet.VlanId; |
sangho | 8995ac5 | 2015-02-04 11:29:03 -0800 | [diff] [blame] | 23 | import org.onosproject.core.GroupId; |
Sho SHIMIZU | 2e7ac84 | 2015-05-05 15:45:38 -0700 | [diff] [blame] | 24 | import org.onosproject.net.IndexedLambda; |
| 25 | import org.onosproject.net.Lambda; |
Sho SHIMIZU | e9e1275 | 2015-05-05 14:45:40 -0700 | [diff] [blame] | 26 | import org.onosproject.net.OchSignal; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 27 | import org.onosproject.net.PortNumber; |
| 28 | import org.onosproject.net.flow.instructions.L0ModificationInstruction.L0SubType; |
| 29 | import org.onosproject.net.flow.instructions.L0ModificationInstruction.ModLambdaInstruction; |
Sho SHIMIZU | 2e7ac84 | 2015-05-05 15:45:38 -0700 | [diff] [blame] | 30 | import org.onosproject.net.flow.instructions.L0ModificationInstruction.ModOchSignalInstruction; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 31 | import org.onosproject.net.flow.instructions.L3ModificationInstruction.L3SubType; |
| 32 | import org.onosproject.net.flow.instructions.L3ModificationInstruction.ModIPInstruction; |
Pavlin Radoslavov | febe82c | 2015-02-11 19:08:15 -0800 | [diff] [blame] | 33 | import org.onosproject.net.flow.instructions.L3ModificationInstruction.ModIPv6FlowLabelInstruction; |
sangho | 3f97a17d | 2015-01-29 22:56:29 -0800 | [diff] [blame] | 34 | import org.onosproject.net.flow.instructions.L3ModificationInstruction.ModTtlInstruction; |
Hyunsun Moon | c8bd97c | 2015-07-18 22:47:33 -0700 | [diff] [blame] | 35 | import org.onosproject.net.flow.instructions.L4ModificationInstruction.L4SubType; |
| 36 | import org.onosproject.net.flow.instructions.L4ModificationInstruction.ModTransportPortInstruction; |
Praseed Balakrishnan | 8c67d17 | 2014-11-10 10:15:41 -0800 | [diff] [blame] | 37 | |
Jonathan Hart | 54b406b | 2015-03-06 16:24:14 -0800 | [diff] [blame] | 38 | import java.util.Objects; |
| 39 | |
| 40 | import static com.google.common.base.MoreObjects.toStringHelper; |
| 41 | import static com.google.common.base.Preconditions.checkNotNull; |
alshabib | 64231f6 | 2014-09-16 17:58:36 -0700 | [diff] [blame] | 42 | |
alshabib | 55a55d9 | 2014-09-16 11:59:31 -0700 | [diff] [blame] | 43 | /** |
| 44 | * Factory class for creating various traffic treatment instructions. |
| 45 | */ |
| 46 | public 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 Hart | 54b406b | 2015-03-06 16:24:14 -0800 | [diff] [blame] | 66 | * |
alshabib | 55a55d9 | 2014-09-16 11:59:31 -0700 | [diff] [blame] | 67 | * @return drop instruction |
| 68 | */ |
| 69 | public static DropInstruction createDrop() { |
| 70 | return new DropInstruction(); |
| 71 | } |
| 72 | |
| 73 | /** |
sangho | 8995ac5 | 2015-02-04 11:29:03 -0800 | [diff] [blame] | 74 | * 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 Leenheer | 4908775 | 2014-10-23 13:54:09 -0700 | [diff] [blame] | 85 | * Creates a l0 modification. |
Jonathan Hart | 54b406b | 2015-03-06 16:24:14 -0800 | [diff] [blame] | 86 | * |
| 87 | * @param lambda the lambda to modify to |
Marc De Leenheer | 4908775 | 2014-10-23 13:54:09 -0700 | [diff] [blame] | 88 | * @return a l0 modification |
Sho SHIMIZU | 2e7ac84 | 2015-05-05 15:45:38 -0700 | [diff] [blame] | 89 | * @deprecated in Cardinal Release. Use {@link #modL0Lambda(Lambda)} instead. |
Marc De Leenheer | 4908775 | 2014-10-23 13:54:09 -0700 | [diff] [blame] | 90 | */ |
Sho SHIMIZU | 2e7ac84 | 2015-05-05 15:45:38 -0700 | [diff] [blame] | 91 | @Deprecated |
Marc De Leenheer | 4908775 | 2014-10-23 13:54:09 -0700 | [diff] [blame] | 92 | 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 SHIMIZU | e9e1275 | 2015-05-05 14:45:40 -0700 | [diff] [blame] | 98 | * Creates an L0 modification with the specified OCh signal. |
| 99 | * |
| 100 | * @param lambda OCh signal |
| 101 | * @return an L0 modification |
| 102 | */ |
Sho SHIMIZU | 2e7ac84 | 2015-05-05 15:45:38 -0700 | [diff] [blame] | 103 | public static L0ModificationInstruction modL0Lambda(Lambda lambda) { |
Sho SHIMIZU | e9e1275 | 2015-05-05 14:45:40 -0700 | [diff] [blame] | 104 | checkNotNull(lambda, "L0 OCh signal cannot be null"); |
Sho SHIMIZU | 2e7ac84 | 2015-05-05 15:45:38 -0700 | [diff] [blame] | 105 | |
| 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 SHIMIZU | e9e1275 | 2015-05-05 14:45:40 -0700 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | /** |
alshabib | 55a55d9 | 2014-09-16 11:59:31 -0700 | [diff] [blame] | 116 | * Creates a l2 src modification. |
Jonathan Hart | 54b406b | 2015-03-06 16:24:14 -0800 | [diff] [blame] | 117 | * |
| 118 | * @param addr the mac address to modify to |
alshabib | 55a55d9 | 2014-09-16 11:59:31 -0700 | [diff] [blame] | 119 | * @return a l2 modification |
| 120 | */ |
Ayaka Koshibe | a9c199f | 2014-09-16 16:21:40 -0700 | [diff] [blame] | 121 | public static L2ModificationInstruction modL2Src(MacAddress addr) { |
alshabib | 55a55d9 | 2014-09-16 11:59:31 -0700 | [diff] [blame] | 122 | checkNotNull(addr, "Src l2 address cannot be null"); |
alshabib | d17abc2 | 2015-04-21 18:26:35 -0700 | [diff] [blame] | 123 | return new L2ModificationInstruction.ModEtherInstruction( |
| 124 | L2ModificationInstruction.L2SubType.ETH_SRC, addr); |
alshabib | 55a55d9 | 2014-09-16 11:59:31 -0700 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | /** |
| 128 | * Creates a L2 dst modification. |
Jonathan Hart | 54b406b | 2015-03-06 16:24:14 -0800 | [diff] [blame] | 129 | * |
| 130 | * @param addr the mac address to modify to |
alshabib | 55a55d9 | 2014-09-16 11:59:31 -0700 | [diff] [blame] | 131 | * @return a L2 modification |
| 132 | */ |
Ayaka Koshibe | a9c199f | 2014-09-16 16:21:40 -0700 | [diff] [blame] | 133 | public static L2ModificationInstruction modL2Dst(MacAddress addr) { |
alshabib | 55a55d9 | 2014-09-16 11:59:31 -0700 | [diff] [blame] | 134 | checkNotNull(addr, "Dst l2 address cannot be null"); |
alshabib | d17abc2 | 2015-04-21 18:26:35 -0700 | [diff] [blame] | 135 | return new L2ModificationInstruction.ModEtherInstruction( |
| 136 | L2ModificationInstruction.L2SubType.ETH_DST, addr); |
alshabib | 55a55d9 | 2014-09-16 11:59:31 -0700 | [diff] [blame] | 137 | } |
| 138 | |
| 139 | /** |
Jonathan Hart | 54b406b | 2015-03-06 16:24:14 -0800 | [diff] [blame] | 140 | * Creates a VLAN ID modification. |
| 141 | * |
| 142 | * @param vlanId the VLAN ID to modify to |
alshabib | 7410fea | 2014-09-16 13:48:39 -0700 | [diff] [blame] | 143 | * @return a L2 modification |
| 144 | */ |
Ayaka Koshibe | a9c199f | 2014-09-16 16:21:40 -0700 | [diff] [blame] | 145 | public static L2ModificationInstruction modVlanId(VlanId vlanId) { |
alshabib | 55a55d9 | 2014-09-16 11:59:31 -0700 | [diff] [blame] | 146 | checkNotNull(vlanId, "VLAN id cannot be null"); |
alshabib | d17abc2 | 2015-04-21 18:26:35 -0700 | [diff] [blame] | 147 | return new L2ModificationInstruction.ModVlanIdInstruction(vlanId); |
alshabib | 55a55d9 | 2014-09-16 11:59:31 -0700 | [diff] [blame] | 148 | } |
| 149 | |
alshabib | 7410fea | 2014-09-16 13:48:39 -0700 | [diff] [blame] | 150 | /** |
Jonathan Hart | 54b406b | 2015-03-06 16:24:14 -0800 | [diff] [blame] | 151 | * Creates a VLAN PCP modification. |
| 152 | * |
| 153 | * @param vlanPcp the PCP to modify to |
alshabib | 7410fea | 2014-09-16 13:48:39 -0700 | [diff] [blame] | 154 | * @return a L2 modification |
| 155 | */ |
| 156 | public static L2ModificationInstruction modVlanPcp(Byte vlanPcp) { |
| 157 | checkNotNull(vlanPcp, "VLAN Pcp cannot be null"); |
alshabib | d17abc2 | 2015-04-21 18:26:35 -0700 | [diff] [blame] | 158 | return new L2ModificationInstruction.ModVlanPcpInstruction(vlanPcp); |
alshabib | 7410fea | 2014-09-16 13:48:39 -0700 | [diff] [blame] | 159 | } |
| 160 | |
| 161 | /** |
Praseed Balakrishnan | 8c67d17 | 2014-11-10 10:15:41 -0800 | [diff] [blame] | 162 | * Creates a MPLS label modification. |
Jonathan Hart | 54b406b | 2015-03-06 16:24:14 -0800 | [diff] [blame] | 163 | * |
| 164 | * @param mplsLabel MPLS label to set |
Praseed Balakrishnan | 8c67d17 | 2014-11-10 10:15:41 -0800 | [diff] [blame] | 165 | * @return a L2 Modification |
| 166 | */ |
Michele Santuari | 4b6019e | 2014-12-19 11:31:45 +0100 | [diff] [blame] | 167 | public static L2ModificationInstruction modMplsLabel(MplsLabel mplsLabel) { |
Praseed Balakrishnan | 8c67d17 | 2014-11-10 10:15:41 -0800 | [diff] [blame] | 168 | checkNotNull(mplsLabel, "MPLS label cannot be null"); |
alshabib | d17abc2 | 2015-04-21 18:26:35 -0700 | [diff] [blame] | 169 | return new L2ModificationInstruction.ModMplsLabelInstruction(mplsLabel); |
Praseed Balakrishnan | 8c67d17 | 2014-11-10 10:15:41 -0800 | [diff] [blame] | 170 | } |
sangho | 3f97a17d | 2015-01-29 22:56:29 -0800 | [diff] [blame] | 171 | |
| 172 | /** |
Jonathan Hart | 54b406b | 2015-03-06 16:24:14 -0800 | [diff] [blame] | 173 | * Creates a MPLS decrement TTL modification. |
sangho | 3f97a17d | 2015-01-29 22:56:29 -0800 | [diff] [blame] | 174 | * |
| 175 | * @return a L2 Modification |
| 176 | */ |
| 177 | public static L2ModificationInstruction decMplsTtl() { |
alshabib | d17abc2 | 2015-04-21 18:26:35 -0700 | [diff] [blame] | 178 | return new L2ModificationInstruction.ModMplsTtlInstruction(); |
sangho | 3f97a17d | 2015-01-29 22:56:29 -0800 | [diff] [blame] | 179 | } |
Pavlin Radoslavov | febe82c | 2015-02-11 19:08:15 -0800 | [diff] [blame] | 180 | |
Praseed Balakrishnan | 8c67d17 | 2014-11-10 10:15:41 -0800 | [diff] [blame] | 181 | /** |
Pavlin Radoslavov | febe82c | 2015-02-11 19:08:15 -0800 | [diff] [blame] | 182 | * Creates a L3 IPv4 src modification. |
| 183 | * |
| 184 | * @param addr the IPv4 address to modify to |
alshabib | 7410fea | 2014-09-16 13:48:39 -0700 | [diff] [blame] | 185 | * @return a L3 modification |
| 186 | */ |
Pavlin Radoslavov | 855ea2d | 2014-10-30 15:32:39 -0700 | [diff] [blame] | 187 | public static L3ModificationInstruction modL3Src(IpAddress addr) { |
Pavlin Radoslavov | febe82c | 2015-02-11 19:08:15 -0800 | [diff] [blame] | 188 | checkNotNull(addr, "Src l3 IPv4 address cannot be null"); |
| 189 | return new ModIPInstruction(L3SubType.IPV4_SRC, addr); |
alshabib | 7410fea | 2014-09-16 13:48:39 -0700 | [diff] [blame] | 190 | } |
| 191 | |
| 192 | /** |
Pavlin Radoslavov | febe82c | 2015-02-11 19:08:15 -0800 | [diff] [blame] | 193 | * Creates a L3 IPv4 dst modification. |
| 194 | * |
| 195 | * @param addr the IPv4 address to modify to |
alshabib | 7410fea | 2014-09-16 13:48:39 -0700 | [diff] [blame] | 196 | * @return a L3 modification |
| 197 | */ |
Pavlin Radoslavov | 855ea2d | 2014-10-30 15:32:39 -0700 | [diff] [blame] | 198 | public static L3ModificationInstruction modL3Dst(IpAddress addr) { |
Pavlin Radoslavov | febe82c | 2015-02-11 19:08:15 -0800 | [diff] [blame] | 199 | 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); |
alshabib | 7410fea | 2014-09-16 13:48:39 -0700 | [diff] [blame] | 233 | } |
| 234 | |
Praseed Balakrishnan | 8c67d17 | 2014-11-10 10:15:41 -0800 | [diff] [blame] | 235 | /** |
Jonathan Hart | 54b406b | 2015-03-06 16:24:14 -0800 | [diff] [blame] | 236 | * Creates a L3 decrement TTL modification. |
| 237 | * |
sangho | 3f97a17d | 2015-01-29 22:56:29 -0800 | [diff] [blame] | 238 | * @return a L3 modification |
| 239 | */ |
| 240 | public static L3ModificationInstruction decNwTtl() { |
| 241 | return new ModTtlInstruction(L3SubType.DEC_TTL); |
| 242 | } |
| 243 | |
| 244 | /** |
Jonathan Hart | 54b406b | 2015-03-06 16:24:14 -0800 | [diff] [blame] | 245 | * Creates a L3 copy TTL to outer header modification. |
| 246 | * |
sangho | 3f97a17d | 2015-01-29 22:56:29 -0800 | [diff] [blame] | 247 | * @return a L3 modification |
| 248 | */ |
| 249 | public static L3ModificationInstruction copyTtlOut() { |
| 250 | return new ModTtlInstruction(L3SubType.TTL_OUT); |
| 251 | } |
| 252 | |
| 253 | /** |
Jonathan Hart | 54b406b | 2015-03-06 16:24:14 -0800 | [diff] [blame] | 254 | * Creates a L3 copy TTL to inner header modification. |
| 255 | * |
sangho | 3f97a17d | 2015-01-29 22:56:29 -0800 | [diff] [blame] | 256 | * @return a L3 modification |
| 257 | */ |
| 258 | public static L3ModificationInstruction copyTtlIn() { |
| 259 | return new ModTtlInstruction(L3SubType.TTL_IN); |
| 260 | } |
| 261 | |
| 262 | /** |
Jonathan Hart | 54b406b | 2015-03-06 16:24:14 -0800 | [diff] [blame] | 263 | * Creates a push MPLS header instruction. |
| 264 | * |
Praseed Balakrishnan | 8c67d17 | 2014-11-10 10:15:41 -0800 | [diff] [blame] | 265 | * @return a L2 modification. |
| 266 | */ |
| 267 | public static Instruction pushMpls() { |
alshabib | d17abc2 | 2015-04-21 18:26:35 -0700 | [diff] [blame] | 268 | return new L2ModificationInstruction.PushHeaderInstructions( |
| 269 | L2ModificationInstruction.L2SubType.MPLS_PUSH, |
alshabib | 7b808c5 | 2015-06-26 14:22:24 -0700 | [diff] [blame] | 270 | EthType.EtherType.MPLS_UNICAST.ethType()); |
Praseed Balakrishnan | 8c67d17 | 2014-11-10 10:15:41 -0800 | [diff] [blame] | 271 | } |
| 272 | |
| 273 | /** |
Jonathan Hart | 54b406b | 2015-03-06 16:24:14 -0800 | [diff] [blame] | 274 | * Creates a pop MPLS header instruction. |
| 275 | * |
Praseed Balakrishnan | 8c67d17 | 2014-11-10 10:15:41 -0800 | [diff] [blame] | 276 | * @return a L2 modification. |
| 277 | */ |
| 278 | public static Instruction popMpls() { |
alshabib | d17abc2 | 2015-04-21 18:26:35 -0700 | [diff] [blame] | 279 | return new L2ModificationInstruction.PushHeaderInstructions( |
| 280 | L2ModificationInstruction.L2SubType.MPLS_POP, |
alshabib | 7b808c5 | 2015-06-26 14:22:24 -0700 | [diff] [blame] | 281 | EthType.EtherType.MPLS_UNICAST.ethType()); |
Praseed Balakrishnan | 8c67d17 | 2014-11-10 10:15:41 -0800 | [diff] [blame] | 282 | } |
alshabib | 7410fea | 2014-09-16 13:48:39 -0700 | [diff] [blame] | 283 | |
sangho | 3f97a17d | 2015-01-29 22:56:29 -0800 | [diff] [blame] | 284 | /** |
Jonathan Hart | 54b406b | 2015-03-06 16:24:14 -0800 | [diff] [blame] | 285 | * Creates a pop MPLS header instruction with a particular ethertype. |
sangho | 3f97a17d | 2015-01-29 22:56:29 -0800 | [diff] [blame] | 286 | * |
| 287 | * @param etherType Ethernet type to set |
| 288 | * @return a L2 modification. |
Sho SHIMIZU | be63b23 | 2015-06-30 10:57:58 -0700 | [diff] [blame] | 289 | * @deprecated in Cardinal Release |
sangho | 3f97a17d | 2015-01-29 22:56:29 -0800 | [diff] [blame] | 290 | */ |
alshabib | 7b808c5 | 2015-06-26 14:22:24 -0700 | [diff] [blame] | 291 | @Deprecated |
alshabib | 0ad4398 | 2015-05-07 13:43:13 -0700 | [diff] [blame] | 292 | public static Instruction popMpls(int etherType) { |
sangho | 3f97a17d | 2015-01-29 22:56:29 -0800 | [diff] [blame] | 293 | checkNotNull(etherType, "Ethernet type cannot be null"); |
alshabib | d17abc2 | 2015-04-21 18:26:35 -0700 | [diff] [blame] | 294 | return new L2ModificationInstruction.PushHeaderInstructions( |
alshabib | 7b808c5 | 2015-06-26 14:22:24 -0700 | [diff] [blame] | 295 | 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( |
alshabib | d17abc2 | 2015-04-21 18:26:35 -0700 | [diff] [blame] | 308 | L2ModificationInstruction.L2SubType.MPLS_POP, etherType); |
sangho | 3f97a17d | 2015-01-29 22:56:29 -0800 | [diff] [blame] | 309 | } |
| 310 | |
Saurav Das | fbe25c5 | 2015-03-04 11:12:00 -0800 | [diff] [blame] | 311 | /** |
Jonathan Hart | 54b406b | 2015-03-06 16:24:14 -0800 | [diff] [blame] | 312 | * Creates a pop VLAN header instruction. |
| 313 | * |
| 314 | * @return a L2 modification |
Saurav Das | fbe25c5 | 2015-03-04 11:12:00 -0800 | [diff] [blame] | 315 | */ |
| 316 | public static Instruction popVlan() { |
alshabib | d17abc2 | 2015-04-21 18:26:35 -0700 | [diff] [blame] | 317 | return new L2ModificationInstruction.PopVlanInstruction( |
| 318 | L2ModificationInstruction.L2SubType.VLAN_POP); |
Saurav Das | fbe25c5 | 2015-03-04 11:12:00 -0800 | [diff] [blame] | 319 | } |
| 320 | |
| 321 | /** |
Jonathan Hart | 54b406b | 2015-03-06 16:24:14 -0800 | [diff] [blame] | 322 | * Creates a push VLAN header instruction. |
| 323 | * |
| 324 | * @return a L2 modification |
| 325 | */ |
| 326 | public static Instruction pushVlan() { |
alshabib | d17abc2 | 2015-04-21 18:26:35 -0700 | [diff] [blame] | 327 | return new L2ModificationInstruction.PushHeaderInstructions( |
alshabib | 7b808c5 | 2015-06-26 14:22:24 -0700 | [diff] [blame] | 328 | L2ModificationInstruction.L2SubType.VLAN_PUSH, |
| 329 | EthType.EtherType.VLAN.ethType()); |
Jonathan Hart | 54b406b | 2015-03-06 16:24:14 -0800 | [diff] [blame] | 330 | } |
| 331 | |
| 332 | /** |
alshabib | d17abc2 | 2015-04-21 18:26:35 -0700 | [diff] [blame] | 333 | * Sends the packet to the table id. |
Jonathan Hart | 54b406b | 2015-03-06 16:24:14 -0800 | [diff] [blame] | 334 | * |
alshabib | d17abc2 | 2015-04-21 18:26:35 -0700 | [diff] [blame] | 335 | * @param tableId flow rule table id |
Thomas Vachuska | 3e2b651 | 2015-03-05 09:25:03 -0800 | [diff] [blame] | 336 | * @return table type transition instruction |
Saurav Das | fbe25c5 | 2015-03-04 11:12:00 -0800 | [diff] [blame] | 337 | */ |
alshabib | d17abc2 | 2015-04-21 18:26:35 -0700 | [diff] [blame] | 338 | public static Instruction transition(Integer tableId) { |
| 339 | checkNotNull(tableId, "Table id cannot be null"); |
| 340 | return new TableTypeTransition(tableId); |
alshabib | 9af7007 | 2015-02-09 14:34:16 -0800 | [diff] [blame] | 341 | } |
| 342 | |
Yuta HIGUCHI | 6a47964 | 2015-02-08 01:28:50 -0800 | [diff] [blame] | 343 | /** |
Saurav Das | 86af8f1 | 2015-05-25 23:55:33 -0700 | [diff] [blame] | 344 | * 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 Moon | a08c5d0 | 2015-07-14 17:53:00 -0700 | [diff] [blame] | 355 | * 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 Moon | c8bd97c | 2015-07-18 22:47:33 -0700 | [diff] [blame] | 366 | * 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 HIGUCHI | 6a47964 | 2015-02-08 01:28:50 -0800 | [diff] [blame] | 410 | * Drop instruction. |
alshabib | 55a55d9 | 2014-09-16 11:59:31 -0700 | [diff] [blame] | 411 | */ |
alshabib | 55a55d9 | 2014-09-16 11:59:31 -0700 | [diff] [blame] | 412 | public static final class DropInstruction implements Instruction { |
Yuta HIGUCHI | 6a47964 | 2015-02-08 01:28:50 -0800 | [diff] [blame] | 413 | |
| 414 | private DropInstruction() {} |
| 415 | |
alshabib | 55a55d9 | 2014-09-16 11:59:31 -0700 | [diff] [blame] | 416 | @Override |
| 417 | public Type type() { |
| 418 | return Type.DROP; |
| 419 | } |
alshabib | 99b8fdc | 2014-09-25 14:30:22 -0700 | [diff] [blame] | 420 | |
| 421 | @Override |
| 422 | public String toString() { |
alshabib | 346b5b3 | 2015-03-06 00:42:16 -0800 | [diff] [blame] | 423 | return toStringHelper(type().toString()).toString(); |
alshabib | 99b8fdc | 2014-09-25 14:30:22 -0700 | [diff] [blame] | 424 | } |
alshabib | 8ca5390 | 2014-10-07 13:11:17 -0700 | [diff] [blame] | 425 | |
| 426 | @Override |
| 427 | public int hashCode() { |
Thomas Vachuska | 6c8eff3 | 2015-05-27 16:11:44 -0700 | [diff] [blame] | 428 | return Objects.hash(type().ordinal()); |
alshabib | 8ca5390 | 2014-10-07 13:11:17 -0700 | [diff] [blame] | 429 | } |
| 430 | |
| 431 | @Override |
| 432 | public boolean equals(Object obj) { |
| 433 | if (this == obj) { |
| 434 | return true; |
| 435 | } |
| 436 | if (obj instanceof DropInstruction) { |
Yuta HIGUCHI | 6a47964 | 2015-02-08 01:28:50 -0800 | [diff] [blame] | 437 | return true; |
alshabib | 8ca5390 | 2014-10-07 13:11:17 -0700 | [diff] [blame] | 438 | } |
| 439 | return false; |
| 440 | } |
alshabib | 55a55d9 | 2014-09-16 11:59:31 -0700 | [diff] [blame] | 441 | } |
| 442 | |
Yuta HIGUCHI | 6a47964 | 2015-02-08 01:28:50 -0800 | [diff] [blame] | 443 | /** |
| 444 | * Output Instruction. |
sangho | 8995ac5 | 2015-02-04 11:29:03 -0800 | [diff] [blame] | 445 | */ |
alshabib | 55a55d9 | 2014-09-16 11:59:31 -0700 | [diff] [blame] | 446 | 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 | } |
alshabib | 99b8fdc | 2014-09-25 14:30:22 -0700 | [diff] [blame] | 461 | @Override |
| 462 | public String toString() { |
| 463 | return toStringHelper(type().toString()) |
| 464 | .add("port", port).toString(); |
| 465 | } |
alshabib | 8ca5390 | 2014-10-07 13:11:17 -0700 | [diff] [blame] | 466 | |
| 467 | @Override |
| 468 | public int hashCode() { |
Thomas Vachuska | 6c8eff3 | 2015-05-27 16:11:44 -0700 | [diff] [blame] | 469 | return Objects.hash(type().ordinal(), port); |
alshabib | 8ca5390 | 2014-10-07 13:11:17 -0700 | [diff] [blame] | 470 | } |
| 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 HIGUCHI | 4ce6529 | 2014-11-01 12:09:55 -0700 | [diff] [blame] | 479 | return Objects.equals(port, that.port); |
alshabib | 8ca5390 | 2014-10-07 13:11:17 -0700 | [diff] [blame] | 480 | |
| 481 | } |
| 482 | return false; |
| 483 | } |
alshabib | 55a55d9 | 2014-09-16 11:59:31 -0700 | [diff] [blame] | 484 | } |
| 485 | |
Yuta HIGUCHI | 6a47964 | 2015-02-08 01:28:50 -0800 | [diff] [blame] | 486 | /** |
| 487 | * Group Instruction. |
sangho | 8995ac5 | 2015-02-04 11:29:03 -0800 | [diff] [blame] | 488 | */ |
sangho | 8995ac5 | 2015-02-04 11:29:03 -0800 | [diff] [blame] | 489 | 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 | } |
alshabib | 9af7007 | 2015-02-09 14:34:16 -0800 | [diff] [blame] | 504 | |
sangho | 8995ac5 | 2015-02-04 11:29:03 -0800 | [diff] [blame] | 505 | @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 Vachuska | 6c8eff3 | 2015-05-27 16:11:44 -0700 | [diff] [blame] | 513 | return Objects.hash(type().ordinal(), groupId); |
sangho | 8995ac5 | 2015-02-04 11:29:03 -0800 | [diff] [blame] | 514 | } |
| 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 Das | 86af8f1 | 2015-05-25 23:55:33 -0700 | [diff] [blame] | 530 | /** |
| 531 | * Transition instruction. |
| 532 | */ |
alshabib | d17abc2 | 2015-04-21 18:26:35 -0700 | [diff] [blame] | 533 | public static class TableTypeTransition implements Instruction { |
| 534 | private final Integer tableId; |
| 535 | |
| 536 | TableTypeTransition(Integer tableId) { |
| 537 | this.tableId = tableId; |
alshabib | 9af7007 | 2015-02-09 14:34:16 -0800 | [diff] [blame] | 538 | } |
| 539 | |
| 540 | @Override |
| 541 | public Type type() { |
| 542 | return Type.TABLE; |
| 543 | } |
| 544 | |
alshabib | d17abc2 | 2015-04-21 18:26:35 -0700 | [diff] [blame] | 545 | public Integer tableId() { |
| 546 | return this.tableId; |
alshabib | 9af7007 | 2015-02-09 14:34:16 -0800 | [diff] [blame] | 547 | } |
| 548 | |
| 549 | @Override |
| 550 | public String toString() { |
| 551 | return toStringHelper(type().toString()) |
alshabib | d17abc2 | 2015-04-21 18:26:35 -0700 | [diff] [blame] | 552 | .add("tableId", this.tableId).toString(); |
alshabib | 9af7007 | 2015-02-09 14:34:16 -0800 | [diff] [blame] | 553 | } |
| 554 | |
| 555 | @Override |
| 556 | public int hashCode() { |
Thomas Vachuska | 6c8eff3 | 2015-05-27 16:11:44 -0700 | [diff] [blame] | 557 | return Objects.hash(type().ordinal(), tableId); |
alshabib | 9af7007 | 2015-02-09 14:34:16 -0800 | [diff] [blame] | 558 | } |
| 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; |
alshabib | d17abc2 | 2015-04-21 18:26:35 -0700 | [diff] [blame] | 567 | return Objects.equals(tableId, that.tableId); |
alshabib | 9af7007 | 2015-02-09 14:34:16 -0800 | [diff] [blame] | 568 | |
| 569 | } |
| 570 | return false; |
| 571 | } |
Saurav Das | 86af8f1 | 2015-05-25 23:55:33 -0700 | [diff] [blame] | 572 | } |
alshabib | 9af7007 | 2015-02-09 14:34:16 -0800 | [diff] [blame] | 573 | |
Saurav Das | 86af8f1 | 2015-05-25 23:55:33 -0700 | [diff] [blame] | 574 | /** |
| 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 Vachuska | 6c8eff3 | 2015-05-27 16:11:44 -0700 | [diff] [blame] | 609 | return Objects.hash(type().ordinal(), metadata, metadataMask); |
Saurav Das | 86af8f1 | 2015-05-25 23:55:33 -0700 | [diff] [blame] | 610 | } |
| 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 | } |
alshabib | 9af7007 | 2015-02-09 14:34:16 -0800 | [diff] [blame] | 625 | } |
alshabib | 55a55d9 | 2014-09-16 11:59:31 -0700 | [diff] [blame] | 626 | } |
alshabib | 8ca5390 | 2014-10-07 13:11:17 -0700 | [diff] [blame] | 627 | |
| 628 | |