blob: 1bf5531918f98ec7bdaa14b35bf9dc848c4fbc70 [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
6import org.onlab.onos.net.PortNumber;
alshabib7410fea2014-09-16 13:48:39 -07007import org.onlab.onos.net.flow.instructions.L2ModificationInstruction.L2SubType;
alshabib55a55d92014-09-16 11:59:31 -07008import org.onlab.onos.net.flow.instructions.L2ModificationInstruction.ModEtherInstruction;
alshabib7410fea2014-09-16 13:48:39 -07009import org.onlab.onos.net.flow.instructions.L3ModificationInstruction.L3SubType;
10import org.onlab.onos.net.flow.instructions.L3ModificationInstruction.ModIPInstruction;
Ayaka Koshibe1d56fe42014-09-19 16:51:58 -070011import org.onlab.packet.IpPrefix;
Ayaka Koshibea9c199f2014-09-16 16:21:40 -070012import org.onlab.packet.MacAddress;
13import org.onlab.packet.VlanId;
alshabib64231f62014-09-16 17:58:36 -070014
alshabib55a55d92014-09-16 11:59:31 -070015/**
16 * Factory class for creating various traffic treatment instructions.
17 */
18public final class Instructions {
19
20
21 // Ban construction
22 private Instructions() {}
23
24 /**
25 * Creates an output instruction using the specified port number. This can
26 * include logical ports such as CONTROLLER, FLOOD, etc.
27 *
28 * @param number port number
29 * @return output instruction
30 */
31 public static OutputInstruction createOutput(final PortNumber number) {
32 checkNotNull(number, "PortNumber cannot be null");
33 return new OutputInstruction(number);
34 }
35
36 /**
37 * Creates a drop instruction.
38 * @return drop instruction
39 */
40 public static DropInstruction createDrop() {
41 return new DropInstruction();
42 }
43
44 /**
45 * Creates a l2 src modification.
46 * @param addr the mac address to modify to.
47 * @return a l2 modification
48 */
Ayaka Koshibea9c199f2014-09-16 16:21:40 -070049 public static L2ModificationInstruction modL2Src(MacAddress addr) {
alshabib55a55d92014-09-16 11:59:31 -070050 checkNotNull(addr, "Src l2 address cannot be null");
alshabib99b8fdc2014-09-25 14:30:22 -070051 return new ModEtherInstruction(L2SubType.ETH_SRC, addr);
alshabib55a55d92014-09-16 11:59:31 -070052 }
53
54 /**
55 * Creates a L2 dst modification.
56 * @param addr the mac address to modify to.
57 * @return a L2 modification
58 */
Ayaka Koshibea9c199f2014-09-16 16:21:40 -070059 public static L2ModificationInstruction modL2Dst(MacAddress addr) {
alshabib55a55d92014-09-16 11:59:31 -070060 checkNotNull(addr, "Dst l2 address cannot be null");
alshabib99b8fdc2014-09-25 14:30:22 -070061 return new L2ModificationInstruction.ModEtherInstruction(L2SubType.ETH_DST, addr);
alshabib55a55d92014-09-16 11:59:31 -070062 }
63
64 /**
alshabib7410fea2014-09-16 13:48:39 -070065 * Creates a Vlan id modification.
66 * @param vlanId the vlan id to modify to.
67 * @return a L2 modification
68 */
Ayaka Koshibea9c199f2014-09-16 16:21:40 -070069 public static L2ModificationInstruction modVlanId(VlanId vlanId) {
alshabib55a55d92014-09-16 11:59:31 -070070 checkNotNull(vlanId, "VLAN id cannot be null");
71 return new L2ModificationInstruction.ModVlanIdInstruction(vlanId);
72 }
73
alshabib7410fea2014-09-16 13:48:39 -070074 /**
75 * Creates a Vlan pcp modification.
76 * @param vlanPcp the pcp to modify to.
77 * @return a L2 modification
78 */
79 public static L2ModificationInstruction modVlanPcp(Byte vlanPcp) {
80 checkNotNull(vlanPcp, "VLAN Pcp cannot be null");
81 return new L2ModificationInstruction.ModVlanPcpInstruction(vlanPcp);
82 }
83
84 /**
85 * Creates a L3 src modification.
86 * @param addr the ip address to modify to.
87 * @return a L3 modification
88 */
Ayaka Koshibe1d56fe42014-09-19 16:51:58 -070089 public static L3ModificationInstruction modL3Src(IpPrefix addr) {
alshabib7410fea2014-09-16 13:48:39 -070090 checkNotNull(addr, "Src l3 address cannot be null");
alshabib99b8fdc2014-09-25 14:30:22 -070091 return new ModIPInstruction(L3SubType.IP_SRC, addr);
alshabib7410fea2014-09-16 13:48:39 -070092 }
93
94 /**
95 * Creates a L3 dst modification.
96 * @param addr the ip address to modify to.
97 * @return a L3 modification
98 */
Ayaka Koshibe1d56fe42014-09-19 16:51:58 -070099 public static L3ModificationInstruction modL3Dst(IpPrefix addr) {
alshabib7410fea2014-09-16 13:48:39 -0700100 checkNotNull(addr, "Dst l3 address cannot be null");
alshabib99b8fdc2014-09-25 14:30:22 -0700101 return new ModIPInstruction(L3SubType.IP_DST, addr);
alshabib7410fea2014-09-16 13:48:39 -0700102 }
103
alshabib7410fea2014-09-16 13:48:39 -0700104
alshabib55a55d92014-09-16 11:59:31 -0700105 /*
106 * Output instructions
107 */
108
109 public static final class DropInstruction implements Instruction {
110 @Override
111 public Type type() {
112 return Type.DROP;
113 }
alshabib99b8fdc2014-09-25 14:30:22 -0700114
115 @Override
116 public String toString() {
117 return toStringHelper(type()).toString();
118
119 }
alshabib55a55d92014-09-16 11:59:31 -0700120 }
121
122
123 public static final class OutputInstruction implements Instruction {
124 private final PortNumber port;
125
126 private OutputInstruction(PortNumber port) {
127 this.port = port;
128 }
129
130 public PortNumber port() {
131 return port;
132 }
133
134 @Override
135 public Type type() {
136 return Type.OUTPUT;
137 }
alshabib99b8fdc2014-09-25 14:30:22 -0700138 @Override
139 public String toString() {
140 return toStringHelper(type().toString())
141 .add("port", port).toString();
142 }
alshabib55a55d92014-09-16 11:59:31 -0700143 }
144
145}