blob: 6f1281f3458e5a091f927dd467c9b03720651e41 [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
Brian O'Connorabafb502014-12-02 22:26:20 -080024import org.onosproject.net.PortNumber;
25import org.onosproject.net.flow.instructions.L0ModificationInstruction.L0SubType;
26import org.onosproject.net.flow.instructions.L0ModificationInstruction.ModLambdaInstruction;
27import org.onosproject.net.flow.instructions.L2ModificationInstruction.L2SubType;
28import org.onosproject.net.flow.instructions.L2ModificationInstruction.ModEtherInstruction;
29import org.onosproject.net.flow.instructions.L3ModificationInstruction.L3SubType;
30import org.onosproject.net.flow.instructions.L3ModificationInstruction.ModIPInstruction;
sangho3f97a17d2015-01-29 22:56:29 -080031import org.onosproject.net.flow.instructions.L3ModificationInstruction.ModTtlInstruction;
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -080032
33import org.onlab.packet.Ethernet;
Pavlin Radoslavov855ea2d2014-10-30 15:32:39 -070034import org.onlab.packet.IpAddress;
Ayaka Koshibea9c199f2014-09-16 16:21:40 -070035import org.onlab.packet.MacAddress;
36import org.onlab.packet.VlanId;
alshabib64231f62014-09-16 17:58:36 -070037
alshabib55a55d92014-09-16 11:59:31 -070038/**
39 * Factory class for creating various traffic treatment instructions.
40 */
41public final class Instructions {
42
43
44 // Ban construction
45 private Instructions() {}
46
47 /**
48 * Creates an output instruction using the specified port number. This can
49 * include logical ports such as CONTROLLER, FLOOD, etc.
50 *
51 * @param number port number
52 * @return output instruction
53 */
54 public static OutputInstruction createOutput(final PortNumber number) {
55 checkNotNull(number, "PortNumber cannot be null");
56 return new OutputInstruction(number);
57 }
58
59 /**
60 * Creates a drop instruction.
61 * @return drop instruction
62 */
63 public static DropInstruction createDrop() {
64 return new DropInstruction();
65 }
66
67 /**
Marc De Leenheer49087752014-10-23 13:54:09 -070068 * Creates a l0 modification.
69 * @param lambda the lambda to modify to.
70 * @return a l0 modification
71 */
72 public static L0ModificationInstruction modL0Lambda(short lambda) {
73 checkNotNull(lambda, "L0 lambda cannot be null");
74 return new ModLambdaInstruction(L0SubType.LAMBDA, lambda);
75 }
76
77 /**
alshabib55a55d92014-09-16 11:59:31 -070078 * Creates a l2 src modification.
79 * @param addr the mac address to modify to.
80 * @return a l2 modification
81 */
Ayaka Koshibea9c199f2014-09-16 16:21:40 -070082 public static L2ModificationInstruction modL2Src(MacAddress addr) {
alshabib55a55d92014-09-16 11:59:31 -070083 checkNotNull(addr, "Src l2 address cannot be null");
alshabib99b8fdc2014-09-25 14:30:22 -070084 return new ModEtherInstruction(L2SubType.ETH_SRC, addr);
alshabib55a55d92014-09-16 11:59:31 -070085 }
86
87 /**
88 * Creates a L2 dst modification.
89 * @param addr the mac address to modify to.
90 * @return a L2 modification
91 */
Ayaka Koshibea9c199f2014-09-16 16:21:40 -070092 public static L2ModificationInstruction modL2Dst(MacAddress addr) {
alshabib55a55d92014-09-16 11:59:31 -070093 checkNotNull(addr, "Dst l2 address cannot be null");
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -080094 return new ModEtherInstruction(L2SubType.ETH_DST, addr);
alshabib55a55d92014-09-16 11:59:31 -070095 }
96
97 /**
alshabib7410fea2014-09-16 13:48:39 -070098 * Creates a Vlan id modification.
99 * @param vlanId the vlan id to modify to.
100 * @return a L2 modification
101 */
Ayaka Koshibea9c199f2014-09-16 16:21:40 -0700102 public static L2ModificationInstruction modVlanId(VlanId vlanId) {
alshabib55a55d92014-09-16 11:59:31 -0700103 checkNotNull(vlanId, "VLAN id cannot be null");
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -0800104 return new ModVlanIdInstruction(vlanId);
alshabib55a55d92014-09-16 11:59:31 -0700105 }
106
alshabib7410fea2014-09-16 13:48:39 -0700107 /**
108 * Creates a Vlan pcp modification.
109 * @param vlanPcp the pcp to modify to.
110 * @return a L2 modification
111 */
112 public static L2ModificationInstruction modVlanPcp(Byte vlanPcp) {
113 checkNotNull(vlanPcp, "VLAN Pcp cannot be null");
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -0800114 return new ModVlanPcpInstruction(vlanPcp);
alshabib7410fea2014-09-16 13:48:39 -0700115 }
116
117 /**
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -0800118 * Creates a MPLS label modification.
119 * @param mplsLabel to set.
120 * @return a L2 Modification
121 */
122 public static L2ModificationInstruction modMplsLabel(Integer mplsLabel) {
123 checkNotNull(mplsLabel, "MPLS label cannot be null");
124 return new ModMplsLabelInstruction(mplsLabel);
125 }
sangho3f97a17d2015-01-29 22:56:29 -0800126
127 /**
128 * Creates a MPLS TTL modification.
129 *
130 * @return a L2 Modification
131 */
132 public static L2ModificationInstruction decMplsTtl() {
133 return new ModMplsTtlInstruction();
134 }
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -0800135 /**
alshabib7410fea2014-09-16 13:48:39 -0700136 * Creates a L3 src modification.
137 * @param addr the ip address to modify to.
138 * @return a L3 modification
139 */
Pavlin Radoslavov855ea2d2014-10-30 15:32:39 -0700140 public static L3ModificationInstruction modL3Src(IpAddress addr) {
alshabib7410fea2014-09-16 13:48:39 -0700141 checkNotNull(addr, "Src l3 address cannot be null");
alshabib99b8fdc2014-09-25 14:30:22 -0700142 return new ModIPInstruction(L3SubType.IP_SRC, addr);
alshabib7410fea2014-09-16 13:48:39 -0700143 }
144
145 /**
146 * Creates a L3 dst modification.
147 * @param addr the ip address to modify to.
148 * @return a L3 modification
149 */
Pavlin Radoslavov855ea2d2014-10-30 15:32:39 -0700150 public static L3ModificationInstruction modL3Dst(IpAddress addr) {
alshabib7410fea2014-09-16 13:48:39 -0700151 checkNotNull(addr, "Dst l3 address cannot be null");
alshabib99b8fdc2014-09-25 14:30:22 -0700152 return new ModIPInstruction(L3SubType.IP_DST, addr);
alshabib7410fea2014-09-16 13:48:39 -0700153 }
154
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -0800155 /**
sangho3f97a17d2015-01-29 22:56:29 -0800156 * Creates a L3 TTL modification.
157 * @return a L3 modification
158 */
159 public static L3ModificationInstruction decNwTtl() {
160 return new ModTtlInstruction(L3SubType.DEC_TTL);
161 }
162
163 /**
164 * Creates a L3 TTL modification.
165 * @return a L3 modification
166 */
167 public static L3ModificationInstruction copyTtlOut() {
168 return new ModTtlInstruction(L3SubType.TTL_OUT);
169 }
170
171 /**
172 * Creates a L3 TTL modification.
173 * @return a L3 modification
174 */
175 public static L3ModificationInstruction copyTtlIn() {
176 return new ModTtlInstruction(L3SubType.TTL_IN);
177 }
178
179 /**
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -0800180 * Creates a mpls header instruction.
181 * @return a L2 modification.
182 */
183 public static Instruction pushMpls() {
184 return new PushHeaderInstructions(L2SubType.MPLS_PUSH,
185 new Ethernet().setEtherType(Ethernet.MPLS_UNICAST));
186 }
187
188 /**
189 * Creates a mpls header instruction.
190 * @return a L2 modification.
191 */
192 public static Instruction popMpls() {
193 return new PushHeaderInstructions(L2SubType.MPLS_POP,
194 new Ethernet().setEtherType(Ethernet.MPLS_UNICAST));
195 }
alshabib7410fea2014-09-16 13:48:39 -0700196
sangho3f97a17d2015-01-29 22:56:29 -0800197 /**
198 * Creates a mpls header instruction.
199 *
200 * @param etherType Ethernet type to set
201 * @return a L2 modification.
202 */
203 public static Instruction popMpls(Short etherType) {
204 checkNotNull(etherType, "Ethernet type cannot be null");
205 return new PushHeaderInstructions(L2SubType.MPLS_POP,
206 new Ethernet().setEtherType(etherType));
207 }
208
alshabib55a55d92014-09-16 11:59:31 -0700209 /*
210 * Output instructions
211 */
212
213 public static final class DropInstruction implements Instruction {
214 @Override
215 public Type type() {
216 return Type.DROP;
217 }
alshabib99b8fdc2014-09-25 14:30:22 -0700218
219 @Override
220 public String toString() {
221 return toStringHelper(type()).toString();
222
223 }
alshabib8ca53902014-10-07 13:11:17 -0700224
225 @Override
226 public int hashCode() {
227 return Objects.hash(type());
228 }
229
230 @Override
231 public boolean equals(Object obj) {
232 if (this == obj) {
233 return true;
234 }
235 if (obj instanceof DropInstruction) {
236 DropInstruction that = (DropInstruction) obj;
237 return Objects.equals(type(), that.type());
238
239 }
240 return false;
241 }
alshabib55a55d92014-09-16 11:59:31 -0700242 }
243
244
245 public static final class OutputInstruction implements Instruction {
246 private final PortNumber port;
247
248 private OutputInstruction(PortNumber port) {
249 this.port = port;
250 }
251
252 public PortNumber port() {
253 return port;
254 }
255
256 @Override
257 public Type type() {
258 return Type.OUTPUT;
259 }
alshabib99b8fdc2014-09-25 14:30:22 -0700260 @Override
261 public String toString() {
262 return toStringHelper(type().toString())
263 .add("port", port).toString();
264 }
alshabib8ca53902014-10-07 13:11:17 -0700265
266 @Override
267 public int hashCode() {
Thomas Vachuska9b2da212014-11-10 19:30:25 -0800268 return Objects.hash(type(), port);
alshabib8ca53902014-10-07 13:11:17 -0700269 }
270
271 @Override
272 public boolean equals(Object obj) {
273 if (this == obj) {
274 return true;
275 }
276 if (obj instanceof OutputInstruction) {
277 OutputInstruction that = (OutputInstruction) obj;
Yuta HIGUCHI4ce65292014-11-01 12:09:55 -0700278 return Objects.equals(port, that.port);
alshabib8ca53902014-10-07 13:11:17 -0700279
280 }
281 return false;
282 }
alshabib55a55d92014-09-16 11:59:31 -0700283 }
284
285}
alshabib8ca53902014-10-07 13:11:17 -0700286
287