blob: b2ebdeeb9b6f6e298f605416712025ef7a5a69a4 [file] [log] [blame]
alshabib55a55d92014-09-16 11:59:31 -07001package org.onlab.onos.net.flow.instructions;
2
alshabib99b8fdc2014-09-25 14:30:22 -07003import static com.google.common.base.MoreObjects.toStringHelper;
alshabib55a55d92014-09-16 11:59:31 -07004import static com.google.common.base.Preconditions.checkNotNull;
5
alshabib8ca53902014-10-07 13:11:17 -07006import java.util.Objects;
7
alshabib55a55d92014-09-16 11:59:31 -07008import org.onlab.onos.net.PortNumber;
alshabib7410fea2014-09-16 13:48:39 -07009import org.onlab.onos.net.flow.instructions.L2ModificationInstruction.L2SubType;
alshabib55a55d92014-09-16 11:59:31 -070010import org.onlab.onos.net.flow.instructions.L2ModificationInstruction.ModEtherInstruction;
alshabib7410fea2014-09-16 13:48:39 -070011import org.onlab.onos.net.flow.instructions.L3ModificationInstruction.L3SubType;
12import org.onlab.onos.net.flow.instructions.L3ModificationInstruction.ModIPInstruction;
Ayaka Koshibe1d56fe42014-09-19 16:51:58 -070013import org.onlab.packet.IpPrefix;
Ayaka Koshibea9c199f2014-09-16 16:21:40 -070014import org.onlab.packet.MacAddress;
15import org.onlab.packet.VlanId;
alshabib64231f62014-09-16 17:58:36 -070016
alshabib55a55d92014-09-16 11:59:31 -070017/**
18 * Factory class for creating various traffic treatment instructions.
19 */
20public final class Instructions {
21
22
23 // Ban construction
24 private Instructions() {}
25
26 /**
27 * Creates an output instruction using the specified port number. This can
28 * include logical ports such as CONTROLLER, FLOOD, etc.
29 *
30 * @param number port number
31 * @return output instruction
32 */
33 public static OutputInstruction createOutput(final PortNumber number) {
34 checkNotNull(number, "PortNumber cannot be null");
35 return new OutputInstruction(number);
36 }
37
38 /**
39 * Creates a drop instruction.
40 * @return drop instruction
41 */
42 public static DropInstruction createDrop() {
43 return new DropInstruction();
44 }
45
46 /**
47 * Creates a l2 src modification.
48 * @param addr the mac address to modify to.
49 * @return a l2 modification
50 */
Ayaka Koshibea9c199f2014-09-16 16:21:40 -070051 public static L2ModificationInstruction modL2Src(MacAddress addr) {
alshabib55a55d92014-09-16 11:59:31 -070052 checkNotNull(addr, "Src l2 address cannot be null");
alshabib99b8fdc2014-09-25 14:30:22 -070053 return new ModEtherInstruction(L2SubType.ETH_SRC, addr);
alshabib55a55d92014-09-16 11:59:31 -070054 }
55
56 /**
57 * Creates a L2 dst modification.
58 * @param addr the mac address to modify to.
59 * @return a L2 modification
60 */
Ayaka Koshibea9c199f2014-09-16 16:21:40 -070061 public static L2ModificationInstruction modL2Dst(MacAddress addr) {
alshabib55a55d92014-09-16 11:59:31 -070062 checkNotNull(addr, "Dst l2 address cannot be null");
alshabib99b8fdc2014-09-25 14:30:22 -070063 return new L2ModificationInstruction.ModEtherInstruction(L2SubType.ETH_DST, addr);
alshabib55a55d92014-09-16 11:59:31 -070064 }
65
66 /**
alshabib7410fea2014-09-16 13:48:39 -070067 * Creates a Vlan id modification.
68 * @param vlanId the vlan id to modify to.
69 * @return a L2 modification
70 */
Ayaka Koshibea9c199f2014-09-16 16:21:40 -070071 public static L2ModificationInstruction modVlanId(VlanId vlanId) {
alshabib55a55d92014-09-16 11:59:31 -070072 checkNotNull(vlanId, "VLAN id cannot be null");
73 return new L2ModificationInstruction.ModVlanIdInstruction(vlanId);
74 }
75
alshabib7410fea2014-09-16 13:48:39 -070076 /**
77 * Creates a Vlan pcp modification.
78 * @param vlanPcp the pcp to modify to.
79 * @return a L2 modification
80 */
81 public static L2ModificationInstruction modVlanPcp(Byte vlanPcp) {
82 checkNotNull(vlanPcp, "VLAN Pcp cannot be null");
83 return new L2ModificationInstruction.ModVlanPcpInstruction(vlanPcp);
84 }
85
86 /**
87 * Creates a L3 src modification.
88 * @param addr the ip address to modify to.
89 * @return a L3 modification
90 */
Ayaka Koshibe1d56fe42014-09-19 16:51:58 -070091 public static L3ModificationInstruction modL3Src(IpPrefix addr) {
alshabib7410fea2014-09-16 13:48:39 -070092 checkNotNull(addr, "Src l3 address cannot be null");
alshabib99b8fdc2014-09-25 14:30:22 -070093 return new ModIPInstruction(L3SubType.IP_SRC, addr);
alshabib7410fea2014-09-16 13:48:39 -070094 }
95
96 /**
97 * Creates a L3 dst modification.
98 * @param addr the ip address to modify to.
99 * @return a L3 modification
100 */
Ayaka Koshibe1d56fe42014-09-19 16:51:58 -0700101 public static L3ModificationInstruction modL3Dst(IpPrefix addr) {
alshabib7410fea2014-09-16 13:48:39 -0700102 checkNotNull(addr, "Dst l3 address cannot be null");
alshabib99b8fdc2014-09-25 14:30:22 -0700103 return new ModIPInstruction(L3SubType.IP_DST, addr);
alshabib7410fea2014-09-16 13:48:39 -0700104 }
105
alshabib7410fea2014-09-16 13:48:39 -0700106
alshabib55a55d92014-09-16 11:59:31 -0700107 /*
108 * Output instructions
109 */
110
111 public static final class DropInstruction implements Instruction {
112 @Override
113 public Type type() {
114 return Type.DROP;
115 }
alshabib99b8fdc2014-09-25 14:30:22 -0700116
117 @Override
118 public String toString() {
119 return toStringHelper(type()).toString();
120
121 }
alshabib8ca53902014-10-07 13:11:17 -0700122
123 @Override
124 public int hashCode() {
125 return Objects.hash(type());
126 }
127
128 @Override
129 public boolean equals(Object obj) {
130 if (this == obj) {
131 return true;
132 }
133 if (obj instanceof DropInstruction) {
134 DropInstruction that = (DropInstruction) obj;
135 return Objects.equals(type(), that.type());
136
137 }
138 return false;
139 }
alshabib55a55d92014-09-16 11:59:31 -0700140 }
141
142
143 public static final class OutputInstruction implements Instruction {
144 private final PortNumber port;
145
146 private OutputInstruction(PortNumber port) {
147 this.port = port;
148 }
149
150 public PortNumber port() {
151 return port;
152 }
153
154 @Override
155 public Type type() {
156 return Type.OUTPUT;
157 }
alshabib99b8fdc2014-09-25 14:30:22 -0700158 @Override
159 public String toString() {
160 return toStringHelper(type().toString())
161 .add("port", port).toString();
162 }
alshabib8ca53902014-10-07 13:11:17 -0700163
164 @Override
165 public int hashCode() {
166 return Objects.hash(port, type());
167 }
168
169 @Override
170 public boolean equals(Object obj) {
171 if (this == obj) {
172 return true;
173 }
174 if (obj instanceof OutputInstruction) {
175 OutputInstruction that = (OutputInstruction) obj;
176 Objects.equals(port, that.port);
177
178 }
179 return false;
180 }
alshabib55a55d92014-09-16 11:59:31 -0700181 }
182
183}
alshabib8ca53902014-10-07 13:11:17 -0700184
185