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() { |
| 81 | return Objects.hash(mac, subtype); |
| 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) && |
| 92 | Objects.equals(subtype, that.subtype); |
| 93 | |
| 94 | } |
| 95 | return false; |
| 96 | } |
| 97 | |
alshabib | 99b8fdc | 2014-09-25 14:30:22 -0700 | [diff] [blame] | 98 | |
alshabib | 55a55d9 | 2014-09-16 11:59:31 -0700 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | /** |
alshabib | 55a55d9 | 2014-09-16 11:59:31 -0700 | [diff] [blame] | 102 | * Represents a VLAN id modification instruction. |
| 103 | */ |
| 104 | public static final class ModVlanIdInstruction extends L2ModificationInstruction { |
| 105 | |
Ayaka Koshibe | a9c199f | 2014-09-16 16:21:40 -0700 | [diff] [blame] | 106 | public final VlanId vlanId; |
alshabib | 55a55d9 | 2014-09-16 11:59:31 -0700 | [diff] [blame] | 107 | |
Ayaka Koshibe | a9c199f | 2014-09-16 16:21:40 -0700 | [diff] [blame] | 108 | public ModVlanIdInstruction(VlanId vlanId) { |
alshabib | 55a55d9 | 2014-09-16 11:59:31 -0700 | [diff] [blame] | 109 | this.vlanId = vlanId; |
| 110 | } |
| 111 | |
| 112 | @Override |
alshabib | 35edb1a | 2014-09-16 17:44:44 -0700 | [diff] [blame] | 113 | public L2SubType subtype() { |
alshabib | 7410fea | 2014-09-16 13:48:39 -0700 | [diff] [blame] | 114 | return L2SubType.VLAN_ID; |
alshabib | 55a55d9 | 2014-09-16 11:59:31 -0700 | [diff] [blame] | 115 | } |
| 116 | |
Ayaka Koshibe | a9c199f | 2014-09-16 16:21:40 -0700 | [diff] [blame] | 117 | public VlanId vlanId() { |
alshabib | 55a55d9 | 2014-09-16 11:59:31 -0700 | [diff] [blame] | 118 | return this.vlanId; |
| 119 | } |
| 120 | |
alshabib | 99b8fdc | 2014-09-25 14:30:22 -0700 | [diff] [blame] | 121 | @Override |
| 122 | public String toString() { |
| 123 | return toStringHelper(subtype().toString()) |
| 124 | .add("id", vlanId).toString(); |
| 125 | } |
| 126 | |
alshabib | 8ca5390 | 2014-10-07 13:11:17 -0700 | [diff] [blame^] | 127 | @Override |
| 128 | public int hashCode() { |
| 129 | return Objects.hash(vlanId, subtype()); |
| 130 | } |
| 131 | |
| 132 | @Override |
| 133 | public boolean equals(Object obj) { |
| 134 | if (this == obj) { |
| 135 | return true; |
| 136 | } |
| 137 | if (obj instanceof ModVlanIdInstruction) { |
| 138 | ModVlanIdInstruction that = (ModVlanIdInstruction) obj; |
| 139 | return Objects.equals(vlanId, that.vlanId); |
| 140 | |
| 141 | } |
| 142 | return false; |
| 143 | } |
| 144 | |
| 145 | |
alshabib | 55a55d9 | 2014-09-16 11:59:31 -0700 | [diff] [blame] | 146 | } |
| 147 | |
alshabib | 7410fea | 2014-09-16 13:48:39 -0700 | [diff] [blame] | 148 | /** |
| 149 | * Represents a VLAN PCP modification instruction. |
| 150 | */ |
| 151 | public static final class ModVlanPcpInstruction extends L2ModificationInstruction { |
| 152 | |
| 153 | public final Byte vlanPcp; |
| 154 | |
| 155 | public ModVlanPcpInstruction(Byte vlanPcp) { |
| 156 | this.vlanPcp = vlanPcp; |
| 157 | } |
| 158 | |
| 159 | @Override |
alshabib | 35edb1a | 2014-09-16 17:44:44 -0700 | [diff] [blame] | 160 | public L2SubType subtype() { |
alshabib | 7410fea | 2014-09-16 13:48:39 -0700 | [diff] [blame] | 161 | return L2SubType.VLAN_PCP; |
| 162 | } |
| 163 | |
| 164 | public Byte vlanPcp() { |
| 165 | return this.vlanPcp; |
| 166 | } |
| 167 | |
alshabib | 99b8fdc | 2014-09-25 14:30:22 -0700 | [diff] [blame] | 168 | @Override |
| 169 | public String toString() { |
| 170 | return toStringHelper(subtype().toString()) |
| 171 | .add("pcp", Long.toHexString(vlanPcp)).toString(); |
| 172 | } |
| 173 | |
alshabib | 8ca5390 | 2014-10-07 13:11:17 -0700 | [diff] [blame^] | 174 | @Override |
| 175 | public int hashCode() { |
| 176 | return Objects.hash(vlanPcp, subtype()); |
| 177 | } |
| 178 | |
| 179 | @Override |
| 180 | public boolean equals(Object obj) { |
| 181 | if (this == obj) { |
| 182 | return true; |
| 183 | } |
| 184 | if (obj instanceof ModVlanPcpInstruction) { |
| 185 | ModVlanPcpInstruction that = (ModVlanPcpInstruction) obj; |
| 186 | return Objects.equals(vlanPcp, that.vlanPcp); |
| 187 | |
| 188 | } |
| 189 | return false; |
| 190 | } |
| 191 | |
alshabib | 7410fea | 2014-09-16 13:48:39 -0700 | [diff] [blame] | 192 | } |
| 193 | |
alshabib | 55a55d9 | 2014-09-16 11:59:31 -0700 | [diff] [blame] | 194 | |
| 195 | } |