blob: a13d9676551d37a7d592871db8031d1f24ee3ea5 [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 Lid6483cc2016-12-12 02:26:13 +090032 * Instance ID type is defined in draft-ietf-lisp-lcaf-22
33 * https://tools.ietf.org/html/draft-ietf-lisp-lcaf-22#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 Li3a99e712016-12-16 21:23:16 +090062 private LispSegmentLcafAddress(byte idMaskLength, int instanceId,
63 LispAfiAddress address) {
Jian Lic7e20a52016-07-18 19:03:49 +090064 super(LispCanonicalAddressFormatEnum.SEGMENT, idMaskLength);
65 this.address = address;
66 this.instanceId = instanceId;
67 }
68
69 /**
Jian Lia7b394d2016-08-21 23:11:46 +090070 * Initializes segment type LCAF address.
71 *
72 * @param reserved1 reserved1
73 * @param idMaskLength ID mask length
74 * @param flag flag
75 * @param length length
76 * @param instanceId instance id
77 * @param address address
78 */
Jian Li3a99e712016-12-16 21:23:16 +090079 private LispSegmentLcafAddress(byte reserved1, byte idMaskLength, byte flag,
80 short length, int instanceId,
81 LispAfiAddress address) {
82 super(LispCanonicalAddressFormatEnum.SEGMENT, reserved1,
83 idMaskLength, flag, length);
Jian Lia7b394d2016-08-21 23:11:46 +090084 this.address = address;
85 this.instanceId = instanceId;
86 }
87
88 /**
Jian Lic7e20a52016-07-18 19:03:49 +090089 * Obtains address.
90 *
91 * @return address
92 */
93 public LispAfiAddress getAddress() {
94 return address;
95 }
96
97 /**
98 * Obtains instance id.
99 *
100 * @return instance id
101 */
102 public int getInstanceId() {
103 return instanceId;
104 }
105
106 /**
107 * Obtains id mask length.
108 *
109 * @return id mask length
110 */
111 public byte getIdMaskLength() {
Jian Lia7b394d2016-08-21 23:11:46 +0900112 return getReserved2();
Jian Lic7e20a52016-07-18 19:03:49 +0900113 }
114
115 @Override
116 public int hashCode() {
Jian Lia7b394d2016-08-21 23:11:46 +0900117 return Objects.hash(address, instanceId, getReserved2());
Jian Lic7e20a52016-07-18 19:03:49 +0900118 }
119
120 @Override
121 public boolean equals(Object obj) {
122 if (this == obj) {
123 return true;
124 }
125
126 if (obj instanceof LispSegmentLcafAddress) {
127 final LispSegmentLcafAddress other = (LispSegmentLcafAddress) obj;
128 return Objects.equals(this.address, other.address) &&
Jian Lia7b394d2016-08-21 23:11:46 +0900129 Objects.equals(this.instanceId, other.instanceId) &&
130 Objects.equals(this.getReserved2(), other.getReserved2());
Jian Lic7e20a52016-07-18 19:03:49 +0900131 }
132 return false;
133 }
134
135 @Override
136 public String toString() {
137 return toStringHelper(this)
138 .add("address", address)
139 .add("instanceId", instanceId)
Jian Lia7b394d2016-08-21 23:11:46 +0900140 .add("idMaskLength", getReserved2())
Jian Lic7e20a52016-07-18 19:03:49 +0900141 .toString();
142 }
Jian Li115d8602016-08-15 20:21:53 +0900143
Jian Lia7b394d2016-08-21 23:11:46 +0900144 public static final class SegmentAddressBuilder
145 extends LcafAddressBuilder<SegmentAddressBuilder> {
Jian Li115d8602016-08-15 20:21:53 +0900146 private byte idMaskLength;
147 private LispAfiAddress address;
148 private int instanceId;
149
150 /**
151 * Sets identifier mask length.
152 *
153 * @param idMaskLength identifier mask length
154 * @return SegmentAddressBuilder object
155 */
156 public SegmentAddressBuilder withIdMaskLength(byte idMaskLength) {
157 this.idMaskLength = idMaskLength;
158 return this;
159 }
160
161 /**
162 * Sets instance identifer.
163 *
164 * @param instanceId instance identifier
165 * @return SegmentAddressBuilder object
166 */
167 public SegmentAddressBuilder withInstanceId(int instanceId) {
168 this.instanceId = instanceId;
169 return this;
170 }
171
172 /**
173 * Sets AFI address.
174 *
175 * @param address AFI address
176 * @return SegmentAddressBuilder object
177 */
178 public SegmentAddressBuilder withAddress(LispAfiAddress address) {
179 this.address = address;
180 return this;
181 }
182
183 /**
184 * Builds LispSegmentLcafAddress instance.
185 *
186 * @return LispSegmentLcafAddress instance
187 */
188 public LispSegmentLcafAddress build() {
Jian Lid4e63702016-08-30 18:29:20 +0900189
190 checkNotNull(address, "Must specify an address");
191
Jian Lia7b394d2016-08-21 23:11:46 +0900192 return new LispSegmentLcafAddress(reserved1, idMaskLength, flag,
193 length, instanceId, address);
Jian Li115d8602016-08-15 20:21:53 +0900194 }
195 }
196
197 /**
198 * Segment LCAF address reader class.
199 */
200 public static class SegmentLcafAddressReader
Jian Lia7b394d2016-08-21 23:11:46 +0900201 implements LispAddressReader<LispSegmentLcafAddress> {
Jian Li115d8602016-08-15 20:21:53 +0900202
203 @Override
204 public LispSegmentLcafAddress readFrom(ByteBuf byteBuf)
Jian Lia7b394d2016-08-21 23:11:46 +0900205 throws LispParseError, LispReaderException {
Jian Li115d8602016-08-15 20:21:53 +0900206
Jian Lia7b394d2016-08-21 23:11:46 +0900207 LispLcafAddress lcafAddress = LispLcafAddress.deserializeCommon(byteBuf);
208
209 byte idMaskLength = lcafAddress.getReserved2();
210
Jian Li115d8602016-08-15 20:21:53 +0900211 int instanceId = (int) byteBuf.readUnsignedInt();
Jian Li5e505c62016-12-05 02:44:24 +0900212 LispAfiAddress address = new AfiAddressReader().readFrom(byteBuf);
Jian Li115d8602016-08-15 20:21:53 +0900213
214 return new SegmentAddressBuilder()
Jian Lia7b394d2016-08-21 23:11:46 +0900215 .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}