blob: b38319fb102d35a98879eb03e7ad55eb147c4c3e [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 Li5e505c62016-12-05 02:44:24 +090022import org.onosproject.lisp.msg.types.LispAfiAddress.AfiAddressReader;
Jian Li115d8602016-08-15 20:21:53 +090023
Jian Lic7e20a52016-07-18 19:03:49 +090024import java.util.Objects;
25
26import static com.google.common.base.MoreObjects.toStringHelper;
Jian Lid4e63702016-08-30 18:29:20 +090027import static com.google.common.base.Preconditions.checkNotNull;
Jian Lic7e20a52016-07-18 19:03:49 +090028
29/**
30 * Instance ID type LCAF address class.
Jian Li8fc2d2f2016-08-08 14:43:53 +090031 * <p>
Jian Li89c9ca92016-11-11 04:09:02 +090032 * Instance ID type is defined in draft-ietf-lisp-lcaf-20
33 * https://tools.ietf.org/html/draft-ietf-lisp-lcaf-20#page-7
Jian Lic7e20a52016-07-18 19:03:49 +090034 *
Jian Li8fc2d2f2016-08-08 14:43:53 +090035 * <pre>
36 * {@literal
Jian Lic7e20a52016-07-18 19:03:49 +090037 * 0 1 2 3
38 * 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
39 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
40 * | AFI = 16387 | Rsvd1 | Flags |
41 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
Jian Li89c9ca92016-11-11 04:09:02 +090042 * | Type = 2 | IID mask-len | Length |
Jian Lic7e20a52016-07-18 19:03:49 +090043 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
44 * | Instance ID |
45 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
46 * | AFI = x | Address ... |
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 LispSegmentLcafAddress extends LispLcafAddress {
Jian Lic7e20a52016-07-18 19:03:49 +090051
52 private final LispAfiAddress address;
53 private final int instanceId;
54
55 /**
56 * Initializes segment type LCAF address.
57 *
58 * @param idMaskLength Id mask length
Jian Lia7b394d2016-08-21 23:11:46 +090059 * @param instanceId instance id
60 * @param address address
Jian Lic7e20a52016-07-18 19:03:49 +090061 */
Jian Li115d8602016-08-15 20:21:53 +090062 private LispSegmentLcafAddress(byte idMaskLength, int instanceId, LispAfiAddress address) {
Jian Lic7e20a52016-07-18 19:03:49 +090063 super(LispCanonicalAddressFormatEnum.SEGMENT, idMaskLength);
64 this.address = address;
65 this.instanceId = instanceId;
66 }
67
68 /**
Jian Lia7b394d2016-08-21 23:11:46 +090069 * Initializes segment type LCAF address.
70 *
71 * @param reserved1 reserved1
72 * @param idMaskLength ID mask length
73 * @param flag flag
74 * @param length length
75 * @param instanceId instance id
76 * @param address address
77 */
78 private LispSegmentLcafAddress(byte reserved1, byte idMaskLength, byte flag, short length,
79 int instanceId, LispAfiAddress address) {
80 super(LispCanonicalAddressFormatEnum.SEGMENT, reserved1, idMaskLength, flag, length);
81 this.address = address;
82 this.instanceId = instanceId;
83 }
84
85 /**
Jian Lic7e20a52016-07-18 19:03:49 +090086 * Obtains address.
87 *
88 * @return address
89 */
90 public LispAfiAddress getAddress() {
91 return address;
92 }
93
94 /**
95 * Obtains instance id.
96 *
97 * @return instance id
98 */
99 public int getInstanceId() {
100 return instanceId;
101 }
102
103 /**
104 * Obtains id mask length.
105 *
106 * @return id mask length
107 */
108 public byte getIdMaskLength() {
Jian Lia7b394d2016-08-21 23:11:46 +0900109 return getReserved2();
Jian Lic7e20a52016-07-18 19:03:49 +0900110 }
111
112 @Override
113 public int hashCode() {
Jian Lia7b394d2016-08-21 23:11:46 +0900114 return Objects.hash(address, instanceId, getReserved2());
Jian Lic7e20a52016-07-18 19:03:49 +0900115 }
116
117 @Override
118 public boolean equals(Object obj) {
119 if (this == obj) {
120 return true;
121 }
122
123 if (obj instanceof LispSegmentLcafAddress) {
124 final LispSegmentLcafAddress other = (LispSegmentLcafAddress) obj;
125 return Objects.equals(this.address, other.address) &&
Jian Lia7b394d2016-08-21 23:11:46 +0900126 Objects.equals(this.instanceId, other.instanceId) &&
127 Objects.equals(this.getReserved2(), other.getReserved2());
Jian Lic7e20a52016-07-18 19:03:49 +0900128 }
129 return false;
130 }
131
132 @Override
133 public String toString() {
134 return toStringHelper(this)
135 .add("address", address)
136 .add("instanceId", instanceId)
Jian Lia7b394d2016-08-21 23:11:46 +0900137 .add("idMaskLength", getReserved2())
Jian Lic7e20a52016-07-18 19:03:49 +0900138 .toString();
139 }
Jian Li115d8602016-08-15 20:21:53 +0900140
Jian Lia7b394d2016-08-21 23:11:46 +0900141 public static final class SegmentAddressBuilder
142 extends LcafAddressBuilder<SegmentAddressBuilder> {
Jian Li115d8602016-08-15 20:21:53 +0900143 private byte idMaskLength;
144 private LispAfiAddress address;
145 private int instanceId;
146
147 /**
148 * Sets identifier mask length.
149 *
150 * @param idMaskLength identifier mask length
151 * @return SegmentAddressBuilder object
152 */
153 public SegmentAddressBuilder withIdMaskLength(byte idMaskLength) {
154 this.idMaskLength = idMaskLength;
155 return this;
156 }
157
158 /**
159 * Sets instance identifer.
160 *
161 * @param instanceId instance identifier
162 * @return SegmentAddressBuilder object
163 */
164 public SegmentAddressBuilder withInstanceId(int instanceId) {
165 this.instanceId = instanceId;
166 return this;
167 }
168
169 /**
170 * Sets AFI address.
171 *
172 * @param address AFI address
173 * @return SegmentAddressBuilder object
174 */
175 public SegmentAddressBuilder withAddress(LispAfiAddress address) {
176 this.address = address;
177 return this;
178 }
179
180 /**
181 * Builds LispSegmentLcafAddress instance.
182 *
183 * @return LispSegmentLcafAddress instance
184 */
185 public LispSegmentLcafAddress build() {
Jian Lid4e63702016-08-30 18:29:20 +0900186
187 checkNotNull(address, "Must specify an address");
188
Jian Lia7b394d2016-08-21 23:11:46 +0900189 return new LispSegmentLcafAddress(reserved1, idMaskLength, flag,
190 length, instanceId, address);
Jian Li115d8602016-08-15 20:21:53 +0900191 }
192 }
193
194 /**
195 * Segment LCAF address reader class.
196 */
197 public static class SegmentLcafAddressReader
Jian Lia7b394d2016-08-21 23:11:46 +0900198 implements LispAddressReader<LispSegmentLcafAddress> {
Jian Li115d8602016-08-15 20:21:53 +0900199
200 @Override
201 public LispSegmentLcafAddress readFrom(ByteBuf byteBuf)
Jian Lia7b394d2016-08-21 23:11:46 +0900202 throws LispParseError, LispReaderException {
Jian Li115d8602016-08-15 20:21:53 +0900203
Jian Lia7b394d2016-08-21 23:11:46 +0900204 LispLcafAddress lcafAddress = LispLcafAddress.deserializeCommon(byteBuf);
205
206 byte idMaskLength = lcafAddress.getReserved2();
207
Jian Li115d8602016-08-15 20:21:53 +0900208 int instanceId = (int) byteBuf.readUnsignedInt();
Jian Li5e505c62016-12-05 02:44:24 +0900209 LispAfiAddress address = new AfiAddressReader().readFrom(byteBuf);
Jian Li115d8602016-08-15 20:21:53 +0900210
211 return new SegmentAddressBuilder()
Jian Lia7b394d2016-08-21 23:11:46 +0900212 .withReserved1(lcafAddress.getReserved1())
213 .withFlag(lcafAddress.getFlag())
214 .withLength(lcafAddress.getLength())
215 .withIdMaskLength(idMaskLength)
216 .withInstanceId(instanceId)
217 .withAddress(address)
218 .build();
Jian Li115d8602016-08-15 20:21:53 +0900219 }
220 }
GUNiba871702016-08-22 21:06:02 +0900221
222 /**
223 * Segment LCAF address writer class.
224 */
225 public static class SegmentLcafAddressWriter
226 implements LispAddressWriter<LispSegmentLcafAddress> {
227
228 @Override
229 public void writeTo(ByteBuf byteBuf, LispSegmentLcafAddress address)
230 throws LispWriterException {
231
Jian Lief0f7232016-11-15 19:55:46 +0900232 int lcafIndex = byteBuf.writerIndex();
GUNiba871702016-08-22 21:06:02 +0900233 LispLcafAddress.serializeCommon(byteBuf, address);
234
235 byteBuf.writeInt(address.getInstanceId());
Jian Li76ea0572016-08-29 12:41:16 +0900236
237 new LispAfiAddress.AfiAddressWriter().writeTo(byteBuf, address.getAddress());
Jian Lief0f7232016-11-15 19:55:46 +0900238
239 LispLcafAddress.updateLength(lcafIndex, byteBuf);
GUNiba871702016-08-22 21:06:02 +0900240 }
241 }
Jian Lic7e20a52016-07-18 19:03:49 +0900242}