Jian Li | 10a0906 | 2016-07-26 23:58:50 +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 | 26069e2 | 2016-08-10 22:00:52 +0900 | [diff] [blame] | 18 | import io.netty.buffer.ByteBuf; |
Jian Li | 1118c12 | 2016-11-01 21:58:15 +0900 | [diff] [blame] | 19 | import com.google.common.base.Objects; |
Jian Li | 26069e2 | 2016-08-10 22:00:52 +0900 | [diff] [blame] | 20 | import org.onosproject.lisp.msg.exceptions.LispParseError; |
Jian Li | a7b394d | 2016-08-21 23:11:46 +0900 | [diff] [blame] | 21 | import org.onosproject.lisp.msg.exceptions.LispReaderException; |
Jian Li | edc5db1 | 2016-08-23 17:30:19 +0900 | [diff] [blame] | 22 | import org.onosproject.lisp.msg.exceptions.LispWriterException; |
Jian Li | 10a0906 | 2016-07-26 23:58:50 +0900 | [diff] [blame] | 23 | import org.onosproject.lisp.msg.types.LispAfiAddress; |
| 24 | |
Jian Li | 1118c12 | 2016-11-01 21:58:15 +0900 | [diff] [blame] | 25 | import static com.google.common.base.MoreObjects.toStringHelper; |
Jian Li | d4e6370 | 2016-08-30 18:29:20 +0900 | [diff] [blame] | 26 | import static com.google.common.base.Preconditions.checkNotNull; |
Jian Li | edc5db1 | 2016-08-23 17:30:19 +0900 | [diff] [blame] | 27 | import static org.onosproject.lisp.msg.types.LispAfiAddress.AfiAddressWriter; |
| 28 | |
Jian Li | 10a0906 | 2016-07-26 23:58:50 +0900 | [diff] [blame] | 29 | /** |
| 30 | * LISP EID record section which is part of LISP map request message. |
| 31 | */ |
Jian Li | 631e62a | 2016-08-03 22:42:00 +0900 | [diff] [blame] | 32 | public final class LispEidRecord { |
Jian Li | 10a0906 | 2016-07-26 23:58:50 +0900 | [diff] [blame] | 33 | |
| 34 | private final byte maskLength; |
| 35 | private final LispAfiAddress prefix; |
| 36 | |
| 37 | /** |
| 38 | * Initializes LispEidRecord with mask length and EID prefix. |
| 39 | * |
| 40 | * @param maskLength mask length |
| 41 | * @param prefix EID prefix |
| 42 | */ |
| 43 | public LispEidRecord(byte maskLength, LispAfiAddress prefix) { |
| 44 | this.maskLength = maskLength; |
Jian Li | d4e6370 | 2016-08-30 18:29:20 +0900 | [diff] [blame] | 45 | |
| 46 | checkNotNull(prefix, "Must specify an address prefix"); |
| 47 | |
Jian Li | 10a0906 | 2016-07-26 23:58:50 +0900 | [diff] [blame] | 48 | this.prefix = prefix; |
| 49 | } |
| 50 | |
| 51 | /** |
| 52 | * Obtains mask length of the EID Record. |
| 53 | * |
| 54 | * @return mask length of the EID Record |
| 55 | */ |
| 56 | public byte getMaskLength() { |
| 57 | return maskLength; |
| 58 | } |
| 59 | |
| 60 | /** |
| 61 | * Obtains EID prefix. |
| 62 | * |
| 63 | * @return EID prefix |
| 64 | */ |
| 65 | public LispAfiAddress getPrefix() { |
| 66 | return prefix; |
| 67 | } |
Jian Li | 26069e2 | 2016-08-10 22:00:52 +0900 | [diff] [blame] | 68 | |
Jian Li | 1118c12 | 2016-11-01 21:58:15 +0900 | [diff] [blame] | 69 | @Override |
| 70 | public String toString() { |
| 71 | return toStringHelper(this) |
| 72 | .add("maskLength", maskLength) |
| 73 | .add("prefix", prefix).toString(); |
| 74 | } |
| 75 | |
| 76 | @Override |
| 77 | public boolean equals(Object o) { |
| 78 | if (this == o) { |
| 79 | return true; |
| 80 | } |
| 81 | if (o == null || getClass() != o.getClass()) { |
| 82 | return false; |
| 83 | } |
| 84 | |
| 85 | LispEidRecord that = (LispEidRecord) o; |
| 86 | return Objects.equal(maskLength, that.maskLength) && |
| 87 | Objects.equal(prefix, that.prefix); |
| 88 | } |
| 89 | |
| 90 | @Override |
| 91 | public int hashCode() { |
| 92 | return Objects.hashCode(maskLength, prefix); |
| 93 | } |
| 94 | |
Jian Li | 26069e2 | 2016-08-10 22:00:52 +0900 | [diff] [blame] | 95 | /** |
Jian Li | edc5db1 | 2016-08-23 17:30:19 +0900 | [diff] [blame] | 96 | * A LISP message reader for EidRecord portion. |
Jian Li | 26069e2 | 2016-08-10 22:00:52 +0900 | [diff] [blame] | 97 | */ |
Jian Li | edc5db1 | 2016-08-23 17:30:19 +0900 | [diff] [blame] | 98 | public static final class EidRecordReader implements LispMessageReader<LispEidRecord> { |
Jian Li | 26069e2 | 2016-08-10 22:00:52 +0900 | [diff] [blame] | 99 | |
Jian Li | 4767190 | 2016-08-11 01:18:18 +0900 | [diff] [blame] | 100 | private static final int RESERVED_SKIP_LENGTH = 1; |
| 101 | |
Jian Li | 26069e2 | 2016-08-10 22:00:52 +0900 | [diff] [blame] | 102 | @Override |
Jian Li | a7b394d | 2016-08-21 23:11:46 +0900 | [diff] [blame] | 103 | public LispEidRecord readFrom(ByteBuf byteBuf) throws LispParseError, LispReaderException { |
Jian Li | 4767190 | 2016-08-11 01:18:18 +0900 | [diff] [blame] | 104 | |
| 105 | // let's skip the reserved field |
| 106 | byteBuf.skipBytes(RESERVED_SKIP_LENGTH); |
| 107 | |
Jian Li | b26d350 | 2016-08-31 01:43:24 +0900 | [diff] [blame] | 108 | // mask length -> 8 bits |
| 109 | short maskLength = byteBuf.readUnsignedByte(); |
Jian Li | 4767190 | 2016-08-11 01:18:18 +0900 | [diff] [blame] | 110 | |
Jian Li | a7b394d | 2016-08-21 23:11:46 +0900 | [diff] [blame] | 111 | LispAfiAddress prefix = new LispAfiAddress.AfiAddressReader().readFrom(byteBuf); |
Jian Li | 4767190 | 2016-08-11 01:18:18 +0900 | [diff] [blame] | 112 | |
| 113 | return new LispEidRecord((byte) maskLength, prefix); |
Jian Li | 26069e2 | 2016-08-10 22:00:52 +0900 | [diff] [blame] | 114 | } |
| 115 | } |
Jian Li | edc5db1 | 2016-08-23 17:30:19 +0900 | [diff] [blame] | 116 | |
| 117 | /** |
| 118 | * A LISP message writer for EidRecord portion. |
| 119 | */ |
| 120 | public static final class EidRecordWriter implements LispMessageWriter<LispEidRecord> { |
| 121 | |
| 122 | private static final int UNUSED_ZERO = 0; |
| 123 | |
| 124 | @Override |
| 125 | public void writeTo(ByteBuf byteBuf, LispEidRecord message) throws LispWriterException { |
| 126 | |
| 127 | // fill zero into reserved field |
| 128 | byteBuf.writeByte((short) UNUSED_ZERO); |
| 129 | |
| 130 | // mask length |
| 131 | byteBuf.writeByte(message.getMaskLength()); |
| 132 | |
| 133 | // EID prefix AFI with EID prefix |
| 134 | AfiAddressWriter afiAddressWriter = new AfiAddressWriter(); |
| 135 | afiAddressWriter.writeTo(byteBuf, message.getPrefix()); |
| 136 | } |
| 137 | } |
Jian Li | 10a0906 | 2016-07-26 23:58:50 +0900 | [diff] [blame] | 138 | } |