blob: 12dbc5db4fb0b1a6959ca08e58e9f0adb8bf10ed [file] [log] [blame]
Jian Li672ebda2017-02-06 20:21:04 +09001/*
2 * Copyright 2017-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
Jian Li4fc55e32017-02-07 06:25:50 +090018import com.google.common.base.Objects;
19import io.netty.buffer.ByteBuf;
20import org.onlab.packet.DeserializationException;
21import org.onosproject.lisp.msg.exceptions.LispParseError;
22import org.onosproject.lisp.msg.exceptions.LispReaderException;
23import org.onosproject.lisp.msg.exceptions.LispWriterException;
24
25import static com.google.common.base.MoreObjects.toStringHelper;
26
Jian Li672ebda2017-02-06 20:21:04 +090027/**
28 * Default LISP signature class.
29 */
Jian Li4fc55e32017-02-07 06:25:50 +090030public final class DefaultLispSignature implements LispSignature {
31
32 private final int recordTtl;
33 private final int sigExpiration;
34 private final int sigInception;
35 private final short keyTag;
36 private final short sigLength;
37 private final byte sigAlgorithm;
38 private final int signature;
39
40 /**
41 * A private constructor that protects object instantiation from external.
42 *
43 * @param recordTtl record time-to-live value
44 * @param sigExpiration signature expiration
45 * @param sigInception signature inception
46 * @param keyTag key tag
47 * @param sigLength signature length
48 * @param sigAlgorithm signature algorithm
49 * @param signature signature
50 */
51 private DefaultLispSignature(int recordTtl, int sigExpiration, int sigInception,
52 short keyTag, short sigLength, byte sigAlgorithm,
53 int signature) {
54 this.recordTtl = recordTtl;
55 this.sigExpiration = sigExpiration;
56 this.sigInception = sigInception;
57 this.keyTag = keyTag;
58 this.sigLength = sigLength;
59 this.sigAlgorithm = sigAlgorithm;
60 this.signature = signature;
61 }
62
63 @Override
64 public int getRecordTtl() {
65 return recordTtl;
66 }
67
68 @Override
69 public int getSigExpiration() {
70 return sigExpiration;
71 }
72
73 @Override
74 public int getSigInception() {
75 return sigInception;
76 }
77
78 @Override
79 public short getKeyTag() {
80 return keyTag;
81 }
82
83 @Override
84 public short getSigLength() {
85 return sigLength;
86 }
87
88 @Override
89 public byte getSigAlgorithm() {
90 return sigAlgorithm;
91 }
92
93 @Override
94 public int getSignature() {
95 return signature;
96 }
97
98 @Override
99 public void writeTo(ByteBuf byteBuf) throws LispWriterException {
100
101 }
102
103 @Override
104 public String toString() {
105 return toStringHelper(this)
106 .add("record TTL", recordTtl)
107 .add("signature expiration", sigExpiration)
108 .add("signature inception", sigInception)
109 .add("key tag", keyTag)
110 .add("signature length", sigLength)
111 .add("signature algorithm", sigAlgorithm)
112 .add("signature", signature)
113 .toString();
114 }
115
116 @Override
117 public boolean equals(Object o) {
118 if (this == o) {
119 return true;
120 }
121 if (o == null || getClass() != o.getClass()) {
122 return false;
123 }
124 DefaultLispSignature that = (DefaultLispSignature) o;
125 return Objects.equal(recordTtl, that.recordTtl) &&
126 Objects.equal(sigExpiration, that.sigExpiration) &&
127 Objects.equal(sigInception, that.sigInception) &&
128 Objects.equal(keyTag, that.keyTag) &&
129 Objects.equal(sigLength, that.sigLength) &&
130 Objects.equal(sigAlgorithm, that.sigAlgorithm) &&
131 Objects.equal(signature, that.signature);
132 }
133
134 @Override
135 public int hashCode() {
136 return Objects.hashCode(recordTtl, sigExpiration, sigInception,
137 keyTag, sigLength, sigAlgorithm, signature);
138 }
139
140 public static final class DefaultSignatureBuilder implements LispSignature.SignatureBuilder {
141
142 private int recordTtl;
143 private int sigExpiration;
144 private int sigInception;
145 private short keyTag;
146 private short sigLength;
147 private byte sigAlgorithm;
148 private int signature;
149
150 @Override
151 public SignatureBuilder withRecordTtl(int recordTtl) {
152 this.recordTtl = recordTtl;
153 return this;
154 }
155
156 @Override
157 public SignatureBuilder withSigExpiration(int sigExpiration) {
158 this.sigExpiration = sigExpiration;
159 return this;
160 }
161
162 @Override
163 public SignatureBuilder withSigInception(int sigInception) {
164 this.sigInception = sigInception;
165 return this;
166 }
167
168 @Override
169 public SignatureBuilder withKeyTag(short keyTag) {
170 this.keyTag = keyTag;
171 return this;
172 }
173
174 @Override
175 public SignatureBuilder withSigLength(short sigLength) {
176 this.sigLength = sigLength;
177 return this;
178 }
179
180 @Override
181 public SignatureBuilder withSigAlgorithm(byte sigAlgorithm) {
182 this.sigAlgorithm = sigAlgorithm;
183 return this;
184 }
185
186 @Override
187 public SignatureBuilder withSignature(int signature) {
188 this.signature = signature;
189 return this;
190 }
191
192 @Override
193 public LispSignature build() {
194
195 return new DefaultLispSignature(recordTtl, sigExpiration, sigInception,
196 keyTag, sigLength, sigAlgorithm, signature);
197 }
198 }
199
200 /**
201 * A LISP reader for Signature section.
202 */
203 public static final class SignatureReader
204 implements LispMessageReader<LispSignature> {
205
206 private static final int RESERVED_SKIP_LENGTH = 3;
207
208 @Override
209 public LispSignature readFrom(ByteBuf byteBuf) throws LispParseError,
210 LispReaderException, DeserializationException {
211
212 // record TTL -> 32 bits
213 int recordTtl = byteBuf.readInt();
214
215 // signature expiration -> 32 bits
216 int sigExpiration = byteBuf.readInt();
217
218 // signature inception -> 32 bits
219 int sigInception = byteBuf.readInt();
220
221 // key tag -> 16 bits
222 short keyTag = byteBuf.readShort();
223
224 // signature length -> 16 bits
225 short sigLength = byteBuf.readShort();
226
227 // signature algorithm -> 8 bits
228 byte sigAlgorithm = byteBuf.readByte();
229
230 byteBuf.skipBytes(RESERVED_SKIP_LENGTH);
231
232 // TODO: the size of signature should be determined by sigAlgorithm
233 int signature = byteBuf.readInt();
234
235 return new DefaultSignatureBuilder()
236 .withRecordTtl(recordTtl)
237 .withSigExpiration(sigExpiration)
238 .withSigInception(sigInception)
239 .withKeyTag(keyTag)
240 .withSigLength(sigLength)
241 .withSigAlgorithm(sigAlgorithm)
242 .withSignature(signature)
243 .build();
244 }
245 }
246
247 /**
248 * A LISP writer for Signature section.
249 */
250 public static final class SignatureWriter implements LispMessageWriter<LispSignature> {
251
252 private static final int UNUSED_ZERO = 0;
253
254 @Override
255 public void writeTo(ByteBuf byteBuf, LispSignature message) throws LispWriterException {
256
257 // record TTL
258 byteBuf.writeInt(message.getRecordTtl());
259
260 // signature expiration
261 byteBuf.writeInt(message.getSigExpiration());
262
263 // signature inception
264 byteBuf.writeInt(message.getSigInception());
265
266 // key tag
267 byteBuf.writeShort(message.getKeyTag());
268
269 // signature length
270 byteBuf.writeShort(message.getSigLength());
271
272 // signature algorithm
273 byteBuf.writeByte(message.getSigAlgorithm());
274
275 byteBuf.writeByte(UNUSED_ZERO);
276 byteBuf.writeShort(UNUSED_ZERO);
277
278 // signature
279 // TODO: the size of signature should be determined by sigAlgorithm
280 byteBuf.writeInt(message.getSignature());
281 }
282 }
Jian Li672ebda2017-02-06 20:21:04 +0900283}