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