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 | |
alshabib | 8ca5390 | 2014-10-07 13:11:17 -0700 | [diff] [blame^] | 6 | import java.util.Objects; |
| 7 | |
alshabib | 55a55d9 | 2014-09-16 11:59:31 -0700 | [diff] [blame] | 8 | import org.onlab.onos.net.PortNumber; |
alshabib | 7410fea | 2014-09-16 13:48:39 -0700 | [diff] [blame] | 9 | import org.onlab.onos.net.flow.instructions.L2ModificationInstruction.L2SubType; |
alshabib | 55a55d9 | 2014-09-16 11:59:31 -0700 | [diff] [blame] | 10 | import org.onlab.onos.net.flow.instructions.L2ModificationInstruction.ModEtherInstruction; |
alshabib | 7410fea | 2014-09-16 13:48:39 -0700 | [diff] [blame] | 11 | import org.onlab.onos.net.flow.instructions.L3ModificationInstruction.L3SubType; |
| 12 | import org.onlab.onos.net.flow.instructions.L3ModificationInstruction.ModIPInstruction; |
Ayaka Koshibe | 1d56fe4 | 2014-09-19 16:51:58 -0700 | [diff] [blame] | 13 | import org.onlab.packet.IpPrefix; |
Ayaka Koshibe | a9c199f | 2014-09-16 16:21:40 -0700 | [diff] [blame] | 14 | import org.onlab.packet.MacAddress; |
| 15 | import org.onlab.packet.VlanId; |
alshabib | 64231f6 | 2014-09-16 17:58:36 -0700 | [diff] [blame] | 16 | |
alshabib | 55a55d9 | 2014-09-16 11:59:31 -0700 | [diff] [blame] | 17 | /** |
| 18 | * Factory class for creating various traffic treatment instructions. |
| 19 | */ |
| 20 | public final class Instructions { |
| 21 | |
| 22 | |
| 23 | // Ban construction |
| 24 | private Instructions() {} |
| 25 | |
| 26 | /** |
| 27 | * Creates an output instruction using the specified port number. This can |
| 28 | * include logical ports such as CONTROLLER, FLOOD, etc. |
| 29 | * |
| 30 | * @param number port number |
| 31 | * @return output instruction |
| 32 | */ |
| 33 | public static OutputInstruction createOutput(final PortNumber number) { |
| 34 | checkNotNull(number, "PortNumber cannot be null"); |
| 35 | return new OutputInstruction(number); |
| 36 | } |
| 37 | |
| 38 | /** |
| 39 | * Creates a drop instruction. |
| 40 | * @return drop instruction |
| 41 | */ |
| 42 | public static DropInstruction createDrop() { |
| 43 | return new DropInstruction(); |
| 44 | } |
| 45 | |
| 46 | /** |
| 47 | * Creates a l2 src modification. |
| 48 | * @param addr the mac address to modify to. |
| 49 | * @return a l2 modification |
| 50 | */ |
Ayaka Koshibe | a9c199f | 2014-09-16 16:21:40 -0700 | [diff] [blame] | 51 | public static L2ModificationInstruction modL2Src(MacAddress addr) { |
alshabib | 55a55d9 | 2014-09-16 11:59:31 -0700 | [diff] [blame] | 52 | checkNotNull(addr, "Src l2 address cannot be null"); |
alshabib | 99b8fdc | 2014-09-25 14:30:22 -0700 | [diff] [blame] | 53 | return new ModEtherInstruction(L2SubType.ETH_SRC, addr); |
alshabib | 55a55d9 | 2014-09-16 11:59:31 -0700 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | /** |
| 57 | * Creates a L2 dst modification. |
| 58 | * @param addr the mac address to modify to. |
| 59 | * @return a L2 modification |
| 60 | */ |
Ayaka Koshibe | a9c199f | 2014-09-16 16:21:40 -0700 | [diff] [blame] | 61 | public static L2ModificationInstruction modL2Dst(MacAddress addr) { |
alshabib | 55a55d9 | 2014-09-16 11:59:31 -0700 | [diff] [blame] | 62 | checkNotNull(addr, "Dst l2 address cannot be null"); |
alshabib | 99b8fdc | 2014-09-25 14:30:22 -0700 | [diff] [blame] | 63 | return new L2ModificationInstruction.ModEtherInstruction(L2SubType.ETH_DST, addr); |
alshabib | 55a55d9 | 2014-09-16 11:59:31 -0700 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | /** |
alshabib | 7410fea | 2014-09-16 13:48:39 -0700 | [diff] [blame] | 67 | * Creates a Vlan id modification. |
| 68 | * @param vlanId the vlan id to modify to. |
| 69 | * @return a L2 modification |
| 70 | */ |
Ayaka Koshibe | a9c199f | 2014-09-16 16:21:40 -0700 | [diff] [blame] | 71 | public static L2ModificationInstruction modVlanId(VlanId vlanId) { |
alshabib | 55a55d9 | 2014-09-16 11:59:31 -0700 | [diff] [blame] | 72 | checkNotNull(vlanId, "VLAN id cannot be null"); |
| 73 | return new L2ModificationInstruction.ModVlanIdInstruction(vlanId); |
| 74 | } |
| 75 | |
alshabib | 7410fea | 2014-09-16 13:48:39 -0700 | [diff] [blame] | 76 | /** |
| 77 | * Creates a Vlan pcp modification. |
| 78 | * @param vlanPcp the pcp to modify to. |
| 79 | * @return a L2 modification |
| 80 | */ |
| 81 | public static L2ModificationInstruction modVlanPcp(Byte vlanPcp) { |
| 82 | checkNotNull(vlanPcp, "VLAN Pcp cannot be null"); |
| 83 | return new L2ModificationInstruction.ModVlanPcpInstruction(vlanPcp); |
| 84 | } |
| 85 | |
| 86 | /** |
| 87 | * Creates a L3 src modification. |
| 88 | * @param addr the ip address to modify to. |
| 89 | * @return a L3 modification |
| 90 | */ |
Ayaka Koshibe | 1d56fe4 | 2014-09-19 16:51:58 -0700 | [diff] [blame] | 91 | public static L3ModificationInstruction modL3Src(IpPrefix addr) { |
alshabib | 7410fea | 2014-09-16 13:48:39 -0700 | [diff] [blame] | 92 | checkNotNull(addr, "Src l3 address cannot be null"); |
alshabib | 99b8fdc | 2014-09-25 14:30:22 -0700 | [diff] [blame] | 93 | return new ModIPInstruction(L3SubType.IP_SRC, addr); |
alshabib | 7410fea | 2014-09-16 13:48:39 -0700 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | /** |
| 97 | * Creates a L3 dst modification. |
| 98 | * @param addr the ip address to modify to. |
| 99 | * @return a L3 modification |
| 100 | */ |
Ayaka Koshibe | 1d56fe4 | 2014-09-19 16:51:58 -0700 | [diff] [blame] | 101 | public static L3ModificationInstruction modL3Dst(IpPrefix addr) { |
alshabib | 7410fea | 2014-09-16 13:48:39 -0700 | [diff] [blame] | 102 | checkNotNull(addr, "Dst l3 address cannot be null"); |
alshabib | 99b8fdc | 2014-09-25 14:30:22 -0700 | [diff] [blame] | 103 | return new ModIPInstruction(L3SubType.IP_DST, addr); |
alshabib | 7410fea | 2014-09-16 13:48:39 -0700 | [diff] [blame] | 104 | } |
| 105 | |
alshabib | 7410fea | 2014-09-16 13:48:39 -0700 | [diff] [blame] | 106 | |
alshabib | 55a55d9 | 2014-09-16 11:59:31 -0700 | [diff] [blame] | 107 | /* |
| 108 | * Output instructions |
| 109 | */ |
| 110 | |
| 111 | public static final class DropInstruction implements Instruction { |
| 112 | @Override |
| 113 | public Type type() { |
| 114 | return Type.DROP; |
| 115 | } |
alshabib | 99b8fdc | 2014-09-25 14:30:22 -0700 | [diff] [blame] | 116 | |
| 117 | @Override |
| 118 | public String toString() { |
| 119 | return toStringHelper(type()).toString(); |
| 120 | |
| 121 | } |
alshabib | 8ca5390 | 2014-10-07 13:11:17 -0700 | [diff] [blame^] | 122 | |
| 123 | @Override |
| 124 | public int hashCode() { |
| 125 | return Objects.hash(type()); |
| 126 | } |
| 127 | |
| 128 | @Override |
| 129 | public boolean equals(Object obj) { |
| 130 | if (this == obj) { |
| 131 | return true; |
| 132 | } |
| 133 | if (obj instanceof DropInstruction) { |
| 134 | DropInstruction that = (DropInstruction) obj; |
| 135 | return Objects.equals(type(), that.type()); |
| 136 | |
| 137 | } |
| 138 | return false; |
| 139 | } |
alshabib | 55a55d9 | 2014-09-16 11:59:31 -0700 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | |
| 143 | public static final class OutputInstruction implements Instruction { |
| 144 | private final PortNumber port; |
| 145 | |
| 146 | private OutputInstruction(PortNumber port) { |
| 147 | this.port = port; |
| 148 | } |
| 149 | |
| 150 | public PortNumber port() { |
| 151 | return port; |
| 152 | } |
| 153 | |
| 154 | @Override |
| 155 | public Type type() { |
| 156 | return Type.OUTPUT; |
| 157 | } |
alshabib | 99b8fdc | 2014-09-25 14:30:22 -0700 | [diff] [blame] | 158 | @Override |
| 159 | public String toString() { |
| 160 | return toStringHelper(type().toString()) |
| 161 | .add("port", port).toString(); |
| 162 | } |
alshabib | 8ca5390 | 2014-10-07 13:11:17 -0700 | [diff] [blame^] | 163 | |
| 164 | @Override |
| 165 | public int hashCode() { |
| 166 | return Objects.hash(port, type()); |
| 167 | } |
| 168 | |
| 169 | @Override |
| 170 | public boolean equals(Object obj) { |
| 171 | if (this == obj) { |
| 172 | return true; |
| 173 | } |
| 174 | if (obj instanceof OutputInstruction) { |
| 175 | OutputInstruction that = (OutputInstruction) obj; |
| 176 | Objects.equals(port, that.port); |
| 177 | |
| 178 | } |
| 179 | return false; |
| 180 | } |
alshabib | 55a55d9 | 2014-09-16 11:59:31 -0700 | [diff] [blame] | 181 | } |
| 182 | |
| 183 | } |
alshabib | 8ca5390 | 2014-10-07 13:11:17 -0700 | [diff] [blame^] | 184 | |
| 185 | |