blob: 988c52f9fb9e329b9ff8e96f79fd756d0d064125 [file] [log] [blame]
Thomas Vachuska83e090e2014-10-22 14:25:35 -07001/*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
alshabib55a55d92014-09-16 11:59:31 -070019package org.onlab.onos.net.flow.instructions;
20
alshabib99b8fdc2014-09-25 14:30:22 -070021import static com.google.common.base.MoreObjects.toStringHelper;
alshabib55a55d92014-09-16 11:59:31 -070022import static com.google.common.base.Preconditions.checkNotNull;
23
alshabib8ca53902014-10-07 13:11:17 -070024import java.util.Objects;
25
alshabib55a55d92014-09-16 11:59:31 -070026import org.onlab.onos.net.PortNumber;
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;
Ayaka Koshibe1d56fe42014-09-19 16:51:58 -070031import org.onlab.packet.IpPrefix;
Ayaka Koshibea9c199f2014-09-16 16:21:40 -070032import org.onlab.packet.MacAddress;
33import org.onlab.packet.VlanId;
alshabib64231f62014-09-16 17:58:36 -070034
alshabib55a55d92014-09-16 11:59:31 -070035/**
36 * Factory class for creating various traffic treatment instructions.
37 */
38public final class Instructions {
39
40
41 // Ban construction
42 private Instructions() {}
43
44 /**
45 * Creates an output instruction using the specified port number. This can
46 * include logical ports such as CONTROLLER, FLOOD, etc.
47 *
48 * @param number port number
49 * @return output instruction
50 */
51 public static OutputInstruction createOutput(final PortNumber number) {
52 checkNotNull(number, "PortNumber cannot be null");
53 return new OutputInstruction(number);
54 }
55
56 /**
57 * Creates a drop instruction.
58 * @return drop instruction
59 */
60 public static DropInstruction createDrop() {
61 return new DropInstruction();
62 }
63
64 /**
65 * Creates a l2 src modification.
66 * @param addr the mac address to modify to.
67 * @return a l2 modification
68 */
Ayaka Koshibea9c199f2014-09-16 16:21:40 -070069 public static L2ModificationInstruction modL2Src(MacAddress addr) {
alshabib55a55d92014-09-16 11:59:31 -070070 checkNotNull(addr, "Src l2 address cannot be null");
alshabib99b8fdc2014-09-25 14:30:22 -070071 return new ModEtherInstruction(L2SubType.ETH_SRC, addr);
alshabib55a55d92014-09-16 11:59:31 -070072 }
73
74 /**
75 * Creates a L2 dst modification.
76 * @param addr the mac address to modify to.
77 * @return a L2 modification
78 */
Ayaka Koshibea9c199f2014-09-16 16:21:40 -070079 public static L2ModificationInstruction modL2Dst(MacAddress addr) {
alshabib55a55d92014-09-16 11:59:31 -070080 checkNotNull(addr, "Dst l2 address cannot be null");
alshabib99b8fdc2014-09-25 14:30:22 -070081 return new L2ModificationInstruction.ModEtherInstruction(L2SubType.ETH_DST, addr);
alshabib55a55d92014-09-16 11:59:31 -070082 }
83
84 /**
alshabib7410fea2014-09-16 13:48:39 -070085 * Creates a Vlan id modification.
86 * @param vlanId the vlan id to modify to.
87 * @return a L2 modification
88 */
Ayaka Koshibea9c199f2014-09-16 16:21:40 -070089 public static L2ModificationInstruction modVlanId(VlanId vlanId) {
alshabib55a55d92014-09-16 11:59:31 -070090 checkNotNull(vlanId, "VLAN id cannot be null");
91 return new L2ModificationInstruction.ModVlanIdInstruction(vlanId);
92 }
93
alshabib7410fea2014-09-16 13:48:39 -070094 /**
95 * Creates a Vlan pcp modification.
96 * @param vlanPcp the pcp to modify to.
97 * @return a L2 modification
98 */
99 public static L2ModificationInstruction modVlanPcp(Byte vlanPcp) {
100 checkNotNull(vlanPcp, "VLAN Pcp cannot be null");
101 return new L2ModificationInstruction.ModVlanPcpInstruction(vlanPcp);
102 }
103
104 /**
105 * Creates a L3 src modification.
106 * @param addr the ip address to modify to.
107 * @return a L3 modification
108 */
Ayaka Koshibe1d56fe42014-09-19 16:51:58 -0700109 public static L3ModificationInstruction modL3Src(IpPrefix addr) {
alshabib7410fea2014-09-16 13:48:39 -0700110 checkNotNull(addr, "Src l3 address cannot be null");
alshabib99b8fdc2014-09-25 14:30:22 -0700111 return new ModIPInstruction(L3SubType.IP_SRC, addr);
alshabib7410fea2014-09-16 13:48:39 -0700112 }
113
114 /**
115 * Creates a L3 dst modification.
116 * @param addr the ip address to modify to.
117 * @return a L3 modification
118 */
Ayaka Koshibe1d56fe42014-09-19 16:51:58 -0700119 public static L3ModificationInstruction modL3Dst(IpPrefix addr) {
alshabib7410fea2014-09-16 13:48:39 -0700120 checkNotNull(addr, "Dst l3 address cannot be null");
alshabib99b8fdc2014-09-25 14:30:22 -0700121 return new ModIPInstruction(L3SubType.IP_DST, addr);
alshabib7410fea2014-09-16 13:48:39 -0700122 }
123
alshabib7410fea2014-09-16 13:48:39 -0700124
alshabib55a55d92014-09-16 11:59:31 -0700125 /*
126 * Output instructions
127 */
128
129 public static final class DropInstruction implements Instruction {
130 @Override
131 public Type type() {
132 return Type.DROP;
133 }
alshabib99b8fdc2014-09-25 14:30:22 -0700134
135 @Override
136 public String toString() {
137 return toStringHelper(type()).toString();
138
139 }
alshabib8ca53902014-10-07 13:11:17 -0700140
141 @Override
142 public int hashCode() {
143 return Objects.hash(type());
144 }
145
146 @Override
147 public boolean equals(Object obj) {
148 if (this == obj) {
149 return true;
150 }
151 if (obj instanceof DropInstruction) {
152 DropInstruction that = (DropInstruction) obj;
153 return Objects.equals(type(), that.type());
154
155 }
156 return false;
157 }
alshabib55a55d92014-09-16 11:59:31 -0700158 }
159
160
161 public static final class OutputInstruction implements Instruction {
162 private final PortNumber port;
163
164 private OutputInstruction(PortNumber port) {
165 this.port = port;
166 }
167
168 public PortNumber port() {
169 return port;
170 }
171
172 @Override
173 public Type type() {
174 return Type.OUTPUT;
175 }
alshabib99b8fdc2014-09-25 14:30:22 -0700176 @Override
177 public String toString() {
178 return toStringHelper(type().toString())
179 .add("port", port).toString();
180 }
alshabib8ca53902014-10-07 13:11:17 -0700181
182 @Override
183 public int hashCode() {
184 return Objects.hash(port, type());
185 }
186
187 @Override
188 public boolean equals(Object obj) {
189 if (this == obj) {
190 return true;
191 }
192 if (obj instanceof OutputInstruction) {
193 OutputInstruction that = (OutputInstruction) obj;
194 Objects.equals(port, that.port);
195
196 }
197 return false;
198 }
alshabib55a55d92014-09-16 11:59:31 -0700199 }
200
201}
alshabib8ca53902014-10-07 13:11:17 -0700202
203