blob: 08d9205d84464f56421afff47c7fe5065777ef63 [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;
Jian Lid4e63702016-08-30 18:29:20 +090026import static com.google.common.base.Preconditions.checkNotNull;
Jian Lic7e20a52016-07-18 19:03:49 +090027
28/**
29 * Source/Dest key type LCAF address class.
Jian Li8fc2d2f2016-08-08 14:43:53 +090030 * <p>
Jian Li89c9ca92016-11-11 04:09:02 +090031 * 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 Lic7e20a52016-07-18 19:03:49 +090033 *
Jian Li8fc2d2f2016-08-08 14:43:53 +090034 * <pre>
35 * {@literal
Jian Lic7e20a52016-07-18 19:03:49 +090036 * 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 Li89c9ca92016-11-11 04:09:02 +090041 * | Type = 12 | Rsvd2 | Length |
Jian Lic7e20a52016-07-18 19:03:49 +090042 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
43 * | Reserved | Source-ML | Dest-ML |
44 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
45 * | AFI = x | Source-Prefix ... |
46 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
47 * | AFI = x | Destination-Prefix ... |
48 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
Jian Li8fc2d2f2016-08-08 14:43:53 +090049 * }</pre>
Jian Lic7e20a52016-07-18 19:03:49 +090050 */
Jian Li115d8602016-08-15 20:21:53 +090051public final class LispSourceDestLcafAddress extends LispLcafAddress {
Jian Lic7e20a52016-07-18 19:03:49 +090052
Jian Li115d8602016-08-15 20:21:53 +090053 private final LispAfiAddress srcPrefix;
54 private final LispAfiAddress dstPrefix;
Jian Lic7e20a52016-07-18 19:03:49 +090055 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 Lic7e20a52016-07-18 19:03:49 +090061 *
Jian Li115d8602016-08-15 20:21:53 +090062 * @param reserved reserved
Jian Lic7e20a52016-07-18 19:03:49 +090063 * @param srcMaskLength source mask length
64 * @param dstMaskLength destination mask length
Jian Li115d8602016-08-15 20:21:53 +090065 * @param srcPrefix source address prefix
66 * @param dstPrefix destination address prefix
Jian Lic7e20a52016-07-18 19:03:49 +090067 */
Jian Li115d8602016-08-15 20:21:53 +090068 private LispSourceDestLcafAddress(short reserved, byte srcMaskLength,
69 byte dstMaskLength,
70 LispAfiAddress srcPrefix,
71 LispAfiAddress dstPrefix) {
Jian Lic7e20a52016-07-18 19:03:49 +090072 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 Lia7b394d2016-08-21 23:11:46 +090081 * 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 Lic7e20a52016-07-18 19:03:49 +0900106 * 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 Lid56f97e2016-07-19 15:48:15 +0900152 return Objects.hash(srcPrefix, dstPrefix, srcMaskLength, dstMaskLength, reserved);
Jian Lic7e20a52016-07-18 19:03:49 +0900153 }
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 Li115d8602016-08-15 20:21:53 +0900164 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 Lic7e20a52016-07-18 19:03:49 +0900168 }
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 Li115d8602016-08-15 20:21:53 +0900182
Jian Lia7b394d2016-08-21 23:11:46 +0900183 public static final class SourceDestAddressBuilder
184 extends LcafAddressBuilder<SourceDestAddressBuilder> {
Jian Li115d8602016-08-15 20:21:53 +0900185 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 *
GUNiba871702016-08-22 21:06:02 +0900205 * @param dstPrefix destination prefix
Jian Li115d8602016-08-15 20:21:53 +0900206 * @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 Lid4e63702016-08-30 18:29:20 +0900252
253 checkNotNull(srcPrefix, "Must specify a source address prefix");
254 checkNotNull(dstPrefix, "Must specify a destination address prefix");
255
Jian Lia7b394d2016-08-21 23:11:46 +0900256 return new LispSourceDestLcafAddress(reserved1, reserved2, flag, length,
257 reserved, srcMaskLength, dstMaskLength, srcPrefix, dstPrefix);
Jian Li115d8602016-08-15 20:21:53 +0900258 }
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 Lia7b394d2016-08-21 23:11:46 +0900270 LispLcafAddress lcafAddress = LispLcafAddress.deserializeCommon(byteBuf);
271
Jian Li115d8602016-08-15 20:21:53 +0900272 short reserved = byteBuf.readShort();
273 byte srcMaskLength = (byte) byteBuf.readUnsignedByte();
274 byte dstMaskLength = (byte) byteBuf.readUnsignedByte();
275
Jian Lia7b394d2016-08-21 23:11:46 +0900276 LispAfiAddress srcPrefix = new LispAfiAddress.AfiAddressReader().readFrom(byteBuf);
277 LispAfiAddress dstPrefix = new LispAfiAddress.AfiAddressReader().readFrom(byteBuf);
Jian Li115d8602016-08-15 20:21:53 +0900278
279 return new SourceDestAddressBuilder()
Jian Lia7b394d2016-08-21 23:11:46 +0900280 .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 Li115d8602016-08-15 20:21:53 +0900290 }
291 }
GUNiba871702016-08-22 21:06:02 +0900292
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
303 LispLcafAddress.serializeCommon(byteBuf, address);
304
305 byteBuf.writeShort(address.getReserved());
306 byteBuf.writeByte(address.getSrcMaskLength());
307 byteBuf.writeByte(address.getDstMaskLength());
308 AfiAddressWriter writer = new LispAfiAddress.AfiAddressWriter();
309 writer.writeTo(byteBuf, address.getSrcPrefix());
310 writer.writeTo(byteBuf, address.getDstPrefix());
311 }
312 }
Jian Lic7e20a52016-07-18 19:03:49 +0900313}