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