blob: 30eba1fd90321ae0e42bcff213ffa79345a62ac0 [file] [log] [blame]
Jian Li451175e2016-07-19 23:22:20 +09001/*
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 */
16package org.onosproject.lisp.msg.protocols;
17
Jian Li20850d32016-08-04 02:15:57 +090018import com.google.common.base.Objects;
Jian Lif59c0ad2016-08-02 18:11:30 +090019import com.google.common.collect.ImmutableList;
20import com.google.common.collect.Lists;
Jian Li451175e2016-07-19 23:22:20 +090021import io.netty.buffer.ByteBuf;
Jian Lif59c0ad2016-08-02 18:11:30 +090022import org.onlab.util.ImmutableByteSequence;
Jian Li26069e22016-08-10 22:00:52 +090023import org.onosproject.lisp.msg.exceptions.LispParseError;
Jian Lia7b394d2016-08-21 23:11:46 +090024import org.onosproject.lisp.msg.exceptions.LispReaderException;
Jian Liedc5db12016-08-23 17:30:19 +090025import org.onosproject.lisp.msg.exceptions.LispWriterException;
Jian Li451175e2016-07-19 23:22:20 +090026
Jian Liedc5db12016-08-23 17:30:19 +090027import java.util.Arrays;
Jian Li719b3bf2016-07-22 00:38:29 +090028import java.util.List;
29
Jian Li20850d32016-08-04 02:15:57 +090030import static com.google.common.base.MoreObjects.toStringHelper;
Jian Liedc5db12016-08-23 17:30:19 +090031import static org.onosproject.lisp.msg.protocols.DefaultLispMapRecord.MapRecordWriter;
Jian Li20850d32016-08-04 02:15:57 +090032
Jian Li451175e2016-07-19 23:22:20 +090033/**
34 * Default LISP map notify message class.
35 */
Jian Lif59c0ad2016-08-02 18:11:30 +090036public final class DefaultLispMapNotify implements LispMapNotify {
37
38 private final long nonce;
39 private final short keyId;
Jian Liedc5db12016-08-23 17:30:19 +090040 private final short authDataLength;
Jian Lif59c0ad2016-08-02 18:11:30 +090041 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 Liedc5db12016-08-23 17:30:19 +090054 private DefaultLispMapNotify(long nonce, short keyId, short authDataLength,
55 byte[] authenticationData, byte recordCount,
56 List<LispMapRecord> mapRecords) {
Jian Lif59c0ad2016-08-02 18:11:30 +090057 this.nonce = nonce;
58 this.keyId = keyId;
Jian Liedc5db12016-08-23 17:30:19 +090059 this.authDataLength = authDataLength;
Jian Lif59c0ad2016-08-02 18:11:30 +090060 this.authenticationData = authenticationData;
61 this.recordCount = recordCount;
62 this.mapRecords = mapRecords;
63 }
Jian Li451175e2016-07-19 23:22:20 +090064
65 @Override
66 public LispType getType() {
Jian Lif59c0ad2016-08-02 18:11:30 +090067 return LispType.LISP_MAP_NOTIFY;
Jian Li451175e2016-07-19 23:22:20 +090068 }
69
70 @Override
71 public void writeTo(ByteBuf byteBuf) {
Jian Lif59c0ad2016-08-02 18:11:30 +090072 // TODO: serialize LispMapRegister message
Jian Li451175e2016-07-19 23:22:20 +090073 }
74
75 @Override
76 public Builder createBuilder() {
Jian Li525fded2016-08-04 01:15:33 +090077 return new DefaultNotifyBuilder();
Jian Li451175e2016-07-19 23:22:20 +090078 }
Jian Li719b3bf2016-07-22 00:38:29 +090079
80 @Override
81 public long getNonce() {
Jian Liedc5db12016-08-23 17:30:19 +090082 return nonce;
Jian Li719b3bf2016-07-22 00:38:29 +090083 }
84
85 @Override
86 public byte getRecordCount() {
Jian Liedc5db12016-08-23 17:30:19 +090087 return recordCount;
Jian Li719b3bf2016-07-22 00:38:29 +090088 }
89
90 @Override
91 public short getKeyId() {
Jian Liedc5db12016-08-23 17:30:19 +090092 return keyId;
93 }
94
95 @Override
96 public short getAuthDataLength() {
97 return authDataLength;
Jian Li719b3bf2016-07-22 00:38:29 +090098 }
99
100 @Override
101 public byte[] getAuthenticationData() {
Jian Liedc5db12016-08-23 17:30:19 +0900102 return ImmutableByteSequence.copyFrom(authenticationData).asArray();
Jian Li719b3bf2016-07-22 00:38:29 +0900103 }
104
105 @Override
Jian Liedc5db12016-08-23 17:30:19 +0900106 public List<LispMapRecord> getMapRecords() {
Jian Lif59c0ad2016-08-02 18:11:30 +0900107 return ImmutableList.copyOf(mapRecords);
Jian Li719b3bf2016-07-22 00:38:29 +0900108 }
109
Jian Li20850d32016-08-04 02:15:57 +0900110 @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 Liedc5db12016-08-23 17:30:19 +0900117 .add("authentication data length", authDataLength)
118 .add("authentication data", authenticationData)
Jian Li20850d32016-08-04 02:15:57 +0900119 .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 Liedc5db12016-08-23 17:30:19 +0900134 Objects.equal(authDataLength, that.authDataLength) &&
Jian Li20850d32016-08-04 02:15:57 +0900135 Objects.equal(authenticationData, that.authenticationData);
136 }
137
138 @Override
139 public int hashCode() {
Jian Liedc5db12016-08-23 17:30:19 +0900140 return Objects.hashCode(nonce, recordCount, keyId, authDataLength, authenticationData);
Jian Li20850d32016-08-04 02:15:57 +0900141 }
142
Jian Li719b3bf2016-07-22 00:38:29 +0900143 public static final class DefaultNotifyBuilder implements NotifyBuilder {
144
Jian Lif59c0ad2016-08-02 18:11:30 +0900145 private long nonce;
146 private short keyId;
Jian Liedc5db12016-08-23 17:30:19 +0900147 private short authDataLength;
Jian Lif59c0ad2016-08-02 18:11:30 +0900148 private byte[] authenticationData;
149 private byte recordCount;
Jian Li47671902016-08-11 01:18:18 +0900150 private List<LispMapRecord> mapRecords;
Jian Li719b3bf2016-07-22 00:38:29 +0900151
152 @Override
153 public LispType getType() {
Jian Lif59c0ad2016-08-02 18:11:30 +0900154 return LispType.LISP_MAP_NOTIFY;
Jian Li719b3bf2016-07-22 00:38:29 +0900155 }
156
157 @Override
158 public NotifyBuilder withNonce(long nonce) {
Jian Lif59c0ad2016-08-02 18:11:30 +0900159 this.nonce = nonce;
160 return this;
Jian Li719b3bf2016-07-22 00:38:29 +0900161 }
162
163 @Override
164 public NotifyBuilder withRecordCount(byte recordCount) {
Jian Lif59c0ad2016-08-02 18:11:30 +0900165 this.recordCount = recordCount;
166 return this;
Jian Li719b3bf2016-07-22 00:38:29 +0900167 }
168
169 @Override
170 public NotifyBuilder withKeyId(short keyId) {
Jian Lif59c0ad2016-08-02 18:11:30 +0900171 this.keyId = keyId;
172 return this;
Jian Li719b3bf2016-07-22 00:38:29 +0900173 }
174
175 @Override
Jian Liedc5db12016-08-23 17:30:19 +0900176 public NotifyBuilder withAuthDataLength(short authDataLength) {
177 this.authDataLength = authDataLength;
178 return this;
179 }
180
181 @Override
Jian Li719b3bf2016-07-22 00:38:29 +0900182 public NotifyBuilder withAuthenticationData(byte[] authenticationData) {
Jian Lif59c0ad2016-08-02 18:11:30 +0900183 this.authenticationData = authenticationData;
184 return this;
Jian Li719b3bf2016-07-22 00:38:29 +0900185 }
186
187 @Override
Jian Li47671902016-08-11 01:18:18 +0900188 public NotifyBuilder withMapRecords(List<LispMapRecord> mapRecords) {
189 this.mapRecords = ImmutableList.copyOf(mapRecords);
Jian Lif59c0ad2016-08-02 18:11:30 +0900190 return this;
191 }
192
193 @Override
Jian Li525fded2016-08-04 01:15:33 +0900194 public LispMapNotify build() {
Jian Liedc5db12016-08-23 17:30:19 +0900195 return new DefaultLispMapNotify(nonce, keyId, authDataLength,
196 authenticationData, recordCount, mapRecords);
Jian Li719b3bf2016-07-22 00:38:29 +0900197 }
198 }
Jian Li26069e22016-08-10 22:00:52 +0900199
200 /**
Jian Liedc5db12016-08-23 17:30:19 +0900201 * A LISP message reader for MapNotify message.
Jian Li26069e22016-08-10 22:00:52 +0900202 */
Jian Liedc5db12016-08-23 17:30:19 +0900203 public static final class NotifyReader implements LispMessageReader<LispMapNotify> {
Jian Li26069e22016-08-10 22:00:52 +0900204
Jian Li47671902016-08-11 01:18:18 +0900205 private static final int RESERVED_SKIP_LENGTH = 3;
206
Jian Li26069e22016-08-10 22:00:52 +0900207 @Override
Jian Lia7b394d2016-08-21 23:11:46 +0900208 public LispMapNotify readFrom(ByteBuf byteBuf) throws LispParseError, LispReaderException {
Jian Li47671902016-08-11 01:18:18 +0900209
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 Liedc5db12016-08-23 17:30:19 +0900242 .withAuthDataLength(authLength)
Jian Li47671902016-08-11 01:18:18 +0900243 .withAuthenticationData(authData)
244 .withMapRecords(mapRecords)
245 .build();
Jian Li26069e22016-08-10 22:00:52 +0900246 }
247 }
Jian Liedc5db12016-08-23 17:30:19 +0900248
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 Li451175e2016-07-19 23:22:20 +0900302}