alshabib | 55a55d9 | 2014-09-16 11:59:31 -0700 | [diff] [blame] | 1 | package org.onlab.onos.net.flow.instructions; |
| 2 | |
alshabib | 99b8fdc | 2014-09-25 14:30:22 -0700 | [diff] [blame^] | 3 | import static com.google.common.base.MoreObjects.toStringHelper; |
alshabib | 55a55d9 | 2014-09-16 11:59:31 -0700 | [diff] [blame] | 4 | import static com.google.common.base.Preconditions.checkNotNull; |
| 5 | |
| 6 | import org.onlab.onos.net.PortNumber; |
alshabib | 7410fea | 2014-09-16 13:48:39 -0700 | [diff] [blame] | 7 | import org.onlab.onos.net.flow.instructions.L2ModificationInstruction.L2SubType; |
alshabib | 55a55d9 | 2014-09-16 11:59:31 -0700 | [diff] [blame] | 8 | import org.onlab.onos.net.flow.instructions.L2ModificationInstruction.ModEtherInstruction; |
alshabib | 7410fea | 2014-09-16 13:48:39 -0700 | [diff] [blame] | 9 | import org.onlab.onos.net.flow.instructions.L3ModificationInstruction.L3SubType; |
| 10 | import org.onlab.onos.net.flow.instructions.L3ModificationInstruction.ModIPInstruction; |
Ayaka Koshibe | 1d56fe4 | 2014-09-19 16:51:58 -0700 | [diff] [blame] | 11 | import org.onlab.packet.IpPrefix; |
Ayaka Koshibe | a9c199f | 2014-09-16 16:21:40 -0700 | [diff] [blame] | 12 | import org.onlab.packet.MacAddress; |
| 13 | import org.onlab.packet.VlanId; |
alshabib | 64231f6 | 2014-09-16 17:58:36 -0700 | [diff] [blame] | 14 | |
alshabib | 55a55d9 | 2014-09-16 11:59:31 -0700 | [diff] [blame] | 15 | /** |
| 16 | * Factory class for creating various traffic treatment instructions. |
| 17 | */ |
| 18 | public 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 Koshibe | a9c199f | 2014-09-16 16:21:40 -0700 | [diff] [blame] | 49 | public static L2ModificationInstruction modL2Src(MacAddress addr) { |
alshabib | 55a55d9 | 2014-09-16 11:59:31 -0700 | [diff] [blame] | 50 | checkNotNull(addr, "Src l2 address cannot be null"); |
alshabib | 99b8fdc | 2014-09-25 14:30:22 -0700 | [diff] [blame^] | 51 | return new ModEtherInstruction(L2SubType.ETH_SRC, addr); |
alshabib | 55a55d9 | 2014-09-16 11:59:31 -0700 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | /** |
| 55 | * Creates a L2 dst modification. |
| 56 | * @param addr the mac address to modify to. |
| 57 | * @return a L2 modification |
| 58 | */ |
Ayaka Koshibe | a9c199f | 2014-09-16 16:21:40 -0700 | [diff] [blame] | 59 | public static L2ModificationInstruction modL2Dst(MacAddress addr) { |
alshabib | 55a55d9 | 2014-09-16 11:59:31 -0700 | [diff] [blame] | 60 | checkNotNull(addr, "Dst l2 address cannot be null"); |
alshabib | 99b8fdc | 2014-09-25 14:30:22 -0700 | [diff] [blame^] | 61 | return new L2ModificationInstruction.ModEtherInstruction(L2SubType.ETH_DST, addr); |
alshabib | 55a55d9 | 2014-09-16 11:59:31 -0700 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | /** |
alshabib | 7410fea | 2014-09-16 13:48:39 -0700 | [diff] [blame] | 65 | * Creates a Vlan id modification. |
| 66 | * @param vlanId the vlan id to modify to. |
| 67 | * @return a L2 modification |
| 68 | */ |
Ayaka Koshibe | a9c199f | 2014-09-16 16:21:40 -0700 | [diff] [blame] | 69 | public static L2ModificationInstruction modVlanId(VlanId vlanId) { |
alshabib | 55a55d9 | 2014-09-16 11:59:31 -0700 | [diff] [blame] | 70 | checkNotNull(vlanId, "VLAN id cannot be null"); |
| 71 | return new L2ModificationInstruction.ModVlanIdInstruction(vlanId); |
| 72 | } |
| 73 | |
alshabib | 7410fea | 2014-09-16 13:48:39 -0700 | [diff] [blame] | 74 | /** |
| 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 Koshibe | 1d56fe4 | 2014-09-19 16:51:58 -0700 | [diff] [blame] | 89 | public static L3ModificationInstruction modL3Src(IpPrefix addr) { |
alshabib | 7410fea | 2014-09-16 13:48:39 -0700 | [diff] [blame] | 90 | checkNotNull(addr, "Src l3 address cannot be null"); |
alshabib | 99b8fdc | 2014-09-25 14:30:22 -0700 | [diff] [blame^] | 91 | return new ModIPInstruction(L3SubType.IP_SRC, addr); |
alshabib | 7410fea | 2014-09-16 13:48:39 -0700 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | /** |
| 95 | * Creates a L3 dst modification. |
| 96 | * @param addr the ip address to modify to. |
| 97 | * @return a L3 modification |
| 98 | */ |
Ayaka Koshibe | 1d56fe4 | 2014-09-19 16:51:58 -0700 | [diff] [blame] | 99 | public static L3ModificationInstruction modL3Dst(IpPrefix addr) { |
alshabib | 7410fea | 2014-09-16 13:48:39 -0700 | [diff] [blame] | 100 | checkNotNull(addr, "Dst l3 address cannot be null"); |
alshabib | 99b8fdc | 2014-09-25 14:30:22 -0700 | [diff] [blame^] | 101 | return new ModIPInstruction(L3SubType.IP_DST, addr); |
alshabib | 7410fea | 2014-09-16 13:48:39 -0700 | [diff] [blame] | 102 | } |
| 103 | |
alshabib | 7410fea | 2014-09-16 13:48:39 -0700 | [diff] [blame] | 104 | |
alshabib | 55a55d9 | 2014-09-16 11:59:31 -0700 | [diff] [blame] | 105 | /* |
| 106 | * Output instructions |
| 107 | */ |
| 108 | |
| 109 | public static final class DropInstruction implements Instruction { |
| 110 | @Override |
| 111 | public Type type() { |
| 112 | return Type.DROP; |
| 113 | } |
alshabib | 99b8fdc | 2014-09-25 14:30:22 -0700 | [diff] [blame^] | 114 | |
| 115 | @Override |
| 116 | public String toString() { |
| 117 | return toStringHelper(type()).toString(); |
| 118 | |
| 119 | } |
alshabib | 55a55d9 | 2014-09-16 11:59:31 -0700 | [diff] [blame] | 120 | } |
| 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 | } |
alshabib | 99b8fdc | 2014-09-25 14:30:22 -0700 | [diff] [blame^] | 138 | @Override |
| 139 | public String toString() { |
| 140 | return toStringHelper(type().toString()) |
| 141 | .add("port", port).toString(); |
| 142 | } |
alshabib | 55a55d9 | 2014-09-16 11:59:31 -0700 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | } |