Thomas Vachuska | 83e090e | 2014-10-22 14:25:35 -0700 | [diff] [blame] | 1 | /* |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 2 | * Copyright 2014 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 | 99b8fdc | 2014-09-25 14:30:22 -0700 | [diff] [blame] | 18 | import static com.google.common.base.MoreObjects.toStringHelper; |
alshabib | 55a55d9 | 2014-09-16 11:59:31 -0700 | [diff] [blame] | 19 | import static com.google.common.base.Preconditions.checkNotNull; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 20 | import static org.onosproject.net.flow.instructions.L2ModificationInstruction.*; |
alshabib | 55a55d9 | 2014-09-16 11:59:31 -0700 | [diff] [blame] | 21 | |
alshabib | 8ca5390 | 2014-10-07 13:11:17 -0700 | [diff] [blame] | 22 | import java.util.Objects; |
| 23 | |
sangho | 8995ac5 | 2015-02-04 11:29:03 -0800 | [diff] [blame] | 24 | import org.onosproject.core.GroupId; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 25 | import org.onosproject.net.PortNumber; |
| 26 | import org.onosproject.net.flow.instructions.L0ModificationInstruction.L0SubType; |
| 27 | import org.onosproject.net.flow.instructions.L0ModificationInstruction.ModLambdaInstruction; |
| 28 | import org.onosproject.net.flow.instructions.L2ModificationInstruction.L2SubType; |
| 29 | import org.onosproject.net.flow.instructions.L2ModificationInstruction.ModEtherInstruction; |
| 30 | import org.onosproject.net.flow.instructions.L3ModificationInstruction.L3SubType; |
| 31 | import org.onosproject.net.flow.instructions.L3ModificationInstruction.ModIPInstruction; |
Pavlin Radoslavov | febe82c | 2015-02-11 19:08:15 -0800 | [diff] [blame] | 32 | import org.onosproject.net.flow.instructions.L3ModificationInstruction.ModIPv6FlowLabelInstruction; |
sangho | 3f97a17d | 2015-01-29 22:56:29 -0800 | [diff] [blame] | 33 | import org.onosproject.net.flow.instructions.L3ModificationInstruction.ModTtlInstruction; |
Praseed Balakrishnan | 8c67d17 | 2014-11-10 10:15:41 -0800 | [diff] [blame] | 34 | |
| 35 | import org.onlab.packet.Ethernet; |
Pavlin Radoslavov | 855ea2d | 2014-10-30 15:32:39 -0700 | [diff] [blame] | 36 | import org.onlab.packet.IpAddress; |
Ayaka Koshibe | a9c199f | 2014-09-16 16:21:40 -0700 | [diff] [blame] | 37 | import org.onlab.packet.MacAddress; |
| 38 | import org.onlab.packet.VlanId; |
alshabib | 64231f6 | 2014-09-16 17:58:36 -0700 | [diff] [blame] | 39 | |
alshabib | 55a55d9 | 2014-09-16 11:59:31 -0700 | [diff] [blame] | 40 | /** |
| 41 | * Factory class for creating various traffic treatment instructions. |
| 42 | */ |
| 43 | public final class Instructions { |
| 44 | |
| 45 | |
| 46 | // Ban construction |
| 47 | private Instructions() {} |
| 48 | |
| 49 | /** |
| 50 | * Creates an output instruction using the specified port number. This can |
| 51 | * include logical ports such as CONTROLLER, FLOOD, etc. |
| 52 | * |
| 53 | * @param number port number |
| 54 | * @return output instruction |
| 55 | */ |
| 56 | public static OutputInstruction createOutput(final PortNumber number) { |
| 57 | checkNotNull(number, "PortNumber cannot be null"); |
| 58 | return new OutputInstruction(number); |
| 59 | } |
| 60 | |
| 61 | /** |
| 62 | * Creates a drop instruction. |
| 63 | * @return drop instruction |
| 64 | */ |
| 65 | public static DropInstruction createDrop() { |
| 66 | return new DropInstruction(); |
| 67 | } |
| 68 | |
| 69 | /** |
sangho | 8995ac5 | 2015-02-04 11:29:03 -0800 | [diff] [blame] | 70 | * Creates a group instruction. |
| 71 | * |
| 72 | * @param groupId Group Id |
| 73 | * @return group instruction |
| 74 | */ |
| 75 | public static GroupInstruction createGroup(final GroupId groupId) { |
| 76 | checkNotNull(groupId, "GroupId cannot be null"); |
| 77 | return new GroupInstruction(groupId); |
| 78 | } |
| 79 | |
| 80 | /** |
Marc De Leenheer | 4908775 | 2014-10-23 13:54:09 -0700 | [diff] [blame] | 81 | * Creates a l0 modification. |
| 82 | * @param lambda the lambda to modify to. |
| 83 | * @return a l0 modification |
| 84 | */ |
| 85 | public static L0ModificationInstruction modL0Lambda(short lambda) { |
| 86 | checkNotNull(lambda, "L0 lambda cannot be null"); |
| 87 | return new ModLambdaInstruction(L0SubType.LAMBDA, lambda); |
| 88 | } |
| 89 | |
| 90 | /** |
alshabib | 55a55d9 | 2014-09-16 11:59:31 -0700 | [diff] [blame] | 91 | * Creates a l2 src modification. |
| 92 | * @param addr the mac address to modify to. |
| 93 | * @return a l2 modification |
| 94 | */ |
Ayaka Koshibe | a9c199f | 2014-09-16 16:21:40 -0700 | [diff] [blame] | 95 | public static L2ModificationInstruction modL2Src(MacAddress addr) { |
alshabib | 55a55d9 | 2014-09-16 11:59:31 -0700 | [diff] [blame] | 96 | checkNotNull(addr, "Src l2 address cannot be null"); |
alshabib | 99b8fdc | 2014-09-25 14:30:22 -0700 | [diff] [blame] | 97 | return new ModEtherInstruction(L2SubType.ETH_SRC, addr); |
alshabib | 55a55d9 | 2014-09-16 11:59:31 -0700 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | /** |
| 101 | * Creates a L2 dst modification. |
| 102 | * @param addr the mac address to modify to. |
| 103 | * @return a L2 modification |
| 104 | */ |
Ayaka Koshibe | a9c199f | 2014-09-16 16:21:40 -0700 | [diff] [blame] | 105 | public static L2ModificationInstruction modL2Dst(MacAddress addr) { |
alshabib | 55a55d9 | 2014-09-16 11:59:31 -0700 | [diff] [blame] | 106 | checkNotNull(addr, "Dst l2 address cannot be null"); |
Praseed Balakrishnan | 8c67d17 | 2014-11-10 10:15:41 -0800 | [diff] [blame] | 107 | return new ModEtherInstruction(L2SubType.ETH_DST, addr); |
alshabib | 55a55d9 | 2014-09-16 11:59:31 -0700 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | /** |
alshabib | 7410fea | 2014-09-16 13:48:39 -0700 | [diff] [blame] | 111 | * Creates a Vlan id modification. |
| 112 | * @param vlanId the vlan id to modify to. |
| 113 | * @return a L2 modification |
| 114 | */ |
Ayaka Koshibe | a9c199f | 2014-09-16 16:21:40 -0700 | [diff] [blame] | 115 | public static L2ModificationInstruction modVlanId(VlanId vlanId) { |
alshabib | 55a55d9 | 2014-09-16 11:59:31 -0700 | [diff] [blame] | 116 | checkNotNull(vlanId, "VLAN id cannot be null"); |
Praseed Balakrishnan | 8c67d17 | 2014-11-10 10:15:41 -0800 | [diff] [blame] | 117 | return new ModVlanIdInstruction(vlanId); |
alshabib | 55a55d9 | 2014-09-16 11:59:31 -0700 | [diff] [blame] | 118 | } |
| 119 | |
alshabib | 7410fea | 2014-09-16 13:48:39 -0700 | [diff] [blame] | 120 | /** |
| 121 | * Creates a Vlan pcp modification. |
| 122 | * @param vlanPcp the pcp to modify to. |
| 123 | * @return a L2 modification |
| 124 | */ |
| 125 | public static L2ModificationInstruction modVlanPcp(Byte vlanPcp) { |
| 126 | checkNotNull(vlanPcp, "VLAN Pcp cannot be null"); |
Praseed Balakrishnan | 8c67d17 | 2014-11-10 10:15:41 -0800 | [diff] [blame] | 127 | return new ModVlanPcpInstruction(vlanPcp); |
alshabib | 7410fea | 2014-09-16 13:48:39 -0700 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | /** |
Praseed Balakrishnan | 8c67d17 | 2014-11-10 10:15:41 -0800 | [diff] [blame] | 131 | * Creates a MPLS label modification. |
| 132 | * @param mplsLabel to set. |
| 133 | * @return a L2 Modification |
| 134 | */ |
| 135 | public static L2ModificationInstruction modMplsLabel(Integer mplsLabel) { |
| 136 | checkNotNull(mplsLabel, "MPLS label cannot be null"); |
| 137 | return new ModMplsLabelInstruction(mplsLabel); |
| 138 | } |
sangho | 3f97a17d | 2015-01-29 22:56:29 -0800 | [diff] [blame] | 139 | |
| 140 | /** |
| 141 | * Creates a MPLS TTL modification. |
| 142 | * |
| 143 | * @return a L2 Modification |
| 144 | */ |
| 145 | public static L2ModificationInstruction decMplsTtl() { |
| 146 | return new ModMplsTtlInstruction(); |
| 147 | } |
Pavlin Radoslavov | febe82c | 2015-02-11 19:08:15 -0800 | [diff] [blame] | 148 | |
Praseed Balakrishnan | 8c67d17 | 2014-11-10 10:15:41 -0800 | [diff] [blame] | 149 | /** |
Pavlin Radoslavov | febe82c | 2015-02-11 19:08:15 -0800 | [diff] [blame] | 150 | * Creates a L3 IPv4 src modification. |
| 151 | * |
| 152 | * @param addr the IPv4 address to modify to |
alshabib | 7410fea | 2014-09-16 13:48:39 -0700 | [diff] [blame] | 153 | * @return a L3 modification |
| 154 | */ |
Pavlin Radoslavov | 855ea2d | 2014-10-30 15:32:39 -0700 | [diff] [blame] | 155 | public static L3ModificationInstruction modL3Src(IpAddress addr) { |
Pavlin Radoslavov | febe82c | 2015-02-11 19:08:15 -0800 | [diff] [blame] | 156 | checkNotNull(addr, "Src l3 IPv4 address cannot be null"); |
| 157 | return new ModIPInstruction(L3SubType.IPV4_SRC, addr); |
alshabib | 7410fea | 2014-09-16 13:48:39 -0700 | [diff] [blame] | 158 | } |
| 159 | |
| 160 | /** |
Pavlin Radoslavov | febe82c | 2015-02-11 19:08:15 -0800 | [diff] [blame] | 161 | * Creates a L3 IPv4 dst modification. |
| 162 | * |
| 163 | * @param addr the IPv4 address to modify to |
alshabib | 7410fea | 2014-09-16 13:48:39 -0700 | [diff] [blame] | 164 | * @return a L3 modification |
| 165 | */ |
Pavlin Radoslavov | 855ea2d | 2014-10-30 15:32:39 -0700 | [diff] [blame] | 166 | public static L3ModificationInstruction modL3Dst(IpAddress addr) { |
Pavlin Radoslavov | febe82c | 2015-02-11 19:08:15 -0800 | [diff] [blame] | 167 | checkNotNull(addr, "Dst l3 IPv4 address cannot be null"); |
| 168 | return new ModIPInstruction(L3SubType.IPV4_DST, addr); |
| 169 | } |
| 170 | |
| 171 | /** |
| 172 | * Creates a L3 IPv6 src modification. |
| 173 | * |
| 174 | * @param addr the IPv6 address to modify to |
| 175 | * @return a L3 modification |
| 176 | */ |
| 177 | public static L3ModificationInstruction modL3IPv6Src(IpAddress addr) { |
| 178 | checkNotNull(addr, "Src l3 IPv6 address cannot be null"); |
| 179 | return new ModIPInstruction(L3SubType.IPV6_SRC, addr); |
| 180 | } |
| 181 | |
| 182 | /** |
| 183 | * Creates a L3 IPv6 dst modification. |
| 184 | * |
| 185 | * @param addr the IPv6 address to modify to |
| 186 | * @return a L3 modification |
| 187 | */ |
| 188 | public static L3ModificationInstruction modL3IPv6Dst(IpAddress addr) { |
| 189 | checkNotNull(addr, "Dst l3 IPv6 address cannot be null"); |
| 190 | return new ModIPInstruction(L3SubType.IPV6_DST, addr); |
| 191 | } |
| 192 | |
| 193 | /** |
| 194 | * Creates a L3 IPv6 Flow Label modification. |
| 195 | * |
| 196 | * @param flowLabel the IPv6 flow label to modify to (20 bits) |
| 197 | * @return a L3 modification |
| 198 | */ |
| 199 | public static L3ModificationInstruction modL3IPv6FlowLabel(int flowLabel) { |
| 200 | return new ModIPv6FlowLabelInstruction(flowLabel); |
alshabib | 7410fea | 2014-09-16 13:48:39 -0700 | [diff] [blame] | 201 | } |
| 202 | |
Praseed Balakrishnan | 8c67d17 | 2014-11-10 10:15:41 -0800 | [diff] [blame] | 203 | /** |
sangho | 3f97a17d | 2015-01-29 22:56:29 -0800 | [diff] [blame] | 204 | * Creates a L3 TTL modification. |
| 205 | * @return a L3 modification |
| 206 | */ |
| 207 | public static L3ModificationInstruction decNwTtl() { |
| 208 | return new ModTtlInstruction(L3SubType.DEC_TTL); |
| 209 | } |
| 210 | |
| 211 | /** |
| 212 | * Creates a L3 TTL modification. |
| 213 | * @return a L3 modification |
| 214 | */ |
| 215 | public static L3ModificationInstruction copyTtlOut() { |
| 216 | return new ModTtlInstruction(L3SubType.TTL_OUT); |
| 217 | } |
| 218 | |
| 219 | /** |
| 220 | * Creates a L3 TTL modification. |
| 221 | * @return a L3 modification |
| 222 | */ |
| 223 | public static L3ModificationInstruction copyTtlIn() { |
| 224 | return new ModTtlInstruction(L3SubType.TTL_IN); |
| 225 | } |
| 226 | |
| 227 | /** |
Praseed Balakrishnan | 8c67d17 | 2014-11-10 10:15:41 -0800 | [diff] [blame] | 228 | * Creates a mpls header instruction. |
| 229 | * @return a L2 modification. |
| 230 | */ |
| 231 | public static Instruction pushMpls() { |
| 232 | return new PushHeaderInstructions(L2SubType.MPLS_PUSH, |
Yuta HIGUCHI | 32a53c5 | 2015-02-08 01:25:40 -0800 | [diff] [blame] | 233 | Ethernet.MPLS_UNICAST); |
Praseed Balakrishnan | 8c67d17 | 2014-11-10 10:15:41 -0800 | [diff] [blame] | 234 | } |
| 235 | |
| 236 | /** |
| 237 | * Creates a mpls header instruction. |
| 238 | * @return a L2 modification. |
| 239 | */ |
| 240 | public static Instruction popMpls() { |
| 241 | return new PushHeaderInstructions(L2SubType.MPLS_POP, |
Yuta HIGUCHI | 32a53c5 | 2015-02-08 01:25:40 -0800 | [diff] [blame] | 242 | Ethernet.MPLS_UNICAST); |
Praseed Balakrishnan | 8c67d17 | 2014-11-10 10:15:41 -0800 | [diff] [blame] | 243 | } |
alshabib | 7410fea | 2014-09-16 13:48:39 -0700 | [diff] [blame] | 244 | |
sangho | 3f97a17d | 2015-01-29 22:56:29 -0800 | [diff] [blame] | 245 | /** |
| 246 | * Creates a mpls header instruction. |
| 247 | * |
| 248 | * @param etherType Ethernet type to set |
| 249 | * @return a L2 modification. |
| 250 | */ |
| 251 | public static Instruction popMpls(Short etherType) { |
| 252 | checkNotNull(etherType, "Ethernet type cannot be null"); |
Yuta HIGUCHI | 32a53c5 | 2015-02-08 01:25:40 -0800 | [diff] [blame] | 253 | return new PushHeaderInstructions(L2SubType.MPLS_POP, etherType); |
sangho | 3f97a17d | 2015-01-29 22:56:29 -0800 | [diff] [blame] | 254 | } |
| 255 | |
alshabib | 55a55d9 | 2014-09-16 11:59:31 -0700 | [diff] [blame] | 256 | /* |
sangho | 8995ac5 | 2015-02-04 11:29:03 -0800 | [diff] [blame] | 257 | * Drop instructions |
alshabib | 55a55d9 | 2014-09-16 11:59:31 -0700 | [diff] [blame] | 258 | */ |
| 259 | |
| 260 | public static final class DropInstruction implements Instruction { |
| 261 | @Override |
| 262 | public Type type() { |
| 263 | return Type.DROP; |
| 264 | } |
alshabib | 99b8fdc | 2014-09-25 14:30:22 -0700 | [diff] [blame] | 265 | |
| 266 | @Override |
| 267 | public String toString() { |
| 268 | return toStringHelper(type()).toString(); |
| 269 | |
| 270 | } |
alshabib | 8ca5390 | 2014-10-07 13:11:17 -0700 | [diff] [blame] | 271 | |
| 272 | @Override |
| 273 | public int hashCode() { |
| 274 | return Objects.hash(type()); |
| 275 | } |
| 276 | |
| 277 | @Override |
| 278 | public boolean equals(Object obj) { |
| 279 | if (this == obj) { |
| 280 | return true; |
| 281 | } |
| 282 | if (obj instanceof DropInstruction) { |
| 283 | DropInstruction that = (DropInstruction) obj; |
| 284 | return Objects.equals(type(), that.type()); |
| 285 | |
| 286 | } |
| 287 | return false; |
| 288 | } |
alshabib | 55a55d9 | 2014-09-16 11:59:31 -0700 | [diff] [blame] | 289 | } |
| 290 | |
sangho | 8995ac5 | 2015-02-04 11:29:03 -0800 | [diff] [blame] | 291 | /* |
| 292 | * Output Instruction |
| 293 | */ |
alshabib | 55a55d9 | 2014-09-16 11:59:31 -0700 | [diff] [blame] | 294 | |
| 295 | public static final class OutputInstruction implements Instruction { |
| 296 | private final PortNumber port; |
| 297 | |
| 298 | private OutputInstruction(PortNumber port) { |
| 299 | this.port = port; |
| 300 | } |
| 301 | |
| 302 | public PortNumber port() { |
| 303 | return port; |
| 304 | } |
| 305 | |
| 306 | @Override |
| 307 | public Type type() { |
| 308 | return Type.OUTPUT; |
| 309 | } |
alshabib | 99b8fdc | 2014-09-25 14:30:22 -0700 | [diff] [blame] | 310 | @Override |
| 311 | public String toString() { |
| 312 | return toStringHelper(type().toString()) |
| 313 | .add("port", port).toString(); |
| 314 | } |
alshabib | 8ca5390 | 2014-10-07 13:11:17 -0700 | [diff] [blame] | 315 | |
| 316 | @Override |
| 317 | public int hashCode() { |
Thomas Vachuska | 9b2da21 | 2014-11-10 19:30:25 -0800 | [diff] [blame] | 318 | return Objects.hash(type(), port); |
alshabib | 8ca5390 | 2014-10-07 13:11:17 -0700 | [diff] [blame] | 319 | } |
| 320 | |
| 321 | @Override |
| 322 | public boolean equals(Object obj) { |
| 323 | if (this == obj) { |
| 324 | return true; |
| 325 | } |
| 326 | if (obj instanceof OutputInstruction) { |
| 327 | OutputInstruction that = (OutputInstruction) obj; |
Yuta HIGUCHI | 4ce6529 | 2014-11-01 12:09:55 -0700 | [diff] [blame] | 328 | return Objects.equals(port, that.port); |
alshabib | 8ca5390 | 2014-10-07 13:11:17 -0700 | [diff] [blame] | 329 | |
| 330 | } |
| 331 | return false; |
| 332 | } |
alshabib | 55a55d9 | 2014-09-16 11:59:31 -0700 | [diff] [blame] | 333 | } |
| 334 | |
sangho | 8995ac5 | 2015-02-04 11:29:03 -0800 | [diff] [blame] | 335 | /* |
| 336 | * Group Instruction |
| 337 | */ |
| 338 | |
| 339 | public static final class GroupInstruction implements Instruction { |
| 340 | private final GroupId groupId; |
| 341 | |
| 342 | private GroupInstruction(GroupId groupId) { |
| 343 | this.groupId = groupId; |
| 344 | } |
| 345 | |
| 346 | public GroupId groupId() { |
| 347 | return groupId; |
| 348 | } |
| 349 | |
| 350 | @Override |
| 351 | public Type type() { |
| 352 | return Type.GROUP; |
| 353 | } |
| 354 | @Override |
| 355 | public String toString() { |
| 356 | return toStringHelper(type().toString()) |
| 357 | .add("group ID", groupId.id()).toString(); |
| 358 | } |
| 359 | |
| 360 | @Override |
| 361 | public int hashCode() { |
| 362 | return Objects.hash(type(), groupId); |
| 363 | } |
| 364 | |
| 365 | @Override |
| 366 | public boolean equals(Object obj) { |
| 367 | if (this == obj) { |
| 368 | return true; |
| 369 | } |
| 370 | if (obj instanceof GroupInstruction) { |
| 371 | GroupInstruction that = (GroupInstruction) obj; |
| 372 | return Objects.equals(groupId, that.groupId); |
| 373 | |
| 374 | } |
| 375 | return false; |
| 376 | } |
| 377 | } |
| 378 | |
alshabib | 55a55d9 | 2014-09-16 11:59:31 -0700 | [diff] [blame] | 379 | } |
alshabib | 8ca5390 | 2014-10-07 13:11:17 -0700 | [diff] [blame] | 380 | |
| 381 | |