Jian Li | 2775935 | 2016-10-04 20:14:42 +0900 | [diff] [blame] | 1 | /* |
| 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 | */ |
| 16 | package org.onosproject.lisp.msg.protocols; |
| 17 | |
| 18 | import com.google.common.base.Objects; |
| 19 | import io.netty.buffer.ByteBuf; |
Jian Li | d1a109e | 2016-11-12 09:00:42 +0900 | [diff] [blame] | 20 | import io.netty.buffer.Unpooled; |
| 21 | import org.onosproject.lisp.msg.authentication.LispAuthenticationFactory; |
| 22 | import org.onosproject.lisp.msg.authentication.LispAuthenticationKeyEnum; |
Jian Li | 2775935 | 2016-10-04 20:14:42 +0900 | [diff] [blame] | 23 | import org.onosproject.lisp.msg.exceptions.LispParseError; |
| 24 | import org.onosproject.lisp.msg.exceptions.LispReaderException; |
| 25 | import org.onosproject.lisp.msg.exceptions.LispWriterException; |
| 26 | import org.onosproject.lisp.msg.types.LispAfiAddress; |
Jian Li | d1a109e | 2016-11-12 09:00:42 +0900 | [diff] [blame] | 27 | import org.slf4j.Logger; |
| 28 | import org.slf4j.LoggerFactory; |
Jian Li | 2775935 | 2016-10-04 20:14:42 +0900 | [diff] [blame] | 29 | |
| 30 | import java.util.Arrays; |
| 31 | |
| 32 | import static com.google.common.base.MoreObjects.toStringHelper; |
Jian Li | d1a109e | 2016-11-12 09:00:42 +0900 | [diff] [blame] | 33 | import static org.onosproject.lisp.msg.authentication.LispAuthenticationKeyEnum.valueOf; |
Jian Li | 2775935 | 2016-10-04 20:14:42 +0900 | [diff] [blame] | 34 | |
| 35 | /** |
| 36 | * Default LISP info request message class. |
| 37 | */ |
| 38 | public class DefaultLispInfoRequest extends DefaultLispInfo implements LispInfoRequest { |
| 39 | |
Jian Li | d1a109e | 2016-11-12 09:00:42 +0900 | [diff] [blame] | 40 | private static final Logger log = LoggerFactory.getLogger(DefaultLispInfoRequest.class); |
| 41 | |
Jian Li | 2775935 | 2016-10-04 20:14:42 +0900 | [diff] [blame] | 42 | /** |
| 43 | * A private constructor that protects object instantiation from external. |
| 44 | * |
Jian Li | d1a109e | 2016-11-12 09:00:42 +0900 | [diff] [blame] | 45 | * @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 Li | 2775935 | 2016-10-04 20:14:42 +0900 | [diff] [blame] | 53 | */ |
| 54 | protected DefaultLispInfoRequest(boolean infoReply, long nonce, short keyId, short authDataLength, |
Jian Li | d1a109e | 2016-11-12 09:00:42 +0900 | [diff] [blame] | 55 | byte[] authData, int ttl, byte maskLength, |
Jian Li | 2775935 | 2016-10-04 20:14:42 +0900 | [diff] [blame] | 56 | LispAfiAddress eidPrefix) { |
Jian Li | d1a109e | 2016-11-12 09:00:42 +0900 | [diff] [blame] | 57 | super(infoReply, nonce, keyId, authDataLength, authData, ttl, maskLength, eidPrefix); |
Jian Li | 2775935 | 2016-10-04 20:14:42 +0900 | [diff] [blame] | 58 | } |
| 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 Li | d1a109e | 2016-11-12 09:00:42 +0900 | [diff] [blame] | 67 | .add("authentication data", authData) |
Jian Li | 2775935 | 2016-10-04 20:14:42 +0900 | [diff] [blame] | 68 | .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 Li | d1a109e | 2016-11-12 09:00:42 +0900 | [diff] [blame] | 86 | Arrays.equals(authData, that.authData) && |
Jian Li | 2775935 | 2016-10-04 20:14:42 +0900 | [diff] [blame] | 87 | 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 Li | d1a109e | 2016-11-12 09:00:42 +0900 | [diff] [blame] | 95 | eidPrefix) + Arrays.hashCode(authData); |
Jian Li | 2775935 | 2016-10-04 20:14:42 +0900 | [diff] [blame] | 96 | } |
| 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 Li | d1a109e | 2016-11-12 09:00:42 +0900 | [diff] [blame] | 104 | private byte[] authData; |
| 105 | private String authKey; |
Jian Li | 2775935 | 2016-10-04 20:14:42 +0900 | [diff] [blame] | 106 | 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 Li | 6ef1b3f | 2016-11-12 18:16:06 +0900 | [diff] [blame] | 117 | public InfoRequestBuilder withIsInfoReply(boolean infoReply) { |
Jian Li | 2775935 | 2016-10-04 20:14:42 +0900 | [diff] [blame] | 118 | 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 Li | d1a109e | 2016-11-12 09:00:42 +0900 | [diff] [blame] | 141 | public InfoRequestBuilder withAuthData(byte[] authenticationData) { |
Jian Li | 2775935 | 2016-10-04 20:14:42 +0900 | [diff] [blame] | 142 | if (authenticationData != null) { |
Jian Li | d1a109e | 2016-11-12 09:00:42 +0900 | [diff] [blame] | 143 | this.authData = authenticationData; |
Jian Li | 2775935 | 2016-10-04 20:14:42 +0900 | [diff] [blame] | 144 | } |
| 145 | return this; |
| 146 | } |
| 147 | |
| 148 | @Override |
Jian Li | d1a109e | 2016-11-12 09:00:42 +0900 | [diff] [blame] | 149 | public InfoRequestBuilder withAuthKey(String key) { |
| 150 | this.authKey = key; |
| 151 | return this; |
| 152 | } |
| 153 | |
| 154 | @Override |
Jian Li | 2775935 | 2016-10-04 20:14:42 +0900 | [diff] [blame] | 155 | 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 Li | d1a109e | 2016-11-12 09:00:42 +0900 | [diff] [blame] | 174 | |
| 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 Li | 2775935 | 2016-10-04 20:14:42 +0900 | [diff] [blame] | 202 | return new DefaultLispInfoRequest(infoReply, nonce, keyId, |
Jian Li | d1a109e | 2016-11-12 09:00:42 +0900 | [diff] [blame] | 203 | authDataLength, authData, ttl, maskLength, eidPrefix); |
Jian Li | 2775935 | 2016-10-04 20:14:42 +0900 | [diff] [blame] | 204 | } |
| 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 Li | 6ef1b3f | 2016-11-12 18:16:06 +0900 | [diff] [blame] | 218 | .withIsInfoReply(lispInfo.isInfoReply()) |
Jian Li | 2775935 | 2016-10-04 20:14:42 +0900 | [diff] [blame] | 219 | .withNonce(lispInfo.getNonce()) |
| 220 | .withKeyId(lispInfo.getKeyId()) |
| 221 | .withAuthDataLength(lispInfo.getAuthDataLength()) |
Jian Li | d1a109e | 2016-11-12 09:00:42 +0900 | [diff] [blame] | 222 | .withAuthData(lispInfo.getAuthData()) |
Jian Li | 2775935 | 2016-10-04 20:14:42 +0900 | [diff] [blame] | 223 | .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 | } |