blob: 06903e57749f8a2a69d23cafc1e788559a7cc747 [file] [log] [blame]
alshabib55a55d92014-09-16 11:59:31 -07001package org.onlab.onos.net.flow.instructions;
2
3import static com.google.common.base.Preconditions.checkNotNull;
4
5import org.onlab.onos.net.PortNumber;
alshabib7410fea2014-09-16 13:48:39 -07006import org.onlab.onos.net.flow.instructions.L2ModificationInstruction.L2SubType;
alshabib55a55d92014-09-16 11:59:31 -07007import org.onlab.onos.net.flow.instructions.L2ModificationInstruction.ModEtherInstruction;
alshabib7410fea2014-09-16 13:48:39 -07008import org.onlab.onos.net.flow.instructions.L3ModificationInstruction.L3SubType;
9import org.onlab.onos.net.flow.instructions.L3ModificationInstruction.ModIPInstruction;
10import org.onlab.onos.net.flow.instructions.L3ModificationInstruction.ModIPProtoInstruction;
Ayaka Koshibea9c199f2014-09-16 16:21:40 -070011import org.onlab.packet.IpAddress;
12import org.onlab.packet.MacAddress;
13import org.onlab.packet.VlanId;
alshabib55a55d92014-09-16 11:59:31 -070014/**
15 * Factory class for creating various traffic treatment instructions.
16 */
17public final class Instructions {
18
19
20 // Ban construction
21 private Instructions() {}
22
23 /**
24 * Creates an output instruction using the specified port number. This can
25 * include logical ports such as CONTROLLER, FLOOD, etc.
26 *
27 * @param number port number
28 * @return output instruction
29 */
30 public static OutputInstruction createOutput(final PortNumber number) {
31 checkNotNull(number, "PortNumber cannot be null");
32 return new OutputInstruction(number);
33 }
34
35 /**
36 * Creates a drop instruction.
37 * @return drop instruction
38 */
39 public static DropInstruction createDrop() {
40 return new DropInstruction();
41 }
42
43 /**
44 * Creates a l2 src modification.
45 * @param addr the mac address to modify to.
46 * @return a l2 modification
47 */
Ayaka Koshibea9c199f2014-09-16 16:21:40 -070048 public static L2ModificationInstruction modL2Src(MacAddress addr) {
alshabib55a55d92014-09-16 11:59:31 -070049 checkNotNull(addr, "Src l2 address cannot be null");
alshabib7410fea2014-09-16 13:48:39 -070050 return new ModEtherInstruction(L2SubType.L2_SRC, addr);
alshabib55a55d92014-09-16 11:59:31 -070051 }
52
53 /**
54 * Creates a L2 dst modification.
55 * @param addr the mac address to modify to.
56 * @return a L2 modification
57 */
Ayaka Koshibea9c199f2014-09-16 16:21:40 -070058 public static L2ModificationInstruction modL2Dst(MacAddress addr) {
alshabib55a55d92014-09-16 11:59:31 -070059 checkNotNull(addr, "Dst l2 address cannot be null");
alshabib7410fea2014-09-16 13:48:39 -070060 return new L2ModificationInstruction.ModEtherInstruction(L2SubType.L2_DST, addr);
alshabib55a55d92014-09-16 11:59:31 -070061 }
62
63 /**
64 * Creates a L2 type modification.
65 * @param l2Type the type to change to
66 * @return a L2 modifications
67 */
68 public static L2ModificationInstruction modL2Type(Short l2Type) {
69 checkNotNull(l2Type, "L2 type cannot be null");
70 return new L2ModificationInstruction.ModEtherTypeInstruction(l2Type);
71 }
72
alshabib7410fea2014-09-16 13:48:39 -070073 /**
74 * Creates a Vlan id modification.
75 * @param vlanId the vlan id to modify to.
76 * @return a L2 modification
77 */
Ayaka Koshibea9c199f2014-09-16 16:21:40 -070078 public static L2ModificationInstruction modVlanId(VlanId vlanId) {
alshabib55a55d92014-09-16 11:59:31 -070079 checkNotNull(vlanId, "VLAN id cannot be null");
80 return new L2ModificationInstruction.ModVlanIdInstruction(vlanId);
81 }
82
alshabib7410fea2014-09-16 13:48:39 -070083 /**
84 * Creates a Vlan pcp modification.
85 * @param vlanPcp the pcp to modify to.
86 * @return a L2 modification
87 */
88 public static L2ModificationInstruction modVlanPcp(Byte vlanPcp) {
89 checkNotNull(vlanPcp, "VLAN Pcp cannot be null");
90 return new L2ModificationInstruction.ModVlanPcpInstruction(vlanPcp);
91 }
92
93 /**
94 * Creates a L3 src modification.
95 * @param addr the ip address to modify to.
96 * @return a L3 modification
97 */
Ayaka Koshibea9c199f2014-09-16 16:21:40 -070098 public static L3ModificationInstruction modL3Src(IpAddress addr) {
alshabib7410fea2014-09-16 13:48:39 -070099 checkNotNull(addr, "Src l3 address cannot be null");
100 return new ModIPInstruction(L3SubType.L3_SRC, addr);
101 }
102
103 /**
104 * Creates a L3 dst modification.
105 * @param addr the ip address to modify to.
106 * @return a L3 modification
107 */
Ayaka Koshibea9c199f2014-09-16 16:21:40 -0700108 public static L3ModificationInstruction modL3Dst(IpAddress addr) {
alshabib7410fea2014-09-16 13:48:39 -0700109 checkNotNull(addr, "Dst l3 address cannot be null");
110 return new ModIPInstruction(L3SubType.L3_DST, addr);
111 }
112
113 /**
114 * Creates an L3 protocol modification.
115 * @param proto the protocol to change to
116 * @return a L3 modification
117 */
118 public static L3ModificationInstruction modIPProto(Byte proto) {
119 checkNotNull(proto, "IP protocol cannot be null");
120 return new ModIPProtoInstruction(proto);
121 }
122
alshabib55a55d92014-09-16 11:59:31 -0700123 /*
124 * Output instructions
125 */
126
127 public static final class DropInstruction implements Instruction {
128 @Override
129 public Type type() {
130 return Type.DROP;
131 }
alshabib7410fea2014-09-16 13:48:39 -0700132
133 @Override
134 public SubType subtype() {
135 return NoneSubType.NONE;
136 }
alshabib55a55d92014-09-16 11:59:31 -0700137 }
138
139
140 public static final class OutputInstruction implements Instruction {
141 private final PortNumber port;
142
143 private OutputInstruction(PortNumber port) {
144 this.port = port;
145 }
146
147 public PortNumber port() {
148 return port;
149 }
150
151 @Override
152 public Type type() {
153 return Type.OUTPUT;
154 }
alshabib7410fea2014-09-16 13:48:39 -0700155
156 @Override
157 public SubType subtype() {
158 return NoneSubType.NONE;
159 }
alshabib55a55d92014-09-16 11:59:31 -0700160 }
161
162}