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 | |
Ayaka Koshibe | a9c199f | 2014-09-16 16:21:40 -0700 | [diff] [blame] | 22 | import org.onlab.packet.MacAddress; |
Michele Santuari | 4b6019e | 2014-12-19 11:31:45 +0100 | [diff] [blame] | 23 | import org.onlab.packet.MplsLabel; |
Ayaka Koshibe | a9c199f | 2014-09-16 16:21:40 -0700 | [diff] [blame] | 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 | */ |
sangho | 3f97a17d | 2015-01-29 22:56:29 -0800 | [diff] [blame] | 68 | MPLS_POP, |
| 69 | |
| 70 | /** |
| 71 | * MPLS TTL modification. |
| 72 | */ |
| 73 | DEC_MPLS_TTL |
| 74 | |
alshabib | 55a55d9 | 2014-09-16 11:59:31 -0700 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | // TODO: Create factory class 'Instructions' that will have various factory |
| 78 | // to create specific instructions. |
| 79 | |
alshabib | 35edb1a | 2014-09-16 17:44:44 -0700 | [diff] [blame] | 80 | public abstract L2SubType subtype(); |
alshabib | 55a55d9 | 2014-09-16 11:59:31 -0700 | [diff] [blame] | 81 | |
| 82 | @Override |
| 83 | public Type type() { |
alshabib | 35edb1a | 2014-09-16 17:44:44 -0700 | [diff] [blame] | 84 | return Type.L2MODIFICATION; |
alshabib | 55a55d9 | 2014-09-16 11:59:31 -0700 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | /** |
| 88 | * Represents a L2 src/dst modification instruction. |
| 89 | */ |
| 90 | public static final class ModEtherInstruction extends L2ModificationInstruction { |
| 91 | |
alshabib | 35edb1a | 2014-09-16 17:44:44 -0700 | [diff] [blame] | 92 | private final L2SubType subtype; |
Ayaka Koshibe | a9c199f | 2014-09-16 16:21:40 -0700 | [diff] [blame] | 93 | private final MacAddress mac; |
alshabib | 55a55d9 | 2014-09-16 11:59:31 -0700 | [diff] [blame] | 94 | |
alshabib | 64231f6 | 2014-09-16 17:58:36 -0700 | [diff] [blame] | 95 | public ModEtherInstruction(L2SubType subType, MacAddress addr) { |
| 96 | |
alshabib | 55a55d9 | 2014-09-16 11:59:31 -0700 | [diff] [blame] | 97 | this.subtype = subType; |
| 98 | this.mac = addr; |
| 99 | } |
| 100 | |
| 101 | @Override |
alshabib | 35edb1a | 2014-09-16 17:44:44 -0700 | [diff] [blame] | 102 | public L2SubType subtype() { |
alshabib | 55a55d9 | 2014-09-16 11:59:31 -0700 | [diff] [blame] | 103 | return this.subtype; |
| 104 | } |
| 105 | |
Ayaka Koshibe | a9c199f | 2014-09-16 16:21:40 -0700 | [diff] [blame] | 106 | public MacAddress mac() { |
alshabib | 55a55d9 | 2014-09-16 11:59:31 -0700 | [diff] [blame] | 107 | return this.mac; |
| 108 | } |
| 109 | |
alshabib | 99b8fdc | 2014-09-25 14:30:22 -0700 | [diff] [blame] | 110 | @Override |
| 111 | public String toString() { |
| 112 | return toStringHelper(subtype().toString()) |
| 113 | .add("mac", mac).toString(); |
| 114 | } |
| 115 | |
alshabib | 8ca5390 | 2014-10-07 13:11:17 -0700 | [diff] [blame] | 116 | @Override |
| 117 | public int hashCode() { |
Thomas Vachuska | 9b2da21 | 2014-11-10 19:30:25 -0800 | [diff] [blame] | 118 | return Objects.hash(type(), subtype, mac); |
alshabib | 8ca5390 | 2014-10-07 13:11:17 -0700 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | @Override |
| 122 | public boolean equals(Object obj) { |
| 123 | if (this == obj) { |
| 124 | return true; |
| 125 | } |
| 126 | if (obj instanceof ModEtherInstruction) { |
| 127 | ModEtherInstruction that = (ModEtherInstruction) obj; |
| 128 | return Objects.equals(mac, that.mac) && |
alshabib | 2020b89 | 2014-10-20 15:47:08 -0700 | [diff] [blame] | 129 | Objects.equals(this.type(), that.type()) && |
alshabib | 8ca5390 | 2014-10-07 13:11:17 -0700 | [diff] [blame] | 130 | Objects.equals(subtype, that.subtype); |
| 131 | |
| 132 | } |
| 133 | return false; |
| 134 | } |
| 135 | |
alshabib | 99b8fdc | 2014-09-25 14:30:22 -0700 | [diff] [blame] | 136 | |
alshabib | 55a55d9 | 2014-09-16 11:59:31 -0700 | [diff] [blame] | 137 | } |
| 138 | |
Praseed Balakrishnan | 8c67d17 | 2014-11-10 10:15:41 -0800 | [diff] [blame] | 139 | public static final class PushHeaderInstructions extends |
| 140 | L2ModificationInstruction { |
| 141 | |
| 142 | private final L2SubType subtype; |
Yuta HIGUCHI | 32a53c5 | 2015-02-08 01:25:40 -0800 | [diff] [blame] | 143 | private final short ethernetType; // uint16_t |
Praseed Balakrishnan | 8c67d17 | 2014-11-10 10:15:41 -0800 | [diff] [blame] | 144 | |
Yuta HIGUCHI | 32a53c5 | 2015-02-08 01:25:40 -0800 | [diff] [blame] | 145 | PushHeaderInstructions(L2SubType subType, short ethernetType) { |
Praseed Balakrishnan | 8c67d17 | 2014-11-10 10:15:41 -0800 | [diff] [blame] | 146 | this.subtype = subType; |
| 147 | this.ethernetType = ethernetType; |
| 148 | } |
| 149 | |
Yuta HIGUCHI | 32a53c5 | 2015-02-08 01:25:40 -0800 | [diff] [blame] | 150 | public int ethernetType() { |
| 151 | return Short.toUnsignedInt(ethernetType); |
Praseed Balakrishnan | 8c67d17 | 2014-11-10 10:15:41 -0800 | [diff] [blame] | 152 | } |
| 153 | |
| 154 | @Override |
| 155 | public L2SubType subtype() { |
| 156 | return this.subtype; |
| 157 | } |
| 158 | |
| 159 | @Override |
| 160 | public String toString() { |
Yuta HIGUCHI | 32a53c5 | 2015-02-08 01:25:40 -0800 | [diff] [blame] | 161 | return toStringHelper(subtype().toString()) |
| 162 | .add("ethernetType", String.format("0x%04x", ethernetType())) |
| 163 | .toString(); |
Praseed Balakrishnan | 8c67d17 | 2014-11-10 10:15:41 -0800 | [diff] [blame] | 164 | } |
| 165 | |
| 166 | @Override |
| 167 | public int hashCode() { |
Yuta HIGUCHI | 32a53c5 | 2015-02-08 01:25:40 -0800 | [diff] [blame] | 168 | return Objects.hash(type(), subtype, ethernetType); |
Praseed Balakrishnan | 8c67d17 | 2014-11-10 10:15:41 -0800 | [diff] [blame] | 169 | } |
| 170 | |
| 171 | @Override |
| 172 | public boolean equals(Object obj) { |
| 173 | if (this == obj) { |
| 174 | return true; |
| 175 | } |
| 176 | if (obj instanceof PushHeaderInstructions) { |
| 177 | PushHeaderInstructions that = (PushHeaderInstructions) obj; |
Yuta HIGUCHI | 32a53c5 | 2015-02-08 01:25:40 -0800 | [diff] [blame] | 178 | return Objects.equals(subtype, that.subtype) && |
| 179 | Objects.equals(this.ethernetType, that.ethernetType); |
Praseed Balakrishnan | 8c67d17 | 2014-11-10 10:15:41 -0800 | [diff] [blame] | 180 | } |
| 181 | return false; |
| 182 | } |
| 183 | } |
| 184 | |
| 185 | |
| 186 | |
alshabib | 55a55d9 | 2014-09-16 11:59:31 -0700 | [diff] [blame] | 187 | /** |
alshabib | 55a55d9 | 2014-09-16 11:59:31 -0700 | [diff] [blame] | 188 | * Represents a VLAN id modification instruction. |
| 189 | */ |
| 190 | public static final class ModVlanIdInstruction extends L2ModificationInstruction { |
| 191 | |
Ray Milkey | 7808105 | 2014-11-05 10:38:12 -0800 | [diff] [blame] | 192 | private final VlanId vlanId; |
alshabib | 55a55d9 | 2014-09-16 11:59:31 -0700 | [diff] [blame] | 193 | |
Ayaka Koshibe | a9c199f | 2014-09-16 16:21:40 -0700 | [diff] [blame] | 194 | public ModVlanIdInstruction(VlanId vlanId) { |
alshabib | 55a55d9 | 2014-09-16 11:59:31 -0700 | [diff] [blame] | 195 | this.vlanId = vlanId; |
| 196 | } |
| 197 | |
| 198 | @Override |
alshabib | 35edb1a | 2014-09-16 17:44:44 -0700 | [diff] [blame] | 199 | public L2SubType subtype() { |
alshabib | 7410fea | 2014-09-16 13:48:39 -0700 | [diff] [blame] | 200 | return L2SubType.VLAN_ID; |
alshabib | 55a55d9 | 2014-09-16 11:59:31 -0700 | [diff] [blame] | 201 | } |
| 202 | |
Ayaka Koshibe | a9c199f | 2014-09-16 16:21:40 -0700 | [diff] [blame] | 203 | public VlanId vlanId() { |
alshabib | 55a55d9 | 2014-09-16 11:59:31 -0700 | [diff] [blame] | 204 | return this.vlanId; |
| 205 | } |
| 206 | |
alshabib | 99b8fdc | 2014-09-25 14:30:22 -0700 | [diff] [blame] | 207 | @Override |
| 208 | public String toString() { |
| 209 | return toStringHelper(subtype().toString()) |
| 210 | .add("id", vlanId).toString(); |
| 211 | } |
| 212 | |
alshabib | 8ca5390 | 2014-10-07 13:11:17 -0700 | [diff] [blame] | 213 | @Override |
| 214 | public int hashCode() { |
Thomas Vachuska | 9b2da21 | 2014-11-10 19:30:25 -0800 | [diff] [blame] | 215 | return Objects.hash(type(), subtype(), vlanId); |
alshabib | 8ca5390 | 2014-10-07 13:11:17 -0700 | [diff] [blame] | 216 | } |
| 217 | |
| 218 | @Override |
| 219 | public boolean equals(Object obj) { |
| 220 | if (this == obj) { |
| 221 | return true; |
| 222 | } |
| 223 | if (obj instanceof ModVlanIdInstruction) { |
| 224 | ModVlanIdInstruction that = (ModVlanIdInstruction) obj; |
alshabib | 2020b89 | 2014-10-20 15:47:08 -0700 | [diff] [blame] | 225 | return Objects.equals(vlanId, that.vlanId) && |
| 226 | Objects.equals(this.type(), that.type()) && |
| 227 | Objects.equals(this.subtype(), that.subtype()); |
alshabib | 8ca5390 | 2014-10-07 13:11:17 -0700 | [diff] [blame] | 228 | |
| 229 | } |
| 230 | return false; |
| 231 | } |
| 232 | |
| 233 | |
alshabib | 55a55d9 | 2014-09-16 11:59:31 -0700 | [diff] [blame] | 234 | } |
| 235 | |
alshabib | 7410fea | 2014-09-16 13:48:39 -0700 | [diff] [blame] | 236 | /** |
| 237 | * Represents a VLAN PCP modification instruction. |
| 238 | */ |
| 239 | public static final class ModVlanPcpInstruction extends L2ModificationInstruction { |
| 240 | |
Ray Milkey | 7808105 | 2014-11-05 10:38:12 -0800 | [diff] [blame] | 241 | private final Byte vlanPcp; |
alshabib | 7410fea | 2014-09-16 13:48:39 -0700 | [diff] [blame] | 242 | |
| 243 | public ModVlanPcpInstruction(Byte vlanPcp) { |
| 244 | this.vlanPcp = vlanPcp; |
| 245 | } |
| 246 | |
| 247 | @Override |
alshabib | 35edb1a | 2014-09-16 17:44:44 -0700 | [diff] [blame] | 248 | public L2SubType subtype() { |
alshabib | 7410fea | 2014-09-16 13:48:39 -0700 | [diff] [blame] | 249 | return L2SubType.VLAN_PCP; |
| 250 | } |
| 251 | |
| 252 | public Byte vlanPcp() { |
| 253 | return this.vlanPcp; |
| 254 | } |
| 255 | |
alshabib | 99b8fdc | 2014-09-25 14:30:22 -0700 | [diff] [blame] | 256 | @Override |
| 257 | public String toString() { |
| 258 | return toStringHelper(subtype().toString()) |
| 259 | .add("pcp", Long.toHexString(vlanPcp)).toString(); |
| 260 | } |
| 261 | |
alshabib | 8ca5390 | 2014-10-07 13:11:17 -0700 | [diff] [blame] | 262 | @Override |
| 263 | public int hashCode() { |
Thomas Vachuska | 9b2da21 | 2014-11-10 19:30:25 -0800 | [diff] [blame] | 264 | return Objects.hash(type(), subtype(), vlanPcp); |
alshabib | 8ca5390 | 2014-10-07 13:11:17 -0700 | [diff] [blame] | 265 | } |
| 266 | |
| 267 | @Override |
| 268 | public boolean equals(Object obj) { |
| 269 | if (this == obj) { |
| 270 | return true; |
| 271 | } |
| 272 | if (obj instanceof ModVlanPcpInstruction) { |
| 273 | ModVlanPcpInstruction that = (ModVlanPcpInstruction) obj; |
alshabib | 2020b89 | 2014-10-20 15:47:08 -0700 | [diff] [blame] | 274 | return Objects.equals(vlanPcp, that.vlanPcp) && |
| 275 | Objects.equals(this.type(), that.type()) && |
| 276 | Objects.equals(this.subtype(), that.subtype()); |
alshabib | 8ca5390 | 2014-10-07 13:11:17 -0700 | [diff] [blame] | 277 | |
| 278 | } |
| 279 | return false; |
| 280 | } |
| 281 | |
alshabib | 7410fea | 2014-09-16 13:48:39 -0700 | [diff] [blame] | 282 | } |
| 283 | |
alshabib | 55a55d9 | 2014-09-16 11:59:31 -0700 | [diff] [blame] | 284 | |
Praseed Balakrishnan | 8c67d17 | 2014-11-10 10:15:41 -0800 | [diff] [blame] | 285 | /** |
| 286 | * Represents a MPLS label modification. |
| 287 | */ |
| 288 | public static final class ModMplsLabelInstruction extends |
| 289 | L2ModificationInstruction { |
| 290 | |
Michele Santuari | 4b6019e | 2014-12-19 11:31:45 +0100 | [diff] [blame] | 291 | private final MplsLabel mplsLabel; |
Praseed Balakrishnan | 8c67d17 | 2014-11-10 10:15:41 -0800 | [diff] [blame] | 292 | |
Michele Santuari | 4b6019e | 2014-12-19 11:31:45 +0100 | [diff] [blame] | 293 | public ModMplsLabelInstruction(MplsLabel mplsLabel) { |
Praseed Balakrishnan | 8c67d17 | 2014-11-10 10:15:41 -0800 | [diff] [blame] | 294 | this.mplsLabel = mplsLabel; |
| 295 | } |
| 296 | |
| 297 | public Integer label() { |
Michele Santuari | 4b6019e | 2014-12-19 11:31:45 +0100 | [diff] [blame] | 298 | return mplsLabel.toInt(); |
Praseed Balakrishnan | 8c67d17 | 2014-11-10 10:15:41 -0800 | [diff] [blame] | 299 | } |
| 300 | |
| 301 | @Override |
| 302 | public L2SubType subtype() { |
| 303 | return L2SubType.MPLS_LABEL; |
| 304 | } |
| 305 | |
| 306 | @Override |
| 307 | public String toString() { |
Pavlin Radoslavov | 3ebe170 | 2015-02-17 09:53:17 -0800 | [diff] [blame] | 308 | return toStringHelper(subtype().toString()) |
Michele Santuari | 4b6019e | 2014-12-19 11:31:45 +0100 | [diff] [blame] | 309 | .add("mpls", mplsLabel).toString(); |
Praseed Balakrishnan | 8c67d17 | 2014-11-10 10:15:41 -0800 | [diff] [blame] | 310 | } |
| 311 | |
| 312 | @Override |
| 313 | public int hashCode() { |
Yuta HIGUCHI | cca5e72 | 2015-02-05 14:00:41 -0800 | [diff] [blame] | 314 | return Objects.hash(type(), subtype(), mplsLabel); |
Praseed Balakrishnan | 8c67d17 | 2014-11-10 10:15:41 -0800 | [diff] [blame] | 315 | } |
| 316 | |
| 317 | @Override |
| 318 | public boolean equals(Object obj) { |
| 319 | if (this == obj) { |
| 320 | return true; |
| 321 | } |
| 322 | if (obj instanceof ModMplsLabelInstruction) { |
| 323 | ModMplsLabelInstruction that = (ModMplsLabelInstruction) obj; |
| 324 | return Objects.equals(mplsLabel, that.mplsLabel) && |
| 325 | Objects.equals(this.type(), that.type()); |
| 326 | |
| 327 | |
| 328 | } |
| 329 | return false; |
| 330 | } |
| 331 | } |
sangho | 3f97a17d | 2015-01-29 22:56:29 -0800 | [diff] [blame] | 332 | |
| 333 | /** |
Yuta HIGUCHI | cca5e72 | 2015-02-05 14:00:41 -0800 | [diff] [blame] | 334 | * Represents a MPLS TTL modification. |
sangho | 3f97a17d | 2015-01-29 22:56:29 -0800 | [diff] [blame] | 335 | */ |
| 336 | public static final class ModMplsTtlInstruction extends |
| 337 | L2ModificationInstruction { |
| 338 | |
| 339 | public ModMplsTtlInstruction() { |
| 340 | } |
| 341 | |
| 342 | @Override |
| 343 | public L2SubType subtype() { |
| 344 | return L2SubType.DEC_MPLS_TTL; |
| 345 | } |
| 346 | |
| 347 | @Override |
| 348 | public String toString() { |
Yuta HIGUCHI | cca5e72 | 2015-02-05 14:00:41 -0800 | [diff] [blame] | 349 | return toStringHelper(subtype().toString()) |
| 350 | .toString(); |
sangho | 3f97a17d | 2015-01-29 22:56:29 -0800 | [diff] [blame] | 351 | } |
| 352 | |
| 353 | @Override |
| 354 | public int hashCode() { |
Yuta HIGUCHI | cca5e72 | 2015-02-05 14:00:41 -0800 | [diff] [blame] | 355 | return Objects.hash(type(), subtype()); |
sangho | 3f97a17d | 2015-01-29 22:56:29 -0800 | [diff] [blame] | 356 | } |
| 357 | |
| 358 | @Override |
| 359 | public boolean equals(Object obj) { |
| 360 | if (this == obj) { |
| 361 | return true; |
| 362 | } |
Yuta HIGUCHI | cca5e72 | 2015-02-05 14:00:41 -0800 | [diff] [blame] | 363 | if (obj instanceof ModMplsTtlInstruction) { |
| 364 | return true; |
sangho | 3f97a17d | 2015-01-29 22:56:29 -0800 | [diff] [blame] | 365 | } |
| 366 | return false; |
| 367 | } |
| 368 | } |
alshabib | 55a55d9 | 2014-09-16 11:59:31 -0700 | [diff] [blame] | 369 | } |