blob: 5acac3eea1c744ee5b391adc6c3a5321ee615432 [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;
11import org.onlab.packet.IPAddress;
alshabib55a55d92014-09-16 11:59:31 -070012import org.onlab.packet.MACAddress;
13/**
14 * Factory class for creating various traffic treatment instructions.
15 */
16public final class Instructions {
17
18
19 // Ban construction
20 private Instructions() {}
21
22 /**
23 * Creates an output instruction using the specified port number. This can
24 * include logical ports such as CONTROLLER, FLOOD, etc.
25 *
26 * @param number port number
27 * @return output instruction
28 */
29 public static OutputInstruction createOutput(final PortNumber number) {
30 checkNotNull(number, "PortNumber cannot be null");
31 return new OutputInstruction(number);
32 }
33
34 /**
35 * Creates a drop instruction.
36 * @return drop instruction
37 */
38 public static DropInstruction createDrop() {
39 return new DropInstruction();
40 }
41
42 /**
43 * Creates a l2 src modification.
44 * @param addr the mac address to modify to.
45 * @return a l2 modification
46 */
47 public static L2ModificationInstruction modL2Src(MACAddress addr) {
48 checkNotNull(addr, "Src l2 address cannot be null");
alshabib7410fea2014-09-16 13:48:39 -070049 return new ModEtherInstruction(L2SubType.L2_SRC, addr);
alshabib55a55d92014-09-16 11:59:31 -070050 }
51
52 /**
53 * Creates a L2 dst modification.
54 * @param addr the mac address to modify to.
55 * @return a L2 modification
56 */
57 public static L2ModificationInstruction modL2Dst(MACAddress addr) {
58 checkNotNull(addr, "Dst l2 address cannot be null");
alshabib7410fea2014-09-16 13:48:39 -070059 return new L2ModificationInstruction.ModEtherInstruction(L2SubType.L2_DST, addr);
alshabib55a55d92014-09-16 11:59:31 -070060 }
61
62 /**
63 * Creates a L2 type modification.
64 * @param l2Type the type to change to
65 * @return a L2 modifications
66 */
67 public static L2ModificationInstruction modL2Type(Short l2Type) {
68 checkNotNull(l2Type, "L2 type cannot be null");
69 return new L2ModificationInstruction.ModEtherTypeInstruction(l2Type);
70 }
71
alshabib7410fea2014-09-16 13:48:39 -070072 /**
73 * Creates a Vlan id modification.
74 * @param vlanId the vlan id to modify to.
75 * @return a L2 modification
76 */
alshabib55a55d92014-09-16 11:59:31 -070077 public static L2ModificationInstruction modVlanId(Short vlanId) {
78 checkNotNull(vlanId, "VLAN id cannot be null");
79 return new L2ModificationInstruction.ModVlanIdInstruction(vlanId);
80 }
81
alshabib7410fea2014-09-16 13:48:39 -070082 /**
83 * Creates a Vlan pcp modification.
84 * @param vlanPcp the pcp to modify to.
85 * @return a L2 modification
86 */
87 public static L2ModificationInstruction modVlanPcp(Byte vlanPcp) {
88 checkNotNull(vlanPcp, "VLAN Pcp cannot be null");
89 return new L2ModificationInstruction.ModVlanPcpInstruction(vlanPcp);
90 }
91
92 /**
93 * Creates a L3 src modification.
94 * @param addr the ip address to modify to.
95 * @return a L3 modification
96 */
97 public static L3ModificationInstruction modL3Src(IPAddress addr) {
98 checkNotNull(addr, "Src l3 address cannot be null");
99 return new ModIPInstruction(L3SubType.L3_SRC, addr);
100 }
101
102 /**
103 * Creates a L3 dst modification.
104 * @param addr the ip address to modify to.
105 * @return a L3 modification
106 */
107 public static L3ModificationInstruction modL3Dst(IPAddress addr) {
108 checkNotNull(addr, "Dst l3 address cannot be null");
109 return new ModIPInstruction(L3SubType.L3_DST, addr);
110 }
111
112 /**
113 * Creates an L3 protocol modification.
114 * @param proto the protocol to change to
115 * @return a L3 modification
116 */
117 public static L3ModificationInstruction modIPProto(Byte proto) {
118 checkNotNull(proto, "IP protocol cannot be null");
119 return new ModIPProtoInstruction(proto);
120 }
121
alshabib55a55d92014-09-16 11:59:31 -0700122 /*
123 * Output instructions
124 */
125
126 public static final class DropInstruction implements Instruction {
127 @Override
128 public Type type() {
129 return Type.DROP;
130 }
alshabib7410fea2014-09-16 13:48:39 -0700131
132 @Override
133 public SubType subtype() {
134 return NoneSubType.NONE;
135 }
alshabib55a55d92014-09-16 11:59:31 -0700136 }
137
138
139 public static final class OutputInstruction implements Instruction {
140 private final PortNumber port;
141
142 private OutputInstruction(PortNumber port) {
143 this.port = port;
144 }
145
146 public PortNumber port() {
147 return port;
148 }
149
150 @Override
151 public Type type() {
152 return Type.OUTPUT;
153 }
alshabib7410fea2014-09-16 13:48:39 -0700154
155 @Override
156 public SubType subtype() {
157 return NoneSubType.NONE;
158 }
alshabib55a55d92014-09-16 11:59:31 -0700159 }
160
161}