blob: 6f2cc222e3b12a8a11bcaacb17d6ffa520337470 [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;
21
Jian Lic7e20a52016-07-18 19:03:49 +090022import java.util.Objects;
23
24import static com.google.common.base.MoreObjects.toStringHelper;
25
26/**
27 * Instance ID type LCAF address class.
Jian Li8fc2d2f2016-08-08 14:43:53 +090028 * <p>
Jian Lic7e20a52016-07-18 19:03:49 +090029 * Instance ID type is defined in draft-ietf-lisp-lcaf-13
30 * https://tools.ietf.org/html/draft-ietf-lisp-lcaf-13#page-7
31 *
Jian Li8fc2d2f2016-08-08 14:43:53 +090032 * <pre>
33 * {@literal
Jian Lic7e20a52016-07-18 19:03:49 +090034 * 0 1 2 3
35 * 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
36 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
37 * | AFI = 16387 | Rsvd1 | Flags |
38 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
39 * | Type = 2 | IID mask-len | 4 + n |
40 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
41 * | Instance ID |
42 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
43 * | AFI = x | Address ... |
44 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
Jian Li8fc2d2f2016-08-08 14:43:53 +090045 * }</pre>
Jian Lic7e20a52016-07-18 19:03:49 +090046 */
Jian Li115d8602016-08-15 20:21:53 +090047public final class LispSegmentLcafAddress extends LispLcafAddress {
Jian Lic7e20a52016-07-18 19:03:49 +090048
49 private final LispAfiAddress address;
50 private final int instanceId;
51
52 /**
53 * Initializes segment type LCAF address.
54 *
55 * @param idMaskLength Id mask length
Jian Lia7b394d2016-08-21 23:11:46 +090056 * @param instanceId instance id
57 * @param address address
Jian Lic7e20a52016-07-18 19:03:49 +090058 */
Jian Li115d8602016-08-15 20:21:53 +090059 private LispSegmentLcafAddress(byte idMaskLength, int instanceId, LispAfiAddress address) {
Jian Lic7e20a52016-07-18 19:03:49 +090060 super(LispCanonicalAddressFormatEnum.SEGMENT, idMaskLength);
61 this.address = address;
62 this.instanceId = instanceId;
63 }
64
65 /**
Jian Lia7b394d2016-08-21 23:11:46 +090066 * Initializes segment type LCAF address.
67 *
68 * @param reserved1 reserved1
69 * @param idMaskLength ID mask length
70 * @param flag flag
71 * @param length length
72 * @param instanceId instance id
73 * @param address address
74 */
75 private LispSegmentLcafAddress(byte reserved1, byte idMaskLength, byte flag, short length,
76 int instanceId, LispAfiAddress address) {
77 super(LispCanonicalAddressFormatEnum.SEGMENT, reserved1, idMaskLength, flag, length);
78 this.address = address;
79 this.instanceId = instanceId;
80 }
81
82 /**
Jian Lic7e20a52016-07-18 19:03:49 +090083 * Obtains address.
84 *
85 * @return address
86 */
87 public LispAfiAddress getAddress() {
88 return address;
89 }
90
91 /**
92 * Obtains instance id.
93 *
94 * @return instance id
95 */
96 public int getInstanceId() {
97 return instanceId;
98 }
99
100 /**
101 * Obtains id mask length.
102 *
103 * @return id mask length
104 */
105 public byte getIdMaskLength() {
Jian Lia7b394d2016-08-21 23:11:46 +0900106 return getReserved2();
Jian Lic7e20a52016-07-18 19:03:49 +0900107 }
108
109 @Override
110 public int hashCode() {
Jian Lia7b394d2016-08-21 23:11:46 +0900111 return Objects.hash(address, instanceId, getReserved2());
Jian Lic7e20a52016-07-18 19:03:49 +0900112 }
113
114 @Override
115 public boolean equals(Object obj) {
116 if (this == obj) {
117 return true;
118 }
119
120 if (obj instanceof LispSegmentLcafAddress) {
121 final LispSegmentLcafAddress other = (LispSegmentLcafAddress) obj;
122 return Objects.equals(this.address, other.address) &&
Jian Lia7b394d2016-08-21 23:11:46 +0900123 Objects.equals(this.instanceId, other.instanceId) &&
124 Objects.equals(this.getReserved2(), other.getReserved2());
Jian Lic7e20a52016-07-18 19:03:49 +0900125 }
126 return false;
127 }
128
129 @Override
130 public String toString() {
131 return toStringHelper(this)
132 .add("address", address)
133 .add("instanceId", instanceId)
Jian Lia7b394d2016-08-21 23:11:46 +0900134 .add("idMaskLength", getReserved2())
Jian Lic7e20a52016-07-18 19:03:49 +0900135 .toString();
136 }
Jian Li115d8602016-08-15 20:21:53 +0900137
Jian Lia7b394d2016-08-21 23:11:46 +0900138 public static final class SegmentAddressBuilder
139 extends LcafAddressBuilder<SegmentAddressBuilder> {
Jian Li115d8602016-08-15 20:21:53 +0900140 private byte idMaskLength;
141 private LispAfiAddress address;
142 private int instanceId;
143
144 /**
145 * Sets identifier mask length.
146 *
147 * @param idMaskLength identifier mask length
148 * @return SegmentAddressBuilder object
149 */
150 public SegmentAddressBuilder withIdMaskLength(byte idMaskLength) {
151 this.idMaskLength = idMaskLength;
152 return this;
153 }
154
155 /**
156 * Sets instance identifer.
157 *
158 * @param instanceId instance identifier
159 * @return SegmentAddressBuilder object
160 */
161 public SegmentAddressBuilder withInstanceId(int instanceId) {
162 this.instanceId = instanceId;
163 return this;
164 }
165
166 /**
167 * Sets AFI address.
168 *
169 * @param address AFI address
170 * @return SegmentAddressBuilder object
171 */
172 public SegmentAddressBuilder withAddress(LispAfiAddress address) {
173 this.address = address;
174 return this;
175 }
176
177 /**
178 * Builds LispSegmentLcafAddress instance.
179 *
180 * @return LispSegmentLcafAddress instance
181 */
182 public LispSegmentLcafAddress build() {
Jian Lia7b394d2016-08-21 23:11:46 +0900183 return new LispSegmentLcafAddress(reserved1, idMaskLength, flag,
184 length, instanceId, address);
Jian Li115d8602016-08-15 20:21:53 +0900185 }
186 }
187
188 /**
189 * Segment LCAF address reader class.
190 */
191 public static class SegmentLcafAddressReader
Jian Lia7b394d2016-08-21 23:11:46 +0900192 implements LispAddressReader<LispSegmentLcafAddress> {
Jian Li115d8602016-08-15 20:21:53 +0900193
194 @Override
195 public LispSegmentLcafAddress readFrom(ByteBuf byteBuf)
Jian Lia7b394d2016-08-21 23:11:46 +0900196 throws LispParseError, LispReaderException {
Jian Li115d8602016-08-15 20:21:53 +0900197
Jian Lia7b394d2016-08-21 23:11:46 +0900198 LispLcafAddress lcafAddress = LispLcafAddress.deserializeCommon(byteBuf);
199
200 byte idMaskLength = lcafAddress.getReserved2();
201
Jian Li115d8602016-08-15 20:21:53 +0900202 int instanceId = (int) byteBuf.readUnsignedInt();
Jian Lia7b394d2016-08-21 23:11:46 +0900203 LispAfiAddress address = new LispAfiAddress.AfiAddressReader().readFrom(byteBuf);
Jian Li115d8602016-08-15 20:21:53 +0900204
205 return new SegmentAddressBuilder()
Jian Lia7b394d2016-08-21 23:11:46 +0900206 .withReserved1(lcafAddress.getReserved1())
207 .withFlag(lcafAddress.getFlag())
208 .withLength(lcafAddress.getLength())
209 .withIdMaskLength(idMaskLength)
210 .withInstanceId(instanceId)
211 .withAddress(address)
212 .build();
Jian Li115d8602016-08-15 20:21:53 +0900213 }
214 }
Jian Lic7e20a52016-07-18 19:03:49 +0900215}