blob: 696d08e9f14afee74cd22860e290800934e147a7 [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 Lid6483cc2016-12-12 02:26:13 +090033 * Source destination key type is defined in draft-ietf-lisp-lcaf-22
34 * https://tools.ietf.org/html/draft-ietf-lisp-lcaf-22#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 Li3a99e712016-12-16 21:23:16 +090083 * Initializes source/destination key type LCAF address.
Jian Lia7b394d2016-08-21 23:11:46 +090084 *
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 */
Jian Li3a99e712016-12-16 21:23:16 +090095 private LispSourceDestLcafAddress(byte reserved1, byte reserved2, byte flag,
96 short length, short reserved,
97 byte srcMaskLength, byte dstMaskLength,
98 LispAfiAddress srcPrefix,
Jian Lia7b394d2016-08-21 23:11:46 +090099 LispAfiAddress dstPrefix) {
Jian Li3a99e712016-12-16 21:23:16 +0900100 super(LispCanonicalAddressFormatEnum.SOURCE_DEST, reserved1,
101 reserved2, flag, length);
Jian Lia7b394d2016-08-21 23:11:46 +0900102 this.reserved = reserved;
103 this.srcMaskLength = srcMaskLength;
104 this.dstMaskLength = dstMaskLength;
105 this.srcPrefix = srcPrefix;
106 this.dstPrefix = dstPrefix;
107 }
108
109 /**
Jian Lic7e20a52016-07-18 19:03:49 +0900110 * Obtains source address prefix.
111 *
112 * @return source address prefix
113 */
114 public LispAfiAddress getSrcPrefix() {
115 return srcPrefix;
116 }
117
118 /**
119 * Obtains destination address prefix.
120 *
121 * @return destination address prefix
122 */
123 public LispAfiAddress getDstPrefix() {
124 return dstPrefix;
125 }
126
127 /**
128 * Obtains source mask length.
129 *
130 * @return source mask length
131 */
132 public byte getSrcMaskLength() {
133 return srcMaskLength;
134 }
135
136 /**
137 * Obtains destination mask length.
138 *
139 * @return destination mask length
140 */
141 public byte getDstMaskLength() {
142 return dstMaskLength;
143 }
144
145 /**
146 * Obtains reserved value.
147 *
148 * @return reserved value
149 */
150 public short getReserved() {
151 return reserved;
152 }
153
154 @Override
155 public int hashCode() {
Jian Lid56f97e2016-07-19 15:48:15 +0900156 return Objects.hash(srcPrefix, dstPrefix, srcMaskLength, dstMaskLength, reserved);
Jian Lic7e20a52016-07-18 19:03:49 +0900157 }
158
159 @Override
160 public boolean equals(Object obj) {
161 if (this == obj) {
162 return true;
163 }
164
165 if (obj instanceof LispSourceDestLcafAddress) {
166 final LispSourceDestLcafAddress other = (LispSourceDestLcafAddress) obj;
167 return Objects.equals(this.srcPrefix, other.srcPrefix) &&
Jian Li115d8602016-08-15 20:21:53 +0900168 Objects.equals(this.dstPrefix, other.dstPrefix) &&
169 Objects.equals(this.srcMaskLength, other.srcMaskLength) &&
170 Objects.equals(this.dstMaskLength, other.dstMaskLength) &&
171 Objects.equals(this.reserved, other.reserved);
Jian Lic7e20a52016-07-18 19:03:49 +0900172 }
173 return false;
174 }
175
176 @Override
177 public String toString() {
178 return toStringHelper(this)
179 .add("source prefix", srcPrefix)
180 .add("destination prefix", dstPrefix)
181 .add("source mask length", srcMaskLength)
182 .add("destination mask length", dstMaskLength)
183 .add("reserved", reserved)
184 .toString();
185 }
Jian Li115d8602016-08-15 20:21:53 +0900186
Jian Lia7b394d2016-08-21 23:11:46 +0900187 public static final class SourceDestAddressBuilder
188 extends LcafAddressBuilder<SourceDestAddressBuilder> {
Jian Li115d8602016-08-15 20:21:53 +0900189 private LispAfiAddress srcPrefix;
190 private LispAfiAddress dstPrefix;
191 private byte srcMaskLength;
192 private byte dstMaskLength;
193 private short reserved;
194
195 /**
196 * Sets source address prefix.
197 *
198 * @param srcPrefix source prefix
199 * @return SourceDestAddressBuilder object
200 */
201 public SourceDestAddressBuilder withSrcPrefix(LispAfiAddress srcPrefix) {
202 this.srcPrefix = srcPrefix;
203 return this;
204 }
205
206 /**
207 * Sets destination address prefix.
208 *
GUNiba871702016-08-22 21:06:02 +0900209 * @param dstPrefix destination prefix
Jian Li115d8602016-08-15 20:21:53 +0900210 * @return SourceDestAddressBuilder object
211 */
212 public SourceDestAddressBuilder withDstPrefix(LispAfiAddress dstPrefix) {
213 this.dstPrefix = dstPrefix;
214 return this;
215 }
216
217 /**
218 * Sets source mask length.
219 *
220 * @param srcMaskLength source mask length
221 * @return SourceDestAddressBuilder object
222 */
223 public SourceDestAddressBuilder withSrcMaskLength(byte srcMaskLength) {
224 this.srcMaskLength = srcMaskLength;
225 return this;
226 }
227
228 /**
229 * Sets destination mask length.
230 *
231 * @param dstMaskLength destination mask length
232 * @return SourceDestAddressBuilder object
233 */
234 public SourceDestAddressBuilder withDstMaskLength(byte dstMaskLength) {
235 this.dstMaskLength = dstMaskLength;
236 return this;
237 }
238
239 /**
240 * Sets reserved value.
241 *
242 * @param reserved reserved field value
243 * @return SourceDestAddressBuilder object
244 */
245 public SourceDestAddressBuilder withReserved(short reserved) {
246 this.reserved = reserved;
247 return this;
248 }
249
250 /**
251 * Builds LispSourceDestLcafAddress instance.
252 *
253 * @return LispSourceDestLcafAddress instance
254 */
255 public LispSourceDestLcafAddress build() {
Jian Lid4e63702016-08-30 18:29:20 +0900256
257 checkNotNull(srcPrefix, "Must specify a source address prefix");
258 checkNotNull(dstPrefix, "Must specify a destination address prefix");
259
Jian Lia7b394d2016-08-21 23:11:46 +0900260 return new LispSourceDestLcafAddress(reserved1, reserved2, flag, length,
261 reserved, srcMaskLength, dstMaskLength, srcPrefix, dstPrefix);
Jian Li115d8602016-08-15 20:21:53 +0900262 }
263 }
264
265 /**
266 * SourceDest LCAF address reader class.
267 */
268 public static class SourceDestLcafAddressReader
269 implements LispAddressReader<LispSourceDestLcafAddress> {
270
271 @Override
Jian Li3a99e712016-12-16 21:23:16 +0900272 public LispSourceDestLcafAddress readFrom(ByteBuf byteBuf)
273 throws LispParseError, LispReaderException {
Jian Li115d8602016-08-15 20:21:53 +0900274
Jian Li3a99e712016-12-16 21:23:16 +0900275 deserializeCommon(byteBuf);
Jian Lia7b394d2016-08-21 23:11:46 +0900276
Jian Li115d8602016-08-15 20:21:53 +0900277 short reserved = byteBuf.readShort();
278 byte srcMaskLength = (byte) byteBuf.readUnsignedByte();
279 byte dstMaskLength = (byte) byteBuf.readUnsignedByte();
280
Jian Li5e505c62016-12-05 02:44:24 +0900281 LispAfiAddress srcPrefix = new AfiAddressReader().readFrom(byteBuf);
282 LispAfiAddress dstPrefix = new AfiAddressReader().readFrom(byteBuf);
Jian Li115d8602016-08-15 20:21:53 +0900283
284 return new SourceDestAddressBuilder()
Jian Lia7b394d2016-08-21 23:11:46 +0900285 .withReserved(reserved)
286 .withSrcMaskLength(srcMaskLength)
287 .withDstMaskLength(dstMaskLength)
288 .withSrcPrefix(srcPrefix)
289 .withDstPrefix(dstPrefix)
290 .build();
Jian Li115d8602016-08-15 20:21:53 +0900291 }
292 }
GUNiba871702016-08-22 21:06:02 +0900293
294 /**
295 * SourceDest LCAF address writer class.
296 */
297 public static class SourceDestLcafAddressWriter
298 implements LispAddressWriter<LispSourceDestLcafAddress> {
299
300 @Override
301 public void writeTo(ByteBuf byteBuf, LispSourceDestLcafAddress address)
302 throws LispWriterException {
303
Jian Lief0f7232016-11-15 19:55:46 +0900304 int lcafIndex = byteBuf.writerIndex();
Jian Li5e505c62016-12-05 02:44:24 +0900305 serializeCommon(byteBuf, address);
GUNiba871702016-08-22 21:06:02 +0900306
307 byteBuf.writeShort(address.getReserved());
308 byteBuf.writeByte(address.getSrcMaskLength());
309 byteBuf.writeByte(address.getDstMaskLength());
Jian Li5e505c62016-12-05 02:44:24 +0900310 AfiAddressWriter writer = new AfiAddressWriter();
GUNiba871702016-08-22 21:06:02 +0900311 writer.writeTo(byteBuf, address.getSrcPrefix());
312 writer.writeTo(byteBuf, address.getDstPrefix());
Jian Lief0f7232016-11-15 19:55:46 +0900313
Jian Li5e505c62016-12-05 02:44:24 +0900314 updateLength(lcafIndex, byteBuf);
GUNiba871702016-08-22 21:06:02 +0900315 }
316 }
Jian Lic7e20a52016-07-18 19:03:49 +0900317}