Jian Li | 451175e | 2016-07-19 23:22:20 +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 | |
Jian Li | 20850d3 | 2016-08-04 02:15:57 +0900 | [diff] [blame] | 18 | import com.google.common.base.Objects; |
Jian Li | f59c0ad | 2016-08-02 18:11:30 +0900 | [diff] [blame] | 19 | import com.google.common.collect.ImmutableList; |
| 20 | import com.google.common.collect.Lists; |
Jian Li | 451175e | 2016-07-19 23:22:20 +0900 | [diff] [blame] | 21 | import io.netty.buffer.ByteBuf; |
Jian Li | 4767190 | 2016-08-11 01:18:18 +0900 | [diff] [blame] | 22 | import org.onlab.util.ByteOperator; |
Jian Li | 26069e2 | 2016-08-10 22:00:52 +0900 | [diff] [blame] | 23 | import org.onosproject.lisp.msg.exceptions.LispParseError; |
Jian Li | a7b394d | 2016-08-21 23:11:46 +0900 | [diff] [blame] | 24 | import org.onosproject.lisp.msg.exceptions.LispReaderException; |
Jian Li | 719b3bf | 2016-07-22 00:38:29 +0900 | [diff] [blame] | 25 | import org.onosproject.lisp.msg.types.LispAfiAddress; |
| 26 | |
| 27 | import java.util.List; |
Jian Li | 451175e | 2016-07-19 23:22:20 +0900 | [diff] [blame] | 28 | |
Jian Li | 20850d3 | 2016-08-04 02:15:57 +0900 | [diff] [blame] | 29 | import static com.google.common.base.MoreObjects.toStringHelper; |
| 30 | |
Jian Li | 451175e | 2016-07-19 23:22:20 +0900 | [diff] [blame] | 31 | /** |
| 32 | * Default LISP map request message class. |
| 33 | */ |
Jian Li | f59c0ad | 2016-08-02 18:11:30 +0900 | [diff] [blame] | 34 | public final class DefaultLispMapRequest implements LispMapRequest { |
| 35 | |
| 36 | private final long nonce; |
| 37 | private final byte recordCount; |
| 38 | private final LispAfiAddress sourceEid; |
| 39 | private final List<LispAfiAddress> itrRlocs; |
| 40 | private final List<LispEidRecord> eidRecords; |
| 41 | private final boolean authoritative; |
| 42 | private final boolean mapDataPresent; |
| 43 | private final boolean probe; |
| 44 | private final boolean smr; |
| 45 | private final boolean pitr; |
| 46 | private final boolean smrInvoked; |
| 47 | |
| 48 | /** |
| 49 | * A private constructor that protects object instantiation from external. |
| 50 | * |
| 51 | * @param nonce nonce |
| 52 | * @param recordCount record count number |
| 53 | * @param sourceEid source EID address |
| 54 | * @param itrRlocs a collection of ITR RLOCs |
| 55 | * @param eidRecords a collection of EID records |
| 56 | * @param authoritative authoritative flag |
| 57 | * @param mapDataPresent map data present flag |
| 58 | * @param probe probe flag |
| 59 | * @param smr smr flag |
| 60 | * @param pitr pitr flag |
| 61 | * @param smrInvoked smrInvoked flag |
| 62 | */ |
| 63 | private DefaultLispMapRequest(long nonce, byte recordCount, LispAfiAddress sourceEid, |
| 64 | List<LispAfiAddress> itrRlocs, List<LispEidRecord> eidRecords, |
| 65 | boolean authoritative, boolean mapDataPresent, boolean probe, |
| 66 | boolean smr, boolean pitr, boolean smrInvoked) { |
| 67 | this.nonce = nonce; |
| 68 | this.recordCount = recordCount; |
| 69 | this.sourceEid = sourceEid; |
| 70 | this.itrRlocs = itrRlocs; |
| 71 | this.eidRecords = eidRecords; |
| 72 | this.authoritative = authoritative; |
| 73 | this.mapDataPresent = mapDataPresent; |
| 74 | this.probe = probe; |
| 75 | this.smr = smr; |
| 76 | this.pitr = pitr; |
| 77 | this.smrInvoked = smrInvoked; |
| 78 | } |
| 79 | |
Jian Li | 451175e | 2016-07-19 23:22:20 +0900 | [diff] [blame] | 80 | @Override |
| 81 | public LispType getType() { |
Jian Li | f59c0ad | 2016-08-02 18:11:30 +0900 | [diff] [blame] | 82 | return LispType.LISP_MAP_REQUEST; |
Jian Li | 451175e | 2016-07-19 23:22:20 +0900 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | @Override |
| 86 | public void writeTo(ByteBuf byteBuf) { |
Jian Li | f59c0ad | 2016-08-02 18:11:30 +0900 | [diff] [blame] | 87 | // TODO: serialize LispMapRequest message |
Jian Li | 451175e | 2016-07-19 23:22:20 +0900 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | @Override |
| 91 | public Builder createBuilder() { |
Jian Li | 525fded | 2016-08-04 01:15:33 +0900 | [diff] [blame] | 92 | return new DefaultRequestBuilder(); |
Jian Li | 451175e | 2016-07-19 23:22:20 +0900 | [diff] [blame] | 93 | } |
Jian Li | 719b3bf | 2016-07-22 00:38:29 +0900 | [diff] [blame] | 94 | |
| 95 | @Override |
| 96 | public boolean isAuthoritative() { |
Jian Li | f59c0ad | 2016-08-02 18:11:30 +0900 | [diff] [blame] | 97 | return this.authoritative; |
| 98 | } |
| 99 | |
| 100 | @Override |
| 101 | public boolean isMapDataPresent() { |
| 102 | return this.mapDataPresent; |
Jian Li | 719b3bf | 2016-07-22 00:38:29 +0900 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | @Override |
| 106 | public boolean isProbe() { |
Jian Li | f59c0ad | 2016-08-02 18:11:30 +0900 | [diff] [blame] | 107 | return this.probe; |
Jian Li | 719b3bf | 2016-07-22 00:38:29 +0900 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | @Override |
| 111 | public boolean isSmr() { |
Jian Li | f59c0ad | 2016-08-02 18:11:30 +0900 | [diff] [blame] | 112 | return this.smr; |
Jian Li | 719b3bf | 2016-07-22 00:38:29 +0900 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | @Override |
| 116 | public boolean isPitr() { |
Jian Li | f59c0ad | 2016-08-02 18:11:30 +0900 | [diff] [blame] | 117 | return this.pitr; |
Jian Li | 719b3bf | 2016-07-22 00:38:29 +0900 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | @Override |
| 121 | public boolean isSmrInvoked() { |
Jian Li | f59c0ad | 2016-08-02 18:11:30 +0900 | [diff] [blame] | 122 | return this.smrInvoked; |
Jian Li | 719b3bf | 2016-07-22 00:38:29 +0900 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | @Override |
| 126 | public byte getRecordCount() { |
Jian Li | f59c0ad | 2016-08-02 18:11:30 +0900 | [diff] [blame] | 127 | return this.recordCount; |
Jian Li | 719b3bf | 2016-07-22 00:38:29 +0900 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | @Override |
| 131 | public long getNonce() { |
Jian Li | f59c0ad | 2016-08-02 18:11:30 +0900 | [diff] [blame] | 132 | return this.nonce; |
Jian Li | 719b3bf | 2016-07-22 00:38:29 +0900 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | @Override |
| 136 | public LispAfiAddress getSourceEid() { |
Jian Li | f59c0ad | 2016-08-02 18:11:30 +0900 | [diff] [blame] | 137 | return this.sourceEid; |
Jian Li | 719b3bf | 2016-07-22 00:38:29 +0900 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | @Override |
| 141 | public List<LispAfiAddress> getItrRlocs() { |
Jian Li | f59c0ad | 2016-08-02 18:11:30 +0900 | [diff] [blame] | 142 | return ImmutableList.copyOf(itrRlocs); |
Jian Li | 719b3bf | 2016-07-22 00:38:29 +0900 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | @Override |
Jian Li | 10a0906 | 2016-07-26 23:58:50 +0900 | [diff] [blame] | 146 | public List<LispEidRecord> getEids() { |
Jian Li | f59c0ad | 2016-08-02 18:11:30 +0900 | [diff] [blame] | 147 | return ImmutableList.copyOf(eidRecords); |
Jian Li | 719b3bf | 2016-07-22 00:38:29 +0900 | [diff] [blame] | 148 | } |
| 149 | |
Jian Li | 20850d3 | 2016-08-04 02:15:57 +0900 | [diff] [blame] | 150 | @Override |
| 151 | public String toString() { |
| 152 | return toStringHelper(this) |
| 153 | .add("type", getType()) |
| 154 | .add("nonce", nonce) |
| 155 | .add("recordCount", recordCount) |
| 156 | .add("source EID", sourceEid) |
| 157 | .add("ITR rlocs", itrRlocs) |
| 158 | .add("EID records", eidRecords) |
| 159 | .add("authoritative", authoritative) |
| 160 | .add("mapDataPresent", mapDataPresent) |
| 161 | .add("probe", probe) |
| 162 | .add("SMR", smr) |
| 163 | .add("Proxy ITR", pitr) |
| 164 | .add("SMR Invoked", smrInvoked).toString(); |
| 165 | } |
| 166 | |
| 167 | @Override |
| 168 | public boolean equals(Object o) { |
| 169 | if (this == o) { |
| 170 | return true; |
| 171 | } |
| 172 | if (o == null || getClass() != o.getClass()) { |
| 173 | return false; |
| 174 | } |
| 175 | DefaultLispMapRequest that = (DefaultLispMapRequest) o; |
| 176 | return Objects.equal(nonce, that.nonce) && |
| 177 | Objects.equal(recordCount, that.recordCount) && |
| 178 | Objects.equal(sourceEid, that.sourceEid) && |
| 179 | Objects.equal(itrRlocs, that.itrRlocs) && |
| 180 | Objects.equal(eidRecords, that.eidRecords) && |
| 181 | Objects.equal(authoritative, that.authoritative) && |
| 182 | Objects.equal(mapDataPresent, that.mapDataPresent) && |
| 183 | Objects.equal(probe, that.probe) && |
| 184 | Objects.equal(smr, that.smr) && |
| 185 | Objects.equal(pitr, that.pitr) && |
| 186 | Objects.equal(smrInvoked, that.smrInvoked); |
| 187 | } |
| 188 | |
| 189 | @Override |
| 190 | public int hashCode() { |
| 191 | return Objects.hashCode(nonce, recordCount, sourceEid, itrRlocs, eidRecords, |
| 192 | authoritative, mapDataPresent, probe, smr, pitr, smrInvoked); |
| 193 | } |
| 194 | |
Jian Li | 719b3bf | 2016-07-22 00:38:29 +0900 | [diff] [blame] | 195 | public static final class DefaultRequestBuilder implements RequestBuilder { |
| 196 | |
Jian Li | f59c0ad | 2016-08-02 18:11:30 +0900 | [diff] [blame] | 197 | private long nonce; |
| 198 | private byte recordCount; |
| 199 | private LispAfiAddress sourceEid; |
| 200 | private List<LispAfiAddress> itrRlocs = Lists.newArrayList(); |
| 201 | private List<LispEidRecord> eidRecords = Lists.newArrayList(); |
| 202 | private boolean authoritative; |
| 203 | private boolean mapDataPresent; |
| 204 | private boolean probe; |
| 205 | private boolean smr; |
| 206 | private boolean pitr; |
| 207 | private boolean smrInvoked; |
Jian Li | 719b3bf | 2016-07-22 00:38:29 +0900 | [diff] [blame] | 208 | |
| 209 | @Override |
| 210 | public LispType getType() { |
Jian Li | f59c0ad | 2016-08-02 18:11:30 +0900 | [diff] [blame] | 211 | return LispType.LISP_MAP_REQUEST; |
Jian Li | 719b3bf | 2016-07-22 00:38:29 +0900 | [diff] [blame] | 212 | } |
| 213 | |
| 214 | @Override |
Jian Li | f59c0ad | 2016-08-02 18:11:30 +0900 | [diff] [blame] | 215 | public RequestBuilder withIsAuthoritative(boolean authoritative) { |
| 216 | this.authoritative = authoritative; |
| 217 | return this; |
Jian Li | 719b3bf | 2016-07-22 00:38:29 +0900 | [diff] [blame] | 218 | } |
| 219 | |
| 220 | @Override |
Jian Li | f59c0ad | 2016-08-02 18:11:30 +0900 | [diff] [blame] | 221 | public RequestBuilder withIsProbe(boolean probe) { |
| 222 | this.probe = probe; |
| 223 | return this; |
Jian Li | 719b3bf | 2016-07-22 00:38:29 +0900 | [diff] [blame] | 224 | } |
| 225 | |
| 226 | @Override |
Jian Li | f59c0ad | 2016-08-02 18:11:30 +0900 | [diff] [blame] | 227 | public RequestBuilder withIsMapDataPresent(boolean mapDataPresent) { |
| 228 | this.mapDataPresent = mapDataPresent; |
| 229 | return this; |
| 230 | } |
| 231 | |
Jian Li | f59c0ad | 2016-08-02 18:11:30 +0900 | [diff] [blame] | 232 | @Override |
| 233 | public RequestBuilder withIsSmr(boolean smr) { |
| 234 | this.smr = smr; |
| 235 | return this; |
Jian Li | 719b3bf | 2016-07-22 00:38:29 +0900 | [diff] [blame] | 236 | } |
| 237 | |
| 238 | @Override |
Jian Li | f59c0ad | 2016-08-02 18:11:30 +0900 | [diff] [blame] | 239 | public RequestBuilder withIsPitr(boolean pitr) { |
| 240 | this.pitr = pitr; |
| 241 | return this; |
Jian Li | 719b3bf | 2016-07-22 00:38:29 +0900 | [diff] [blame] | 242 | } |
| 243 | |
| 244 | @Override |
Jian Li | f59c0ad | 2016-08-02 18:11:30 +0900 | [diff] [blame] | 245 | public RequestBuilder withIsSmrInvoked(boolean smrInvoked) { |
| 246 | this.smrInvoked = smrInvoked; |
| 247 | return this; |
Jian Li | 719b3bf | 2016-07-22 00:38:29 +0900 | [diff] [blame] | 248 | } |
| 249 | |
| 250 | @Override |
| 251 | public RequestBuilder withRecordCount(byte recordCount) { |
Jian Li | f59c0ad | 2016-08-02 18:11:30 +0900 | [diff] [blame] | 252 | this.recordCount = recordCount; |
| 253 | return this; |
Jian Li | 719b3bf | 2016-07-22 00:38:29 +0900 | [diff] [blame] | 254 | } |
| 255 | |
| 256 | @Override |
| 257 | public RequestBuilder withNonce(long nonce) { |
Jian Li | f59c0ad | 2016-08-02 18:11:30 +0900 | [diff] [blame] | 258 | this.nonce = nonce; |
| 259 | return this; |
Jian Li | 719b3bf | 2016-07-22 00:38:29 +0900 | [diff] [blame] | 260 | } |
| 261 | |
| 262 | @Override |
Jian Li | f59c0ad | 2016-08-02 18:11:30 +0900 | [diff] [blame] | 263 | public RequestBuilder withSourceEid(LispAfiAddress sourceEid) { |
| 264 | this.sourceEid = sourceEid; |
| 265 | return this; |
| 266 | } |
| 267 | |
| 268 | @Override |
Jian Li | 4767190 | 2016-08-11 01:18:18 +0900 | [diff] [blame] | 269 | public RequestBuilder withItrRlocs(List<LispAfiAddress> itrRlocs) { |
| 270 | this.itrRlocs = ImmutableList.copyOf(itrRlocs); |
Jian Li | f59c0ad | 2016-08-02 18:11:30 +0900 | [diff] [blame] | 271 | return this; |
Jian Li | 719b3bf | 2016-07-22 00:38:29 +0900 | [diff] [blame] | 272 | } |
| 273 | |
| 274 | @Override |
Jian Li | 4767190 | 2016-08-11 01:18:18 +0900 | [diff] [blame] | 275 | public RequestBuilder withEidRecords(List<LispEidRecord> records) { |
| 276 | this.eidRecords = ImmutableList.copyOf(records); |
Jian Li | f59c0ad | 2016-08-02 18:11:30 +0900 | [diff] [blame] | 277 | return this; |
| 278 | } |
| 279 | |
| 280 | @Override |
Jian Li | 525fded | 2016-08-04 01:15:33 +0900 | [diff] [blame] | 281 | public LispMapRequest build() { |
Jian Li | f59c0ad | 2016-08-02 18:11:30 +0900 | [diff] [blame] | 282 | return new DefaultLispMapRequest(nonce, recordCount, sourceEid, itrRlocs, |
| 283 | eidRecords, authoritative, mapDataPresent, probe, smr, pitr, smrInvoked); |
Jian Li | 719b3bf | 2016-07-22 00:38:29 +0900 | [diff] [blame] | 284 | } |
| 285 | } |
Jian Li | 26069e2 | 2016-08-10 22:00:52 +0900 | [diff] [blame] | 286 | |
| 287 | /** |
| 288 | * A private LISP message reader for MapRequest message. |
| 289 | */ |
| 290 | private static class RequestReader implements LispMessageReader<LispMapRequest> { |
| 291 | |
Jian Li | 4767190 | 2016-08-11 01:18:18 +0900 | [diff] [blame] | 292 | private static final int AUTHORITATIVE_INDEX = 3; |
| 293 | private static final int MAP_DATA_PRESENT_INDEX = 2; |
| 294 | private static final int PROBE_INDEX = 1; |
| 295 | private static final int SMR_INDEX = 0; |
| 296 | private static final int PITR_INDEX = 7; |
| 297 | private static final int SMR_INVOKED_INDEX = 6; |
| 298 | |
Jian Li | 26069e2 | 2016-08-10 22:00:52 +0900 | [diff] [blame] | 299 | @Override |
Jian Li | a7b394d | 2016-08-21 23:11:46 +0900 | [diff] [blame] | 300 | public LispMapRequest readFrom(ByteBuf byteBuf) throws LispParseError, LispReaderException { |
Jian Li | 4767190 | 2016-08-11 01:18:18 +0900 | [diff] [blame] | 301 | |
| 302 | if (byteBuf.readerIndex() != 0) { |
| 303 | return null; |
| 304 | } |
| 305 | |
| 306 | byte typeWithFlags = byteBuf.readByte(); |
| 307 | |
| 308 | // authoritative -> 1 bit |
| 309 | boolean authoritative = ByteOperator.getBit(typeWithFlags, AUTHORITATIVE_INDEX); |
| 310 | |
| 311 | // mapDataPresent -> 1 bit |
| 312 | boolean mapDataPresent = ByteOperator.getBit(typeWithFlags, MAP_DATA_PRESENT_INDEX); |
| 313 | |
| 314 | // probe -> 1 bit |
| 315 | boolean probe = ByteOperator.getBit(typeWithFlags, PROBE_INDEX); |
| 316 | |
| 317 | // smr -> 1 bit |
| 318 | boolean smr = ByteOperator.getBit(typeWithFlags, SMR_INDEX); |
| 319 | |
| 320 | byte reservedWithFlags = byteBuf.readByte(); |
| 321 | |
| 322 | // pitr -> 1 bit |
| 323 | boolean pitr = ByteOperator.getBit(reservedWithFlags, PITR_INDEX); |
| 324 | |
| 325 | // smrInvoked -> 1 bit |
| 326 | boolean smrInvoked = ByteOperator.getBit(reservedWithFlags, SMR_INVOKED_INDEX); |
| 327 | |
| 328 | // let's skip reserved field, only obtains ITR counter value |
| 329 | // assume that first 3 bits are all set as 0, |
| 330 | // remain 5 bits represent Itr Rloc Counter (IRC) |
| 331 | int irc = byteBuf.readUnsignedShort(); |
| 332 | |
| 333 | // record count -> 8 bits |
| 334 | int recordCount = byteBuf.readUnsignedShort(); |
| 335 | |
| 336 | // nonce -> 64 bits |
| 337 | long nonce = byteBuf.readLong(); |
| 338 | |
Jian Li | a7b394d | 2016-08-21 23:11:46 +0900 | [diff] [blame] | 339 | LispAfiAddress sourceEid = new LispAfiAddress.AfiAddressReader().readFrom(byteBuf); |
Jian Li | 4767190 | 2016-08-11 01:18:18 +0900 | [diff] [blame] | 340 | |
Jian Li | a7b394d | 2016-08-21 23:11:46 +0900 | [diff] [blame] | 341 | // deserialize a collection of RLOC addresses |
| 342 | List<LispAfiAddress> itrRlocs = Lists.newArrayList(); |
| 343 | for (int i = 0; i < irc; i++) { |
| 344 | itrRlocs.add(new LispAfiAddress.AfiAddressReader().readFrom(byteBuf)); |
| 345 | } |
Jian Li | 4767190 | 2016-08-11 01:18:18 +0900 | [diff] [blame] | 346 | |
Jian Li | a7b394d | 2016-08-21 23:11:46 +0900 | [diff] [blame] | 347 | // deserialize a collection of EID records |
| 348 | List<LispEidRecord> eidRecords = Lists.newArrayList(); |
| 349 | for (int i = 0; i < recordCount; i++) { |
| 350 | eidRecords.add(new LispEidRecord.EidRecordReader().readFrom(byteBuf)); |
| 351 | } |
Jian Li | 4767190 | 2016-08-11 01:18:18 +0900 | [diff] [blame] | 352 | |
| 353 | return new DefaultRequestBuilder() |
| 354 | .withIsAuthoritative(authoritative) |
| 355 | .withIsMapDataPresent(mapDataPresent) |
| 356 | .withIsProbe(probe) |
| 357 | .withIsSmr(smr) |
| 358 | .withIsPitr(pitr) |
| 359 | .withIsSmrInvoked(smrInvoked) |
| 360 | .withNonce(nonce) |
| 361 | .withRecordCount((byte) recordCount) |
Jian Li | a7b394d | 2016-08-21 23:11:46 +0900 | [diff] [blame] | 362 | .withSourceEid(sourceEid) |
| 363 | .withEidRecords(eidRecords) |
| 364 | .withItrRlocs(itrRlocs) |
Jian Li | 4767190 | 2016-08-11 01:18:18 +0900 | [diff] [blame] | 365 | .build(); |
Jian Li | 26069e2 | 2016-08-10 22:00:52 +0900 | [diff] [blame] | 366 | } |
| 367 | } |
Jian Li | 451175e | 2016-07-19 23:22:20 +0900 | [diff] [blame] | 368 | } |