Jian Li | c7e20a5 | 2016-07-18 19:03:49 +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.types; |
| 17 | |
Jian Li | 115d860 | 2016-08-15 20:21:53 +0900 | [diff] [blame] | 18 | import io.netty.buffer.ByteBuf; |
| 19 | import org.onosproject.lisp.msg.exceptions.LispParseError; |
| 20 | import org.onosproject.lisp.msg.exceptions.LispReaderException; |
GUNi | ba87170 | 2016-08-22 21:06:02 +0900 | [diff] [blame] | 21 | import org.onosproject.lisp.msg.exceptions.LispWriterException; |
Jian Li | 5e505c6 | 2016-12-05 02:44:24 +0900 | [diff] [blame^] | 22 | import org.onosproject.lisp.msg.types.LispAfiAddress.AfiAddressReader; |
Jian Li | 115d860 | 2016-08-15 20:21:53 +0900 | [diff] [blame] | 23 | |
Jian Li | c7e20a5 | 2016-07-18 19:03:49 +0900 | [diff] [blame] | 24 | import java.util.Objects; |
| 25 | |
| 26 | import static com.google.common.base.MoreObjects.toStringHelper; |
Jian Li | d4e6370 | 2016-08-30 18:29:20 +0900 | [diff] [blame] | 27 | import static com.google.common.base.Preconditions.checkNotNull; |
Jian Li | c7e20a5 | 2016-07-18 19:03:49 +0900 | [diff] [blame] | 28 | |
| 29 | /** |
| 30 | * Instance ID type LCAF address class. |
Jian Li | 8fc2d2f | 2016-08-08 14:43:53 +0900 | [diff] [blame] | 31 | * <p> |
Jian Li | 89c9ca9 | 2016-11-11 04:09:02 +0900 | [diff] [blame] | 32 | * Instance ID type is defined in draft-ietf-lisp-lcaf-20 |
| 33 | * https://tools.ietf.org/html/draft-ietf-lisp-lcaf-20#page-7 |
Jian Li | c7e20a5 | 2016-07-18 19:03:49 +0900 | [diff] [blame] | 34 | * |
Jian Li | 8fc2d2f | 2016-08-08 14:43:53 +0900 | [diff] [blame] | 35 | * <pre> |
| 36 | * {@literal |
Jian Li | c7e20a5 | 2016-07-18 19:03:49 +0900 | [diff] [blame] | 37 | * 0 1 2 3 |
| 38 | * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 |
| 39 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 40 | * | AFI = 16387 | Rsvd1 | Flags | |
| 41 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
Jian Li | 89c9ca9 | 2016-11-11 04:09:02 +0900 | [diff] [blame] | 42 | * | Type = 2 | IID mask-len | Length | |
Jian Li | c7e20a5 | 2016-07-18 19:03:49 +0900 | [diff] [blame] | 43 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 44 | * | Instance ID | |
| 45 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 46 | * | AFI = x | Address ... | |
| 47 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
Jian Li | 8fc2d2f | 2016-08-08 14:43:53 +0900 | [diff] [blame] | 48 | * }</pre> |
Jian Li | c7e20a5 | 2016-07-18 19:03:49 +0900 | [diff] [blame] | 49 | */ |
Jian Li | 115d860 | 2016-08-15 20:21:53 +0900 | [diff] [blame] | 50 | public final class LispSegmentLcafAddress extends LispLcafAddress { |
Jian Li | c7e20a5 | 2016-07-18 19:03:49 +0900 | [diff] [blame] | 51 | |
| 52 | private final LispAfiAddress address; |
| 53 | private final int instanceId; |
| 54 | |
| 55 | /** |
| 56 | * Initializes segment type LCAF address. |
| 57 | * |
| 58 | * @param idMaskLength Id mask length |
Jian Li | a7b394d | 2016-08-21 23:11:46 +0900 | [diff] [blame] | 59 | * @param instanceId instance id |
| 60 | * @param address address |
Jian Li | c7e20a5 | 2016-07-18 19:03:49 +0900 | [diff] [blame] | 61 | */ |
Jian Li | 115d860 | 2016-08-15 20:21:53 +0900 | [diff] [blame] | 62 | private LispSegmentLcafAddress(byte idMaskLength, int instanceId, LispAfiAddress address) { |
Jian Li | c7e20a5 | 2016-07-18 19:03:49 +0900 | [diff] [blame] | 63 | super(LispCanonicalAddressFormatEnum.SEGMENT, idMaskLength); |
| 64 | this.address = address; |
| 65 | this.instanceId = instanceId; |
| 66 | } |
| 67 | |
| 68 | /** |
Jian Li | a7b394d | 2016-08-21 23:11:46 +0900 | [diff] [blame] | 69 | * Initializes segment type LCAF address. |
| 70 | * |
| 71 | * @param reserved1 reserved1 |
| 72 | * @param idMaskLength ID mask length |
| 73 | * @param flag flag |
| 74 | * @param length length |
| 75 | * @param instanceId instance id |
| 76 | * @param address address |
| 77 | */ |
| 78 | private LispSegmentLcafAddress(byte reserved1, byte idMaskLength, byte flag, short length, |
| 79 | int instanceId, LispAfiAddress address) { |
| 80 | super(LispCanonicalAddressFormatEnum.SEGMENT, reserved1, idMaskLength, flag, length); |
| 81 | this.address = address; |
| 82 | this.instanceId = instanceId; |
| 83 | } |
| 84 | |
| 85 | /** |
Jian Li | c7e20a5 | 2016-07-18 19:03:49 +0900 | [diff] [blame] | 86 | * Obtains address. |
| 87 | * |
| 88 | * @return address |
| 89 | */ |
| 90 | public LispAfiAddress getAddress() { |
| 91 | return address; |
| 92 | } |
| 93 | |
| 94 | /** |
| 95 | * Obtains instance id. |
| 96 | * |
| 97 | * @return instance id |
| 98 | */ |
| 99 | public int getInstanceId() { |
| 100 | return instanceId; |
| 101 | } |
| 102 | |
| 103 | /** |
| 104 | * Obtains id mask length. |
| 105 | * |
| 106 | * @return id mask length |
| 107 | */ |
| 108 | public byte getIdMaskLength() { |
Jian Li | a7b394d | 2016-08-21 23:11:46 +0900 | [diff] [blame] | 109 | return getReserved2(); |
Jian Li | c7e20a5 | 2016-07-18 19:03:49 +0900 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | @Override |
| 113 | public int hashCode() { |
Jian Li | a7b394d | 2016-08-21 23:11:46 +0900 | [diff] [blame] | 114 | return Objects.hash(address, instanceId, getReserved2()); |
Jian Li | c7e20a5 | 2016-07-18 19:03:49 +0900 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | @Override |
| 118 | public boolean equals(Object obj) { |
| 119 | if (this == obj) { |
| 120 | return true; |
| 121 | } |
| 122 | |
| 123 | if (obj instanceof LispSegmentLcafAddress) { |
| 124 | final LispSegmentLcafAddress other = (LispSegmentLcafAddress) obj; |
| 125 | return Objects.equals(this.address, other.address) && |
Jian Li | a7b394d | 2016-08-21 23:11:46 +0900 | [diff] [blame] | 126 | Objects.equals(this.instanceId, other.instanceId) && |
| 127 | Objects.equals(this.getReserved2(), other.getReserved2()); |
Jian Li | c7e20a5 | 2016-07-18 19:03:49 +0900 | [diff] [blame] | 128 | } |
| 129 | return false; |
| 130 | } |
| 131 | |
| 132 | @Override |
| 133 | public String toString() { |
| 134 | return toStringHelper(this) |
| 135 | .add("address", address) |
| 136 | .add("instanceId", instanceId) |
Jian Li | a7b394d | 2016-08-21 23:11:46 +0900 | [diff] [blame] | 137 | .add("idMaskLength", getReserved2()) |
Jian Li | c7e20a5 | 2016-07-18 19:03:49 +0900 | [diff] [blame] | 138 | .toString(); |
| 139 | } |
Jian Li | 115d860 | 2016-08-15 20:21:53 +0900 | [diff] [blame] | 140 | |
Jian Li | a7b394d | 2016-08-21 23:11:46 +0900 | [diff] [blame] | 141 | public static final class SegmentAddressBuilder |
| 142 | extends LcafAddressBuilder<SegmentAddressBuilder> { |
Jian Li | 115d860 | 2016-08-15 20:21:53 +0900 | [diff] [blame] | 143 | private byte idMaskLength; |
| 144 | private LispAfiAddress address; |
| 145 | private int instanceId; |
| 146 | |
| 147 | /** |
| 148 | * Sets identifier mask length. |
| 149 | * |
| 150 | * @param idMaskLength identifier mask length |
| 151 | * @return SegmentAddressBuilder object |
| 152 | */ |
| 153 | public SegmentAddressBuilder withIdMaskLength(byte idMaskLength) { |
| 154 | this.idMaskLength = idMaskLength; |
| 155 | return this; |
| 156 | } |
| 157 | |
| 158 | /** |
| 159 | * Sets instance identifer. |
| 160 | * |
| 161 | * @param instanceId instance identifier |
| 162 | * @return SegmentAddressBuilder object |
| 163 | */ |
| 164 | public SegmentAddressBuilder withInstanceId(int instanceId) { |
| 165 | this.instanceId = instanceId; |
| 166 | return this; |
| 167 | } |
| 168 | |
| 169 | /** |
| 170 | * Sets AFI address. |
| 171 | * |
| 172 | * @param address AFI address |
| 173 | * @return SegmentAddressBuilder object |
| 174 | */ |
| 175 | public SegmentAddressBuilder withAddress(LispAfiAddress address) { |
| 176 | this.address = address; |
| 177 | return this; |
| 178 | } |
| 179 | |
| 180 | /** |
| 181 | * Builds LispSegmentLcafAddress instance. |
| 182 | * |
| 183 | * @return LispSegmentLcafAddress instance |
| 184 | */ |
| 185 | public LispSegmentLcafAddress build() { |
Jian Li | d4e6370 | 2016-08-30 18:29:20 +0900 | [diff] [blame] | 186 | |
| 187 | checkNotNull(address, "Must specify an address"); |
| 188 | |
Jian Li | a7b394d | 2016-08-21 23:11:46 +0900 | [diff] [blame] | 189 | return new LispSegmentLcafAddress(reserved1, idMaskLength, flag, |
| 190 | length, instanceId, address); |
Jian Li | 115d860 | 2016-08-15 20:21:53 +0900 | [diff] [blame] | 191 | } |
| 192 | } |
| 193 | |
| 194 | /** |
| 195 | * Segment LCAF address reader class. |
| 196 | */ |
| 197 | public static class SegmentLcafAddressReader |
Jian Li | a7b394d | 2016-08-21 23:11:46 +0900 | [diff] [blame] | 198 | implements LispAddressReader<LispSegmentLcafAddress> { |
Jian Li | 115d860 | 2016-08-15 20:21:53 +0900 | [diff] [blame] | 199 | |
| 200 | @Override |
| 201 | public LispSegmentLcafAddress readFrom(ByteBuf byteBuf) |
Jian Li | a7b394d | 2016-08-21 23:11:46 +0900 | [diff] [blame] | 202 | throws LispParseError, LispReaderException { |
Jian Li | 115d860 | 2016-08-15 20:21:53 +0900 | [diff] [blame] | 203 | |
Jian Li | a7b394d | 2016-08-21 23:11:46 +0900 | [diff] [blame] | 204 | LispLcafAddress lcafAddress = LispLcafAddress.deserializeCommon(byteBuf); |
| 205 | |
| 206 | byte idMaskLength = lcafAddress.getReserved2(); |
| 207 | |
Jian Li | 115d860 | 2016-08-15 20:21:53 +0900 | [diff] [blame] | 208 | int instanceId = (int) byteBuf.readUnsignedInt(); |
Jian Li | 5e505c6 | 2016-12-05 02:44:24 +0900 | [diff] [blame^] | 209 | LispAfiAddress address = new AfiAddressReader().readFrom(byteBuf); |
Jian Li | 115d860 | 2016-08-15 20:21:53 +0900 | [diff] [blame] | 210 | |
| 211 | return new SegmentAddressBuilder() |
Jian Li | a7b394d | 2016-08-21 23:11:46 +0900 | [diff] [blame] | 212 | .withReserved1(lcafAddress.getReserved1()) |
| 213 | .withFlag(lcafAddress.getFlag()) |
| 214 | .withLength(lcafAddress.getLength()) |
| 215 | .withIdMaskLength(idMaskLength) |
| 216 | .withInstanceId(instanceId) |
| 217 | .withAddress(address) |
| 218 | .build(); |
Jian Li | 115d860 | 2016-08-15 20:21:53 +0900 | [diff] [blame] | 219 | } |
| 220 | } |
GUNi | ba87170 | 2016-08-22 21:06:02 +0900 | [diff] [blame] | 221 | |
| 222 | /** |
| 223 | * Segment LCAF address writer class. |
| 224 | */ |
| 225 | public static class SegmentLcafAddressWriter |
| 226 | implements LispAddressWriter<LispSegmentLcafAddress> { |
| 227 | |
| 228 | @Override |
| 229 | public void writeTo(ByteBuf byteBuf, LispSegmentLcafAddress address) |
| 230 | throws LispWriterException { |
| 231 | |
Jian Li | ef0f723 | 2016-11-15 19:55:46 +0900 | [diff] [blame] | 232 | int lcafIndex = byteBuf.writerIndex(); |
GUNi | ba87170 | 2016-08-22 21:06:02 +0900 | [diff] [blame] | 233 | LispLcafAddress.serializeCommon(byteBuf, address); |
| 234 | |
| 235 | byteBuf.writeInt(address.getInstanceId()); |
Jian Li | 76ea057 | 2016-08-29 12:41:16 +0900 | [diff] [blame] | 236 | |
| 237 | new LispAfiAddress.AfiAddressWriter().writeTo(byteBuf, address.getAddress()); |
Jian Li | ef0f723 | 2016-11-15 19:55:46 +0900 | [diff] [blame] | 238 | |
| 239 | LispLcafAddress.updateLength(lcafIndex, byteBuf); |
GUNi | ba87170 | 2016-08-22 21:06:02 +0900 | [diff] [blame] | 240 | } |
| 241 | } |
Jian Li | c7e20a5 | 2016-07-18 19:03:49 +0900 | [diff] [blame] | 242 | } |