blob: c6a84cc193075a5d8b90ae61ec9a7cdbd35fdbbd [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;
26
27/**
28 * Instance ID type LCAF address class.
Jian Li8fc2d2f2016-08-08 14:43:53 +090029 * <p>
Jian Lic7e20a52016-07-18 19:03:49 +090030 * Instance ID type is defined in draft-ietf-lisp-lcaf-13
31 * https://tools.ietf.org/html/draft-ietf-lisp-lcaf-13#page-7
32 *
Jian Li8fc2d2f2016-08-08 14:43:53 +090033 * <pre>
34 * {@literal
Jian Lic7e20a52016-07-18 19:03:49 +090035 * 0 1 2 3
36 * 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
37 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
38 * | AFI = 16387 | Rsvd1 | Flags |
39 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
40 * | Type = 2 | IID mask-len | 4 + n |
41 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
42 * | Instance ID |
43 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
44 * | AFI = x | Address ... |
45 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
Jian Li8fc2d2f2016-08-08 14:43:53 +090046 * }</pre>
Jian Lic7e20a52016-07-18 19:03:49 +090047 */
Jian Li115d8602016-08-15 20:21:53 +090048public final class LispSegmentLcafAddress extends LispLcafAddress {
Jian Lic7e20a52016-07-18 19:03:49 +090049
50 private final LispAfiAddress address;
51 private final int instanceId;
52
53 /**
54 * Initializes segment type LCAF address.
55 *
56 * @param idMaskLength Id mask length
Jian Lia7b394d2016-08-21 23:11:46 +090057 * @param instanceId instance id
58 * @param address address
Jian Lic7e20a52016-07-18 19:03:49 +090059 */
Jian Li115d8602016-08-15 20:21:53 +090060 private LispSegmentLcafAddress(byte idMaskLength, int instanceId, LispAfiAddress address) {
Jian Lic7e20a52016-07-18 19:03:49 +090061 super(LispCanonicalAddressFormatEnum.SEGMENT, idMaskLength);
62 this.address = address;
63 this.instanceId = instanceId;
64 }
65
66 /**
Jian Lia7b394d2016-08-21 23:11:46 +090067 * Initializes segment type LCAF address.
68 *
69 * @param reserved1 reserved1
70 * @param idMaskLength ID mask length
71 * @param flag flag
72 * @param length length
73 * @param instanceId instance id
74 * @param address address
75 */
76 private LispSegmentLcafAddress(byte reserved1, byte idMaskLength, byte flag, short length,
77 int instanceId, LispAfiAddress address) {
78 super(LispCanonicalAddressFormatEnum.SEGMENT, reserved1, idMaskLength, flag, length);
79 this.address = address;
80 this.instanceId = instanceId;
81 }
82
83 /**
Jian Lic7e20a52016-07-18 19:03:49 +090084 * Obtains address.
85 *
86 * @return address
87 */
88 public LispAfiAddress getAddress() {
89 return address;
90 }
91
92 /**
93 * Obtains instance id.
94 *
95 * @return instance id
96 */
97 public int getInstanceId() {
98 return instanceId;
99 }
100
101 /**
102 * Obtains id mask length.
103 *
104 * @return id mask length
105 */
106 public byte getIdMaskLength() {
Jian Lia7b394d2016-08-21 23:11:46 +0900107 return getReserved2();
Jian Lic7e20a52016-07-18 19:03:49 +0900108 }
109
110 @Override
111 public int hashCode() {
Jian Lia7b394d2016-08-21 23:11:46 +0900112 return Objects.hash(address, instanceId, getReserved2());
Jian Lic7e20a52016-07-18 19:03:49 +0900113 }
114
115 @Override
116 public boolean equals(Object obj) {
117 if (this == obj) {
118 return true;
119 }
120
121 if (obj instanceof LispSegmentLcafAddress) {
122 final LispSegmentLcafAddress other = (LispSegmentLcafAddress) obj;
123 return Objects.equals(this.address, other.address) &&
Jian Lia7b394d2016-08-21 23:11:46 +0900124 Objects.equals(this.instanceId, other.instanceId) &&
125 Objects.equals(this.getReserved2(), other.getReserved2());
Jian Lic7e20a52016-07-18 19:03:49 +0900126 }
127 return false;
128 }
129
130 @Override
131 public String toString() {
132 return toStringHelper(this)
133 .add("address", address)
134 .add("instanceId", instanceId)
Jian Lia7b394d2016-08-21 23:11:46 +0900135 .add("idMaskLength", getReserved2())
Jian Lic7e20a52016-07-18 19:03:49 +0900136 .toString();
137 }
Jian Li115d8602016-08-15 20:21:53 +0900138
Jian Lia7b394d2016-08-21 23:11:46 +0900139 public static final class SegmentAddressBuilder
140 extends LcafAddressBuilder<SegmentAddressBuilder> {
Jian Li115d8602016-08-15 20:21:53 +0900141 private byte idMaskLength;
142 private LispAfiAddress address;
143 private int instanceId;
144
145 /**
146 * Sets identifier mask length.
147 *
148 * @param idMaskLength identifier mask length
149 * @return SegmentAddressBuilder object
150 */
151 public SegmentAddressBuilder withIdMaskLength(byte idMaskLength) {
152 this.idMaskLength = idMaskLength;
153 return this;
154 }
155
156 /**
157 * Sets instance identifer.
158 *
159 * @param instanceId instance identifier
160 * @return SegmentAddressBuilder object
161 */
162 public SegmentAddressBuilder withInstanceId(int instanceId) {
163 this.instanceId = instanceId;
164 return this;
165 }
166
167 /**
168 * Sets AFI address.
169 *
170 * @param address AFI address
171 * @return SegmentAddressBuilder object
172 */
173 public SegmentAddressBuilder withAddress(LispAfiAddress address) {
174 this.address = address;
175 return this;
176 }
177
178 /**
179 * Builds LispSegmentLcafAddress instance.
180 *
181 * @return LispSegmentLcafAddress instance
182 */
183 public LispSegmentLcafAddress build() {
Jian Lia7b394d2016-08-21 23:11:46 +0900184 return new LispSegmentLcafAddress(reserved1, idMaskLength, flag,
185 length, instanceId, address);
Jian Li115d8602016-08-15 20:21:53 +0900186 }
187 }
188
189 /**
190 * Segment LCAF address reader class.
191 */
192 public static class SegmentLcafAddressReader
Jian Lia7b394d2016-08-21 23:11:46 +0900193 implements LispAddressReader<LispSegmentLcafAddress> {
Jian Li115d8602016-08-15 20:21:53 +0900194
195 @Override
196 public LispSegmentLcafAddress readFrom(ByteBuf byteBuf)
Jian Lia7b394d2016-08-21 23:11:46 +0900197 throws LispParseError, LispReaderException {
Jian Li115d8602016-08-15 20:21:53 +0900198
Jian Lia7b394d2016-08-21 23:11:46 +0900199 LispLcafAddress lcafAddress = LispLcafAddress.deserializeCommon(byteBuf);
200
201 byte idMaskLength = lcafAddress.getReserved2();
202
Jian Li115d8602016-08-15 20:21:53 +0900203 int instanceId = (int) byteBuf.readUnsignedInt();
Jian Lia7b394d2016-08-21 23:11:46 +0900204 LispAfiAddress address = new LispAfiAddress.AfiAddressReader().readFrom(byteBuf);
Jian Li115d8602016-08-15 20:21:53 +0900205
206 return new SegmentAddressBuilder()
Jian Lia7b394d2016-08-21 23:11:46 +0900207 .withReserved1(lcafAddress.getReserved1())
208 .withFlag(lcafAddress.getFlag())
209 .withLength(lcafAddress.getLength())
210 .withIdMaskLength(idMaskLength)
211 .withInstanceId(instanceId)
212 .withAddress(address)
213 .build();
Jian Li115d8602016-08-15 20:21:53 +0900214 }
215 }
GUNiba871702016-08-22 21:06:02 +0900216
217 /**
218 * Segment LCAF address writer class.
219 */
220 public static class SegmentLcafAddressWriter
221 implements LispAddressWriter<LispSegmentLcafAddress> {
222
223 @Override
224 public void writeTo(ByteBuf byteBuf, LispSegmentLcafAddress address)
225 throws LispWriterException {
226
227 LispLcafAddress.serializeCommon(byteBuf, address);
228
229 byteBuf.writeInt(address.getInstanceId());
Jian Li76ea0572016-08-29 12:41:16 +0900230
231 new LispAfiAddress.AfiAddressWriter().writeTo(byteBuf, address.getAddress());
GUNiba871702016-08-22 21:06:02 +0900232 }
233 }
Jian Lic7e20a52016-07-18 19:03:49 +0900234}