Jian Li | 3ff327a | 2017-03-14 17:02:19 +0900 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2017-present Open Networking Laboratory |
| 3 | * |
| 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 |
| 7 | * |
| 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. |
| 15 | */ |
| 16 | package org.onosproject.drivers.lisp.extensions; |
| 17 | |
Jian Li | 299bc1d | 2017-03-17 19:17:40 +0900 | [diff] [blame^] | 18 | import com.google.common.collect.Maps; |
| 19 | import org.onlab.util.KryoNamespace; |
| 20 | import org.onosproject.mapping.addresses.ASMappingAddress; |
| 21 | import org.onosproject.mapping.addresses.DNMappingAddress; |
| 22 | import org.onosproject.mapping.addresses.EthMappingAddress; |
Jian Li | 3ff327a | 2017-03-14 17:02:19 +0900 | [diff] [blame] | 23 | import org.onosproject.mapping.addresses.ExtensionMappingAddress; |
| 24 | import org.onosproject.mapping.addresses.ExtensionMappingAddressType; |
Jian Li | 299bc1d | 2017-03-17 19:17:40 +0900 | [diff] [blame^] | 25 | import org.onosproject.mapping.addresses.IPMappingAddress; |
| 26 | import org.onosproject.mapping.addresses.MappingAddress; |
Jian Li | 3ff327a | 2017-03-14 17:02:19 +0900 | [diff] [blame] | 27 | import org.onosproject.net.flow.AbstractExtension; |
Jian Li | 299bc1d | 2017-03-17 19:17:40 +0900 | [diff] [blame^] | 28 | import org.onosproject.store.serializers.KryoNamespaces; |
| 29 | |
| 30 | import java.util.Map; |
| 31 | import java.util.Objects; |
| 32 | |
| 33 | import static com.google.common.base.MoreObjects.toStringHelper; |
| 34 | import static org.onosproject.mapping.addresses.ExtensionMappingAddressType |
| 35 | .ExtensionMappingAddressTypes.APPLICATION_DATA_ADDRESS; |
Jian Li | 3ff327a | 2017-03-14 17:02:19 +0900 | [diff] [blame] | 36 | |
| 37 | /** |
| 38 | * Implementation of LISP application data address. |
Jian Li | 299bc1d | 2017-03-17 19:17:40 +0900 | [diff] [blame^] | 39 | * When a locator-set needs to be conveyed based on the type of application or |
| 40 | * the Per-Hop Behavior (PHB) of a packet, the Application Data Type can be used. |
Jian Li | 3ff327a | 2017-03-14 17:02:19 +0900 | [diff] [blame] | 41 | */ |
Jian Li | 299bc1d | 2017-03-17 19:17:40 +0900 | [diff] [blame^] | 42 | public final class LispAppDataAddress extends AbstractExtension |
Jian Li | 3ff327a | 2017-03-14 17:02:19 +0900 | [diff] [blame] | 43 | implements ExtensionMappingAddress { |
Jian Li | 299bc1d | 2017-03-17 19:17:40 +0900 | [diff] [blame^] | 44 | |
| 45 | private static final String PROTOCOL = "protocol"; |
| 46 | private static final String IP_TOS = "ipTos"; |
| 47 | private static final String LOCAL_PORT_LOW = "localPortLow"; |
| 48 | private static final String LOCAL_PORT_HIGH = "localPortHigh"; |
| 49 | private static final String REMOTE_PORT_LOW = "remotePortLow"; |
| 50 | private static final String REMOTE_PORT_HIGH = "remotePortHigh"; |
| 51 | private static final String ADDRESS = "address"; |
| 52 | |
| 53 | private byte protocol; |
| 54 | private int ipTos; |
| 55 | private short localPortLow; |
| 56 | private short localPortHigh; |
| 57 | private short remotePortLow; |
| 58 | private short remotePortHigh; |
| 59 | private MappingAddress address; |
| 60 | |
| 61 | private final KryoNamespace appKryo = new KryoNamespace.Builder() |
| 62 | .register(KryoNamespaces.API) |
| 63 | .register(MappingAddress.class) |
| 64 | .register(MappingAddress.Type.class) |
| 65 | .register(IPMappingAddress.class) |
| 66 | .register(ASMappingAddress.class) |
| 67 | .register(DNMappingAddress.class) |
| 68 | .register(EthMappingAddress.class) |
| 69 | .build(); |
| 70 | |
| 71 | /** |
| 72 | * Default constructor. |
| 73 | */ |
| 74 | public LispAppDataAddress() { |
| 75 | } |
| 76 | |
| 77 | /** |
| 78 | * Creates an instance with initialized parameters. |
| 79 | * |
| 80 | * @param protocol protocol number |
| 81 | * @param ipTos IP type of service |
| 82 | * @param localPortLow low-ranged local port number |
| 83 | * @param localPortHigh high-ranged local port number |
| 84 | * @param remotePortLow low-ranged remote port number |
| 85 | * @param remotePortHigh high-ranged remote port number |
| 86 | */ |
| 87 | private LispAppDataAddress(byte protocol, int ipTos, short localPortLow, |
| 88 | short localPortHigh, short remotePortLow, |
| 89 | short remotePortHigh, MappingAddress address) { |
| 90 | this.protocol = protocol; |
| 91 | this.ipTos = ipTos; |
| 92 | this.localPortLow = localPortLow; |
| 93 | this.localPortHigh = localPortHigh; |
| 94 | this.remotePortLow = remotePortLow; |
| 95 | this.remotePortHigh = remotePortHigh; |
| 96 | this.address = address; |
| 97 | } |
| 98 | |
| 99 | /** |
| 100 | * Obtains protocol type. |
| 101 | * |
| 102 | * @return protocol type |
| 103 | */ |
| 104 | public byte getProtocol() { |
| 105 | return protocol; |
| 106 | } |
| 107 | |
| 108 | /** |
| 109 | * Obtains IP type of service. |
| 110 | * |
| 111 | * @return IP type of service |
| 112 | */ |
| 113 | public int getIpTos() { |
| 114 | return ipTos; |
| 115 | } |
| 116 | |
| 117 | /** |
| 118 | * Obtains local port low. |
| 119 | * |
| 120 | * @return local port low |
| 121 | */ |
| 122 | public short getLocalPortLow() { |
| 123 | return localPortLow; |
| 124 | } |
| 125 | |
| 126 | /** |
| 127 | * Obtains local port high. |
| 128 | * |
| 129 | * @return local port high |
| 130 | */ |
| 131 | public short getLocalPortHigh() { |
| 132 | return localPortHigh; |
| 133 | } |
| 134 | |
| 135 | /** |
| 136 | * Obtains remote port low. |
| 137 | * |
| 138 | * @return remote port low |
| 139 | */ |
| 140 | public short getRemotePortLow() { |
| 141 | return remotePortLow; |
| 142 | } |
| 143 | |
| 144 | /** |
| 145 | * Obtains remote port high. |
| 146 | * |
| 147 | * @return remote port high |
| 148 | */ |
| 149 | public short getRemotePortHigh() { |
| 150 | return remotePortHigh; |
| 151 | } |
| 152 | |
| 153 | /** |
| 154 | * Obtains mapping address. |
| 155 | * |
| 156 | * @return mapping address |
| 157 | */ |
| 158 | public MappingAddress getAddress() { |
| 159 | return address; |
| 160 | } |
| 161 | |
Jian Li | 3ff327a | 2017-03-14 17:02:19 +0900 | [diff] [blame] | 162 | @Override |
| 163 | public ExtensionMappingAddressType type() { |
Jian Li | 299bc1d | 2017-03-17 19:17:40 +0900 | [diff] [blame^] | 164 | return APPLICATION_DATA_ADDRESS.type(); |
Jian Li | 3ff327a | 2017-03-14 17:02:19 +0900 | [diff] [blame] | 165 | } |
| 166 | |
| 167 | @Override |
| 168 | public byte[] serialize() { |
Jian Li | 299bc1d | 2017-03-17 19:17:40 +0900 | [diff] [blame^] | 169 | Map<String, Object> parameterMap = Maps.newHashMap(); |
| 170 | parameterMap.put(PROTOCOL, protocol); |
| 171 | parameterMap.put(IP_TOS, ipTos); |
| 172 | parameterMap.put(LOCAL_PORT_LOW, localPortLow); |
| 173 | parameterMap.put(LOCAL_PORT_HIGH, localPortHigh); |
| 174 | parameterMap.put(REMOTE_PORT_LOW, remotePortLow); |
| 175 | parameterMap.put(REMOTE_PORT_HIGH, remotePortHigh); |
| 176 | parameterMap.put(ADDRESS, address); |
| 177 | |
| 178 | return appKryo.serialize(parameterMap); |
Jian Li | 3ff327a | 2017-03-14 17:02:19 +0900 | [diff] [blame] | 179 | } |
| 180 | |
| 181 | @Override |
| 182 | public void deserialize(byte[] data) { |
Jian Li | 299bc1d | 2017-03-17 19:17:40 +0900 | [diff] [blame^] | 183 | Map<String, Object> parameterMap = appKryo.deserialize(data); |
Jian Li | 3ff327a | 2017-03-14 17:02:19 +0900 | [diff] [blame] | 184 | |
Jian Li | 299bc1d | 2017-03-17 19:17:40 +0900 | [diff] [blame^] | 185 | this.protocol = (byte) parameterMap.get(PROTOCOL); |
| 186 | this.ipTos = (int) parameterMap.get(IP_TOS); |
| 187 | this.localPortLow = (short) parameterMap.get(LOCAL_PORT_LOW); |
| 188 | this.localPortHigh = (short) parameterMap.get(LOCAL_PORT_HIGH); |
| 189 | this.remotePortLow = (short) parameterMap.get(REMOTE_PORT_LOW); |
| 190 | this.remotePortHigh = (short) parameterMap.get(REMOTE_PORT_HIGH); |
| 191 | this.address = (MappingAddress) parameterMap.get(ADDRESS); |
| 192 | } |
| 193 | |
| 194 | @Override |
| 195 | public int hashCode() { |
| 196 | return Objects.hash(protocol, ipTos, localPortLow, localPortHigh, |
| 197 | remotePortLow, remotePortHigh, address); |
| 198 | } |
| 199 | |
| 200 | @Override |
| 201 | public boolean equals(Object obj) { |
| 202 | if (this == obj) { |
| 203 | return true; |
| 204 | } |
| 205 | if (obj instanceof LispAppDataAddress) { |
| 206 | LispAppDataAddress that = (LispAppDataAddress) obj; |
| 207 | return Objects.equals(protocol, that.protocol) && |
| 208 | Objects.equals(ipTos, that.ipTos) && |
| 209 | Objects.equals(localPortLow, that.localPortLow) && |
| 210 | Objects.equals(localPortHigh, that.localPortHigh) && |
| 211 | Objects.equals(remotePortLow, that.remotePortLow) && |
| 212 | Objects.equals(remotePortHigh, that.remotePortHigh) && |
| 213 | Objects.equals(address, that.address); |
| 214 | } |
| 215 | return false; |
| 216 | } |
| 217 | |
| 218 | @Override |
| 219 | public String toString() { |
| 220 | return toStringHelper(type().toString()) |
| 221 | .add("protocol", protocol) |
| 222 | .add("IP type of service", ipTos) |
| 223 | .add("low-ranged local port number", localPortLow) |
| 224 | .add("high-ranged local port number", localPortHigh) |
| 225 | .add("low-ranged remote port number", remotePortLow) |
| 226 | .add("high-ranged remote port number", remotePortHigh) |
| 227 | .add("address", address) |
| 228 | .toString(); |
| 229 | } |
| 230 | |
| 231 | /** |
| 232 | * A builder for building LispAppDataAddress. |
| 233 | */ |
| 234 | public static final class Builder { |
| 235 | private byte protocol; |
| 236 | private int ipTos; |
| 237 | private short localPortLow; |
| 238 | private short localPortHigh; |
| 239 | private short remotePortLow; |
| 240 | private short remotePortHigh; |
| 241 | private MappingAddress address; |
| 242 | |
| 243 | /** |
| 244 | * Sets protocol number. |
| 245 | * |
| 246 | * @param protocol protocol number |
| 247 | * @return Builder object |
| 248 | */ |
| 249 | public Builder withProtocol(byte protocol) { |
| 250 | this.protocol = protocol; |
| 251 | return this; |
| 252 | } |
| 253 | |
| 254 | /** |
| 255 | * Sets IP type of service. |
| 256 | * |
| 257 | * @param ipTos IP type of service |
| 258 | * @return Builder object |
| 259 | */ |
| 260 | public Builder withIpTos(int ipTos) { |
| 261 | this.ipTos = ipTos; |
| 262 | return this; |
| 263 | } |
| 264 | |
| 265 | /** |
| 266 | * Sets low-ranged local port number. |
| 267 | * |
| 268 | * @param localPortLow low-ranged local port number |
| 269 | * @return Builder object |
| 270 | */ |
| 271 | public Builder withLocalPortLow(short localPortLow) { |
| 272 | this.localPortLow = localPortLow; |
| 273 | return this; |
| 274 | } |
| 275 | |
| 276 | /** |
| 277 | * Sets high-ranged local port number. |
| 278 | * |
| 279 | * @param localPortHigh high-ranged local port number |
| 280 | * @return Builder object |
| 281 | */ |
| 282 | public Builder withLocalPortHigh(short localPortHigh) { |
| 283 | this.localPortHigh = localPortHigh; |
| 284 | return this; |
| 285 | } |
| 286 | |
| 287 | /** |
| 288 | * Sets low-ranged remote port number. |
| 289 | * |
| 290 | * @param remotePortLow low-ranged remote port number |
| 291 | * @return Builder object |
| 292 | */ |
| 293 | public Builder withRemotePortLow(short remotePortLow) { |
| 294 | this.remotePortLow = remotePortLow; |
| 295 | return this; |
| 296 | } |
| 297 | |
| 298 | /** |
| 299 | * Sets high-ranged remote port number. |
| 300 | * |
| 301 | * @param remotePortHigh high-ranged remote port number |
| 302 | * @return Builder object |
| 303 | */ |
| 304 | public Builder withRemotePortHigh(short remotePortHigh) { |
| 305 | this.remotePortHigh = remotePortHigh; |
| 306 | return this; |
| 307 | } |
| 308 | |
| 309 | /** |
| 310 | * Sets mapping address. |
| 311 | * |
| 312 | * @param address mapping address |
| 313 | * @return Builder object |
| 314 | */ |
| 315 | public Builder withAddress(MappingAddress address) { |
| 316 | this.address = address; |
| 317 | return this; |
| 318 | } |
| 319 | |
| 320 | /** |
| 321 | * Builds LispAppDataLcafAddress instance. |
| 322 | * |
| 323 | * @return LispAddDataLcafAddress instance |
| 324 | */ |
| 325 | public LispAppDataAddress build() { |
| 326 | |
| 327 | return new LispAppDataAddress(protocol, ipTos, localPortLow, |
| 328 | localPortHigh, remotePortLow, remotePortHigh, address); |
| 329 | } |
Jian Li | 3ff327a | 2017-03-14 17:02:19 +0900 | [diff] [blame] | 330 | } |
| 331 | } |