Jian Li | c7e20a5 | 2016-07-18 19:03:49 +0900 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016-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.lisp.msg.types; |
| 17 | |
Jian Li | 115d860 | 2016-08-15 20:21:53 +0900 | [diff] [blame] | 18 | import io.netty.buffer.ByteBuf; |
| 19 | import org.onosproject.lisp.msg.exceptions.LispParseError; |
| 20 | import org.onosproject.lisp.msg.exceptions.LispReaderException; |
GUNi | ba87170 | 2016-08-22 21:06:02 +0900 | [diff] [blame^] | 21 | import org.onosproject.lisp.msg.exceptions.LispWriterException; |
Jian Li | 115d860 | 2016-08-15 20:21:53 +0900 | [diff] [blame] | 22 | |
| 23 | import java.nio.ByteBuffer; |
Jian Li | c7e20a5 | 2016-07-18 19:03:49 +0900 | [diff] [blame] | 24 | import java.util.Objects; |
| 25 | |
| 26 | import static com.google.common.base.MoreObjects.toStringHelper; |
| 27 | |
| 28 | /** |
| 29 | * Application data type LCAF address class. |
Jian Li | 8fc2d2f | 2016-08-08 14:43:53 +0900 | [diff] [blame] | 30 | * <p> |
Jian Li | c7e20a5 | 2016-07-18 19:03:49 +0900 | [diff] [blame] | 31 | * Application data type is defined in draft-ietf-lisp-lcaf-13 |
| 32 | * https://tools.ietf.org/html/draft-ietf-lisp-lcaf-13#page-26 |
| 33 | * |
Jian Li | 8fc2d2f | 2016-08-08 14:43:53 +0900 | [diff] [blame] | 34 | * <pre> |
| 35 | * {@literal |
Jian Li | c7e20a5 | 2016-07-18 19:03:49 +0900 | [diff] [blame] | 36 | * 0 1 2 3 |
| 37 | * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 |
| 38 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 39 | * | AFI = 16387 | Rsvd1 | Flags | |
| 40 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 41 | * | Type = 4 | Rsvd2 | 12 + n | |
| 42 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 43 | * | IP TOS, IPv6 TC, or Flow Label | Protocol | |
| 44 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 45 | * | Local Port (lower-range) | Local Port (upper-range) | |
| 46 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 47 | * | Remote Port (lower-range) | Remote Port (upper-range) | |
| 48 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 49 | * | AFI = x | Address ... | |
| 50 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
Jian Li | 8fc2d2f | 2016-08-08 14:43:53 +0900 | [diff] [blame] | 51 | * }</pre> |
Jian Li | c7e20a5 | 2016-07-18 19:03:49 +0900 | [diff] [blame] | 52 | */ |
Jian Li | 115d860 | 2016-08-15 20:21:53 +0900 | [diff] [blame] | 53 | public final class LispAppDataLcafAddress extends LispLcafAddress { |
Jian Li | c7e20a5 | 2016-07-18 19:03:49 +0900 | [diff] [blame] | 54 | |
| 55 | private final byte protocol; |
| 56 | private final int ipTos; |
Jian Li | 115d860 | 2016-08-15 20:21:53 +0900 | [diff] [blame] | 57 | private final short localPortLow; |
| 58 | private final short localPortHigh; |
| 59 | private final short remotePortLow; |
| 60 | private final short remotePortHigh; |
Jian Li | c7e20a5 | 2016-07-18 19:03:49 +0900 | [diff] [blame] | 61 | private LispAfiAddress address; |
| 62 | |
| 63 | /** |
Jian Li | a7b394d | 2016-08-21 23:11:46 +0900 | [diff] [blame] | 64 | * Initializes application data type LCAF address. |
| 65 | * |
Jian Li | 115d860 | 2016-08-15 20:21:53 +0900 | [diff] [blame] | 66 | * @param protocol protocol number |
| 67 | * @param ipTos IP type of service |
| 68 | * @param localPortLow low-ranged local port number |
| 69 | * @param localPortHigh high-ranged local port number |
| 70 | * @param remotePortLow low-ranged remote port number |
| 71 | * @param remotePortHigh high-ranged remote port number |
| 72 | * @param address address |
Jian Li | c7e20a5 | 2016-07-18 19:03:49 +0900 | [diff] [blame] | 73 | */ |
Jian Li | 115d860 | 2016-08-15 20:21:53 +0900 | [diff] [blame] | 74 | private LispAppDataLcafAddress(byte protocol, int ipTos, short localPortLow, |
| 75 | short localPortHigh, short remotePortLow, |
| 76 | short remotePortHigh, LispAfiAddress address) { |
Jian Li | c7e20a5 | 2016-07-18 19:03:49 +0900 | [diff] [blame] | 77 | super(LispCanonicalAddressFormatEnum.APPLICATION_DATA); |
| 78 | this.protocol = protocol; |
| 79 | this.ipTos = ipTos; |
Jian Li | 115d860 | 2016-08-15 20:21:53 +0900 | [diff] [blame] | 80 | this.localPortLow = localPortLow; |
| 81 | this.localPortHigh = localPortHigh; |
| 82 | this.remotePortLow = remotePortLow; |
| 83 | this.remotePortHigh = remotePortHigh; |
Jian Li | c7e20a5 | 2016-07-18 19:03:49 +0900 | [diff] [blame] | 84 | this.address = address; |
| 85 | } |
| 86 | |
| 87 | /** |
Jian Li | a7b394d | 2016-08-21 23:11:46 +0900 | [diff] [blame] | 88 | * Initializes application data type LCAF address. |
| 89 | * |
| 90 | * @param reserved1 reserved1 |
| 91 | * @param reserved2 reserved2 |
| 92 | * @param flag flag |
| 93 | * @param length length |
| 94 | * @param protocol protocol number |
| 95 | * @param ipTos IP type of service |
| 96 | * @param localPortLow low-ranged local port number |
| 97 | * @param localPortHigh high-ranged local port number |
| 98 | * @param remotePortLow low-ranged remote port number |
| 99 | * @param remotePortHigh high-ranged remote port number |
| 100 | * @param address address |
| 101 | */ |
| 102 | private LispAppDataLcafAddress(byte reserved1, byte reserved2, byte flag, short length, |
| 103 | byte protocol, int ipTos, short localPortLow, |
| 104 | short localPortHigh, short remotePortLow, |
| 105 | short remotePortHigh, LispAfiAddress address) { |
| 106 | super(LispCanonicalAddressFormatEnum.APPLICATION_DATA, reserved1, reserved2, flag, length); |
| 107 | this.protocol = protocol; |
| 108 | this.ipTos = ipTos; |
| 109 | this.localPortLow = localPortLow; |
| 110 | this.localPortHigh = localPortHigh; |
| 111 | this.remotePortLow = remotePortLow; |
| 112 | this.remotePortHigh = remotePortHigh; |
| 113 | this.address = address; |
| 114 | } |
| 115 | |
| 116 | /** |
Jian Li | c7e20a5 | 2016-07-18 19:03:49 +0900 | [diff] [blame] | 117 | * Obtains protocol number. |
| 118 | * |
| 119 | * @return protocol number |
| 120 | */ |
| 121 | public byte getProtocol() { |
| 122 | return protocol; |
| 123 | } |
| 124 | |
| 125 | /** |
| 126 | * Obtains IP type of service. |
| 127 | * |
| 128 | * @return IP type of service |
| 129 | */ |
| 130 | public int getIpTos() { |
| 131 | return ipTos; |
| 132 | } |
| 133 | |
| 134 | /** |
Jian Li | 115d860 | 2016-08-15 20:21:53 +0900 | [diff] [blame] | 135 | * Obtains low-ranged local port number. |
Jian Li | c7e20a5 | 2016-07-18 19:03:49 +0900 | [diff] [blame] | 136 | * |
Jian Li | 115d860 | 2016-08-15 20:21:53 +0900 | [diff] [blame] | 137 | * @return low-ranged local port number |
Jian Li | c7e20a5 | 2016-07-18 19:03:49 +0900 | [diff] [blame] | 138 | */ |
Jian Li | 115d860 | 2016-08-15 20:21:53 +0900 | [diff] [blame] | 139 | public short getLocalPortLow() { |
| 140 | return localPortLow; |
Jian Li | c7e20a5 | 2016-07-18 19:03:49 +0900 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | /** |
Jian Li | 115d860 | 2016-08-15 20:21:53 +0900 | [diff] [blame] | 144 | * Obtains high-ranged local port number. |
Jian Li | c7e20a5 | 2016-07-18 19:03:49 +0900 | [diff] [blame] | 145 | * |
Jian Li | 115d860 | 2016-08-15 20:21:53 +0900 | [diff] [blame] | 146 | * @return high-ranged local port number |
Jian Li | c7e20a5 | 2016-07-18 19:03:49 +0900 | [diff] [blame] | 147 | */ |
Jian Li | 115d860 | 2016-08-15 20:21:53 +0900 | [diff] [blame] | 148 | public short getLocalPortHigh() { |
| 149 | return localPortHigh; |
| 150 | } |
| 151 | |
| 152 | /** |
| 153 | * Obtains low-ranged remote port number. |
| 154 | * |
| 155 | * @return low-ranged remote port number |
| 156 | */ |
| 157 | public short getRemotePortLow() { |
| 158 | return remotePortLow; |
| 159 | } |
| 160 | |
| 161 | /** |
| 162 | * Obtains high-ranged remote port number. |
| 163 | * |
| 164 | * @return high-ranged remote port number |
| 165 | */ |
| 166 | public short getRemotePortHigh() { |
| 167 | return remotePortHigh; |
Jian Li | c7e20a5 | 2016-07-18 19:03:49 +0900 | [diff] [blame] | 168 | } |
| 169 | |
| 170 | /** |
| 171 | * Obtains address. |
| 172 | * |
| 173 | * @return address |
| 174 | */ |
| 175 | public LispAfiAddress getAddress() { |
| 176 | return address; |
| 177 | } |
| 178 | |
| 179 | @Override |
| 180 | public int hashCode() { |
Jian Li | 115d860 | 2016-08-15 20:21:53 +0900 | [diff] [blame] | 181 | return Objects.hash(address, protocol, ipTos, localPortLow, |
| 182 | localPortHigh, remotePortLow, remotePortHigh); |
Jian Li | c7e20a5 | 2016-07-18 19:03:49 +0900 | [diff] [blame] | 183 | } |
| 184 | |
| 185 | @Override |
| 186 | public boolean equals(Object obj) { |
| 187 | if (this == obj) { |
| 188 | return true; |
| 189 | } |
| 190 | |
| 191 | if (obj instanceof LispAppDataLcafAddress) { |
| 192 | final LispAppDataLcafAddress other = (LispAppDataLcafAddress) obj; |
| 193 | return Objects.equals(this.address, other.address) && |
| 194 | Objects.equals(this.protocol, other.protocol) && |
| 195 | Objects.equals(this.ipTos, other.ipTos) && |
Jian Li | 115d860 | 2016-08-15 20:21:53 +0900 | [diff] [blame] | 196 | Objects.equals(this.localPortLow, other.localPortLow) && |
| 197 | Objects.equals(this.localPortHigh, other.localPortHigh) && |
| 198 | Objects.equals(this.remotePortLow, other.remotePortLow) && |
| 199 | Objects.equals(this.remotePortHigh, other.remotePortHigh); |
Jian Li | c7e20a5 | 2016-07-18 19:03:49 +0900 | [diff] [blame] | 200 | } |
| 201 | return false; |
| 202 | } |
| 203 | |
| 204 | @Override |
| 205 | public String toString() { |
| 206 | return toStringHelper(this) |
| 207 | .add("address", address) |
| 208 | .add("protocol", protocol) |
| 209 | .add("ip type of service", ipTos) |
Jian Li | 115d860 | 2016-08-15 20:21:53 +0900 | [diff] [blame] | 210 | .add("low-ranged local port number", localPortLow) |
| 211 | .add("high-ranged local port number", localPortHigh) |
| 212 | .add("low-ranged remote port number", remotePortLow) |
| 213 | .add("high-ranged remote port number", remotePortHigh) |
Jian Li | c7e20a5 | 2016-07-18 19:03:49 +0900 | [diff] [blame] | 214 | .toString(); |
| 215 | } |
Jian Li | 115d860 | 2016-08-15 20:21:53 +0900 | [diff] [blame] | 216 | |
Jian Li | a7b394d | 2016-08-21 23:11:46 +0900 | [diff] [blame] | 217 | public static final class AppDataAddressBuilder |
| 218 | extends LcafAddressBuilder<AppDataAddressBuilder> { |
Jian Li | 115d860 | 2016-08-15 20:21:53 +0900 | [diff] [blame] | 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 LispAfiAddress address; |
| 226 | |
| 227 | /** |
| 228 | * Sets protocol number. |
| 229 | * |
| 230 | * @param protocol protocol number |
| 231 | * @return AppDataAddressBuilder object |
| 232 | */ |
Jian Li | a7b394d | 2016-08-21 23:11:46 +0900 | [diff] [blame] | 233 | public AppDataAddressBuilder withProtocol(byte protocol) { |
Jian Li | 115d860 | 2016-08-15 20:21:53 +0900 | [diff] [blame] | 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 AppDataAddressBuilder object |
| 243 | */ |
Jian Li | a7b394d | 2016-08-21 23:11:46 +0900 | [diff] [blame] | 244 | public AppDataAddressBuilder withIpTos(int ipTos) { |
Jian Li | 115d860 | 2016-08-15 20:21:53 +0900 | [diff] [blame] | 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 AppDataAddressBuilder object |
| 254 | */ |
Jian Li | a7b394d | 2016-08-21 23:11:46 +0900 | [diff] [blame] | 255 | public AppDataAddressBuilder withLocalPortLow(short localPortLow) { |
Jian Li | 115d860 | 2016-08-15 20:21:53 +0900 | [diff] [blame] | 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 AppDataAddressBuilder object |
| 265 | */ |
Jian Li | a7b394d | 2016-08-21 23:11:46 +0900 | [diff] [blame] | 266 | public AppDataAddressBuilder withLocalPortHigh(short localPortHigh) { |
Jian Li | 115d860 | 2016-08-15 20:21:53 +0900 | [diff] [blame] | 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 AppDataAddressBuilder object |
| 276 | */ |
Jian Li | a7b394d | 2016-08-21 23:11:46 +0900 | [diff] [blame] | 277 | public AppDataAddressBuilder withRemotePortLow(short remotePortLow) { |
Jian Li | 115d860 | 2016-08-15 20:21:53 +0900 | [diff] [blame] | 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 AppDataAddressBuilder object |
| 287 | */ |
Jian Li | a7b394d | 2016-08-21 23:11:46 +0900 | [diff] [blame] | 288 | public AppDataAddressBuilder withRemotePortHigh(short remotePortHigh) { |
Jian Li | 115d860 | 2016-08-15 20:21:53 +0900 | [diff] [blame] | 289 | this.remotePortHigh = remotePortHigh; |
| 290 | return this; |
| 291 | } |
| 292 | |
| 293 | /** |
| 294 | * Sets AFI address. |
| 295 | * |
| 296 | * @param address AFI address |
| 297 | * @return AppDataAddressBuilder object |
| 298 | */ |
Jian Li | a7b394d | 2016-08-21 23:11:46 +0900 | [diff] [blame] | 299 | public AppDataAddressBuilder withAddress(LispAfiAddress address) { |
Jian Li | 115d860 | 2016-08-15 20:21:53 +0900 | [diff] [blame] | 300 | this.address = address; |
| 301 | return this; |
| 302 | } |
| 303 | |
| 304 | /** |
| 305 | * Builds LispAppDataLcafAddress instance. |
| 306 | * |
| 307 | * @return LispAddDataLcafAddress instance |
| 308 | */ |
Jian Li | a7b394d | 2016-08-21 23:11:46 +0900 | [diff] [blame] | 309 | public LispAppDataLcafAddress build() { |
| 310 | return new LispAppDataLcafAddress(reserved1, reserved2, flag, length, |
| 311 | protocol, ipTos, localPortLow, localPortHigh, remotePortLow, |
| 312 | remotePortHigh, address); |
Jian Li | 115d860 | 2016-08-15 20:21:53 +0900 | [diff] [blame] | 313 | } |
| 314 | } |
| 315 | |
| 316 | /** |
| 317 | * Application data LCAF address reader class. |
| 318 | */ |
| 319 | public static class AppDataLcafAddressReader |
| 320 | implements LispAddressReader<LispAppDataLcafAddress> { |
| 321 | |
| 322 | @Override |
| 323 | public LispAppDataLcafAddress readFrom(ByteBuf byteBuf) throws LispParseError, LispReaderException { |
| 324 | |
Jian Li | a7b394d | 2016-08-21 23:11:46 +0900 | [diff] [blame] | 325 | LispLcafAddress lcafAddress = LispLcafAddress.deserializeCommon(byteBuf); |
| 326 | |
Jian Li | 115d860 | 2016-08-15 20:21:53 +0900 | [diff] [blame] | 327 | byte[] ipTosByte = new byte[3]; |
| 328 | byteBuf.readBytes(ipTosByte); |
| 329 | |
| 330 | byte protocol = (byte) byteBuf.readUnsignedByte(); |
| 331 | int ipTos = getPartialInt(ipTosByte); |
| 332 | short localPortLow = (short) byteBuf.readUnsignedShort(); |
| 333 | short localPortHigh = (short) byteBuf.readUnsignedShort(); |
| 334 | short remotePortLow = (short) byteBuf.readUnsignedShort(); |
| 335 | short remotePortHigh = (short) byteBuf.readUnsignedShort(); |
| 336 | |
Jian Li | a7b394d | 2016-08-21 23:11:46 +0900 | [diff] [blame] | 337 | LispAfiAddress address = new LispAfiAddress.AfiAddressReader().readFrom(byteBuf); |
Jian Li | 115d860 | 2016-08-15 20:21:53 +0900 | [diff] [blame] | 338 | |
| 339 | return new AppDataAddressBuilder() |
Jian Li | a7b394d | 2016-08-21 23:11:46 +0900 | [diff] [blame] | 340 | .withReserved1(lcafAddress.getReserved1()) |
| 341 | .withReserved2(lcafAddress.getReserved2()) |
| 342 | .withFlag(lcafAddress.getFlag()) |
| 343 | .withLength(lcafAddress.getLength()) |
| 344 | .withProtocol(protocol) |
| 345 | .withIpTos(ipTos) |
| 346 | .withLocalPortLow(localPortLow) |
| 347 | .withLocalPortHigh(localPortHigh) |
| 348 | .withRemotePortLow(remotePortLow) |
| 349 | .withRemotePortHigh(remotePortHigh) |
| 350 | .withAddress(address) |
| 351 | .build(); |
Jian Li | 115d860 | 2016-08-15 20:21:53 +0900 | [diff] [blame] | 352 | } |
| 353 | |
| 354 | /** |
| 355 | * A utility function that obtains the partial int value from byte arrays. |
| 356 | * |
| 357 | * @param bytes an array of bytes |
| 358 | * @return converted integer |
| 359 | */ |
| 360 | public static int getPartialInt(byte[] bytes) { |
| 361 | ByteBuffer buffer = ByteBuffer.allocate(4); |
| 362 | buffer.position(4 - bytes.length); |
| 363 | buffer.put(bytes); |
| 364 | buffer.position(0); |
| 365 | return buffer.getInt(); |
| 366 | } |
| 367 | } |
GUNi | ba87170 | 2016-08-22 21:06:02 +0900 | [diff] [blame^] | 368 | |
| 369 | /** |
| 370 | * Application data LCAF address writer class. |
| 371 | */ |
| 372 | public static class AppDataLcafAddressWriter |
| 373 | implements LispAddressWriter<LispAppDataLcafAddress> { |
| 374 | |
| 375 | @Override |
| 376 | public void writeTo(ByteBuf byteBuf, LispAppDataLcafAddress address) |
| 377 | throws LispWriterException { |
| 378 | |
| 379 | LispLcafAddress.serializeCommon(byteBuf, address); |
| 380 | |
| 381 | // TODO: need to handle TOS |
| 382 | |
| 383 | byteBuf.writeByte(address.getProtocol()); |
| 384 | byteBuf.writeShort(address.getLocalPortLow()); |
| 385 | byteBuf.writeShort(address.getLocalPortHigh()); |
| 386 | byteBuf.writeShort(address.getRemotePortLow()); |
| 387 | byteBuf.writeShort(address.getRemotePortHigh()); |
| 388 | |
| 389 | AfiAddressWriter writer = new LispAfiAddress.AfiAddressWriter(); |
| 390 | writer.writeTo(byteBuf, address.getAddress()); |
| 391 | } |
| 392 | } |
Jian Li | c7e20a5 | 2016-07-18 19:03:49 +0900 | [diff] [blame] | 393 | } |