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