Jian Li | 0959600 | 2016-07-15 17:46: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 | a7b394d | 2016-08-21 23:11:46 +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 | d4e6370 | 2016-08-30 18:29:20 +0900 | [diff] [blame] | 22 | import org.slf4j.Logger; |
| 23 | import org.slf4j.LoggerFactory; |
Jian Li | a7b394d | 2016-08-21 23:11:46 +0900 | [diff] [blame] | 24 | |
Jian Li | 0959600 | 2016-07-15 17:46:49 +0900 | [diff] [blame] | 25 | import java.util.Objects; |
| 26 | |
| 27 | import static com.google.common.base.MoreObjects.toStringHelper; |
Jian Li | a7b394d | 2016-08-21 23:11:46 +0900 | [diff] [blame] | 28 | import static org.onosproject.lisp.msg.types.LispCanonicalAddressFormatEnum.*; |
Jian Li | 0959600 | 2016-07-15 17:46:49 +0900 | [diff] [blame] | 29 | |
| 30 | /** |
| 31 | * LISP Canonical Address Formatted address class. |
Jian Li | c7e20a5 | 2016-07-18 19:03:49 +0900 | [diff] [blame] | 32 | * |
Jian Li | 8fc2d2f | 2016-08-08 14:43:53 +0900 | [diff] [blame] | 33 | * <pre> |
| 34 | * {@literal |
Jian Li | c7e20a5 | 2016-07-18 19:03:49 +0900 | [diff] [blame] | 35 | * 0 1 2 3 |
| 36 | * 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 |
| 37 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 38 | * | AFI = 16387 | Rsvd1 | Flags | |
| 39 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 40 | * | Type | Rsvd2 | Length | |
| 41 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
Jian Li | 8fc2d2f | 2016-08-08 14:43:53 +0900 | [diff] [blame] | 42 | * }</pre> |
Jian Li | 0959600 | 2016-07-15 17:46:49 +0900 | [diff] [blame] | 43 | */ |
| 44 | public class LispLcafAddress extends LispAfiAddress { |
| 45 | |
Jian Li | d4e6370 | 2016-08-30 18:29:20 +0900 | [diff] [blame] | 46 | private static final Logger log = LoggerFactory.getLogger(LispLcafAddress.class); |
| 47 | |
Jian Li | a7b394d | 2016-08-21 23:11:46 +0900 | [diff] [blame] | 48 | private final LispCanonicalAddressFormatEnum lcafType; |
| 49 | private final byte reserved1; |
| 50 | private final byte reserved2; |
| 51 | private final byte flag; |
| 52 | private final short length; |
Jian Li | 0959600 | 2016-07-15 17:46:49 +0900 | [diff] [blame] | 53 | |
| 54 | /** |
| 55 | * Initializes LCAF address. |
| 56 | * |
| 57 | * @param lcafType LCAF type |
Jian Li | c7e20a5 | 2016-07-18 19:03:49 +0900 | [diff] [blame] | 58 | * @param reserved1 reserved1 field |
| 59 | * @param reserved2 reserved2 field |
| 60 | * @param flag flag field |
| 61 | * @param length length field |
Jian Li | 0959600 | 2016-07-15 17:46:49 +0900 | [diff] [blame] | 62 | */ |
Jian Li | c7e20a5 | 2016-07-18 19:03:49 +0900 | [diff] [blame] | 63 | protected LispLcafAddress(LispCanonicalAddressFormatEnum lcafType, |
Jian Li | a7b394d | 2016-08-21 23:11:46 +0900 | [diff] [blame] | 64 | byte reserved1, byte reserved2, byte flag, short length) { |
Jian Li | 0959600 | 2016-07-15 17:46:49 +0900 | [diff] [blame] | 65 | super(AddressFamilyIdentifierEnum.LCAF); |
| 66 | this.lcafType = lcafType; |
Jian Li | c7e20a5 | 2016-07-18 19:03:49 +0900 | [diff] [blame] | 67 | this.reserved1 = reserved1; |
| 68 | this.reserved2 = reserved2; |
| 69 | this.flag = flag; |
| 70 | this.length = length; |
| 71 | } |
| 72 | |
| 73 | /** |
| 74 | * Initializes LCAF address. |
| 75 | * |
| 76 | * @param lcafType LCAF type |
| 77 | * @param reserved2 reserved2 field |
| 78 | * @param flag flag field |
| 79 | * @param length length field |
| 80 | */ |
| 81 | protected LispLcafAddress(LispCanonicalAddressFormatEnum lcafType, |
Jian Li | a7b394d | 2016-08-21 23:11:46 +0900 | [diff] [blame] | 82 | byte reserved2, byte flag, short length) { |
Jian Li | c7e20a5 | 2016-07-18 19:03:49 +0900 | [diff] [blame] | 83 | super(AddressFamilyIdentifierEnum.LCAF); |
| 84 | this.lcafType = lcafType; |
| 85 | this.reserved2 = reserved2; |
| 86 | this.flag = flag; |
| 87 | this.length = length; |
| 88 | this.reserved1 = 0; |
| 89 | } |
| 90 | |
| 91 | /** |
| 92 | * Initializes LCAF address. |
| 93 | * |
| 94 | * @param lcafType LCAF type |
| 95 | * @param reserved2 reserved2 field |
| 96 | * @param length length field |
| 97 | */ |
| 98 | protected LispLcafAddress(LispCanonicalAddressFormatEnum lcafType, |
Jian Li | a7b394d | 2016-08-21 23:11:46 +0900 | [diff] [blame] | 99 | byte reserved2, short length) { |
Jian Li | c7e20a5 | 2016-07-18 19:03:49 +0900 | [diff] [blame] | 100 | super(AddressFamilyIdentifierEnum.LCAF); |
| 101 | this.lcafType = lcafType; |
| 102 | this.reserved2 = reserved2; |
| 103 | this.length = length; |
| 104 | this.reserved1 = 0; |
| 105 | this.flag = 0; |
| 106 | } |
| 107 | |
| 108 | /** |
| 109 | * Initializes LCAF address. |
| 110 | * |
| 111 | * @param lcafType LCAF type |
| 112 | * @param reserved2 reserved2 field |
| 113 | */ |
| 114 | protected LispLcafAddress(LispCanonicalAddressFormatEnum lcafType, byte reserved2) { |
| 115 | super(AddressFamilyIdentifierEnum.LCAF); |
| 116 | this.lcafType = lcafType; |
| 117 | this.reserved2 = reserved2; |
| 118 | this.reserved1 = 0; |
| 119 | this.flag = 0; |
| 120 | this.length = 0; |
| 121 | } |
| 122 | |
| 123 | /** |
| 124 | * Initializes LCAF address. |
| 125 | * |
| 126 | * @param lcafType LCAF type |
Jian Li | a7b394d | 2016-08-21 23:11:46 +0900 | [diff] [blame] | 127 | * @param length length field |
| 128 | */ |
| 129 | protected LispLcafAddress(LispCanonicalAddressFormatEnum lcafType, short length) { |
| 130 | super(AddressFamilyIdentifierEnum.LCAF); |
| 131 | this.lcafType = lcafType; |
| 132 | this.reserved1 = 0; |
| 133 | this.reserved2 = 0; |
| 134 | this.flag = 0; |
| 135 | this.length = length; |
| 136 | } |
| 137 | |
| 138 | /** |
| 139 | * Initializes LCAF address. |
| 140 | * |
| 141 | * @param lcafType LCAF type |
Jian Li | c7e20a5 | 2016-07-18 19:03:49 +0900 | [diff] [blame] | 142 | */ |
| 143 | protected LispLcafAddress(LispCanonicalAddressFormatEnum lcafType) { |
| 144 | super(AddressFamilyIdentifierEnum.LCAF); |
| 145 | this.lcafType = lcafType; |
| 146 | this.reserved1 = 0; |
| 147 | this.reserved2 = 0; |
| 148 | this.flag = 0; |
| 149 | this.length = 0; |
Jian Li | 0959600 | 2016-07-15 17:46:49 +0900 | [diff] [blame] | 150 | } |
| 151 | |
| 152 | /** |
| 153 | * Obtains LCAF type. |
| 154 | * |
| 155 | * @return LCAF type |
| 156 | */ |
| 157 | public LispCanonicalAddressFormatEnum getType() { |
| 158 | return lcafType; |
| 159 | } |
| 160 | |
| 161 | /** |
Jian Li | c7e20a5 | 2016-07-18 19:03:49 +0900 | [diff] [blame] | 162 | * Obtains LCAF reserved1 value. |
Jian Li | 0959600 | 2016-07-15 17:46:49 +0900 | [diff] [blame] | 163 | * |
Jian Li | c7e20a5 | 2016-07-18 19:03:49 +0900 | [diff] [blame] | 164 | * @return LCAF reserved1 value |
Jian Li | 0959600 | 2016-07-15 17:46:49 +0900 | [diff] [blame] | 165 | */ |
Jian Li | c7e20a5 | 2016-07-18 19:03:49 +0900 | [diff] [blame] | 166 | public byte getReserved1() { |
| 167 | return reserved1; |
| 168 | } |
| 169 | |
| 170 | /** |
| 171 | * Obtains LCAF reserved2 value. |
| 172 | * |
| 173 | * @return LCAF reserved2 value |
| 174 | */ |
| 175 | public byte getReserved2() { |
| 176 | return reserved2; |
| 177 | } |
| 178 | |
| 179 | /** |
| 180 | * Obtains LCAF flag value. |
| 181 | * |
| 182 | * @return LCAF flag value |
| 183 | */ |
| 184 | public byte getFlag() { |
| 185 | return flag; |
| 186 | } |
| 187 | |
| 188 | /** |
| 189 | * Obtains LCAF length value. |
| 190 | * |
| 191 | * @return LCAF length value |
| 192 | */ |
Jian Li | a7b394d | 2016-08-21 23:11:46 +0900 | [diff] [blame] | 193 | public short getLength() { |
Jian Li | c7e20a5 | 2016-07-18 19:03:49 +0900 | [diff] [blame] | 194 | return length; |
Jian Li | 0959600 | 2016-07-15 17:46:49 +0900 | [diff] [blame] | 195 | } |
| 196 | |
Jian Li | a7b394d | 2016-08-21 23:11:46 +0900 | [diff] [blame] | 197 | /** |
| 198 | * Deserializes common fields from byte buffer. |
| 199 | * |
| 200 | * @param byteBuf byte buffer |
| 201 | * @return LispLcafAddress with filled common data fields |
| 202 | */ |
| 203 | public static LispLcafAddress deserializeCommon(ByteBuf byteBuf) { |
| 204 | |
| 205 | // reserved1 -> 8 bits |
| 206 | byte reserved1 = (byte) byteBuf.readUnsignedByte(); |
| 207 | |
| 208 | // flags -> 8 bits |
| 209 | byte flag = (byte) byteBuf.readUnsignedByte(); |
| 210 | |
| 211 | // LCAF type -> 8 bits |
| 212 | byte lcafType = (byte) byteBuf.readUnsignedByte(); |
| 213 | |
| 214 | // reserved2 -> 8bits |
| 215 | byte reserved2 = (byte) byteBuf.readUnsignedByte(); |
| 216 | |
| 217 | // length -> 16 bits |
| 218 | short length = (short) byteBuf.readUnsignedShort(); |
| 219 | |
| 220 | return new LispLcafAddress(LispCanonicalAddressFormatEnum.valueOf(lcafType), |
| 221 | reserved1, reserved2, flag, length); |
| 222 | } |
| 223 | |
GUNi | ba87170 | 2016-08-22 21:06:02 +0900 | [diff] [blame] | 224 | /** |
| 225 | * Serializes common fields to byte buffer. |
| 226 | * |
| 227 | * @param byteBuf byte buffer |
| 228 | * @param address LISP LCAF address instance |
| 229 | */ |
| 230 | public static void serializeCommon(ByteBuf byteBuf, LispLcafAddress address) { |
| 231 | byteBuf.writeByte(address.getReserved1()); |
| 232 | byteBuf.writeByte(address.getFlag()); |
| 233 | byteBuf.writeByte(address.getType().getLispCode()); |
| 234 | byteBuf.writeByte(address.getReserved2()); |
| 235 | byteBuf.writeShort(address.getLength()); |
| 236 | } |
| 237 | |
Jian Li | 0959600 | 2016-07-15 17:46:49 +0900 | [diff] [blame] | 238 | @Override |
| 239 | public int hashCode() { |
Jian Li | d56f97e | 2016-07-19 15:48:15 +0900 | [diff] [blame] | 240 | return Objects.hash(lcafType, reserved1, reserved2, flag, length); |
Jian Li | 0959600 | 2016-07-15 17:46:49 +0900 | [diff] [blame] | 241 | } |
| 242 | |
| 243 | @Override |
| 244 | public boolean equals(Object obj) { |
| 245 | if (this == obj) { |
| 246 | return true; |
| 247 | } |
| 248 | |
| 249 | if (obj instanceof LispLcafAddress) { |
| 250 | final LispLcafAddress other = (LispLcafAddress) obj; |
| 251 | return Objects.equals(this.lcafType, other.lcafType) && |
Jian Li | c7e20a5 | 2016-07-18 19:03:49 +0900 | [diff] [blame] | 252 | Objects.equals(this.reserved1, other.reserved1) && |
| 253 | Objects.equals(this.reserved2, other.reserved2) && |
| 254 | Objects.equals(this.flag, other.flag) && |
| 255 | Objects.equals(this.length, other.length); |
Jian Li | 0959600 | 2016-07-15 17:46:49 +0900 | [diff] [blame] | 256 | } |
| 257 | return false; |
| 258 | } |
| 259 | |
| 260 | @Override |
| 261 | public String toString() { |
| 262 | return toStringHelper(this) |
| 263 | .add("lcafType", lcafType) |
Jian Li | c7e20a5 | 2016-07-18 19:03:49 +0900 | [diff] [blame] | 264 | .add("reserved1", reserved1) |
| 265 | .add("reserved2", reserved2) |
| 266 | .add("flag", flag) |
| 267 | .add("length", length) |
Jian Li | 0959600 | 2016-07-15 17:46:49 +0900 | [diff] [blame] | 268 | .toString(); |
| 269 | } |
Jian Li | a7b394d | 2016-08-21 23:11:46 +0900 | [diff] [blame] | 270 | |
| 271 | protected static class LcafAddressBuilder<T> { |
| 272 | |
| 273 | protected byte reserved1; |
| 274 | protected byte flag; |
| 275 | protected byte lcafType; |
| 276 | protected byte reserved2; |
| 277 | protected short length; |
| 278 | |
| 279 | /** |
| 280 | * Sets reserved1 value. |
| 281 | * |
| 282 | * @param reserved1 reserved1 value |
| 283 | * @return LcafAddressBuilder object |
| 284 | */ |
| 285 | public T withReserved1(byte reserved1) { |
| 286 | this.reserved1 = reserved1; |
| 287 | return (T) this; |
| 288 | } |
| 289 | |
| 290 | /** |
| 291 | * Sets flag. |
| 292 | * |
| 293 | * @param flag flag boolean |
| 294 | * @return LcafAddressBuilder object |
| 295 | */ |
| 296 | public T withFlag(byte flag) { |
| 297 | this.flag = flag; |
| 298 | return (T) this; |
| 299 | } |
| 300 | |
| 301 | /** |
| 302 | * Sets LCAF type. |
| 303 | * |
| 304 | * @param lcafType LCAF type |
| 305 | * @return LcafAddressBuilder object |
| 306 | */ |
| 307 | public T withLcafType(byte lcafType) { |
| 308 | this.lcafType = lcafType; |
| 309 | return (T) this; |
| 310 | } |
| 311 | |
| 312 | /** |
| 313 | * Sets reserved2 value. |
| 314 | * |
| 315 | * @param reserved2 reserved2 value |
| 316 | * @return LcafAddressBuilder object |
| 317 | */ |
| 318 | public T withReserved2(byte reserved2) { |
| 319 | this.reserved2 = reserved2; |
| 320 | return (T) this; |
| 321 | } |
| 322 | |
| 323 | /** |
| 324 | * Sets length value. |
| 325 | * |
| 326 | * @param length length value |
| 327 | * @return LcafAddressBuilder object |
| 328 | */ |
| 329 | public T withLength(short length) { |
| 330 | this.length = length; |
| 331 | return (T) this; |
| 332 | } |
| 333 | |
| 334 | /** |
| 335 | * Builds LispLcafAddress object. |
| 336 | * |
| 337 | * @return LispLcafAddress instance |
| 338 | */ |
| 339 | public LispLcafAddress build() { |
| 340 | return new LispLcafAddress(LispCanonicalAddressFormatEnum.valueOf(lcafType), |
| 341 | reserved1, reserved2, flag, length); |
| 342 | } |
| 343 | } |
| 344 | |
GUNi | ba87170 | 2016-08-22 21:06:02 +0900 | [diff] [blame] | 345 | /** |
| 346 | * LISP LCAF reader class. |
| 347 | */ |
Jian Li | a7b394d | 2016-08-21 23:11:46 +0900 | [diff] [blame] | 348 | public static class LcafAddressReader implements LispAddressReader<LispLcafAddress> { |
| 349 | |
| 350 | private static final int LCAF_TYPE_FIELD_INDEX = 4; |
| 351 | |
| 352 | @Override |
| 353 | public LispLcafAddress readFrom(ByteBuf byteBuf) throws LispParseError, LispReaderException { |
| 354 | |
| 355 | int index = byteBuf.readerIndex(); |
| 356 | |
| 357 | // LCAF type -> 8 bits |
| 358 | byte lcafType = (byte) byteBuf.getUnsignedByte(index + LCAF_TYPE_FIELD_INDEX); |
| 359 | |
| 360 | if (lcafType == APPLICATION_DATA.getLispCode()) { |
| 361 | return new LispAppDataLcafAddress.AppDataLcafAddressReader().readFrom(byteBuf); |
| 362 | } |
| 363 | |
| 364 | if (lcafType == LIST.getLispCode()) { |
| 365 | return new LispListLcafAddress.ListLcafAddressReader().readFrom(byteBuf); |
| 366 | } |
| 367 | |
| 368 | if (lcafType == SEGMENT.getLispCode()) { |
| 369 | return new LispSegmentLcafAddress.SegmentLcafAddressReader().readFrom(byteBuf); |
| 370 | } |
| 371 | |
| 372 | if (lcafType == SOURCE_DEST.getLispCode()) { |
| 373 | return new LispSourceDestLcafAddress.SourceDestLcafAddressReader().readFrom(byteBuf); |
| 374 | } |
| 375 | |
Jian Li | d4e6370 | 2016-08-30 18:29:20 +0900 | [diff] [blame] | 376 | log.warn("Unsupported LCAF type, please specify a correct LCAF type"); |
| 377 | |
Jian Li | a7b394d | 2016-08-21 23:11:46 +0900 | [diff] [blame] | 378 | return null; |
| 379 | } |
| 380 | } |
GUNi | ba87170 | 2016-08-22 21:06:02 +0900 | [diff] [blame] | 381 | |
| 382 | /** |
| 383 | * LISP LCAF address writer class. |
| 384 | */ |
| 385 | public static class LcafAddressWriter implements LispAddressWriter<LispLcafAddress> { |
| 386 | |
| 387 | @Override |
| 388 | public void writeTo(ByteBuf byteBuf, LispLcafAddress address) throws LispWriterException { |
| 389 | switch (address.getType()) { |
| 390 | case APPLICATION_DATA: |
| 391 | new LispAppDataLcafAddress.AppDataLcafAddressWriter().writeTo(byteBuf, |
| 392 | (LispAppDataLcafAddress) address); |
| 393 | break; |
| 394 | case LIST: |
| 395 | new LispListLcafAddress.ListLcafAddressWriter().writeTo(byteBuf, |
| 396 | (LispListLcafAddress) address); |
| 397 | break; |
| 398 | case SEGMENT: |
| 399 | new LispSegmentLcafAddress.SegmentLcafAddressWriter().writeTo(byteBuf, |
| 400 | (LispSegmentLcafAddress) address); |
| 401 | break; |
| 402 | case SOURCE_DEST: |
| 403 | new LispSourceDestLcafAddress.SourceDestLcafAddressWriter().writeTo(byteBuf, |
| 404 | (LispSourceDestLcafAddress) address); |
| 405 | break; |
Jian Li | d4e6370 | 2016-08-30 18:29:20 +0900 | [diff] [blame] | 406 | default: |
| 407 | log.warn("Unsupported LCAF type, please specify a correct LCAF type"); |
| 408 | break; |
GUNi | ba87170 | 2016-08-22 21:06:02 +0900 | [diff] [blame] | 409 | } |
| 410 | } |
| 411 | } |
Jian Li | 0959600 | 2016-07-15 17:46:49 +0900 | [diff] [blame] | 412 | } |