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