blob: 9da4a181f63859fbde48466691e5e720249a7eb4 [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;
21
Jian Lic7e20a52016-07-18 19:03:49 +090022import java.util.Objects;
23
24import static com.google.common.base.MoreObjects.toStringHelper;
25
26/**
27 * Source/Dest key type LCAF address class.
Jian Li8fc2d2f2016-08-08 14:43:53 +090028 * <p>
Jian Lic7e20a52016-07-18 19:03:49 +090029 * Source destination key type is defined in draft-ietf-lisp-lcaf-13
30 * https://tools.ietf.org/html/draft-ietf-lisp-lcaf-13#page-18
31 *
Jian Li8fc2d2f2016-08-08 14:43:53 +090032 * <pre>
33 * {@literal
Jian Lic7e20a52016-07-18 19:03:49 +090034 * 0 1 2 3
35 * 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
36 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
37 * | AFI = 16387 | Rsvd1 | Flags |
38 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
39 * | Type = 12 | Rsvd2 | 4 + n |
40 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
41 * | Reserved | Source-ML | Dest-ML |
42 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
43 * | AFI = x | Source-Prefix ... |
44 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
45 * | AFI = x | Destination-Prefix ... |
46 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
Jian Li8fc2d2f2016-08-08 14:43:53 +090047 * }</pre>
Jian Lic7e20a52016-07-18 19:03:49 +090048 */
Jian Li115d8602016-08-15 20:21:53 +090049public final class LispSourceDestLcafAddress extends LispLcafAddress {
Jian Lic7e20a52016-07-18 19:03:49 +090050
Jian Li115d8602016-08-15 20:21:53 +090051 private final LispAfiAddress srcPrefix;
52 private final LispAfiAddress dstPrefix;
Jian Lic7e20a52016-07-18 19:03:49 +090053 private final byte srcMaskLength;
54 private final byte dstMaskLength;
55 private final short reserved;
56
57 /**
58 * Initializes source/dest key type LCAF address.
Jian Lic7e20a52016-07-18 19:03:49 +090059 *
Jian Li115d8602016-08-15 20:21:53 +090060 * @param reserved reserved
Jian Lic7e20a52016-07-18 19:03:49 +090061 * @param srcMaskLength source mask length
62 * @param dstMaskLength destination mask length
Jian Li115d8602016-08-15 20:21:53 +090063 * @param srcPrefix source address prefix
64 * @param dstPrefix destination address prefix
Jian Lic7e20a52016-07-18 19:03:49 +090065 */
Jian Li115d8602016-08-15 20:21:53 +090066 private LispSourceDestLcafAddress(short reserved, byte srcMaskLength,
67 byte dstMaskLength,
68 LispAfiAddress srcPrefix,
69 LispAfiAddress dstPrefix) {
Jian Lic7e20a52016-07-18 19:03:49 +090070 super(LispCanonicalAddressFormatEnum.SOURCE_DEST);
71 this.reserved = reserved;
72 this.srcMaskLength = srcMaskLength;
73 this.dstMaskLength = dstMaskLength;
74 this.srcPrefix = srcPrefix;
75 this.dstPrefix = dstPrefix;
76 }
77
78 /**
Jian Lia7b394d2016-08-21 23:11:46 +090079 * Initializes source/dest key type LCAF address.
80 *
81 * @param reserved1 reserved1
82 * @param reserved2 reserved2
83 * @param flag flag
84 * @param length length
85 * @param reserved reserved
86 * @param srcMaskLength source mask length
87 * @param dstMaskLength destination mask length
88 * @param srcPrefix source address prefix
89 * @param dstPrefix destination address prefix
90 */
91 private LispSourceDestLcafAddress(byte reserved1, byte reserved2, byte flag, short length,
92 short reserved, byte srcMaskLength,
93 byte dstMaskLength, LispAfiAddress srcPrefix,
94 LispAfiAddress dstPrefix) {
95 super(LispCanonicalAddressFormatEnum.SOURCE_DEST, reserved1, reserved2, flag, length);
96 this.reserved = reserved;
97 this.srcMaskLength = srcMaskLength;
98 this.dstMaskLength = dstMaskLength;
99 this.srcPrefix = srcPrefix;
100 this.dstPrefix = dstPrefix;
101 }
102
103 /**
Jian Lic7e20a52016-07-18 19:03:49 +0900104 * Obtains source address prefix.
105 *
106 * @return source address prefix
107 */
108 public LispAfiAddress getSrcPrefix() {
109 return srcPrefix;
110 }
111
112 /**
113 * Obtains destination address prefix.
114 *
115 * @return destination address prefix
116 */
117 public LispAfiAddress getDstPrefix() {
118 return dstPrefix;
119 }
120
121 /**
122 * Obtains source mask length.
123 *
124 * @return source mask length
125 */
126 public byte getSrcMaskLength() {
127 return srcMaskLength;
128 }
129
130 /**
131 * Obtains destination mask length.
132 *
133 * @return destination mask length
134 */
135 public byte getDstMaskLength() {
136 return dstMaskLength;
137 }
138
139 /**
140 * Obtains reserved value.
141 *
142 * @return reserved value
143 */
144 public short getReserved() {
145 return reserved;
146 }
147
148 @Override
149 public int hashCode() {
Jian Lid56f97e2016-07-19 15:48:15 +0900150 return Objects.hash(srcPrefix, dstPrefix, srcMaskLength, dstMaskLength, reserved);
Jian Lic7e20a52016-07-18 19:03:49 +0900151 }
152
153 @Override
154 public boolean equals(Object obj) {
155 if (this == obj) {
156 return true;
157 }
158
159 if (obj instanceof LispSourceDestLcafAddress) {
160 final LispSourceDestLcafAddress other = (LispSourceDestLcafAddress) obj;
161 return Objects.equals(this.srcPrefix, other.srcPrefix) &&
Jian Li115d8602016-08-15 20:21:53 +0900162 Objects.equals(this.dstPrefix, other.dstPrefix) &&
163 Objects.equals(this.srcMaskLength, other.srcMaskLength) &&
164 Objects.equals(this.dstMaskLength, other.dstMaskLength) &&
165 Objects.equals(this.reserved, other.reserved);
Jian Lic7e20a52016-07-18 19:03:49 +0900166 }
167 return false;
168 }
169
170 @Override
171 public String toString() {
172 return toStringHelper(this)
173 .add("source prefix", srcPrefix)
174 .add("destination prefix", dstPrefix)
175 .add("source mask length", srcMaskLength)
176 .add("destination mask length", dstMaskLength)
177 .add("reserved", reserved)
178 .toString();
179 }
Jian Li115d8602016-08-15 20:21:53 +0900180
Jian Lia7b394d2016-08-21 23:11:46 +0900181 public static final class SourceDestAddressBuilder
182 extends LcafAddressBuilder<SourceDestAddressBuilder> {
Jian Li115d8602016-08-15 20:21:53 +0900183 private LispAfiAddress srcPrefix;
184 private LispAfiAddress dstPrefix;
185 private byte srcMaskLength;
186 private byte dstMaskLength;
187 private short reserved;
188
189 /**
190 * Sets source address prefix.
191 *
192 * @param srcPrefix source prefix
193 * @return SourceDestAddressBuilder object
194 */
195 public SourceDestAddressBuilder withSrcPrefix(LispAfiAddress srcPrefix) {
196 this.srcPrefix = srcPrefix;
197 return this;
198 }
199
200 /**
201 * Sets destination address prefix.
202 *
203 * @param dstPrefix
204 * @return SourceDestAddressBuilder object
205 */
206 public SourceDestAddressBuilder withDstPrefix(LispAfiAddress dstPrefix) {
207 this.dstPrefix = dstPrefix;
208 return this;
209 }
210
211 /**
212 * Sets source mask length.
213 *
214 * @param srcMaskLength source mask length
215 * @return SourceDestAddressBuilder object
216 */
217 public SourceDestAddressBuilder withSrcMaskLength(byte srcMaskLength) {
218 this.srcMaskLength = srcMaskLength;
219 return this;
220 }
221
222 /**
223 * Sets destination mask length.
224 *
225 * @param dstMaskLength destination mask length
226 * @return SourceDestAddressBuilder object
227 */
228 public SourceDestAddressBuilder withDstMaskLength(byte dstMaskLength) {
229 this.dstMaskLength = dstMaskLength;
230 return this;
231 }
232
233 /**
234 * Sets reserved value.
235 *
236 * @param reserved reserved field value
237 * @return SourceDestAddressBuilder object
238 */
239 public SourceDestAddressBuilder withReserved(short reserved) {
240 this.reserved = reserved;
241 return this;
242 }
243
244 /**
245 * Builds LispSourceDestLcafAddress instance.
246 *
247 * @return LispSourceDestLcafAddress instance
248 */
249 public LispSourceDestLcafAddress build() {
Jian Lia7b394d2016-08-21 23:11:46 +0900250 return new LispSourceDestLcafAddress(reserved1, reserved2, flag, length,
251 reserved, srcMaskLength, dstMaskLength, srcPrefix, dstPrefix);
Jian Li115d8602016-08-15 20:21:53 +0900252 }
253 }
254
255 /**
256 * SourceDest LCAF address reader class.
257 */
258 public static class SourceDestLcafAddressReader
259 implements LispAddressReader<LispSourceDestLcafAddress> {
260
261 @Override
262 public LispSourceDestLcafAddress readFrom(ByteBuf byteBuf) throws LispParseError, LispReaderException {
263
Jian Lia7b394d2016-08-21 23:11:46 +0900264 LispLcafAddress lcafAddress = LispLcafAddress.deserializeCommon(byteBuf);
265
Jian Li115d8602016-08-15 20:21:53 +0900266 short reserved = byteBuf.readShort();
267 byte srcMaskLength = (byte) byteBuf.readUnsignedByte();
268 byte dstMaskLength = (byte) byteBuf.readUnsignedByte();
269
Jian Lia7b394d2016-08-21 23:11:46 +0900270 LispAfiAddress srcPrefix = new LispAfiAddress.AfiAddressReader().readFrom(byteBuf);
271 LispAfiAddress dstPrefix = new LispAfiAddress.AfiAddressReader().readFrom(byteBuf);
Jian Li115d8602016-08-15 20:21:53 +0900272
273 return new SourceDestAddressBuilder()
Jian Lia7b394d2016-08-21 23:11:46 +0900274 .withReserved1(lcafAddress.getReserved1())
275 .withReserved2(lcafAddress.getReserved2())
276 .withFlag(lcafAddress.getFlag())
277 .withLength(lcafAddress.getLength())
278 .withReserved(reserved)
279 .withSrcMaskLength(srcMaskLength)
280 .withDstMaskLength(dstMaskLength)
281 .withSrcPrefix(srcPrefix)
282 .withDstPrefix(dstPrefix)
283 .build();
Jian Li115d8602016-08-15 20:21:53 +0900284 }
285 }
Jian Lic7e20a52016-07-18 19:03:49 +0900286}