blob: 18a055f43dde4ebc5c8f8874553748f2a602cfd1 [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;
22import org.onosproject.lisp.msg.authentication.LispAuthenticationKeyEnum;
Jian Li27759352016-10-04 20:14:42 +090023import org.onosproject.lisp.msg.exceptions.LispParseError;
24import org.onosproject.lisp.msg.exceptions.LispReaderException;
25import org.onosproject.lisp.msg.exceptions.LispWriterException;
26import org.onosproject.lisp.msg.types.LispAfiAddress;
Jian Lid1a109e2016-11-12 09:00:42 +090027import org.slf4j.Logger;
28import org.slf4j.LoggerFactory;
Jian Li27759352016-10-04 20:14:42 +090029
30import java.util.Arrays;
31
32import static com.google.common.base.MoreObjects.toStringHelper;
Jian Lid1a109e2016-11-12 09:00:42 +090033import static org.onosproject.lisp.msg.authentication.LispAuthenticationKeyEnum.valueOf;
Jian Li27759352016-10-04 20:14:42 +090034
35/**
36 * Default LISP info request message class.
37 */
38public class DefaultLispInfoRequest extends DefaultLispInfo implements LispInfoRequest {
39
Jian Lid1a109e2016-11-12 09:00:42 +090040 private static final Logger log = LoggerFactory.getLogger(DefaultLispInfoRequest.class);
41
Jian Li27759352016-10-04 20:14:42 +090042 /**
43 * A private constructor that protects object instantiation from external.
44 *
Jian Lid1a109e2016-11-12 09:00:42 +090045 * @param infoReply info reply flag
46 * @param nonce nonce
47 * @param keyId key identifier
48 * @param authDataLength authentication data length
49 * @param authData authentication data
50 * @param ttl Time-To-Live value
51 * @param maskLength EID prefix mask length
52 * @param eidPrefix EID prefix
Jian Li27759352016-10-04 20:14:42 +090053 */
54 protected DefaultLispInfoRequest(boolean infoReply, long nonce, short keyId, short authDataLength,
Jian Lid1a109e2016-11-12 09:00:42 +090055 byte[] authData, int ttl, byte maskLength,
Jian Li27759352016-10-04 20:14:42 +090056 LispAfiAddress eidPrefix) {
Jian Lid1a109e2016-11-12 09:00:42 +090057 super(infoReply, nonce, keyId, authDataLength, authData, ttl, maskLength, eidPrefix);
Jian Li27759352016-10-04 20:14:42 +090058 }
59
60 @Override
61 public String toString() {
62 return toStringHelper(this)
63 .add("type", getType())
64 .add("nonce", nonce)
65 .add("keyId", keyId)
66 .add("authentication data length", authDataLength)
Jian Lid1a109e2016-11-12 09:00:42 +090067 .add("authentication data", authData)
Jian Li27759352016-10-04 20:14:42 +090068 .add("TTL", ttl)
69 .add("EID mask length", maskLength)
70 .add("EID prefix", eidPrefix).toString();
71 }
72
73 @Override
74 public boolean equals(Object o) {
75 if (this == o) {
76 return true;
77 }
78 if (o == null || getClass() != o.getClass()) {
79 return false;
80 }
81
82 DefaultLispInfoRequest that = (DefaultLispInfoRequest) o;
83 return Objects.equal(nonce, that.nonce) &&
84 Objects.equal(keyId, that.keyId) &&
85 Objects.equal(authDataLength, that.authDataLength) &&
Jian Lid1a109e2016-11-12 09:00:42 +090086 Arrays.equals(authData, that.authData) &&
Jian Li27759352016-10-04 20:14:42 +090087 Objects.equal(ttl, that.ttl) &&
88 Objects.equal(maskLength, that.maskLength) &&
89 Objects.equal(eidPrefix, that.eidPrefix);
90 }
91
92 @Override
93 public int hashCode() {
94 return Objects.hashCode(nonce, keyId, authDataLength, ttl, maskLength,
Jian Lid1a109e2016-11-12 09:00:42 +090095 eidPrefix) + Arrays.hashCode(authData);
Jian Li27759352016-10-04 20:14:42 +090096 }
97
98 public static final class DefaultInfoRequestBuilder implements InfoRequestBuilder {
99
100 private boolean infoReply;
101 private long nonce;
102 private short keyId;
103 private short authDataLength;
Jian Lid1a109e2016-11-12 09:00:42 +0900104 private byte[] authData;
105 private String authKey;
Jian Li27759352016-10-04 20:14:42 +0900106 private int ttl;
107 private byte maskLength;
108 private LispAfiAddress eidPrefix;
109
110 @Override
111 public LispType getType() {
112 return LispType.LISP_INFO;
113 }
114
115
116 @Override
Jian Li6ef1b3f2016-11-12 18:16:06 +0900117 public InfoRequestBuilder withIsInfoReply(boolean infoReply) {
Jian Li27759352016-10-04 20:14:42 +0900118 this.infoReply = infoReply;
119 return this;
120 }
121
122 @Override
123 public InfoRequestBuilder withNonce(long nonce) {
124 this.nonce = nonce;
125 return this;
126 }
127
128 @Override
129 public InfoRequestBuilder withAuthDataLength(short authDataLength) {
130 this.authDataLength = authDataLength;
131 return this;
132 }
133
134 @Override
135 public InfoRequestBuilder withKeyId(short keyId) {
136 this.keyId = keyId;
137 return this;
138 }
139
140 @Override
Jian Lid1a109e2016-11-12 09:00:42 +0900141 public InfoRequestBuilder withAuthData(byte[] authenticationData) {
Jian Li27759352016-10-04 20:14:42 +0900142 if (authenticationData != null) {
Jian Lid1a109e2016-11-12 09:00:42 +0900143 this.authData = authenticationData;
Jian Li27759352016-10-04 20:14:42 +0900144 }
145 return this;
146 }
147
148 @Override
Jian Lid1a109e2016-11-12 09:00:42 +0900149 public InfoRequestBuilder withAuthKey(String key) {
150 this.authKey = key;
151 return this;
152 }
153
154 @Override
Jian Li27759352016-10-04 20:14:42 +0900155 public InfoRequestBuilder withTtl(int ttl) {
156 this.ttl = ttl;
157 return this;
158 }
159
160 @Override
161 public InfoRequestBuilder withMaskLength(byte maskLength) {
162 this.maskLength = maskLength;
163 return this;
164 }
165
166 @Override
167 public InfoRequestBuilder withEidPrefix(LispAfiAddress eidPrefix) {
168 this.eidPrefix = eidPrefix;
169 return this;
170 }
171
172 @Override
173 public LispInfoRequest build() {
Jian Lid1a109e2016-11-12 09:00:42 +0900174
175 // if authentication data is not specified, we will calculate it
176 if (authData == null) {
177 LispAuthenticationFactory factory = LispAuthenticationFactory.getInstance();
178
179 authDataLength = LispAuthenticationKeyEnum.valueOf(keyId).getHashLength();
180 byte[] tmpAuthData = new byte[authDataLength];
181 Arrays.fill(tmpAuthData, (byte) 0);
182 authData = tmpAuthData;
183
184 ByteBuf byteBuf = Unpooled.buffer();
185 try {
186 new DefaultLispInfoRequest(infoReply, nonce, keyId, authDataLength,
187 authData, ttl, maskLength, eidPrefix).writeTo(byteBuf);
188 } catch (LispWriterException e) {
189 log.warn("Failed to serialize info request", e);
190 }
191
192 byte[] bytes = new byte[byteBuf.readableBytes()];
193 byteBuf.readBytes(bytes);
194
195 if (authKey == null) {
196 log.warn("Must specify authentication key");
197 }
198
199 authData = factory.createAuthenticationData(valueOf(keyId), authKey, bytes);
200 }
201
Jian Li27759352016-10-04 20:14:42 +0900202 return new DefaultLispInfoRequest(infoReply, nonce, keyId,
Jian Lid1a109e2016-11-12 09:00:42 +0900203 authDataLength, authData, ttl, maskLength, eidPrefix);
Jian Li27759352016-10-04 20:14:42 +0900204 }
205 }
206
207 /**
208 * A LISP message reader for InfoRequest message.
209 */
210 public static class InfoRequestReader implements LispMessageReader<LispInfoRequest> {
211
212 @Override
213 public LispInfoRequest readFrom(ByteBuf byteBuf) throws LispParseError, LispReaderException {
214
215 LispInfo lispInfo = DefaultLispInfo.deserialize(byteBuf);
216
217 return new DefaultInfoRequestBuilder()
Jian Li6ef1b3f2016-11-12 18:16:06 +0900218 .withIsInfoReply(lispInfo.isInfoReply())
Jian Li27759352016-10-04 20:14:42 +0900219 .withNonce(lispInfo.getNonce())
220 .withKeyId(lispInfo.getKeyId())
221 .withAuthDataLength(lispInfo.getAuthDataLength())
Jian Lid1a109e2016-11-12 09:00:42 +0900222 .withAuthData(lispInfo.getAuthData())
Jian Li27759352016-10-04 20:14:42 +0900223 .withTtl(lispInfo.getTtl())
224 .withMaskLength(lispInfo.getMaskLength())
225 .withEidPrefix(lispInfo.getPrefix()).build();
226 }
227 }
228
229 /**
230 * A LISP message writer for InfoRequest message.
231 */
232 public static final class InfoRequestWriter implements LispMessageWriter<LispInfoRequest> {
233
234 @Override
235 public void writeTo(ByteBuf byteBuf, LispInfoRequest message) throws LispWriterException {
236 DefaultLispInfo.serialize(byteBuf, message);
237 }
238 }
239}