blob: df4a695f266d0683952bf181792727f787d40919 [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 */
Brian O'Connorabafb502014-12-02 22:26:20 -080016package org.onosproject.net.flow.instructions;
alshabib55a55d92014-09-16 11:59:31 -070017
alshabib99b8fdc2014-09-25 14:30:22 -070018import static com.google.common.base.MoreObjects.toStringHelper;
alshabib55a55d92014-09-16 11:59:31 -070019import static com.google.common.base.Preconditions.checkNotNull;
Brian O'Connorabafb502014-12-02 22:26:20 -080020import static org.onosproject.net.flow.instructions.L2ModificationInstruction.*;
alshabib55a55d92014-09-16 11:59:31 -070021
alshabib8ca53902014-10-07 13:11:17 -070022import java.util.Objects;
23
sangho8995ac52015-02-04 11:29:03 -080024import org.onosproject.core.GroupId;
Brian O'Connorabafb502014-12-02 22:26:20 -080025import org.onosproject.net.PortNumber;
26import org.onosproject.net.flow.instructions.L0ModificationInstruction.L0SubType;
27import org.onosproject.net.flow.instructions.L0ModificationInstruction.ModLambdaInstruction;
28import org.onosproject.net.flow.instructions.L2ModificationInstruction.L2SubType;
29import org.onosproject.net.flow.instructions.L2ModificationInstruction.ModEtherInstruction;
30import org.onosproject.net.flow.instructions.L3ModificationInstruction.L3SubType;
31import org.onosproject.net.flow.instructions.L3ModificationInstruction.ModIPInstruction;
sangho3f97a17d2015-01-29 22:56:29 -080032import org.onosproject.net.flow.instructions.L3ModificationInstruction.ModTtlInstruction;
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -080033
34import org.onlab.packet.Ethernet;
Pavlin Radoslavov855ea2d2014-10-30 15:32:39 -070035import org.onlab.packet.IpAddress;
Ayaka Koshibea9c199f2014-09-16 16:21:40 -070036import org.onlab.packet.MacAddress;
37import org.onlab.packet.VlanId;
alshabib64231f62014-09-16 17:58:36 -070038
alshabib55a55d92014-09-16 11:59:31 -070039/**
40 * Factory class for creating various traffic treatment instructions.
41 */
42public final class Instructions {
43
44
45 // Ban construction
46 private Instructions() {}
47
48 /**
49 * Creates an output instruction using the specified port number. This can
50 * include logical ports such as CONTROLLER, FLOOD, etc.
51 *
52 * @param number port number
53 * @return output instruction
54 */
55 public static OutputInstruction createOutput(final PortNumber number) {
56 checkNotNull(number, "PortNumber cannot be null");
57 return new OutputInstruction(number);
58 }
59
60 /**
61 * Creates a drop instruction.
62 * @return drop instruction
63 */
64 public static DropInstruction createDrop() {
65 return new DropInstruction();
66 }
67
68 /**
sangho8995ac52015-02-04 11:29:03 -080069 * Creates a group instruction.
70 *
71 * @param groupId Group Id
72 * @return group instruction
73 */
74 public static GroupInstruction createGroup(final GroupId groupId) {
75 checkNotNull(groupId, "GroupId cannot be null");
76 return new GroupInstruction(groupId);
77 }
78
79 /**
Marc De Leenheer49087752014-10-23 13:54:09 -070080 * Creates a l0 modification.
81 * @param lambda the lambda to modify to.
82 * @return a l0 modification
83 */
84 public static L0ModificationInstruction modL0Lambda(short lambda) {
85 checkNotNull(lambda, "L0 lambda cannot be null");
86 return new ModLambdaInstruction(L0SubType.LAMBDA, lambda);
87 }
88
89 /**
alshabib55a55d92014-09-16 11:59:31 -070090 * Creates a l2 src modification.
91 * @param addr the mac address to modify to.
92 * @return a l2 modification
93 */
Ayaka Koshibea9c199f2014-09-16 16:21:40 -070094 public static L2ModificationInstruction modL2Src(MacAddress addr) {
alshabib55a55d92014-09-16 11:59:31 -070095 checkNotNull(addr, "Src l2 address cannot be null");
alshabib99b8fdc2014-09-25 14:30:22 -070096 return new ModEtherInstruction(L2SubType.ETH_SRC, addr);
alshabib55a55d92014-09-16 11:59:31 -070097 }
98
99 /**
100 * Creates a L2 dst modification.
101 * @param addr the mac address to modify to.
102 * @return a L2 modification
103 */
Ayaka Koshibea9c199f2014-09-16 16:21:40 -0700104 public static L2ModificationInstruction modL2Dst(MacAddress addr) {
alshabib55a55d92014-09-16 11:59:31 -0700105 checkNotNull(addr, "Dst l2 address cannot be null");
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -0800106 return new ModEtherInstruction(L2SubType.ETH_DST, addr);
alshabib55a55d92014-09-16 11:59:31 -0700107 }
108
109 /**
alshabib7410fea2014-09-16 13:48:39 -0700110 * Creates a Vlan id modification.
111 * @param vlanId the vlan id to modify to.
112 * @return a L2 modification
113 */
Ayaka Koshibea9c199f2014-09-16 16:21:40 -0700114 public static L2ModificationInstruction modVlanId(VlanId vlanId) {
alshabib55a55d92014-09-16 11:59:31 -0700115 checkNotNull(vlanId, "VLAN id cannot be null");
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -0800116 return new ModVlanIdInstruction(vlanId);
alshabib55a55d92014-09-16 11:59:31 -0700117 }
118
alshabib7410fea2014-09-16 13:48:39 -0700119 /**
120 * Creates a Vlan pcp modification.
121 * @param vlanPcp the pcp to modify to.
122 * @return a L2 modification
123 */
124 public static L2ModificationInstruction modVlanPcp(Byte vlanPcp) {
125 checkNotNull(vlanPcp, "VLAN Pcp cannot be null");
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -0800126 return new ModVlanPcpInstruction(vlanPcp);
alshabib7410fea2014-09-16 13:48:39 -0700127 }
128
129 /**
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -0800130 * Creates a MPLS label modification.
131 * @param mplsLabel to set.
132 * @return a L2 Modification
133 */
134 public static L2ModificationInstruction modMplsLabel(Integer mplsLabel) {
135 checkNotNull(mplsLabel, "MPLS label cannot be null");
136 return new ModMplsLabelInstruction(mplsLabel);
137 }
sangho3f97a17d2015-01-29 22:56:29 -0800138
139 /**
140 * Creates a MPLS TTL modification.
141 *
142 * @return a L2 Modification
143 */
144 public static L2ModificationInstruction decMplsTtl() {
145 return new ModMplsTtlInstruction();
146 }
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -0800147 /**
alshabib7410fea2014-09-16 13:48:39 -0700148 * Creates a L3 src modification.
149 * @param addr the ip address to modify to.
150 * @return a L3 modification
151 */
Pavlin Radoslavov855ea2d2014-10-30 15:32:39 -0700152 public static L3ModificationInstruction modL3Src(IpAddress addr) {
alshabib7410fea2014-09-16 13:48:39 -0700153 checkNotNull(addr, "Src l3 address cannot be null");
alshabib99b8fdc2014-09-25 14:30:22 -0700154 return new ModIPInstruction(L3SubType.IP_SRC, addr);
alshabib7410fea2014-09-16 13:48:39 -0700155 }
156
157 /**
158 * Creates a L3 dst modification.
159 * @param addr the ip address to modify to.
160 * @return a L3 modification
161 */
Pavlin Radoslavov855ea2d2014-10-30 15:32:39 -0700162 public static L3ModificationInstruction modL3Dst(IpAddress addr) {
alshabib7410fea2014-09-16 13:48:39 -0700163 checkNotNull(addr, "Dst l3 address cannot be null");
alshabib99b8fdc2014-09-25 14:30:22 -0700164 return new ModIPInstruction(L3SubType.IP_DST, addr);
alshabib7410fea2014-09-16 13:48:39 -0700165 }
166
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -0800167 /**
sangho3f97a17d2015-01-29 22:56:29 -0800168 * Creates a L3 TTL modification.
169 * @return a L3 modification
170 */
171 public static L3ModificationInstruction decNwTtl() {
172 return new ModTtlInstruction(L3SubType.DEC_TTL);
173 }
174
175 /**
176 * Creates a L3 TTL modification.
177 * @return a L3 modification
178 */
179 public static L3ModificationInstruction copyTtlOut() {
180 return new ModTtlInstruction(L3SubType.TTL_OUT);
181 }
182
183 /**
184 * Creates a L3 TTL modification.
185 * @return a L3 modification
186 */
187 public static L3ModificationInstruction copyTtlIn() {
188 return new ModTtlInstruction(L3SubType.TTL_IN);
189 }
190
191 /**
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -0800192 * Creates a mpls header instruction.
193 * @return a L2 modification.
194 */
195 public static Instruction pushMpls() {
196 return new PushHeaderInstructions(L2SubType.MPLS_PUSH,
197 new Ethernet().setEtherType(Ethernet.MPLS_UNICAST));
198 }
199
200 /**
201 * Creates a mpls header instruction.
202 * @return a L2 modification.
203 */
204 public static Instruction popMpls() {
205 return new PushHeaderInstructions(L2SubType.MPLS_POP,
206 new Ethernet().setEtherType(Ethernet.MPLS_UNICAST));
207 }
alshabib7410fea2014-09-16 13:48:39 -0700208
sangho3f97a17d2015-01-29 22:56:29 -0800209 /**
210 * Creates a mpls header instruction.
211 *
212 * @param etherType Ethernet type to set
213 * @return a L2 modification.
214 */
215 public static Instruction popMpls(Short etherType) {
216 checkNotNull(etherType, "Ethernet type cannot be null");
217 return new PushHeaderInstructions(L2SubType.MPLS_POP,
218 new Ethernet().setEtherType(etherType));
219 }
220
alshabib55a55d92014-09-16 11:59:31 -0700221 /*
sangho8995ac52015-02-04 11:29:03 -0800222 * Drop instructions
alshabib55a55d92014-09-16 11:59:31 -0700223 */
224
225 public static final class DropInstruction implements Instruction {
226 @Override
227 public Type type() {
228 return Type.DROP;
229 }
alshabib99b8fdc2014-09-25 14:30:22 -0700230
231 @Override
232 public String toString() {
233 return toStringHelper(type()).toString();
234
235 }
alshabib8ca53902014-10-07 13:11:17 -0700236
237 @Override
238 public int hashCode() {
239 return Objects.hash(type());
240 }
241
242 @Override
243 public boolean equals(Object obj) {
244 if (this == obj) {
245 return true;
246 }
247 if (obj instanceof DropInstruction) {
248 DropInstruction that = (DropInstruction) obj;
249 return Objects.equals(type(), that.type());
250
251 }
252 return false;
253 }
alshabib55a55d92014-09-16 11:59:31 -0700254 }
255
sangho8995ac52015-02-04 11:29:03 -0800256 /*
257 * Output Instruction
258 */
alshabib55a55d92014-09-16 11:59:31 -0700259
260 public static final class OutputInstruction implements Instruction {
261 private final PortNumber port;
262
263 private OutputInstruction(PortNumber port) {
264 this.port = port;
265 }
266
267 public PortNumber port() {
268 return port;
269 }
270
271 @Override
272 public Type type() {
273 return Type.OUTPUT;
274 }
alshabib99b8fdc2014-09-25 14:30:22 -0700275 @Override
276 public String toString() {
277 return toStringHelper(type().toString())
278 .add("port", port).toString();
279 }
alshabib8ca53902014-10-07 13:11:17 -0700280
281 @Override
282 public int hashCode() {
Thomas Vachuska9b2da212014-11-10 19:30:25 -0800283 return Objects.hash(type(), port);
alshabib8ca53902014-10-07 13:11:17 -0700284 }
285
286 @Override
287 public boolean equals(Object obj) {
288 if (this == obj) {
289 return true;
290 }
291 if (obj instanceof OutputInstruction) {
292 OutputInstruction that = (OutputInstruction) obj;
Yuta HIGUCHI4ce65292014-11-01 12:09:55 -0700293 return Objects.equals(port, that.port);
alshabib8ca53902014-10-07 13:11:17 -0700294
295 }
296 return false;
297 }
alshabib55a55d92014-09-16 11:59:31 -0700298 }
299
sangho8995ac52015-02-04 11:29:03 -0800300 /*
301 * Group Instruction
302 */
303
304 public static final class GroupInstruction implements Instruction {
305 private final GroupId groupId;
306
307 private GroupInstruction(GroupId groupId) {
308 this.groupId = groupId;
309 }
310
311 public GroupId groupId() {
312 return groupId;
313 }
314
315 @Override
316 public Type type() {
317 return Type.GROUP;
318 }
319 @Override
320 public String toString() {
321 return toStringHelper(type().toString())
322 .add("group ID", groupId.id()).toString();
323 }
324
325 @Override
326 public int hashCode() {
327 return Objects.hash(type(), groupId);
328 }
329
330 @Override
331 public boolean equals(Object obj) {
332 if (this == obj) {
333 return true;
334 }
335 if (obj instanceof GroupInstruction) {
336 GroupInstruction that = (GroupInstruction) obj;
337 return Objects.equals(groupId, that.groupId);
338
339 }
340 return false;
341 }
342 }
343
alshabib55a55d92014-09-16 11:59:31 -0700344}
alshabib8ca53902014-10-07 13:11:17 -0700345
346