blob: 05cc7f52f0efc7e1e53e28deeb60776fd9a7d388 [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 Li451175e2016-07-19 23:22:20 +090025
Jian Li719b3bf2016-07-22 00:38:29 +090026import java.util.List;
27
Jian Li20850d32016-08-04 02:15:57 +090028import static com.google.common.base.MoreObjects.toStringHelper;
29
Jian Li451175e2016-07-19 23:22:20 +090030/**
31 * Default LISP map notify message class.
32 */
Jian Lif59c0ad2016-08-02 18:11:30 +090033public final class DefaultLispMapNotify implements LispMapNotify {
34
35 private final long nonce;
36 private final short keyId;
37 private final byte[] authenticationData;
38 private final byte recordCount;
39 private final List<LispMapRecord> mapRecords;
40
41 /**
42 * A private constructor that protects object instantiation from external.
43 *
44 * @param nonce nonce
45 * @param keyId key identifier
46 * @param authenticationData authentication data
47 * @param recordCount record count number
48 * @param mapRecords a collection of map records
49 */
50 private DefaultLispMapNotify(long nonce, short keyId, byte[] authenticationData,
51 byte recordCount, List<LispMapRecord> mapRecords) {
52 this.nonce = nonce;
53 this.keyId = keyId;
54 this.authenticationData = authenticationData;
55 this.recordCount = recordCount;
56 this.mapRecords = mapRecords;
57 }
Jian Li451175e2016-07-19 23:22:20 +090058
59 @Override
60 public LispType getType() {
Jian Lif59c0ad2016-08-02 18:11:30 +090061 return LispType.LISP_MAP_NOTIFY;
Jian Li451175e2016-07-19 23:22:20 +090062 }
63
64 @Override
65 public void writeTo(ByteBuf byteBuf) {
Jian Lif59c0ad2016-08-02 18:11:30 +090066 // TODO: serialize LispMapRegister message
Jian Li451175e2016-07-19 23:22:20 +090067 }
68
69 @Override
70 public Builder createBuilder() {
Jian Li525fded2016-08-04 01:15:33 +090071 return new DefaultNotifyBuilder();
Jian Li451175e2016-07-19 23:22:20 +090072 }
Jian Li719b3bf2016-07-22 00:38:29 +090073
74 @Override
75 public long getNonce() {
Jian Lif59c0ad2016-08-02 18:11:30 +090076 return this.nonce;
Jian Li719b3bf2016-07-22 00:38:29 +090077 }
78
79 @Override
80 public byte getRecordCount() {
Jian Lif59c0ad2016-08-02 18:11:30 +090081 return this.recordCount;
Jian Li719b3bf2016-07-22 00:38:29 +090082 }
83
84 @Override
85 public short getKeyId() {
Jian Lif59c0ad2016-08-02 18:11:30 +090086 return this.keyId;
Jian Li719b3bf2016-07-22 00:38:29 +090087 }
88
89 @Override
90 public byte[] getAuthenticationData() {
Jian Lif59c0ad2016-08-02 18:11:30 +090091 return ImmutableByteSequence.copyFrom(this.authenticationData).asArray();
Jian Li719b3bf2016-07-22 00:38:29 +090092 }
93
94 @Override
Jian Li10a09062016-07-26 23:58:50 +090095 public List<LispMapRecord> getLispRecords() {
Jian Lif59c0ad2016-08-02 18:11:30 +090096 return ImmutableList.copyOf(mapRecords);
Jian Li719b3bf2016-07-22 00:38:29 +090097 }
98
Jian Li20850d32016-08-04 02:15:57 +090099 @Override
100 public String toString() {
101 return toStringHelper(this)
102 .add("type", getType())
103 .add("nonce", nonce)
104 .add("recordCount", recordCount)
105 .add("keyId", keyId)
106 .add("mapRecords", mapRecords).toString();
107 }
108
109 @Override
110 public boolean equals(Object o) {
111 if (this == o) {
112 return true;
113 }
114 if (o == null || getClass() != o.getClass()) {
115 return false;
116 }
117 DefaultLispMapNotify that = (DefaultLispMapNotify) o;
118 return Objects.equal(nonce, that.nonce) &&
119 Objects.equal(recordCount, that.recordCount) &&
120 Objects.equal(keyId, that.keyId) &&
121 Objects.equal(authenticationData, that.authenticationData);
122 }
123
124 @Override
125 public int hashCode() {
126 return Objects.hashCode(nonce, recordCount, keyId, authenticationData);
127 }
128
Jian Li719b3bf2016-07-22 00:38:29 +0900129 public static final class DefaultNotifyBuilder implements NotifyBuilder {
130
Jian Lif59c0ad2016-08-02 18:11:30 +0900131 private long nonce;
132 private short keyId;
133 private byte[] authenticationData;
134 private byte recordCount;
Jian Li47671902016-08-11 01:18:18 +0900135 private List<LispMapRecord> mapRecords;
Jian Li719b3bf2016-07-22 00:38:29 +0900136
137 @Override
138 public LispType getType() {
Jian Lif59c0ad2016-08-02 18:11:30 +0900139 return LispType.LISP_MAP_NOTIFY;
Jian Li719b3bf2016-07-22 00:38:29 +0900140 }
141
142 @Override
143 public NotifyBuilder withNonce(long nonce) {
Jian Lif59c0ad2016-08-02 18:11:30 +0900144 this.nonce = nonce;
145 return this;
Jian Li719b3bf2016-07-22 00:38:29 +0900146 }
147
148 @Override
149 public NotifyBuilder withRecordCount(byte recordCount) {
Jian Lif59c0ad2016-08-02 18:11:30 +0900150 this.recordCount = recordCount;
151 return this;
Jian Li719b3bf2016-07-22 00:38:29 +0900152 }
153
154 @Override
155 public NotifyBuilder withKeyId(short keyId) {
Jian Lif59c0ad2016-08-02 18:11:30 +0900156 this.keyId = keyId;
157 return this;
Jian Li719b3bf2016-07-22 00:38:29 +0900158 }
159
160 @Override
161 public NotifyBuilder withAuthenticationData(byte[] authenticationData) {
Jian Lif59c0ad2016-08-02 18:11:30 +0900162 this.authenticationData = authenticationData;
163 return this;
Jian Li719b3bf2016-07-22 00:38:29 +0900164 }
165
166 @Override
Jian Li47671902016-08-11 01:18:18 +0900167 public NotifyBuilder withMapRecords(List<LispMapRecord> mapRecords) {
168 this.mapRecords = ImmutableList.copyOf(mapRecords);
Jian Lif59c0ad2016-08-02 18:11:30 +0900169 return this;
170 }
171
172 @Override
Jian Li525fded2016-08-04 01:15:33 +0900173 public LispMapNotify build() {
Jian Lif59c0ad2016-08-02 18:11:30 +0900174 return new DefaultLispMapNotify(nonce, keyId, authenticationData,
175 recordCount, mapRecords);
Jian Li719b3bf2016-07-22 00:38:29 +0900176 }
177 }
Jian Li26069e22016-08-10 22:00:52 +0900178
179 /**
180 * A private LISP message reader for MapNotify message.
181 */
182 private static class NotifyReader implements LispMessageReader<LispMapNotify> {
183
Jian Li47671902016-08-11 01:18:18 +0900184 private static final int RESERVED_SKIP_LENGTH = 3;
185
Jian Li26069e22016-08-10 22:00:52 +0900186 @Override
Jian Lia7b394d2016-08-21 23:11:46 +0900187 public LispMapNotify readFrom(ByteBuf byteBuf) throws LispParseError, LispReaderException {
Jian Li47671902016-08-11 01:18:18 +0900188
189 if (byteBuf.readerIndex() != 0) {
190 return null;
191 }
192
193 // skip first three bytes as they represent type and reserved fields
194 byteBuf.skipBytes(RESERVED_SKIP_LENGTH);
195
196 // record count -> 8 bits
197 byte recordCount = (byte) byteBuf.readUnsignedByte();
198
199 // nonce -> 64 bits
200 long nonce = byteBuf.readLong();
201
202 // keyId -> 16 bits
203 short keyId = byteBuf.readShort();
204
205 // authenticationDataLength -> 16 bits
206 short authLength = byteBuf.readShort();
207
208 // authenticationData -> depends on the authenticationDataLength
209 byte[] authData = new byte[authLength];
210 byteBuf.readBytes(authData);
211
212 List<LispMapRecord> mapRecords = Lists.newArrayList();
213 for (int i = 0; i < recordCount; i++) {
214 mapRecords.add(new DefaultLispMapRecord.MapRecordReader().readFrom(byteBuf));
215 }
216
217 return new DefaultNotifyBuilder()
218 .withRecordCount(recordCount)
219 .withNonce(nonce)
220 .withKeyId(keyId)
221 .withAuthenticationData(authData)
222 .withMapRecords(mapRecords)
223 .build();
Jian Li26069e22016-08-10 22:00:52 +0900224 }
225 }
Jian Li451175e2016-07-19 23:22:20 +0900226}