blob: f6579edfe0ca8e34673278df56523b6bff291c8a [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 */
Jian Lif31019a2017-02-05 07:57:46 +090016package org.onosproject.lisp.msg.types.lcaf;
Jian Lic7e20a52016-07-18 19:03:49 +090017
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 Lif31019a2017-02-05 07:57:46 +090022import org.onosproject.lisp.msg.types.LispAddressReader;
23import org.onosproject.lisp.msg.types.LispAddressWriter;
24import org.onosproject.lisp.msg.types.LispAfiAddress;
Jian Li115d8602016-08-15 20:21:53 +090025
Jian Lic7e20a52016-07-18 19:03:49 +090026import java.util.Objects;
27
28import static com.google.common.base.MoreObjects.toStringHelper;
Jian Lid4e63702016-08-30 18:29:20 +090029import static com.google.common.base.Preconditions.checkNotNull;
Jian Lic7e20a52016-07-18 19:03:49 +090030
31/**
32 * Instance ID type LCAF address class.
Jian Li8fc2d2f2016-08-08 14:43:53 +090033 * <p>
Jian Lid6483cc2016-12-12 02:26:13 +090034 * Instance ID type is defined in draft-ietf-lisp-lcaf-22
Jian Li299bc1d2017-03-17 19:17:40 +090035 * https://tools.ietf.org/html/draft-ietf-lisp-lcaf-22#page-8
Jian Lic7e20a52016-07-18 19:03:49 +090036 *
Jian Li8fc2d2f2016-08-08 14:43:53 +090037 * <pre>
38 * {@literal
Jian Lic7e20a52016-07-18 19:03:49 +090039 * 0 1 2 3
40 * 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
41 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
42 * | AFI = 16387 | Rsvd1 | Flags |
43 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
Jian Li89c9ca92016-11-11 04:09:02 +090044 * | Type = 2 | IID mask-len | Length |
Jian Lic7e20a52016-07-18 19:03:49 +090045 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
46 * | Instance ID |
47 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
48 * | AFI = x | Address ... |
49 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
Jian Li8fc2d2f2016-08-08 14:43:53 +090050 * }</pre>
Jian Lic7e20a52016-07-18 19:03:49 +090051 */
Jian Li115d8602016-08-15 20:21:53 +090052public final class LispSegmentLcafAddress extends LispLcafAddress {
Jian Lic7e20a52016-07-18 19:03:49 +090053
54 private final LispAfiAddress address;
55 private final int instanceId;
56
57 /**
58 * Initializes segment type LCAF address.
59 *
60 * @param idMaskLength Id mask length
Jian Lia7b394d2016-08-21 23:11:46 +090061 * @param instanceId instance id
62 * @param address address
Jian Lic7e20a52016-07-18 19:03:49 +090063 */
Jian Li3a99e712016-12-16 21:23:16 +090064 private LispSegmentLcafAddress(byte idMaskLength, int instanceId,
65 LispAfiAddress address) {
Jian Lic7e20a52016-07-18 19:03:49 +090066 super(LispCanonicalAddressFormatEnum.SEGMENT, idMaskLength);
67 this.address = address;
68 this.instanceId = instanceId;
69 }
70
71 /**
Jian Lia7b394d2016-08-21 23:11:46 +090072 * Initializes segment type LCAF address.
73 *
74 * @param reserved1 reserved1
75 * @param idMaskLength ID mask length
76 * @param flag flag
77 * @param length length
78 * @param instanceId instance id
79 * @param address address
80 */
Jian Li3a99e712016-12-16 21:23:16 +090081 private LispSegmentLcafAddress(byte reserved1, byte idMaskLength, byte flag,
82 short length, int instanceId,
83 LispAfiAddress address) {
84 super(LispCanonicalAddressFormatEnum.SEGMENT, reserved1,
85 idMaskLength, flag, length);
Jian Lia7b394d2016-08-21 23:11:46 +090086 this.address = address;
87 this.instanceId = instanceId;
88 }
89
90 /**
Jian Lic7e20a52016-07-18 19:03:49 +090091 * Obtains address.
92 *
93 * @return address
94 */
95 public LispAfiAddress getAddress() {
96 return address;
97 }
98
99 /**
100 * Obtains instance id.
101 *
102 * @return instance id
103 */
104 public int getInstanceId() {
105 return instanceId;
106 }
107
108 /**
109 * Obtains id mask length.
110 *
111 * @return id mask length
112 */
113 public byte getIdMaskLength() {
Jian Lia7b394d2016-08-21 23:11:46 +0900114 return getReserved2();
Jian Lic7e20a52016-07-18 19:03:49 +0900115 }
116
117 @Override
118 public int hashCode() {
Jian Lia7b394d2016-08-21 23:11:46 +0900119 return Objects.hash(address, instanceId, getReserved2());
Jian Lic7e20a52016-07-18 19:03:49 +0900120 }
121
122 @Override
123 public boolean equals(Object obj) {
124 if (this == obj) {
125 return true;
126 }
127
128 if (obj instanceof LispSegmentLcafAddress) {
129 final LispSegmentLcafAddress other = (LispSegmentLcafAddress) obj;
130 return Objects.equals(this.address, other.address) &&
Jian Lia7b394d2016-08-21 23:11:46 +0900131 Objects.equals(this.instanceId, other.instanceId) &&
132 Objects.equals(this.getReserved2(), other.getReserved2());
Jian Lic7e20a52016-07-18 19:03:49 +0900133 }
134 return false;
135 }
136
137 @Override
138 public String toString() {
139 return toStringHelper(this)
140 .add("address", address)
141 .add("instanceId", instanceId)
Jian Lia7b394d2016-08-21 23:11:46 +0900142 .add("idMaskLength", getReserved2())
Jian Lic7e20a52016-07-18 19:03:49 +0900143 .toString();
144 }
Jian Li115d8602016-08-15 20:21:53 +0900145
Jian Lia7b394d2016-08-21 23:11:46 +0900146 public static final class SegmentAddressBuilder
147 extends LcafAddressBuilder<SegmentAddressBuilder> {
Jian Li115d8602016-08-15 20:21:53 +0900148 private byte idMaskLength;
149 private LispAfiAddress address;
150 private int instanceId;
151
152 /**
153 * Sets identifier mask length.
154 *
155 * @param idMaskLength identifier mask length
156 * @return SegmentAddressBuilder object
157 */
158 public SegmentAddressBuilder withIdMaskLength(byte idMaskLength) {
159 this.idMaskLength = idMaskLength;
160 return this;
161 }
162
163 /**
164 * Sets instance identifer.
165 *
166 * @param instanceId instance identifier
167 * @return SegmentAddressBuilder object
168 */
169 public SegmentAddressBuilder withInstanceId(int instanceId) {
170 this.instanceId = instanceId;
171 return this;
172 }
173
174 /**
175 * Sets AFI address.
176 *
177 * @param address AFI address
178 * @return SegmentAddressBuilder object
179 */
180 public SegmentAddressBuilder withAddress(LispAfiAddress address) {
181 this.address = address;
182 return this;
183 }
184
185 /**
186 * Builds LispSegmentLcafAddress instance.
187 *
188 * @return LispSegmentLcafAddress instance
189 */
190 public LispSegmentLcafAddress build() {
Jian Lid4e63702016-08-30 18:29:20 +0900191
192 checkNotNull(address, "Must specify an address");
193
Jian Lia7b394d2016-08-21 23:11:46 +0900194 return new LispSegmentLcafAddress(reserved1, idMaskLength, flag,
195 length, instanceId, address);
Jian Li115d8602016-08-15 20:21:53 +0900196 }
197 }
198
199 /**
200 * Segment LCAF address reader class.
201 */
202 public static class SegmentLcafAddressReader
Jian Lia7b394d2016-08-21 23:11:46 +0900203 implements LispAddressReader<LispSegmentLcafAddress> {
Jian Li115d8602016-08-15 20:21:53 +0900204
205 @Override
206 public LispSegmentLcafAddress readFrom(ByteBuf byteBuf)
Jian Lia7b394d2016-08-21 23:11:46 +0900207 throws LispParseError, LispReaderException {
Jian Li115d8602016-08-15 20:21:53 +0900208
Jian Lia7b394d2016-08-21 23:11:46 +0900209 LispLcafAddress lcafAddress = LispLcafAddress.deserializeCommon(byteBuf);
210
211 byte idMaskLength = lcafAddress.getReserved2();
212
Jian Li115d8602016-08-15 20:21:53 +0900213 int instanceId = (int) byteBuf.readUnsignedInt();
Jian Li5e505c62016-12-05 02:44:24 +0900214 LispAfiAddress address = new AfiAddressReader().readFrom(byteBuf);
Jian Li115d8602016-08-15 20:21:53 +0900215
216 return new SegmentAddressBuilder()
Jian Lia7b394d2016-08-21 23:11:46 +0900217 .withIdMaskLength(idMaskLength)
218 .withInstanceId(instanceId)
219 .withAddress(address)
220 .build();
Jian Li115d8602016-08-15 20:21:53 +0900221 }
222 }
GUNiba871702016-08-22 21:06:02 +0900223
224 /**
225 * Segment LCAF address writer class.
226 */
227 public static class SegmentLcafAddressWriter
228 implements LispAddressWriter<LispSegmentLcafAddress> {
229
230 @Override
231 public void writeTo(ByteBuf byteBuf, LispSegmentLcafAddress address)
232 throws LispWriterException {
233
Jian Lief0f7232016-11-15 19:55:46 +0900234 int lcafIndex = byteBuf.writerIndex();
GUNiba871702016-08-22 21:06:02 +0900235 LispLcafAddress.serializeCommon(byteBuf, address);
236
237 byteBuf.writeInt(address.getInstanceId());
Jian Li76ea0572016-08-29 12:41:16 +0900238
239 new LispAfiAddress.AfiAddressWriter().writeTo(byteBuf, address.getAddress());
Jian Lief0f7232016-11-15 19:55:46 +0900240
241 LispLcafAddress.updateLength(lcafIndex, byteBuf);
GUNiba871702016-08-22 21:06:02 +0900242 }
243 }
Jian Lic7e20a52016-07-18 19:03:49 +0900244}