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