blob: 9c4becedc18353c32b71a29027fcc8f80c24a021 [file] [log] [blame]
Jian Li10a09062016-07-26 23:58:50 +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.protocols;
17
Jian Li18f3bce2016-08-04 17:36:41 +090018import com.google.common.base.Objects;
Jian Li47671902016-08-11 01:18:18 +090019import com.google.common.collect.ImmutableList;
20import com.google.common.collect.Lists;
Jian Li26069e22016-08-10 22:00:52 +090021import io.netty.buffer.ByteBuf;
Jian Li47671902016-08-11 01:18:18 +090022import org.onlab.util.ByteOperator;
Jian Li26069e22016-08-10 22:00:52 +090023import org.onosproject.lisp.msg.exceptions.LispParseError;
Jian Lia7b394d2016-08-21 23:11:46 +090024import org.onosproject.lisp.msg.exceptions.LispReaderException;
Jian Liedc5db12016-08-23 17:30:19 +090025import org.onosproject.lisp.msg.exceptions.LispWriterException;
Jian Li10a09062016-07-26 23:58:50 +090026import org.onosproject.lisp.msg.types.LispAfiAddress;
27
Jian Li47671902016-08-11 01:18:18 +090028import java.util.List;
29
Jian Li18f3bce2016-08-04 17:36:41 +090030import static com.google.common.base.MoreObjects.toStringHelper;
Jian Lid4e63702016-08-30 18:29:20 +090031import static com.google.common.base.Preconditions.checkNotNull;
Jian Liedc5db12016-08-23 17:30:19 +090032import static org.onosproject.lisp.msg.types.LispAfiAddress.AfiAddressWriter;
33import static org.onosproject.lisp.msg.protocols.DefaultLispLocatorRecord.LocatorRecordWriter;
Jian Li18f3bce2016-08-04 17:36:41 +090034
Jian Li10a09062016-07-26 23:58:50 +090035/**
36 * Default implementation of LispMapRecord.
37 */
Jian Li631e62a2016-08-03 22:42:00 +090038public final class DefaultLispMapRecord implements LispMapRecord {
Jian Li10a09062016-07-26 23:58:50 +090039
Jian Li631e62a2016-08-03 22:42:00 +090040 private final int recordTtl;
Jian Li631e62a2016-08-03 22:42:00 +090041 private final byte maskLength;
42 private final LispMapReplyAction action;
43 private final boolean authoritative;
44 private final short mapVersionNumber;
45 private final LispAfiAddress eidPrefixAfi;
Jian Li47671902016-08-11 01:18:18 +090046 private final List<LispLocatorRecord> locatorRecords;
Jian Li10a09062016-07-26 23:58:50 +090047
Jian Li631e62a2016-08-03 22:42:00 +090048 /**
49 * A private constructor that protects object instantiation from external.
50 *
51 * @param recordTtl record time-to-live value
Jian Li631e62a2016-08-03 22:42:00 +090052 * @param maskLength mask length
53 * @param action lisp map reply action
54 * @param authoritative authoritative flag
55 * @param mapVersionNumber map version number
56 * @param eidPrefixAfi EID prefix AFI address
57 */
Jian Li42b3e432016-08-31 01:05:20 +090058 private DefaultLispMapRecord(int recordTtl, byte maskLength,
Jian Li631e62a2016-08-03 22:42:00 +090059 LispMapReplyAction action, boolean authoritative,
Jian Li47671902016-08-11 01:18:18 +090060 short mapVersionNumber, LispAfiAddress eidPrefixAfi,
61 List<LispLocatorRecord> locatorRecords) {
Jian Li631e62a2016-08-03 22:42:00 +090062 this.recordTtl = recordTtl;
Jian Li631e62a2016-08-03 22:42:00 +090063 this.maskLength = maskLength;
64 this.action = action;
65 this.authoritative = authoritative;
66 this.mapVersionNumber = mapVersionNumber;
67 this.eidPrefixAfi = eidPrefixAfi;
Jian Li47671902016-08-11 01:18:18 +090068 this.locatorRecords = locatorRecords;
Jian Li631e62a2016-08-03 22:42:00 +090069 }
70
71 @Override
Jian Li10a09062016-07-26 23:58:50 +090072 public int getRecordTtl() {
73 return recordTtl;
74 }
75
Jian Li631e62a2016-08-03 22:42:00 +090076 @Override
Jian Li10a09062016-07-26 23:58:50 +090077 public int getLocatorCount() {
Jian Li42b3e432016-08-31 01:05:20 +090078 return locatorRecords.size();
Jian Li10a09062016-07-26 23:58:50 +090079 }
80
Jian Li631e62a2016-08-03 22:42:00 +090081 @Override
Jian Li10a09062016-07-26 23:58:50 +090082 public byte getMaskLength() {
83 return maskLength;
84 }
85
Jian Li631e62a2016-08-03 22:42:00 +090086 @Override
Jian Li10a09062016-07-26 23:58:50 +090087 public LispMapReplyAction getAction() {
88 return action;
89 }
90
Jian Li631e62a2016-08-03 22:42:00 +090091 @Override
Jian Li10a09062016-07-26 23:58:50 +090092 public boolean isAuthoritative() {
93 return authoritative;
94 }
95
Jian Li631e62a2016-08-03 22:42:00 +090096 @Override
Jian Li10a09062016-07-26 23:58:50 +090097 public short getMapVersionNumber() {
98 return mapVersionNumber;
99 }
100
Jian Li631e62a2016-08-03 22:42:00 +0900101 @Override
Jian Li10a09062016-07-26 23:58:50 +0900102 public LispAfiAddress getEidPrefixAfi() {
103 return eidPrefixAfi;
104 }
105
Jian Li18f3bce2016-08-04 17:36:41 +0900106 @Override
Jian Li47671902016-08-11 01:18:18 +0900107 public List<LispLocatorRecord> getLocators() {
108 return ImmutableList.copyOf(locatorRecords);
109 }
110
111 @Override
112 public void writeTo(ByteBuf byteBuf) {
113
114 }
115
116 @Override
Jian Li18f3bce2016-08-04 17:36:41 +0900117 public String toString() {
118 return toStringHelper(this)
119 .add("record TTL", recordTtl)
Jian Li18f3bce2016-08-04 17:36:41 +0900120 .add("maskLength", maskLength)
121 .add("action", action)
122 .add("authoritative", authoritative)
123 .add("mapVersionNumber", mapVersionNumber)
Jian Li47671902016-08-11 01:18:18 +0900124 .add("EID prefix AFI address", eidPrefixAfi)
125 .add("locator records", locatorRecords).toString();
126
Jian Li18f3bce2016-08-04 17:36:41 +0900127 }
128
129 @Override
130 public boolean equals(Object o) {
131 if (this == o) {
132 return true;
133 }
134 if (o == null || getClass() != o.getClass()) {
135 return false;
136 }
137 DefaultLispMapRecord that = (DefaultLispMapRecord) o;
138 return Objects.equal(recordTtl, that.recordTtl) &&
Jian Li18f3bce2016-08-04 17:36:41 +0900139 Objects.equal(maskLength, that.maskLength) &&
140 Objects.equal(action, that.action) &&
141 Objects.equal(authoritative, that.authoritative) &&
142 Objects.equal(mapVersionNumber, that.mapVersionNumber) &&
Jian Li47671902016-08-11 01:18:18 +0900143 Objects.equal(eidPrefixAfi, that.eidPrefixAfi) &&
144 Objects.equal(locatorRecords, that.locatorRecords);
Jian Li18f3bce2016-08-04 17:36:41 +0900145 }
146
147 @Override
148 public int hashCode() {
Jian Li42b3e432016-08-31 01:05:20 +0900149 return Objects.hashCode(recordTtl, maskLength, action,
Jian Li47671902016-08-11 01:18:18 +0900150 authoritative, mapVersionNumber, eidPrefixAfi, locatorRecords);
Jian Li18f3bce2016-08-04 17:36:41 +0900151 }
152
Jian Li10a09062016-07-26 23:58:50 +0900153 public static final class DefaultMapRecordBuilder implements MapRecordBuilder {
154
Jian Li631e62a2016-08-03 22:42:00 +0900155 private int recordTtl;
Jian Li631e62a2016-08-03 22:42:00 +0900156 private byte maskLength;
157 private LispMapReplyAction action;
158 private boolean authoritative;
159 private short mapVersionNumber;
160 private LispAfiAddress eidPrefixAfi;
Jian Lid4e63702016-08-30 18:29:20 +0900161 private List<LispLocatorRecord> locatorRecords = Lists.newArrayList();
Jian Li631e62a2016-08-03 22:42:00 +0900162
Jian Li10a09062016-07-26 23:58:50 +0900163 @Override
164 public MapRecordBuilder withRecordTtl(int recordTtl) {
Jian Li631e62a2016-08-03 22:42:00 +0900165 this.recordTtl = recordTtl;
166 return this;
Jian Li10a09062016-07-26 23:58:50 +0900167 }
168
169 @Override
Jian Li10a09062016-07-26 23:58:50 +0900170 public MapRecordBuilder withMaskLength(byte maskLength) {
Jian Li631e62a2016-08-03 22:42:00 +0900171 this.maskLength = maskLength;
172 return this;
Jian Li10a09062016-07-26 23:58:50 +0900173 }
174
175 @Override
176 public MapRecordBuilder withAction(LispMapReplyAction action) {
Jian Li631e62a2016-08-03 22:42:00 +0900177 this.action = action;
178 return this;
Jian Li10a09062016-07-26 23:58:50 +0900179 }
180
181 @Override
182 public MapRecordBuilder withAuthoritative(boolean authoritative) {
Jian Li631e62a2016-08-03 22:42:00 +0900183 this.authoritative = authoritative;
184 return this;
Jian Li10a09062016-07-26 23:58:50 +0900185 }
186
187 @Override
188 public MapRecordBuilder withMapVersionNumber(short mapVersionNumber) {
Jian Li631e62a2016-08-03 22:42:00 +0900189 this.mapVersionNumber = mapVersionNumber;
190 return this;
Jian Li10a09062016-07-26 23:58:50 +0900191 }
192
193 @Override
194 public MapRecordBuilder withEidPrefixAfi(LispAfiAddress prefix) {
Jian Li631e62a2016-08-03 22:42:00 +0900195 this.eidPrefixAfi = prefix;
196 return this;
197 }
198
199 @Override
Jian Li47671902016-08-11 01:18:18 +0900200 public MapRecordBuilder withLocators(List<LispLocatorRecord> records) {
Jian Lie4ba2a42016-08-29 20:24:15 +0900201 if (records != null) {
202 this.locatorRecords = ImmutableList.copyOf(records);
Jian Lie4ba2a42016-08-29 20:24:15 +0900203 }
Jian Li47671902016-08-11 01:18:18 +0900204 return this;
205 }
206
207 @Override
Jian Li631e62a2016-08-03 22:42:00 +0900208 public LispMapRecord build() {
Jian Lie4ba2a42016-08-29 20:24:15 +0900209
Jian Lid4e63702016-08-30 18:29:20 +0900210 checkNotNull(eidPrefixAfi, "Must specify an EID prefix");
Jian Lie4ba2a42016-08-29 20:24:15 +0900211
Jian Li42b3e432016-08-31 01:05:20 +0900212 return new DefaultLispMapRecord(recordTtl, maskLength, action,
213 authoritative, mapVersionNumber, eidPrefixAfi, locatorRecords);
Jian Li10a09062016-07-26 23:58:50 +0900214 }
215 }
Jian Li26069e22016-08-10 22:00:52 +0900216
217 /**
Jian Li47671902016-08-11 01:18:18 +0900218 * A LISP message reader for MapRecord portion.
Jian Li26069e22016-08-10 22:00:52 +0900219 */
Jian Li47671902016-08-11 01:18:18 +0900220 public static final class MapRecordReader implements LispMessageReader<LispMapRecord> {
221
222 private static final int AUTHORITATIVE_INDEX = 4;
223 private static final int RESERVED_SKIP_LENGTH = 1;
Jian Li26069e22016-08-10 22:00:52 +0900224
Jian Lie4ba2a42016-08-29 20:24:15 +0900225 private static final int REPLY_ACTION_SHIFT_BIT = 5;
226
Jian Li26069e22016-08-10 22:00:52 +0900227 @Override
Jian Lia7b394d2016-08-21 23:11:46 +0900228 public LispMapRecord readFrom(ByteBuf byteBuf) throws LispParseError, LispReaderException {
Jian Li47671902016-08-11 01:18:18 +0900229
230 // Record TTL -> 32 bits
231 int recordTtl = byteBuf.readInt();
232
233 // Locator count -> 8 bits
Jian Lie4ba2a42016-08-29 20:24:15 +0900234 int locatorCount = byteBuf.readUnsignedByte();
Jian Li47671902016-08-11 01:18:18 +0900235
236 // EID mask length -> 8 bits
237 byte maskLength = (byte) byteBuf.readUnsignedByte();
238
Jian Lie4ba2a42016-08-29 20:24:15 +0900239 byte actionWithFlag = (byte) byteBuf.readUnsignedByte();
Jian Li47671902016-08-11 01:18:18 +0900240
Jian Lie4ba2a42016-08-29 20:24:15 +0900241 // action -> 3 bit
242 int actionByte = actionWithFlag >> REPLY_ACTION_SHIFT_BIT;
243 LispMapReplyAction action = LispMapReplyAction.valueOf(actionByte);
244 if (action == null) {
245 action = LispMapReplyAction.NoAction;
246 }
Jian Li47671902016-08-11 01:18:18 +0900247
248 // authoritative flag -> 1 bit
Jian Lie4ba2a42016-08-29 20:24:15 +0900249 boolean authoritative = ByteOperator.getBit((byte) (actionWithFlag >> AUTHORITATIVE_INDEX), 0);
Jian Li47671902016-08-11 01:18:18 +0900250
251 // let's skip the reserved field
252 byteBuf.skipBytes(RESERVED_SKIP_LENGTH);
253
254 // Map version number -> 12 bits, we treat Rsvd field is all zero
255 short mapVersionNumber = (short) byteBuf.readUnsignedShort();
256
Jian Lia7b394d2016-08-21 23:11:46 +0900257 LispAfiAddress eidPrefixAfi = new LispAfiAddress.AfiAddressReader().readFrom(byteBuf);
Jian Li47671902016-08-11 01:18:18 +0900258
259 List<LispLocatorRecord> locators = Lists.newArrayList();
260 for (int i = 0; i < locatorCount; i++) {
261 locators.add(new DefaultLispLocatorRecord.LocatorRecordReader().readFrom(byteBuf));
262 }
263
264 return new DefaultMapRecordBuilder()
265 .withRecordTtl(recordTtl)
Jian Li47671902016-08-11 01:18:18 +0900266 .withMaskLength(maskLength)
Jian Lie4ba2a42016-08-29 20:24:15 +0900267 .withAction(action)
Jian Li47671902016-08-11 01:18:18 +0900268 .withAuthoritative(authoritative)
269 .withMapVersionNumber(mapVersionNumber)
270 .withLocators(locators)
Jian Lia7b394d2016-08-21 23:11:46 +0900271 .withEidPrefixAfi(eidPrefixAfi)
Jian Li47671902016-08-11 01:18:18 +0900272 .build();
Jian Li26069e22016-08-10 22:00:52 +0900273 }
274 }
Jian Liedc5db12016-08-23 17:30:19 +0900275
276 /**
277 * A LISP message writer for MapRecord portion.
278 */
279 public static final class MapRecordWriter implements LispMessageWriter<LispMapRecord> {
280
281 private static final int REPLY_ACTION_SHIFT_BIT = 5;
282 private static final int AUTHORITATIVE_FLAG_SHIFT_BIT = 4;
283
284 private static final int ENABLE_BIT = 1;
285 private static final int DISABLE_BIT = 0;
286
287 @Override
288 public void writeTo(ByteBuf byteBuf, LispMapRecord message) throws LispWriterException {
289
290 // record TTL
291 byteBuf.writeInt(message.getRecordTtl());
292
293 // locator count
Jian Li42b3e432016-08-31 01:05:20 +0900294 byteBuf.writeByte((byte) message.getLocators().size());
Jian Liedc5db12016-08-23 17:30:19 +0900295
296 // EID mask length
297 byteBuf.writeByte(message.getMaskLength());
298
299 // reply action
300 byte action = (byte) (message.getAction().getAction() << REPLY_ACTION_SHIFT_BIT);
301
302 // authoritative bit
303 byte authoritative = DISABLE_BIT;
304 if (message.isAuthoritative()) {
Jian Lie4ba2a42016-08-29 20:24:15 +0900305 authoritative = ENABLE_BIT << AUTHORITATIVE_FLAG_SHIFT_BIT;
Jian Liedc5db12016-08-23 17:30:19 +0900306 }
Jian Liedc5db12016-08-23 17:30:19 +0900307
308 byteBuf.writeByte((byte) (action + authoritative));
309
310 // fill zero into reserved field
311 byteBuf.writeByte((short) 0);
312
313 // map version number
314 byteBuf.writeShort(message.getMapVersionNumber());
315
316 // EID prefix AFI with EID prefix
317 AfiAddressWriter afiAddressWriter = new AfiAddressWriter();
318 afiAddressWriter.writeTo(byteBuf, message.getEidPrefixAfi());
319
320 // serialize locator
321 LocatorRecordWriter recordWriter = new LocatorRecordWriter();
322 List<LispLocatorRecord> locators = message.getLocators();
323 for (int i = 0; i < locators.size(); i++) {
324 recordWriter.writeTo(byteBuf, locators.get(i));
325 }
326 }
327 }
Jian Li10a09062016-07-26 23:58:50 +0900328}