Jian Li | 10a0906 | 2016-07-26 23:58:50 +0900 | [diff] [blame] | 1 | /* |
| 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 | */ |
| 16 | package org.onosproject.lisp.msg.protocols; |
| 17 | |
Jian Li | 18f3bce | 2016-08-04 17:36:41 +0900 | [diff] [blame] | 18 | import com.google.common.base.Objects; |
Jian Li | 4767190 | 2016-08-11 01:18:18 +0900 | [diff] [blame] | 19 | import com.google.common.collect.ImmutableList; |
| 20 | import com.google.common.collect.Lists; |
Jian Li | 26069e2 | 2016-08-10 22:00:52 +0900 | [diff] [blame] | 21 | import io.netty.buffer.ByteBuf; |
Jian Li | 4767190 | 2016-08-11 01:18:18 +0900 | [diff] [blame] | 22 | import org.onlab.util.ByteOperator; |
Jian Li | 26069e2 | 2016-08-10 22:00:52 +0900 | [diff] [blame] | 23 | import org.onosproject.lisp.msg.exceptions.LispParseError; |
Jian Li | 5e505c6 | 2016-12-05 02:44:24 +0900 | [diff] [blame] | 24 | import org.onosproject.lisp.msg.protocols.DefaultLispLocatorRecord.LocatorRecordReader; |
| 25 | import org.onosproject.lisp.msg.types.LispAfiAddress; |
Jian Li | a7b394d | 2016-08-21 23:11:46 +0900 | [diff] [blame] | 26 | import org.onosproject.lisp.msg.exceptions.LispReaderException; |
Jian Li | edc5db1 | 2016-08-23 17:30:19 +0900 | [diff] [blame] | 27 | import org.onosproject.lisp.msg.exceptions.LispWriterException; |
Jian Li | 5e505c6 | 2016-12-05 02:44:24 +0900 | [diff] [blame] | 28 | import org.onosproject.lisp.msg.types.LispAfiAddress.AfiAddressWriter; |
Jian Li | 10a0906 | 2016-07-26 23:58:50 +0900 | [diff] [blame] | 29 | |
Jian Li | 4767190 | 2016-08-11 01:18:18 +0900 | [diff] [blame] | 30 | import java.util.List; |
| 31 | |
Jian Li | 18f3bce | 2016-08-04 17:36:41 +0900 | [diff] [blame] | 32 | import static com.google.common.base.MoreObjects.toStringHelper; |
Jian Li | d4e6370 | 2016-08-30 18:29:20 +0900 | [diff] [blame] | 33 | import static com.google.common.base.Preconditions.checkNotNull; |
Jian Li | edc5db1 | 2016-08-23 17:30:19 +0900 | [diff] [blame] | 34 | import static org.onosproject.lisp.msg.protocols.DefaultLispLocatorRecord.LocatorRecordWriter; |
Jian Li | 18f3bce | 2016-08-04 17:36:41 +0900 | [diff] [blame] | 35 | |
Jian Li | 10a0906 | 2016-07-26 23:58:50 +0900 | [diff] [blame] | 36 | /** |
| 37 | * Default implementation of LispMapRecord. |
| 38 | */ |
Jian Li | 631e62a | 2016-08-03 22:42:00 +0900 | [diff] [blame] | 39 | public final class DefaultLispMapRecord implements LispMapRecord { |
Jian Li | 10a0906 | 2016-07-26 23:58:50 +0900 | [diff] [blame] | 40 | |
Jian Li | 631e62a | 2016-08-03 22:42:00 +0900 | [diff] [blame] | 41 | private final int recordTtl; |
Jian Li | 631e62a | 2016-08-03 22:42:00 +0900 | [diff] [blame] | 42 | private final byte maskLength; |
| 43 | private final LispMapReplyAction action; |
| 44 | private final boolean authoritative; |
| 45 | private final short mapVersionNumber; |
| 46 | private final LispAfiAddress eidPrefixAfi; |
Jian Li | 4767190 | 2016-08-11 01:18:18 +0900 | [diff] [blame] | 47 | private final List<LispLocatorRecord> locatorRecords; |
Jian Li | 10a0906 | 2016-07-26 23:58:50 +0900 | [diff] [blame] | 48 | |
Yoonseon Han | ca814bf | 2016-09-12 11:37:48 -0700 | [diff] [blame] | 49 | static final MapRecordWriter WRITER; |
| 50 | static { |
| 51 | WRITER = new MapRecordWriter(); |
| 52 | } |
| 53 | |
Jian Li | 631e62a | 2016-08-03 22:42:00 +0900 | [diff] [blame] | 54 | /** |
| 55 | * A private constructor that protects object instantiation from external. |
| 56 | * |
| 57 | * @param recordTtl record time-to-live value |
Jian Li | 631e62a | 2016-08-03 22:42:00 +0900 | [diff] [blame] | 58 | * @param maskLength mask length |
| 59 | * @param action lisp map reply action |
| 60 | * @param authoritative authoritative flag |
| 61 | * @param mapVersionNumber map version number |
| 62 | * @param eidPrefixAfi EID prefix AFI address |
| 63 | */ |
Jian Li | 42b3e43 | 2016-08-31 01:05:20 +0900 | [diff] [blame] | 64 | private DefaultLispMapRecord(int recordTtl, byte maskLength, |
Jian Li | 631e62a | 2016-08-03 22:42:00 +0900 | [diff] [blame] | 65 | LispMapReplyAction action, boolean authoritative, |
Jian Li | 4767190 | 2016-08-11 01:18:18 +0900 | [diff] [blame] | 66 | short mapVersionNumber, LispAfiAddress eidPrefixAfi, |
| 67 | List<LispLocatorRecord> locatorRecords) { |
Jian Li | 631e62a | 2016-08-03 22:42:00 +0900 | [diff] [blame] | 68 | this.recordTtl = recordTtl; |
Jian Li | 631e62a | 2016-08-03 22:42:00 +0900 | [diff] [blame] | 69 | this.maskLength = maskLength; |
| 70 | this.action = action; |
| 71 | this.authoritative = authoritative; |
| 72 | this.mapVersionNumber = mapVersionNumber; |
| 73 | this.eidPrefixAfi = eidPrefixAfi; |
Jian Li | 4767190 | 2016-08-11 01:18:18 +0900 | [diff] [blame] | 74 | this.locatorRecords = locatorRecords; |
Jian Li | 631e62a | 2016-08-03 22:42:00 +0900 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | @Override |
Jian Li | 10a0906 | 2016-07-26 23:58:50 +0900 | [diff] [blame] | 78 | public int getRecordTtl() { |
| 79 | return recordTtl; |
| 80 | } |
| 81 | |
Jian Li | 631e62a | 2016-08-03 22:42:00 +0900 | [diff] [blame] | 82 | @Override |
Jian Li | 10a0906 | 2016-07-26 23:58:50 +0900 | [diff] [blame] | 83 | public int getLocatorCount() { |
Jian Li | 42b3e43 | 2016-08-31 01:05:20 +0900 | [diff] [blame] | 84 | return locatorRecords.size(); |
Jian Li | 10a0906 | 2016-07-26 23:58:50 +0900 | [diff] [blame] | 85 | } |
| 86 | |
Jian Li | 631e62a | 2016-08-03 22:42:00 +0900 | [diff] [blame] | 87 | @Override |
Jian Li | 10a0906 | 2016-07-26 23:58:50 +0900 | [diff] [blame] | 88 | public byte getMaskLength() { |
| 89 | return maskLength; |
| 90 | } |
| 91 | |
Jian Li | 631e62a | 2016-08-03 22:42:00 +0900 | [diff] [blame] | 92 | @Override |
Jian Li | 10a0906 | 2016-07-26 23:58:50 +0900 | [diff] [blame] | 93 | public LispMapReplyAction getAction() { |
| 94 | return action; |
| 95 | } |
| 96 | |
Jian Li | 631e62a | 2016-08-03 22:42:00 +0900 | [diff] [blame] | 97 | @Override |
Jian Li | 10a0906 | 2016-07-26 23:58:50 +0900 | [diff] [blame] | 98 | public boolean isAuthoritative() { |
| 99 | return authoritative; |
| 100 | } |
| 101 | |
Jian Li | 631e62a | 2016-08-03 22:42:00 +0900 | [diff] [blame] | 102 | @Override |
Jian Li | 10a0906 | 2016-07-26 23:58:50 +0900 | [diff] [blame] | 103 | public short getMapVersionNumber() { |
| 104 | return mapVersionNumber; |
| 105 | } |
| 106 | |
Jian Li | 631e62a | 2016-08-03 22:42:00 +0900 | [diff] [blame] | 107 | @Override |
Jian Li | 10a0906 | 2016-07-26 23:58:50 +0900 | [diff] [blame] | 108 | public LispAfiAddress getEidPrefixAfi() { |
| 109 | return eidPrefixAfi; |
| 110 | } |
| 111 | |
Jian Li | 18f3bce | 2016-08-04 17:36:41 +0900 | [diff] [blame] | 112 | @Override |
Jian Li | 4767190 | 2016-08-11 01:18:18 +0900 | [diff] [blame] | 113 | public List<LispLocatorRecord> getLocators() { |
| 114 | return ImmutableList.copyOf(locatorRecords); |
| 115 | } |
| 116 | |
| 117 | @Override |
Yoonseon Han | ca814bf | 2016-09-12 11:37:48 -0700 | [diff] [blame] | 118 | public void writeTo(ByteBuf byteBuf) throws LispWriterException { |
| 119 | WRITER.writeTo(byteBuf, this); |
Jian Li | 4767190 | 2016-08-11 01:18:18 +0900 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | @Override |
Jian Li | 18f3bce | 2016-08-04 17:36:41 +0900 | [diff] [blame] | 123 | public String toString() { |
| 124 | return toStringHelper(this) |
| 125 | .add("record TTL", recordTtl) |
Jian Li | 18f3bce | 2016-08-04 17:36:41 +0900 | [diff] [blame] | 126 | .add("maskLength", maskLength) |
| 127 | .add("action", action) |
| 128 | .add("authoritative", authoritative) |
| 129 | .add("mapVersionNumber", mapVersionNumber) |
Jian Li | 4767190 | 2016-08-11 01:18:18 +0900 | [diff] [blame] | 130 | .add("EID prefix AFI address", eidPrefixAfi) |
| 131 | .add("locator records", locatorRecords).toString(); |
| 132 | |
Jian Li | 18f3bce | 2016-08-04 17:36:41 +0900 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | @Override |
| 136 | public boolean equals(Object o) { |
| 137 | if (this == o) { |
| 138 | return true; |
| 139 | } |
| 140 | if (o == null || getClass() != o.getClass()) { |
| 141 | return false; |
| 142 | } |
| 143 | DefaultLispMapRecord that = (DefaultLispMapRecord) o; |
| 144 | return Objects.equal(recordTtl, that.recordTtl) && |
Jian Li | 18f3bce | 2016-08-04 17:36:41 +0900 | [diff] [blame] | 145 | Objects.equal(maskLength, that.maskLength) && |
| 146 | Objects.equal(action, that.action) && |
| 147 | Objects.equal(authoritative, that.authoritative) && |
| 148 | Objects.equal(mapVersionNumber, that.mapVersionNumber) && |
Jian Li | 4767190 | 2016-08-11 01:18:18 +0900 | [diff] [blame] | 149 | Objects.equal(eidPrefixAfi, that.eidPrefixAfi) && |
| 150 | Objects.equal(locatorRecords, that.locatorRecords); |
Jian Li | 18f3bce | 2016-08-04 17:36:41 +0900 | [diff] [blame] | 151 | } |
| 152 | |
| 153 | @Override |
| 154 | public int hashCode() { |
Jian Li | 42b3e43 | 2016-08-31 01:05:20 +0900 | [diff] [blame] | 155 | return Objects.hashCode(recordTtl, maskLength, action, |
Jian Li | 4767190 | 2016-08-11 01:18:18 +0900 | [diff] [blame] | 156 | authoritative, mapVersionNumber, eidPrefixAfi, locatorRecords); |
Jian Li | 18f3bce | 2016-08-04 17:36:41 +0900 | [diff] [blame] | 157 | } |
| 158 | |
Jian Li | 10a0906 | 2016-07-26 23:58:50 +0900 | [diff] [blame] | 159 | public static final class DefaultMapRecordBuilder implements MapRecordBuilder { |
| 160 | |
Jian Li | 631e62a | 2016-08-03 22:42:00 +0900 | [diff] [blame] | 161 | private int recordTtl; |
Jian Li | 631e62a | 2016-08-03 22:42:00 +0900 | [diff] [blame] | 162 | private byte maskLength; |
| 163 | private LispMapReplyAction action; |
| 164 | private boolean authoritative; |
| 165 | private short mapVersionNumber; |
| 166 | private LispAfiAddress eidPrefixAfi; |
Jian Li | d4e6370 | 2016-08-30 18:29:20 +0900 | [diff] [blame] | 167 | private List<LispLocatorRecord> locatorRecords = Lists.newArrayList(); |
Jian Li | 631e62a | 2016-08-03 22:42:00 +0900 | [diff] [blame] | 168 | |
Jian Li | 10a0906 | 2016-07-26 23:58:50 +0900 | [diff] [blame] | 169 | @Override |
| 170 | public MapRecordBuilder withRecordTtl(int recordTtl) { |
Jian Li | 631e62a | 2016-08-03 22:42:00 +0900 | [diff] [blame] | 171 | this.recordTtl = recordTtl; |
| 172 | return this; |
Jian Li | 10a0906 | 2016-07-26 23:58:50 +0900 | [diff] [blame] | 173 | } |
| 174 | |
| 175 | @Override |
Jian Li | 10a0906 | 2016-07-26 23:58:50 +0900 | [diff] [blame] | 176 | public MapRecordBuilder withMaskLength(byte maskLength) { |
Jian Li | 631e62a | 2016-08-03 22:42:00 +0900 | [diff] [blame] | 177 | this.maskLength = maskLength; |
| 178 | return this; |
Jian Li | 10a0906 | 2016-07-26 23:58:50 +0900 | [diff] [blame] | 179 | } |
| 180 | |
| 181 | @Override |
| 182 | public MapRecordBuilder withAction(LispMapReplyAction action) { |
Jian Li | 631e62a | 2016-08-03 22:42:00 +0900 | [diff] [blame] | 183 | this.action = action; |
| 184 | return this; |
Jian Li | 10a0906 | 2016-07-26 23:58:50 +0900 | [diff] [blame] | 185 | } |
| 186 | |
| 187 | @Override |
| 188 | public MapRecordBuilder withAuthoritative(boolean authoritative) { |
Jian Li | 631e62a | 2016-08-03 22:42:00 +0900 | [diff] [blame] | 189 | this.authoritative = authoritative; |
| 190 | return this; |
Jian Li | 10a0906 | 2016-07-26 23:58:50 +0900 | [diff] [blame] | 191 | } |
| 192 | |
| 193 | @Override |
| 194 | public MapRecordBuilder withMapVersionNumber(short mapVersionNumber) { |
Jian Li | 631e62a | 2016-08-03 22:42:00 +0900 | [diff] [blame] | 195 | this.mapVersionNumber = mapVersionNumber; |
| 196 | return this; |
Jian Li | 10a0906 | 2016-07-26 23:58:50 +0900 | [diff] [blame] | 197 | } |
| 198 | |
| 199 | @Override |
| 200 | public MapRecordBuilder withEidPrefixAfi(LispAfiAddress prefix) { |
Jian Li | 631e62a | 2016-08-03 22:42:00 +0900 | [diff] [blame] | 201 | this.eidPrefixAfi = prefix; |
| 202 | return this; |
| 203 | } |
| 204 | |
| 205 | @Override |
Jian Li | 4767190 | 2016-08-11 01:18:18 +0900 | [diff] [blame] | 206 | public MapRecordBuilder withLocators(List<LispLocatorRecord> records) { |
Jian Li | e4ba2a4 | 2016-08-29 20:24:15 +0900 | [diff] [blame] | 207 | if (records != null) { |
| 208 | this.locatorRecords = ImmutableList.copyOf(records); |
Jian Li | e4ba2a4 | 2016-08-29 20:24:15 +0900 | [diff] [blame] | 209 | } |
Jian Li | 4767190 | 2016-08-11 01:18:18 +0900 | [diff] [blame] | 210 | return this; |
| 211 | } |
| 212 | |
| 213 | @Override |
Jian Li | 631e62a | 2016-08-03 22:42:00 +0900 | [diff] [blame] | 214 | public LispMapRecord build() { |
Jian Li | e4ba2a4 | 2016-08-29 20:24:15 +0900 | [diff] [blame] | 215 | |
Jian Li | d4e6370 | 2016-08-30 18:29:20 +0900 | [diff] [blame] | 216 | checkNotNull(eidPrefixAfi, "Must specify an EID prefix"); |
Jian Li | e4ba2a4 | 2016-08-29 20:24:15 +0900 | [diff] [blame] | 217 | |
Jian Li | 42b3e43 | 2016-08-31 01:05:20 +0900 | [diff] [blame] | 218 | return new DefaultLispMapRecord(recordTtl, maskLength, action, |
| 219 | authoritative, mapVersionNumber, eidPrefixAfi, locatorRecords); |
Jian Li | 10a0906 | 2016-07-26 23:58:50 +0900 | [diff] [blame] | 220 | } |
| 221 | } |
Jian Li | 26069e2 | 2016-08-10 22:00:52 +0900 | [diff] [blame] | 222 | |
| 223 | /** |
Jian Li | 4767190 | 2016-08-11 01:18:18 +0900 | [diff] [blame] | 224 | * A LISP message reader for MapRecord portion. |
Jian Li | 26069e2 | 2016-08-10 22:00:52 +0900 | [diff] [blame] | 225 | */ |
Jian Li | 4767190 | 2016-08-11 01:18:18 +0900 | [diff] [blame] | 226 | public static final class MapRecordReader implements LispMessageReader<LispMapRecord> { |
| 227 | |
| 228 | private static final int AUTHORITATIVE_INDEX = 4; |
| 229 | private static final int RESERVED_SKIP_LENGTH = 1; |
Jian Li | 26069e2 | 2016-08-10 22:00:52 +0900 | [diff] [blame] | 230 | |
Jian Li | e4ba2a4 | 2016-08-29 20:24:15 +0900 | [diff] [blame] | 231 | private static final int REPLY_ACTION_SHIFT_BIT = 5; |
| 232 | |
Jian Li | 26069e2 | 2016-08-10 22:00:52 +0900 | [diff] [blame] | 233 | @Override |
Jian Li | a7b394d | 2016-08-21 23:11:46 +0900 | [diff] [blame] | 234 | public LispMapRecord readFrom(ByteBuf byteBuf) throws LispParseError, LispReaderException { |
Jian Li | 4767190 | 2016-08-11 01:18:18 +0900 | [diff] [blame] | 235 | |
| 236 | // Record TTL -> 32 bits |
| 237 | int recordTtl = byteBuf.readInt(); |
| 238 | |
| 239 | // Locator count -> 8 bits |
Jian Li | e4ba2a4 | 2016-08-29 20:24:15 +0900 | [diff] [blame] | 240 | int locatorCount = byteBuf.readUnsignedByte(); |
Jian Li | 4767190 | 2016-08-11 01:18:18 +0900 | [diff] [blame] | 241 | |
| 242 | // EID mask length -> 8 bits |
| 243 | byte maskLength = (byte) byteBuf.readUnsignedByte(); |
| 244 | |
Jian Li | e4ba2a4 | 2016-08-29 20:24:15 +0900 | [diff] [blame] | 245 | byte actionWithFlag = (byte) byteBuf.readUnsignedByte(); |
Jian Li | 4767190 | 2016-08-11 01:18:18 +0900 | [diff] [blame] | 246 | |
Jian Li | e4ba2a4 | 2016-08-29 20:24:15 +0900 | [diff] [blame] | 247 | // action -> 3 bit |
| 248 | int actionByte = actionWithFlag >> REPLY_ACTION_SHIFT_BIT; |
| 249 | LispMapReplyAction action = LispMapReplyAction.valueOf(actionByte); |
| 250 | if (action == null) { |
| 251 | action = LispMapReplyAction.NoAction; |
| 252 | } |
Jian Li | 4767190 | 2016-08-11 01:18:18 +0900 | [diff] [blame] | 253 | |
| 254 | // authoritative flag -> 1 bit |
Jian Li | e4ba2a4 | 2016-08-29 20:24:15 +0900 | [diff] [blame] | 255 | boolean authoritative = ByteOperator.getBit((byte) (actionWithFlag >> AUTHORITATIVE_INDEX), 0); |
Jian Li | 4767190 | 2016-08-11 01:18:18 +0900 | [diff] [blame] | 256 | |
| 257 | // let's skip the reserved field |
| 258 | byteBuf.skipBytes(RESERVED_SKIP_LENGTH); |
| 259 | |
| 260 | // Map version number -> 12 bits, we treat Rsvd field is all zero |
| 261 | short mapVersionNumber = (short) byteBuf.readUnsignedShort(); |
| 262 | |
Jian Li | a7b394d | 2016-08-21 23:11:46 +0900 | [diff] [blame] | 263 | LispAfiAddress eidPrefixAfi = new LispAfiAddress.AfiAddressReader().readFrom(byteBuf); |
Jian Li | 4767190 | 2016-08-11 01:18:18 +0900 | [diff] [blame] | 264 | |
| 265 | List<LispLocatorRecord> locators = Lists.newArrayList(); |
| 266 | for (int i = 0; i < locatorCount; i++) { |
Jian Li | 5e505c6 | 2016-12-05 02:44:24 +0900 | [diff] [blame] | 267 | locators.add(new LocatorRecordReader().readFrom(byteBuf)); |
Jian Li | 4767190 | 2016-08-11 01:18:18 +0900 | [diff] [blame] | 268 | } |
| 269 | |
| 270 | return new DefaultMapRecordBuilder() |
| 271 | .withRecordTtl(recordTtl) |
Jian Li | 4767190 | 2016-08-11 01:18:18 +0900 | [diff] [blame] | 272 | .withMaskLength(maskLength) |
Jian Li | e4ba2a4 | 2016-08-29 20:24:15 +0900 | [diff] [blame] | 273 | .withAction(action) |
Jian Li | 4767190 | 2016-08-11 01:18:18 +0900 | [diff] [blame] | 274 | .withAuthoritative(authoritative) |
| 275 | .withMapVersionNumber(mapVersionNumber) |
| 276 | .withLocators(locators) |
Jian Li | a7b394d | 2016-08-21 23:11:46 +0900 | [diff] [blame] | 277 | .withEidPrefixAfi(eidPrefixAfi) |
Jian Li | 4767190 | 2016-08-11 01:18:18 +0900 | [diff] [blame] | 278 | .build(); |
Jian Li | 26069e2 | 2016-08-10 22:00:52 +0900 | [diff] [blame] | 279 | } |
| 280 | } |
Jian Li | edc5db1 | 2016-08-23 17:30:19 +0900 | [diff] [blame] | 281 | |
| 282 | /** |
| 283 | * A LISP message writer for MapRecord portion. |
| 284 | */ |
| 285 | public static final class MapRecordWriter implements LispMessageWriter<LispMapRecord> { |
| 286 | |
| 287 | private static final int REPLY_ACTION_SHIFT_BIT = 5; |
| 288 | private static final int AUTHORITATIVE_FLAG_SHIFT_BIT = 4; |
| 289 | |
| 290 | private static final int ENABLE_BIT = 1; |
| 291 | private static final int DISABLE_BIT = 0; |
| 292 | |
| 293 | @Override |
| 294 | public void writeTo(ByteBuf byteBuf, LispMapRecord message) throws LispWriterException { |
| 295 | |
| 296 | // record TTL |
| 297 | byteBuf.writeInt(message.getRecordTtl()); |
| 298 | |
| 299 | // locator count |
Jian Li | 42b3e43 | 2016-08-31 01:05:20 +0900 | [diff] [blame] | 300 | byteBuf.writeByte((byte) message.getLocators().size()); |
Jian Li | edc5db1 | 2016-08-23 17:30:19 +0900 | [diff] [blame] | 301 | |
| 302 | // EID mask length |
| 303 | byteBuf.writeByte(message.getMaskLength()); |
| 304 | |
| 305 | // reply action |
| 306 | byte action = (byte) (message.getAction().getAction() << REPLY_ACTION_SHIFT_BIT); |
| 307 | |
| 308 | // authoritative bit |
| 309 | byte authoritative = DISABLE_BIT; |
| 310 | if (message.isAuthoritative()) { |
Jian Li | e4ba2a4 | 2016-08-29 20:24:15 +0900 | [diff] [blame] | 311 | authoritative = ENABLE_BIT << AUTHORITATIVE_FLAG_SHIFT_BIT; |
Jian Li | edc5db1 | 2016-08-23 17:30:19 +0900 | [diff] [blame] | 312 | } |
Jian Li | edc5db1 | 2016-08-23 17:30:19 +0900 | [diff] [blame] | 313 | |
| 314 | byteBuf.writeByte((byte) (action + authoritative)); |
| 315 | |
| 316 | // fill zero into reserved field |
| 317 | byteBuf.writeByte((short) 0); |
| 318 | |
| 319 | // map version number |
| 320 | byteBuf.writeShort(message.getMapVersionNumber()); |
| 321 | |
| 322 | // EID prefix AFI with EID prefix |
| 323 | AfiAddressWriter afiAddressWriter = new AfiAddressWriter(); |
| 324 | afiAddressWriter.writeTo(byteBuf, message.getEidPrefixAfi()); |
| 325 | |
| 326 | // serialize locator |
| 327 | LocatorRecordWriter recordWriter = new LocatorRecordWriter(); |
| 328 | List<LispLocatorRecord> locators = message.getLocators(); |
| 329 | for (int i = 0; i < locators.size(); i++) { |
| 330 | recordWriter.writeTo(byteBuf, locators.get(i)); |
| 331 | } |
| 332 | } |
| 333 | } |
Jian Li | 10a0906 | 2016-07-26 23:58:50 +0900 | [diff] [blame] | 334 | } |