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