blob: d2d7bdb7e21a7f0478b87536757fe343d13c381d [file] [log] [blame]
Jian Li451175e2016-07-19 23:22:20 +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
Jian Li20850d32016-08-04 02:15:57 +090018import com.google.common.base.Objects;
Jian Lif59c0ad2016-08-02 18:11:30 +090019import com.google.common.collect.ImmutableList;
20import com.google.common.collect.Lists;
Jian Li451175e2016-07-19 23:22:20 +090021import io.netty.buffer.ByteBuf;
Jian Li47671902016-08-11 01:18:18 +090022import org.onlab.util.ByteOperator;
Jian Li26069e22016-08-10 22:00:52 +090023import org.onosproject.lisp.msg.exceptions.LispParseError;
Jian Li719b3bf2016-07-22 00:38:29 +090024import org.onosproject.lisp.msg.types.LispAfiAddress;
25
26import java.util.List;
Jian Li451175e2016-07-19 23:22:20 +090027
Jian Li20850d32016-08-04 02:15:57 +090028import static com.google.common.base.MoreObjects.toStringHelper;
29
Jian Li451175e2016-07-19 23:22:20 +090030/**
31 * Default LISP map request message class.
32 */
Jian Lif59c0ad2016-08-02 18:11:30 +090033public final class DefaultLispMapRequest implements LispMapRequest {
34
35 private final long nonce;
36 private final byte recordCount;
37 private final LispAfiAddress sourceEid;
38 private final List<LispAfiAddress> itrRlocs;
39 private final List<LispEidRecord> eidRecords;
40 private final boolean authoritative;
41 private final boolean mapDataPresent;
42 private final boolean probe;
43 private final boolean smr;
44 private final boolean pitr;
45 private final boolean smrInvoked;
46
47 /**
48 * A private constructor that protects object instantiation from external.
49 *
50 * @param nonce nonce
51 * @param recordCount record count number
52 * @param sourceEid source EID address
53 * @param itrRlocs a collection of ITR RLOCs
54 * @param eidRecords a collection of EID records
55 * @param authoritative authoritative flag
56 * @param mapDataPresent map data present flag
57 * @param probe probe flag
58 * @param smr smr flag
59 * @param pitr pitr flag
60 * @param smrInvoked smrInvoked flag
61 */
62 private DefaultLispMapRequest(long nonce, byte recordCount, LispAfiAddress sourceEid,
63 List<LispAfiAddress> itrRlocs, List<LispEidRecord> eidRecords,
64 boolean authoritative, boolean mapDataPresent, boolean probe,
65 boolean smr, boolean pitr, boolean smrInvoked) {
66 this.nonce = nonce;
67 this.recordCount = recordCount;
68 this.sourceEid = sourceEid;
69 this.itrRlocs = itrRlocs;
70 this.eidRecords = eidRecords;
71 this.authoritative = authoritative;
72 this.mapDataPresent = mapDataPresent;
73 this.probe = probe;
74 this.smr = smr;
75 this.pitr = pitr;
76 this.smrInvoked = smrInvoked;
77 }
78
Jian Li451175e2016-07-19 23:22:20 +090079 @Override
80 public LispType getType() {
Jian Lif59c0ad2016-08-02 18:11:30 +090081 return LispType.LISP_MAP_REQUEST;
Jian Li451175e2016-07-19 23:22:20 +090082 }
83
84 @Override
85 public void writeTo(ByteBuf byteBuf) {
Jian Lif59c0ad2016-08-02 18:11:30 +090086 // TODO: serialize LispMapRequest message
Jian Li451175e2016-07-19 23:22:20 +090087 }
88
89 @Override
90 public Builder createBuilder() {
Jian Li525fded2016-08-04 01:15:33 +090091 return new DefaultRequestBuilder();
Jian Li451175e2016-07-19 23:22:20 +090092 }
Jian Li719b3bf2016-07-22 00:38:29 +090093
94 @Override
95 public boolean isAuthoritative() {
Jian Lif59c0ad2016-08-02 18:11:30 +090096 return this.authoritative;
97 }
98
99 @Override
100 public boolean isMapDataPresent() {
101 return this.mapDataPresent;
Jian Li719b3bf2016-07-22 00:38:29 +0900102 }
103
104 @Override
105 public boolean isProbe() {
Jian Lif59c0ad2016-08-02 18:11:30 +0900106 return this.probe;
Jian Li719b3bf2016-07-22 00:38:29 +0900107 }
108
109 @Override
110 public boolean isSmr() {
Jian Lif59c0ad2016-08-02 18:11:30 +0900111 return this.smr;
Jian Li719b3bf2016-07-22 00:38:29 +0900112 }
113
114 @Override
115 public boolean isPitr() {
Jian Lif59c0ad2016-08-02 18:11:30 +0900116 return this.pitr;
Jian Li719b3bf2016-07-22 00:38:29 +0900117 }
118
119 @Override
120 public boolean isSmrInvoked() {
Jian Lif59c0ad2016-08-02 18:11:30 +0900121 return this.smrInvoked;
Jian Li719b3bf2016-07-22 00:38:29 +0900122 }
123
124 @Override
125 public byte getRecordCount() {
Jian Lif59c0ad2016-08-02 18:11:30 +0900126 return this.recordCount;
Jian Li719b3bf2016-07-22 00:38:29 +0900127 }
128
129 @Override
130 public long getNonce() {
Jian Lif59c0ad2016-08-02 18:11:30 +0900131 return this.nonce;
Jian Li719b3bf2016-07-22 00:38:29 +0900132 }
133
134 @Override
135 public LispAfiAddress getSourceEid() {
Jian Lif59c0ad2016-08-02 18:11:30 +0900136 return this.sourceEid;
Jian Li719b3bf2016-07-22 00:38:29 +0900137 }
138
139 @Override
140 public List<LispAfiAddress> getItrRlocs() {
Jian Lif59c0ad2016-08-02 18:11:30 +0900141 return ImmutableList.copyOf(itrRlocs);
Jian Li719b3bf2016-07-22 00:38:29 +0900142 }
143
144 @Override
Jian Li10a09062016-07-26 23:58:50 +0900145 public List<LispEidRecord> getEids() {
Jian Lif59c0ad2016-08-02 18:11:30 +0900146 return ImmutableList.copyOf(eidRecords);
Jian Li719b3bf2016-07-22 00:38:29 +0900147 }
148
Jian Li20850d32016-08-04 02:15:57 +0900149 @Override
150 public String toString() {
151 return toStringHelper(this)
152 .add("type", getType())
153 .add("nonce", nonce)
154 .add("recordCount", recordCount)
155 .add("source EID", sourceEid)
156 .add("ITR rlocs", itrRlocs)
157 .add("EID records", eidRecords)
158 .add("authoritative", authoritative)
159 .add("mapDataPresent", mapDataPresent)
160 .add("probe", probe)
161 .add("SMR", smr)
162 .add("Proxy ITR", pitr)
163 .add("SMR Invoked", smrInvoked).toString();
164 }
165
166 @Override
167 public boolean equals(Object o) {
168 if (this == o) {
169 return true;
170 }
171 if (o == null || getClass() != o.getClass()) {
172 return false;
173 }
174 DefaultLispMapRequest that = (DefaultLispMapRequest) o;
175 return Objects.equal(nonce, that.nonce) &&
176 Objects.equal(recordCount, that.recordCount) &&
177 Objects.equal(sourceEid, that.sourceEid) &&
178 Objects.equal(itrRlocs, that.itrRlocs) &&
179 Objects.equal(eidRecords, that.eidRecords) &&
180 Objects.equal(authoritative, that.authoritative) &&
181 Objects.equal(mapDataPresent, that.mapDataPresent) &&
182 Objects.equal(probe, that.probe) &&
183 Objects.equal(smr, that.smr) &&
184 Objects.equal(pitr, that.pitr) &&
185 Objects.equal(smrInvoked, that.smrInvoked);
186 }
187
188 @Override
189 public int hashCode() {
190 return Objects.hashCode(nonce, recordCount, sourceEid, itrRlocs, eidRecords,
191 authoritative, mapDataPresent, probe, smr, pitr, smrInvoked);
192 }
193
Jian Li719b3bf2016-07-22 00:38:29 +0900194 public static final class DefaultRequestBuilder implements RequestBuilder {
195
Jian Lif59c0ad2016-08-02 18:11:30 +0900196 private long nonce;
197 private byte recordCount;
198 private LispAfiAddress sourceEid;
199 private List<LispAfiAddress> itrRlocs = Lists.newArrayList();
200 private List<LispEidRecord> eidRecords = Lists.newArrayList();
201 private boolean authoritative;
202 private boolean mapDataPresent;
203 private boolean probe;
204 private boolean smr;
205 private boolean pitr;
206 private boolean smrInvoked;
Jian Li719b3bf2016-07-22 00:38:29 +0900207
208 @Override
209 public LispType getType() {
Jian Lif59c0ad2016-08-02 18:11:30 +0900210 return LispType.LISP_MAP_REQUEST;
Jian Li719b3bf2016-07-22 00:38:29 +0900211 }
212
213 @Override
Jian Lif59c0ad2016-08-02 18:11:30 +0900214 public RequestBuilder withIsAuthoritative(boolean authoritative) {
215 this.authoritative = authoritative;
216 return this;
Jian Li719b3bf2016-07-22 00:38:29 +0900217 }
218
219 @Override
Jian Lif59c0ad2016-08-02 18:11:30 +0900220 public RequestBuilder withIsProbe(boolean probe) {
221 this.probe = probe;
222 return this;
Jian Li719b3bf2016-07-22 00:38:29 +0900223 }
224
225 @Override
Jian Lif59c0ad2016-08-02 18:11:30 +0900226 public RequestBuilder withIsMapDataPresent(boolean mapDataPresent) {
227 this.mapDataPresent = mapDataPresent;
228 return this;
229 }
230
Jian Lif59c0ad2016-08-02 18:11:30 +0900231 @Override
232 public RequestBuilder withIsSmr(boolean smr) {
233 this.smr = smr;
234 return this;
Jian Li719b3bf2016-07-22 00:38:29 +0900235 }
236
237 @Override
Jian Lif59c0ad2016-08-02 18:11:30 +0900238 public RequestBuilder withIsPitr(boolean pitr) {
239 this.pitr = pitr;
240 return this;
Jian Li719b3bf2016-07-22 00:38:29 +0900241 }
242
243 @Override
Jian Lif59c0ad2016-08-02 18:11:30 +0900244 public RequestBuilder withIsSmrInvoked(boolean smrInvoked) {
245 this.smrInvoked = smrInvoked;
246 return this;
Jian Li719b3bf2016-07-22 00:38:29 +0900247 }
248
249 @Override
250 public RequestBuilder withRecordCount(byte recordCount) {
Jian Lif59c0ad2016-08-02 18:11:30 +0900251 this.recordCount = recordCount;
252 return this;
Jian Li719b3bf2016-07-22 00:38:29 +0900253 }
254
255 @Override
256 public RequestBuilder withNonce(long nonce) {
Jian Lif59c0ad2016-08-02 18:11:30 +0900257 this.nonce = nonce;
258 return this;
Jian Li719b3bf2016-07-22 00:38:29 +0900259 }
260
261 @Override
Jian Lif59c0ad2016-08-02 18:11:30 +0900262 public RequestBuilder withSourceEid(LispAfiAddress sourceEid) {
263 this.sourceEid = sourceEid;
264 return this;
265 }
266
267 @Override
Jian Li47671902016-08-11 01:18:18 +0900268 public RequestBuilder withItrRlocs(List<LispAfiAddress> itrRlocs) {
269 this.itrRlocs = ImmutableList.copyOf(itrRlocs);
Jian Lif59c0ad2016-08-02 18:11:30 +0900270 return this;
Jian Li719b3bf2016-07-22 00:38:29 +0900271 }
272
273 @Override
Jian Li47671902016-08-11 01:18:18 +0900274 public RequestBuilder withEidRecords(List<LispEidRecord> records) {
275 this.eidRecords = ImmutableList.copyOf(records);
Jian Lif59c0ad2016-08-02 18:11:30 +0900276 return this;
277 }
278
279 @Override
Jian Li525fded2016-08-04 01:15:33 +0900280 public LispMapRequest build() {
Jian Lif59c0ad2016-08-02 18:11:30 +0900281 return new DefaultLispMapRequest(nonce, recordCount, sourceEid, itrRlocs,
282 eidRecords, authoritative, mapDataPresent, probe, smr, pitr, smrInvoked);
Jian Li719b3bf2016-07-22 00:38:29 +0900283 }
284 }
Jian Li26069e22016-08-10 22:00:52 +0900285
286 /**
287 * A private LISP message reader for MapRequest message.
288 */
289 private static class RequestReader implements LispMessageReader<LispMapRequest> {
290
Jian Li47671902016-08-11 01:18:18 +0900291 private static final int AUTHORITATIVE_INDEX = 3;
292 private static final int MAP_DATA_PRESENT_INDEX = 2;
293 private static final int PROBE_INDEX = 1;
294 private static final int SMR_INDEX = 0;
295 private static final int PITR_INDEX = 7;
296 private static final int SMR_INVOKED_INDEX = 6;
297
Jian Li26069e22016-08-10 22:00:52 +0900298 @Override
299 public LispMapRequest readFrom(ByteBuf byteBuf) throws LispParseError {
Jian Li47671902016-08-11 01:18:18 +0900300
301 if (byteBuf.readerIndex() != 0) {
302 return null;
303 }
304
305 byte typeWithFlags = byteBuf.readByte();
306
307 // authoritative -> 1 bit
308 boolean authoritative = ByteOperator.getBit(typeWithFlags, AUTHORITATIVE_INDEX);
309
310 // mapDataPresent -> 1 bit
311 boolean mapDataPresent = ByteOperator.getBit(typeWithFlags, MAP_DATA_PRESENT_INDEX);
312
313 // probe -> 1 bit
314 boolean probe = ByteOperator.getBit(typeWithFlags, PROBE_INDEX);
315
316 // smr -> 1 bit
317 boolean smr = ByteOperator.getBit(typeWithFlags, SMR_INDEX);
318
319 byte reservedWithFlags = byteBuf.readByte();
320
321 // pitr -> 1 bit
322 boolean pitr = ByteOperator.getBit(reservedWithFlags, PITR_INDEX);
323
324 // smrInvoked -> 1 bit
325 boolean smrInvoked = ByteOperator.getBit(reservedWithFlags, SMR_INVOKED_INDEX);
326
327 // let's skip reserved field, only obtains ITR counter value
328 // assume that first 3 bits are all set as 0,
329 // remain 5 bits represent Itr Rloc Counter (IRC)
330 int irc = byteBuf.readUnsignedShort();
331
332 // record count -> 8 bits
333 int recordCount = byteBuf.readUnsignedShort();
334
335 // nonce -> 64 bits
336 long nonce = byteBuf.readLong();
337
338 // TODO: de-serialize source EID AFI and address
339
340 // TODO: de-serialize ITR-RLOC AFI and address
341
342 // TODO: de-serialize EID-RECORD
343
344 return new DefaultRequestBuilder()
345 .withIsAuthoritative(authoritative)
346 .withIsMapDataPresent(mapDataPresent)
347 .withIsProbe(probe)
348 .withIsSmr(smr)
349 .withIsPitr(pitr)
350 .withIsSmrInvoked(smrInvoked)
351 .withNonce(nonce)
352 .withRecordCount((byte) recordCount)
353 .build();
Jian Li26069e22016-08-10 22:00:52 +0900354 }
355 }
Jian Li451175e2016-07-19 23:22:20 +0900356}