blob: 2e0904939e0770250ed79cc81b7193ed7b0ba60f [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;
Jian Lif59c0ad2016-08-02 18:11:30 +090042 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 Lif59c0ad2016-08-02 18:11:30 +090050 * @param mapRecords a collection of map records
51 */
Jian Liedc5db12016-08-23 17:30:19 +090052 private DefaultLispMapNotify(long nonce, short keyId, short authDataLength,
Jian Li42b3e432016-08-31 01:05:20 +090053 byte[] authenticationData, List<LispMapRecord> mapRecords) {
Jian Lif59c0ad2016-08-02 18:11:30 +090054 this.nonce = nonce;
55 this.keyId = keyId;
Jian Liedc5db12016-08-23 17:30:19 +090056 this.authDataLength = authDataLength;
Jian Lif59c0ad2016-08-02 18:11:30 +090057 this.authenticationData = authenticationData;
Jian Lif59c0ad2016-08-02 18:11:30 +090058 this.mapRecords = mapRecords;
59 }
Jian Li451175e2016-07-19 23:22:20 +090060
61 @Override
62 public LispType getType() {
Jian Lif59c0ad2016-08-02 18:11:30 +090063 return LispType.LISP_MAP_NOTIFY;
Jian Li451175e2016-07-19 23:22:20 +090064 }
65
66 @Override
67 public void writeTo(ByteBuf byteBuf) {
Jian Lif59c0ad2016-08-02 18:11:30 +090068 // TODO: serialize LispMapRegister message
Jian Li451175e2016-07-19 23:22:20 +090069 }
70
71 @Override
72 public Builder createBuilder() {
Jian Li525fded2016-08-04 01:15:33 +090073 return new DefaultNotifyBuilder();
Jian Li451175e2016-07-19 23:22:20 +090074 }
Jian Li719b3bf2016-07-22 00:38:29 +090075
76 @Override
77 public long getNonce() {
Jian Liedc5db12016-08-23 17:30:19 +090078 return nonce;
Jian Li719b3bf2016-07-22 00:38:29 +090079 }
80
81 @Override
Jian Li42b3e432016-08-31 01:05:20 +090082 public int getRecordCount() {
83 return mapRecords.size();
Jian Li719b3bf2016-07-22 00:38:29 +090084 }
85
86 @Override
87 public short getKeyId() {
Jian Liedc5db12016-08-23 17:30:19 +090088 return keyId;
89 }
90
91 @Override
92 public short getAuthDataLength() {
93 return authDataLength;
Jian Li719b3bf2016-07-22 00:38:29 +090094 }
95
96 @Override
97 public byte[] getAuthenticationData() {
Jian Lie4ba2a42016-08-29 20:24:15 +090098 if (authenticationData != null && authenticationData.length != 0) {
99 return ImmutableByteSequence.copyFrom(authenticationData).asArray();
100 } else {
101 return new byte[0];
102 }
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)
Jian Li20850d32016-08-04 02:15:57 +0900115 .add("keyId", keyId)
Jian Liedc5db12016-08-23 17:30:19 +0900116 .add("authentication data length", authDataLength)
117 .add("authentication data", authenticationData)
Jian Li20850d32016-08-04 02:15:57 +0900118 .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 Li20850d32016-08-04 02:15:57 +0900131 Objects.equal(keyId, that.keyId) &&
Jian Liedc5db12016-08-23 17:30:19 +0900132 Objects.equal(authDataLength, that.authDataLength) &&
Jian Lie4ba2a42016-08-29 20:24:15 +0900133 Arrays.equals(authenticationData, that.authenticationData);
Jian Li20850d32016-08-04 02:15:57 +0900134 }
135
136 @Override
137 public int hashCode() {
Jian Li42b3e432016-08-31 01:05:20 +0900138 return Objects.hashCode(nonce, keyId, authDataLength) +
Jian Lie4ba2a42016-08-29 20:24:15 +0900139 Arrays.hashCode(authenticationData);
Jian Li20850d32016-08-04 02:15:57 +0900140 }
141
Jian Li719b3bf2016-07-22 00:38:29 +0900142 public static final class DefaultNotifyBuilder implements NotifyBuilder {
143
Jian Lif59c0ad2016-08-02 18:11:30 +0900144 private long nonce;
145 private short keyId;
Jian Liedc5db12016-08-23 17:30:19 +0900146 private short authDataLength;
Jian Lif59c0ad2016-08-02 18:11:30 +0900147 private byte[] authenticationData;
Jian Lid4e63702016-08-30 18:29:20 +0900148 private List<LispMapRecord> mapRecords = Lists.newArrayList();
Jian Li719b3bf2016-07-22 00:38:29 +0900149
150 @Override
151 public LispType getType() {
Jian Lif59c0ad2016-08-02 18:11:30 +0900152 return LispType.LISP_MAP_NOTIFY;
Jian Li719b3bf2016-07-22 00:38:29 +0900153 }
154
155 @Override
156 public NotifyBuilder withNonce(long nonce) {
Jian Lif59c0ad2016-08-02 18:11:30 +0900157 this.nonce = nonce;
158 return this;
Jian Li719b3bf2016-07-22 00:38:29 +0900159 }
160
161 @Override
Jian Li719b3bf2016-07-22 00:38:29 +0900162 public NotifyBuilder withKeyId(short keyId) {
Jian Lif59c0ad2016-08-02 18:11:30 +0900163 this.keyId = keyId;
164 return this;
Jian Li719b3bf2016-07-22 00:38:29 +0900165 }
166
167 @Override
Jian Liedc5db12016-08-23 17:30:19 +0900168 public NotifyBuilder withAuthDataLength(short authDataLength) {
169 this.authDataLength = authDataLength;
170 return this;
171 }
172
173 @Override
Jian Li719b3bf2016-07-22 00:38:29 +0900174 public NotifyBuilder withAuthenticationData(byte[] authenticationData) {
Jian Lie4ba2a42016-08-29 20:24:15 +0900175 if (authenticationData != null) {
176 this.authenticationData = authenticationData;
177 } else {
178 this.authenticationData = new byte[0];
179 }
Jian Lif59c0ad2016-08-02 18:11:30 +0900180 return this;
Jian Li719b3bf2016-07-22 00:38:29 +0900181 }
182
183 @Override
Jian Li47671902016-08-11 01:18:18 +0900184 public NotifyBuilder withMapRecords(List<LispMapRecord> mapRecords) {
Jian Lie4ba2a42016-08-29 20:24:15 +0900185 if (mapRecords != null) {
186 this.mapRecords = ImmutableList.copyOf(mapRecords);
Jian Lie4ba2a42016-08-29 20:24:15 +0900187 }
Jian Lif59c0ad2016-08-02 18:11:30 +0900188 return this;
189 }
190
191 @Override
Jian Li525fded2016-08-04 01:15:33 +0900192 public LispMapNotify build() {
Jian Lie4ba2a42016-08-29 20:24:15 +0900193
194 if (authenticationData == null) {
195 authenticationData = new byte[0];
196 }
197
Jian Liedc5db12016-08-23 17:30:19 +0900198 return new DefaultLispMapNotify(nonce, keyId, authDataLength,
Jian Li42b3e432016-08-31 01:05:20 +0900199 authenticationData, mapRecords);
Jian Li719b3bf2016-07-22 00:38:29 +0900200 }
201 }
Jian Li26069e22016-08-10 22:00:52 +0900202
203 /**
Jian Liedc5db12016-08-23 17:30:19 +0900204 * A LISP message reader for MapNotify message.
Jian Li26069e22016-08-10 22:00:52 +0900205 */
Jian Liedc5db12016-08-23 17:30:19 +0900206 public static final class NotifyReader implements LispMessageReader<LispMapNotify> {
Jian Li26069e22016-08-10 22:00:52 +0900207
Jian Li47671902016-08-11 01:18:18 +0900208 private static final int RESERVED_SKIP_LENGTH = 3;
209
Jian Li26069e22016-08-10 22:00:52 +0900210 @Override
Jian Lia7b394d2016-08-21 23:11:46 +0900211 public LispMapNotify readFrom(ByteBuf byteBuf) throws LispParseError, LispReaderException {
Jian Li47671902016-08-11 01:18:18 +0900212
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 Li47671902016-08-11 01:18:18 +0900242 .withNonce(nonce)
243 .withKeyId(keyId)
Jian Liedc5db12016-08-23 17:30:19 +0900244 .withAuthDataLength(authLength)
Jian Li47671902016-08-11 01:18:18 +0900245 .withAuthenticationData(authData)
246 .withMapRecords(mapRecords)
247 .build();
Jian Li26069e22016-08-10 22:00:52 +0900248 }
249 }
Jian Liedc5db12016-08-23 17:30:19 +0900250
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 Li42b3e432016-08-31 01:05:20 +0900272 byteBuf.writeByte(message.getMapRecords().size());
Jian Liedc5db12016-08-23 17:30:19 +0900273
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 Li451175e2016-07-19 23:22:20 +0900304}