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