blob: 24d159a02faeeb888187b27c508384a59076c688 [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 /**
79 * Obtains source address prefix.
80 *
81 * @return source address prefix
82 */
83 public LispAfiAddress getSrcPrefix() {
84 return srcPrefix;
85 }
86
87 /**
88 * Obtains destination address prefix.
89 *
90 * @return destination address prefix
91 */
92 public LispAfiAddress getDstPrefix() {
93 return dstPrefix;
94 }
95
96 /**
97 * Obtains source mask length.
98 *
99 * @return source mask length
100 */
101 public byte getSrcMaskLength() {
102 return srcMaskLength;
103 }
104
105 /**
106 * Obtains destination mask length.
107 *
108 * @return destination mask length
109 */
110 public byte getDstMaskLength() {
111 return dstMaskLength;
112 }
113
114 /**
115 * Obtains reserved value.
116 *
117 * @return reserved value
118 */
119 public short getReserved() {
120 return reserved;
121 }
122
123 @Override
124 public int hashCode() {
Jian Lid56f97e2016-07-19 15:48:15 +0900125 return Objects.hash(srcPrefix, dstPrefix, srcMaskLength, dstMaskLength, reserved);
Jian Lic7e20a52016-07-18 19:03:49 +0900126 }
127
128 @Override
129 public boolean equals(Object obj) {
130 if (this == obj) {
131 return true;
132 }
133
134 if (obj instanceof LispSourceDestLcafAddress) {
135 final LispSourceDestLcafAddress other = (LispSourceDestLcafAddress) obj;
136 return Objects.equals(this.srcPrefix, other.srcPrefix) &&
Jian Li115d8602016-08-15 20:21:53 +0900137 Objects.equals(this.dstPrefix, other.dstPrefix) &&
138 Objects.equals(this.srcMaskLength, other.srcMaskLength) &&
139 Objects.equals(this.dstMaskLength, other.dstMaskLength) &&
140 Objects.equals(this.reserved, other.reserved);
Jian Lic7e20a52016-07-18 19:03:49 +0900141 }
142 return false;
143 }
144
145 @Override
146 public String toString() {
147 return toStringHelper(this)
148 .add("source prefix", srcPrefix)
149 .add("destination prefix", dstPrefix)
150 .add("source mask length", srcMaskLength)
151 .add("destination mask length", dstMaskLength)
152 .add("reserved", reserved)
153 .toString();
154 }
Jian Li115d8602016-08-15 20:21:53 +0900155
156 public static final class SourceDestAddressBuilder {
157 private LispAfiAddress srcPrefix;
158 private LispAfiAddress dstPrefix;
159 private byte srcMaskLength;
160 private byte dstMaskLength;
161 private short reserved;
162
163 /**
164 * Sets source address prefix.
165 *
166 * @param srcPrefix source prefix
167 * @return SourceDestAddressBuilder object
168 */
169 public SourceDestAddressBuilder withSrcPrefix(LispAfiAddress srcPrefix) {
170 this.srcPrefix = srcPrefix;
171 return this;
172 }
173
174 /**
175 * Sets destination address prefix.
176 *
177 * @param dstPrefix
178 * @return SourceDestAddressBuilder object
179 */
180 public SourceDestAddressBuilder withDstPrefix(LispAfiAddress dstPrefix) {
181 this.dstPrefix = dstPrefix;
182 return this;
183 }
184
185 /**
186 * Sets source mask length.
187 *
188 * @param srcMaskLength source mask length
189 * @return SourceDestAddressBuilder object
190 */
191 public SourceDestAddressBuilder withSrcMaskLength(byte srcMaskLength) {
192 this.srcMaskLength = srcMaskLength;
193 return this;
194 }
195
196 /**
197 * Sets destination mask length.
198 *
199 * @param dstMaskLength destination mask length
200 * @return SourceDestAddressBuilder object
201 */
202 public SourceDestAddressBuilder withDstMaskLength(byte dstMaskLength) {
203 this.dstMaskLength = dstMaskLength;
204 return this;
205 }
206
207 /**
208 * Sets reserved value.
209 *
210 * @param reserved reserved field value
211 * @return SourceDestAddressBuilder object
212 */
213 public SourceDestAddressBuilder withReserved(short reserved) {
214 this.reserved = reserved;
215 return this;
216 }
217
218 /**
219 * Builds LispSourceDestLcafAddress instance.
220 *
221 * @return LispSourceDestLcafAddress instance
222 */
223 public LispSourceDestLcafAddress build() {
224 return new LispSourceDestLcafAddress(reserved, srcMaskLength,
225 dstMaskLength, srcPrefix, dstPrefix);
226 }
227 }
228
229 /**
230 * SourceDest LCAF address reader class.
231 */
232 public static class SourceDestLcafAddressReader
233 implements LispAddressReader<LispSourceDestLcafAddress> {
234
235 @Override
236 public LispSourceDestLcafAddress readFrom(ByteBuf byteBuf) throws LispParseError, LispReaderException {
237
238 short reserved = byteBuf.readShort();
239 byte srcMaskLength = (byte) byteBuf.readUnsignedByte();
240 byte dstMaskLength = (byte) byteBuf.readUnsignedByte();
241
242 LispAfiAddress srcPrefix = new LispIpAddress.IpAddressReader().readFrom(byteBuf);
243 LispAfiAddress dstPrefix = new LispIpAddress.IpAddressReader().readFrom(byteBuf);
244
245 return new SourceDestAddressBuilder()
246 .withReserved(reserved)
247 .withSrcMaskLength(srcMaskLength)
248 .withDstMaskLength(dstMaskLength)
249 .withSrcPrefix(srcPrefix)
250 .withDstPrefix(dstPrefix)
251 .build();
252 }
253 }
Jian Lic7e20a52016-07-18 19:03:49 +0900254}