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