blob: c0fe386c02a6c25ec4004f23ebd4e8bd458baad4 [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;
alshabib55a55d92014-09-16 11:59:31 -070019import static com.google.common.base.Preconditions.checkNotNull;
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -080020import static org.onlab.onos.net.flow.instructions.L2ModificationInstruction.*;
alshabib55a55d92014-09-16 11:59:31 -070021
alshabib8ca53902014-10-07 13:11:17 -070022import java.util.Objects;
23
alshabib55a55d92014-09-16 11:59:31 -070024import org.onlab.onos.net.PortNumber;
Marc De Leenheer49087752014-10-23 13:54:09 -070025import org.onlab.onos.net.flow.instructions.L0ModificationInstruction.L0SubType;
26import org.onlab.onos.net.flow.instructions.L0ModificationInstruction.ModLambdaInstruction;
alshabib7410fea2014-09-16 13:48:39 -070027import org.onlab.onos.net.flow.instructions.L2ModificationInstruction.L2SubType;
alshabib55a55d92014-09-16 11:59:31 -070028import org.onlab.onos.net.flow.instructions.L2ModificationInstruction.ModEtherInstruction;
alshabib7410fea2014-09-16 13:48:39 -070029import org.onlab.onos.net.flow.instructions.L3ModificationInstruction.L3SubType;
30import org.onlab.onos.net.flow.instructions.L3ModificationInstruction.ModIPInstruction;
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -080031
32import org.onlab.packet.Ethernet;
Pavlin Radoslavov855ea2d2014-10-30 15:32:39 -070033import org.onlab.packet.IpAddress;
Ayaka Koshibea9c199f2014-09-16 16:21:40 -070034import org.onlab.packet.MacAddress;
35import org.onlab.packet.VlanId;
alshabib64231f62014-09-16 17:58:36 -070036
alshabib55a55d92014-09-16 11:59:31 -070037/**
38 * Factory class for creating various traffic treatment instructions.
39 */
40public final class Instructions {
41
42
43 // Ban construction
44 private Instructions() {}
45
46 /**
47 * Creates an output instruction using the specified port number. This can
48 * include logical ports such as CONTROLLER, FLOOD, etc.
49 *
50 * @param number port number
51 * @return output instruction
52 */
53 public static OutputInstruction createOutput(final PortNumber number) {
54 checkNotNull(number, "PortNumber cannot be null");
55 return new OutputInstruction(number);
56 }
57
58 /**
59 * Creates a drop instruction.
60 * @return drop instruction
61 */
62 public static DropInstruction createDrop() {
63 return new DropInstruction();
64 }
65
66 /**
Marc De Leenheer49087752014-10-23 13:54:09 -070067 * Creates a l0 modification.
68 * @param lambda the lambda to modify to.
69 * @return a l0 modification
70 */
71 public static L0ModificationInstruction modL0Lambda(short lambda) {
72 checkNotNull(lambda, "L0 lambda cannot be null");
73 return new ModLambdaInstruction(L0SubType.LAMBDA, lambda);
74 }
75
76 /**
alshabib55a55d92014-09-16 11:59:31 -070077 * Creates a l2 src modification.
78 * @param addr the mac address to modify to.
79 * @return a l2 modification
80 */
Ayaka Koshibea9c199f2014-09-16 16:21:40 -070081 public static L2ModificationInstruction modL2Src(MacAddress addr) {
alshabib55a55d92014-09-16 11:59:31 -070082 checkNotNull(addr, "Src l2 address cannot be null");
alshabib99b8fdc2014-09-25 14:30:22 -070083 return new ModEtherInstruction(L2SubType.ETH_SRC, addr);
alshabib55a55d92014-09-16 11:59:31 -070084 }
85
86 /**
87 * Creates a L2 dst modification.
88 * @param addr the mac address to modify to.
89 * @return a L2 modification
90 */
Ayaka Koshibea9c199f2014-09-16 16:21:40 -070091 public static L2ModificationInstruction modL2Dst(MacAddress addr) {
alshabib55a55d92014-09-16 11:59:31 -070092 checkNotNull(addr, "Dst l2 address cannot be null");
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -080093 return new ModEtherInstruction(L2SubType.ETH_DST, addr);
alshabib55a55d92014-09-16 11:59:31 -070094 }
95
96 /**
alshabib7410fea2014-09-16 13:48:39 -070097 * Creates a Vlan id modification.
98 * @param vlanId the vlan id to modify to.
99 * @return a L2 modification
100 */
Ayaka Koshibea9c199f2014-09-16 16:21:40 -0700101 public static L2ModificationInstruction modVlanId(VlanId vlanId) {
alshabib55a55d92014-09-16 11:59:31 -0700102 checkNotNull(vlanId, "VLAN id cannot be null");
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -0800103 return new ModVlanIdInstruction(vlanId);
alshabib55a55d92014-09-16 11:59:31 -0700104 }
105
alshabib7410fea2014-09-16 13:48:39 -0700106 /**
107 * Creates a Vlan pcp modification.
108 * @param vlanPcp the pcp to modify to.
109 * @return a L2 modification
110 */
111 public static L2ModificationInstruction modVlanPcp(Byte vlanPcp) {
112 checkNotNull(vlanPcp, "VLAN Pcp cannot be null");
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -0800113 return new ModVlanPcpInstruction(vlanPcp);
alshabib7410fea2014-09-16 13:48:39 -0700114 }
115
116 /**
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -0800117 * Creates a MPLS label modification.
118 * @param mplsLabel to set.
119 * @return a L2 Modification
120 */
121 public static L2ModificationInstruction modMplsLabel(Integer mplsLabel) {
122 checkNotNull(mplsLabel, "MPLS label cannot be null");
123 return new ModMplsLabelInstruction(mplsLabel);
124 }
125 /**
alshabib7410fea2014-09-16 13:48:39 -0700126 * Creates a L3 src modification.
127 * @param addr the ip address to modify to.
128 * @return a L3 modification
129 */
Pavlin Radoslavov855ea2d2014-10-30 15:32:39 -0700130 public static L3ModificationInstruction modL3Src(IpAddress addr) {
alshabib7410fea2014-09-16 13:48:39 -0700131 checkNotNull(addr, "Src l3 address cannot be null");
alshabib99b8fdc2014-09-25 14:30:22 -0700132 return new ModIPInstruction(L3SubType.IP_SRC, addr);
alshabib7410fea2014-09-16 13:48:39 -0700133 }
134
135 /**
136 * Creates a L3 dst 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 modL3Dst(IpAddress addr) {
alshabib7410fea2014-09-16 13:48:39 -0700141 checkNotNull(addr, "Dst l3 address cannot be null");
alshabib99b8fdc2014-09-25 14:30:22 -0700142 return new ModIPInstruction(L3SubType.IP_DST, addr);
alshabib7410fea2014-09-16 13:48:39 -0700143 }
144
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -0800145 /**
146 * Creates a mpls header instruction.
147 * @return a L2 modification.
148 */
149 public static Instruction pushMpls() {
150 return new PushHeaderInstructions(L2SubType.MPLS_PUSH,
151 new Ethernet().setEtherType(Ethernet.MPLS_UNICAST));
152 }
153
154 /**
155 * Creates a mpls header instruction.
156 * @return a L2 modification.
157 */
158 public static Instruction popMpls() {
159 return new PushHeaderInstructions(L2SubType.MPLS_POP,
160 new Ethernet().setEtherType(Ethernet.MPLS_UNICAST));
161 }
alshabib7410fea2014-09-16 13:48:39 -0700162
alshabib55a55d92014-09-16 11:59:31 -0700163 /*
164 * Output instructions
165 */
166
167 public static final class DropInstruction implements Instruction {
168 @Override
169 public Type type() {
170 return Type.DROP;
171 }
alshabib99b8fdc2014-09-25 14:30:22 -0700172
173 @Override
174 public String toString() {
175 return toStringHelper(type()).toString();
176
177 }
alshabib8ca53902014-10-07 13:11:17 -0700178
179 @Override
180 public int hashCode() {
181 return Objects.hash(type());
182 }
183
184 @Override
185 public boolean equals(Object obj) {
186 if (this == obj) {
187 return true;
188 }
189 if (obj instanceof DropInstruction) {
190 DropInstruction that = (DropInstruction) obj;
191 return Objects.equals(type(), that.type());
192
193 }
194 return false;
195 }
alshabib55a55d92014-09-16 11:59:31 -0700196 }
197
198
199 public static final class OutputInstruction implements Instruction {
200 private final PortNumber port;
201
202 private OutputInstruction(PortNumber port) {
203 this.port = port;
204 }
205
206 public PortNumber port() {
207 return port;
208 }
209
210 @Override
211 public Type type() {
212 return Type.OUTPUT;
213 }
alshabib99b8fdc2014-09-25 14:30:22 -0700214 @Override
215 public String toString() {
216 return toStringHelper(type().toString())
217 .add("port", port).toString();
218 }
alshabib8ca53902014-10-07 13:11:17 -0700219
220 @Override
221 public int hashCode() {
Thomas Vachuska9b2da212014-11-10 19:30:25 -0800222 return Objects.hash(type(), port);
alshabib8ca53902014-10-07 13:11:17 -0700223 }
224
225 @Override
226 public boolean equals(Object obj) {
227 if (this == obj) {
228 return true;
229 }
230 if (obj instanceof OutputInstruction) {
231 OutputInstruction that = (OutputInstruction) obj;
Yuta HIGUCHI4ce65292014-11-01 12:09:55 -0700232 return Objects.equals(port, that.port);
alshabib8ca53902014-10-07 13:11:17 -0700233
234 }
235 return false;
236 }
alshabib55a55d92014-09-16 11:59:31 -0700237 }
238
239}
alshabib8ca53902014-10-07 13:11:17 -0700240
241