alshabib | 55a55d9 | 2014-09-16 11:59:31 -0700 | [diff] [blame] | 1 | package org.onlab.onos.net.flow.instructions; |
| 2 | |
| 3 | import static com.google.common.base.Preconditions.checkNotNull; |
| 4 | |
| 5 | import org.onlab.onos.net.PortNumber; |
alshabib | 7410fea | 2014-09-16 13:48:39 -0700 | [diff] [blame] | 6 | import org.onlab.onos.net.flow.instructions.L2ModificationInstruction.L2SubType; |
alshabib | 55a55d9 | 2014-09-16 11:59:31 -0700 | [diff] [blame] | 7 | import org.onlab.onos.net.flow.instructions.L2ModificationInstruction.ModEtherInstruction; |
alshabib | 7410fea | 2014-09-16 13:48:39 -0700 | [diff] [blame] | 8 | import org.onlab.onos.net.flow.instructions.L3ModificationInstruction.L3SubType; |
| 9 | import org.onlab.onos.net.flow.instructions.L3ModificationInstruction.ModIPInstruction; |
alshabib | 7410fea | 2014-09-16 13:48:39 -0700 | [diff] [blame] | 10 | import org.onlab.packet.IPAddress; |
alshabib | 55a55d9 | 2014-09-16 11:59:31 -0700 | [diff] [blame] | 11 | import org.onlab.packet.MACAddress; |
alshabib | 7b79549 | 2014-09-16 14:38:39 -0700 | [diff] [blame] | 12 | import org.onlab.packet.VLANID; |
alshabib | 55a55d9 | 2014-09-16 11:59:31 -0700 | [diff] [blame] | 13 | /** |
| 14 | * Factory class for creating various traffic treatment instructions. |
| 15 | */ |
| 16 | public 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"); |
alshabib | 7410fea | 2014-09-16 13:48:39 -0700 | [diff] [blame] | 49 | return new ModEtherInstruction(L2SubType.L2_SRC, addr); |
alshabib | 55a55d9 | 2014-09-16 11:59:31 -0700 | [diff] [blame] | 50 | } |
| 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"); |
alshabib | 7410fea | 2014-09-16 13:48:39 -0700 | [diff] [blame] | 59 | return new L2ModificationInstruction.ModEtherInstruction(L2SubType.L2_DST, addr); |
alshabib | 55a55d9 | 2014-09-16 11:59:31 -0700 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | /** |
alshabib | 7410fea | 2014-09-16 13:48:39 -0700 | [diff] [blame] | 63 | * Creates a Vlan id modification. |
| 64 | * @param vlanId the vlan id to modify to. |
| 65 | * @return a L2 modification |
| 66 | */ |
alshabib | 7b79549 | 2014-09-16 14:38:39 -0700 | [diff] [blame] | 67 | public static L2ModificationInstruction modVlanId(VLANID vlanId) { |
alshabib | 55a55d9 | 2014-09-16 11:59:31 -0700 | [diff] [blame] | 68 | checkNotNull(vlanId, "VLAN id cannot be null"); |
| 69 | return new L2ModificationInstruction.ModVlanIdInstruction(vlanId); |
| 70 | } |
| 71 | |
alshabib | 7410fea | 2014-09-16 13:48:39 -0700 | [diff] [blame] | 72 | /** |
| 73 | * Creates a Vlan pcp modification. |
| 74 | * @param vlanPcp the pcp to modify to. |
| 75 | * @return a L2 modification |
| 76 | */ |
| 77 | public static L2ModificationInstruction modVlanPcp(Byte vlanPcp) { |
| 78 | checkNotNull(vlanPcp, "VLAN Pcp cannot be null"); |
| 79 | return new L2ModificationInstruction.ModVlanPcpInstruction(vlanPcp); |
| 80 | } |
| 81 | |
| 82 | /** |
| 83 | * Creates a L3 src modification. |
| 84 | * @param addr the ip address to modify to. |
| 85 | * @return a L3 modification |
| 86 | */ |
| 87 | public static L3ModificationInstruction modL3Src(IPAddress addr) { |
| 88 | checkNotNull(addr, "Src l3 address cannot be null"); |
| 89 | return new ModIPInstruction(L3SubType.L3_SRC, addr); |
| 90 | } |
| 91 | |
| 92 | /** |
| 93 | * Creates a L3 dst modification. |
| 94 | * @param addr the ip address to modify to. |
| 95 | * @return a L3 modification |
| 96 | */ |
| 97 | public static L3ModificationInstruction modL3Dst(IPAddress addr) { |
| 98 | checkNotNull(addr, "Dst l3 address cannot be null"); |
| 99 | return new ModIPInstruction(L3SubType.L3_DST, addr); |
| 100 | } |
| 101 | |
alshabib | 7410fea | 2014-09-16 13:48:39 -0700 | [diff] [blame] | 102 | |
alshabib | 55a55d9 | 2014-09-16 11:59:31 -0700 | [diff] [blame] | 103 | /* |
| 104 | * Output instructions |
| 105 | */ |
| 106 | |
| 107 | public static final class DropInstruction implements Instruction { |
| 108 | @Override |
| 109 | public Type type() { |
| 110 | return Type.DROP; |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | |
| 115 | public static final class OutputInstruction implements Instruction { |
| 116 | private final PortNumber port; |
| 117 | |
| 118 | private OutputInstruction(PortNumber port) { |
| 119 | this.port = port; |
| 120 | } |
| 121 | |
| 122 | public PortNumber port() { |
| 123 | return port; |
| 124 | } |
| 125 | |
| 126 | @Override |
| 127 | public Type type() { |
| 128 | return Type.OUTPUT; |
| 129 | } |
alshabib | 55a55d9 | 2014-09-16 11:59:31 -0700 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | } |