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; |
| 21 | |
Jian Li | c7e20a5 | 2016-07-18 19:03:49 +0900 | [diff] [blame] | 22 | import java.util.Objects; |
| 23 | |
| 24 | import static com.google.common.base.MoreObjects.toStringHelper; |
| 25 | |
| 26 | /** |
| 27 | * Instance ID type LCAF address class. |
Jian Li | 8fc2d2f | 2016-08-08 14:43:53 +0900 | [diff] [blame] | 28 | * <p> |
Jian Li | c7e20a5 | 2016-07-18 19:03:49 +0900 | [diff] [blame] | 29 | * Instance ID type is defined in draft-ietf-lisp-lcaf-13 |
| 30 | * https://tools.ietf.org/html/draft-ietf-lisp-lcaf-13#page-7 |
| 31 | * |
Jian Li | 8fc2d2f | 2016-08-08 14:43:53 +0900 | [diff] [blame] | 32 | * <pre> |
| 33 | * {@literal |
Jian Li | c7e20a5 | 2016-07-18 19:03:49 +0900 | [diff] [blame] | 34 | * 0 1 2 3 |
| 35 | * 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 |
| 36 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 37 | * | AFI = 16387 | Rsvd1 | Flags | |
| 38 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 39 | * | Type = 2 | IID mask-len | 4 + n | |
| 40 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 41 | * | Instance ID | |
| 42 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 43 | * | AFI = x | Address ... | |
| 44 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
Jian Li | 8fc2d2f | 2016-08-08 14:43:53 +0900 | [diff] [blame] | 45 | * }</pre> |
Jian Li | c7e20a5 | 2016-07-18 19:03:49 +0900 | [diff] [blame] | 46 | */ |
Jian Li | 115d860 | 2016-08-15 20:21:53 +0900 | [diff] [blame] | 47 | public final class LispSegmentLcafAddress extends LispLcafAddress { |
Jian Li | c7e20a5 | 2016-07-18 19:03:49 +0900 | [diff] [blame] | 48 | |
| 49 | private final LispAfiAddress address; |
| 50 | private final int instanceId; |
| 51 | |
| 52 | /** |
| 53 | * Initializes segment type LCAF address. |
| 54 | * |
| 55 | * @param idMaskLength Id mask length |
| 56 | * @param instanceId instance id |
| 57 | * @param address address |
| 58 | */ |
Jian Li | 115d860 | 2016-08-15 20:21:53 +0900 | [diff] [blame] | 59 | private LispSegmentLcafAddress(byte idMaskLength, int instanceId, LispAfiAddress address) { |
Jian Li | c7e20a5 | 2016-07-18 19:03:49 +0900 | [diff] [blame] | 60 | super(LispCanonicalAddressFormatEnum.SEGMENT, idMaskLength); |
| 61 | this.address = address; |
| 62 | this.instanceId = instanceId; |
| 63 | } |
| 64 | |
| 65 | /** |
| 66 | * Obtains address. |
| 67 | * |
| 68 | * @return address |
| 69 | */ |
| 70 | public LispAfiAddress getAddress() { |
| 71 | return address; |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * Obtains instance id. |
| 76 | * |
| 77 | * @return instance id |
| 78 | */ |
| 79 | public int getInstanceId() { |
| 80 | return instanceId; |
| 81 | } |
| 82 | |
| 83 | /** |
| 84 | * Obtains id mask length. |
| 85 | * |
| 86 | * @return id mask length |
| 87 | */ |
| 88 | public byte getIdMaskLength() { |
| 89 | return reserved2; |
| 90 | } |
| 91 | |
| 92 | @Override |
| 93 | public int hashCode() { |
Jian Li | d56f97e | 2016-07-19 15:48:15 +0900 | [diff] [blame] | 94 | return Objects.hash(address, instanceId, reserved2); |
Jian Li | c7e20a5 | 2016-07-18 19:03:49 +0900 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | @Override |
| 98 | public boolean equals(Object obj) { |
| 99 | if (this == obj) { |
| 100 | return true; |
| 101 | } |
| 102 | |
| 103 | if (obj instanceof LispSegmentLcafAddress) { |
| 104 | final LispSegmentLcafAddress other = (LispSegmentLcafAddress) obj; |
| 105 | return Objects.equals(this.address, other.address) && |
| 106 | Objects.equals(this.instanceId, other.instanceId) && |
| 107 | Objects.equals(this.reserved2, other.reserved2); |
| 108 | } |
| 109 | return false; |
| 110 | } |
| 111 | |
| 112 | @Override |
| 113 | public String toString() { |
| 114 | return toStringHelper(this) |
| 115 | .add("address", address) |
| 116 | .add("instanceId", instanceId) |
| 117 | .add("idMaskLength", reserved2) |
| 118 | .toString(); |
| 119 | } |
Jian Li | 115d860 | 2016-08-15 20:21:53 +0900 | [diff] [blame] | 120 | |
| 121 | public static final class SegmentAddressBuilder { |
| 122 | private byte idMaskLength; |
| 123 | private LispAfiAddress address; |
| 124 | private int instanceId; |
| 125 | |
| 126 | /** |
| 127 | * Sets identifier mask length. |
| 128 | * |
| 129 | * @param idMaskLength identifier mask length |
| 130 | * @return SegmentAddressBuilder object |
| 131 | */ |
| 132 | public SegmentAddressBuilder withIdMaskLength(byte idMaskLength) { |
| 133 | this.idMaskLength = idMaskLength; |
| 134 | return this; |
| 135 | } |
| 136 | |
| 137 | /** |
| 138 | * Sets instance identifer. |
| 139 | * |
| 140 | * @param instanceId instance identifier |
| 141 | * @return SegmentAddressBuilder object |
| 142 | */ |
| 143 | public SegmentAddressBuilder withInstanceId(int instanceId) { |
| 144 | this.instanceId = instanceId; |
| 145 | return this; |
| 146 | } |
| 147 | |
| 148 | /** |
| 149 | * Sets AFI address. |
| 150 | * |
| 151 | * @param address AFI address |
| 152 | * @return SegmentAddressBuilder object |
| 153 | */ |
| 154 | public SegmentAddressBuilder withAddress(LispAfiAddress address) { |
| 155 | this.address = address; |
| 156 | return this; |
| 157 | } |
| 158 | |
| 159 | /** |
| 160 | * Builds LispSegmentLcafAddress instance. |
| 161 | * |
| 162 | * @return LispSegmentLcafAddress instance |
| 163 | */ |
| 164 | public LispSegmentLcafAddress build() { |
| 165 | return new LispSegmentLcafAddress(idMaskLength, instanceId, address); |
| 166 | } |
| 167 | } |
| 168 | |
| 169 | /** |
| 170 | * Segment LCAF address reader class. |
| 171 | */ |
| 172 | public static class SegmentLcafAddressReader |
| 173 | implements LispAddressReader<LispSegmentLcafAddress> { |
| 174 | |
| 175 | @Override |
| 176 | public LispSegmentLcafAddress readFrom(ByteBuf byteBuf) |
| 177 | throws LispParseError, LispReaderException { |
| 178 | |
| 179 | // TODO: need to de-serialize IdMaskLength |
| 180 | byte idMaskLength = 0x01; |
| 181 | int instanceId = (int) byteBuf.readUnsignedInt(); |
| 182 | LispAfiAddress address = new LispIpAddress.IpAddressReader().readFrom(byteBuf); |
| 183 | |
| 184 | return new SegmentAddressBuilder() |
| 185 | .withIdMaskLength(idMaskLength) |
| 186 | .withInstanceId(instanceId) |
| 187 | .withAddress(address) |
| 188 | .build(); |
| 189 | } |
| 190 | } |
Jian Li | c7e20a5 | 2016-07-18 19:03:49 +0900 | [diff] [blame] | 191 | } |