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 | |
Jonathan Hart | 54b406b | 2015-03-06 16:24:14 -0800 | [diff] [blame] | 18 | import org.onlab.packet.Ethernet; |
| 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, |
Yuta HIGUCHI | 32a53c5 | 2015-02-08 01:25:40 -0800 | [diff] [blame] | 268 | Ethernet.MPLS_UNICAST); |
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, |
Yuta HIGUCHI | 32a53c5 | 2015-02-08 01:25:40 -0800 | [diff] [blame] | 279 | Ethernet.MPLS_UNICAST); |
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. |
| 287 | */ |
alshabib | 0ad4398 | 2015-05-07 13:43:13 -0700 | [diff] [blame] | 288 | public static Instruction popMpls(int etherType) { |
sangho | 3f97a17d | 2015-01-29 22:56:29 -0800 | [diff] [blame] | 289 | checkNotNull(etherType, "Ethernet type cannot be null"); |
alshabib | d17abc2 | 2015-04-21 18:26:35 -0700 | [diff] [blame] | 290 | return new L2ModificationInstruction.PushHeaderInstructions( |
| 291 | L2ModificationInstruction.L2SubType.MPLS_POP, etherType); |
sangho | 3f97a17d | 2015-01-29 22:56:29 -0800 | [diff] [blame] | 292 | } |
| 293 | |
Saurav Das | fbe25c5 | 2015-03-04 11:12:00 -0800 | [diff] [blame] | 294 | /** |
Jonathan Hart | 54b406b | 2015-03-06 16:24:14 -0800 | [diff] [blame] | 295 | * Creates a pop VLAN header instruction. |
| 296 | * |
| 297 | * @return a L2 modification |
Saurav Das | fbe25c5 | 2015-03-04 11:12:00 -0800 | [diff] [blame] | 298 | */ |
| 299 | public static Instruction popVlan() { |
alshabib | d17abc2 | 2015-04-21 18:26:35 -0700 | [diff] [blame] | 300 | return new L2ModificationInstruction.PopVlanInstruction( |
| 301 | L2ModificationInstruction.L2SubType.VLAN_POP); |
Saurav Das | fbe25c5 | 2015-03-04 11:12:00 -0800 | [diff] [blame] | 302 | } |
| 303 | |
| 304 | /** |
Jonathan Hart | 54b406b | 2015-03-06 16:24:14 -0800 | [diff] [blame] | 305 | * Creates a push VLAN header instruction. |
| 306 | * |
| 307 | * @return a L2 modification |
| 308 | */ |
| 309 | public static Instruction pushVlan() { |
alshabib | d17abc2 | 2015-04-21 18:26:35 -0700 | [diff] [blame] | 310 | return new L2ModificationInstruction.PushHeaderInstructions( |
| 311 | L2ModificationInstruction.L2SubType.VLAN_PUSH, Ethernet.TYPE_VLAN); |
Jonathan Hart | 54b406b | 2015-03-06 16:24:14 -0800 | [diff] [blame] | 312 | } |
| 313 | |
| 314 | /** |
alshabib | d17abc2 | 2015-04-21 18:26:35 -0700 | [diff] [blame] | 315 | * Sends the packet to the table id. |
Jonathan Hart | 54b406b | 2015-03-06 16:24:14 -0800 | [diff] [blame] | 316 | * |
alshabib | d17abc2 | 2015-04-21 18:26:35 -0700 | [diff] [blame] | 317 | * @param tableId flow rule table id |
Thomas Vachuska | 3e2b651 | 2015-03-05 09:25:03 -0800 | [diff] [blame] | 318 | * @return table type transition instruction |
Saurav Das | fbe25c5 | 2015-03-04 11:12:00 -0800 | [diff] [blame] | 319 | */ |
alshabib | d17abc2 | 2015-04-21 18:26:35 -0700 | [diff] [blame] | 320 | public static Instruction transition(Integer tableId) { |
| 321 | checkNotNull(tableId, "Table id cannot be null"); |
| 322 | return new TableTypeTransition(tableId); |
alshabib | 9af7007 | 2015-02-09 14:34:16 -0800 | [diff] [blame] | 323 | } |
| 324 | |
Yuta HIGUCHI | 6a47964 | 2015-02-08 01:28:50 -0800 | [diff] [blame] | 325 | /** |
Saurav Das | 86af8f1 | 2015-05-25 23:55:33 -0700 | [diff] [blame] | 326 | * Writes metadata to associate with a packet. |
| 327 | * |
| 328 | * @param metadata the metadata value to write |
| 329 | * @param metadataMask the bits to mask for the metadata value |
| 330 | * @return metadata instruction |
| 331 | */ |
| 332 | public static Instruction writeMetadata(long metadata, long metadataMask) { |
| 333 | return new MetadataInstruction(metadata, metadataMask); |
| 334 | } |
| 335 | |
| 336 | /** |
Yuta HIGUCHI | 6a47964 | 2015-02-08 01:28:50 -0800 | [diff] [blame] | 337 | * Drop instruction. |
alshabib | 55a55d9 | 2014-09-16 11:59:31 -0700 | [diff] [blame] | 338 | */ |
alshabib | 55a55d9 | 2014-09-16 11:59:31 -0700 | [diff] [blame] | 339 | public static final class DropInstruction implements Instruction { |
Yuta HIGUCHI | 6a47964 | 2015-02-08 01:28:50 -0800 | [diff] [blame] | 340 | |
| 341 | private DropInstruction() {} |
| 342 | |
alshabib | 55a55d9 | 2014-09-16 11:59:31 -0700 | [diff] [blame] | 343 | @Override |
| 344 | public Type type() { |
| 345 | return Type.DROP; |
| 346 | } |
alshabib | 99b8fdc | 2014-09-25 14:30:22 -0700 | [diff] [blame] | 347 | |
| 348 | @Override |
| 349 | public String toString() { |
alshabib | 346b5b3 | 2015-03-06 00:42:16 -0800 | [diff] [blame] | 350 | return toStringHelper(type().toString()).toString(); |
alshabib | 99b8fdc | 2014-09-25 14:30:22 -0700 | [diff] [blame] | 351 | } |
alshabib | 8ca5390 | 2014-10-07 13:11:17 -0700 | [diff] [blame] | 352 | |
| 353 | @Override |
| 354 | public int hashCode() { |
Thomas Vachuska | 6c8eff3 | 2015-05-27 16:11:44 -0700 | [diff] [blame] | 355 | return Objects.hash(type().ordinal()); |
alshabib | 8ca5390 | 2014-10-07 13:11:17 -0700 | [diff] [blame] | 356 | } |
| 357 | |
| 358 | @Override |
| 359 | public boolean equals(Object obj) { |
| 360 | if (this == obj) { |
| 361 | return true; |
| 362 | } |
| 363 | if (obj instanceof DropInstruction) { |
Yuta HIGUCHI | 6a47964 | 2015-02-08 01:28:50 -0800 | [diff] [blame] | 364 | return true; |
alshabib | 8ca5390 | 2014-10-07 13:11:17 -0700 | [diff] [blame] | 365 | } |
| 366 | return false; |
| 367 | } |
alshabib | 55a55d9 | 2014-09-16 11:59:31 -0700 | [diff] [blame] | 368 | } |
| 369 | |
Yuta HIGUCHI | 6a47964 | 2015-02-08 01:28:50 -0800 | [diff] [blame] | 370 | /** |
| 371 | * Output Instruction. |
sangho | 8995ac5 | 2015-02-04 11:29:03 -0800 | [diff] [blame] | 372 | */ |
alshabib | 55a55d9 | 2014-09-16 11:59:31 -0700 | [diff] [blame] | 373 | public static final class OutputInstruction implements Instruction { |
| 374 | private final PortNumber port; |
| 375 | |
| 376 | private OutputInstruction(PortNumber port) { |
| 377 | this.port = port; |
| 378 | } |
| 379 | |
| 380 | public PortNumber port() { |
| 381 | return port; |
| 382 | } |
| 383 | |
| 384 | @Override |
| 385 | public Type type() { |
| 386 | return Type.OUTPUT; |
| 387 | } |
alshabib | 99b8fdc | 2014-09-25 14:30:22 -0700 | [diff] [blame] | 388 | @Override |
| 389 | public String toString() { |
| 390 | return toStringHelper(type().toString()) |
| 391 | .add("port", port).toString(); |
| 392 | } |
alshabib | 8ca5390 | 2014-10-07 13:11:17 -0700 | [diff] [blame] | 393 | |
| 394 | @Override |
| 395 | public int hashCode() { |
Thomas Vachuska | 6c8eff3 | 2015-05-27 16:11:44 -0700 | [diff] [blame] | 396 | return Objects.hash(type().ordinal(), port); |
alshabib | 8ca5390 | 2014-10-07 13:11:17 -0700 | [diff] [blame] | 397 | } |
| 398 | |
| 399 | @Override |
| 400 | public boolean equals(Object obj) { |
| 401 | if (this == obj) { |
| 402 | return true; |
| 403 | } |
| 404 | if (obj instanceof OutputInstruction) { |
| 405 | OutputInstruction that = (OutputInstruction) obj; |
Yuta HIGUCHI | 4ce6529 | 2014-11-01 12:09:55 -0700 | [diff] [blame] | 406 | return Objects.equals(port, that.port); |
alshabib | 8ca5390 | 2014-10-07 13:11:17 -0700 | [diff] [blame] | 407 | |
| 408 | } |
| 409 | return false; |
| 410 | } |
alshabib | 55a55d9 | 2014-09-16 11:59:31 -0700 | [diff] [blame] | 411 | } |
| 412 | |
Yuta HIGUCHI | 6a47964 | 2015-02-08 01:28:50 -0800 | [diff] [blame] | 413 | /** |
| 414 | * Group Instruction. |
sangho | 8995ac5 | 2015-02-04 11:29:03 -0800 | [diff] [blame] | 415 | */ |
sangho | 8995ac5 | 2015-02-04 11:29:03 -0800 | [diff] [blame] | 416 | public static final class GroupInstruction implements Instruction { |
| 417 | private final GroupId groupId; |
| 418 | |
| 419 | private GroupInstruction(GroupId groupId) { |
| 420 | this.groupId = groupId; |
| 421 | } |
| 422 | |
| 423 | public GroupId groupId() { |
| 424 | return groupId; |
| 425 | } |
| 426 | |
| 427 | @Override |
| 428 | public Type type() { |
| 429 | return Type.GROUP; |
| 430 | } |
alshabib | 9af7007 | 2015-02-09 14:34:16 -0800 | [diff] [blame] | 431 | |
sangho | 8995ac5 | 2015-02-04 11:29:03 -0800 | [diff] [blame] | 432 | @Override |
| 433 | public String toString() { |
| 434 | return toStringHelper(type().toString()) |
| 435 | .add("group ID", groupId.id()).toString(); |
| 436 | } |
| 437 | |
| 438 | @Override |
| 439 | public int hashCode() { |
Thomas Vachuska | 6c8eff3 | 2015-05-27 16:11:44 -0700 | [diff] [blame] | 440 | return Objects.hash(type().ordinal(), groupId); |
sangho | 8995ac5 | 2015-02-04 11:29:03 -0800 | [diff] [blame] | 441 | } |
| 442 | |
| 443 | @Override |
| 444 | public boolean equals(Object obj) { |
| 445 | if (this == obj) { |
| 446 | return true; |
| 447 | } |
| 448 | if (obj instanceof GroupInstruction) { |
| 449 | GroupInstruction that = (GroupInstruction) obj; |
| 450 | return Objects.equals(groupId, that.groupId); |
| 451 | |
| 452 | } |
| 453 | return false; |
| 454 | } |
| 455 | } |
| 456 | |
Saurav Das | 86af8f1 | 2015-05-25 23:55:33 -0700 | [diff] [blame] | 457 | /** |
| 458 | * Transition instruction. |
| 459 | */ |
alshabib | d17abc2 | 2015-04-21 18:26:35 -0700 | [diff] [blame] | 460 | public static class TableTypeTransition implements Instruction { |
| 461 | private final Integer tableId; |
| 462 | |
| 463 | TableTypeTransition(Integer tableId) { |
| 464 | this.tableId = tableId; |
alshabib | 9af7007 | 2015-02-09 14:34:16 -0800 | [diff] [blame] | 465 | } |
| 466 | |
| 467 | @Override |
| 468 | public Type type() { |
| 469 | return Type.TABLE; |
| 470 | } |
| 471 | |
alshabib | d17abc2 | 2015-04-21 18:26:35 -0700 | [diff] [blame] | 472 | public Integer tableId() { |
| 473 | return this.tableId; |
alshabib | 9af7007 | 2015-02-09 14:34:16 -0800 | [diff] [blame] | 474 | } |
| 475 | |
| 476 | @Override |
| 477 | public String toString() { |
| 478 | return toStringHelper(type().toString()) |
alshabib | d17abc2 | 2015-04-21 18:26:35 -0700 | [diff] [blame] | 479 | .add("tableId", this.tableId).toString(); |
alshabib | 9af7007 | 2015-02-09 14:34:16 -0800 | [diff] [blame] | 480 | } |
| 481 | |
| 482 | @Override |
| 483 | public int hashCode() { |
Thomas Vachuska | 6c8eff3 | 2015-05-27 16:11:44 -0700 | [diff] [blame] | 484 | return Objects.hash(type().ordinal(), tableId); |
alshabib | 9af7007 | 2015-02-09 14:34:16 -0800 | [diff] [blame] | 485 | } |
| 486 | |
| 487 | @Override |
| 488 | public boolean equals(Object obj) { |
| 489 | if (this == obj) { |
| 490 | return true; |
| 491 | } |
| 492 | if (obj instanceof TableTypeTransition) { |
| 493 | TableTypeTransition that = (TableTypeTransition) obj; |
alshabib | d17abc2 | 2015-04-21 18:26:35 -0700 | [diff] [blame] | 494 | return Objects.equals(tableId, that.tableId); |
alshabib | 9af7007 | 2015-02-09 14:34:16 -0800 | [diff] [blame] | 495 | |
| 496 | } |
| 497 | return false; |
| 498 | } |
Saurav Das | 86af8f1 | 2015-05-25 23:55:33 -0700 | [diff] [blame] | 499 | } |
alshabib | 9af7007 | 2015-02-09 14:34:16 -0800 | [diff] [blame] | 500 | |
Saurav Das | 86af8f1 | 2015-05-25 23:55:33 -0700 | [diff] [blame] | 501 | /** |
| 502 | * Metadata instruction. |
| 503 | */ |
| 504 | public static class MetadataInstruction implements Instruction { |
| 505 | private final long metadata; |
| 506 | private final long metadataMask; |
| 507 | |
| 508 | MetadataInstruction(long metadata, long metadataMask) { |
| 509 | this.metadata = metadata; |
| 510 | this.metadataMask = metadataMask; |
| 511 | } |
| 512 | |
| 513 | @Override |
| 514 | public Type type() { |
| 515 | return Type.METADATA; |
| 516 | } |
| 517 | |
| 518 | public long metadata() { |
| 519 | return this.metadata; |
| 520 | } |
| 521 | |
| 522 | public long metadataMask() { |
| 523 | return this.metadataMask; |
| 524 | } |
| 525 | |
| 526 | @Override |
| 527 | public String toString() { |
| 528 | return toStringHelper(type().toString()) |
| 529 | .add("metadata", Long.toHexString(this.metadata)) |
| 530 | .add("metadata mask", Long.toHexString(this.metadataMask)) |
| 531 | .toString(); |
| 532 | } |
| 533 | |
| 534 | @Override |
| 535 | public int hashCode() { |
Thomas Vachuska | 6c8eff3 | 2015-05-27 16:11:44 -0700 | [diff] [blame] | 536 | return Objects.hash(type().ordinal(), metadata, metadataMask); |
Saurav Das | 86af8f1 | 2015-05-25 23:55:33 -0700 | [diff] [blame] | 537 | } |
| 538 | |
| 539 | @Override |
| 540 | public boolean equals(Object obj) { |
| 541 | if (this == obj) { |
| 542 | return true; |
| 543 | } |
| 544 | if (obj instanceof MetadataInstruction) { |
| 545 | MetadataInstruction that = (MetadataInstruction) obj; |
| 546 | return Objects.equals(metadata, that.metadata) && |
| 547 | Objects.equals(metadataMask, that.metadataMask); |
| 548 | |
| 549 | } |
| 550 | return false; |
| 551 | } |
alshabib | 9af7007 | 2015-02-09 14:34:16 -0800 | [diff] [blame] | 552 | } |
alshabib | 55a55d9 | 2014-09-16 11:59:31 -0700 | [diff] [blame] | 553 | } |
alshabib | 8ca5390 | 2014-10-07 13:11:17 -0700 | [diff] [blame] | 554 | |
| 555 | |