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