Thomas Vachuska | 83e090e | 2014-10-22 14:25:35 -0700 | [diff] [blame] | 1 | /* |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame^] | 2 | * Copyright 2014 Open Networking Laboratory |
Thomas Vachuska | 83e090e | 2014-10-22 14:25:35 -0700 | [diff] [blame] | 3 | * |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame^] | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
Thomas Vachuska | 83e090e | 2014-10-22 14:25:35 -0700 | [diff] [blame] | 7 | * |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame^] | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
Thomas Vachuska | 83e090e | 2014-10-22 14:25:35 -0700 | [diff] [blame] | 15 | */ |
alshabib | 55a55d9 | 2014-09-16 11:59:31 -0700 | [diff] [blame] | 16 | package org.onlab.onos.net.flow.instructions; |
| 17 | |
alshabib | 99b8fdc | 2014-09-25 14:30:22 -0700 | [diff] [blame] | 18 | import static com.google.common.base.MoreObjects.toStringHelper; |
alshabib | 55a55d9 | 2014-09-16 11:59:31 -0700 | [diff] [blame] | 19 | import static com.google.common.base.Preconditions.checkNotNull; |
| 20 | |
alshabib | 8ca5390 | 2014-10-07 13:11:17 -0700 | [diff] [blame] | 21 | import java.util.Objects; |
| 22 | |
alshabib | 55a55d9 | 2014-09-16 11:59:31 -0700 | [diff] [blame] | 23 | import org.onlab.onos.net.PortNumber; |
Marc De Leenheer | 4908775 | 2014-10-23 13:54:09 -0700 | [diff] [blame] | 24 | import org.onlab.onos.net.flow.instructions.L0ModificationInstruction.L0SubType; |
| 25 | import org.onlab.onos.net.flow.instructions.L0ModificationInstruction.ModLambdaInstruction; |
alshabib | 7410fea | 2014-09-16 13:48:39 -0700 | [diff] [blame] | 26 | import org.onlab.onos.net.flow.instructions.L2ModificationInstruction.L2SubType; |
alshabib | 55a55d9 | 2014-09-16 11:59:31 -0700 | [diff] [blame] | 27 | import org.onlab.onos.net.flow.instructions.L2ModificationInstruction.ModEtherInstruction; |
alshabib | 7410fea | 2014-09-16 13:48:39 -0700 | [diff] [blame] | 28 | import org.onlab.onos.net.flow.instructions.L3ModificationInstruction.L3SubType; |
| 29 | import org.onlab.onos.net.flow.instructions.L3ModificationInstruction.ModIPInstruction; |
Ayaka Koshibe | 1d56fe4 | 2014-09-19 16:51:58 -0700 | [diff] [blame] | 30 | import org.onlab.packet.IpPrefix; |
Ayaka Koshibe | a9c199f | 2014-09-16 16:21:40 -0700 | [diff] [blame] | 31 | import org.onlab.packet.MacAddress; |
| 32 | import org.onlab.packet.VlanId; |
alshabib | 64231f6 | 2014-09-16 17:58:36 -0700 | [diff] [blame] | 33 | |
alshabib | 55a55d9 | 2014-09-16 11:59:31 -0700 | [diff] [blame] | 34 | /** |
| 35 | * Factory class for creating various traffic treatment instructions. |
| 36 | */ |
| 37 | public final class Instructions { |
| 38 | |
| 39 | |
| 40 | // Ban construction |
| 41 | private Instructions() {} |
| 42 | |
| 43 | /** |
| 44 | * Creates an output instruction using the specified port number. This can |
| 45 | * include logical ports such as CONTROLLER, FLOOD, etc. |
| 46 | * |
| 47 | * @param number port number |
| 48 | * @return output instruction |
| 49 | */ |
| 50 | public static OutputInstruction createOutput(final PortNumber number) { |
| 51 | checkNotNull(number, "PortNumber cannot be null"); |
| 52 | return new OutputInstruction(number); |
| 53 | } |
| 54 | |
| 55 | /** |
| 56 | * Creates a drop instruction. |
| 57 | * @return drop instruction |
| 58 | */ |
| 59 | public static DropInstruction createDrop() { |
| 60 | return new DropInstruction(); |
| 61 | } |
| 62 | |
| 63 | /** |
Marc De Leenheer | 4908775 | 2014-10-23 13:54:09 -0700 | [diff] [blame] | 64 | * Creates a l0 modification. |
| 65 | * @param lambda the lambda to modify to. |
| 66 | * @return a l0 modification |
| 67 | */ |
| 68 | public static L0ModificationInstruction modL0Lambda(short lambda) { |
| 69 | checkNotNull(lambda, "L0 lambda cannot be null"); |
| 70 | return new ModLambdaInstruction(L0SubType.LAMBDA, lambda); |
| 71 | } |
| 72 | |
| 73 | /** |
alshabib | 55a55d9 | 2014-09-16 11:59:31 -0700 | [diff] [blame] | 74 | * Creates a l2 src modification. |
| 75 | * @param addr the mac address 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 modL2Src(MacAddress addr) { |
alshabib | 55a55d9 | 2014-09-16 11:59:31 -0700 | [diff] [blame] | 79 | checkNotNull(addr, "Src l2 address cannot be null"); |
alshabib | 99b8fdc | 2014-09-25 14:30:22 -0700 | [diff] [blame] | 80 | return new ModEtherInstruction(L2SubType.ETH_SRC, addr); |
alshabib | 55a55d9 | 2014-09-16 11:59:31 -0700 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | /** |
| 84 | * Creates a L2 dst modification. |
| 85 | * @param addr the mac address to modify to. |
| 86 | * @return a L2 modification |
| 87 | */ |
Ayaka Koshibe | a9c199f | 2014-09-16 16:21:40 -0700 | [diff] [blame] | 88 | public static L2ModificationInstruction modL2Dst(MacAddress addr) { |
alshabib | 55a55d9 | 2014-09-16 11:59:31 -0700 | [diff] [blame] | 89 | checkNotNull(addr, "Dst l2 address cannot be null"); |
alshabib | 99b8fdc | 2014-09-25 14:30:22 -0700 | [diff] [blame] | 90 | return new L2ModificationInstruction.ModEtherInstruction(L2SubType.ETH_DST, addr); |
alshabib | 55a55d9 | 2014-09-16 11:59:31 -0700 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | /** |
alshabib | 7410fea | 2014-09-16 13:48:39 -0700 | [diff] [blame] | 94 | * Creates a Vlan id modification. |
| 95 | * @param vlanId the vlan id to modify to. |
| 96 | * @return a L2 modification |
| 97 | */ |
Ayaka Koshibe | a9c199f | 2014-09-16 16:21:40 -0700 | [diff] [blame] | 98 | public static L2ModificationInstruction modVlanId(VlanId vlanId) { |
alshabib | 55a55d9 | 2014-09-16 11:59:31 -0700 | [diff] [blame] | 99 | checkNotNull(vlanId, "VLAN id cannot be null"); |
| 100 | return new L2ModificationInstruction.ModVlanIdInstruction(vlanId); |
| 101 | } |
| 102 | |
alshabib | 7410fea | 2014-09-16 13:48:39 -0700 | [diff] [blame] | 103 | /** |
| 104 | * Creates a Vlan pcp modification. |
| 105 | * @param vlanPcp the pcp to modify to. |
| 106 | * @return a L2 modification |
| 107 | */ |
| 108 | public static L2ModificationInstruction modVlanPcp(Byte vlanPcp) { |
| 109 | checkNotNull(vlanPcp, "VLAN Pcp cannot be null"); |
| 110 | return new L2ModificationInstruction.ModVlanPcpInstruction(vlanPcp); |
| 111 | } |
| 112 | |
| 113 | /** |
| 114 | * Creates a L3 src modification. |
| 115 | * @param addr the ip address to modify to. |
| 116 | * @return a L3 modification |
| 117 | */ |
Ayaka Koshibe | 1d56fe4 | 2014-09-19 16:51:58 -0700 | [diff] [blame] | 118 | public static L3ModificationInstruction modL3Src(IpPrefix addr) { |
alshabib | 7410fea | 2014-09-16 13:48:39 -0700 | [diff] [blame] | 119 | checkNotNull(addr, "Src l3 address cannot be null"); |
alshabib | 99b8fdc | 2014-09-25 14:30:22 -0700 | [diff] [blame] | 120 | return new ModIPInstruction(L3SubType.IP_SRC, addr); |
alshabib | 7410fea | 2014-09-16 13:48:39 -0700 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | /** |
| 124 | * Creates a L3 dst modification. |
| 125 | * @param addr the ip address to modify to. |
| 126 | * @return a L3 modification |
| 127 | */ |
Ayaka Koshibe | 1d56fe4 | 2014-09-19 16:51:58 -0700 | [diff] [blame] | 128 | public static L3ModificationInstruction modL3Dst(IpPrefix addr) { |
alshabib | 7410fea | 2014-09-16 13:48:39 -0700 | [diff] [blame] | 129 | checkNotNull(addr, "Dst l3 address cannot be null"); |
alshabib | 99b8fdc | 2014-09-25 14:30:22 -0700 | [diff] [blame] | 130 | return new ModIPInstruction(L3SubType.IP_DST, addr); |
alshabib | 7410fea | 2014-09-16 13:48:39 -0700 | [diff] [blame] | 131 | } |
| 132 | |
alshabib | 7410fea | 2014-09-16 13:48:39 -0700 | [diff] [blame] | 133 | |
alshabib | 55a55d9 | 2014-09-16 11:59:31 -0700 | [diff] [blame] | 134 | /* |
| 135 | * Output instructions |
| 136 | */ |
| 137 | |
| 138 | public static final class DropInstruction implements Instruction { |
| 139 | @Override |
| 140 | public Type type() { |
| 141 | return Type.DROP; |
| 142 | } |
alshabib | 99b8fdc | 2014-09-25 14:30:22 -0700 | [diff] [blame] | 143 | |
| 144 | @Override |
| 145 | public String toString() { |
| 146 | return toStringHelper(type()).toString(); |
| 147 | |
| 148 | } |
alshabib | 8ca5390 | 2014-10-07 13:11:17 -0700 | [diff] [blame] | 149 | |
| 150 | @Override |
| 151 | public int hashCode() { |
| 152 | return Objects.hash(type()); |
| 153 | } |
| 154 | |
| 155 | @Override |
| 156 | public boolean equals(Object obj) { |
| 157 | if (this == obj) { |
| 158 | return true; |
| 159 | } |
| 160 | if (obj instanceof DropInstruction) { |
| 161 | DropInstruction that = (DropInstruction) obj; |
| 162 | return Objects.equals(type(), that.type()); |
| 163 | |
| 164 | } |
| 165 | return false; |
| 166 | } |
alshabib | 55a55d9 | 2014-09-16 11:59:31 -0700 | [diff] [blame] | 167 | } |
| 168 | |
| 169 | |
| 170 | public static final class OutputInstruction implements Instruction { |
| 171 | private final PortNumber port; |
| 172 | |
| 173 | private OutputInstruction(PortNumber port) { |
| 174 | this.port = port; |
| 175 | } |
| 176 | |
| 177 | public PortNumber port() { |
| 178 | return port; |
| 179 | } |
| 180 | |
| 181 | @Override |
| 182 | public Type type() { |
| 183 | return Type.OUTPUT; |
| 184 | } |
alshabib | 99b8fdc | 2014-09-25 14:30:22 -0700 | [diff] [blame] | 185 | @Override |
| 186 | public String toString() { |
| 187 | return toStringHelper(type().toString()) |
| 188 | .add("port", port).toString(); |
| 189 | } |
alshabib | 8ca5390 | 2014-10-07 13:11:17 -0700 | [diff] [blame] | 190 | |
| 191 | @Override |
| 192 | public int hashCode() { |
| 193 | return Objects.hash(port, type()); |
| 194 | } |
| 195 | |
| 196 | @Override |
| 197 | public boolean equals(Object obj) { |
| 198 | if (this == obj) { |
| 199 | return true; |
| 200 | } |
| 201 | if (obj instanceof OutputInstruction) { |
| 202 | OutputInstruction that = (OutputInstruction) obj; |
| 203 | Objects.equals(port, that.port); |
| 204 | |
| 205 | } |
| 206 | return false; |
| 207 | } |
alshabib | 55a55d9 | 2014-09-16 11:59:31 -0700 | [diff] [blame] | 208 | } |
| 209 | |
| 210 | } |
alshabib | 8ca5390 | 2014-10-07 13:11:17 -0700 | [diff] [blame] | 211 | |
| 212 | |