blob: 35acd16cc6b48879173ce7be0c36e7c9877f7061 [file] [log] [blame]
Thomas Vachuska83e090e2014-10-22 14:25:35 -07001/*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
alshabib55a55d92014-09-16 11:59:31 -070019package org.onlab.onos.net.flow.instructions;
20
alshabib99b8fdc2014-09-25 14:30:22 -070021import static com.google.common.base.MoreObjects.toStringHelper;
22
alshabib8ca53902014-10-07 13:11:17 -070023import java.util.Objects;
24
Ayaka Koshibea9c199f2014-09-16 16:21:40 -070025import org.onlab.packet.MacAddress;
26import org.onlab.packet.VlanId;
alshabib55a55d92014-09-16 11:59:31 -070027
28/**
29 * Abstraction of a single traffic treatment step.
alshabib55a55d92014-09-16 11:59:31 -070030 */
31public abstract class L2ModificationInstruction implements Instruction {
32
33 /**
34 * Represents the type of traffic treatment.
35 */
alshabib35edb1a2014-09-16 17:44:44 -070036 public enum L2SubType {
alshabib55a55d92014-09-16 11:59:31 -070037 /**
38 * Ether src modification.
39 */
alshabib99b8fdc2014-09-25 14:30:22 -070040 ETH_SRC,
alshabib55a55d92014-09-16 11:59:31 -070041
42 /**
43 * Ether dst modification.
44 */
alshabib99b8fdc2014-09-25 14:30:22 -070045 ETH_DST,
alshabib55a55d92014-09-16 11:59:31 -070046
47 /**
alshabib55a55d92014-09-16 11:59:31 -070048 * VLAN id modification.
49 */
50 VLAN_ID,
51
52 /**
53 * VLAN priority modification.
54 */
55 VLAN_PCP
56 }
57
58 // TODO: Create factory class 'Instructions' that will have various factory
59 // to create specific instructions.
60
alshabib35edb1a2014-09-16 17:44:44 -070061 public abstract L2SubType subtype();
alshabib55a55d92014-09-16 11:59:31 -070062
63 @Override
64 public Type type() {
alshabib35edb1a2014-09-16 17:44:44 -070065 return Type.L2MODIFICATION;
alshabib55a55d92014-09-16 11:59:31 -070066 }
67
68 /**
69 * Represents a L2 src/dst modification instruction.
70 */
71 public static final class ModEtherInstruction extends L2ModificationInstruction {
72
alshabib35edb1a2014-09-16 17:44:44 -070073 private final L2SubType subtype;
Ayaka Koshibea9c199f2014-09-16 16:21:40 -070074 private final MacAddress mac;
alshabib55a55d92014-09-16 11:59:31 -070075
alshabib64231f62014-09-16 17:58:36 -070076 public ModEtherInstruction(L2SubType subType, MacAddress addr) {
77
alshabib55a55d92014-09-16 11:59:31 -070078 this.subtype = subType;
79 this.mac = addr;
80 }
81
82 @Override
alshabib35edb1a2014-09-16 17:44:44 -070083 public L2SubType subtype() {
alshabib55a55d92014-09-16 11:59:31 -070084 return this.subtype;
85 }
86
Ayaka Koshibea9c199f2014-09-16 16:21:40 -070087 public MacAddress mac() {
alshabib55a55d92014-09-16 11:59:31 -070088 return this.mac;
89 }
90
alshabib99b8fdc2014-09-25 14:30:22 -070091 @Override
92 public String toString() {
93 return toStringHelper(subtype().toString())
94 .add("mac", mac).toString();
95 }
96
alshabib8ca53902014-10-07 13:11:17 -070097 @Override
98 public int hashCode() {
alshabib2020b892014-10-20 15:47:08 -070099 return Objects.hash(mac, type(), subtype);
alshabib8ca53902014-10-07 13:11:17 -0700100 }
101
102 @Override
103 public boolean equals(Object obj) {
104 if (this == obj) {
105 return true;
106 }
107 if (obj instanceof ModEtherInstruction) {
108 ModEtherInstruction that = (ModEtherInstruction) obj;
109 return Objects.equals(mac, that.mac) &&
alshabib2020b892014-10-20 15:47:08 -0700110 Objects.equals(this.type(), that.type()) &&
alshabib8ca53902014-10-07 13:11:17 -0700111 Objects.equals(subtype, that.subtype);
112
113 }
114 return false;
115 }
116
alshabib99b8fdc2014-09-25 14:30:22 -0700117
alshabib55a55d92014-09-16 11:59:31 -0700118 }
119
120 /**
alshabib55a55d92014-09-16 11:59:31 -0700121 * Represents a VLAN id modification instruction.
122 */
123 public static final class ModVlanIdInstruction extends L2ModificationInstruction {
124
Ayaka Koshibea9c199f2014-09-16 16:21:40 -0700125 public final VlanId vlanId;
alshabib55a55d92014-09-16 11:59:31 -0700126
Ayaka Koshibea9c199f2014-09-16 16:21:40 -0700127 public ModVlanIdInstruction(VlanId vlanId) {
alshabib55a55d92014-09-16 11:59:31 -0700128 this.vlanId = vlanId;
129 }
130
131 @Override
alshabib35edb1a2014-09-16 17:44:44 -0700132 public L2SubType subtype() {
alshabib7410fea2014-09-16 13:48:39 -0700133 return L2SubType.VLAN_ID;
alshabib55a55d92014-09-16 11:59:31 -0700134 }
135
Ayaka Koshibea9c199f2014-09-16 16:21:40 -0700136 public VlanId vlanId() {
alshabib55a55d92014-09-16 11:59:31 -0700137 return this.vlanId;
138 }
139
alshabib99b8fdc2014-09-25 14:30:22 -0700140 @Override
141 public String toString() {
142 return toStringHelper(subtype().toString())
143 .add("id", vlanId).toString();
144 }
145
alshabib8ca53902014-10-07 13:11:17 -0700146 @Override
147 public int hashCode() {
alshabib2020b892014-10-20 15:47:08 -0700148 return Objects.hash(vlanId, type(), subtype());
alshabib8ca53902014-10-07 13:11:17 -0700149 }
150
151 @Override
152 public boolean equals(Object obj) {
153 if (this == obj) {
154 return true;
155 }
156 if (obj instanceof ModVlanIdInstruction) {
157 ModVlanIdInstruction that = (ModVlanIdInstruction) obj;
alshabib2020b892014-10-20 15:47:08 -0700158 return Objects.equals(vlanId, that.vlanId) &&
159 Objects.equals(this.type(), that.type()) &&
160 Objects.equals(this.subtype(), that.subtype());
alshabib8ca53902014-10-07 13:11:17 -0700161
162 }
163 return false;
164 }
165
166
alshabib55a55d92014-09-16 11:59:31 -0700167 }
168
alshabib7410fea2014-09-16 13:48:39 -0700169 /**
170 * Represents a VLAN PCP modification instruction.
171 */
172 public static final class ModVlanPcpInstruction extends L2ModificationInstruction {
173
174 public final Byte vlanPcp;
175
176 public ModVlanPcpInstruction(Byte vlanPcp) {
177 this.vlanPcp = vlanPcp;
178 }
179
180 @Override
alshabib35edb1a2014-09-16 17:44:44 -0700181 public L2SubType subtype() {
alshabib7410fea2014-09-16 13:48:39 -0700182 return L2SubType.VLAN_PCP;
183 }
184
185 public Byte vlanPcp() {
186 return this.vlanPcp;
187 }
188
alshabib99b8fdc2014-09-25 14:30:22 -0700189 @Override
190 public String toString() {
191 return toStringHelper(subtype().toString())
192 .add("pcp", Long.toHexString(vlanPcp)).toString();
193 }
194
alshabib8ca53902014-10-07 13:11:17 -0700195 @Override
196 public int hashCode() {
alshabib2020b892014-10-20 15:47:08 -0700197 return Objects.hash(vlanPcp, type(), subtype());
alshabib8ca53902014-10-07 13:11:17 -0700198 }
199
200 @Override
201 public boolean equals(Object obj) {
202 if (this == obj) {
203 return true;
204 }
205 if (obj instanceof ModVlanPcpInstruction) {
206 ModVlanPcpInstruction that = (ModVlanPcpInstruction) obj;
alshabib2020b892014-10-20 15:47:08 -0700207 return Objects.equals(vlanPcp, that.vlanPcp) &&
208 Objects.equals(this.type(), that.type()) &&
209 Objects.equals(this.subtype(), that.subtype());
alshabib8ca53902014-10-07 13:11:17 -0700210
211 }
212 return false;
213 }
214
alshabib7410fea2014-09-16 13:48:39 -0700215 }
216
alshabib55a55d92014-09-16 11:59:31 -0700217
218}