blob: 3b1d433c1a8d3d42b1fd6bae2122a6b063d50f6a [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;
20
alshabib8ca53902014-10-07 13:11:17 -070021import java.util.Objects;
22
alshabib55a55d92014-09-16 11:59:31 -070023import org.onlab.onos.net.PortNumber;
Marc De Leenheer49087752014-10-23 13:54:09 -070024import org.onlab.onos.net.flow.instructions.L0ModificationInstruction.L0SubType;
25import org.onlab.onos.net.flow.instructions.L0ModificationInstruction.ModLambdaInstruction;
alshabib7410fea2014-09-16 13:48:39 -070026import org.onlab.onos.net.flow.instructions.L2ModificationInstruction.L2SubType;
alshabib55a55d92014-09-16 11:59:31 -070027import org.onlab.onos.net.flow.instructions.L2ModificationInstruction.ModEtherInstruction;
alshabib7410fea2014-09-16 13:48:39 -070028import org.onlab.onos.net.flow.instructions.L3ModificationInstruction.L3SubType;
29import org.onlab.onos.net.flow.instructions.L3ModificationInstruction.ModIPInstruction;
Ayaka Koshibe1d56fe42014-09-19 16:51:58 -070030import org.onlab.packet.IpPrefix;
Ayaka Koshibea9c199f2014-09-16 16:21:40 -070031import org.onlab.packet.MacAddress;
32import org.onlab.packet.VlanId;
alshabib64231f62014-09-16 17:58:36 -070033
alshabib55a55d92014-09-16 11:59:31 -070034/**
35 * Factory class for creating various traffic treatment instructions.
36 */
37public final class Instructions {
38
39
40 // Ban construction
41 private Instructions() {}
42
43 /**
44 * Creates an output instruction using the specified port number. This can
45 * include logical ports such as CONTROLLER, FLOOD, etc.
46 *
47 * @param number port number
48 * @return output instruction
49 */
50 public static OutputInstruction createOutput(final PortNumber number) {
51 checkNotNull(number, "PortNumber cannot be null");
52 return new OutputInstruction(number);
53 }
54
55 /**
56 * Creates a drop instruction.
57 * @return drop instruction
58 */
59 public static DropInstruction createDrop() {
60 return new DropInstruction();
61 }
62
63 /**
Marc De Leenheer49087752014-10-23 13:54:09 -070064 * Creates a l0 modification.
65 * @param lambda the lambda to modify to.
66 * @return a l0 modification
67 */
68 public static L0ModificationInstruction modL0Lambda(short lambda) {
69 checkNotNull(lambda, "L0 lambda cannot be null");
70 return new ModLambdaInstruction(L0SubType.LAMBDA, lambda);
71 }
72
73 /**
alshabib55a55d92014-09-16 11:59:31 -070074 * Creates a l2 src modification.
75 * @param addr the mac address to modify to.
76 * @return a l2 modification
77 */
Ayaka Koshibea9c199f2014-09-16 16:21:40 -070078 public static L2ModificationInstruction modL2Src(MacAddress addr) {
alshabib55a55d92014-09-16 11:59:31 -070079 checkNotNull(addr, "Src l2 address cannot be null");
alshabib99b8fdc2014-09-25 14:30:22 -070080 return new ModEtherInstruction(L2SubType.ETH_SRC, addr);
alshabib55a55d92014-09-16 11:59:31 -070081 }
82
83 /**
84 * Creates a L2 dst modification.
85 * @param addr the mac address to modify to.
86 * @return a L2 modification
87 */
Ayaka Koshibea9c199f2014-09-16 16:21:40 -070088 public static L2ModificationInstruction modL2Dst(MacAddress addr) {
alshabib55a55d92014-09-16 11:59:31 -070089 checkNotNull(addr, "Dst l2 address cannot be null");
alshabib99b8fdc2014-09-25 14:30:22 -070090 return new L2ModificationInstruction.ModEtherInstruction(L2SubType.ETH_DST, addr);
alshabib55a55d92014-09-16 11:59:31 -070091 }
92
93 /**
alshabib7410fea2014-09-16 13:48:39 -070094 * Creates a Vlan id modification.
95 * @param vlanId the vlan id to modify to.
96 * @return a L2 modification
97 */
Ayaka Koshibea9c199f2014-09-16 16:21:40 -070098 public static L2ModificationInstruction modVlanId(VlanId vlanId) {
alshabib55a55d92014-09-16 11:59:31 -070099 checkNotNull(vlanId, "VLAN id cannot be null");
100 return new L2ModificationInstruction.ModVlanIdInstruction(vlanId);
101 }
102
alshabib7410fea2014-09-16 13:48:39 -0700103 /**
104 * Creates a Vlan pcp modification.
105 * @param vlanPcp the pcp to modify to.
106 * @return a L2 modification
107 */
108 public static L2ModificationInstruction modVlanPcp(Byte vlanPcp) {
109 checkNotNull(vlanPcp, "VLAN Pcp cannot be null");
110 return new L2ModificationInstruction.ModVlanPcpInstruction(vlanPcp);
111 }
112
113 /**
114 * Creates a L3 src modification.
115 * @param addr the ip address to modify to.
116 * @return a L3 modification
117 */
Ayaka Koshibe1d56fe42014-09-19 16:51:58 -0700118 public static L3ModificationInstruction modL3Src(IpPrefix addr) {
alshabib7410fea2014-09-16 13:48:39 -0700119 checkNotNull(addr, "Src l3 address cannot be null");
alshabib99b8fdc2014-09-25 14:30:22 -0700120 return new ModIPInstruction(L3SubType.IP_SRC, addr);
alshabib7410fea2014-09-16 13:48:39 -0700121 }
122
123 /**
124 * Creates a L3 dst modification.
125 * @param addr the ip address to modify to.
126 * @return a L3 modification
127 */
Ayaka Koshibe1d56fe42014-09-19 16:51:58 -0700128 public static L3ModificationInstruction modL3Dst(IpPrefix addr) {
alshabib7410fea2014-09-16 13:48:39 -0700129 checkNotNull(addr, "Dst l3 address cannot be null");
alshabib99b8fdc2014-09-25 14:30:22 -0700130 return new ModIPInstruction(L3SubType.IP_DST, addr);
alshabib7410fea2014-09-16 13:48:39 -0700131 }
132
alshabib7410fea2014-09-16 13:48:39 -0700133
alshabib55a55d92014-09-16 11:59:31 -0700134 /*
135 * Output instructions
136 */
137
138 public static final class DropInstruction implements Instruction {
139 @Override
140 public Type type() {
141 return Type.DROP;
142 }
alshabib99b8fdc2014-09-25 14:30:22 -0700143
144 @Override
145 public String toString() {
146 return toStringHelper(type()).toString();
147
148 }
alshabib8ca53902014-10-07 13:11:17 -0700149
150 @Override
151 public int hashCode() {
152 return Objects.hash(type());
153 }
154
155 @Override
156 public boolean equals(Object obj) {
157 if (this == obj) {
158 return true;
159 }
160 if (obj instanceof DropInstruction) {
161 DropInstruction that = (DropInstruction) obj;
162 return Objects.equals(type(), that.type());
163
164 }
165 return false;
166 }
alshabib55a55d92014-09-16 11:59:31 -0700167 }
168
169
170 public static final class OutputInstruction implements Instruction {
171 private final PortNumber port;
172
173 private OutputInstruction(PortNumber port) {
174 this.port = port;
175 }
176
177 public PortNumber port() {
178 return port;
179 }
180
181 @Override
182 public Type type() {
183 return Type.OUTPUT;
184 }
alshabib99b8fdc2014-09-25 14:30:22 -0700185 @Override
186 public String toString() {
187 return toStringHelper(type().toString())
188 .add("port", port).toString();
189 }
alshabib8ca53902014-10-07 13:11:17 -0700190
191 @Override
192 public int hashCode() {
193 return Objects.hash(port, type());
194 }
195
196 @Override
197 public boolean equals(Object obj) {
198 if (this == obj) {
199 return true;
200 }
201 if (obj instanceof OutputInstruction) {
202 OutputInstruction that = (OutputInstruction) obj;
203 Objects.equals(port, that.port);
204
205 }
206 return false;
207 }
alshabib55a55d92014-09-16 11:59:31 -0700208 }
209
210}
alshabib8ca53902014-10-07 13:11:17 -0700211
212