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 | */ |
alshabib | 55a55d9 | 2014-09-16 11:59:31 -0700 | [diff] [blame] | 16 | package org.onlab.onos.net.flow.instructions; |
| 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; |
| 23 | import org.onlab.packet.VlanId; |
alshabib | 55a55d9 | 2014-09-16 11:59:31 -0700 | [diff] [blame] | 24 | |
| 25 | /** |
| 26 | * Abstraction of a single traffic treatment step. |
alshabib | 55a55d9 | 2014-09-16 11:59:31 -0700 | [diff] [blame] | 27 | */ |
| 28 | public abstract class L2ModificationInstruction implements Instruction { |
| 29 | |
| 30 | /** |
| 31 | * Represents the type of traffic treatment. |
| 32 | */ |
alshabib | 35edb1a | 2014-09-16 17:44:44 -0700 | [diff] [blame] | 33 | public enum L2SubType { |
alshabib | 55a55d9 | 2014-09-16 11:59:31 -0700 | [diff] [blame] | 34 | /** |
| 35 | * Ether src modification. |
| 36 | */ |
alshabib | 99b8fdc | 2014-09-25 14:30:22 -0700 | [diff] [blame] | 37 | ETH_SRC, |
alshabib | 55a55d9 | 2014-09-16 11:59:31 -0700 | [diff] [blame] | 38 | |
| 39 | /** |
| 40 | * Ether dst modification. |
| 41 | */ |
alshabib | 99b8fdc | 2014-09-25 14:30:22 -0700 | [diff] [blame] | 42 | ETH_DST, |
alshabib | 55a55d9 | 2014-09-16 11:59:31 -0700 | [diff] [blame] | 43 | |
| 44 | /** |
alshabib | 55a55d9 | 2014-09-16 11:59:31 -0700 | [diff] [blame] | 45 | * VLAN id modification. |
| 46 | */ |
| 47 | VLAN_ID, |
| 48 | |
| 49 | /** |
| 50 | * VLAN priority modification. |
| 51 | */ |
| 52 | VLAN_PCP |
| 53 | } |
| 54 | |
| 55 | // TODO: Create factory class 'Instructions' that will have various factory |
| 56 | // to create specific instructions. |
| 57 | |
alshabib | 35edb1a | 2014-09-16 17:44:44 -0700 | [diff] [blame] | 58 | public abstract L2SubType subtype(); |
alshabib | 55a55d9 | 2014-09-16 11:59:31 -0700 | [diff] [blame] | 59 | |
| 60 | @Override |
| 61 | public Type type() { |
alshabib | 35edb1a | 2014-09-16 17:44:44 -0700 | [diff] [blame] | 62 | return Type.L2MODIFICATION; |
alshabib | 55a55d9 | 2014-09-16 11:59:31 -0700 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | /** |
| 66 | * Represents a L2 src/dst modification instruction. |
| 67 | */ |
| 68 | public static final class ModEtherInstruction extends L2ModificationInstruction { |
| 69 | |
alshabib | 35edb1a | 2014-09-16 17:44:44 -0700 | [diff] [blame] | 70 | private final L2SubType subtype; |
Ayaka Koshibe | a9c199f | 2014-09-16 16:21:40 -0700 | [diff] [blame] | 71 | private final MacAddress mac; |
alshabib | 55a55d9 | 2014-09-16 11:59:31 -0700 | [diff] [blame] | 72 | |
alshabib | 64231f6 | 2014-09-16 17:58:36 -0700 | [diff] [blame] | 73 | public ModEtherInstruction(L2SubType subType, MacAddress addr) { |
| 74 | |
alshabib | 55a55d9 | 2014-09-16 11:59:31 -0700 | [diff] [blame] | 75 | this.subtype = subType; |
| 76 | this.mac = addr; |
| 77 | } |
| 78 | |
| 79 | @Override |
alshabib | 35edb1a | 2014-09-16 17:44:44 -0700 | [diff] [blame] | 80 | public L2SubType subtype() { |
alshabib | 55a55d9 | 2014-09-16 11:59:31 -0700 | [diff] [blame] | 81 | return this.subtype; |
| 82 | } |
| 83 | |
Ayaka Koshibe | a9c199f | 2014-09-16 16:21:40 -0700 | [diff] [blame] | 84 | public MacAddress mac() { |
alshabib | 55a55d9 | 2014-09-16 11:59:31 -0700 | [diff] [blame] | 85 | return this.mac; |
| 86 | } |
| 87 | |
alshabib | 99b8fdc | 2014-09-25 14:30:22 -0700 | [diff] [blame] | 88 | @Override |
| 89 | public String toString() { |
| 90 | return toStringHelper(subtype().toString()) |
| 91 | .add("mac", mac).toString(); |
| 92 | } |
| 93 | |
alshabib | 8ca5390 | 2014-10-07 13:11:17 -0700 | [diff] [blame] | 94 | @Override |
| 95 | public int hashCode() { |
alshabib | 2020b89 | 2014-10-20 15:47:08 -0700 | [diff] [blame] | 96 | return Objects.hash(mac, type(), subtype); |
alshabib | 8ca5390 | 2014-10-07 13:11:17 -0700 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | @Override |
| 100 | public boolean equals(Object obj) { |
| 101 | if (this == obj) { |
| 102 | return true; |
| 103 | } |
| 104 | if (obj instanceof ModEtherInstruction) { |
| 105 | ModEtherInstruction that = (ModEtherInstruction) obj; |
| 106 | return Objects.equals(mac, that.mac) && |
alshabib | 2020b89 | 2014-10-20 15:47:08 -0700 | [diff] [blame] | 107 | Objects.equals(this.type(), that.type()) && |
alshabib | 8ca5390 | 2014-10-07 13:11:17 -0700 | [diff] [blame] | 108 | Objects.equals(subtype, that.subtype); |
| 109 | |
| 110 | } |
| 111 | return false; |
| 112 | } |
| 113 | |
alshabib | 99b8fdc | 2014-09-25 14:30:22 -0700 | [diff] [blame] | 114 | |
alshabib | 55a55d9 | 2014-09-16 11:59:31 -0700 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | /** |
alshabib | 55a55d9 | 2014-09-16 11:59:31 -0700 | [diff] [blame] | 118 | * Represents a VLAN id modification instruction. |
| 119 | */ |
| 120 | public static final class ModVlanIdInstruction extends L2ModificationInstruction { |
| 121 | |
Ayaka Koshibe | a9c199f | 2014-09-16 16:21:40 -0700 | [diff] [blame] | 122 | public final VlanId vlanId; |
alshabib | 55a55d9 | 2014-09-16 11:59:31 -0700 | [diff] [blame] | 123 | |
Ayaka Koshibe | a9c199f | 2014-09-16 16:21:40 -0700 | [diff] [blame] | 124 | public ModVlanIdInstruction(VlanId vlanId) { |
alshabib | 55a55d9 | 2014-09-16 11:59:31 -0700 | [diff] [blame] | 125 | this.vlanId = vlanId; |
| 126 | } |
| 127 | |
| 128 | @Override |
alshabib | 35edb1a | 2014-09-16 17:44:44 -0700 | [diff] [blame] | 129 | public L2SubType subtype() { |
alshabib | 7410fea | 2014-09-16 13:48:39 -0700 | [diff] [blame] | 130 | return L2SubType.VLAN_ID; |
alshabib | 55a55d9 | 2014-09-16 11:59:31 -0700 | [diff] [blame] | 131 | } |
| 132 | |
Ayaka Koshibe | a9c199f | 2014-09-16 16:21:40 -0700 | [diff] [blame] | 133 | public VlanId vlanId() { |
alshabib | 55a55d9 | 2014-09-16 11:59:31 -0700 | [diff] [blame] | 134 | return this.vlanId; |
| 135 | } |
| 136 | |
alshabib | 99b8fdc | 2014-09-25 14:30:22 -0700 | [diff] [blame] | 137 | @Override |
| 138 | public String toString() { |
| 139 | return toStringHelper(subtype().toString()) |
| 140 | .add("id", vlanId).toString(); |
| 141 | } |
| 142 | |
alshabib | 8ca5390 | 2014-10-07 13:11:17 -0700 | [diff] [blame] | 143 | @Override |
| 144 | public int hashCode() { |
alshabib | 2020b89 | 2014-10-20 15:47:08 -0700 | [diff] [blame] | 145 | return Objects.hash(vlanId, type(), subtype()); |
alshabib | 8ca5390 | 2014-10-07 13:11:17 -0700 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | @Override |
| 149 | public boolean equals(Object obj) { |
| 150 | if (this == obj) { |
| 151 | return true; |
| 152 | } |
| 153 | if (obj instanceof ModVlanIdInstruction) { |
| 154 | ModVlanIdInstruction that = (ModVlanIdInstruction) obj; |
alshabib | 2020b89 | 2014-10-20 15:47:08 -0700 | [diff] [blame] | 155 | return Objects.equals(vlanId, that.vlanId) && |
| 156 | Objects.equals(this.type(), that.type()) && |
| 157 | Objects.equals(this.subtype(), that.subtype()); |
alshabib | 8ca5390 | 2014-10-07 13:11:17 -0700 | [diff] [blame] | 158 | |
| 159 | } |
| 160 | return false; |
| 161 | } |
| 162 | |
| 163 | |
alshabib | 55a55d9 | 2014-09-16 11:59:31 -0700 | [diff] [blame] | 164 | } |
| 165 | |
alshabib | 7410fea | 2014-09-16 13:48:39 -0700 | [diff] [blame] | 166 | /** |
| 167 | * Represents a VLAN PCP modification instruction. |
| 168 | */ |
| 169 | public static final class ModVlanPcpInstruction extends L2ModificationInstruction { |
| 170 | |
| 171 | public final Byte vlanPcp; |
| 172 | |
| 173 | public ModVlanPcpInstruction(Byte vlanPcp) { |
| 174 | this.vlanPcp = vlanPcp; |
| 175 | } |
| 176 | |
| 177 | @Override |
alshabib | 35edb1a | 2014-09-16 17:44:44 -0700 | [diff] [blame] | 178 | public L2SubType subtype() { |
alshabib | 7410fea | 2014-09-16 13:48:39 -0700 | [diff] [blame] | 179 | return L2SubType.VLAN_PCP; |
| 180 | } |
| 181 | |
| 182 | public Byte vlanPcp() { |
| 183 | return this.vlanPcp; |
| 184 | } |
| 185 | |
alshabib | 99b8fdc | 2014-09-25 14:30:22 -0700 | [diff] [blame] | 186 | @Override |
| 187 | public String toString() { |
| 188 | return toStringHelper(subtype().toString()) |
| 189 | .add("pcp", Long.toHexString(vlanPcp)).toString(); |
| 190 | } |
| 191 | |
alshabib | 8ca5390 | 2014-10-07 13:11:17 -0700 | [diff] [blame] | 192 | @Override |
| 193 | public int hashCode() { |
alshabib | 2020b89 | 2014-10-20 15:47:08 -0700 | [diff] [blame] | 194 | return Objects.hash(vlanPcp, type(), subtype()); |
alshabib | 8ca5390 | 2014-10-07 13:11:17 -0700 | [diff] [blame] | 195 | } |
| 196 | |
| 197 | @Override |
| 198 | public boolean equals(Object obj) { |
| 199 | if (this == obj) { |
| 200 | return true; |
| 201 | } |
| 202 | if (obj instanceof ModVlanPcpInstruction) { |
| 203 | ModVlanPcpInstruction that = (ModVlanPcpInstruction) obj; |
alshabib | 2020b89 | 2014-10-20 15:47:08 -0700 | [diff] [blame] | 204 | return Objects.equals(vlanPcp, that.vlanPcp) && |
| 205 | Objects.equals(this.type(), that.type()) && |
| 206 | Objects.equals(this.subtype(), that.subtype()); |
alshabib | 8ca5390 | 2014-10-07 13:11:17 -0700 | [diff] [blame] | 207 | |
| 208 | } |
| 209 | return false; |
| 210 | } |
| 211 | |
alshabib | 7410fea | 2014-09-16 13:48:39 -0700 | [diff] [blame] | 212 | } |
| 213 | |
alshabib | 55a55d9 | 2014-09-16 11:59:31 -0700 | [diff] [blame] | 214 | |
| 215 | } |