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 | 719b3bf | 2016-07-22 00:38:29 +0900 | [diff] [blame] | 24 | import org.onosproject.lisp.msg.types.LispAfiAddress; |
| 25 | |
| 26 | import java.util.List; |
Jian Li | 451175e | 2016-07-19 23:22:20 +0900 | [diff] [blame] | 27 | |
Jian Li | 20850d3 | 2016-08-04 02:15:57 +0900 | [diff] [blame] | 28 | import static com.google.common.base.MoreObjects.toStringHelper; |
| 29 | |
Jian Li | 451175e | 2016-07-19 23:22:20 +0900 | [diff] [blame] | 30 | /** |
| 31 | * Default LISP map request message class. |
| 32 | */ |
Jian Li | f59c0ad | 2016-08-02 18:11:30 +0900 | [diff] [blame] | 33 | public 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 Li | 451175e | 2016-07-19 23:22:20 +0900 | [diff] [blame] | 79 | @Override |
| 80 | public LispType getType() { |
Jian Li | f59c0ad | 2016-08-02 18:11:30 +0900 | [diff] [blame] | 81 | return LispType.LISP_MAP_REQUEST; |
Jian Li | 451175e | 2016-07-19 23:22:20 +0900 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | @Override |
| 85 | public void writeTo(ByteBuf byteBuf) { |
Jian Li | f59c0ad | 2016-08-02 18:11:30 +0900 | [diff] [blame] | 86 | // TODO: serialize LispMapRequest message |
Jian Li | 451175e | 2016-07-19 23:22:20 +0900 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | @Override |
| 90 | public Builder createBuilder() { |
Jian Li | 525fded | 2016-08-04 01:15:33 +0900 | [diff] [blame] | 91 | return new DefaultRequestBuilder(); |
Jian Li | 451175e | 2016-07-19 23:22:20 +0900 | [diff] [blame] | 92 | } |
Jian Li | 719b3bf | 2016-07-22 00:38:29 +0900 | [diff] [blame] | 93 | |
| 94 | @Override |
| 95 | public boolean isAuthoritative() { |
Jian Li | f59c0ad | 2016-08-02 18:11:30 +0900 | [diff] [blame] | 96 | return this.authoritative; |
| 97 | } |
| 98 | |
| 99 | @Override |
| 100 | public boolean isMapDataPresent() { |
| 101 | return this.mapDataPresent; |
Jian Li | 719b3bf | 2016-07-22 00:38:29 +0900 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | @Override |
| 105 | public boolean isProbe() { |
Jian Li | f59c0ad | 2016-08-02 18:11:30 +0900 | [diff] [blame] | 106 | return this.probe; |
Jian Li | 719b3bf | 2016-07-22 00:38:29 +0900 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | @Override |
| 110 | public boolean isSmr() { |
Jian Li | f59c0ad | 2016-08-02 18:11:30 +0900 | [diff] [blame] | 111 | return this.smr; |
Jian Li | 719b3bf | 2016-07-22 00:38:29 +0900 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | @Override |
| 115 | public boolean isPitr() { |
Jian Li | f59c0ad | 2016-08-02 18:11:30 +0900 | [diff] [blame] | 116 | return this.pitr; |
Jian Li | 719b3bf | 2016-07-22 00:38:29 +0900 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | @Override |
| 120 | public boolean isSmrInvoked() { |
Jian Li | f59c0ad | 2016-08-02 18:11:30 +0900 | [diff] [blame] | 121 | return this.smrInvoked; |
Jian Li | 719b3bf | 2016-07-22 00:38:29 +0900 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | @Override |
| 125 | public byte getRecordCount() { |
Jian Li | f59c0ad | 2016-08-02 18:11:30 +0900 | [diff] [blame] | 126 | return this.recordCount; |
Jian Li | 719b3bf | 2016-07-22 00:38:29 +0900 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | @Override |
| 130 | public long getNonce() { |
Jian Li | f59c0ad | 2016-08-02 18:11:30 +0900 | [diff] [blame] | 131 | return this.nonce; |
Jian Li | 719b3bf | 2016-07-22 00:38:29 +0900 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | @Override |
| 135 | public LispAfiAddress getSourceEid() { |
Jian Li | f59c0ad | 2016-08-02 18:11:30 +0900 | [diff] [blame] | 136 | return this.sourceEid; |
Jian Li | 719b3bf | 2016-07-22 00:38:29 +0900 | [diff] [blame] | 137 | } |
| 138 | |
| 139 | @Override |
| 140 | public List<LispAfiAddress> getItrRlocs() { |
Jian Li | f59c0ad | 2016-08-02 18:11:30 +0900 | [diff] [blame] | 141 | return ImmutableList.copyOf(itrRlocs); |
Jian Li | 719b3bf | 2016-07-22 00:38:29 +0900 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | @Override |
Jian Li | 10a0906 | 2016-07-26 23:58:50 +0900 | [diff] [blame] | 145 | public List<LispEidRecord> getEids() { |
Jian Li | f59c0ad | 2016-08-02 18:11:30 +0900 | [diff] [blame] | 146 | return ImmutableList.copyOf(eidRecords); |
Jian Li | 719b3bf | 2016-07-22 00:38:29 +0900 | [diff] [blame] | 147 | } |
| 148 | |
Jian Li | 20850d3 | 2016-08-04 02:15:57 +0900 | [diff] [blame] | 149 | @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 Li | 719b3bf | 2016-07-22 00:38:29 +0900 | [diff] [blame] | 194 | public static final class DefaultRequestBuilder implements RequestBuilder { |
| 195 | |
Jian Li | f59c0ad | 2016-08-02 18:11:30 +0900 | [diff] [blame] | 196 | 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 Li | 719b3bf | 2016-07-22 00:38:29 +0900 | [diff] [blame] | 207 | |
| 208 | @Override |
| 209 | public LispType getType() { |
Jian Li | f59c0ad | 2016-08-02 18:11:30 +0900 | [diff] [blame] | 210 | return LispType.LISP_MAP_REQUEST; |
Jian Li | 719b3bf | 2016-07-22 00:38:29 +0900 | [diff] [blame] | 211 | } |
| 212 | |
| 213 | @Override |
Jian Li | f59c0ad | 2016-08-02 18:11:30 +0900 | [diff] [blame] | 214 | public RequestBuilder withIsAuthoritative(boolean authoritative) { |
| 215 | this.authoritative = authoritative; |
| 216 | return this; |
Jian Li | 719b3bf | 2016-07-22 00:38:29 +0900 | [diff] [blame] | 217 | } |
| 218 | |
| 219 | @Override |
Jian Li | f59c0ad | 2016-08-02 18:11:30 +0900 | [diff] [blame] | 220 | public RequestBuilder withIsProbe(boolean probe) { |
| 221 | this.probe = probe; |
| 222 | return this; |
Jian Li | 719b3bf | 2016-07-22 00:38:29 +0900 | [diff] [blame] | 223 | } |
| 224 | |
| 225 | @Override |
Jian Li | f59c0ad | 2016-08-02 18:11:30 +0900 | [diff] [blame] | 226 | public RequestBuilder withIsMapDataPresent(boolean mapDataPresent) { |
| 227 | this.mapDataPresent = mapDataPresent; |
| 228 | return this; |
| 229 | } |
| 230 | |
Jian Li | f59c0ad | 2016-08-02 18:11:30 +0900 | [diff] [blame] | 231 | @Override |
| 232 | public RequestBuilder withIsSmr(boolean smr) { |
| 233 | this.smr = smr; |
| 234 | return this; |
Jian Li | 719b3bf | 2016-07-22 00:38:29 +0900 | [diff] [blame] | 235 | } |
| 236 | |
| 237 | @Override |
Jian Li | f59c0ad | 2016-08-02 18:11:30 +0900 | [diff] [blame] | 238 | public RequestBuilder withIsPitr(boolean pitr) { |
| 239 | this.pitr = pitr; |
| 240 | return this; |
Jian Li | 719b3bf | 2016-07-22 00:38:29 +0900 | [diff] [blame] | 241 | } |
| 242 | |
| 243 | @Override |
Jian Li | f59c0ad | 2016-08-02 18:11:30 +0900 | [diff] [blame] | 244 | public RequestBuilder withIsSmrInvoked(boolean smrInvoked) { |
| 245 | this.smrInvoked = smrInvoked; |
| 246 | return this; |
Jian Li | 719b3bf | 2016-07-22 00:38:29 +0900 | [diff] [blame] | 247 | } |
| 248 | |
| 249 | @Override |
| 250 | public RequestBuilder withRecordCount(byte recordCount) { |
Jian Li | f59c0ad | 2016-08-02 18:11:30 +0900 | [diff] [blame] | 251 | this.recordCount = recordCount; |
| 252 | return this; |
Jian Li | 719b3bf | 2016-07-22 00:38:29 +0900 | [diff] [blame] | 253 | } |
| 254 | |
| 255 | @Override |
| 256 | public RequestBuilder withNonce(long nonce) { |
Jian Li | f59c0ad | 2016-08-02 18:11:30 +0900 | [diff] [blame] | 257 | this.nonce = nonce; |
| 258 | return this; |
Jian Li | 719b3bf | 2016-07-22 00:38:29 +0900 | [diff] [blame] | 259 | } |
| 260 | |
| 261 | @Override |
Jian Li | f59c0ad | 2016-08-02 18:11:30 +0900 | [diff] [blame] | 262 | public RequestBuilder withSourceEid(LispAfiAddress sourceEid) { |
| 263 | this.sourceEid = sourceEid; |
| 264 | return this; |
| 265 | } |
| 266 | |
| 267 | @Override |
Jian Li | 4767190 | 2016-08-11 01:18:18 +0900 | [diff] [blame^] | 268 | public RequestBuilder withItrRlocs(List<LispAfiAddress> itrRlocs) { |
| 269 | this.itrRlocs = ImmutableList.copyOf(itrRlocs); |
Jian Li | f59c0ad | 2016-08-02 18:11:30 +0900 | [diff] [blame] | 270 | return this; |
Jian Li | 719b3bf | 2016-07-22 00:38:29 +0900 | [diff] [blame] | 271 | } |
| 272 | |
| 273 | @Override |
Jian Li | 4767190 | 2016-08-11 01:18:18 +0900 | [diff] [blame^] | 274 | public RequestBuilder withEidRecords(List<LispEidRecord> records) { |
| 275 | this.eidRecords = ImmutableList.copyOf(records); |
Jian Li | f59c0ad | 2016-08-02 18:11:30 +0900 | [diff] [blame] | 276 | return this; |
| 277 | } |
| 278 | |
| 279 | @Override |
Jian Li | 525fded | 2016-08-04 01:15:33 +0900 | [diff] [blame] | 280 | public LispMapRequest build() { |
Jian Li | f59c0ad | 2016-08-02 18:11:30 +0900 | [diff] [blame] | 281 | return new DefaultLispMapRequest(nonce, recordCount, sourceEid, itrRlocs, |
| 282 | eidRecords, authoritative, mapDataPresent, probe, smr, pitr, smrInvoked); |
Jian Li | 719b3bf | 2016-07-22 00:38:29 +0900 | [diff] [blame] | 283 | } |
| 284 | } |
Jian Li | 26069e2 | 2016-08-10 22:00:52 +0900 | [diff] [blame] | 285 | |
| 286 | /** |
| 287 | * A private LISP message reader for MapRequest message. |
| 288 | */ |
| 289 | private static class RequestReader implements LispMessageReader<LispMapRequest> { |
| 290 | |
Jian Li | 4767190 | 2016-08-11 01:18:18 +0900 | [diff] [blame^] | 291 | 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 Li | 26069e2 | 2016-08-10 22:00:52 +0900 | [diff] [blame] | 298 | @Override |
| 299 | public LispMapRequest readFrom(ByteBuf byteBuf) throws LispParseError { |
Jian Li | 4767190 | 2016-08-11 01:18:18 +0900 | [diff] [blame^] | 300 | |
| 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 Li | 26069e2 | 2016-08-10 22:00:52 +0900 | [diff] [blame] | 354 | } |
| 355 | } |
Jian Li | 451175e | 2016-07-19 23:22:20 +0900 | [diff] [blame] | 356 | } |