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 | |
Jian Li | c7e20a5 | 2016-07-18 19:03:49 +0900 | [diff] [blame] | 23 | import java.util.Objects; |
| 24 | |
| 25 | import static com.google.common.base.MoreObjects.toStringHelper; |
Jian Li | d4e6370 | 2016-08-30 18:29:20 +0900 | [diff] [blame] | 26 | import static com.google.common.base.Preconditions.checkNotNull; |
Jian Li | c7e20a5 | 2016-07-18 19:03:49 +0900 | [diff] [blame] | 27 | |
| 28 | /** |
| 29 | * Source/Dest key type LCAF address class. |
Jian Li | 8fc2d2f | 2016-08-08 14:43:53 +0900 | [diff] [blame] | 30 | * <p> |
Jian Li | 89c9ca9 | 2016-11-11 04:09:02 +0900 | [diff] [blame] | 31 | * Source destination key type is defined in draft-ietf-lisp-lcaf-20 |
| 32 | * https://tools.ietf.org/html/draft-ietf-lisp-lcaf-20#page-19 |
Jian Li | c7e20a5 | 2016-07-18 19:03:49 +0900 | [diff] [blame] | 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 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
Jian Li | 89c9ca9 | 2016-11-11 04:09:02 +0900 | [diff] [blame] | 41 | * | Type = 12 | Rsvd2 | Length | |
Jian Li | c7e20a5 | 2016-07-18 19:03:49 +0900 | [diff] [blame] | 42 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 43 | * | Reserved | Source-ML | Dest-ML | |
| 44 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 45 | * | AFI = x | Source-Prefix ... | |
| 46 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 47 | * | AFI = x | Destination-Prefix ... | |
| 48 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
Jian Li | 8fc2d2f | 2016-08-08 14:43:53 +0900 | [diff] [blame] | 49 | * }</pre> |
Jian Li | c7e20a5 | 2016-07-18 19:03:49 +0900 | [diff] [blame] | 50 | */ |
Jian Li | 115d860 | 2016-08-15 20:21:53 +0900 | [diff] [blame] | 51 | public final class LispSourceDestLcafAddress extends LispLcafAddress { |
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 | private final LispAfiAddress srcPrefix; |
| 54 | private final LispAfiAddress dstPrefix; |
Jian Li | c7e20a5 | 2016-07-18 19:03:49 +0900 | [diff] [blame] | 55 | private final byte srcMaskLength; |
| 56 | private final byte dstMaskLength; |
| 57 | private final short reserved; |
| 58 | |
| 59 | /** |
| 60 | * Initializes source/dest key type LCAF address. |
Jian Li | c7e20a5 | 2016-07-18 19:03:49 +0900 | [diff] [blame] | 61 | * |
Jian Li | 115d860 | 2016-08-15 20:21:53 +0900 | [diff] [blame] | 62 | * @param reserved reserved |
Jian Li | c7e20a5 | 2016-07-18 19:03:49 +0900 | [diff] [blame] | 63 | * @param srcMaskLength source mask length |
| 64 | * @param dstMaskLength destination mask length |
Jian Li | 115d860 | 2016-08-15 20:21:53 +0900 | [diff] [blame] | 65 | * @param srcPrefix source address prefix |
| 66 | * @param dstPrefix destination address prefix |
Jian Li | c7e20a5 | 2016-07-18 19:03:49 +0900 | [diff] [blame] | 67 | */ |
Jian Li | 115d860 | 2016-08-15 20:21:53 +0900 | [diff] [blame] | 68 | private LispSourceDestLcafAddress(short reserved, byte srcMaskLength, |
| 69 | byte dstMaskLength, |
| 70 | LispAfiAddress srcPrefix, |
| 71 | LispAfiAddress dstPrefix) { |
Jian Li | c7e20a5 | 2016-07-18 19:03:49 +0900 | [diff] [blame] | 72 | super(LispCanonicalAddressFormatEnum.SOURCE_DEST); |
| 73 | this.reserved = reserved; |
| 74 | this.srcMaskLength = srcMaskLength; |
| 75 | this.dstMaskLength = dstMaskLength; |
| 76 | this.srcPrefix = srcPrefix; |
| 77 | this.dstPrefix = dstPrefix; |
| 78 | } |
| 79 | |
| 80 | /** |
Jian Li | a7b394d | 2016-08-21 23:11:46 +0900 | [diff] [blame] | 81 | * Initializes source/dest key type LCAF address. |
| 82 | * |
| 83 | * @param reserved1 reserved1 |
| 84 | * @param reserved2 reserved2 |
| 85 | * @param flag flag |
| 86 | * @param length length |
| 87 | * @param reserved reserved |
| 88 | * @param srcMaskLength source mask length |
| 89 | * @param dstMaskLength destination mask length |
| 90 | * @param srcPrefix source address prefix |
| 91 | * @param dstPrefix destination address prefix |
| 92 | */ |
| 93 | private LispSourceDestLcafAddress(byte reserved1, byte reserved2, byte flag, short length, |
| 94 | short reserved, byte srcMaskLength, |
| 95 | byte dstMaskLength, LispAfiAddress srcPrefix, |
| 96 | LispAfiAddress dstPrefix) { |
| 97 | super(LispCanonicalAddressFormatEnum.SOURCE_DEST, reserved1, reserved2, flag, length); |
| 98 | this.reserved = reserved; |
| 99 | this.srcMaskLength = srcMaskLength; |
| 100 | this.dstMaskLength = dstMaskLength; |
| 101 | this.srcPrefix = srcPrefix; |
| 102 | this.dstPrefix = dstPrefix; |
| 103 | } |
| 104 | |
| 105 | /** |
Jian Li | c7e20a5 | 2016-07-18 19:03:49 +0900 | [diff] [blame] | 106 | * Obtains source address prefix. |
| 107 | * |
| 108 | * @return source address prefix |
| 109 | */ |
| 110 | public LispAfiAddress getSrcPrefix() { |
| 111 | return srcPrefix; |
| 112 | } |
| 113 | |
| 114 | /** |
| 115 | * Obtains destination address prefix. |
| 116 | * |
| 117 | * @return destination address prefix |
| 118 | */ |
| 119 | public LispAfiAddress getDstPrefix() { |
| 120 | return dstPrefix; |
| 121 | } |
| 122 | |
| 123 | /** |
| 124 | * Obtains source mask length. |
| 125 | * |
| 126 | * @return source mask length |
| 127 | */ |
| 128 | public byte getSrcMaskLength() { |
| 129 | return srcMaskLength; |
| 130 | } |
| 131 | |
| 132 | /** |
| 133 | * Obtains destination mask length. |
| 134 | * |
| 135 | * @return destination mask length |
| 136 | */ |
| 137 | public byte getDstMaskLength() { |
| 138 | return dstMaskLength; |
| 139 | } |
| 140 | |
| 141 | /** |
| 142 | * Obtains reserved value. |
| 143 | * |
| 144 | * @return reserved value |
| 145 | */ |
| 146 | public short getReserved() { |
| 147 | return reserved; |
| 148 | } |
| 149 | |
| 150 | @Override |
| 151 | public int hashCode() { |
Jian Li | d56f97e | 2016-07-19 15:48:15 +0900 | [diff] [blame] | 152 | return Objects.hash(srcPrefix, dstPrefix, srcMaskLength, dstMaskLength, reserved); |
Jian Li | c7e20a5 | 2016-07-18 19:03:49 +0900 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | @Override |
| 156 | public boolean equals(Object obj) { |
| 157 | if (this == obj) { |
| 158 | return true; |
| 159 | } |
| 160 | |
| 161 | if (obj instanceof LispSourceDestLcafAddress) { |
| 162 | final LispSourceDestLcafAddress other = (LispSourceDestLcafAddress) obj; |
| 163 | return Objects.equals(this.srcPrefix, other.srcPrefix) && |
Jian Li | 115d860 | 2016-08-15 20:21:53 +0900 | [diff] [blame] | 164 | Objects.equals(this.dstPrefix, other.dstPrefix) && |
| 165 | Objects.equals(this.srcMaskLength, other.srcMaskLength) && |
| 166 | Objects.equals(this.dstMaskLength, other.dstMaskLength) && |
| 167 | Objects.equals(this.reserved, other.reserved); |
Jian Li | c7e20a5 | 2016-07-18 19:03:49 +0900 | [diff] [blame] | 168 | } |
| 169 | return false; |
| 170 | } |
| 171 | |
| 172 | @Override |
| 173 | public String toString() { |
| 174 | return toStringHelper(this) |
| 175 | .add("source prefix", srcPrefix) |
| 176 | .add("destination prefix", dstPrefix) |
| 177 | .add("source mask length", srcMaskLength) |
| 178 | .add("destination mask length", dstMaskLength) |
| 179 | .add("reserved", reserved) |
| 180 | .toString(); |
| 181 | } |
Jian Li | 115d860 | 2016-08-15 20:21:53 +0900 | [diff] [blame] | 182 | |
Jian Li | a7b394d | 2016-08-21 23:11:46 +0900 | [diff] [blame] | 183 | public static final class SourceDestAddressBuilder |
| 184 | extends LcafAddressBuilder<SourceDestAddressBuilder> { |
Jian Li | 115d860 | 2016-08-15 20:21:53 +0900 | [diff] [blame] | 185 | private LispAfiAddress srcPrefix; |
| 186 | private LispAfiAddress dstPrefix; |
| 187 | private byte srcMaskLength; |
| 188 | private byte dstMaskLength; |
| 189 | private short reserved; |
| 190 | |
| 191 | /** |
| 192 | * Sets source address prefix. |
| 193 | * |
| 194 | * @param srcPrefix source prefix |
| 195 | * @return SourceDestAddressBuilder object |
| 196 | */ |
| 197 | public SourceDestAddressBuilder withSrcPrefix(LispAfiAddress srcPrefix) { |
| 198 | this.srcPrefix = srcPrefix; |
| 199 | return this; |
| 200 | } |
| 201 | |
| 202 | /** |
| 203 | * Sets destination address prefix. |
| 204 | * |
GUNi | ba87170 | 2016-08-22 21:06:02 +0900 | [diff] [blame] | 205 | * @param dstPrefix destination prefix |
Jian Li | 115d860 | 2016-08-15 20:21:53 +0900 | [diff] [blame] | 206 | * @return SourceDestAddressBuilder object |
| 207 | */ |
| 208 | public SourceDestAddressBuilder withDstPrefix(LispAfiAddress dstPrefix) { |
| 209 | this.dstPrefix = dstPrefix; |
| 210 | return this; |
| 211 | } |
| 212 | |
| 213 | /** |
| 214 | * Sets source mask length. |
| 215 | * |
| 216 | * @param srcMaskLength source mask length |
| 217 | * @return SourceDestAddressBuilder object |
| 218 | */ |
| 219 | public SourceDestAddressBuilder withSrcMaskLength(byte srcMaskLength) { |
| 220 | this.srcMaskLength = srcMaskLength; |
| 221 | return this; |
| 222 | } |
| 223 | |
| 224 | /** |
| 225 | * Sets destination mask length. |
| 226 | * |
| 227 | * @param dstMaskLength destination mask length |
| 228 | * @return SourceDestAddressBuilder object |
| 229 | */ |
| 230 | public SourceDestAddressBuilder withDstMaskLength(byte dstMaskLength) { |
| 231 | this.dstMaskLength = dstMaskLength; |
| 232 | return this; |
| 233 | } |
| 234 | |
| 235 | /** |
| 236 | * Sets reserved value. |
| 237 | * |
| 238 | * @param reserved reserved field value |
| 239 | * @return SourceDestAddressBuilder object |
| 240 | */ |
| 241 | public SourceDestAddressBuilder withReserved(short reserved) { |
| 242 | this.reserved = reserved; |
| 243 | return this; |
| 244 | } |
| 245 | |
| 246 | /** |
| 247 | * Builds LispSourceDestLcafAddress instance. |
| 248 | * |
| 249 | * @return LispSourceDestLcafAddress instance |
| 250 | */ |
| 251 | public LispSourceDestLcafAddress build() { |
Jian Li | d4e6370 | 2016-08-30 18:29:20 +0900 | [diff] [blame] | 252 | |
| 253 | checkNotNull(srcPrefix, "Must specify a source address prefix"); |
| 254 | checkNotNull(dstPrefix, "Must specify a destination address prefix"); |
| 255 | |
Jian Li | a7b394d | 2016-08-21 23:11:46 +0900 | [diff] [blame] | 256 | return new LispSourceDestLcafAddress(reserved1, reserved2, flag, length, |
| 257 | reserved, srcMaskLength, dstMaskLength, srcPrefix, dstPrefix); |
Jian Li | 115d860 | 2016-08-15 20:21:53 +0900 | [diff] [blame] | 258 | } |
| 259 | } |
| 260 | |
| 261 | /** |
| 262 | * SourceDest LCAF address reader class. |
| 263 | */ |
| 264 | public static class SourceDestLcafAddressReader |
| 265 | implements LispAddressReader<LispSourceDestLcafAddress> { |
| 266 | |
| 267 | @Override |
| 268 | public LispSourceDestLcafAddress readFrom(ByteBuf byteBuf) throws LispParseError, LispReaderException { |
| 269 | |
Jian Li | a7b394d | 2016-08-21 23:11:46 +0900 | [diff] [blame] | 270 | LispLcafAddress lcafAddress = LispLcafAddress.deserializeCommon(byteBuf); |
| 271 | |
Jian Li | 115d860 | 2016-08-15 20:21:53 +0900 | [diff] [blame] | 272 | short reserved = byteBuf.readShort(); |
| 273 | byte srcMaskLength = (byte) byteBuf.readUnsignedByte(); |
| 274 | byte dstMaskLength = (byte) byteBuf.readUnsignedByte(); |
| 275 | |
Jian Li | a7b394d | 2016-08-21 23:11:46 +0900 | [diff] [blame] | 276 | LispAfiAddress srcPrefix = new LispAfiAddress.AfiAddressReader().readFrom(byteBuf); |
| 277 | LispAfiAddress dstPrefix = new LispAfiAddress.AfiAddressReader().readFrom(byteBuf); |
Jian Li | 115d860 | 2016-08-15 20:21:53 +0900 | [diff] [blame] | 278 | |
| 279 | return new SourceDestAddressBuilder() |
Jian Li | a7b394d | 2016-08-21 23:11:46 +0900 | [diff] [blame] | 280 | .withReserved1(lcafAddress.getReserved1()) |
| 281 | .withReserved2(lcafAddress.getReserved2()) |
| 282 | .withFlag(lcafAddress.getFlag()) |
| 283 | .withLength(lcafAddress.getLength()) |
| 284 | .withReserved(reserved) |
| 285 | .withSrcMaskLength(srcMaskLength) |
| 286 | .withDstMaskLength(dstMaskLength) |
| 287 | .withSrcPrefix(srcPrefix) |
| 288 | .withDstPrefix(dstPrefix) |
| 289 | .build(); |
Jian Li | 115d860 | 2016-08-15 20:21:53 +0900 | [diff] [blame] | 290 | } |
| 291 | } |
GUNi | ba87170 | 2016-08-22 21:06:02 +0900 | [diff] [blame] | 292 | |
| 293 | /** |
| 294 | * SourceDest LCAF address writer class. |
| 295 | */ |
| 296 | public static class SourceDestLcafAddressWriter |
| 297 | implements LispAddressWriter<LispSourceDestLcafAddress> { |
| 298 | |
| 299 | @Override |
| 300 | public void writeTo(ByteBuf byteBuf, LispSourceDestLcafAddress address) |
| 301 | throws LispWriterException { |
| 302 | |
Jian Li | ef0f723 | 2016-11-15 19:55:46 +0900 | [diff] [blame] | 303 | int lcafIndex = byteBuf.writerIndex(); |
GUNi | ba87170 | 2016-08-22 21:06:02 +0900 | [diff] [blame] | 304 | LispLcafAddress.serializeCommon(byteBuf, address); |
| 305 | |
| 306 | byteBuf.writeShort(address.getReserved()); |
| 307 | byteBuf.writeByte(address.getSrcMaskLength()); |
| 308 | byteBuf.writeByte(address.getDstMaskLength()); |
| 309 | AfiAddressWriter writer = new LispAfiAddress.AfiAddressWriter(); |
| 310 | writer.writeTo(byteBuf, address.getSrcPrefix()); |
| 311 | writer.writeTo(byteBuf, address.getDstPrefix()); |
Jian Li | ef0f723 | 2016-11-15 19:55:46 +0900 | [diff] [blame] | 312 | |
| 313 | LispLcafAddress.updateLength(lcafIndex, byteBuf); |
GUNi | ba87170 | 2016-08-22 21:06:02 +0900 | [diff] [blame] | 314 | } |
| 315 | } |
Jian Li | c7e20a5 | 2016-07-18 19:03:49 +0900 | [diff] [blame] | 316 | } |