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; |
| 10 | import org.onlab.onos.net.flow.instructions.L3ModificationInstruction.ModIPProtoInstruction; |
| 11 | import org.onlab.packet.IPAddress; |
alshabib | 55a55d9 | 2014-09-16 11:59:31 -0700 | [diff] [blame] | 12 | import org.onlab.packet.MACAddress; |
| 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 | /** |
| 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 | |
alshabib | 7410fea | 2014-09-16 13:48:39 -0700 | [diff] [blame^] | 72 | /** |
| 73 | * Creates a Vlan id modification. |
| 74 | * @param vlanId the vlan id to modify to. |
| 75 | * @return a L2 modification |
| 76 | */ |
alshabib | 55a55d9 | 2014-09-16 11:59:31 -0700 | [diff] [blame] | 77 | public static L2ModificationInstruction modVlanId(Short vlanId) { |
| 78 | checkNotNull(vlanId, "VLAN id cannot be null"); |
| 79 | return new L2ModificationInstruction.ModVlanIdInstruction(vlanId); |
| 80 | } |
| 81 | |
alshabib | 7410fea | 2014-09-16 13:48:39 -0700 | [diff] [blame^] | 82 | /** |
| 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 | |
alshabib | 55a55d9 | 2014-09-16 11:59:31 -0700 | [diff] [blame] | 122 | /* |
| 123 | * Output instructions |
| 124 | */ |
| 125 | |
| 126 | public static final class DropInstruction implements Instruction { |
| 127 | @Override |
| 128 | public Type type() { |
| 129 | return Type.DROP; |
| 130 | } |
alshabib | 7410fea | 2014-09-16 13:48:39 -0700 | [diff] [blame^] | 131 | |
| 132 | @Override |
| 133 | public SubType subtype() { |
| 134 | return NoneSubType.NONE; |
| 135 | } |
alshabib | 55a55d9 | 2014-09-16 11:59:31 -0700 | [diff] [blame] | 136 | } |
| 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 | } |
alshabib | 7410fea | 2014-09-16 13:48:39 -0700 | [diff] [blame^] | 154 | |
| 155 | @Override |
| 156 | public SubType subtype() { |
| 157 | return NoneSubType.NONE; |
| 158 | } |
alshabib | 55a55d9 | 2014-09-16 11:59:31 -0700 | [diff] [blame] | 159 | } |
| 160 | |
| 161 | } |