blob: 50e4128d9e6fa922adbc5a2c13fd0ef398ce883b [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 * Instance ID type LCAF address class.
Jian Li8fc2d2f2016-08-08 14:43:53 +090030 * <p>
Jian Li89c9ca92016-11-11 04:09:02 +090031 * Instance ID type is defined in draft-ietf-lisp-lcaf-20
32 * https://tools.ietf.org/html/draft-ietf-lisp-lcaf-20#page-7
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 = 2 | IID mask-len | Length |
Jian Lic7e20a52016-07-18 19:03:49 +090042 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
43 * | Instance ID |
44 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
45 * | AFI = x | Address ... |
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 LispSegmentLcafAddress extends LispLcafAddress {
Jian Lic7e20a52016-07-18 19:03:49 +090050
51 private final LispAfiAddress address;
52 private final int instanceId;
53
54 /**
55 * Initializes segment type LCAF address.
56 *
57 * @param idMaskLength Id mask length
Jian Lia7b394d2016-08-21 23:11:46 +090058 * @param instanceId instance id
59 * @param address address
Jian Lic7e20a52016-07-18 19:03:49 +090060 */
Jian Li115d8602016-08-15 20:21:53 +090061 private LispSegmentLcafAddress(byte idMaskLength, int instanceId, LispAfiAddress address) {
Jian Lic7e20a52016-07-18 19:03:49 +090062 super(LispCanonicalAddressFormatEnum.SEGMENT, idMaskLength);
63 this.address = address;
64 this.instanceId = instanceId;
65 }
66
67 /**
Jian Lia7b394d2016-08-21 23:11:46 +090068 * Initializes segment type LCAF address.
69 *
70 * @param reserved1 reserved1
71 * @param idMaskLength ID mask length
72 * @param flag flag
73 * @param length length
74 * @param instanceId instance id
75 * @param address address
76 */
77 private LispSegmentLcafAddress(byte reserved1, byte idMaskLength, byte flag, short length,
78 int instanceId, LispAfiAddress address) {
79 super(LispCanonicalAddressFormatEnum.SEGMENT, reserved1, idMaskLength, flag, length);
80 this.address = address;
81 this.instanceId = instanceId;
82 }
83
84 /**
Jian Lic7e20a52016-07-18 19:03:49 +090085 * Obtains address.
86 *
87 * @return address
88 */
89 public LispAfiAddress getAddress() {
90 return address;
91 }
92
93 /**
94 * Obtains instance id.
95 *
96 * @return instance id
97 */
98 public int getInstanceId() {
99 return instanceId;
100 }
101
102 /**
103 * Obtains id mask length.
104 *
105 * @return id mask length
106 */
107 public byte getIdMaskLength() {
Jian Lia7b394d2016-08-21 23:11:46 +0900108 return getReserved2();
Jian Lic7e20a52016-07-18 19:03:49 +0900109 }
110
111 @Override
112 public int hashCode() {
Jian Lia7b394d2016-08-21 23:11:46 +0900113 return Objects.hash(address, instanceId, getReserved2());
Jian Lic7e20a52016-07-18 19:03:49 +0900114 }
115
116 @Override
117 public boolean equals(Object obj) {
118 if (this == obj) {
119 return true;
120 }
121
122 if (obj instanceof LispSegmentLcafAddress) {
123 final LispSegmentLcafAddress other = (LispSegmentLcafAddress) obj;
124 return Objects.equals(this.address, other.address) &&
Jian Lia7b394d2016-08-21 23:11:46 +0900125 Objects.equals(this.instanceId, other.instanceId) &&
126 Objects.equals(this.getReserved2(), other.getReserved2());
Jian Lic7e20a52016-07-18 19:03:49 +0900127 }
128 return false;
129 }
130
131 @Override
132 public String toString() {
133 return toStringHelper(this)
134 .add("address", address)
135 .add("instanceId", instanceId)
Jian Lia7b394d2016-08-21 23:11:46 +0900136 .add("idMaskLength", getReserved2())
Jian Lic7e20a52016-07-18 19:03:49 +0900137 .toString();
138 }
Jian Li115d8602016-08-15 20:21:53 +0900139
Jian Lia7b394d2016-08-21 23:11:46 +0900140 public static final class SegmentAddressBuilder
141 extends LcafAddressBuilder<SegmentAddressBuilder> {
Jian Li115d8602016-08-15 20:21:53 +0900142 private byte idMaskLength;
143 private LispAfiAddress address;
144 private int instanceId;
145
146 /**
147 * Sets identifier mask length.
148 *
149 * @param idMaskLength identifier mask length
150 * @return SegmentAddressBuilder object
151 */
152 public SegmentAddressBuilder withIdMaskLength(byte idMaskLength) {
153 this.idMaskLength = idMaskLength;
154 return this;
155 }
156
157 /**
158 * Sets instance identifer.
159 *
160 * @param instanceId instance identifier
161 * @return SegmentAddressBuilder object
162 */
163 public SegmentAddressBuilder withInstanceId(int instanceId) {
164 this.instanceId = instanceId;
165 return this;
166 }
167
168 /**
169 * Sets AFI address.
170 *
171 * @param address AFI address
172 * @return SegmentAddressBuilder object
173 */
174 public SegmentAddressBuilder withAddress(LispAfiAddress address) {
175 this.address = address;
176 return this;
177 }
178
179 /**
180 * Builds LispSegmentLcafAddress instance.
181 *
182 * @return LispSegmentLcafAddress instance
183 */
184 public LispSegmentLcafAddress build() {
Jian Lid4e63702016-08-30 18:29:20 +0900185
186 checkNotNull(address, "Must specify an address");
187
Jian Lia7b394d2016-08-21 23:11:46 +0900188 return new LispSegmentLcafAddress(reserved1, idMaskLength, flag,
189 length, instanceId, address);
Jian Li115d8602016-08-15 20:21:53 +0900190 }
191 }
192
193 /**
194 * Segment LCAF address reader class.
195 */
196 public static class SegmentLcafAddressReader
Jian Lia7b394d2016-08-21 23:11:46 +0900197 implements LispAddressReader<LispSegmentLcafAddress> {
Jian Li115d8602016-08-15 20:21:53 +0900198
199 @Override
200 public LispSegmentLcafAddress readFrom(ByteBuf byteBuf)
Jian Lia7b394d2016-08-21 23:11:46 +0900201 throws LispParseError, LispReaderException {
Jian Li115d8602016-08-15 20:21:53 +0900202
Jian Lia7b394d2016-08-21 23:11:46 +0900203 LispLcafAddress lcafAddress = LispLcafAddress.deserializeCommon(byteBuf);
204
205 byte idMaskLength = lcafAddress.getReserved2();
206
Jian Li115d8602016-08-15 20:21:53 +0900207 int instanceId = (int) byteBuf.readUnsignedInt();
Jian Lia7b394d2016-08-21 23:11:46 +0900208 LispAfiAddress address = new LispAfiAddress.AfiAddressReader().readFrom(byteBuf);
Jian Li115d8602016-08-15 20:21:53 +0900209
210 return new SegmentAddressBuilder()
Jian Lia7b394d2016-08-21 23:11:46 +0900211 .withReserved1(lcafAddress.getReserved1())
212 .withFlag(lcafAddress.getFlag())
213 .withLength(lcafAddress.getLength())
214 .withIdMaskLength(idMaskLength)
215 .withInstanceId(instanceId)
216 .withAddress(address)
217 .build();
Jian Li115d8602016-08-15 20:21:53 +0900218 }
219 }
GUNiba871702016-08-22 21:06:02 +0900220
221 /**
222 * Segment LCAF address writer class.
223 */
224 public static class SegmentLcafAddressWriter
225 implements LispAddressWriter<LispSegmentLcafAddress> {
226
227 @Override
228 public void writeTo(ByteBuf byteBuf, LispSegmentLcafAddress address)
229 throws LispWriterException {
230
231 LispLcafAddress.serializeCommon(byteBuf, address);
232
233 byteBuf.writeInt(address.getInstanceId());
Jian Li76ea0572016-08-29 12:41:16 +0900234
235 new LispAfiAddress.AfiAddressWriter().writeTo(byteBuf, address.getAddress());
GUNiba871702016-08-22 21:06:02 +0900236 }
237 }
Jian Lic7e20a52016-07-18 19:03:49 +0900238}