blob: 13e36a0d61b4641e076cfe9765041c5480238be5 [file] [log] [blame]
Jian Li27759352016-10-04 20:14:42 +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
18import com.google.common.base.Objects;
19import io.netty.buffer.ByteBuf;
Jian Lid1a109e2016-11-12 09:00:42 +090020import io.netty.buffer.Unpooled;
21import org.onosproject.lisp.msg.authentication.LispAuthenticationFactory;
Jian Li27759352016-10-04 20:14:42 +090022import org.onosproject.lisp.msg.exceptions.LispParseError;
23import org.onosproject.lisp.msg.exceptions.LispReaderException;
24import org.onosproject.lisp.msg.exceptions.LispWriterException;
25import org.onosproject.lisp.msg.types.LispAfiAddress;
yoonseon980cd7c2016-11-18 14:18:46 -080026import org.onosproject.lisp.msg.types.LispNoAddress;
Jian Li5e505c62016-12-05 02:44:24 +090027import org.onosproject.lisp.msg.authentication.LispAuthenticationKeyEnum;
Jian Lid1a109e2016-11-12 09:00:42 +090028import org.slf4j.Logger;
29import org.slf4j.LoggerFactory;
Jian Li27759352016-10-04 20:14:42 +090030
31import java.util.Arrays;
32
33import static com.google.common.base.MoreObjects.toStringHelper;
Jian Lid1a109e2016-11-12 09:00:42 +090034import static org.onosproject.lisp.msg.authentication.LispAuthenticationKeyEnum.valueOf;
Jian Li27759352016-10-04 20:14:42 +090035
36/**
37 * Default LISP info request message class.
38 */
yoonseon980cd7c2016-11-18 14:18:46 -080039public class DefaultLispInfoRequest extends DefaultLispInfo
40 implements LispInfoRequest {
Jian Li27759352016-10-04 20:14:42 +090041
yoonseon980cd7c2016-11-18 14:18:46 -080042 private static final Logger log =
43 LoggerFactory.getLogger(DefaultLispInfoRequest.class);
44
45 static final InfoRequestWriter WRITER;
46
47 static {
48 WRITER = new InfoRequestWriter();
49 }
Jian Lid1a109e2016-11-12 09:00:42 +090050
Jian Li27759352016-10-04 20:14:42 +090051 /**
52 * A private constructor that protects object instantiation from external.
53 *
Jian Lid1a109e2016-11-12 09:00:42 +090054 * @param infoReply info reply flag
55 * @param nonce nonce
56 * @param keyId key identifier
57 * @param authDataLength authentication data length
58 * @param authData authentication data
59 * @param ttl Time-To-Live value
60 * @param maskLength EID prefix mask length
61 * @param eidPrefix EID prefix
Jian Li27759352016-10-04 20:14:42 +090062 */
yoonseon980cd7c2016-11-18 14:18:46 -080063 protected DefaultLispInfoRequest(boolean infoReply, long nonce, short keyId,
64 short authDataLength, byte[] authData,
65 int ttl, byte maskLength,
Jian Li27759352016-10-04 20:14:42 +090066 LispAfiAddress eidPrefix) {
yoonseon980cd7c2016-11-18 14:18:46 -080067
68 super(infoReply, nonce, keyId, authDataLength, authData, ttl,
69 maskLength, eidPrefix);
Jian Li27759352016-10-04 20:14:42 +090070 }
71
72 @Override
73 public String toString() {
74 return toStringHelper(this)
75 .add("type", getType())
76 .add("nonce", nonce)
77 .add("keyId", keyId)
78 .add("authentication data length", authDataLength)
Jian Lid1a109e2016-11-12 09:00:42 +090079 .add("authentication data", authData)
Jian Li27759352016-10-04 20:14:42 +090080 .add("TTL", ttl)
81 .add("EID mask length", maskLength)
82 .add("EID prefix", eidPrefix).toString();
83 }
84
85 @Override
86 public boolean equals(Object o) {
87 if (this == o) {
88 return true;
89 }
90 if (o == null || getClass() != o.getClass()) {
91 return false;
92 }
93
94 DefaultLispInfoRequest that = (DefaultLispInfoRequest) o;
95 return Objects.equal(nonce, that.nonce) &&
96 Objects.equal(keyId, that.keyId) &&
97 Objects.equal(authDataLength, that.authDataLength) &&
Jian Lid1a109e2016-11-12 09:00:42 +090098 Arrays.equals(authData, that.authData) &&
Jian Li27759352016-10-04 20:14:42 +090099 Objects.equal(ttl, that.ttl) &&
100 Objects.equal(maskLength, that.maskLength) &&
101 Objects.equal(eidPrefix, that.eidPrefix);
102 }
103
104 @Override
105 public int hashCode() {
106 return Objects.hashCode(nonce, keyId, authDataLength, ttl, maskLength,
Jian Lid1a109e2016-11-12 09:00:42 +0900107 eidPrefix) + Arrays.hashCode(authData);
Jian Li27759352016-10-04 20:14:42 +0900108 }
109
yoonseon980cd7c2016-11-18 14:18:46 -0800110 @Override
111 public void writeTo(ByteBuf byteBuf) throws LispWriterException {
112 WRITER.writeTo(byteBuf, this);
113 }
114
115 public static final class DefaultInfoRequestBuilder
116 implements InfoRequestBuilder {
Jian Li27759352016-10-04 20:14:42 +0900117
118 private boolean infoReply;
119 private long nonce;
120 private short keyId;
121 private short authDataLength;
Jian Lid1a109e2016-11-12 09:00:42 +0900122 private byte[] authData;
123 private String authKey;
Jian Li27759352016-10-04 20:14:42 +0900124 private int ttl;
125 private byte maskLength;
126 private LispAfiAddress eidPrefix;
127
128 @Override
129 public LispType getType() {
130 return LispType.LISP_INFO;
131 }
132
133
134 @Override
Jian Li6ef1b3f2016-11-12 18:16:06 +0900135 public InfoRequestBuilder withIsInfoReply(boolean infoReply) {
Jian Li27759352016-10-04 20:14:42 +0900136 this.infoReply = infoReply;
137 return this;
138 }
139
140 @Override
141 public InfoRequestBuilder withNonce(long nonce) {
142 this.nonce = nonce;
143 return this;
144 }
145
146 @Override
147 public InfoRequestBuilder withAuthDataLength(short authDataLength) {
148 this.authDataLength = authDataLength;
149 return this;
150 }
151
152 @Override
153 public InfoRequestBuilder withKeyId(short keyId) {
154 this.keyId = keyId;
155 return this;
156 }
157
158 @Override
Jian Lid1a109e2016-11-12 09:00:42 +0900159 public InfoRequestBuilder withAuthData(byte[] authenticationData) {
Jian Li27759352016-10-04 20:14:42 +0900160 if (authenticationData != null) {
Jian Lid1a109e2016-11-12 09:00:42 +0900161 this.authData = authenticationData;
Jian Li27759352016-10-04 20:14:42 +0900162 }
163 return this;
164 }
165
166 @Override
Jian Lid1a109e2016-11-12 09:00:42 +0900167 public InfoRequestBuilder withAuthKey(String key) {
168 this.authKey = key;
169 return this;
170 }
171
172 @Override
Jian Li27759352016-10-04 20:14:42 +0900173 public InfoRequestBuilder withTtl(int ttl) {
174 this.ttl = ttl;
175 return this;
176 }
177
178 @Override
179 public InfoRequestBuilder withMaskLength(byte maskLength) {
180 this.maskLength = maskLength;
181 return this;
182 }
183
184 @Override
185 public InfoRequestBuilder withEidPrefix(LispAfiAddress eidPrefix) {
186 this.eidPrefix = eidPrefix;
187 return this;
188 }
189
190 @Override
191 public LispInfoRequest build() {
Jian Lid1a109e2016-11-12 09:00:42 +0900192
193 // if authentication data is not specified, we will calculate it
194 if (authData == null) {
yoonseon980cd7c2016-11-18 14:18:46 -0800195 LispAuthenticationFactory factory =
196 LispAuthenticationFactory.getInstance();
Jian Lid1a109e2016-11-12 09:00:42 +0900197
yoonseon980cd7c2016-11-18 14:18:46 -0800198 authDataLength =
199 LispAuthenticationKeyEnum.valueOf(keyId).getHashLength();
Jian Lid1a109e2016-11-12 09:00:42 +0900200 byte[] tmpAuthData = new byte[authDataLength];
201 Arrays.fill(tmpAuthData, (byte) 0);
202 authData = tmpAuthData;
203
204 ByteBuf byteBuf = Unpooled.buffer();
205 try {
yoonseon980cd7c2016-11-18 14:18:46 -0800206 new DefaultLispInfoRequest(infoReply, nonce, keyId,
207 authDataLength, authData, ttl,
208 maskLength, eidPrefix)
209 .writeTo(byteBuf);
Jian Lid1a109e2016-11-12 09:00:42 +0900210 } catch (LispWriterException e) {
211 log.warn("Failed to serialize info request", e);
212 }
213
214 byte[] bytes = new byte[byteBuf.readableBytes()];
215 byteBuf.readBytes(bytes);
216
217 if (authKey == null) {
218 log.warn("Must specify authentication key");
219 }
220
yoonseon980cd7c2016-11-18 14:18:46 -0800221 authData = factory
222 .createAuthenticationData(valueOf(keyId), authKey, bytes);
Jian Lid1a109e2016-11-12 09:00:42 +0900223 }
224
Jian Li27759352016-10-04 20:14:42 +0900225 return new DefaultLispInfoRequest(infoReply, nonce, keyId,
Jian Lid1a109e2016-11-12 09:00:42 +0900226 authDataLength, authData, ttl, maskLength, eidPrefix);
Jian Li27759352016-10-04 20:14:42 +0900227 }
228 }
229
230 /**
231 * A LISP message reader for InfoRequest message.
232 */
yoonseon980cd7c2016-11-18 14:18:46 -0800233 public static class InfoRequestReader
234 implements LispMessageReader<LispInfoRequest> {
Jian Li27759352016-10-04 20:14:42 +0900235
236 @Override
yoonseon980cd7c2016-11-18 14:18:46 -0800237 public LispInfoRequest readFrom(ByteBuf byteBuf)
238 throws LispParseError, LispReaderException {
Jian Li27759352016-10-04 20:14:42 +0900239
Jian Li5e505c62016-12-05 02:44:24 +0900240 LispInfo lispInfo = deserialize(byteBuf);
Jian Li27759352016-10-04 20:14:42 +0900241
242 return new DefaultInfoRequestBuilder()
Jian Li6ef1b3f2016-11-12 18:16:06 +0900243 .withIsInfoReply(lispInfo.isInfoReply())
Jian Li27759352016-10-04 20:14:42 +0900244 .withNonce(lispInfo.getNonce())
245 .withKeyId(lispInfo.getKeyId())
246 .withAuthDataLength(lispInfo.getAuthDataLength())
Jian Lid1a109e2016-11-12 09:00:42 +0900247 .withAuthData(lispInfo.getAuthData())
Jian Li27759352016-10-04 20:14:42 +0900248 .withTtl(lispInfo.getTtl())
249 .withMaskLength(lispInfo.getMaskLength())
250 .withEidPrefix(lispInfo.getPrefix()).build();
251 }
252 }
253
254 /**
255 * A LISP message writer for InfoRequest message.
256 */
yoonseon980cd7c2016-11-18 14:18:46 -0800257 public static final class InfoRequestWriter
258 implements LispMessageWriter<LispInfoRequest> {
Jian Li27759352016-10-04 20:14:42 +0900259
260 @Override
yoonseon980cd7c2016-11-18 14:18:46 -0800261 public void writeTo(ByteBuf byteBuf, LispInfoRequest message)
262 throws LispWriterException {
263
Jian Li5e505c62016-12-05 02:44:24 +0900264 serialize(byteBuf, message);
yoonseon980cd7c2016-11-18 14:18:46 -0800265
266 //Fill AFI=0, no address
267 new LispAfiAddress.AfiAddressWriter()
268 .writeTo(byteBuf, new LispNoAddress());
269
Jian Li27759352016-10-04 20:14:42 +0900270 }
271 }
272}