blob: d36b591d950193b6d9469ad164e19f5018bedba3 [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 */
16package org.onosproject.lisp.msg.types;
17
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 Li5e505c62016-12-05 02:44:24 +090022import org.onosproject.lisp.msg.types.LispAfiAddress.AfiAddressReader;
23import org.onosproject.lisp.msg.types.LispAfiAddress.AfiAddressWriter;
Jian Li115d8602016-08-15 20:21:53 +090024
Jian Lic7e20a52016-07-18 19:03:49 +090025import java.util.Objects;
26
27import static com.google.common.base.MoreObjects.toStringHelper;
Jian Lid4e63702016-08-30 18:29:20 +090028import static com.google.common.base.Preconditions.checkNotNull;
Jian Lic7e20a52016-07-18 19:03:49 +090029
30/**
31 * Source/Dest key type LCAF address class.
Jian Li8fc2d2f2016-08-08 14:43:53 +090032 * <p>
Jian Li89c9ca92016-11-11 04:09:02 +090033 * Source destination key type is defined in draft-ietf-lisp-lcaf-20
34 * https://tools.ietf.org/html/draft-ietf-lisp-lcaf-20#page-19
Jian Lic7e20a52016-07-18 19:03:49 +090035 *
Jian Li8fc2d2f2016-08-08 14:43:53 +090036 * <pre>
37 * {@literal
Jian Lic7e20a52016-07-18 19:03:49 +090038 * 0 1 2 3
39 * 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
40 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
41 * | AFI = 16387 | Rsvd1 | Flags |
42 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
Jian Li89c9ca92016-11-11 04:09:02 +090043 * | Type = 12 | Rsvd2 | Length |
Jian Lic7e20a52016-07-18 19:03:49 +090044 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
45 * | Reserved | Source-ML | Dest-ML |
46 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
47 * | AFI = x | Source-Prefix ... |
48 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
49 * | AFI = x | Destination-Prefix ... |
50 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
Jian Li8fc2d2f2016-08-08 14:43:53 +090051 * }</pre>
Jian Lic7e20a52016-07-18 19:03:49 +090052 */
Jian Li115d8602016-08-15 20:21:53 +090053public final class LispSourceDestLcafAddress extends LispLcafAddress {
Jian Lic7e20a52016-07-18 19:03:49 +090054
Jian Li115d8602016-08-15 20:21:53 +090055 private final LispAfiAddress srcPrefix;
56 private final LispAfiAddress dstPrefix;
Jian Lic7e20a52016-07-18 19:03:49 +090057 private final byte srcMaskLength;
58 private final byte dstMaskLength;
59 private final short reserved;
60
61 /**
62 * Initializes source/dest key type LCAF address.
Jian Lic7e20a52016-07-18 19:03:49 +090063 *
Jian Li115d8602016-08-15 20:21:53 +090064 * @param reserved reserved
Jian Lic7e20a52016-07-18 19:03:49 +090065 * @param srcMaskLength source mask length
66 * @param dstMaskLength destination mask length
Jian Li115d8602016-08-15 20:21:53 +090067 * @param srcPrefix source address prefix
68 * @param dstPrefix destination address prefix
Jian Lic7e20a52016-07-18 19:03:49 +090069 */
Jian Li115d8602016-08-15 20:21:53 +090070 private LispSourceDestLcafAddress(short reserved, byte srcMaskLength,
71 byte dstMaskLength,
72 LispAfiAddress srcPrefix,
73 LispAfiAddress dstPrefix) {
Jian Lic7e20a52016-07-18 19:03:49 +090074 super(LispCanonicalAddressFormatEnum.SOURCE_DEST);
75 this.reserved = reserved;
76 this.srcMaskLength = srcMaskLength;
77 this.dstMaskLength = dstMaskLength;
78 this.srcPrefix = srcPrefix;
79 this.dstPrefix = dstPrefix;
80 }
81
82 /**
Jian Lia7b394d2016-08-21 23:11:46 +090083 * Initializes source/dest key type LCAF address.
84 *
85 * @param reserved1 reserved1
86 * @param reserved2 reserved2
87 * @param flag flag
88 * @param length length
89 * @param reserved reserved
90 * @param srcMaskLength source mask length
91 * @param dstMaskLength destination mask length
92 * @param srcPrefix source address prefix
93 * @param dstPrefix destination address prefix
94 */
95 private LispSourceDestLcafAddress(byte reserved1, byte reserved2, byte flag, short length,
96 short reserved, byte srcMaskLength,
97 byte dstMaskLength, LispAfiAddress srcPrefix,
98 LispAfiAddress dstPrefix) {
99 super(LispCanonicalAddressFormatEnum.SOURCE_DEST, reserved1, reserved2, flag, length);
100 this.reserved = reserved;
101 this.srcMaskLength = srcMaskLength;
102 this.dstMaskLength = dstMaskLength;
103 this.srcPrefix = srcPrefix;
104 this.dstPrefix = dstPrefix;
105 }
106
107 /**
Jian Lic7e20a52016-07-18 19:03:49 +0900108 * Obtains source address prefix.
109 *
110 * @return source address prefix
111 */
112 public LispAfiAddress getSrcPrefix() {
113 return srcPrefix;
114 }
115
116 /**
117 * Obtains destination address prefix.
118 *
119 * @return destination address prefix
120 */
121 public LispAfiAddress getDstPrefix() {
122 return dstPrefix;
123 }
124
125 /**
126 * Obtains source mask length.
127 *
128 * @return source mask length
129 */
130 public byte getSrcMaskLength() {
131 return srcMaskLength;
132 }
133
134 /**
135 * Obtains destination mask length.
136 *
137 * @return destination mask length
138 */
139 public byte getDstMaskLength() {
140 return dstMaskLength;
141 }
142
143 /**
144 * Obtains reserved value.
145 *
146 * @return reserved value
147 */
148 public short getReserved() {
149 return reserved;
150 }
151
152 @Override
153 public int hashCode() {
Jian Lid56f97e2016-07-19 15:48:15 +0900154 return Objects.hash(srcPrefix, dstPrefix, srcMaskLength, dstMaskLength, reserved);
Jian Lic7e20a52016-07-18 19:03:49 +0900155 }
156
157 @Override
158 public boolean equals(Object obj) {
159 if (this == obj) {
160 return true;
161 }
162
163 if (obj instanceof LispSourceDestLcafAddress) {
164 final LispSourceDestLcafAddress other = (LispSourceDestLcafAddress) obj;
165 return Objects.equals(this.srcPrefix, other.srcPrefix) &&
Jian Li115d8602016-08-15 20:21:53 +0900166 Objects.equals(this.dstPrefix, other.dstPrefix) &&
167 Objects.equals(this.srcMaskLength, other.srcMaskLength) &&
168 Objects.equals(this.dstMaskLength, other.dstMaskLength) &&
169 Objects.equals(this.reserved, other.reserved);
Jian Lic7e20a52016-07-18 19:03:49 +0900170 }
171 return false;
172 }
173
174 @Override
175 public String toString() {
176 return toStringHelper(this)
177 .add("source prefix", srcPrefix)
178 .add("destination prefix", dstPrefix)
179 .add("source mask length", srcMaskLength)
180 .add("destination mask length", dstMaskLength)
181 .add("reserved", reserved)
182 .toString();
183 }
Jian Li115d8602016-08-15 20:21:53 +0900184
Jian Lia7b394d2016-08-21 23:11:46 +0900185 public static final class SourceDestAddressBuilder
186 extends LcafAddressBuilder<SourceDestAddressBuilder> {
Jian Li115d8602016-08-15 20:21:53 +0900187 private LispAfiAddress srcPrefix;
188 private LispAfiAddress dstPrefix;
189 private byte srcMaskLength;
190 private byte dstMaskLength;
191 private short reserved;
192
193 /**
194 * Sets source address prefix.
195 *
196 * @param srcPrefix source prefix
197 * @return SourceDestAddressBuilder object
198 */
199 public SourceDestAddressBuilder withSrcPrefix(LispAfiAddress srcPrefix) {
200 this.srcPrefix = srcPrefix;
201 return this;
202 }
203
204 /**
205 * Sets destination address prefix.
206 *
GUNiba871702016-08-22 21:06:02 +0900207 * @param dstPrefix destination prefix
Jian Li115d8602016-08-15 20:21:53 +0900208 * @return SourceDestAddressBuilder object
209 */
210 public SourceDestAddressBuilder withDstPrefix(LispAfiAddress dstPrefix) {
211 this.dstPrefix = dstPrefix;
212 return this;
213 }
214
215 /**
216 * Sets source mask length.
217 *
218 * @param srcMaskLength source mask length
219 * @return SourceDestAddressBuilder object
220 */
221 public SourceDestAddressBuilder withSrcMaskLength(byte srcMaskLength) {
222 this.srcMaskLength = srcMaskLength;
223 return this;
224 }
225
226 /**
227 * Sets destination mask length.
228 *
229 * @param dstMaskLength destination mask length
230 * @return SourceDestAddressBuilder object
231 */
232 public SourceDestAddressBuilder withDstMaskLength(byte dstMaskLength) {
233 this.dstMaskLength = dstMaskLength;
234 return this;
235 }
236
237 /**
238 * Sets reserved value.
239 *
240 * @param reserved reserved field value
241 * @return SourceDestAddressBuilder object
242 */
243 public SourceDestAddressBuilder withReserved(short reserved) {
244 this.reserved = reserved;
245 return this;
246 }
247
248 /**
249 * Builds LispSourceDestLcafAddress instance.
250 *
251 * @return LispSourceDestLcafAddress instance
252 */
253 public LispSourceDestLcafAddress build() {
Jian Lid4e63702016-08-30 18:29:20 +0900254
255 checkNotNull(srcPrefix, "Must specify a source address prefix");
256 checkNotNull(dstPrefix, "Must specify a destination address prefix");
257
Jian Lia7b394d2016-08-21 23:11:46 +0900258 return new LispSourceDestLcafAddress(reserved1, reserved2, flag, length,
259 reserved, srcMaskLength, dstMaskLength, srcPrefix, dstPrefix);
Jian Li115d8602016-08-15 20:21:53 +0900260 }
261 }
262
263 /**
264 * SourceDest LCAF address reader class.
265 */
266 public static class SourceDestLcafAddressReader
267 implements LispAddressReader<LispSourceDestLcafAddress> {
268
269 @Override
270 public LispSourceDestLcafAddress readFrom(ByteBuf byteBuf) throws LispParseError, LispReaderException {
271
Jian Li5e505c62016-12-05 02:44:24 +0900272 LispLcafAddress lcafAddress = deserializeCommon(byteBuf);
Jian Lia7b394d2016-08-21 23:11:46 +0900273
Jian Li115d8602016-08-15 20:21:53 +0900274 short reserved = byteBuf.readShort();
275 byte srcMaskLength = (byte) byteBuf.readUnsignedByte();
276 byte dstMaskLength = (byte) byteBuf.readUnsignedByte();
277
Jian Li5e505c62016-12-05 02:44:24 +0900278 LispAfiAddress srcPrefix = new AfiAddressReader().readFrom(byteBuf);
279 LispAfiAddress dstPrefix = new AfiAddressReader().readFrom(byteBuf);
Jian Li115d8602016-08-15 20:21:53 +0900280
281 return new SourceDestAddressBuilder()
Jian Lia7b394d2016-08-21 23:11:46 +0900282 .withReserved1(lcafAddress.getReserved1())
283 .withReserved2(lcafAddress.getReserved2())
284 .withFlag(lcafAddress.getFlag())
285 .withLength(lcafAddress.getLength())
286 .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}