blob: bfd64fcc8ec313bed10fcbce5c53904809255159 [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 Lie4ba2a42016-08-29 20:24:15 +0900102 if (authenticationData != null && authenticationData.length != 0) {
103 return ImmutableByteSequence.copyFrom(authenticationData).asArray();
104 } else {
105 return new byte[0];
106 }
Jian Li719b3bf2016-07-22 00:38:29 +0900107 }
108
109 @Override
Jian Liedc5db12016-08-23 17:30:19 +0900110 public List<LispMapRecord> getMapRecords() {
Jian Lif59c0ad2016-08-02 18:11:30 +0900111 return ImmutableList.copyOf(mapRecords);
Jian Li719b3bf2016-07-22 00:38:29 +0900112 }
113
Jian Li20850d32016-08-04 02:15:57 +0900114 @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 Liedc5db12016-08-23 17:30:19 +0900121 .add("authentication data length", authDataLength)
122 .add("authentication data", authenticationData)
Jian Li20850d32016-08-04 02:15:57 +0900123 .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 Liedc5db12016-08-23 17:30:19 +0900138 Objects.equal(authDataLength, that.authDataLength) &&
Jian Lie4ba2a42016-08-29 20:24:15 +0900139 Arrays.equals(authenticationData, that.authenticationData);
Jian Li20850d32016-08-04 02:15:57 +0900140 }
141
142 @Override
143 public int hashCode() {
Jian Lie4ba2a42016-08-29 20:24:15 +0900144 return Objects.hashCode(nonce, recordCount, keyId, authDataLength) +
145 Arrays.hashCode(authenticationData);
Jian Li20850d32016-08-04 02:15:57 +0900146 }
147
Jian Li719b3bf2016-07-22 00:38:29 +0900148 public static final class DefaultNotifyBuilder implements NotifyBuilder {
149
Jian Lif59c0ad2016-08-02 18:11:30 +0900150 private long nonce;
151 private short keyId;
Jian Liedc5db12016-08-23 17:30:19 +0900152 private short authDataLength;
Jian Lif59c0ad2016-08-02 18:11:30 +0900153 private byte[] authenticationData;
154 private byte recordCount;
Jian Lid4e63702016-08-30 18:29:20 +0900155 private List<LispMapRecord> mapRecords = Lists.newArrayList();
Jian Li719b3bf2016-07-22 00:38:29 +0900156
157 @Override
158 public LispType getType() {
Jian Lif59c0ad2016-08-02 18:11:30 +0900159 return LispType.LISP_MAP_NOTIFY;
Jian Li719b3bf2016-07-22 00:38:29 +0900160 }
161
162 @Override
163 public NotifyBuilder withNonce(long nonce) {
Jian Lif59c0ad2016-08-02 18:11:30 +0900164 this.nonce = nonce;
165 return this;
Jian Li719b3bf2016-07-22 00:38:29 +0900166 }
167
168 @Override
169 public NotifyBuilder withRecordCount(byte recordCount) {
Jian Lif59c0ad2016-08-02 18:11:30 +0900170 this.recordCount = recordCount;
171 return this;
Jian Li719b3bf2016-07-22 00:38:29 +0900172 }
173
174 @Override
175 public NotifyBuilder withKeyId(short keyId) {
Jian Lif59c0ad2016-08-02 18:11:30 +0900176 this.keyId = keyId;
177 return this;
Jian Li719b3bf2016-07-22 00:38:29 +0900178 }
179
180 @Override
Jian Liedc5db12016-08-23 17:30:19 +0900181 public NotifyBuilder withAuthDataLength(short authDataLength) {
182 this.authDataLength = authDataLength;
183 return this;
184 }
185
186 @Override
Jian Li719b3bf2016-07-22 00:38:29 +0900187 public NotifyBuilder withAuthenticationData(byte[] authenticationData) {
Jian Lie4ba2a42016-08-29 20:24:15 +0900188 if (authenticationData != null) {
189 this.authenticationData = authenticationData;
190 } else {
191 this.authenticationData = new byte[0];
192 }
Jian Lif59c0ad2016-08-02 18:11:30 +0900193 return this;
Jian Li719b3bf2016-07-22 00:38:29 +0900194 }
195
196 @Override
Jian Li47671902016-08-11 01:18:18 +0900197 public NotifyBuilder withMapRecords(List<LispMapRecord> mapRecords) {
Jian Lie4ba2a42016-08-29 20:24:15 +0900198 if (mapRecords != null) {
199 this.mapRecords = ImmutableList.copyOf(mapRecords);
Jian Lie4ba2a42016-08-29 20:24:15 +0900200 }
Jian Lif59c0ad2016-08-02 18:11:30 +0900201 return this;
202 }
203
204 @Override
Jian Li525fded2016-08-04 01:15:33 +0900205 public LispMapNotify build() {
Jian Lie4ba2a42016-08-29 20:24:15 +0900206
207 if (authenticationData == null) {
208 authenticationData = new byte[0];
209 }
210
Jian Liedc5db12016-08-23 17:30:19 +0900211 return new DefaultLispMapNotify(nonce, keyId, authDataLength,
212 authenticationData, recordCount, mapRecords);
Jian Li719b3bf2016-07-22 00:38:29 +0900213 }
214 }
Jian Li26069e22016-08-10 22:00:52 +0900215
216 /**
Jian Liedc5db12016-08-23 17:30:19 +0900217 * A LISP message reader for MapNotify message.
Jian Li26069e22016-08-10 22:00:52 +0900218 */
Jian Liedc5db12016-08-23 17:30:19 +0900219 public static final class NotifyReader implements LispMessageReader<LispMapNotify> {
Jian Li26069e22016-08-10 22:00:52 +0900220
Jian Li47671902016-08-11 01:18:18 +0900221 private static final int RESERVED_SKIP_LENGTH = 3;
222
Jian Li26069e22016-08-10 22:00:52 +0900223 @Override
Jian Lia7b394d2016-08-21 23:11:46 +0900224 public LispMapNotify readFrom(ByteBuf byteBuf) throws LispParseError, LispReaderException {
Jian Li47671902016-08-11 01:18:18 +0900225
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 Liedc5db12016-08-23 17:30:19 +0900258 .withAuthDataLength(authLength)
Jian Li47671902016-08-11 01:18:18 +0900259 .withAuthenticationData(authData)
260 .withMapRecords(mapRecords)
261 .build();
Jian Li26069e22016-08-10 22:00:52 +0900262 }
263 }
Jian Liedc5db12016-08-23 17:30:19 +0900264
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 Li451175e2016-07-19 23:22:20 +0900318}