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; |
alshabib | 10c810b | 2015-08-18 16:59:04 -0700 | [diff] [blame] | 37 | import org.onosproject.net.meter.MeterId; |
Praseed Balakrishnan | 8c67d17 | 2014-11-10 10:15:41 -0800 | [diff] [blame] | 38 | |
Jonathan Hart | 54b406b | 2015-03-06 16:24:14 -0800 | [diff] [blame] | 39 | import java.util.Objects; |
| 40 | |
| 41 | import static com.google.common.base.MoreObjects.toStringHelper; |
| 42 | import static com.google.common.base.Preconditions.checkNotNull; |
alshabib | 64231f6 | 2014-09-16 17:58:36 -0700 | [diff] [blame] | 43 | |
alshabib | 55a55d9 | 2014-09-16 11:59:31 -0700 | [diff] [blame] | 44 | /** |
| 45 | * Factory class for creating various traffic treatment instructions. |
| 46 | */ |
| 47 | public final class Instructions { |
| 48 | |
| 49 | |
| 50 | // Ban construction |
| 51 | private Instructions() {} |
| 52 | |
| 53 | /** |
| 54 | * Creates an output instruction using the specified port number. This can |
| 55 | * include logical ports such as CONTROLLER, FLOOD, etc. |
| 56 | * |
| 57 | * @param number port number |
| 58 | * @return output instruction |
| 59 | */ |
| 60 | public static OutputInstruction createOutput(final PortNumber number) { |
| 61 | checkNotNull(number, "PortNumber cannot be null"); |
| 62 | return new OutputInstruction(number); |
| 63 | } |
| 64 | |
| 65 | /** |
| 66 | * Creates a drop instruction. |
Jonathan Hart | 54b406b | 2015-03-06 16:24:14 -0800 | [diff] [blame] | 67 | * |
alshabib | 55a55d9 | 2014-09-16 11:59:31 -0700 | [diff] [blame] | 68 | * @return drop instruction |
| 69 | */ |
| 70 | public static DropInstruction createDrop() { |
| 71 | return new DropInstruction(); |
| 72 | } |
| 73 | |
| 74 | /** |
sangho | 8995ac5 | 2015-02-04 11:29:03 -0800 | [diff] [blame] | 75 | * Creates a group instruction. |
| 76 | * |
| 77 | * @param groupId Group Id |
| 78 | * @return group instruction |
| 79 | */ |
| 80 | public static GroupInstruction createGroup(final GroupId groupId) { |
| 81 | checkNotNull(groupId, "GroupId cannot be null"); |
| 82 | return new GroupInstruction(groupId); |
| 83 | } |
| 84 | |
alshabib | 10c810b | 2015-08-18 16:59:04 -0700 | [diff] [blame] | 85 | public static MeterInstruction meterTraffic(final MeterId meterId) { |
| 86 | checkNotNull(meterId, "meter id cannot be null"); |
| 87 | return new MeterInstruction(meterId); |
| 88 | } |
| 89 | |
sangho | 8995ac5 | 2015-02-04 11:29:03 -0800 | [diff] [blame] | 90 | /** |
Marc De Leenheer | 4908775 | 2014-10-23 13:54:09 -0700 | [diff] [blame] | 91 | * Creates a l0 modification. |
Jonathan Hart | 54b406b | 2015-03-06 16:24:14 -0800 | [diff] [blame] | 92 | * |
| 93 | * @param lambda the lambda to modify to |
Marc De Leenheer | 4908775 | 2014-10-23 13:54:09 -0700 | [diff] [blame] | 94 | * @return a l0 modification |
Sho SHIMIZU | 2e7ac84 | 2015-05-05 15:45:38 -0700 | [diff] [blame] | 95 | * @deprecated in Cardinal Release. Use {@link #modL0Lambda(Lambda)} instead. |
Marc De Leenheer | 4908775 | 2014-10-23 13:54:09 -0700 | [diff] [blame] | 96 | */ |
Sho SHIMIZU | 2e7ac84 | 2015-05-05 15:45:38 -0700 | [diff] [blame] | 97 | @Deprecated |
Marc De Leenheer | 4908775 | 2014-10-23 13:54:09 -0700 | [diff] [blame] | 98 | public static L0ModificationInstruction modL0Lambda(short lambda) { |
| 99 | checkNotNull(lambda, "L0 lambda cannot be null"); |
| 100 | return new ModLambdaInstruction(L0SubType.LAMBDA, lambda); |
| 101 | } |
| 102 | |
| 103 | /** |
Sho SHIMIZU | e9e1275 | 2015-05-05 14:45:40 -0700 | [diff] [blame] | 104 | * Creates an L0 modification with the specified OCh signal. |
| 105 | * |
| 106 | * @param lambda OCh signal |
| 107 | * @return an L0 modification |
| 108 | */ |
Sho SHIMIZU | 2e7ac84 | 2015-05-05 15:45:38 -0700 | [diff] [blame] | 109 | public static L0ModificationInstruction modL0Lambda(Lambda lambda) { |
Sho SHIMIZU | e9e1275 | 2015-05-05 14:45:40 -0700 | [diff] [blame] | 110 | checkNotNull(lambda, "L0 OCh signal cannot be null"); |
Sho SHIMIZU | 2e7ac84 | 2015-05-05 15:45:38 -0700 | [diff] [blame] | 111 | |
| 112 | if (lambda instanceof IndexedLambda) { |
| 113 | return new ModLambdaInstruction(L0SubType.LAMBDA, (short) ((IndexedLambda) lambda).index()); |
| 114 | } else if (lambda instanceof OchSignal) { |
| 115 | return new ModOchSignalInstruction((OchSignal) lambda); |
| 116 | } else { |
| 117 | throw new UnsupportedOperationException(String.format("Unsupported type: %s", lambda)); |
| 118 | } |
Sho SHIMIZU | e9e1275 | 2015-05-05 14:45:40 -0700 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | /** |
alshabib | 55a55d9 | 2014-09-16 11:59:31 -0700 | [diff] [blame] | 122 | * Creates a l2 src modification. |
Jonathan Hart | 54b406b | 2015-03-06 16:24:14 -0800 | [diff] [blame] | 123 | * |
| 124 | * @param addr the mac address to modify to |
alshabib | 55a55d9 | 2014-09-16 11:59:31 -0700 | [diff] [blame] | 125 | * @return a l2 modification |
| 126 | */ |
Ayaka Koshibe | a9c199f | 2014-09-16 16:21:40 -0700 | [diff] [blame] | 127 | public static L2ModificationInstruction modL2Src(MacAddress addr) { |
alshabib | 55a55d9 | 2014-09-16 11:59:31 -0700 | [diff] [blame] | 128 | checkNotNull(addr, "Src l2 address cannot be null"); |
alshabib | d17abc2 | 2015-04-21 18:26:35 -0700 | [diff] [blame] | 129 | return new L2ModificationInstruction.ModEtherInstruction( |
| 130 | L2ModificationInstruction.L2SubType.ETH_SRC, addr); |
alshabib | 55a55d9 | 2014-09-16 11:59:31 -0700 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | /** |
| 134 | * Creates a L2 dst modification. |
Jonathan Hart | 54b406b | 2015-03-06 16:24:14 -0800 | [diff] [blame] | 135 | * |
| 136 | * @param addr the mac address to modify to |
alshabib | 55a55d9 | 2014-09-16 11:59:31 -0700 | [diff] [blame] | 137 | * @return a L2 modification |
| 138 | */ |
Ayaka Koshibe | a9c199f | 2014-09-16 16:21:40 -0700 | [diff] [blame] | 139 | public static L2ModificationInstruction modL2Dst(MacAddress addr) { |
alshabib | 55a55d9 | 2014-09-16 11:59:31 -0700 | [diff] [blame] | 140 | checkNotNull(addr, "Dst l2 address cannot be null"); |
alshabib | d17abc2 | 2015-04-21 18:26:35 -0700 | [diff] [blame] | 141 | return new L2ModificationInstruction.ModEtherInstruction( |
| 142 | L2ModificationInstruction.L2SubType.ETH_DST, addr); |
alshabib | 55a55d9 | 2014-09-16 11:59:31 -0700 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | /** |
Jonathan Hart | 54b406b | 2015-03-06 16:24:14 -0800 | [diff] [blame] | 146 | * Creates a VLAN ID modification. |
| 147 | * |
| 148 | * @param vlanId the VLAN ID to modify to |
alshabib | 7410fea | 2014-09-16 13:48:39 -0700 | [diff] [blame] | 149 | * @return a L2 modification |
| 150 | */ |
Ayaka Koshibe | a9c199f | 2014-09-16 16:21:40 -0700 | [diff] [blame] | 151 | public static L2ModificationInstruction modVlanId(VlanId vlanId) { |
alshabib | 55a55d9 | 2014-09-16 11:59:31 -0700 | [diff] [blame] | 152 | checkNotNull(vlanId, "VLAN id cannot be null"); |
alshabib | d17abc2 | 2015-04-21 18:26:35 -0700 | [diff] [blame] | 153 | return new L2ModificationInstruction.ModVlanIdInstruction(vlanId); |
alshabib | 55a55d9 | 2014-09-16 11:59:31 -0700 | [diff] [blame] | 154 | } |
| 155 | |
alshabib | 7410fea | 2014-09-16 13:48:39 -0700 | [diff] [blame] | 156 | /** |
Jonathan Hart | 54b406b | 2015-03-06 16:24:14 -0800 | [diff] [blame] | 157 | * Creates a VLAN PCP modification. |
| 158 | * |
| 159 | * @param vlanPcp the PCP to modify to |
alshabib | 7410fea | 2014-09-16 13:48:39 -0700 | [diff] [blame] | 160 | * @return a L2 modification |
| 161 | */ |
| 162 | public static L2ModificationInstruction modVlanPcp(Byte vlanPcp) { |
| 163 | checkNotNull(vlanPcp, "VLAN Pcp cannot be null"); |
alshabib | d17abc2 | 2015-04-21 18:26:35 -0700 | [diff] [blame] | 164 | return new L2ModificationInstruction.ModVlanPcpInstruction(vlanPcp); |
alshabib | 7410fea | 2014-09-16 13:48:39 -0700 | [diff] [blame] | 165 | } |
| 166 | |
| 167 | /** |
Praseed Balakrishnan | 8c67d17 | 2014-11-10 10:15:41 -0800 | [diff] [blame] | 168 | * Creates a MPLS label modification. |
Jonathan Hart | 54b406b | 2015-03-06 16:24:14 -0800 | [diff] [blame] | 169 | * |
| 170 | * @param mplsLabel MPLS label to set |
Praseed Balakrishnan | 8c67d17 | 2014-11-10 10:15:41 -0800 | [diff] [blame] | 171 | * @return a L2 Modification |
| 172 | */ |
Michele Santuari | 4b6019e | 2014-12-19 11:31:45 +0100 | [diff] [blame] | 173 | public static L2ModificationInstruction modMplsLabel(MplsLabel mplsLabel) { |
Praseed Balakrishnan | 8c67d17 | 2014-11-10 10:15:41 -0800 | [diff] [blame] | 174 | checkNotNull(mplsLabel, "MPLS label cannot be null"); |
alshabib | d17abc2 | 2015-04-21 18:26:35 -0700 | [diff] [blame] | 175 | return new L2ModificationInstruction.ModMplsLabelInstruction(mplsLabel); |
Praseed Balakrishnan | 8c67d17 | 2014-11-10 10:15:41 -0800 | [diff] [blame] | 176 | } |
sangho | 3f97a17d | 2015-01-29 22:56:29 -0800 | [diff] [blame] | 177 | |
| 178 | /** |
Saurav Das | 73a7dd4 | 2015-08-19 22:20:31 -0700 | [diff] [blame] | 179 | * Creates a MPLS BOS bit modification. |
| 180 | * |
| 181 | * @param mplsBos MPLS BOS bit to set (true) or unset (false) |
| 182 | * @return a L2 Modification |
| 183 | */ |
| 184 | public static L2ModificationInstruction modMplsBos(boolean mplsBos) { |
| 185 | return new L2ModificationInstruction.ModMplsBosInstruction(mplsBos); |
| 186 | } |
| 187 | |
| 188 | /** |
Jonathan Hart | 54b406b | 2015-03-06 16:24:14 -0800 | [diff] [blame] | 189 | * Creates a MPLS decrement TTL modification. |
sangho | 3f97a17d | 2015-01-29 22:56:29 -0800 | [diff] [blame] | 190 | * |
| 191 | * @return a L2 Modification |
| 192 | */ |
| 193 | public static L2ModificationInstruction decMplsTtl() { |
alshabib | d17abc2 | 2015-04-21 18:26:35 -0700 | [diff] [blame] | 194 | return new L2ModificationInstruction.ModMplsTtlInstruction(); |
sangho | 3f97a17d | 2015-01-29 22:56:29 -0800 | [diff] [blame] | 195 | } |
Pavlin Radoslavov | febe82c | 2015-02-11 19:08:15 -0800 | [diff] [blame] | 196 | |
Praseed Balakrishnan | 8c67d17 | 2014-11-10 10:15:41 -0800 | [diff] [blame] | 197 | /** |
Pavlin Radoslavov | febe82c | 2015-02-11 19:08:15 -0800 | [diff] [blame] | 198 | * Creates a L3 IPv4 src modification. |
| 199 | * |
| 200 | * @param addr the IPv4 address to modify to |
alshabib | 7410fea | 2014-09-16 13:48:39 -0700 | [diff] [blame] | 201 | * @return a L3 modification |
| 202 | */ |
Pavlin Radoslavov | 855ea2d | 2014-10-30 15:32:39 -0700 | [diff] [blame] | 203 | public static L3ModificationInstruction modL3Src(IpAddress addr) { |
Pavlin Radoslavov | febe82c | 2015-02-11 19:08:15 -0800 | [diff] [blame] | 204 | checkNotNull(addr, "Src l3 IPv4 address cannot be null"); |
| 205 | return new ModIPInstruction(L3SubType.IPV4_SRC, addr); |
alshabib | 7410fea | 2014-09-16 13:48:39 -0700 | [diff] [blame] | 206 | } |
| 207 | |
| 208 | /** |
Pavlin Radoslavov | febe82c | 2015-02-11 19:08:15 -0800 | [diff] [blame] | 209 | * Creates a L3 IPv4 dst modification. |
| 210 | * |
| 211 | * @param addr the IPv4 address to modify to |
alshabib | 7410fea | 2014-09-16 13:48:39 -0700 | [diff] [blame] | 212 | * @return a L3 modification |
| 213 | */ |
Pavlin Radoslavov | 855ea2d | 2014-10-30 15:32:39 -0700 | [diff] [blame] | 214 | public static L3ModificationInstruction modL3Dst(IpAddress addr) { |
Pavlin Radoslavov | febe82c | 2015-02-11 19:08:15 -0800 | [diff] [blame] | 215 | checkNotNull(addr, "Dst l3 IPv4 address cannot be null"); |
| 216 | return new ModIPInstruction(L3SubType.IPV4_DST, addr); |
| 217 | } |
| 218 | |
| 219 | /** |
| 220 | * Creates a L3 IPv6 src modification. |
| 221 | * |
| 222 | * @param addr the IPv6 address to modify to |
| 223 | * @return a L3 modification |
| 224 | */ |
| 225 | public static L3ModificationInstruction modL3IPv6Src(IpAddress addr) { |
| 226 | checkNotNull(addr, "Src l3 IPv6 address cannot be null"); |
| 227 | return new ModIPInstruction(L3SubType.IPV6_SRC, addr); |
| 228 | } |
| 229 | |
| 230 | /** |
| 231 | * Creates a L3 IPv6 dst modification. |
| 232 | * |
| 233 | * @param addr the IPv6 address to modify to |
| 234 | * @return a L3 modification |
| 235 | */ |
| 236 | public static L3ModificationInstruction modL3IPv6Dst(IpAddress addr) { |
| 237 | checkNotNull(addr, "Dst l3 IPv6 address cannot be null"); |
| 238 | return new ModIPInstruction(L3SubType.IPV6_DST, addr); |
| 239 | } |
| 240 | |
| 241 | /** |
| 242 | * Creates a L3 IPv6 Flow Label modification. |
| 243 | * |
| 244 | * @param flowLabel the IPv6 flow label to modify to (20 bits) |
| 245 | * @return a L3 modification |
| 246 | */ |
| 247 | public static L3ModificationInstruction modL3IPv6FlowLabel(int flowLabel) { |
| 248 | return new ModIPv6FlowLabelInstruction(flowLabel); |
alshabib | 7410fea | 2014-09-16 13:48:39 -0700 | [diff] [blame] | 249 | } |
| 250 | |
Praseed Balakrishnan | 8c67d17 | 2014-11-10 10:15:41 -0800 | [diff] [blame] | 251 | /** |
Jonathan Hart | 54b406b | 2015-03-06 16:24:14 -0800 | [diff] [blame] | 252 | * Creates a L3 decrement TTL modification. |
| 253 | * |
sangho | 3f97a17d | 2015-01-29 22:56:29 -0800 | [diff] [blame] | 254 | * @return a L3 modification |
| 255 | */ |
| 256 | public static L3ModificationInstruction decNwTtl() { |
| 257 | return new ModTtlInstruction(L3SubType.DEC_TTL); |
| 258 | } |
| 259 | |
| 260 | /** |
Jonathan Hart | 54b406b | 2015-03-06 16:24:14 -0800 | [diff] [blame] | 261 | * Creates a L3 copy TTL to outer header modification. |
| 262 | * |
sangho | 3f97a17d | 2015-01-29 22:56:29 -0800 | [diff] [blame] | 263 | * @return a L3 modification |
| 264 | */ |
| 265 | public static L3ModificationInstruction copyTtlOut() { |
| 266 | return new ModTtlInstruction(L3SubType.TTL_OUT); |
| 267 | } |
| 268 | |
| 269 | /** |
Jonathan Hart | 54b406b | 2015-03-06 16:24:14 -0800 | [diff] [blame] | 270 | * Creates a L3 copy TTL to inner header modification. |
| 271 | * |
sangho | 3f97a17d | 2015-01-29 22:56:29 -0800 | [diff] [blame] | 272 | * @return a L3 modification |
| 273 | */ |
| 274 | public static L3ModificationInstruction copyTtlIn() { |
| 275 | return new ModTtlInstruction(L3SubType.TTL_IN); |
| 276 | } |
| 277 | |
| 278 | /** |
Jonathan Hart | 54b406b | 2015-03-06 16:24:14 -0800 | [diff] [blame] | 279 | * Creates a push MPLS header instruction. |
| 280 | * |
Praseed Balakrishnan | 8c67d17 | 2014-11-10 10:15:41 -0800 | [diff] [blame] | 281 | * @return a L2 modification. |
| 282 | */ |
| 283 | public static Instruction pushMpls() { |
alshabib | d17abc2 | 2015-04-21 18:26:35 -0700 | [diff] [blame] | 284 | return new L2ModificationInstruction.PushHeaderInstructions( |
| 285 | L2ModificationInstruction.L2SubType.MPLS_PUSH, |
alshabib | 7b808c5 | 2015-06-26 14:22:24 -0700 | [diff] [blame] | 286 | EthType.EtherType.MPLS_UNICAST.ethType()); |
Praseed Balakrishnan | 8c67d17 | 2014-11-10 10:15:41 -0800 | [diff] [blame] | 287 | } |
| 288 | |
| 289 | /** |
Jonathan Hart | 54b406b | 2015-03-06 16:24:14 -0800 | [diff] [blame] | 290 | * Creates a pop MPLS header instruction. |
| 291 | * |
Praseed Balakrishnan | 8c67d17 | 2014-11-10 10:15:41 -0800 | [diff] [blame] | 292 | * @return a L2 modification. |
| 293 | */ |
| 294 | public static Instruction popMpls() { |
alshabib | d17abc2 | 2015-04-21 18:26:35 -0700 | [diff] [blame] | 295 | return new L2ModificationInstruction.PushHeaderInstructions( |
| 296 | L2ModificationInstruction.L2SubType.MPLS_POP, |
alshabib | 7b808c5 | 2015-06-26 14:22:24 -0700 | [diff] [blame] | 297 | EthType.EtherType.MPLS_UNICAST.ethType()); |
Praseed Balakrishnan | 8c67d17 | 2014-11-10 10:15:41 -0800 | [diff] [blame] | 298 | } |
alshabib | 7410fea | 2014-09-16 13:48:39 -0700 | [diff] [blame] | 299 | |
sangho | 3f97a17d | 2015-01-29 22:56:29 -0800 | [diff] [blame] | 300 | /** |
Jonathan Hart | 54b406b | 2015-03-06 16:24:14 -0800 | [diff] [blame] | 301 | * Creates a pop MPLS header instruction with a particular ethertype. |
sangho | 3f97a17d | 2015-01-29 22:56:29 -0800 | [diff] [blame] | 302 | * |
| 303 | * @param etherType Ethernet type to set |
| 304 | * @return a L2 modification. |
Sho SHIMIZU | be63b23 | 2015-06-30 10:57:58 -0700 | [diff] [blame] | 305 | * @deprecated in Cardinal Release |
sangho | 3f97a17d | 2015-01-29 22:56:29 -0800 | [diff] [blame] | 306 | */ |
alshabib | 7b808c5 | 2015-06-26 14:22:24 -0700 | [diff] [blame] | 307 | @Deprecated |
alshabib | 0ad4398 | 2015-05-07 13:43:13 -0700 | [diff] [blame] | 308 | public static Instruction popMpls(int etherType) { |
sangho | 3f97a17d | 2015-01-29 22:56:29 -0800 | [diff] [blame] | 309 | checkNotNull(etherType, "Ethernet type cannot be null"); |
alshabib | d17abc2 | 2015-04-21 18:26:35 -0700 | [diff] [blame] | 310 | return new L2ModificationInstruction.PushHeaderInstructions( |
alshabib | 7b808c5 | 2015-06-26 14:22:24 -0700 | [diff] [blame] | 311 | L2ModificationInstruction.L2SubType.MPLS_POP, new EthType(etherType)); |
| 312 | } |
| 313 | |
| 314 | |
| 315 | /** |
| 316 | * Creates a pop MPLS header instruction with a particular ethertype. |
| 317 | * |
| 318 | * @param etherType Ethernet type to set |
| 319 | * @return a L2 modification. |
| 320 | */ |
| 321 | public static Instruction popMpls(EthType etherType) { |
| 322 | checkNotNull(etherType, "Ethernet type cannot be null"); |
| 323 | return new L2ModificationInstruction.PushHeaderInstructions( |
alshabib | d17abc2 | 2015-04-21 18:26:35 -0700 | [diff] [blame] | 324 | L2ModificationInstruction.L2SubType.MPLS_POP, etherType); |
sangho | 3f97a17d | 2015-01-29 22:56:29 -0800 | [diff] [blame] | 325 | } |
| 326 | |
Saurav Das | fbe25c5 | 2015-03-04 11:12:00 -0800 | [diff] [blame] | 327 | /** |
Jonathan Hart | 54b406b | 2015-03-06 16:24:14 -0800 | [diff] [blame] | 328 | * Creates a pop VLAN header instruction. |
| 329 | * |
| 330 | * @return a L2 modification |
Saurav Das | fbe25c5 | 2015-03-04 11:12:00 -0800 | [diff] [blame] | 331 | */ |
| 332 | public static Instruction popVlan() { |
alshabib | d17abc2 | 2015-04-21 18:26:35 -0700 | [diff] [blame] | 333 | return new L2ModificationInstruction.PopVlanInstruction( |
| 334 | L2ModificationInstruction.L2SubType.VLAN_POP); |
Saurav Das | fbe25c5 | 2015-03-04 11:12:00 -0800 | [diff] [blame] | 335 | } |
| 336 | |
| 337 | /** |
Jonathan Hart | 54b406b | 2015-03-06 16:24:14 -0800 | [diff] [blame] | 338 | * Creates a push VLAN header instruction. |
| 339 | * |
| 340 | * @return a L2 modification |
| 341 | */ |
| 342 | public static Instruction pushVlan() { |
alshabib | d17abc2 | 2015-04-21 18:26:35 -0700 | [diff] [blame] | 343 | return new L2ModificationInstruction.PushHeaderInstructions( |
alshabib | 7b808c5 | 2015-06-26 14:22:24 -0700 | [diff] [blame] | 344 | L2ModificationInstruction.L2SubType.VLAN_PUSH, |
| 345 | EthType.EtherType.VLAN.ethType()); |
Jonathan Hart | 54b406b | 2015-03-06 16:24:14 -0800 | [diff] [blame] | 346 | } |
| 347 | |
| 348 | /** |
alshabib | d17abc2 | 2015-04-21 18:26:35 -0700 | [diff] [blame] | 349 | * Sends the packet to the table id. |
Jonathan Hart | 54b406b | 2015-03-06 16:24:14 -0800 | [diff] [blame] | 350 | * |
alshabib | d17abc2 | 2015-04-21 18:26:35 -0700 | [diff] [blame] | 351 | * @param tableId flow rule table id |
Thomas Vachuska | 3e2b651 | 2015-03-05 09:25:03 -0800 | [diff] [blame] | 352 | * @return table type transition instruction |
Saurav Das | fbe25c5 | 2015-03-04 11:12:00 -0800 | [diff] [blame] | 353 | */ |
alshabib | d17abc2 | 2015-04-21 18:26:35 -0700 | [diff] [blame] | 354 | public static Instruction transition(Integer tableId) { |
| 355 | checkNotNull(tableId, "Table id cannot be null"); |
| 356 | return new TableTypeTransition(tableId); |
alshabib | 9af7007 | 2015-02-09 14:34:16 -0800 | [diff] [blame] | 357 | } |
| 358 | |
Yuta HIGUCHI | 6a47964 | 2015-02-08 01:28:50 -0800 | [diff] [blame] | 359 | /** |
Saurav Das | 86af8f1 | 2015-05-25 23:55:33 -0700 | [diff] [blame] | 360 | * Writes metadata to associate with a packet. |
| 361 | * |
| 362 | * @param metadata the metadata value to write |
| 363 | * @param metadataMask the bits to mask for the metadata value |
| 364 | * @return metadata instruction |
| 365 | */ |
| 366 | public static Instruction writeMetadata(long metadata, long metadataMask) { |
| 367 | return new MetadataInstruction(metadata, metadataMask); |
| 368 | } |
| 369 | |
| 370 | /** |
Hyunsun Moon | a08c5d0 | 2015-07-14 17:53:00 -0700 | [diff] [blame] | 371 | * Creates a Tunnel ID modification. |
| 372 | * |
| 373 | * @param tunnelId the Tunnel ID to modify to |
| 374 | * @return a L2 modification |
| 375 | */ |
| 376 | public static L2ModificationInstruction modTunnelId(long tunnelId) { |
| 377 | checkNotNull(tunnelId, "Tunnel id cannot be null"); |
| 378 | return new L2ModificationInstruction.ModTunnelIdInstruction(tunnelId); |
| 379 | } |
| 380 | |
| 381 | /** |
Hyunsun Moon | c8bd97c | 2015-07-18 22:47:33 -0700 | [diff] [blame] | 382 | * Creates a TCP src modification. |
| 383 | * |
| 384 | * @param port the TCP port number to modify to |
| 385 | * @return a L4 modification |
| 386 | */ |
| 387 | public static L4ModificationInstruction modTcpSrc(short port) { |
| 388 | checkNotNull(port, "Src TCP port cannot be null"); |
| 389 | return new ModTransportPortInstruction(L4SubType.TCP_SRC, port); |
| 390 | } |
| 391 | |
| 392 | /** |
| 393 | * Creates a TCP dst modification. |
| 394 | * |
| 395 | * @param port the TCP port number to modify to |
| 396 | * @return a L4 modification |
| 397 | */ |
| 398 | public static L4ModificationInstruction modTcpDst(short port) { |
| 399 | checkNotNull(port, "Dst TCP port cannot be null"); |
| 400 | return new ModTransportPortInstruction(L4SubType.TCP_DST, port); |
| 401 | } |
| 402 | |
| 403 | /** |
| 404 | * Creates a UDP src modification. |
| 405 | * |
| 406 | * @param port the UDP port number to modify to |
| 407 | * @return a L4 modification |
| 408 | */ |
| 409 | public static L4ModificationInstruction modUdpSrc(short port) { |
| 410 | checkNotNull(port, "Src UDP port cannot be null"); |
| 411 | return new ModTransportPortInstruction(L4SubType.UDP_SRC, port); |
| 412 | } |
| 413 | |
| 414 | /** |
| 415 | * Creates a UDP dst modification. |
| 416 | * |
| 417 | * @param port the UDP port number to modify to |
| 418 | * @return a L4 modification |
| 419 | */ |
| 420 | public static L4ModificationInstruction modUdpDst(short port) { |
| 421 | checkNotNull(port, "Dst UDP port cannot be null"); |
| 422 | return new ModTransportPortInstruction(L4SubType.UDP_DST, port); |
| 423 | } |
| 424 | |
| 425 | /** |
Yuta HIGUCHI | 6a47964 | 2015-02-08 01:28:50 -0800 | [diff] [blame] | 426 | * Drop instruction. |
alshabib | 55a55d9 | 2014-09-16 11:59:31 -0700 | [diff] [blame] | 427 | */ |
alshabib | 55a55d9 | 2014-09-16 11:59:31 -0700 | [diff] [blame] | 428 | public static final class DropInstruction implements Instruction { |
Yuta HIGUCHI | 6a47964 | 2015-02-08 01:28:50 -0800 | [diff] [blame] | 429 | |
| 430 | private DropInstruction() {} |
| 431 | |
alshabib | 55a55d9 | 2014-09-16 11:59:31 -0700 | [diff] [blame] | 432 | @Override |
| 433 | public Type type() { |
| 434 | return Type.DROP; |
| 435 | } |
alshabib | 99b8fdc | 2014-09-25 14:30:22 -0700 | [diff] [blame] | 436 | |
| 437 | @Override |
| 438 | public String toString() { |
alshabib | 346b5b3 | 2015-03-06 00:42:16 -0800 | [diff] [blame] | 439 | return toStringHelper(type().toString()).toString(); |
alshabib | 99b8fdc | 2014-09-25 14:30:22 -0700 | [diff] [blame] | 440 | } |
alshabib | 8ca5390 | 2014-10-07 13:11:17 -0700 | [diff] [blame] | 441 | |
| 442 | @Override |
| 443 | public int hashCode() { |
Thomas Vachuska | 6c8eff3 | 2015-05-27 16:11:44 -0700 | [diff] [blame] | 444 | return Objects.hash(type().ordinal()); |
alshabib | 8ca5390 | 2014-10-07 13:11:17 -0700 | [diff] [blame] | 445 | } |
| 446 | |
| 447 | @Override |
| 448 | public boolean equals(Object obj) { |
| 449 | if (this == obj) { |
| 450 | return true; |
| 451 | } |
| 452 | if (obj instanceof DropInstruction) { |
Yuta HIGUCHI | 6a47964 | 2015-02-08 01:28:50 -0800 | [diff] [blame] | 453 | return true; |
alshabib | 8ca5390 | 2014-10-07 13:11:17 -0700 | [diff] [blame] | 454 | } |
| 455 | return false; |
| 456 | } |
alshabib | 55a55d9 | 2014-09-16 11:59:31 -0700 | [diff] [blame] | 457 | } |
| 458 | |
Yuta HIGUCHI | 6a47964 | 2015-02-08 01:28:50 -0800 | [diff] [blame] | 459 | /** |
| 460 | * Output Instruction. |
sangho | 8995ac5 | 2015-02-04 11:29:03 -0800 | [diff] [blame] | 461 | */ |
alshabib | 55a55d9 | 2014-09-16 11:59:31 -0700 | [diff] [blame] | 462 | public static final class OutputInstruction implements Instruction { |
| 463 | private final PortNumber port; |
| 464 | |
| 465 | private OutputInstruction(PortNumber port) { |
| 466 | this.port = port; |
| 467 | } |
| 468 | |
| 469 | public PortNumber port() { |
| 470 | return port; |
| 471 | } |
| 472 | |
| 473 | @Override |
| 474 | public Type type() { |
| 475 | return Type.OUTPUT; |
| 476 | } |
alshabib | 99b8fdc | 2014-09-25 14:30:22 -0700 | [diff] [blame] | 477 | @Override |
| 478 | public String toString() { |
| 479 | return toStringHelper(type().toString()) |
| 480 | .add("port", port).toString(); |
| 481 | } |
alshabib | 8ca5390 | 2014-10-07 13:11:17 -0700 | [diff] [blame] | 482 | |
| 483 | @Override |
| 484 | public int hashCode() { |
Thomas Vachuska | 6c8eff3 | 2015-05-27 16:11:44 -0700 | [diff] [blame] | 485 | return Objects.hash(type().ordinal(), port); |
alshabib | 8ca5390 | 2014-10-07 13:11:17 -0700 | [diff] [blame] | 486 | } |
| 487 | |
| 488 | @Override |
| 489 | public boolean equals(Object obj) { |
| 490 | if (this == obj) { |
| 491 | return true; |
| 492 | } |
| 493 | if (obj instanceof OutputInstruction) { |
| 494 | OutputInstruction that = (OutputInstruction) obj; |
Yuta HIGUCHI | 4ce6529 | 2014-11-01 12:09:55 -0700 | [diff] [blame] | 495 | return Objects.equals(port, that.port); |
alshabib | 8ca5390 | 2014-10-07 13:11:17 -0700 | [diff] [blame] | 496 | |
| 497 | } |
| 498 | return false; |
| 499 | } |
alshabib | 55a55d9 | 2014-09-16 11:59:31 -0700 | [diff] [blame] | 500 | } |
| 501 | |
Yuta HIGUCHI | 6a47964 | 2015-02-08 01:28:50 -0800 | [diff] [blame] | 502 | /** |
| 503 | * Group Instruction. |
sangho | 8995ac5 | 2015-02-04 11:29:03 -0800 | [diff] [blame] | 504 | */ |
sangho | 8995ac5 | 2015-02-04 11:29:03 -0800 | [diff] [blame] | 505 | public static final class GroupInstruction implements Instruction { |
| 506 | private final GroupId groupId; |
| 507 | |
| 508 | private GroupInstruction(GroupId groupId) { |
| 509 | this.groupId = groupId; |
| 510 | } |
| 511 | |
| 512 | public GroupId groupId() { |
| 513 | return groupId; |
| 514 | } |
| 515 | |
| 516 | @Override |
| 517 | public Type type() { |
| 518 | return Type.GROUP; |
| 519 | } |
alshabib | 9af7007 | 2015-02-09 14:34:16 -0800 | [diff] [blame] | 520 | |
sangho | 8995ac5 | 2015-02-04 11:29:03 -0800 | [diff] [blame] | 521 | @Override |
| 522 | public String toString() { |
| 523 | return toStringHelper(type().toString()) |
| 524 | .add("group ID", groupId.id()).toString(); |
| 525 | } |
| 526 | |
| 527 | @Override |
| 528 | public int hashCode() { |
Thomas Vachuska | 6c8eff3 | 2015-05-27 16:11:44 -0700 | [diff] [blame] | 529 | return Objects.hash(type().ordinal(), groupId); |
sangho | 8995ac5 | 2015-02-04 11:29:03 -0800 | [diff] [blame] | 530 | } |
| 531 | |
| 532 | @Override |
| 533 | public boolean equals(Object obj) { |
| 534 | if (this == obj) { |
| 535 | return true; |
| 536 | } |
| 537 | if (obj instanceof GroupInstruction) { |
| 538 | GroupInstruction that = (GroupInstruction) obj; |
| 539 | return Objects.equals(groupId, that.groupId); |
| 540 | |
| 541 | } |
| 542 | return false; |
| 543 | } |
| 544 | } |
| 545 | |
Saurav Das | 86af8f1 | 2015-05-25 23:55:33 -0700 | [diff] [blame] | 546 | /** |
alshabib | 10c810b | 2015-08-18 16:59:04 -0700 | [diff] [blame] | 547 | * A meter instruction. |
| 548 | */ |
| 549 | public static final class MeterInstruction implements Instruction { |
| 550 | private final MeterId meterId; |
| 551 | |
| 552 | private MeterInstruction(MeterId meterId) { |
| 553 | this.meterId = meterId; |
| 554 | } |
| 555 | |
| 556 | public MeterId meterId() { |
| 557 | return meterId; |
| 558 | } |
| 559 | |
| 560 | @Override |
| 561 | public Type type() { |
| 562 | return Type.METER; |
| 563 | } |
| 564 | |
| 565 | @Override |
| 566 | public String toString() { |
| 567 | return toStringHelper(type().toString()) |
| 568 | .add("meter ID", meterId.id()).toString(); |
| 569 | } |
| 570 | |
| 571 | @Override |
| 572 | public int hashCode() { |
| 573 | return Objects.hash(type().ordinal(), meterId); |
| 574 | } |
| 575 | |
| 576 | @Override |
| 577 | public boolean equals(Object obj) { |
| 578 | if (this == obj) { |
| 579 | return true; |
| 580 | } |
| 581 | if (obj instanceof MeterInstruction) { |
| 582 | MeterInstruction that = (MeterInstruction) obj; |
| 583 | return Objects.equals(meterId, that.meterId); |
| 584 | |
| 585 | } |
| 586 | return false; |
| 587 | } |
| 588 | } |
| 589 | |
| 590 | /** |
Saurav Das | 86af8f1 | 2015-05-25 23:55:33 -0700 | [diff] [blame] | 591 | * Transition instruction. |
| 592 | */ |
alshabib | d17abc2 | 2015-04-21 18:26:35 -0700 | [diff] [blame] | 593 | public static class TableTypeTransition implements Instruction { |
| 594 | private final Integer tableId; |
| 595 | |
| 596 | TableTypeTransition(Integer tableId) { |
| 597 | this.tableId = tableId; |
alshabib | 9af7007 | 2015-02-09 14:34:16 -0800 | [diff] [blame] | 598 | } |
| 599 | |
| 600 | @Override |
| 601 | public Type type() { |
| 602 | return Type.TABLE; |
| 603 | } |
| 604 | |
alshabib | d17abc2 | 2015-04-21 18:26:35 -0700 | [diff] [blame] | 605 | public Integer tableId() { |
| 606 | return this.tableId; |
alshabib | 9af7007 | 2015-02-09 14:34:16 -0800 | [diff] [blame] | 607 | } |
| 608 | |
| 609 | @Override |
| 610 | public String toString() { |
| 611 | return toStringHelper(type().toString()) |
alshabib | d17abc2 | 2015-04-21 18:26:35 -0700 | [diff] [blame] | 612 | .add("tableId", this.tableId).toString(); |
alshabib | 9af7007 | 2015-02-09 14:34:16 -0800 | [diff] [blame] | 613 | } |
| 614 | |
| 615 | @Override |
| 616 | public int hashCode() { |
Thomas Vachuska | 6c8eff3 | 2015-05-27 16:11:44 -0700 | [diff] [blame] | 617 | return Objects.hash(type().ordinal(), tableId); |
alshabib | 9af7007 | 2015-02-09 14:34:16 -0800 | [diff] [blame] | 618 | } |
| 619 | |
| 620 | @Override |
| 621 | public boolean equals(Object obj) { |
| 622 | if (this == obj) { |
| 623 | return true; |
| 624 | } |
| 625 | if (obj instanceof TableTypeTransition) { |
| 626 | TableTypeTransition that = (TableTypeTransition) obj; |
alshabib | d17abc2 | 2015-04-21 18:26:35 -0700 | [diff] [blame] | 627 | return Objects.equals(tableId, that.tableId); |
alshabib | 9af7007 | 2015-02-09 14:34:16 -0800 | [diff] [blame] | 628 | |
| 629 | } |
| 630 | return false; |
| 631 | } |
Saurav Das | 86af8f1 | 2015-05-25 23:55:33 -0700 | [diff] [blame] | 632 | } |
alshabib | 9af7007 | 2015-02-09 14:34:16 -0800 | [diff] [blame] | 633 | |
Saurav Das | 86af8f1 | 2015-05-25 23:55:33 -0700 | [diff] [blame] | 634 | /** |
| 635 | * Metadata instruction. |
| 636 | */ |
| 637 | public static class MetadataInstruction implements Instruction { |
| 638 | private final long metadata; |
| 639 | private final long metadataMask; |
| 640 | |
| 641 | MetadataInstruction(long metadata, long metadataMask) { |
| 642 | this.metadata = metadata; |
| 643 | this.metadataMask = metadataMask; |
| 644 | } |
| 645 | |
| 646 | @Override |
| 647 | public Type type() { |
| 648 | return Type.METADATA; |
| 649 | } |
| 650 | |
| 651 | public long metadata() { |
| 652 | return this.metadata; |
| 653 | } |
| 654 | |
| 655 | public long metadataMask() { |
| 656 | return this.metadataMask; |
| 657 | } |
| 658 | |
| 659 | @Override |
| 660 | public String toString() { |
| 661 | return toStringHelper(type().toString()) |
| 662 | .add("metadata", Long.toHexString(this.metadata)) |
| 663 | .add("metadata mask", Long.toHexString(this.metadataMask)) |
| 664 | .toString(); |
| 665 | } |
| 666 | |
| 667 | @Override |
| 668 | public int hashCode() { |
Thomas Vachuska | 6c8eff3 | 2015-05-27 16:11:44 -0700 | [diff] [blame] | 669 | return Objects.hash(type().ordinal(), metadata, metadataMask); |
Saurav Das | 86af8f1 | 2015-05-25 23:55:33 -0700 | [diff] [blame] | 670 | } |
| 671 | |
| 672 | @Override |
| 673 | public boolean equals(Object obj) { |
| 674 | if (this == obj) { |
| 675 | return true; |
| 676 | } |
| 677 | if (obj instanceof MetadataInstruction) { |
| 678 | MetadataInstruction that = (MetadataInstruction) obj; |
| 679 | return Objects.equals(metadata, that.metadata) && |
| 680 | Objects.equals(metadataMask, that.metadataMask); |
| 681 | |
| 682 | } |
| 683 | return false; |
| 684 | } |
alshabib | 9af7007 | 2015-02-09 14:34:16 -0800 | [diff] [blame] | 685 | } |
Saurav Das | 73a7dd4 | 2015-08-19 22:20:31 -0700 | [diff] [blame] | 686 | |
alshabib | 55a55d9 | 2014-09-16 11:59:31 -0700 | [diff] [blame] | 687 | } |
alshabib | 8ca5390 | 2014-10-07 13:11:17 -0700 | [diff] [blame] | 688 | |
| 689 | |