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