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 | a7b394d | 2016-08-21 23:11:46 +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 | edc5db1 | 2016-08-23 17:30:19 +0900 | [diff] [blame] | 25 | import org.onosproject.lisp.msg.exceptions.LispWriterException; |
Jian Li | a7b394d | 2016-08-21 23:11:46 +0900 | [diff] [blame] | 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; |
Jian Li | edc5db1 | 2016-08-23 17:30:19 +0900 | [diff] [blame] | 30 | import static org.onosproject.lisp.msg.protocols.DefaultLispMapRecord.MapRecordWriter; |
Jian Li | 20850d3 | 2016-08-04 02:15:57 +0900 | [diff] [blame] | 31 | |
Jian Li | 451175e | 2016-07-19 23:22:20 +0900 | [diff] [blame] | 32 | /** |
| 33 | * Default LISP map reply message class. |
| 34 | */ |
Jian Li | f59c0ad | 2016-08-02 18:11:30 +0900 | [diff] [blame] | 35 | public final class DefaultLispMapReply implements LispMapReply { |
| 36 | |
| 37 | private final long nonce; |
| 38 | private final byte recordCount; |
| 39 | private final boolean probe; |
| 40 | private final boolean etr; |
| 41 | private final boolean security; |
Jian Li | a7b394d | 2016-08-21 23:11:46 +0900 | [diff] [blame] | 42 | private final List<LispMapRecord> mapRecords; |
Jian Li | f59c0ad | 2016-08-02 18:11:30 +0900 | [diff] [blame] | 43 | |
| 44 | /** |
| 45 | * A private constructor that protects object instantiation from external. |
| 46 | * |
| 47 | * @param nonce nonce |
| 48 | * @param recordCount record count number |
| 49 | * @param probe probe flag |
| 50 | * @param etr etr flag |
| 51 | * @param security security flag |
| 52 | */ |
| 53 | private DefaultLispMapReply(long nonce, byte recordCount, boolean probe, |
Jian Li | a7b394d | 2016-08-21 23:11:46 +0900 | [diff] [blame] | 54 | boolean etr, boolean security, List<LispMapRecord> mapRecords) { |
Jian Li | f59c0ad | 2016-08-02 18:11:30 +0900 | [diff] [blame] | 55 | this.nonce = nonce; |
| 56 | this.recordCount = recordCount; |
| 57 | this.probe = probe; |
| 58 | this.etr = etr; |
| 59 | this.security = security; |
Jian Li | a7b394d | 2016-08-21 23:11:46 +0900 | [diff] [blame] | 60 | this.mapRecords = mapRecords; |
Jian Li | f59c0ad | 2016-08-02 18:11:30 +0900 | [diff] [blame] | 61 | } |
| 62 | |
Jian Li | 451175e | 2016-07-19 23:22:20 +0900 | [diff] [blame] | 63 | @Override |
| 64 | public LispType getType() { |
Jian Li | f59c0ad | 2016-08-02 18:11:30 +0900 | [diff] [blame] | 65 | return LispType.LISP_MAP_REPLY; |
Jian Li | 451175e | 2016-07-19 23:22:20 +0900 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | @Override |
| 69 | public void writeTo(ByteBuf byteBuf) { |
Jian Li | f59c0ad | 2016-08-02 18:11:30 +0900 | [diff] [blame] | 70 | // TODO: serialize LispMapReply message |
Jian Li | 451175e | 2016-07-19 23:22:20 +0900 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | @Override |
| 74 | public Builder createBuilder() { |
Jian Li | 525fded | 2016-08-04 01:15:33 +0900 | [diff] [blame] | 75 | return new DefaultReplyBuilder(); |
Jian Li | 451175e | 2016-07-19 23:22:20 +0900 | [diff] [blame] | 76 | } |
Jian Li | 719b3bf | 2016-07-22 00:38:29 +0900 | [diff] [blame] | 77 | |
| 78 | @Override |
| 79 | public boolean isProbe() { |
Jian Li | edc5db1 | 2016-08-23 17:30:19 +0900 | [diff] [blame] | 80 | return probe; |
Jian Li | 719b3bf | 2016-07-22 00:38:29 +0900 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | @Override |
| 84 | public boolean isEtr() { |
Jian Li | edc5db1 | 2016-08-23 17:30:19 +0900 | [diff] [blame] | 85 | return etr; |
Jian Li | 719b3bf | 2016-07-22 00:38:29 +0900 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | @Override |
| 89 | public boolean isSecurity() { |
Jian Li | edc5db1 | 2016-08-23 17:30:19 +0900 | [diff] [blame] | 90 | return security; |
Jian Li | 719b3bf | 2016-07-22 00:38:29 +0900 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | @Override |
| 94 | public byte getRecordCount() { |
Jian Li | edc5db1 | 2016-08-23 17:30:19 +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 | edc5db1 | 2016-08-23 17:30:19 +0900 | [diff] [blame] | 100 | return nonce; |
Jian Li | 719b3bf | 2016-07-22 00:38:29 +0900 | [diff] [blame] | 101 | } |
| 102 | |
Jian Li | 20850d3 | 2016-08-04 02:15:57 +0900 | [diff] [blame] | 103 | @Override |
Jian Li | a7b394d | 2016-08-21 23:11:46 +0900 | [diff] [blame] | 104 | public List<LispMapRecord> getMapRecords() { |
| 105 | return ImmutableList.copyOf(mapRecords); |
| 106 | } |
| 107 | |
| 108 | @Override |
Jian Li | 20850d3 | 2016-08-04 02:15:57 +0900 | [diff] [blame] | 109 | public String toString() { |
| 110 | return toStringHelper(this) |
| 111 | .add("type", getType()) |
| 112 | .add("nonce", nonce) |
| 113 | .add("recordCount", recordCount) |
| 114 | .add("probe", probe) |
| 115 | .add("etr", etr) |
Jian Li | a7b394d | 2016-08-21 23:11:46 +0900 | [diff] [blame] | 116 | .add("security", security) |
| 117 | .add("map records", mapRecords).toString(); |
Jian Li | 20850d3 | 2016-08-04 02:15:57 +0900 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | @Override |
| 121 | public boolean equals(Object o) { |
| 122 | if (this == o) { |
| 123 | return true; |
| 124 | } |
| 125 | if (o == null || getClass() != o.getClass()) { |
| 126 | return false; |
| 127 | } |
| 128 | DefaultLispMapReply that = (DefaultLispMapReply) o; |
| 129 | return Objects.equal(nonce, that.nonce) && |
| 130 | Objects.equal(recordCount, that.recordCount) && |
| 131 | Objects.equal(probe, that.probe) && |
| 132 | Objects.equal(etr, that.etr) && |
Jian Li | a7b394d | 2016-08-21 23:11:46 +0900 | [diff] [blame] | 133 | Objects.equal(security, that.security) && |
| 134 | Objects.equal(mapRecords, that.mapRecords); |
Jian Li | 20850d3 | 2016-08-04 02:15:57 +0900 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | @Override |
| 138 | public int hashCode() { |
Jian Li | a7b394d | 2016-08-21 23:11:46 +0900 | [diff] [blame] | 139 | return Objects.hashCode(nonce, recordCount, probe, etr, security, mapRecords); |
Jian Li | 20850d3 | 2016-08-04 02:15:57 +0900 | [diff] [blame] | 140 | } |
| 141 | |
Jian Li | 719b3bf | 2016-07-22 00:38:29 +0900 | [diff] [blame] | 142 | public static final class DefaultReplyBuilder implements ReplyBuilder { |
| 143 | |
Jian Li | f59c0ad | 2016-08-02 18:11:30 +0900 | [diff] [blame] | 144 | private long nonce; |
| 145 | private byte recordCount; |
| 146 | private boolean probe; |
| 147 | private boolean etr; |
| 148 | private boolean security; |
Jian Li | a7b394d | 2016-08-21 23:11:46 +0900 | [diff] [blame] | 149 | private List<LispMapRecord> mapRecords; |
Jian Li | 719b3bf | 2016-07-22 00:38:29 +0900 | [diff] [blame] | 150 | |
| 151 | @Override |
| 152 | public LispType getType() { |
Jian Li | f59c0ad | 2016-08-02 18:11:30 +0900 | [diff] [blame] | 153 | return LispType.LISP_MAP_REPLY; |
Jian Li | 719b3bf | 2016-07-22 00:38:29 +0900 | [diff] [blame] | 154 | } |
| 155 | |
| 156 | @Override |
Jian Li | f59c0ad | 2016-08-02 18:11:30 +0900 | [diff] [blame] | 157 | public ReplyBuilder withIsProbe(boolean probe) { |
| 158 | this.probe = probe; |
| 159 | return this; |
Jian Li | 719b3bf | 2016-07-22 00:38:29 +0900 | [diff] [blame] | 160 | } |
| 161 | |
| 162 | @Override |
Jian Li | f59c0ad | 2016-08-02 18:11:30 +0900 | [diff] [blame] | 163 | public ReplyBuilder withIsEtr(boolean etr) { |
| 164 | this.etr = etr; |
| 165 | return this; |
Jian Li | 719b3bf | 2016-07-22 00:38:29 +0900 | [diff] [blame] | 166 | } |
| 167 | |
| 168 | @Override |
Jian Li | f59c0ad | 2016-08-02 18:11:30 +0900 | [diff] [blame] | 169 | public ReplyBuilder withIsSecurity(boolean security) { |
| 170 | this.security = security; |
| 171 | return this; |
Jian Li | 719b3bf | 2016-07-22 00:38:29 +0900 | [diff] [blame] | 172 | } |
| 173 | |
| 174 | @Override |
| 175 | public ReplyBuilder withRecordCount(byte recordCount) { |
Jian Li | f59c0ad | 2016-08-02 18:11:30 +0900 | [diff] [blame] | 176 | this.recordCount = recordCount; |
| 177 | return this; |
Jian Li | 719b3bf | 2016-07-22 00:38:29 +0900 | [diff] [blame] | 178 | } |
| 179 | |
| 180 | @Override |
| 181 | public ReplyBuilder withNonce(long nonce) { |
Jian Li | f59c0ad | 2016-08-02 18:11:30 +0900 | [diff] [blame] | 182 | this.nonce = nonce; |
| 183 | return this; |
| 184 | } |
| 185 | |
| 186 | @Override |
Jian Li | a7b394d | 2016-08-21 23:11:46 +0900 | [diff] [blame] | 187 | public ReplyBuilder withMapRecords(List<LispMapRecord> mapRecords) { |
Jian Li | e4ba2a4 | 2016-08-29 20:24:15 +0900 | [diff] [blame] | 188 | |
| 189 | if (this.mapRecords != null) { |
| 190 | this.mapRecords = ImmutableList.copyOf(mapRecords); |
| 191 | } else { |
| 192 | this.mapRecords = Lists.newArrayList(); |
| 193 | } |
Jian Li | a7b394d | 2016-08-21 23:11:46 +0900 | [diff] [blame] | 194 | return this; |
| 195 | } |
| 196 | |
| 197 | @Override |
Jian Li | 525fded | 2016-08-04 01:15:33 +0900 | [diff] [blame] | 198 | public LispMapReply build() { |
Jian Li | e4ba2a4 | 2016-08-29 20:24:15 +0900 | [diff] [blame] | 199 | |
| 200 | if (mapRecords == null) { |
| 201 | mapRecords = Lists.newArrayList(); |
| 202 | } |
| 203 | |
Jian Li | a7b394d | 2016-08-21 23:11:46 +0900 | [diff] [blame] | 204 | return new DefaultLispMapReply(nonce, recordCount, probe, etr, security, mapRecords); |
Jian Li | 719b3bf | 2016-07-22 00:38:29 +0900 | [diff] [blame] | 205 | } |
| 206 | } |
Jian Li | 26069e2 | 2016-08-10 22:00:52 +0900 | [diff] [blame] | 207 | |
| 208 | /** |
Jian Li | edc5db1 | 2016-08-23 17:30:19 +0900 | [diff] [blame] | 209 | * A LISP message reader for MapReply message. |
Jian Li | 26069e2 | 2016-08-10 22:00:52 +0900 | [diff] [blame] | 210 | */ |
Jian Li | edc5db1 | 2016-08-23 17:30:19 +0900 | [diff] [blame] | 211 | public static final class ReplyReader implements LispMessageReader<LispMapReply> { |
Jian Li | 26069e2 | 2016-08-10 22:00:52 +0900 | [diff] [blame] | 212 | |
Jian Li | 4767190 | 2016-08-11 01:18:18 +0900 | [diff] [blame] | 213 | private static final int PROBE_INDEX = 3; |
| 214 | private static final int ETR_INDEX = 2; |
| 215 | private static final int SECURITY_INDEX = 1; |
| 216 | private static final int RESERVED_SKIP_LENGTH = 2; |
| 217 | |
Jian Li | 26069e2 | 2016-08-10 22:00:52 +0900 | [diff] [blame] | 218 | @Override |
Jian Li | a7b394d | 2016-08-21 23:11:46 +0900 | [diff] [blame] | 219 | public LispMapReply readFrom(ByteBuf byteBuf) throws LispParseError, LispReaderException { |
Jian Li | 4767190 | 2016-08-11 01:18:18 +0900 | [diff] [blame] | 220 | |
| 221 | if (byteBuf.readerIndex() != 0) { |
| 222 | return null; |
| 223 | } |
| 224 | |
| 225 | byte typeWithFlags = byteBuf.readByte(); |
| 226 | |
| 227 | // probe -> 1 bit |
| 228 | boolean probe = ByteOperator.getBit(typeWithFlags, PROBE_INDEX); |
| 229 | |
| 230 | // etr -> 1bit |
| 231 | boolean etr = ByteOperator.getBit(typeWithFlags, ETR_INDEX); |
| 232 | |
| 233 | // security -> 1 bit |
| 234 | boolean security = ByteOperator.getBit(typeWithFlags, SECURITY_INDEX); |
| 235 | |
| 236 | // skip two bytes as they represent reserved fields |
| 237 | byteBuf.skipBytes(RESERVED_SKIP_LENGTH); |
| 238 | |
| 239 | // record count -> 8 bits |
| 240 | byte recordCount = (byte) byteBuf.readUnsignedByte(); |
| 241 | |
| 242 | // nonce -> 64 bits |
| 243 | long nonce = byteBuf.readLong(); |
| 244 | |
Jian Li | a7b394d | 2016-08-21 23:11:46 +0900 | [diff] [blame] | 245 | List<LispMapRecord> mapRecords = Lists.newArrayList(); |
| 246 | for (int i = 0; i < recordCount; i++) { |
| 247 | mapRecords.add(new DefaultLispMapRecord.MapRecordReader().readFrom(byteBuf)); |
| 248 | } |
Jian Li | 4767190 | 2016-08-11 01:18:18 +0900 | [diff] [blame] | 249 | |
| 250 | return new DefaultReplyBuilder() |
| 251 | .withIsProbe(probe) |
| 252 | .withIsEtr(etr) |
| 253 | .withIsSecurity(security) |
| 254 | .withRecordCount(recordCount) |
| 255 | .withNonce(nonce) |
| 256 | .build(); |
Jian Li | 26069e2 | 2016-08-10 22:00:52 +0900 | [diff] [blame] | 257 | } |
| 258 | } |
Jian Li | edc5db1 | 2016-08-23 17:30:19 +0900 | [diff] [blame] | 259 | |
| 260 | /** |
| 261 | * A LISP message writer for MapReply message. |
| 262 | */ |
| 263 | public static final class ReplyWriter implements LispMessageWriter<LispMapReply> { |
| 264 | |
| 265 | private static final int REPLY_MSG_TYPE = 2; |
| 266 | private static final int REPLY_SHIFT_BIT = 4; |
| 267 | |
| 268 | private static final int PROBE_FLAG_SHIFT_BIT = 3; |
| 269 | private static final int ETR_FLAG_SHIFT_BIT = 2; |
| 270 | private static final int SECURITY_FLAG_SHIFT_BIT = 1; |
| 271 | |
| 272 | private static final int ENABLE_BIT = 1; |
| 273 | private static final int DISABLE_BIT = 0; |
| 274 | |
| 275 | private static final int UNUSED_ZERO = 0; |
| 276 | |
| 277 | @Override |
| 278 | public void writeTo(ByteBuf byteBuf, LispMapReply message) throws LispWriterException { |
| 279 | |
| 280 | // specify LISP message type |
| 281 | byte msgType = (byte) (REPLY_MSG_TYPE << REPLY_SHIFT_BIT); |
| 282 | |
| 283 | // probe flag |
| 284 | byte probe = DISABLE_BIT; |
| 285 | if (message.isProbe()) { |
| 286 | probe = (byte) (ENABLE_BIT << PROBE_FLAG_SHIFT_BIT); |
| 287 | } |
| 288 | |
| 289 | // etr flag |
| 290 | byte etr = DISABLE_BIT; |
| 291 | if (message.isEtr()) { |
| 292 | etr = (byte) (ENABLE_BIT << ETR_FLAG_SHIFT_BIT); |
| 293 | } |
| 294 | |
| 295 | // security flag |
| 296 | byte security = DISABLE_BIT; |
| 297 | if (message.isSecurity()) { |
| 298 | security = (byte) (ENABLE_BIT << SECURITY_FLAG_SHIFT_BIT); |
| 299 | } |
| 300 | |
| 301 | byteBuf.writeByte((byte) (msgType + probe + etr + security)); |
| 302 | |
| 303 | // reserved field |
| 304 | byteBuf.writeShort((short) UNUSED_ZERO); |
| 305 | |
| 306 | // record count |
| 307 | byteBuf.writeByte(message.getRecordCount()); |
| 308 | |
| 309 | // nonce |
| 310 | byteBuf.writeLong(message.getNonce()); |
| 311 | |
| 312 | // serialize map records |
| 313 | MapRecordWriter writer = new MapRecordWriter(); |
| 314 | List<LispMapRecord> records = message.getMapRecords(); |
| 315 | |
| 316 | for (int i = 0; i < records.size(); i++) { |
| 317 | writer.writeTo(byteBuf, records.get(i)); |
| 318 | } |
| 319 | } |
| 320 | } |
Jian Li | 451175e | 2016-07-19 23:22:20 +0900 | [diff] [blame] | 321 | } |