blob: 8141c9aeefa6b4d6fceb82dc2a7dbd24b8cefaa2 [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,
Yuta HIGUCHI32a53c52015-02-08 01:25:40 -0800197 Ethernet.MPLS_UNICAST);
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -0800198 }
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,
Yuta HIGUCHI32a53c52015-02-08 01:25:40 -0800206 Ethernet.MPLS_UNICAST);
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -0800207 }
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");
Yuta HIGUCHI32a53c52015-02-08 01:25:40 -0800217 return new PushHeaderInstructions(L2SubType.MPLS_POP, etherType);
sangho3f97a17d2015-01-29 22:56:29 -0800218 }
219
alshabib55a55d92014-09-16 11:59:31 -0700220 /*
sangho8995ac52015-02-04 11:29:03 -0800221 * Drop instructions
alshabib55a55d92014-09-16 11:59:31 -0700222 */
223
224 public static final class DropInstruction implements Instruction {
225 @Override
226 public Type type() {
227 return Type.DROP;
228 }
alshabib99b8fdc2014-09-25 14:30:22 -0700229
230 @Override
231 public String toString() {
232 return toStringHelper(type()).toString();
233
234 }
alshabib8ca53902014-10-07 13:11:17 -0700235
236 @Override
237 public int hashCode() {
238 return Objects.hash(type());
239 }
240
241 @Override
242 public boolean equals(Object obj) {
243 if (this == obj) {
244 return true;
245 }
246 if (obj instanceof DropInstruction) {
247 DropInstruction that = (DropInstruction) obj;
248 return Objects.equals(type(), that.type());
249
250 }
251 return false;
252 }
alshabib55a55d92014-09-16 11:59:31 -0700253 }
254
sangho8995ac52015-02-04 11:29:03 -0800255 /*
256 * Output Instruction
257 */
alshabib55a55d92014-09-16 11:59:31 -0700258
259 public static final class OutputInstruction implements Instruction {
260 private final PortNumber port;
261
262 private OutputInstruction(PortNumber port) {
263 this.port = port;
264 }
265
266 public PortNumber port() {
267 return port;
268 }
269
270 @Override
271 public Type type() {
272 return Type.OUTPUT;
273 }
alshabib99b8fdc2014-09-25 14:30:22 -0700274 @Override
275 public String toString() {
276 return toStringHelper(type().toString())
277 .add("port", port).toString();
278 }
alshabib8ca53902014-10-07 13:11:17 -0700279
280 @Override
281 public int hashCode() {
Thomas Vachuska9b2da212014-11-10 19:30:25 -0800282 return Objects.hash(type(), port);
alshabib8ca53902014-10-07 13:11:17 -0700283 }
284
285 @Override
286 public boolean equals(Object obj) {
287 if (this == obj) {
288 return true;
289 }
290 if (obj instanceof OutputInstruction) {
291 OutputInstruction that = (OutputInstruction) obj;
Yuta HIGUCHI4ce65292014-11-01 12:09:55 -0700292 return Objects.equals(port, that.port);
alshabib8ca53902014-10-07 13:11:17 -0700293
294 }
295 return false;
296 }
alshabib55a55d92014-09-16 11:59:31 -0700297 }
298
sangho8995ac52015-02-04 11:29:03 -0800299 /*
300 * Group Instruction
301 */
302
303 public static final class GroupInstruction implements Instruction {
304 private final GroupId groupId;
305
306 private GroupInstruction(GroupId groupId) {
307 this.groupId = groupId;
308 }
309
310 public GroupId groupId() {
311 return groupId;
312 }
313
314 @Override
315 public Type type() {
316 return Type.GROUP;
317 }
318 @Override
319 public String toString() {
320 return toStringHelper(type().toString())
321 .add("group ID", groupId.id()).toString();
322 }
323
324 @Override
325 public int hashCode() {
326 return Objects.hash(type(), groupId);
327 }
328
329 @Override
330 public boolean equals(Object obj) {
331 if (this == obj) {
332 return true;
333 }
334 if (obj instanceof GroupInstruction) {
335 GroupInstruction that = (GroupInstruction) obj;
336 return Objects.equals(groupId, that.groupId);
337
338 }
339 return false;
340 }
341 }
342
alshabib55a55d92014-09-16 11:59:31 -0700343}
alshabib8ca53902014-10-07 13:11:17 -0700344
345