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