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; |
| 19 | |
alshabib | 8ca5390 | 2014-10-07 13:11:17 -0700 | [diff] [blame] | 20 | import java.util.Objects; |
| 21 | |
Praseed Balakrishnan | 8c67d17 | 2014-11-10 10:15:41 -0800 | [diff] [blame] | 22 | import org.onlab.packet.Ethernet; |
Ayaka Koshibe | a9c199f | 2014-09-16 16:21:40 -0700 | [diff] [blame] | 23 | import org.onlab.packet.MacAddress; |
| 24 | import org.onlab.packet.VlanId; |
alshabib | 55a55d9 | 2014-09-16 11:59:31 -0700 | [diff] [blame] | 25 | |
| 26 | /** |
| 27 | * Abstraction of a single traffic treatment step. |
alshabib | 55a55d9 | 2014-09-16 11:59:31 -0700 | [diff] [blame] | 28 | */ |
| 29 | public abstract class L2ModificationInstruction implements Instruction { |
| 30 | |
| 31 | /** |
| 32 | * Represents the type of traffic treatment. |
| 33 | */ |
alshabib | 35edb1a | 2014-09-16 17:44:44 -0700 | [diff] [blame] | 34 | public enum L2SubType { |
alshabib | 55a55d9 | 2014-09-16 11:59:31 -0700 | [diff] [blame] | 35 | /** |
| 36 | * Ether src modification. |
| 37 | */ |
alshabib | 99b8fdc | 2014-09-25 14:30:22 -0700 | [diff] [blame] | 38 | ETH_SRC, |
alshabib | 55a55d9 | 2014-09-16 11:59:31 -0700 | [diff] [blame] | 39 | |
| 40 | /** |
| 41 | * Ether dst modification. |
| 42 | */ |
alshabib | 99b8fdc | 2014-09-25 14:30:22 -0700 | [diff] [blame] | 43 | ETH_DST, |
alshabib | 55a55d9 | 2014-09-16 11:59:31 -0700 | [diff] [blame] | 44 | |
| 45 | /** |
alshabib | 55a55d9 | 2014-09-16 11:59:31 -0700 | [diff] [blame] | 46 | * VLAN id modification. |
| 47 | */ |
| 48 | VLAN_ID, |
| 49 | |
| 50 | /** |
| 51 | * VLAN priority modification. |
| 52 | */ |
Praseed Balakrishnan | 8c67d17 | 2014-11-10 10:15:41 -0800 | [diff] [blame] | 53 | VLAN_PCP, |
| 54 | |
| 55 | /** |
| 56 | * MPLS Label modification. |
| 57 | */ |
| 58 | MPLS_LABEL, |
| 59 | |
| 60 | /** |
| 61 | * MPLS Push modification. |
| 62 | */ |
| 63 | MPLS_PUSH, |
| 64 | |
| 65 | /** |
| 66 | * MPLS Pop modification. |
| 67 | */ |
| 68 | MPLS_POP |
alshabib | 55a55d9 | 2014-09-16 11:59:31 -0700 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | // TODO: Create factory class 'Instructions' that will have various factory |
| 72 | // to create specific instructions. |
| 73 | |
alshabib | 35edb1a | 2014-09-16 17:44:44 -0700 | [diff] [blame] | 74 | public abstract L2SubType subtype(); |
alshabib | 55a55d9 | 2014-09-16 11:59:31 -0700 | [diff] [blame] | 75 | |
| 76 | @Override |
| 77 | public Type type() { |
alshabib | 35edb1a | 2014-09-16 17:44:44 -0700 | [diff] [blame] | 78 | return Type.L2MODIFICATION; |
alshabib | 55a55d9 | 2014-09-16 11:59:31 -0700 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | /** |
| 82 | * Represents a L2 src/dst modification instruction. |
| 83 | */ |
| 84 | public static final class ModEtherInstruction extends L2ModificationInstruction { |
| 85 | |
alshabib | 35edb1a | 2014-09-16 17:44:44 -0700 | [diff] [blame] | 86 | private final L2SubType subtype; |
Ayaka Koshibe | a9c199f | 2014-09-16 16:21:40 -0700 | [diff] [blame] | 87 | private final MacAddress mac; |
alshabib | 55a55d9 | 2014-09-16 11:59:31 -0700 | [diff] [blame] | 88 | |
alshabib | 64231f6 | 2014-09-16 17:58:36 -0700 | [diff] [blame] | 89 | public ModEtherInstruction(L2SubType subType, MacAddress addr) { |
| 90 | |
alshabib | 55a55d9 | 2014-09-16 11:59:31 -0700 | [diff] [blame] | 91 | this.subtype = subType; |
| 92 | this.mac = addr; |
| 93 | } |
| 94 | |
| 95 | @Override |
alshabib | 35edb1a | 2014-09-16 17:44:44 -0700 | [diff] [blame] | 96 | public L2SubType subtype() { |
alshabib | 55a55d9 | 2014-09-16 11:59:31 -0700 | [diff] [blame] | 97 | return this.subtype; |
| 98 | } |
| 99 | |
Ayaka Koshibe | a9c199f | 2014-09-16 16:21:40 -0700 | [diff] [blame] | 100 | public MacAddress mac() { |
alshabib | 55a55d9 | 2014-09-16 11:59:31 -0700 | [diff] [blame] | 101 | return this.mac; |
| 102 | } |
| 103 | |
alshabib | 99b8fdc | 2014-09-25 14:30:22 -0700 | [diff] [blame] | 104 | @Override |
| 105 | public String toString() { |
| 106 | return toStringHelper(subtype().toString()) |
| 107 | .add("mac", mac).toString(); |
| 108 | } |
| 109 | |
alshabib | 8ca5390 | 2014-10-07 13:11:17 -0700 | [diff] [blame] | 110 | @Override |
| 111 | public int hashCode() { |
Thomas Vachuska | 9b2da21 | 2014-11-10 19:30:25 -0800 | [diff] [blame] | 112 | return Objects.hash(type(), subtype, mac); |
alshabib | 8ca5390 | 2014-10-07 13:11:17 -0700 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | @Override |
| 116 | public boolean equals(Object obj) { |
| 117 | if (this == obj) { |
| 118 | return true; |
| 119 | } |
| 120 | if (obj instanceof ModEtherInstruction) { |
| 121 | ModEtherInstruction that = (ModEtherInstruction) obj; |
| 122 | return Objects.equals(mac, that.mac) && |
alshabib | 2020b89 | 2014-10-20 15:47:08 -0700 | [diff] [blame] | 123 | Objects.equals(this.type(), that.type()) && |
alshabib | 8ca5390 | 2014-10-07 13:11:17 -0700 | [diff] [blame] | 124 | Objects.equals(subtype, that.subtype); |
| 125 | |
| 126 | } |
| 127 | return false; |
| 128 | } |
| 129 | |
alshabib | 99b8fdc | 2014-09-25 14:30:22 -0700 | [diff] [blame] | 130 | |
alshabib | 55a55d9 | 2014-09-16 11:59:31 -0700 | [diff] [blame] | 131 | } |
| 132 | |
Praseed Balakrishnan | 8c67d17 | 2014-11-10 10:15:41 -0800 | [diff] [blame] | 133 | public static final class PushHeaderInstructions extends |
| 134 | L2ModificationInstruction { |
| 135 | |
| 136 | private final L2SubType subtype; |
| 137 | private final Ethernet ethernetType; |
| 138 | |
| 139 | public PushHeaderInstructions(L2SubType subType, Ethernet ethernetType) { |
| 140 | this.subtype = subType; |
| 141 | this.ethernetType = ethernetType; |
| 142 | } |
| 143 | |
| 144 | public Ethernet ethernetType() { |
| 145 | return ethernetType; |
| 146 | } |
| 147 | |
| 148 | @Override |
| 149 | public L2SubType subtype() { |
| 150 | return this.subtype; |
| 151 | } |
| 152 | |
| 153 | @Override |
| 154 | public String toString() { |
| 155 | return toStringHelper(subtype().toString()).toString(); |
| 156 | } |
| 157 | |
| 158 | @Override |
| 159 | public int hashCode() { |
| 160 | return Objects.hash(type(), subtype); |
| 161 | } |
| 162 | |
| 163 | @Override |
| 164 | public boolean equals(Object obj) { |
| 165 | if (this == obj) { |
| 166 | return true; |
| 167 | } |
| 168 | if (obj instanceof PushHeaderInstructions) { |
| 169 | PushHeaderInstructions that = (PushHeaderInstructions) obj; |
| 170 | return Objects.equals(this.type(), that.type()) && |
| 171 | Objects.equals(subtype, that.subtype); |
| 172 | |
| 173 | } |
| 174 | return false; |
| 175 | } |
| 176 | } |
| 177 | |
| 178 | |
| 179 | |
alshabib | 55a55d9 | 2014-09-16 11:59:31 -0700 | [diff] [blame] | 180 | /** |
alshabib | 55a55d9 | 2014-09-16 11:59:31 -0700 | [diff] [blame] | 181 | * Represents a VLAN id modification instruction. |
| 182 | */ |
| 183 | public static final class ModVlanIdInstruction extends L2ModificationInstruction { |
| 184 | |
Ray Milkey | 7808105 | 2014-11-05 10:38:12 -0800 | [diff] [blame] | 185 | private final VlanId vlanId; |
alshabib | 55a55d9 | 2014-09-16 11:59:31 -0700 | [diff] [blame] | 186 | |
Ayaka Koshibe | a9c199f | 2014-09-16 16:21:40 -0700 | [diff] [blame] | 187 | public ModVlanIdInstruction(VlanId vlanId) { |
alshabib | 55a55d9 | 2014-09-16 11:59:31 -0700 | [diff] [blame] | 188 | this.vlanId = vlanId; |
| 189 | } |
| 190 | |
| 191 | @Override |
alshabib | 35edb1a | 2014-09-16 17:44:44 -0700 | [diff] [blame] | 192 | public L2SubType subtype() { |
alshabib | 7410fea | 2014-09-16 13:48:39 -0700 | [diff] [blame] | 193 | return L2SubType.VLAN_ID; |
alshabib | 55a55d9 | 2014-09-16 11:59:31 -0700 | [diff] [blame] | 194 | } |
| 195 | |
Ayaka Koshibe | a9c199f | 2014-09-16 16:21:40 -0700 | [diff] [blame] | 196 | public VlanId vlanId() { |
alshabib | 55a55d9 | 2014-09-16 11:59:31 -0700 | [diff] [blame] | 197 | return this.vlanId; |
| 198 | } |
| 199 | |
alshabib | 99b8fdc | 2014-09-25 14:30:22 -0700 | [diff] [blame] | 200 | @Override |
| 201 | public String toString() { |
| 202 | return toStringHelper(subtype().toString()) |
| 203 | .add("id", vlanId).toString(); |
| 204 | } |
| 205 | |
alshabib | 8ca5390 | 2014-10-07 13:11:17 -0700 | [diff] [blame] | 206 | @Override |
| 207 | public int hashCode() { |
Thomas Vachuska | 9b2da21 | 2014-11-10 19:30:25 -0800 | [diff] [blame] | 208 | return Objects.hash(type(), subtype(), vlanId); |
alshabib | 8ca5390 | 2014-10-07 13:11:17 -0700 | [diff] [blame] | 209 | } |
| 210 | |
| 211 | @Override |
| 212 | public boolean equals(Object obj) { |
| 213 | if (this == obj) { |
| 214 | return true; |
| 215 | } |
| 216 | if (obj instanceof ModVlanIdInstruction) { |
| 217 | ModVlanIdInstruction that = (ModVlanIdInstruction) obj; |
alshabib | 2020b89 | 2014-10-20 15:47:08 -0700 | [diff] [blame] | 218 | return Objects.equals(vlanId, that.vlanId) && |
| 219 | Objects.equals(this.type(), that.type()) && |
| 220 | Objects.equals(this.subtype(), that.subtype()); |
alshabib | 8ca5390 | 2014-10-07 13:11:17 -0700 | [diff] [blame] | 221 | |
| 222 | } |
| 223 | return false; |
| 224 | } |
| 225 | |
| 226 | |
alshabib | 55a55d9 | 2014-09-16 11:59:31 -0700 | [diff] [blame] | 227 | } |
| 228 | |
alshabib | 7410fea | 2014-09-16 13:48:39 -0700 | [diff] [blame] | 229 | /** |
| 230 | * Represents a VLAN PCP modification instruction. |
| 231 | */ |
| 232 | public static final class ModVlanPcpInstruction extends L2ModificationInstruction { |
| 233 | |
Ray Milkey | 7808105 | 2014-11-05 10:38:12 -0800 | [diff] [blame] | 234 | private final Byte vlanPcp; |
alshabib | 7410fea | 2014-09-16 13:48:39 -0700 | [diff] [blame] | 235 | |
| 236 | public ModVlanPcpInstruction(Byte vlanPcp) { |
| 237 | this.vlanPcp = vlanPcp; |
| 238 | } |
| 239 | |
| 240 | @Override |
alshabib | 35edb1a | 2014-09-16 17:44:44 -0700 | [diff] [blame] | 241 | public L2SubType subtype() { |
alshabib | 7410fea | 2014-09-16 13:48:39 -0700 | [diff] [blame] | 242 | return L2SubType.VLAN_PCP; |
| 243 | } |
| 244 | |
| 245 | public Byte vlanPcp() { |
| 246 | return this.vlanPcp; |
| 247 | } |
| 248 | |
alshabib | 99b8fdc | 2014-09-25 14:30:22 -0700 | [diff] [blame] | 249 | @Override |
| 250 | public String toString() { |
| 251 | return toStringHelper(subtype().toString()) |
| 252 | .add("pcp", Long.toHexString(vlanPcp)).toString(); |
| 253 | } |
| 254 | |
alshabib | 8ca5390 | 2014-10-07 13:11:17 -0700 | [diff] [blame] | 255 | @Override |
| 256 | public int hashCode() { |
Thomas Vachuska | 9b2da21 | 2014-11-10 19:30:25 -0800 | [diff] [blame] | 257 | return Objects.hash(type(), subtype(), vlanPcp); |
alshabib | 8ca5390 | 2014-10-07 13:11:17 -0700 | [diff] [blame] | 258 | } |
| 259 | |
| 260 | @Override |
| 261 | public boolean equals(Object obj) { |
| 262 | if (this == obj) { |
| 263 | return true; |
| 264 | } |
| 265 | if (obj instanceof ModVlanPcpInstruction) { |
| 266 | ModVlanPcpInstruction that = (ModVlanPcpInstruction) obj; |
alshabib | 2020b89 | 2014-10-20 15:47:08 -0700 | [diff] [blame] | 267 | return Objects.equals(vlanPcp, that.vlanPcp) && |
| 268 | Objects.equals(this.type(), that.type()) && |
| 269 | Objects.equals(this.subtype(), that.subtype()); |
alshabib | 8ca5390 | 2014-10-07 13:11:17 -0700 | [diff] [blame] | 270 | |
| 271 | } |
| 272 | return false; |
| 273 | } |
| 274 | |
alshabib | 7410fea | 2014-09-16 13:48:39 -0700 | [diff] [blame] | 275 | } |
| 276 | |
alshabib | 55a55d9 | 2014-09-16 11:59:31 -0700 | [diff] [blame] | 277 | |
Praseed Balakrishnan | 8c67d17 | 2014-11-10 10:15:41 -0800 | [diff] [blame] | 278 | /** |
| 279 | * Represents a MPLS label modification. |
| 280 | */ |
| 281 | public static final class ModMplsLabelInstruction extends |
| 282 | L2ModificationInstruction { |
| 283 | |
| 284 | private final Integer mplsLabel; |
| 285 | |
| 286 | public ModMplsLabelInstruction(Integer mplsLabel) { |
| 287 | this.mplsLabel = mplsLabel; |
| 288 | } |
| 289 | |
| 290 | public Integer label() { |
| 291 | return mplsLabel; |
| 292 | } |
| 293 | |
| 294 | @Override |
| 295 | public L2SubType subtype() { |
| 296 | return L2SubType.MPLS_LABEL; |
| 297 | } |
| 298 | |
| 299 | @Override |
| 300 | public String toString() { |
| 301 | return toStringHelper(type().toString()) |
| 302 | .add("mpls", mplsLabel.intValue()).toString(); |
| 303 | } |
| 304 | |
| 305 | @Override |
| 306 | public int hashCode() { |
| 307 | return Objects.hash(mplsLabel); |
| 308 | } |
| 309 | |
| 310 | @Override |
| 311 | public boolean equals(Object obj) { |
| 312 | if (this == obj) { |
| 313 | return true; |
| 314 | } |
| 315 | if (obj instanceof ModMplsLabelInstruction) { |
| 316 | ModMplsLabelInstruction that = (ModMplsLabelInstruction) obj; |
| 317 | return Objects.equals(mplsLabel, that.mplsLabel) && |
| 318 | Objects.equals(this.type(), that.type()); |
| 319 | |
| 320 | |
| 321 | } |
| 322 | return false; |
| 323 | } |
| 324 | } |
alshabib | 55a55d9 | 2014-09-16 11:59:31 -0700 | [diff] [blame] | 325 | } |