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 | edc5db1 | 2016-08-23 17:30:19 +0900 | [diff] [blame] | 102 | return ImmutableByteSequence.copyFrom(authenticationData).asArray(); |
Jian Li | 719b3bf | 2016-07-22 00:38:29 +0900 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | @Override |
Jian Li | edc5db1 | 2016-08-23 17:30:19 +0900 | [diff] [blame] | 106 | public List<LispMapRecord> getMapRecords() { |
Jian Li | f59c0ad | 2016-08-02 18:11:30 +0900 | [diff] [blame] | 107 | return ImmutableList.copyOf(mapRecords); |
Jian Li | 719b3bf | 2016-07-22 00:38:29 +0900 | [diff] [blame] | 108 | } |
| 109 | |
Jian Li | 20850d3 | 2016-08-04 02:15:57 +0900 | [diff] [blame] | 110 | @Override |
| 111 | public String toString() { |
| 112 | return toStringHelper(this) |
| 113 | .add("type", getType()) |
| 114 | .add("nonce", nonce) |
| 115 | .add("recordCount", recordCount) |
| 116 | .add("keyId", keyId) |
Jian Li | edc5db1 | 2016-08-23 17:30:19 +0900 | [diff] [blame] | 117 | .add("authentication data length", authDataLength) |
| 118 | .add("authentication data", authenticationData) |
Jian Li | 20850d3 | 2016-08-04 02:15:57 +0900 | [diff] [blame] | 119 | .add("mapRecords", mapRecords).toString(); |
| 120 | } |
| 121 | |
| 122 | @Override |
| 123 | public boolean equals(Object o) { |
| 124 | if (this == o) { |
| 125 | return true; |
| 126 | } |
| 127 | if (o == null || getClass() != o.getClass()) { |
| 128 | return false; |
| 129 | } |
| 130 | DefaultLispMapNotify that = (DefaultLispMapNotify) o; |
| 131 | return Objects.equal(nonce, that.nonce) && |
| 132 | Objects.equal(recordCount, that.recordCount) && |
| 133 | Objects.equal(keyId, that.keyId) && |
Jian Li | edc5db1 | 2016-08-23 17:30:19 +0900 | [diff] [blame] | 134 | Objects.equal(authDataLength, that.authDataLength) && |
Jian Li | 20850d3 | 2016-08-04 02:15:57 +0900 | [diff] [blame] | 135 | Objects.equal(authenticationData, that.authenticationData); |
| 136 | } |
| 137 | |
| 138 | @Override |
| 139 | public int hashCode() { |
Jian Li | edc5db1 | 2016-08-23 17:30:19 +0900 | [diff] [blame] | 140 | return Objects.hashCode(nonce, recordCount, keyId, authDataLength, authenticationData); |
Jian Li | 20850d3 | 2016-08-04 02:15:57 +0900 | [diff] [blame] | 141 | } |
| 142 | |
Jian Li | 719b3bf | 2016-07-22 00:38:29 +0900 | [diff] [blame] | 143 | public static final class DefaultNotifyBuilder implements NotifyBuilder { |
| 144 | |
Jian Li | f59c0ad | 2016-08-02 18:11:30 +0900 | [diff] [blame] | 145 | private long nonce; |
| 146 | private short keyId; |
Jian Li | edc5db1 | 2016-08-23 17:30:19 +0900 | [diff] [blame] | 147 | private short authDataLength; |
Jian Li | f59c0ad | 2016-08-02 18:11:30 +0900 | [diff] [blame] | 148 | private byte[] authenticationData; |
| 149 | private byte recordCount; |
Jian Li | 4767190 | 2016-08-11 01:18:18 +0900 | [diff] [blame] | 150 | private List<LispMapRecord> mapRecords; |
Jian Li | 719b3bf | 2016-07-22 00:38:29 +0900 | [diff] [blame] | 151 | |
| 152 | @Override |
| 153 | public LispType getType() { |
Jian Li | f59c0ad | 2016-08-02 18:11:30 +0900 | [diff] [blame] | 154 | return LispType.LISP_MAP_NOTIFY; |
Jian Li | 719b3bf | 2016-07-22 00:38:29 +0900 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | @Override |
| 158 | public NotifyBuilder withNonce(long nonce) { |
Jian Li | f59c0ad | 2016-08-02 18:11:30 +0900 | [diff] [blame] | 159 | this.nonce = nonce; |
| 160 | return this; |
Jian Li | 719b3bf | 2016-07-22 00:38:29 +0900 | [diff] [blame] | 161 | } |
| 162 | |
| 163 | @Override |
| 164 | public NotifyBuilder withRecordCount(byte recordCount) { |
Jian Li | f59c0ad | 2016-08-02 18:11:30 +0900 | [diff] [blame] | 165 | this.recordCount = recordCount; |
| 166 | return this; |
Jian Li | 719b3bf | 2016-07-22 00:38:29 +0900 | [diff] [blame] | 167 | } |
| 168 | |
| 169 | @Override |
| 170 | public NotifyBuilder withKeyId(short keyId) { |
Jian Li | f59c0ad | 2016-08-02 18:11:30 +0900 | [diff] [blame] | 171 | this.keyId = keyId; |
| 172 | return this; |
Jian Li | 719b3bf | 2016-07-22 00:38:29 +0900 | [diff] [blame] | 173 | } |
| 174 | |
| 175 | @Override |
Jian Li | edc5db1 | 2016-08-23 17:30:19 +0900 | [diff] [blame] | 176 | public NotifyBuilder withAuthDataLength(short authDataLength) { |
| 177 | this.authDataLength = authDataLength; |
| 178 | return this; |
| 179 | } |
| 180 | |
| 181 | @Override |
Jian Li | 719b3bf | 2016-07-22 00:38:29 +0900 | [diff] [blame] | 182 | public NotifyBuilder withAuthenticationData(byte[] authenticationData) { |
Jian Li | f59c0ad | 2016-08-02 18:11:30 +0900 | [diff] [blame] | 183 | this.authenticationData = authenticationData; |
| 184 | return this; |
Jian Li | 719b3bf | 2016-07-22 00:38:29 +0900 | [diff] [blame] | 185 | } |
| 186 | |
| 187 | @Override |
Jian Li | 4767190 | 2016-08-11 01:18:18 +0900 | [diff] [blame] | 188 | public NotifyBuilder withMapRecords(List<LispMapRecord> mapRecords) { |
| 189 | this.mapRecords = ImmutableList.copyOf(mapRecords); |
Jian Li | f59c0ad | 2016-08-02 18:11:30 +0900 | [diff] [blame] | 190 | return this; |
| 191 | } |
| 192 | |
| 193 | @Override |
Jian Li | 525fded | 2016-08-04 01:15:33 +0900 | [diff] [blame] | 194 | public LispMapNotify build() { |
Jian Li | edc5db1 | 2016-08-23 17:30:19 +0900 | [diff] [blame] | 195 | return new DefaultLispMapNotify(nonce, keyId, authDataLength, |
| 196 | authenticationData, recordCount, mapRecords); |
Jian Li | 719b3bf | 2016-07-22 00:38:29 +0900 | [diff] [blame] | 197 | } |
| 198 | } |
Jian Li | 26069e2 | 2016-08-10 22:00:52 +0900 | [diff] [blame] | 199 | |
| 200 | /** |
Jian Li | edc5db1 | 2016-08-23 17:30:19 +0900 | [diff] [blame] | 201 | * A LISP message reader for MapNotify message. |
Jian Li | 26069e2 | 2016-08-10 22:00:52 +0900 | [diff] [blame] | 202 | */ |
Jian Li | edc5db1 | 2016-08-23 17:30:19 +0900 | [diff] [blame] | 203 | public static final class NotifyReader implements LispMessageReader<LispMapNotify> { |
Jian Li | 26069e2 | 2016-08-10 22:00:52 +0900 | [diff] [blame] | 204 | |
Jian Li | 4767190 | 2016-08-11 01:18:18 +0900 | [diff] [blame] | 205 | private static final int RESERVED_SKIP_LENGTH = 3; |
| 206 | |
Jian Li | 26069e2 | 2016-08-10 22:00:52 +0900 | [diff] [blame] | 207 | @Override |
Jian Li | a7b394d | 2016-08-21 23:11:46 +0900 | [diff] [blame] | 208 | public LispMapNotify readFrom(ByteBuf byteBuf) throws LispParseError, LispReaderException { |
Jian Li | 4767190 | 2016-08-11 01:18:18 +0900 | [diff] [blame] | 209 | |
| 210 | if (byteBuf.readerIndex() != 0) { |
| 211 | return null; |
| 212 | } |
| 213 | |
| 214 | // skip first three bytes as they represent type and reserved fields |
| 215 | byteBuf.skipBytes(RESERVED_SKIP_LENGTH); |
| 216 | |
| 217 | // record count -> 8 bits |
| 218 | byte recordCount = (byte) byteBuf.readUnsignedByte(); |
| 219 | |
| 220 | // nonce -> 64 bits |
| 221 | long nonce = byteBuf.readLong(); |
| 222 | |
| 223 | // keyId -> 16 bits |
| 224 | short keyId = byteBuf.readShort(); |
| 225 | |
| 226 | // authenticationDataLength -> 16 bits |
| 227 | short authLength = byteBuf.readShort(); |
| 228 | |
| 229 | // authenticationData -> depends on the authenticationDataLength |
| 230 | byte[] authData = new byte[authLength]; |
| 231 | byteBuf.readBytes(authData); |
| 232 | |
| 233 | List<LispMapRecord> mapRecords = Lists.newArrayList(); |
| 234 | for (int i = 0; i < recordCount; i++) { |
| 235 | mapRecords.add(new DefaultLispMapRecord.MapRecordReader().readFrom(byteBuf)); |
| 236 | } |
| 237 | |
| 238 | return new DefaultNotifyBuilder() |
| 239 | .withRecordCount(recordCount) |
| 240 | .withNonce(nonce) |
| 241 | .withKeyId(keyId) |
Jian Li | edc5db1 | 2016-08-23 17:30:19 +0900 | [diff] [blame] | 242 | .withAuthDataLength(authLength) |
Jian Li | 4767190 | 2016-08-11 01:18:18 +0900 | [diff] [blame] | 243 | .withAuthenticationData(authData) |
| 244 | .withMapRecords(mapRecords) |
| 245 | .build(); |
Jian Li | 26069e2 | 2016-08-10 22:00:52 +0900 | [diff] [blame] | 246 | } |
| 247 | } |
Jian Li | edc5db1 | 2016-08-23 17:30:19 +0900 | [diff] [blame] | 248 | |
| 249 | /** |
| 250 | * A LISP message reader for MapNotify message. |
| 251 | */ |
| 252 | public static final class NotifyWriter implements LispMessageWriter<LispMapNotify> { |
| 253 | |
| 254 | private static final int NOTIFY_MSG_TYPE = 4; |
| 255 | private static final int NOTIFY_SHIFT_BIT = 4; |
| 256 | |
| 257 | private static final int UNUSED_ZERO = 0; |
| 258 | |
| 259 | @Override |
| 260 | public void writeTo(ByteBuf byteBuf, LispMapNotify message) throws LispWriterException { |
| 261 | |
| 262 | // specify LISP message type |
| 263 | byte msgType = (byte) (NOTIFY_MSG_TYPE << NOTIFY_SHIFT_BIT); |
| 264 | byteBuf.writeByte(msgType); |
| 265 | |
| 266 | // reserved field |
| 267 | byteBuf.writeShort((short) UNUSED_ZERO); |
| 268 | |
| 269 | // record count |
| 270 | byteBuf.writeByte(message.getRecordCount()); |
| 271 | |
| 272 | // nonce |
| 273 | byteBuf.writeLong(message.getNonce()); |
| 274 | |
| 275 | // keyId |
| 276 | byteBuf.writeShort(message.getKeyId()); |
| 277 | |
| 278 | // authentication data length in octet |
| 279 | byteBuf.writeShort(message.getAuthDataLength()); |
| 280 | |
| 281 | // authentication data |
| 282 | byte[] data = message.getAuthenticationData(); |
| 283 | byte[] clone; |
| 284 | if (data != null) { |
| 285 | clone = data.clone(); |
| 286 | Arrays.fill(clone, (byte) UNUSED_ZERO); |
| 287 | } |
| 288 | |
| 289 | byteBuf.writeBytes(data); |
| 290 | |
| 291 | // TODO: need to implement MAC authentication mechanism |
| 292 | |
| 293 | // serialize map records |
| 294 | MapRecordWriter writer = new MapRecordWriter(); |
| 295 | List<LispMapRecord> records = message.getMapRecords(); |
| 296 | |
| 297 | for (int i = 0; i < records.size(); i++) { |
| 298 | writer.writeTo(byteBuf, records.get(i)); |
| 299 | } |
| 300 | } |
| 301 | } |
Jian Li | 451175e | 2016-07-19 23:22:20 +0900 | [diff] [blame] | 302 | } |