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