blob: 20eaf6eeff7b268de99e4e7d68268b7434dfb562 [file] [log] [blame]
Thomas Vachuska83e090e2014-10-22 14:25:35 -07001/*
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07002 * Copyright 2014 Open Networking Laboratory
Thomas Vachuska83e090e2014-10-22 14:25:35 -07003 *
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07004 * 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 Vachuska83e090e2014-10-22 14:25:35 -07007 *
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07008 * 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 Vachuska83e090e2014-10-22 14:25:35 -070015 */
alshabib55a55d92014-09-16 11:59:31 -070016package org.onlab.onos.net.flow.instructions;
17
alshabib99b8fdc2014-09-25 14:30:22 -070018import static com.google.common.base.MoreObjects.toStringHelper;
19
alshabib8ca53902014-10-07 13:11:17 -070020import java.util.Objects;
21
Ayaka Koshibea9c199f2014-09-16 16:21:40 -070022import org.onlab.packet.MacAddress;
23import org.onlab.packet.VlanId;
alshabib55a55d92014-09-16 11:59:31 -070024
25/**
26 * Abstraction of a single traffic treatment step.
alshabib55a55d92014-09-16 11:59:31 -070027 */
28public abstract class L2ModificationInstruction implements Instruction {
29
30 /**
31 * Represents the type of traffic treatment.
32 */
alshabib35edb1a2014-09-16 17:44:44 -070033 public enum L2SubType {
alshabib55a55d92014-09-16 11:59:31 -070034 /**
35 * Ether src modification.
36 */
alshabib99b8fdc2014-09-25 14:30:22 -070037 ETH_SRC,
alshabib55a55d92014-09-16 11:59:31 -070038
39 /**
40 * Ether dst modification.
41 */
alshabib99b8fdc2014-09-25 14:30:22 -070042 ETH_DST,
alshabib55a55d92014-09-16 11:59:31 -070043
44 /**
alshabib55a55d92014-09-16 11:59:31 -070045 * 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
alshabib35edb1a2014-09-16 17:44:44 -070058 public abstract L2SubType subtype();
alshabib55a55d92014-09-16 11:59:31 -070059
60 @Override
61 public Type type() {
alshabib35edb1a2014-09-16 17:44:44 -070062 return Type.L2MODIFICATION;
alshabib55a55d92014-09-16 11:59:31 -070063 }
64
65 /**
66 * Represents a L2 src/dst modification instruction.
67 */
68 public static final class ModEtherInstruction extends L2ModificationInstruction {
69
alshabib35edb1a2014-09-16 17:44:44 -070070 private final L2SubType subtype;
Ayaka Koshibea9c199f2014-09-16 16:21:40 -070071 private final MacAddress mac;
alshabib55a55d92014-09-16 11:59:31 -070072
alshabib64231f62014-09-16 17:58:36 -070073 public ModEtherInstruction(L2SubType subType, MacAddress addr) {
74
alshabib55a55d92014-09-16 11:59:31 -070075 this.subtype = subType;
76 this.mac = addr;
77 }
78
79 @Override
alshabib35edb1a2014-09-16 17:44:44 -070080 public L2SubType subtype() {
alshabib55a55d92014-09-16 11:59:31 -070081 return this.subtype;
82 }
83
Ayaka Koshibea9c199f2014-09-16 16:21:40 -070084 public MacAddress mac() {
alshabib55a55d92014-09-16 11:59:31 -070085 return this.mac;
86 }
87
alshabib99b8fdc2014-09-25 14:30:22 -070088 @Override
89 public String toString() {
90 return toStringHelper(subtype().toString())
91 .add("mac", mac).toString();
92 }
93
alshabib8ca53902014-10-07 13:11:17 -070094 @Override
95 public int hashCode() {
Thomas Vachuska9b2da212014-11-10 19:30:25 -080096 return Objects.hash(type(), subtype, mac);
alshabib8ca53902014-10-07 13:11:17 -070097 }
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) &&
alshabib2020b892014-10-20 15:47:08 -0700107 Objects.equals(this.type(), that.type()) &&
alshabib8ca53902014-10-07 13:11:17 -0700108 Objects.equals(subtype, that.subtype);
109
110 }
111 return false;
112 }
113
alshabib99b8fdc2014-09-25 14:30:22 -0700114
alshabib55a55d92014-09-16 11:59:31 -0700115 }
116
117 /**
alshabib55a55d92014-09-16 11:59:31 -0700118 * Represents a VLAN id modification instruction.
119 */
120 public static final class ModVlanIdInstruction extends L2ModificationInstruction {
121
Ray Milkey78081052014-11-05 10:38:12 -0800122 private final VlanId vlanId;
alshabib55a55d92014-09-16 11:59:31 -0700123
Ayaka Koshibea9c199f2014-09-16 16:21:40 -0700124 public ModVlanIdInstruction(VlanId vlanId) {
alshabib55a55d92014-09-16 11:59:31 -0700125 this.vlanId = vlanId;
126 }
127
128 @Override
alshabib35edb1a2014-09-16 17:44:44 -0700129 public L2SubType subtype() {
alshabib7410fea2014-09-16 13:48:39 -0700130 return L2SubType.VLAN_ID;
alshabib55a55d92014-09-16 11:59:31 -0700131 }
132
Ayaka Koshibea9c199f2014-09-16 16:21:40 -0700133 public VlanId vlanId() {
alshabib55a55d92014-09-16 11:59:31 -0700134 return this.vlanId;
135 }
136
alshabib99b8fdc2014-09-25 14:30:22 -0700137 @Override
138 public String toString() {
139 return toStringHelper(subtype().toString())
140 .add("id", vlanId).toString();
141 }
142
alshabib8ca53902014-10-07 13:11:17 -0700143 @Override
144 public int hashCode() {
Thomas Vachuska9b2da212014-11-10 19:30:25 -0800145 return Objects.hash(type(), subtype(), vlanId);
alshabib8ca53902014-10-07 13:11:17 -0700146 }
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;
alshabib2020b892014-10-20 15:47:08 -0700155 return Objects.equals(vlanId, that.vlanId) &&
156 Objects.equals(this.type(), that.type()) &&
157 Objects.equals(this.subtype(), that.subtype());
alshabib8ca53902014-10-07 13:11:17 -0700158
159 }
160 return false;
161 }
162
163
alshabib55a55d92014-09-16 11:59:31 -0700164 }
165
alshabib7410fea2014-09-16 13:48:39 -0700166 /**
167 * Represents a VLAN PCP modification instruction.
168 */
169 public static final class ModVlanPcpInstruction extends L2ModificationInstruction {
170
Ray Milkey78081052014-11-05 10:38:12 -0800171 private final Byte vlanPcp;
alshabib7410fea2014-09-16 13:48:39 -0700172
173 public ModVlanPcpInstruction(Byte vlanPcp) {
174 this.vlanPcp = vlanPcp;
175 }
176
177 @Override
alshabib35edb1a2014-09-16 17:44:44 -0700178 public L2SubType subtype() {
alshabib7410fea2014-09-16 13:48:39 -0700179 return L2SubType.VLAN_PCP;
180 }
181
182 public Byte vlanPcp() {
183 return this.vlanPcp;
184 }
185
alshabib99b8fdc2014-09-25 14:30:22 -0700186 @Override
187 public String toString() {
188 return toStringHelper(subtype().toString())
189 .add("pcp", Long.toHexString(vlanPcp)).toString();
190 }
191
alshabib8ca53902014-10-07 13:11:17 -0700192 @Override
193 public int hashCode() {
Thomas Vachuska9b2da212014-11-10 19:30:25 -0800194 return Objects.hash(type(), subtype(), vlanPcp);
alshabib8ca53902014-10-07 13:11:17 -0700195 }
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;
alshabib2020b892014-10-20 15:47:08 -0700204 return Objects.equals(vlanPcp, that.vlanPcp) &&
205 Objects.equals(this.type(), that.type()) &&
206 Objects.equals(this.subtype(), that.subtype());
alshabib8ca53902014-10-07 13:11:17 -0700207
208 }
209 return false;
210 }
211
alshabib7410fea2014-09-16 13:48:39 -0700212 }
213
alshabib55a55d92014-09-16 11:59:31 -0700214
215}