Jian Li | 451175e | 2016-07-19 23:22:20 +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 | 20850d3 | 2016-08-04 02:15:57 +0900 | [diff] [blame] | 18 | import com.google.common.base.Objects; |
Jian Li | f59c0ad | 2016-08-02 18:11:30 +0900 | [diff] [blame] | 19 | import com.google.common.collect.ImmutableList; |
| 20 | import com.google.common.collect.Lists; |
Jian Li | 451175e | 2016-07-19 23:22:20 +0900 | [diff] [blame] | 21 | import io.netty.buffer.ByteBuf; |
Jian Li | f59c0ad | 2016-08-02 18:11:30 +0900 | [diff] [blame] | 22 | import org.onlab.util.ImmutableByteSequence; |
Jian Li | 26069e2 | 2016-08-10 22:00:52 +0900 | [diff] [blame] | 23 | import org.onosproject.lisp.msg.exceptions.LispParseError; |
Jian Li | a7b394d | 2016-08-21 23:11:46 +0900 | [diff] [blame] | 24 | import org.onosproject.lisp.msg.exceptions.LispReaderException; |
Jian Li | edc5db1 | 2016-08-23 17:30:19 +0900 | [diff] [blame] | 25 | import org.onosproject.lisp.msg.exceptions.LispWriterException; |
Jian Li | 451175e | 2016-07-19 23:22:20 +0900 | [diff] [blame] | 26 | |
Jian Li | edc5db1 | 2016-08-23 17:30:19 +0900 | [diff] [blame] | 27 | import java.util.Arrays; |
Jian Li | 719b3bf | 2016-07-22 00:38:29 +0900 | [diff] [blame] | 28 | import java.util.List; |
| 29 | |
Jian Li | 20850d3 | 2016-08-04 02:15:57 +0900 | [diff] [blame] | 30 | import static com.google.common.base.MoreObjects.toStringHelper; |
Jian Li | edc5db1 | 2016-08-23 17:30:19 +0900 | [diff] [blame] | 31 | import static org.onosproject.lisp.msg.protocols.DefaultLispMapRecord.MapRecordWriter; |
Jian Li | 20850d3 | 2016-08-04 02:15:57 +0900 | [diff] [blame] | 32 | |
Jian Li | 451175e | 2016-07-19 23:22:20 +0900 | [diff] [blame] | 33 | /** |
| 34 | * Default LISP map notify message class. |
| 35 | */ |
Jian Li | f59c0ad | 2016-08-02 18:11:30 +0900 | [diff] [blame] | 36 | public final class DefaultLispMapNotify implements LispMapNotify { |
| 37 | |
| 38 | private final long nonce; |
| 39 | private final short keyId; |
Jian Li | edc5db1 | 2016-08-23 17:30:19 +0900 | [diff] [blame] | 40 | private final short authDataLength; |
Jian Li | f59c0ad | 2016-08-02 18:11:30 +0900 | [diff] [blame] | 41 | private final byte[] authenticationData; |
Jian Li | f59c0ad | 2016-08-02 18:11:30 +0900 | [diff] [blame] | 42 | private final List<LispMapRecord> mapRecords; |
| 43 | |
Yoonseon Han | ca814bf | 2016-09-12 11:37:48 -0700 | [diff] [blame^] | 44 | static final NotifyWriter WRITER; |
| 45 | static { |
| 46 | WRITER = new NotifyWriter(); |
| 47 | } |
| 48 | |
Jian Li | f59c0ad | 2016-08-02 18:11:30 +0900 | [diff] [blame] | 49 | /** |
| 50 | * A private constructor that protects object instantiation from external. |
| 51 | * |
| 52 | * @param nonce nonce |
| 53 | * @param keyId key identifier |
| 54 | * @param authenticationData authentication data |
Jian Li | f59c0ad | 2016-08-02 18:11:30 +0900 | [diff] [blame] | 55 | * @param mapRecords a collection of map records |
| 56 | */ |
Jian Li | edc5db1 | 2016-08-23 17:30:19 +0900 | [diff] [blame] | 57 | private DefaultLispMapNotify(long nonce, short keyId, short authDataLength, |
Jian Li | 42b3e43 | 2016-08-31 01:05:20 +0900 | [diff] [blame] | 58 | byte[] authenticationData, List<LispMapRecord> mapRecords) { |
Jian Li | f59c0ad | 2016-08-02 18:11:30 +0900 | [diff] [blame] | 59 | this.nonce = nonce; |
| 60 | this.keyId = keyId; |
Jian Li | edc5db1 | 2016-08-23 17:30:19 +0900 | [diff] [blame] | 61 | this.authDataLength = authDataLength; |
Jian Li | f59c0ad | 2016-08-02 18:11:30 +0900 | [diff] [blame] | 62 | this.authenticationData = authenticationData; |
Jian Li | f59c0ad | 2016-08-02 18:11:30 +0900 | [diff] [blame] | 63 | this.mapRecords = mapRecords; |
| 64 | } |
Jian Li | 451175e | 2016-07-19 23:22:20 +0900 | [diff] [blame] | 65 | |
| 66 | @Override |
| 67 | public LispType getType() { |
Jian Li | f59c0ad | 2016-08-02 18:11:30 +0900 | [diff] [blame] | 68 | return LispType.LISP_MAP_NOTIFY; |
Jian Li | 451175e | 2016-07-19 23:22:20 +0900 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | @Override |
Yoonseon Han | ca814bf | 2016-09-12 11:37:48 -0700 | [diff] [blame^] | 72 | public void writeTo(ByteBuf byteBuf) throws LispWriterException { |
| 73 | WRITER.writeTo(byteBuf, this); |
Jian Li | 451175e | 2016-07-19 23:22:20 +0900 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | @Override |
| 77 | public Builder createBuilder() { |
Jian Li | 525fded | 2016-08-04 01:15:33 +0900 | [diff] [blame] | 78 | return new DefaultNotifyBuilder(); |
Jian Li | 451175e | 2016-07-19 23:22:20 +0900 | [diff] [blame] | 79 | } |
Jian Li | 719b3bf | 2016-07-22 00:38:29 +0900 | [diff] [blame] | 80 | |
| 81 | @Override |
| 82 | public long getNonce() { |
Jian Li | edc5db1 | 2016-08-23 17:30:19 +0900 | [diff] [blame] | 83 | return nonce; |
Jian Li | 719b3bf | 2016-07-22 00:38:29 +0900 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | @Override |
Jian Li | 42b3e43 | 2016-08-31 01:05:20 +0900 | [diff] [blame] | 87 | public int getRecordCount() { |
| 88 | return mapRecords.size(); |
Jian Li | 719b3bf | 2016-07-22 00:38:29 +0900 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | @Override |
| 92 | public short getKeyId() { |
Jian Li | edc5db1 | 2016-08-23 17:30:19 +0900 | [diff] [blame] | 93 | return keyId; |
| 94 | } |
| 95 | |
| 96 | @Override |
| 97 | public short getAuthDataLength() { |
| 98 | return authDataLength; |
Jian Li | 719b3bf | 2016-07-22 00:38:29 +0900 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | @Override |
| 102 | public byte[] getAuthenticationData() { |
Jian Li | e4ba2a4 | 2016-08-29 20:24:15 +0900 | [diff] [blame] | 103 | if (authenticationData != null && authenticationData.length != 0) { |
| 104 | return ImmutableByteSequence.copyFrom(authenticationData).asArray(); |
| 105 | } else { |
| 106 | return new byte[0]; |
| 107 | } |
Jian Li | 719b3bf | 2016-07-22 00:38:29 +0900 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | @Override |
Jian Li | edc5db1 | 2016-08-23 17:30:19 +0900 | [diff] [blame] | 111 | public List<LispMapRecord> getMapRecords() { |
Jian Li | f59c0ad | 2016-08-02 18:11:30 +0900 | [diff] [blame] | 112 | return ImmutableList.copyOf(mapRecords); |
Jian Li | 719b3bf | 2016-07-22 00:38:29 +0900 | [diff] [blame] | 113 | } |
| 114 | |
Jian Li | 20850d3 | 2016-08-04 02:15:57 +0900 | [diff] [blame] | 115 | @Override |
| 116 | public String toString() { |
| 117 | return toStringHelper(this) |
| 118 | .add("type", getType()) |
| 119 | .add("nonce", nonce) |
Jian Li | 20850d3 | 2016-08-04 02:15:57 +0900 | [diff] [blame] | 120 | .add("keyId", keyId) |
Jian Li | edc5db1 | 2016-08-23 17:30:19 +0900 | [diff] [blame] | 121 | .add("authentication data length", authDataLength) |
| 122 | .add("authentication data", authenticationData) |
Jian Li | 20850d3 | 2016-08-04 02:15:57 +0900 | [diff] [blame] | 123 | .add("mapRecords", mapRecords).toString(); |
| 124 | } |
| 125 | |
| 126 | @Override |
| 127 | public boolean equals(Object o) { |
| 128 | if (this == o) { |
| 129 | return true; |
| 130 | } |
| 131 | if (o == null || getClass() != o.getClass()) { |
| 132 | return false; |
| 133 | } |
| 134 | DefaultLispMapNotify that = (DefaultLispMapNotify) o; |
| 135 | return Objects.equal(nonce, that.nonce) && |
Jian Li | 20850d3 | 2016-08-04 02:15:57 +0900 | [diff] [blame] | 136 | Objects.equal(keyId, that.keyId) && |
Jian Li | edc5db1 | 2016-08-23 17:30:19 +0900 | [diff] [blame] | 137 | Objects.equal(authDataLength, that.authDataLength) && |
Jian Li | e4ba2a4 | 2016-08-29 20:24:15 +0900 | [diff] [blame] | 138 | Arrays.equals(authenticationData, that.authenticationData); |
Jian Li | 20850d3 | 2016-08-04 02:15:57 +0900 | [diff] [blame] | 139 | } |
| 140 | |
| 141 | @Override |
| 142 | public int hashCode() { |
Jian Li | 42b3e43 | 2016-08-31 01:05:20 +0900 | [diff] [blame] | 143 | return Objects.hashCode(nonce, keyId, authDataLength) + |
Jian Li | e4ba2a4 | 2016-08-29 20:24:15 +0900 | [diff] [blame] | 144 | Arrays.hashCode(authenticationData); |
Jian Li | 20850d3 | 2016-08-04 02:15:57 +0900 | [diff] [blame] | 145 | } |
| 146 | |
Jian Li | 719b3bf | 2016-07-22 00:38:29 +0900 | [diff] [blame] | 147 | public static final class DefaultNotifyBuilder implements NotifyBuilder { |
| 148 | |
Jian Li | f59c0ad | 2016-08-02 18:11:30 +0900 | [diff] [blame] | 149 | private long nonce; |
| 150 | private short keyId; |
Jian Li | edc5db1 | 2016-08-23 17:30:19 +0900 | [diff] [blame] | 151 | private short authDataLength; |
Jian Li | f59c0ad | 2016-08-02 18:11:30 +0900 | [diff] [blame] | 152 | private byte[] authenticationData; |
Jian Li | d4e6370 | 2016-08-30 18:29:20 +0900 | [diff] [blame] | 153 | private List<LispMapRecord> mapRecords = Lists.newArrayList(); |
Jian Li | 719b3bf | 2016-07-22 00:38:29 +0900 | [diff] [blame] | 154 | |
| 155 | @Override |
| 156 | public LispType getType() { |
Jian Li | f59c0ad | 2016-08-02 18:11:30 +0900 | [diff] [blame] | 157 | return LispType.LISP_MAP_NOTIFY; |
Jian Li | 719b3bf | 2016-07-22 00:38:29 +0900 | [diff] [blame] | 158 | } |
| 159 | |
| 160 | @Override |
| 161 | public NotifyBuilder withNonce(long nonce) { |
Jian Li | f59c0ad | 2016-08-02 18:11:30 +0900 | [diff] [blame] | 162 | this.nonce = nonce; |
| 163 | return this; |
Jian Li | 719b3bf | 2016-07-22 00:38:29 +0900 | [diff] [blame] | 164 | } |
| 165 | |
| 166 | @Override |
Jian Li | 719b3bf | 2016-07-22 00:38:29 +0900 | [diff] [blame] | 167 | public NotifyBuilder withKeyId(short keyId) { |
Jian Li | f59c0ad | 2016-08-02 18:11:30 +0900 | [diff] [blame] | 168 | this.keyId = keyId; |
| 169 | return this; |
Jian Li | 719b3bf | 2016-07-22 00:38:29 +0900 | [diff] [blame] | 170 | } |
| 171 | |
| 172 | @Override |
Jian Li | edc5db1 | 2016-08-23 17:30:19 +0900 | [diff] [blame] | 173 | public NotifyBuilder withAuthDataLength(short authDataLength) { |
| 174 | this.authDataLength = authDataLength; |
| 175 | return this; |
| 176 | } |
| 177 | |
| 178 | @Override |
Jian Li | 719b3bf | 2016-07-22 00:38:29 +0900 | [diff] [blame] | 179 | public NotifyBuilder withAuthenticationData(byte[] authenticationData) { |
Jian Li | e4ba2a4 | 2016-08-29 20:24:15 +0900 | [diff] [blame] | 180 | if (authenticationData != null) { |
| 181 | this.authenticationData = authenticationData; |
| 182 | } else { |
| 183 | this.authenticationData = new byte[0]; |
| 184 | } |
Jian Li | f59c0ad | 2016-08-02 18:11:30 +0900 | [diff] [blame] | 185 | return this; |
Jian Li | 719b3bf | 2016-07-22 00:38:29 +0900 | [diff] [blame] | 186 | } |
| 187 | |
| 188 | @Override |
Jian Li | 4767190 | 2016-08-11 01:18:18 +0900 | [diff] [blame] | 189 | public NotifyBuilder withMapRecords(List<LispMapRecord> mapRecords) { |
Jian Li | e4ba2a4 | 2016-08-29 20:24:15 +0900 | [diff] [blame] | 190 | if (mapRecords != null) { |
| 191 | this.mapRecords = ImmutableList.copyOf(mapRecords); |
Jian Li | e4ba2a4 | 2016-08-29 20:24:15 +0900 | [diff] [blame] | 192 | } |
Jian Li | f59c0ad | 2016-08-02 18:11:30 +0900 | [diff] [blame] | 193 | return this; |
| 194 | } |
| 195 | |
| 196 | @Override |
Jian Li | 525fded | 2016-08-04 01:15:33 +0900 | [diff] [blame] | 197 | public LispMapNotify build() { |
Jian Li | e4ba2a4 | 2016-08-29 20:24:15 +0900 | [diff] [blame] | 198 | |
| 199 | if (authenticationData == null) { |
| 200 | authenticationData = new byte[0]; |
| 201 | } |
| 202 | |
Jian Li | edc5db1 | 2016-08-23 17:30:19 +0900 | [diff] [blame] | 203 | return new DefaultLispMapNotify(nonce, keyId, authDataLength, |
Jian Li | 42b3e43 | 2016-08-31 01:05:20 +0900 | [diff] [blame] | 204 | authenticationData, mapRecords); |
Jian Li | 719b3bf | 2016-07-22 00:38:29 +0900 | [diff] [blame] | 205 | } |
| 206 | } |
Jian Li | 26069e2 | 2016-08-10 22:00:52 +0900 | [diff] [blame] | 207 | |
| 208 | /** |
Jian Li | edc5db1 | 2016-08-23 17:30:19 +0900 | [diff] [blame] | 209 | * A LISP message reader for MapNotify message. |
Jian Li | 26069e2 | 2016-08-10 22:00:52 +0900 | [diff] [blame] | 210 | */ |
Jian Li | edc5db1 | 2016-08-23 17:30:19 +0900 | [diff] [blame] | 211 | public static final class NotifyReader implements LispMessageReader<LispMapNotify> { |
Jian Li | 26069e2 | 2016-08-10 22:00:52 +0900 | [diff] [blame] | 212 | |
Jian Li | 4767190 | 2016-08-11 01:18:18 +0900 | [diff] [blame] | 213 | private static final int RESERVED_SKIP_LENGTH = 3; |
| 214 | |
Jian Li | 26069e2 | 2016-08-10 22:00:52 +0900 | [diff] [blame] | 215 | @Override |
Jian Li | a7b394d | 2016-08-21 23:11:46 +0900 | [diff] [blame] | 216 | public LispMapNotify readFrom(ByteBuf byteBuf) throws LispParseError, LispReaderException { |
Jian Li | 4767190 | 2016-08-11 01:18:18 +0900 | [diff] [blame] | 217 | |
| 218 | if (byteBuf.readerIndex() != 0) { |
| 219 | return null; |
| 220 | } |
| 221 | |
| 222 | // skip first three bytes as they represent type and reserved fields |
| 223 | byteBuf.skipBytes(RESERVED_SKIP_LENGTH); |
| 224 | |
| 225 | // record count -> 8 bits |
| 226 | byte recordCount = (byte) byteBuf.readUnsignedByte(); |
| 227 | |
| 228 | // nonce -> 64 bits |
| 229 | long nonce = byteBuf.readLong(); |
| 230 | |
| 231 | // keyId -> 16 bits |
| 232 | short keyId = byteBuf.readShort(); |
| 233 | |
| 234 | // authenticationDataLength -> 16 bits |
| 235 | short authLength = byteBuf.readShort(); |
| 236 | |
| 237 | // authenticationData -> depends on the authenticationDataLength |
| 238 | byte[] authData = new byte[authLength]; |
| 239 | byteBuf.readBytes(authData); |
| 240 | |
| 241 | List<LispMapRecord> mapRecords = Lists.newArrayList(); |
| 242 | for (int i = 0; i < recordCount; i++) { |
| 243 | mapRecords.add(new DefaultLispMapRecord.MapRecordReader().readFrom(byteBuf)); |
| 244 | } |
| 245 | |
| 246 | return new DefaultNotifyBuilder() |
Jian Li | 4767190 | 2016-08-11 01:18:18 +0900 | [diff] [blame] | 247 | .withNonce(nonce) |
| 248 | .withKeyId(keyId) |
Jian Li | edc5db1 | 2016-08-23 17:30:19 +0900 | [diff] [blame] | 249 | .withAuthDataLength(authLength) |
Jian Li | 4767190 | 2016-08-11 01:18:18 +0900 | [diff] [blame] | 250 | .withAuthenticationData(authData) |
| 251 | .withMapRecords(mapRecords) |
| 252 | .build(); |
Jian Li | 26069e2 | 2016-08-10 22:00:52 +0900 | [diff] [blame] | 253 | } |
| 254 | } |
Jian Li | edc5db1 | 2016-08-23 17:30:19 +0900 | [diff] [blame] | 255 | |
| 256 | /** |
| 257 | * A LISP message reader for MapNotify message. |
| 258 | */ |
| 259 | public static final class NotifyWriter implements LispMessageWriter<LispMapNotify> { |
| 260 | |
Jian Li | edc5db1 | 2016-08-23 17:30:19 +0900 | [diff] [blame] | 261 | private static final int NOTIFY_SHIFT_BIT = 4; |
| 262 | |
| 263 | private static final int UNUSED_ZERO = 0; |
| 264 | |
| 265 | @Override |
| 266 | public void writeTo(ByteBuf byteBuf, LispMapNotify message) throws LispWriterException { |
| 267 | |
| 268 | // specify LISP message type |
Jian Li | cbc57e3 | 2016-09-14 09:06:54 +0900 | [diff] [blame] | 269 | byte msgType = (byte) (LispType.LISP_MAP_NOTIFY.getTypeCode() << NOTIFY_SHIFT_BIT); |
Jian Li | edc5db1 | 2016-08-23 17:30:19 +0900 | [diff] [blame] | 270 | byteBuf.writeByte(msgType); |
| 271 | |
| 272 | // reserved field |
| 273 | byteBuf.writeShort((short) UNUSED_ZERO); |
| 274 | |
| 275 | // record count |
Jian Li | 42b3e43 | 2016-08-31 01:05:20 +0900 | [diff] [blame] | 276 | byteBuf.writeByte(message.getMapRecords().size()); |
Jian Li | edc5db1 | 2016-08-23 17:30:19 +0900 | [diff] [blame] | 277 | |
| 278 | // nonce |
| 279 | byteBuf.writeLong(message.getNonce()); |
| 280 | |
| 281 | // keyId |
| 282 | byteBuf.writeShort(message.getKeyId()); |
| 283 | |
Jian Li | f8c2d4a | 2016-09-15 02:33:12 +0900 | [diff] [blame] | 284 | // authentication data and its length |
| 285 | if (message.getAuthenticationData() == null) { |
| 286 | byteBuf.writeShort((short) 0); |
| 287 | } else { |
| 288 | byteBuf.writeShort(message.getAuthenticationData().length); |
| 289 | byteBuf.writeBytes(message.getAuthenticationData()); |
Jian Li | edc5db1 | 2016-08-23 17:30:19 +0900 | [diff] [blame] | 290 | } |
| 291 | |
Jian Li | edc5db1 | 2016-08-23 17:30:19 +0900 | [diff] [blame] | 292 | // serialize map records |
| 293 | MapRecordWriter writer = new MapRecordWriter(); |
| 294 | List<LispMapRecord> records = message.getMapRecords(); |
| 295 | |
| 296 | for (int i = 0; i < records.size(); i++) { |
| 297 | writer.writeTo(byteBuf, records.get(i)); |
| 298 | } |
| 299 | } |
| 300 | } |
Jian Li | 451175e | 2016-07-19 23:22:20 +0900 | [diff] [blame] | 301 | } |