blob: 80dd53839bf382220de20f58406f2b1558d71242 [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 Li115d8602016-08-15 20:21:53 +090022
Jian Lic7e20a52016-07-18 19:03:49 +090023import java.util.Objects;
24
25import static com.google.common.base.MoreObjects.toStringHelper;
26
27/**
28 * Source/Dest key type LCAF address class.
Jian Li8fc2d2f2016-08-08 14:43:53 +090029 * <p>
Jian Lic7e20a52016-07-18 19:03:49 +090030 * Source destination key type is defined in draft-ietf-lisp-lcaf-13
31 * https://tools.ietf.org/html/draft-ietf-lisp-lcaf-13#page-18
32 *
Jian Li8fc2d2f2016-08-08 14:43:53 +090033 * <pre>
34 * {@literal
Jian Lic7e20a52016-07-18 19:03:49 +090035 * 0 1 2 3
36 * 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
37 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
38 * | AFI = 16387 | Rsvd1 | Flags |
39 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
40 * | Type = 12 | Rsvd2 | 4 + n |
41 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
42 * | Reserved | Source-ML | Dest-ML |
43 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
44 * | AFI = x | Source-Prefix ... |
45 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
46 * | AFI = x | Destination-Prefix ... |
47 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
Jian Li8fc2d2f2016-08-08 14:43:53 +090048 * }</pre>
Jian Lic7e20a52016-07-18 19:03:49 +090049 */
Jian Li115d8602016-08-15 20:21:53 +090050public final class LispSourceDestLcafAddress extends LispLcafAddress {
Jian Lic7e20a52016-07-18 19:03:49 +090051
Jian Li115d8602016-08-15 20:21:53 +090052 private final LispAfiAddress srcPrefix;
53 private final LispAfiAddress dstPrefix;
Jian Lic7e20a52016-07-18 19:03:49 +090054 private final byte srcMaskLength;
55 private final byte dstMaskLength;
56 private final short reserved;
57
58 /**
59 * Initializes source/dest key type LCAF address.
Jian Lic7e20a52016-07-18 19:03:49 +090060 *
Jian Li115d8602016-08-15 20:21:53 +090061 * @param reserved reserved
Jian Lic7e20a52016-07-18 19:03:49 +090062 * @param srcMaskLength source mask length
63 * @param dstMaskLength destination mask length
Jian Li115d8602016-08-15 20:21:53 +090064 * @param srcPrefix source address prefix
65 * @param dstPrefix destination address prefix
Jian Lic7e20a52016-07-18 19:03:49 +090066 */
Jian Li115d8602016-08-15 20:21:53 +090067 private LispSourceDestLcafAddress(short reserved, byte srcMaskLength,
68 byte dstMaskLength,
69 LispAfiAddress srcPrefix,
70 LispAfiAddress dstPrefix) {
Jian Lic7e20a52016-07-18 19:03:49 +090071 super(LispCanonicalAddressFormatEnum.SOURCE_DEST);
72 this.reserved = reserved;
73 this.srcMaskLength = srcMaskLength;
74 this.dstMaskLength = dstMaskLength;
75 this.srcPrefix = srcPrefix;
76 this.dstPrefix = dstPrefix;
77 }
78
79 /**
Jian Lia7b394d2016-08-21 23:11:46 +090080 * Initializes source/dest key type LCAF address.
81 *
82 * @param reserved1 reserved1
83 * @param reserved2 reserved2
84 * @param flag flag
85 * @param length length
86 * @param reserved reserved
87 * @param srcMaskLength source mask length
88 * @param dstMaskLength destination mask length
89 * @param srcPrefix source address prefix
90 * @param dstPrefix destination address prefix
91 */
92 private LispSourceDestLcafAddress(byte reserved1, byte reserved2, byte flag, short length,
93 short reserved, byte srcMaskLength,
94 byte dstMaskLength, LispAfiAddress srcPrefix,
95 LispAfiAddress dstPrefix) {
96 super(LispCanonicalAddressFormatEnum.SOURCE_DEST, reserved1, reserved2, flag, length);
97 this.reserved = reserved;
98 this.srcMaskLength = srcMaskLength;
99 this.dstMaskLength = dstMaskLength;
100 this.srcPrefix = srcPrefix;
101 this.dstPrefix = dstPrefix;
102 }
103
104 /**
Jian Lic7e20a52016-07-18 19:03:49 +0900105 * Obtains source address prefix.
106 *
107 * @return source address prefix
108 */
109 public LispAfiAddress getSrcPrefix() {
110 return srcPrefix;
111 }
112
113 /**
114 * Obtains destination address prefix.
115 *
116 * @return destination address prefix
117 */
118 public LispAfiAddress getDstPrefix() {
119 return dstPrefix;
120 }
121
122 /**
123 * Obtains source mask length.
124 *
125 * @return source mask length
126 */
127 public byte getSrcMaskLength() {
128 return srcMaskLength;
129 }
130
131 /**
132 * Obtains destination mask length.
133 *
134 * @return destination mask length
135 */
136 public byte getDstMaskLength() {
137 return dstMaskLength;
138 }
139
140 /**
141 * Obtains reserved value.
142 *
143 * @return reserved value
144 */
145 public short getReserved() {
146 return reserved;
147 }
148
149 @Override
150 public int hashCode() {
Jian Lid56f97e2016-07-19 15:48:15 +0900151 return Objects.hash(srcPrefix, dstPrefix, srcMaskLength, dstMaskLength, reserved);
Jian Lic7e20a52016-07-18 19:03:49 +0900152 }
153
154 @Override
155 public boolean equals(Object obj) {
156 if (this == obj) {
157 return true;
158 }
159
160 if (obj instanceof LispSourceDestLcafAddress) {
161 final LispSourceDestLcafAddress other = (LispSourceDestLcafAddress) obj;
162 return Objects.equals(this.srcPrefix, other.srcPrefix) &&
Jian Li115d8602016-08-15 20:21:53 +0900163 Objects.equals(this.dstPrefix, other.dstPrefix) &&
164 Objects.equals(this.srcMaskLength, other.srcMaskLength) &&
165 Objects.equals(this.dstMaskLength, other.dstMaskLength) &&
166 Objects.equals(this.reserved, other.reserved);
Jian Lic7e20a52016-07-18 19:03:49 +0900167 }
168 return false;
169 }
170
171 @Override
172 public String toString() {
173 return toStringHelper(this)
174 .add("source prefix", srcPrefix)
175 .add("destination prefix", dstPrefix)
176 .add("source mask length", srcMaskLength)
177 .add("destination mask length", dstMaskLength)
178 .add("reserved", reserved)
179 .toString();
180 }
Jian Li115d8602016-08-15 20:21:53 +0900181
Jian Lia7b394d2016-08-21 23:11:46 +0900182 public static final class SourceDestAddressBuilder
183 extends LcafAddressBuilder<SourceDestAddressBuilder> {
Jian Li115d8602016-08-15 20:21:53 +0900184 private LispAfiAddress srcPrefix;
185 private LispAfiAddress dstPrefix;
186 private byte srcMaskLength;
187 private byte dstMaskLength;
188 private short reserved;
189
190 /**
191 * Sets source address prefix.
192 *
193 * @param srcPrefix source prefix
194 * @return SourceDestAddressBuilder object
195 */
196 public SourceDestAddressBuilder withSrcPrefix(LispAfiAddress srcPrefix) {
197 this.srcPrefix = srcPrefix;
198 return this;
199 }
200
201 /**
202 * Sets destination address prefix.
203 *
GUNiba871702016-08-22 21:06:02 +0900204 * @param dstPrefix destination prefix
Jian Li115d8602016-08-15 20:21:53 +0900205 * @return SourceDestAddressBuilder object
206 */
207 public SourceDestAddressBuilder withDstPrefix(LispAfiAddress dstPrefix) {
208 this.dstPrefix = dstPrefix;
209 return this;
210 }
211
212 /**
213 * Sets source mask length.
214 *
215 * @param srcMaskLength source mask length
216 * @return SourceDestAddressBuilder object
217 */
218 public SourceDestAddressBuilder withSrcMaskLength(byte srcMaskLength) {
219 this.srcMaskLength = srcMaskLength;
220 return this;
221 }
222
223 /**
224 * Sets destination mask length.
225 *
226 * @param dstMaskLength destination mask length
227 * @return SourceDestAddressBuilder object
228 */
229 public SourceDestAddressBuilder withDstMaskLength(byte dstMaskLength) {
230 this.dstMaskLength = dstMaskLength;
231 return this;
232 }
233
234 /**
235 * Sets reserved value.
236 *
237 * @param reserved reserved field value
238 * @return SourceDestAddressBuilder object
239 */
240 public SourceDestAddressBuilder withReserved(short reserved) {
241 this.reserved = reserved;
242 return this;
243 }
244
245 /**
246 * Builds LispSourceDestLcafAddress instance.
247 *
248 * @return LispSourceDestLcafAddress instance
249 */
250 public LispSourceDestLcafAddress build() {
Jian Lia7b394d2016-08-21 23:11:46 +0900251 return new LispSourceDestLcafAddress(reserved1, reserved2, flag, length,
252 reserved, srcMaskLength, dstMaskLength, srcPrefix, dstPrefix);
Jian Li115d8602016-08-15 20:21:53 +0900253 }
254 }
255
256 /**
257 * SourceDest LCAF address reader class.
258 */
259 public static class SourceDestLcafAddressReader
260 implements LispAddressReader<LispSourceDestLcafAddress> {
261
262 @Override
263 public LispSourceDestLcafAddress readFrom(ByteBuf byteBuf) throws LispParseError, LispReaderException {
264
Jian Lia7b394d2016-08-21 23:11:46 +0900265 LispLcafAddress lcafAddress = LispLcafAddress.deserializeCommon(byteBuf);
266
Jian Li115d8602016-08-15 20:21:53 +0900267 short reserved = byteBuf.readShort();
268 byte srcMaskLength = (byte) byteBuf.readUnsignedByte();
269 byte dstMaskLength = (byte) byteBuf.readUnsignedByte();
270
Jian Lia7b394d2016-08-21 23:11:46 +0900271 LispAfiAddress srcPrefix = new LispAfiAddress.AfiAddressReader().readFrom(byteBuf);
272 LispAfiAddress dstPrefix = new LispAfiAddress.AfiAddressReader().readFrom(byteBuf);
Jian Li115d8602016-08-15 20:21:53 +0900273
274 return new SourceDestAddressBuilder()
Jian Lia7b394d2016-08-21 23:11:46 +0900275 .withReserved1(lcafAddress.getReserved1())
276 .withReserved2(lcafAddress.getReserved2())
277 .withFlag(lcafAddress.getFlag())
278 .withLength(lcafAddress.getLength())
279 .withReserved(reserved)
280 .withSrcMaskLength(srcMaskLength)
281 .withDstMaskLength(dstMaskLength)
282 .withSrcPrefix(srcPrefix)
283 .withDstPrefix(dstPrefix)
284 .build();
Jian Li115d8602016-08-15 20:21:53 +0900285 }
286 }
GUNiba871702016-08-22 21:06:02 +0900287
288 /**
289 * SourceDest LCAF address writer class.
290 */
291 public static class SourceDestLcafAddressWriter
292 implements LispAddressWriter<LispSourceDestLcafAddress> {
293
294 @Override
295 public void writeTo(ByteBuf byteBuf, LispSourceDestLcafAddress address)
296 throws LispWriterException {
297
298 LispLcafAddress.serializeCommon(byteBuf, address);
299
300 byteBuf.writeShort(address.getReserved());
301 byteBuf.writeByte(address.getSrcMaskLength());
302 byteBuf.writeByte(address.getDstMaskLength());
303 AfiAddressWriter writer = new LispAfiAddress.AfiAddressWriter();
304 writer.writeTo(byteBuf, address.getSrcPrefix());
305 writer.writeTo(byteBuf, address.getDstPrefix());
306 }
307 }
Jian Lic7e20a52016-07-18 19:03:49 +0900308}