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; |
| 20 | import org.onosproject.lisp.msg.exceptions.LispParseError; |
| 21 | import org.onosproject.lisp.msg.exceptions.LispReaderException; |
| 22 | import org.onosproject.lisp.msg.exceptions.LispWriterException; |
| 23 | import org.onosproject.lisp.msg.types.LispAfiAddress; |
| 24 | |
| 25 | import java.util.Arrays; |
| 26 | |
| 27 | import static com.google.common.base.MoreObjects.toStringHelper; |
| 28 | |
| 29 | /** |
| 30 | * Default LISP info request message class. |
| 31 | */ |
| 32 | public class DefaultLispInfoRequest extends DefaultLispInfo implements LispInfoRequest { |
| 33 | |
| 34 | /** |
| 35 | * A private constructor that protects object instantiation from external. |
| 36 | * |
| 37 | * @param infoReply info reply flag |
| 38 | * @param nonce nonce |
| 39 | * @param keyId key identifier |
| 40 | * @param authDataLength authentication data length |
| 41 | * @param authenticationData authentication data |
| 42 | * @param ttl Time-To-Live value |
| 43 | * @param maskLength EID prefix mask length |
| 44 | * @param eidPrefix EID prefix |
| 45 | */ |
| 46 | protected DefaultLispInfoRequest(boolean infoReply, long nonce, short keyId, short authDataLength, |
| 47 | byte[] authenticationData, int ttl, byte maskLength, |
| 48 | LispAfiAddress eidPrefix) { |
| 49 | super(infoReply, nonce, keyId, authDataLength, authenticationData, ttl, maskLength, eidPrefix); |
| 50 | } |
| 51 | |
| 52 | @Override |
| 53 | public String toString() { |
| 54 | return toStringHelper(this) |
| 55 | .add("type", getType()) |
| 56 | .add("nonce", nonce) |
| 57 | .add("keyId", keyId) |
| 58 | .add("authentication data length", authDataLength) |
| 59 | .add("authentication data", authenticationData) |
| 60 | .add("TTL", ttl) |
| 61 | .add("EID mask length", maskLength) |
| 62 | .add("EID prefix", eidPrefix).toString(); |
| 63 | } |
| 64 | |
| 65 | @Override |
| 66 | public boolean equals(Object o) { |
| 67 | if (this == o) { |
| 68 | return true; |
| 69 | } |
| 70 | if (o == null || getClass() != o.getClass()) { |
| 71 | return false; |
| 72 | } |
| 73 | |
| 74 | DefaultLispInfoRequest that = (DefaultLispInfoRequest) o; |
| 75 | return Objects.equal(nonce, that.nonce) && |
| 76 | Objects.equal(keyId, that.keyId) && |
| 77 | Objects.equal(authDataLength, that.authDataLength) && |
| 78 | Arrays.equals(authenticationData, that.authenticationData) && |
| 79 | Objects.equal(ttl, that.ttl) && |
| 80 | Objects.equal(maskLength, that.maskLength) && |
| 81 | Objects.equal(eidPrefix, that.eidPrefix); |
| 82 | } |
| 83 | |
| 84 | @Override |
| 85 | public int hashCode() { |
| 86 | return Objects.hashCode(nonce, keyId, authDataLength, ttl, maskLength, |
| 87 | eidPrefix) + Arrays.hashCode(authenticationData); |
| 88 | } |
| 89 | |
| 90 | public static final class DefaultInfoRequestBuilder implements InfoRequestBuilder { |
| 91 | |
| 92 | private boolean infoReply; |
| 93 | private long nonce; |
| 94 | private short keyId; |
| 95 | private short authDataLength; |
| 96 | private byte[] authenticationData = new byte[0]; |
| 97 | private int ttl; |
| 98 | private byte maskLength; |
| 99 | private LispAfiAddress eidPrefix; |
| 100 | |
| 101 | @Override |
| 102 | public LispType getType() { |
| 103 | return LispType.LISP_INFO; |
| 104 | } |
| 105 | |
| 106 | |
| 107 | @Override |
| 108 | public InfoRequestBuilder withInfoReply(boolean infoReply) { |
| 109 | this.infoReply = infoReply; |
| 110 | return this; |
| 111 | } |
| 112 | |
| 113 | @Override |
| 114 | public InfoRequestBuilder withNonce(long nonce) { |
| 115 | this.nonce = nonce; |
| 116 | return this; |
| 117 | } |
| 118 | |
| 119 | @Override |
| 120 | public InfoRequestBuilder withAuthDataLength(short authDataLength) { |
| 121 | this.authDataLength = authDataLength; |
| 122 | return this; |
| 123 | } |
| 124 | |
| 125 | @Override |
| 126 | public InfoRequestBuilder withKeyId(short keyId) { |
| 127 | this.keyId = keyId; |
| 128 | return this; |
| 129 | } |
| 130 | |
| 131 | @Override |
| 132 | public InfoRequestBuilder withAuthenticationData(byte[] authenticationData) { |
| 133 | if (authenticationData != null) { |
| 134 | this.authenticationData = authenticationData; |
| 135 | } |
| 136 | return this; |
| 137 | } |
| 138 | |
| 139 | @Override |
| 140 | public InfoRequestBuilder withTtl(int ttl) { |
| 141 | this.ttl = ttl; |
| 142 | return this; |
| 143 | } |
| 144 | |
| 145 | @Override |
| 146 | public InfoRequestBuilder withMaskLength(byte maskLength) { |
| 147 | this.maskLength = maskLength; |
| 148 | return this; |
| 149 | } |
| 150 | |
| 151 | @Override |
| 152 | public InfoRequestBuilder withEidPrefix(LispAfiAddress eidPrefix) { |
| 153 | this.eidPrefix = eidPrefix; |
| 154 | return this; |
| 155 | } |
| 156 | |
| 157 | @Override |
| 158 | public LispInfoRequest build() { |
| 159 | return new DefaultLispInfoRequest(infoReply, nonce, keyId, |
| 160 | authDataLength, authenticationData, ttl, maskLength, eidPrefix); |
| 161 | } |
| 162 | } |
| 163 | |
| 164 | /** |
| 165 | * A LISP message reader for InfoRequest message. |
| 166 | */ |
| 167 | public static class InfoRequestReader implements LispMessageReader<LispInfoRequest> { |
| 168 | |
| 169 | @Override |
| 170 | public LispInfoRequest readFrom(ByteBuf byteBuf) throws LispParseError, LispReaderException { |
| 171 | |
| 172 | LispInfo lispInfo = DefaultLispInfo.deserialize(byteBuf); |
| 173 | |
| 174 | return new DefaultInfoRequestBuilder() |
| 175 | .withInfoReply(lispInfo.hasInfoReply()) |
| 176 | .withNonce(lispInfo.getNonce()) |
| 177 | .withKeyId(lispInfo.getKeyId()) |
| 178 | .withAuthDataLength(lispInfo.getAuthDataLength()) |
| 179 | .withAuthenticationData(lispInfo.getAuthenticationData()) |
| 180 | .withTtl(lispInfo.getTtl()) |
| 181 | .withMaskLength(lispInfo.getMaskLength()) |
| 182 | .withEidPrefix(lispInfo.getPrefix()).build(); |
| 183 | } |
| 184 | } |
| 185 | |
| 186 | /** |
| 187 | * A LISP message writer for InfoRequest message. |
| 188 | */ |
| 189 | public static final class InfoRequestWriter implements LispMessageWriter<LispInfoRequest> { |
| 190 | |
| 191 | @Override |
| 192 | public void writeTo(ByteBuf byteBuf, LispInfoRequest message) throws LispWriterException { |
| 193 | DefaultLispInfo.serialize(byteBuf, message); |
| 194 | } |
| 195 | } |
| 196 | } |