blob: ce7e16b827cce54b3f6806de1b030a7e901a6f9d [file] [log] [blame]
alshabib55a55d92014-09-16 11:59:31 -07001package org.onlab.onos.net.flow.instructions;
2
alshabib99b8fdc2014-09-25 14:30:22 -07003import static com.google.common.base.MoreObjects.toStringHelper;
4
alshabib8ca53902014-10-07 13:11:17 -07005import java.util.Objects;
6
Ayaka Koshibea9c199f2014-09-16 16:21:40 -07007import org.onlab.packet.MacAddress;
8import org.onlab.packet.VlanId;
alshabib55a55d92014-09-16 11:59:31 -07009
10/**
11 * Abstraction of a single traffic treatment step.
alshabib55a55d92014-09-16 11:59:31 -070012 */
13public abstract class L2ModificationInstruction implements Instruction {
14
15 /**
16 * Represents the type of traffic treatment.
17 */
alshabib35edb1a2014-09-16 17:44:44 -070018 public enum L2SubType {
alshabib55a55d92014-09-16 11:59:31 -070019 /**
20 * Ether src modification.
21 */
alshabib99b8fdc2014-09-25 14:30:22 -070022 ETH_SRC,
alshabib55a55d92014-09-16 11:59:31 -070023
24 /**
25 * Ether dst modification.
26 */
alshabib99b8fdc2014-09-25 14:30:22 -070027 ETH_DST,
alshabib55a55d92014-09-16 11:59:31 -070028
29 /**
alshabib55a55d92014-09-16 11:59:31 -070030 * 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
alshabib35edb1a2014-09-16 17:44:44 -070043 public abstract L2SubType subtype();
alshabib55a55d92014-09-16 11:59:31 -070044
45 @Override
46 public Type type() {
alshabib35edb1a2014-09-16 17:44:44 -070047 return Type.L2MODIFICATION;
alshabib55a55d92014-09-16 11:59:31 -070048 }
49
50 /**
51 * Represents a L2 src/dst modification instruction.
52 */
53 public static final class ModEtherInstruction extends L2ModificationInstruction {
54
alshabib35edb1a2014-09-16 17:44:44 -070055 private final L2SubType subtype;
Ayaka Koshibea9c199f2014-09-16 16:21:40 -070056 private final MacAddress mac;
alshabib55a55d92014-09-16 11:59:31 -070057
alshabib64231f62014-09-16 17:58:36 -070058 public ModEtherInstruction(L2SubType subType, MacAddress addr) {
59
alshabib55a55d92014-09-16 11:59:31 -070060 this.subtype = subType;
61 this.mac = addr;
62 }
63
64 @Override
alshabib35edb1a2014-09-16 17:44:44 -070065 public L2SubType subtype() {
alshabib55a55d92014-09-16 11:59:31 -070066 return this.subtype;
67 }
68
Ayaka Koshibea9c199f2014-09-16 16:21:40 -070069 public MacAddress mac() {
alshabib55a55d92014-09-16 11:59:31 -070070 return this.mac;
71 }
72
alshabib99b8fdc2014-09-25 14:30:22 -070073 @Override
74 public String toString() {
75 return toStringHelper(subtype().toString())
76 .add("mac", mac).toString();
77 }
78
alshabib8ca53902014-10-07 13:11:17 -070079 @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
alshabib99b8fdc2014-09-25 14:30:22 -070098
alshabib55a55d92014-09-16 11:59:31 -070099 }
100
101 /**
alshabib55a55d92014-09-16 11:59:31 -0700102 * Represents a VLAN id modification instruction.
103 */
104 public static final class ModVlanIdInstruction extends L2ModificationInstruction {
105
Ayaka Koshibea9c199f2014-09-16 16:21:40 -0700106 public final VlanId vlanId;
alshabib55a55d92014-09-16 11:59:31 -0700107
Ayaka Koshibea9c199f2014-09-16 16:21:40 -0700108 public ModVlanIdInstruction(VlanId vlanId) {
alshabib55a55d92014-09-16 11:59:31 -0700109 this.vlanId = vlanId;
110 }
111
112 @Override
alshabib35edb1a2014-09-16 17:44:44 -0700113 public L2SubType subtype() {
alshabib7410fea2014-09-16 13:48:39 -0700114 return L2SubType.VLAN_ID;
alshabib55a55d92014-09-16 11:59:31 -0700115 }
116
Ayaka Koshibea9c199f2014-09-16 16:21:40 -0700117 public VlanId vlanId() {
alshabib55a55d92014-09-16 11:59:31 -0700118 return this.vlanId;
119 }
120
alshabib99b8fdc2014-09-25 14:30:22 -0700121 @Override
122 public String toString() {
123 return toStringHelper(subtype().toString())
124 .add("id", vlanId).toString();
125 }
126
alshabib8ca53902014-10-07 13:11:17 -0700127 @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
alshabib55a55d92014-09-16 11:59:31 -0700146 }
147
alshabib7410fea2014-09-16 13:48:39 -0700148 /**
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
alshabib35edb1a2014-09-16 17:44:44 -0700160 public L2SubType subtype() {
alshabib7410fea2014-09-16 13:48:39 -0700161 return L2SubType.VLAN_PCP;
162 }
163
164 public Byte vlanPcp() {
165 return this.vlanPcp;
166 }
167
alshabib99b8fdc2014-09-25 14:30:22 -0700168 @Override
169 public String toString() {
170 return toStringHelper(subtype().toString())
171 .add("pcp", Long.toHexString(vlanPcp)).toString();
172 }
173
alshabib8ca53902014-10-07 13:11:17 -0700174 @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
alshabib7410fea2014-09-16 13:48:39 -0700192 }
193
alshabib55a55d92014-09-16 11:59:31 -0700194
195}