blob: 2152532cfd85fb4f1d82604cd4f02424488d2527 [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() {
alshabib2020b892014-10-20 15:47:08 -070081 return Objects.hash(mac, type(), subtype);
alshabib8ca53902014-10-07 13:11:17 -070082 }
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) &&
alshabib2020b892014-10-20 15:47:08 -070092 Objects.equals(this.type(), that.type()) &&
alshabib8ca53902014-10-07 13:11:17 -070093 Objects.equals(subtype, that.subtype);
94
95 }
96 return false;
97 }
98
alshabib99b8fdc2014-09-25 14:30:22 -070099
alshabib55a55d92014-09-16 11:59:31 -0700100 }
101
102 /**
alshabib55a55d92014-09-16 11:59:31 -0700103 * Represents a VLAN id modification instruction.
104 */
105 public static final class ModVlanIdInstruction extends L2ModificationInstruction {
106
Ayaka Koshibea9c199f2014-09-16 16:21:40 -0700107 public final VlanId vlanId;
alshabib55a55d92014-09-16 11:59:31 -0700108
Ayaka Koshibea9c199f2014-09-16 16:21:40 -0700109 public ModVlanIdInstruction(VlanId vlanId) {
alshabib55a55d92014-09-16 11:59:31 -0700110 this.vlanId = vlanId;
111 }
112
113 @Override
alshabib35edb1a2014-09-16 17:44:44 -0700114 public L2SubType subtype() {
alshabib7410fea2014-09-16 13:48:39 -0700115 return L2SubType.VLAN_ID;
alshabib55a55d92014-09-16 11:59:31 -0700116 }
117
Ayaka Koshibea9c199f2014-09-16 16:21:40 -0700118 public VlanId vlanId() {
alshabib55a55d92014-09-16 11:59:31 -0700119 return this.vlanId;
120 }
121
alshabib99b8fdc2014-09-25 14:30:22 -0700122 @Override
123 public String toString() {
124 return toStringHelper(subtype().toString())
125 .add("id", vlanId).toString();
126 }
127
alshabib8ca53902014-10-07 13:11:17 -0700128 @Override
129 public int hashCode() {
alshabib2020b892014-10-20 15:47:08 -0700130 return Objects.hash(vlanId, type(), subtype());
alshabib8ca53902014-10-07 13:11:17 -0700131 }
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;
alshabib2020b892014-10-20 15:47:08 -0700140 return Objects.equals(vlanId, that.vlanId) &&
141 Objects.equals(this.type(), that.type()) &&
142 Objects.equals(this.subtype(), that.subtype());
alshabib8ca53902014-10-07 13:11:17 -0700143
144 }
145 return false;
146 }
147
148
alshabib55a55d92014-09-16 11:59:31 -0700149 }
150
alshabib7410fea2014-09-16 13:48:39 -0700151 /**
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
alshabib35edb1a2014-09-16 17:44:44 -0700163 public L2SubType subtype() {
alshabib7410fea2014-09-16 13:48:39 -0700164 return L2SubType.VLAN_PCP;
165 }
166
167 public Byte vlanPcp() {
168 return this.vlanPcp;
169 }
170
alshabib99b8fdc2014-09-25 14:30:22 -0700171 @Override
172 public String toString() {
173 return toStringHelper(subtype().toString())
174 .add("pcp", Long.toHexString(vlanPcp)).toString();
175 }
176
alshabib8ca53902014-10-07 13:11:17 -0700177 @Override
178 public int hashCode() {
alshabib2020b892014-10-20 15:47:08 -0700179 return Objects.hash(vlanPcp, type(), subtype());
alshabib8ca53902014-10-07 13:11:17 -0700180 }
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;
alshabib2020b892014-10-20 15:47:08 -0700189 return Objects.equals(vlanPcp, that.vlanPcp) &&
190 Objects.equals(this.type(), that.type()) &&
191 Objects.equals(this.subtype(), that.subtype());
alshabib8ca53902014-10-07 13:11:17 -0700192
193 }
194 return false;
195 }
196
alshabib7410fea2014-09-16 13:48:39 -0700197 }
198
alshabib55a55d92014-09-16 11:59:31 -0700199
200}