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