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