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