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