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 | f59c0ad | 2016-08-02 18:11:30 +0900 | [diff] [blame] | 23 | import org.onlab.util.ImmutableByteSequence; |
Jian Li | 26069e2 | 2016-08-10 22:00:52 +0900 | [diff] [blame] | 24 | import org.onosproject.lisp.msg.exceptions.LispParseError; |
Jian Li | a7b394d | 2016-08-21 23:11:46 +0900 | [diff] [blame] | 25 | import org.onosproject.lisp.msg.exceptions.LispReaderException; |
Jian Li | 451175e | 2016-07-19 23:22:20 +0900 | [diff] [blame] | 26 | |
Jian Li | 719b3bf | 2016-07-22 00:38:29 +0900 | [diff] [blame] | 27 | import java.util.List; |
| 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 register message class. |
| 33 | */ |
Jian Li | f59c0ad | 2016-08-02 18:11:30 +0900 | [diff] [blame] | 34 | public final class DefaultLispMapRegister implements LispMapRegister { |
| 35 | |
| 36 | private final long nonce; |
| 37 | private final short keyId; |
| 38 | private final byte[] authenticationData; |
| 39 | private final byte recordCount; |
| 40 | private final List<LispMapRecord> mapRecords; |
| 41 | private final boolean proxyMapReply; |
| 42 | private final boolean wantMapNotify; |
| 43 | |
| 44 | /** |
| 45 | * A private constructor that protects object instantiation from external. |
| 46 | * |
| 47 | * @param nonce nonce |
| 48 | * @param keyId key identifier |
| 49 | * @param authenticationData authentication data |
| 50 | * @param recordCount record count number |
| 51 | * @param mapRecords a collection of map records |
| 52 | * @param proxyMapReply proxy map reply flag |
| 53 | * @param wantMapNotify want map notify flag |
| 54 | */ |
| 55 | private DefaultLispMapRegister(long nonce, short keyId, |
| 56 | byte[] authenticationData, byte recordCount, |
| 57 | List<LispMapRecord> mapRecords, |
| 58 | boolean proxyMapReply, boolean wantMapNotify) { |
| 59 | this.nonce = nonce; |
| 60 | this.keyId = keyId; |
| 61 | this.authenticationData = authenticationData; |
| 62 | this.recordCount = recordCount; |
| 63 | this.mapRecords = mapRecords; |
| 64 | this.proxyMapReply = proxyMapReply; |
| 65 | this.wantMapNotify = wantMapNotify; |
| 66 | } |
| 67 | |
Jian Li | 451175e | 2016-07-19 23:22:20 +0900 | [diff] [blame] | 68 | @Override |
| 69 | public LispType getType() { |
Jian Li | f59c0ad | 2016-08-02 18:11:30 +0900 | [diff] [blame] | 70 | return LispType.LISP_MAP_REGISTER; |
Jian Li | 451175e | 2016-07-19 23:22:20 +0900 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | @Override |
| 74 | public void writeTo(ByteBuf byteBuf) { |
Jian Li | f59c0ad | 2016-08-02 18:11:30 +0900 | [diff] [blame] | 75 | // TODO: serialize LispMapRegister message |
Jian Li | 451175e | 2016-07-19 23:22:20 +0900 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | @Override |
| 79 | public Builder createBuilder() { |
Jian Li | 525fded | 2016-08-04 01:15:33 +0900 | [diff] [blame] | 80 | return new DefaultRegisterBuilder(); |
Jian Li | 451175e | 2016-07-19 23:22:20 +0900 | [diff] [blame] | 81 | } |
Jian Li | 719b3bf | 2016-07-22 00:38:29 +0900 | [diff] [blame] | 82 | |
| 83 | @Override |
| 84 | public boolean isProxyMapReply() { |
Jian Li | f59c0ad | 2016-08-02 18:11:30 +0900 | [diff] [blame] | 85 | return proxyMapReply; |
Jian Li | 719b3bf | 2016-07-22 00:38:29 +0900 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | @Override |
| 89 | public boolean isWantMapNotify() { |
Jian Li | f59c0ad | 2016-08-02 18:11:30 +0900 | [diff] [blame] | 90 | return wantMapNotify; |
Jian Li | 719b3bf | 2016-07-22 00:38:29 +0900 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | @Override |
| 94 | public byte getRecordCount() { |
Jian Li | f59c0ad | 2016-08-02 18:11:30 +0900 | [diff] [blame] | 95 | return recordCount; |
Jian Li | 719b3bf | 2016-07-22 00:38:29 +0900 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | @Override |
| 99 | public long getNonce() { |
Jian Li | f59c0ad | 2016-08-02 18:11:30 +0900 | [diff] [blame] | 100 | return nonce; |
Jian Li | 719b3bf | 2016-07-22 00:38:29 +0900 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | @Override |
| 104 | public short getKeyId() { |
Jian Li | f59c0ad | 2016-08-02 18:11:30 +0900 | [diff] [blame] | 105 | return keyId; |
Jian Li | 719b3bf | 2016-07-22 00:38:29 +0900 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | @Override |
| 109 | public byte[] getAuthenticationData() { |
Jian Li | f59c0ad | 2016-08-02 18:11:30 +0900 | [diff] [blame] | 110 | return ImmutableByteSequence.copyFrom(this.authenticationData).asArray(); |
Jian Li | 719b3bf | 2016-07-22 00:38:29 +0900 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | @Override |
Jian Li | a7b394d | 2016-08-21 23:11:46 +0900 | [diff] [blame] | 114 | public List<LispMapRecord> getMapRecords() { |
Jian Li | f59c0ad | 2016-08-02 18:11:30 +0900 | [diff] [blame] | 115 | return ImmutableList.copyOf(mapRecords); |
Jian Li | 719b3bf | 2016-07-22 00:38:29 +0900 | [diff] [blame] | 116 | } |
| 117 | |
Jian Li | 20850d3 | 2016-08-04 02:15:57 +0900 | [diff] [blame] | 118 | @Override |
| 119 | public String toString() { |
| 120 | return toStringHelper(this) |
| 121 | .add("type", getType()) |
| 122 | .add("nonce", nonce) |
| 123 | .add("recordCount", recordCount) |
| 124 | .add("keyId", keyId) |
| 125 | .add("mapRecords", mapRecords) |
| 126 | .add("proxyMapReply", proxyMapReply) |
| 127 | .add("wantMapNotify", wantMapNotify).toString(); |
| 128 | } |
| 129 | |
| 130 | @Override |
| 131 | public boolean equals(Object o) { |
| 132 | if (this == o) { |
| 133 | return true; |
| 134 | } |
| 135 | if (o == null || getClass() != o.getClass()) { |
| 136 | return false; |
| 137 | } |
| 138 | |
| 139 | DefaultLispMapRegister that = (DefaultLispMapRegister) o; |
| 140 | return Objects.equal(nonce, that.nonce) && |
| 141 | Objects.equal(recordCount, that.recordCount) && |
| 142 | Objects.equal(keyId, that.keyId) && |
| 143 | Objects.equal(authenticationData, that.authenticationData) && |
| 144 | Objects.equal(proxyMapReply, that.proxyMapReply) && |
| 145 | Objects.equal(wantMapNotify, that.wantMapNotify); |
| 146 | } |
| 147 | |
| 148 | @Override |
| 149 | public int hashCode() { |
| 150 | return Objects.hashCode(nonce, recordCount, keyId, authenticationData, |
| 151 | proxyMapReply, wantMapNotify); |
| 152 | } |
| 153 | |
Jian Li | 719b3bf | 2016-07-22 00:38:29 +0900 | [diff] [blame] | 154 | public static final class DefaultRegisterBuilder implements RegisterBuilder { |
| 155 | |
Jian Li | f59c0ad | 2016-08-02 18:11:30 +0900 | [diff] [blame] | 156 | private long nonce; |
| 157 | private short keyId; |
| 158 | private byte[] authenticationData; |
| 159 | private byte recordCount; |
Jian Li | 4767190 | 2016-08-11 01:18:18 +0900 | [diff] [blame] | 160 | private List<LispMapRecord> mapRecords; |
Jian Li | f59c0ad | 2016-08-02 18:11:30 +0900 | [diff] [blame] | 161 | private boolean proxyMapReply; |
| 162 | private boolean wantMapNotify; |
Jian Li | 719b3bf | 2016-07-22 00:38:29 +0900 | [diff] [blame] | 163 | |
| 164 | @Override |
| 165 | public LispType getType() { |
Jian Li | f59c0ad | 2016-08-02 18:11:30 +0900 | [diff] [blame] | 166 | return LispType.LISP_MAP_REGISTER; |
Jian Li | 719b3bf | 2016-07-22 00:38:29 +0900 | [diff] [blame] | 167 | } |
| 168 | |
| 169 | @Override |
Jian Li | f59c0ad | 2016-08-02 18:11:30 +0900 | [diff] [blame] | 170 | public RegisterBuilder withIsProxyMapReply(boolean proxyMapReply) { |
| 171 | this.proxyMapReply = proxyMapReply; |
| 172 | return this; |
Jian Li | 719b3bf | 2016-07-22 00:38:29 +0900 | [diff] [blame] | 173 | } |
| 174 | |
| 175 | @Override |
Jian Li | f59c0ad | 2016-08-02 18:11:30 +0900 | [diff] [blame] | 176 | public RegisterBuilder withIsWantMapNotify(boolean wantMapNotify) { |
| 177 | this.wantMapNotify = wantMapNotify; |
| 178 | return this; |
Jian Li | 719b3bf | 2016-07-22 00:38:29 +0900 | [diff] [blame] | 179 | } |
| 180 | |
| 181 | @Override |
| 182 | public RegisterBuilder withRecordCount(byte recordCount) { |
Jian Li | f59c0ad | 2016-08-02 18:11:30 +0900 | [diff] [blame] | 183 | this.recordCount = recordCount; |
| 184 | return this; |
Jian Li | 719b3bf | 2016-07-22 00:38:29 +0900 | [diff] [blame] | 185 | } |
| 186 | |
| 187 | @Override |
| 188 | public RegisterBuilder withNonce(long nonce) { |
Jian Li | f59c0ad | 2016-08-02 18:11:30 +0900 | [diff] [blame] | 189 | this.nonce = nonce; |
| 190 | return this; |
Jian Li | 719b3bf | 2016-07-22 00:38:29 +0900 | [diff] [blame] | 191 | } |
| 192 | |
| 193 | @Override |
| 194 | public RegisterBuilder withKeyId(short keyId) { |
Jian Li | f59c0ad | 2016-08-02 18:11:30 +0900 | [diff] [blame] | 195 | this.keyId = keyId; |
| 196 | return this; |
Jian Li | 719b3bf | 2016-07-22 00:38:29 +0900 | [diff] [blame] | 197 | } |
| 198 | |
| 199 | @Override |
| 200 | public RegisterBuilder withAuthenticationData(byte[] authenticationData) { |
Jian Li | f59c0ad | 2016-08-02 18:11:30 +0900 | [diff] [blame] | 201 | this.authenticationData = authenticationData; |
| 202 | return this; |
Jian Li | 719b3bf | 2016-07-22 00:38:29 +0900 | [diff] [blame] | 203 | } |
| 204 | |
| 205 | @Override |
Jian Li | 4767190 | 2016-08-11 01:18:18 +0900 | [diff] [blame] | 206 | public RegisterBuilder withMapRecords(List<LispMapRecord> mapRecords) { |
| 207 | this.mapRecords = ImmutableList.copyOf(mapRecords); |
Jian Li | f59c0ad | 2016-08-02 18:11:30 +0900 | [diff] [blame] | 208 | return this; |
| 209 | } |
| 210 | |
| 211 | @Override |
Jian Li | 525fded | 2016-08-04 01:15:33 +0900 | [diff] [blame] | 212 | public LispMapRegister build() { |
Jian Li | f59c0ad | 2016-08-02 18:11:30 +0900 | [diff] [blame] | 213 | return new DefaultLispMapRegister(nonce, keyId, authenticationData, |
| 214 | recordCount, mapRecords, proxyMapReply, wantMapNotify); |
Jian Li | 719b3bf | 2016-07-22 00:38:29 +0900 | [diff] [blame] | 215 | } |
| 216 | } |
Jian Li | 26069e2 | 2016-08-10 22:00:52 +0900 | [diff] [blame] | 217 | |
| 218 | /** |
| 219 | * A private LISP message reader for MapRegister message. |
| 220 | */ |
| 221 | private static class RegisterReader implements LispMessageReader<LispMapRegister> { |
| 222 | |
Jian Li | 4767190 | 2016-08-11 01:18:18 +0900 | [diff] [blame] | 223 | private static final int PROXY_MAP_REPLY_INDEX = 3; |
| 224 | private static final int WANT_MAP_NOTIFY_INDEX = 0; |
| 225 | private static final int RESERVED_SKIP_LENGTH = 1; |
| 226 | |
Jian Li | 26069e2 | 2016-08-10 22:00:52 +0900 | [diff] [blame] | 227 | @Override |
Jian Li | a7b394d | 2016-08-21 23:11:46 +0900 | [diff] [blame] | 228 | public LispMapRegister readFrom(ByteBuf byteBuf) throws LispParseError, LispReaderException { |
Jian Li | 4767190 | 2016-08-11 01:18:18 +0900 | [diff] [blame] | 229 | |
| 230 | if (byteBuf.readerIndex() != 0) { |
| 231 | return null; |
| 232 | } |
| 233 | |
| 234 | // proxyMapReply -> 1 bit |
| 235 | boolean proxyMapReplyFlag = ByteOperator.getBit(byteBuf.readByte(), PROXY_MAP_REPLY_INDEX); |
| 236 | |
| 237 | // let's skip the reserved field |
| 238 | byteBuf.skipBytes(RESERVED_SKIP_LENGTH); |
| 239 | |
| 240 | byte reservedWithFlag = byteBuf.readByte(); |
| 241 | |
| 242 | // wantMapReply -> 1 bit |
| 243 | boolean wantMapNotifyFlag = ByteOperator.getBit(reservedWithFlag, WANT_MAP_NOTIFY_INDEX); |
| 244 | |
| 245 | // record count -> 8 bits |
| 246 | byte recordCount = (byte) byteBuf.readUnsignedByte(); |
| 247 | |
| 248 | // nonce -> 64 bits |
| 249 | long nonce = byteBuf.readLong(); |
| 250 | |
| 251 | // keyId -> 16 bits |
| 252 | short keyId = byteBuf.readShort(); |
| 253 | |
| 254 | // authenticationDataLength -> 16 bits |
| 255 | short authLength = byteBuf.readShort(); |
| 256 | |
| 257 | // authenticationData -> depends on the authenticationDataLength |
| 258 | byte[] authData = new byte[authLength]; |
| 259 | byteBuf.readBytes(authData); |
| 260 | |
| 261 | List<LispMapRecord> mapRecords = Lists.newArrayList(); |
| 262 | for (int i = 0; i < recordCount; i++) { |
| 263 | mapRecords.add(new DefaultLispMapRecord.MapRecordReader().readFrom(byteBuf)); |
| 264 | } |
| 265 | |
| 266 | return new DefaultRegisterBuilder() |
| 267 | .withIsProxyMapReply(proxyMapReplyFlag) |
| 268 | .withIsWantMapNotify(wantMapNotifyFlag) |
| 269 | .withRecordCount(recordCount) |
| 270 | .withNonce(nonce) |
| 271 | .withKeyId(keyId) |
| 272 | .withAuthenticationData(authData) |
| 273 | .withMapRecords(mapRecords) |
| 274 | .build(); |
Jian Li | 26069e2 | 2016-08-10 22:00:52 +0900 | [diff] [blame] | 275 | } |
| 276 | } |
Jian Li | 451175e | 2016-07-19 23:22:20 +0900 | [diff] [blame] | 277 | } |