blob: 00accd92a98207f871f1633d1eb9a5ccb9408bbd [file] [log] [blame]
Jian Lic7e20a52016-07-18 19:03:49 +09001/*
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 */
Jian Lif31019a2017-02-05 07:57:46 +090016package org.onosproject.lisp.msg.types.lcaf;
Jian Lic7e20a52016-07-18 19:03:49 +090017
Jian Li115d8602016-08-15 20:21:53 +090018import io.netty.buffer.ByteBuf;
19import org.onosproject.lisp.msg.exceptions.LispParseError;
20import org.onosproject.lisp.msg.exceptions.LispReaderException;
GUNiba871702016-08-22 21:06:02 +090021import org.onosproject.lisp.msg.exceptions.LispWriterException;
Jian Lif31019a2017-02-05 07:57:46 +090022import org.onosproject.lisp.msg.types.LispAddressReader;
23import org.onosproject.lisp.msg.types.LispAddressWriter;
24import org.onosproject.lisp.msg.types.LispAfiAddress;
Jian Li115d8602016-08-15 20:21:53 +090025
Jian Lic7e20a52016-07-18 19:03:49 +090026import java.util.Objects;
27
28import static com.google.common.base.MoreObjects.toStringHelper;
Jian Lid4e63702016-08-30 18:29:20 +090029import static com.google.common.base.Preconditions.checkNotNull;
Jian Lic7e20a52016-07-18 19:03:49 +090030
31/**
32 * Source/Dest key type LCAF address class.
Jian Li8fc2d2f2016-08-08 14:43:53 +090033 * <p>
Jian Lid6483cc2016-12-12 02:26:13 +090034 * Source destination key type is defined in draft-ietf-lisp-lcaf-22
35 * https://tools.ietf.org/html/draft-ietf-lisp-lcaf-22#page-19
Jian Lic7e20a52016-07-18 19:03:49 +090036 *
Jian Li8fc2d2f2016-08-08 14:43:53 +090037 * <pre>
38 * {@literal
Jian Lic7e20a52016-07-18 19:03:49 +090039 * 0 1 2 3
40 * 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
41 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
42 * | AFI = 16387 | Rsvd1 | Flags |
43 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
Jian Li89c9ca92016-11-11 04:09:02 +090044 * | Type = 12 | Rsvd2 | Length |
Jian Lic7e20a52016-07-18 19:03:49 +090045 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
46 * | Reserved | Source-ML | Dest-ML |
47 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
48 * | AFI = x | Source-Prefix ... |
49 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
50 * | AFI = x | Destination-Prefix ... |
51 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
Jian Li8fc2d2f2016-08-08 14:43:53 +090052 * }</pre>
Jian Lic7e20a52016-07-18 19:03:49 +090053 */
Jian Li115d8602016-08-15 20:21:53 +090054public final class LispSourceDestLcafAddress extends LispLcafAddress {
Jian Lic7e20a52016-07-18 19:03:49 +090055
Jian Li115d8602016-08-15 20:21:53 +090056 private final LispAfiAddress srcPrefix;
57 private final LispAfiAddress dstPrefix;
Jian Lic7e20a52016-07-18 19:03:49 +090058 private final byte srcMaskLength;
59 private final byte dstMaskLength;
60 private final short reserved;
61
62 /**
63 * Initializes source/dest key type LCAF address.
Jian Lic7e20a52016-07-18 19:03:49 +090064 *
Jian Li115d8602016-08-15 20:21:53 +090065 * @param reserved reserved
Jian Lic7e20a52016-07-18 19:03:49 +090066 * @param srcMaskLength source mask length
67 * @param dstMaskLength destination mask length
Jian Li115d8602016-08-15 20:21:53 +090068 * @param srcPrefix source address prefix
69 * @param dstPrefix destination address prefix
Jian Lic7e20a52016-07-18 19:03:49 +090070 */
Jian Li115d8602016-08-15 20:21:53 +090071 private LispSourceDestLcafAddress(short reserved, byte srcMaskLength,
72 byte dstMaskLength,
73 LispAfiAddress srcPrefix,
74 LispAfiAddress dstPrefix) {
Jian Lic7e20a52016-07-18 19:03:49 +090075 super(LispCanonicalAddressFormatEnum.SOURCE_DEST);
76 this.reserved = reserved;
77 this.srcMaskLength = srcMaskLength;
78 this.dstMaskLength = dstMaskLength;
79 this.srcPrefix = srcPrefix;
80 this.dstPrefix = dstPrefix;
81 }
82
83 /**
Jian Li3a99e712016-12-16 21:23:16 +090084 * Initializes source/destination key type LCAF address.
Jian Lia7b394d2016-08-21 23:11:46 +090085 *
86 * @param reserved1 reserved1
87 * @param reserved2 reserved2
88 * @param flag flag
89 * @param length length
90 * @param reserved reserved
91 * @param srcMaskLength source mask length
92 * @param dstMaskLength destination mask length
93 * @param srcPrefix source address prefix
94 * @param dstPrefix destination address prefix
95 */
Jian Li3a99e712016-12-16 21:23:16 +090096 private LispSourceDestLcafAddress(byte reserved1, byte reserved2, byte flag,
97 short length, short reserved,
98 byte srcMaskLength, byte dstMaskLength,
99 LispAfiAddress srcPrefix,
Jian Lia7b394d2016-08-21 23:11:46 +0900100 LispAfiAddress dstPrefix) {
Jian Li3a99e712016-12-16 21:23:16 +0900101 super(LispCanonicalAddressFormatEnum.SOURCE_DEST, reserved1,
102 reserved2, flag, length);
Jian Lia7b394d2016-08-21 23:11:46 +0900103 this.reserved = reserved;
104 this.srcMaskLength = srcMaskLength;
105 this.dstMaskLength = dstMaskLength;
106 this.srcPrefix = srcPrefix;
107 this.dstPrefix = dstPrefix;
108 }
109
110 /**
Jian Lic7e20a52016-07-18 19:03:49 +0900111 * Obtains source address prefix.
112 *
113 * @return source address prefix
114 */
115 public LispAfiAddress getSrcPrefix() {
116 return srcPrefix;
117 }
118
119 /**
120 * Obtains destination address prefix.
121 *
122 * @return destination address prefix
123 */
124 public LispAfiAddress getDstPrefix() {
125 return dstPrefix;
126 }
127
128 /**
129 * Obtains source mask length.
130 *
131 * @return source mask length
132 */
133 public byte getSrcMaskLength() {
134 return srcMaskLength;
135 }
136
137 /**
138 * Obtains destination mask length.
139 *
140 * @return destination mask length
141 */
142 public byte getDstMaskLength() {
143 return dstMaskLength;
144 }
145
146 /**
147 * Obtains reserved value.
148 *
149 * @return reserved value
150 */
151 public short getReserved() {
152 return reserved;
153 }
154
155 @Override
156 public int hashCode() {
Jian Lid56f97e2016-07-19 15:48:15 +0900157 return Objects.hash(srcPrefix, dstPrefix, srcMaskLength, dstMaskLength, reserved);
Jian Lic7e20a52016-07-18 19:03:49 +0900158 }
159
160 @Override
161 public boolean equals(Object obj) {
162 if (this == obj) {
163 return true;
164 }
165
166 if (obj instanceof LispSourceDestLcafAddress) {
167 final LispSourceDestLcafAddress other = (LispSourceDestLcafAddress) obj;
168 return Objects.equals(this.srcPrefix, other.srcPrefix) &&
Jian Li115d8602016-08-15 20:21:53 +0900169 Objects.equals(this.dstPrefix, other.dstPrefix) &&
170 Objects.equals(this.srcMaskLength, other.srcMaskLength) &&
171 Objects.equals(this.dstMaskLength, other.dstMaskLength) &&
172 Objects.equals(this.reserved, other.reserved);
Jian Lic7e20a52016-07-18 19:03:49 +0900173 }
174 return false;
175 }
176
177 @Override
178 public String toString() {
179 return toStringHelper(this)
180 .add("source prefix", srcPrefix)
181 .add("destination prefix", dstPrefix)
182 .add("source mask length", srcMaskLength)
183 .add("destination mask length", dstMaskLength)
184 .add("reserved", reserved)
185 .toString();
186 }
Jian Li115d8602016-08-15 20:21:53 +0900187
Jian Lia7b394d2016-08-21 23:11:46 +0900188 public static final class SourceDestAddressBuilder
189 extends LcafAddressBuilder<SourceDestAddressBuilder> {
Jian Li115d8602016-08-15 20:21:53 +0900190 private LispAfiAddress srcPrefix;
191 private LispAfiAddress dstPrefix;
192 private byte srcMaskLength;
193 private byte dstMaskLength;
194 private short reserved;
195
196 /**
197 * Sets source address prefix.
198 *
199 * @param srcPrefix source prefix
200 * @return SourceDestAddressBuilder object
201 */
202 public SourceDestAddressBuilder withSrcPrefix(LispAfiAddress srcPrefix) {
203 this.srcPrefix = srcPrefix;
204 return this;
205 }
206
207 /**
208 * Sets destination address prefix.
209 *
GUNiba871702016-08-22 21:06:02 +0900210 * @param dstPrefix destination prefix
Jian Li115d8602016-08-15 20:21:53 +0900211 * @return SourceDestAddressBuilder object
212 */
213 public SourceDestAddressBuilder withDstPrefix(LispAfiAddress dstPrefix) {
214 this.dstPrefix = dstPrefix;
215 return this;
216 }
217
218 /**
219 * Sets source mask length.
220 *
221 * @param srcMaskLength source mask length
222 * @return SourceDestAddressBuilder object
223 */
224 public SourceDestAddressBuilder withSrcMaskLength(byte srcMaskLength) {
225 this.srcMaskLength = srcMaskLength;
226 return this;
227 }
228
229 /**
230 * Sets destination mask length.
231 *
232 * @param dstMaskLength destination mask length
233 * @return SourceDestAddressBuilder object
234 */
235 public SourceDestAddressBuilder withDstMaskLength(byte dstMaskLength) {
236 this.dstMaskLength = dstMaskLength;
237 return this;
238 }
239
240 /**
241 * Sets reserved value.
242 *
243 * @param reserved reserved field value
244 * @return SourceDestAddressBuilder object
245 */
246 public SourceDestAddressBuilder withReserved(short reserved) {
247 this.reserved = reserved;
248 return this;
249 }
250
251 /**
252 * Builds LispSourceDestLcafAddress instance.
253 *
254 * @return LispSourceDestLcafAddress instance
255 */
256 public LispSourceDestLcafAddress build() {
Jian Lid4e63702016-08-30 18:29:20 +0900257
258 checkNotNull(srcPrefix, "Must specify a source address prefix");
259 checkNotNull(dstPrefix, "Must specify a destination address prefix");
260
Jian Lia7b394d2016-08-21 23:11:46 +0900261 return new LispSourceDestLcafAddress(reserved1, reserved2, flag, length,
262 reserved, srcMaskLength, dstMaskLength, srcPrefix, dstPrefix);
Jian Li115d8602016-08-15 20:21:53 +0900263 }
264 }
265
266 /**
267 * SourceDest LCAF address reader class.
268 */
269 public static class SourceDestLcafAddressReader
270 implements LispAddressReader<LispSourceDestLcafAddress> {
271
272 @Override
Jian Li3a99e712016-12-16 21:23:16 +0900273 public LispSourceDestLcafAddress readFrom(ByteBuf byteBuf)
274 throws LispParseError, LispReaderException {
Jian Li115d8602016-08-15 20:21:53 +0900275
Jian Li3a99e712016-12-16 21:23:16 +0900276 deserializeCommon(byteBuf);
Jian Lia7b394d2016-08-21 23:11:46 +0900277
Jian Li115d8602016-08-15 20:21:53 +0900278 short reserved = byteBuf.readShort();
279 byte srcMaskLength = (byte) byteBuf.readUnsignedByte();
280 byte dstMaskLength = (byte) byteBuf.readUnsignedByte();
281
Jian Li5e505c62016-12-05 02:44:24 +0900282 LispAfiAddress srcPrefix = new AfiAddressReader().readFrom(byteBuf);
283 LispAfiAddress dstPrefix = new AfiAddressReader().readFrom(byteBuf);
Jian Li115d8602016-08-15 20:21:53 +0900284
285 return new SourceDestAddressBuilder()
Jian Lia7b394d2016-08-21 23:11:46 +0900286 .withReserved(reserved)
287 .withSrcMaskLength(srcMaskLength)
288 .withDstMaskLength(dstMaskLength)
289 .withSrcPrefix(srcPrefix)
290 .withDstPrefix(dstPrefix)
291 .build();
Jian Li115d8602016-08-15 20:21:53 +0900292 }
293 }
GUNiba871702016-08-22 21:06:02 +0900294
295 /**
296 * SourceDest LCAF address writer class.
297 */
298 public static class SourceDestLcafAddressWriter
299 implements LispAddressWriter<LispSourceDestLcafAddress> {
300
301 @Override
302 public void writeTo(ByteBuf byteBuf, LispSourceDestLcafAddress address)
303 throws LispWriterException {
304
Jian Lief0f7232016-11-15 19:55:46 +0900305 int lcafIndex = byteBuf.writerIndex();
Jian Li5e505c62016-12-05 02:44:24 +0900306 serializeCommon(byteBuf, address);
GUNiba871702016-08-22 21:06:02 +0900307
308 byteBuf.writeShort(address.getReserved());
309 byteBuf.writeByte(address.getSrcMaskLength());
310 byteBuf.writeByte(address.getDstMaskLength());
Jian Li5e505c62016-12-05 02:44:24 +0900311 AfiAddressWriter writer = new AfiAddressWriter();
GUNiba871702016-08-22 21:06:02 +0900312 writer.writeTo(byteBuf, address.getSrcPrefix());
313 writer.writeTo(byteBuf, address.getDstPrefix());
Jian Lief0f7232016-11-15 19:55:46 +0900314
Jian Li5e505c62016-12-05 02:44:24 +0900315 updateLength(lcafIndex, byteBuf);
GUNiba871702016-08-22 21:06:02 +0900316 }
317 }
Jian Lic7e20a52016-07-18 19:03:49 +0900318}