blob: 771b3ca20d4313935a7084ea73237bee067b31a5 [file] [log] [blame]
Jian Li2af9eaa2017-02-05 09:15:07 +09001/*
2 * Copyright 2017-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.lcaf;
17
18import io.netty.buffer.ByteBuf;
19import org.onosproject.lisp.msg.exceptions.LispParseError;
20import org.onosproject.lisp.msg.exceptions.LispReaderException;
21import org.onosproject.lisp.msg.exceptions.LispWriterException;
22import org.onosproject.lisp.msg.types.LispAddressReader;
23import org.onosproject.lisp.msg.types.LispAddressWriter;
24import org.onosproject.lisp.msg.types.LispAfiAddress;
25
26import java.util.Objects;
27
28import static com.google.common.base.MoreObjects.toStringHelper;
29import static com.google.common.base.Preconditions.checkNotNull;
30
31/**
32 * Multicast group membership type LCAF address class.
33 * <p>
34 * Multicast group membership type is defined in draft-ietf-lisp-lcaf-22
35 * https://tools.ietf.org/html/draft-ietf-lisp-lcaf-22#page-15
36 * <p>
37 * <pre>
38 * {@literal
39 * 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 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
44 * | Type = 9 | Rsvd2 | Length |
45 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
46 * | Instance-ID |
47 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
48 * | Reserved | Source MaskLen| Group MaskLen |
49 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
50 * | AFI = x | Source/Subnet Address ... |
51 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
52 * | AFI = x | Group Address ... |
53 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
54 * }</pre>
55 */
56public final class LispMulticastLcafAddress extends LispLcafAddress {
57
58 private final int instanceId;
59 private final byte srcMaskLenth;
60 private final byte grpMaskLength;
61 private final LispAfiAddress srcAddress;
62 private final LispAfiAddress grpAddress;
63
64 /**
65 * Initializes multicast type LCAF address.
66 *
67 * @param instanceId instance identifier
68 * @param srcMaskLenth source mask length
69 * @param grpMaskLength group mask length
70 * @param srcAddress source address
71 * @param grpAddress group address
72 */
73 private LispMulticastLcafAddress(int instanceId, byte srcMaskLenth,
74 byte grpMaskLength, LispAfiAddress srcAddress,
75 LispAfiAddress grpAddress) {
76 super(LispCanonicalAddressFormatEnum.MULTICAST);
77 this.instanceId = instanceId;
78 this.srcMaskLenth = srcMaskLenth;
79 this.grpMaskLength = grpMaskLength;
80 this.srcAddress = srcAddress;
81 this.grpAddress = grpAddress;
82 }
83
84 /**
85 * Obtains instance identifier.
86 *
87 * @return instance identifier
88 */
89 public int getInstanceId() {
90 return instanceId;
91 }
92
93 /**
94 * Obtains source mask length.
95 *
96 * @return source mask length
97 */
98 public byte getSrcMaskLenth() {
99 return srcMaskLenth;
100 }
101
102 /**
103 * Obtains group mask length.
104 *
105 * @return group mask length
106 */
107 public byte getGrpMaskLength() {
108 return grpMaskLength;
109 }
110
111 /**
112 * Obtains source address.
113 *
114 * @return source address
115 */
116 public LispAfiAddress getSrcAddress() {
117 return srcAddress;
118 }
119
120 /**
121 * Obtains group address.
122 *
123 * @return group address
124 */
125 public LispAfiAddress getGrpAddress() {
126 return grpAddress;
127 }
128
129 @Override
130 public int hashCode() {
131 return Objects.hash(instanceId, srcMaskLenth, grpMaskLength,
132 srcAddress, grpAddress);
133 }
134
135 @Override
136 public boolean equals(Object obj) {
137 if (this == obj) {
138 return true;
139 }
140
141 if (obj instanceof LispMulticastLcafAddress) {
142 final LispMulticastLcafAddress other = (LispMulticastLcafAddress) obj;
143 return Objects.equals(this.instanceId, other.instanceId) &&
144 Objects.equals(this.srcMaskLenth, other.srcMaskLenth) &&
145 Objects.equals(this.grpMaskLength, other.grpMaskLength) &&
146 Objects.equals(this.srcAddress, other.srcAddress) &&
147 Objects.equals(this.grpAddress, other.grpAddress);
148
149 }
150 return false;
151 }
152
153 @Override
154 public String toString() {
155 return toStringHelper(this)
156 .add("instance ID", instanceId)
157 .add("source mask length", srcMaskLenth)
158 .add("group mask length", grpMaskLength)
159 .add("source address", srcAddress)
160 .add("group address", grpAddress)
161 .toString();
162 }
163
164 public static final class MulticastAddressBuilder
165 extends LcafAddressBuilder<MulticastAddressBuilder> {
166 private int instanceId;
167 private byte srcMaskLenth;
168 private byte grpMaskLength;
169 private LispAfiAddress srcAddress;
170 private LispAfiAddress grpAddress;
171
172 /**
173 * Sets instance identifier.
174 *
175 * @param instanceId instance identifier
176 * @return MulticastAddressBuilder object
177 */
178 public MulticastAddressBuilder withInstanceId(int instanceId) {
179 this.instanceId = instanceId;
180 return this;
181 }
182
183 /**
184 * Sets source mask length.
185 *
186 * @param srcMaskLenth source mask length
187 * @return MulticastAddressBuilder object
188 */
189 public MulticastAddressBuilder withSrcMaskLength(byte srcMaskLenth) {
190 this.srcMaskLenth = srcMaskLenth;
191 return this;
192 }
193
194 /**
195 * Sets group mask length.
196 *
197 * @param grpMaskLength group mask length
198 * @return MulticastAddressBuilder object
199 */
200 public MulticastAddressBuilder withGrpMaskLength(byte grpMaskLength) {
201 this.grpMaskLength = grpMaskLength;
202 return this;
203 }
204
205 /**
206 * Sets source address.
207 *
208 * @param srcAddress source address
209 * @return MulticastAddressBuilder object
210 */
211 public MulticastAddressBuilder withSrcAddress(LispAfiAddress srcAddress) {
212 this.srcAddress = srcAddress;
213 return this;
214 }
215
216 /**
217 * Sets group address.
218 *
219 * @param grpAddress group address
220 * @return MulticastAddressBuilder object
221 */
222 public MulticastAddressBuilder withGrpAddress(LispAfiAddress grpAddress) {
223 this.grpAddress = grpAddress;
224 return this;
225 }
226
227 /**
228 * Builds LispMulticastLcafAddress instance.
229 *
230 * @return LispMulticastLcafAddress instance
231 */
232 public LispMulticastLcafAddress build() {
233
234 checkNotNull(srcAddress, "Must specify a source address");
235 checkNotNull(grpAddress, "Must specify a group address");
236
237 return new LispMulticastLcafAddress(instanceId, srcMaskLenth,
238 grpMaskLength, srcAddress, grpAddress);
239 }
240 }
241
242 /**
243 * Multicast LCAF address reader class.
244 */
245 public static class MulticastLcafAddressReader
246 implements LispAddressReader<LispMulticastLcafAddress> {
247
248 private static final int RESERVED_SKIP_LENGTH = 2;
249
250 @Override
251 public LispMulticastLcafAddress readFrom(ByteBuf byteBuf)
252 throws LispParseError, LispReaderException {
253
254 LispLcafAddress.deserializeCommon(byteBuf);
255
256 int instanceId = (int) byteBuf.readUnsignedInt();
257 byteBuf.skipBytes(RESERVED_SKIP_LENGTH);
258 byte srcMaskLength = (byte) byteBuf.readUnsignedByte();
259 byte grpMaskLength = (byte) byteBuf.readUnsignedByte();
260 LispAfiAddress srcAddress = new AfiAddressReader().readFrom(byteBuf);
261 LispAfiAddress grpAddress = new AfiAddressReader().readFrom(byteBuf);
262
263 return new MulticastAddressBuilder()
264 .withInstanceId(instanceId)
265 .withSrcMaskLength(srcMaskLength)
266 .withGrpMaskLength(grpMaskLength)
267 .withSrcAddress(srcAddress)
268 .withGrpAddress(grpAddress)
269 .build();
270 }
271 }
272
273 /**
274 * Multicast LCAF address writer class.
275 */
276 public static class MulticastLcafAddressWriter
277 implements LispAddressWriter<LispMulticastLcafAddress> {
278
279 private static final int UNUSED_ZERO = 0;
280
281 @Override
282 public void writeTo(ByteBuf byteBuf, LispMulticastLcafAddress address)
283 throws LispWriterException {
284 int lcafIndex = byteBuf.writerIndex();
285 LispLcafAddress.serializeCommon(byteBuf, address);
286
287 // instance identifier
288 byteBuf.writeInt(address.getInstanceId());
289
290 // reserved field
291 byteBuf.writeByte(UNUSED_ZERO);
292 byteBuf.writeByte(UNUSED_ZERO);
293
294 // source mask length
295 byteBuf.writeByte(address.getSrcMaskLenth());
296
297 // group mask length
298 byteBuf.writeByte(address.getGrpMaskLength());
299
300 // source address
301 AfiAddressWriter srcWriter = new AfiAddressWriter();
302 srcWriter.writeTo(byteBuf, address.getSrcAddress());
303
304 // group address
305 AfiAddressWriter grpWriter = new AfiAddressWriter();
306 grpWriter.writeTo(byteBuf, address.getGrpAddress());
307
308 LispLcafAddress.updateLength(lcafIndex, byteBuf);
309 }
310 }
311}