Jian Li | 01f75b9 | 2016-12-12 03:05:07 +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 | 01f75b9 | 2016-12-12 03:05:07 +0900 | [diff] [blame] | 17 | |
| 18 | import io.netty.buffer.ByteBuf; |
| 19 | import org.onosproject.lisp.msg.exceptions.LispParseError; |
| 20 | import org.onosproject.lisp.msg.exceptions.LispReaderException; |
| 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 | 01f75b9 | 2016-12-12 03:05:07 +0900 | [diff] [blame] | 25 | |
| 26 | import java.util.Objects; |
| 27 | |
| 28 | import static com.google.common.base.MoreObjects.toStringHelper; |
| 29 | import static com.google.common.base.Preconditions.checkNotNull; |
| 30 | |
| 31 | /** |
| 32 | * AS Numbers type LCAF address class. |
| 33 | * <p> |
| 34 | * AS Number type is defined in draft-ietf-lisp-lcaf-22 |
| 35 | * https://tools.ietf.org/html/draft-ietf-lisp-lcaf-22#page-9 |
| 36 | * |
| 37 | * <pre> |
| 38 | * {@literal |
| 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 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 44 | * | Type = 3 | Rsvd2 | Length | |
| 45 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 46 | * | AS Number | |
| 47 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 48 | * | AFI = x | Address ... | |
| 49 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 50 | * }</pre> |
| 51 | */ |
| 52 | public final class LispAsLcafAddress extends LispLcafAddress { |
| 53 | |
| 54 | private final LispAfiAddress address; |
| 55 | private final int asNumber; |
| 56 | |
| 57 | /** |
| 58 | * Initializes AS numbers type LCAF address. |
| 59 | * |
| 60 | * @param asNumber AS number |
| 61 | * @param address AFI address |
| 62 | */ |
| 63 | private LispAsLcafAddress(int asNumber, LispAfiAddress address) { |
| 64 | super(LispCanonicalAddressFormatEnum.AS); |
| 65 | this.asNumber = asNumber; |
| 66 | this.address = address; |
| 67 | } |
| 68 | |
| 69 | /** |
| 70 | * Obtains address. |
| 71 | * |
| 72 | * @return address |
| 73 | */ |
| 74 | public LispAfiAddress getAddress() { |
| 75 | return address; |
| 76 | } |
| 77 | |
| 78 | /** |
| 79 | * Obtains AS number. |
| 80 | * |
| 81 | * @return AS number |
| 82 | */ |
| 83 | public int getAsNumber() { |
| 84 | return asNumber; |
| 85 | } |
| 86 | |
| 87 | @Override |
| 88 | public int hashCode() { |
| 89 | return Objects.hash(asNumber, address); |
| 90 | } |
| 91 | |
| 92 | @Override |
| 93 | public boolean equals(Object obj) { |
| 94 | if (this == obj) { |
| 95 | return true; |
| 96 | } |
| 97 | |
| 98 | if (obj instanceof LispAsLcafAddress) { |
| 99 | final LispAsLcafAddress other = (LispAsLcafAddress) obj; |
| 100 | return Objects.equals(this.asNumber, other.asNumber) && |
| 101 | Objects.equals(this.address, other.address); |
| 102 | } |
| 103 | return false; |
| 104 | } |
| 105 | |
| 106 | @Override |
| 107 | public String toString() { |
| 108 | return toStringHelper(this) |
| 109 | .add("asNumber", asNumber) |
| 110 | .add("address", address) |
| 111 | .toString(); |
| 112 | } |
| 113 | |
| 114 | public static final class AsAddressBuilder |
| 115 | extends LcafAddressBuilder<AsAddressBuilder> { |
| 116 | private int asNumber; |
| 117 | private LispAfiAddress address; |
| 118 | |
| 119 | /** |
| 120 | * Sets AS number. |
| 121 | * |
| 122 | * @param asNumber AS number |
| 123 | * @return AsAddressBuilder object |
| 124 | */ |
| 125 | public AsAddressBuilder withAsNumber(int asNumber) { |
| 126 | this.asNumber = asNumber; |
| 127 | return this; |
| 128 | } |
| 129 | |
| 130 | /** |
| 131 | * Sets AFI address. |
| 132 | * |
| 133 | * @param address AFI address |
| 134 | * @return AsAddressBuilder object |
| 135 | */ |
| 136 | public AsAddressBuilder withAddress(LispAfiAddress address) { |
| 137 | this.address = address; |
| 138 | return this; |
| 139 | } |
| 140 | |
| 141 | /** |
| 142 | * Builds LispAsLcafAddress instance. |
| 143 | * |
| 144 | * @return LispAsLcafAddress instance |
| 145 | */ |
| 146 | public LispAsLcafAddress build() { |
| 147 | |
| 148 | checkNotNull(address, "Must specify an address"); |
| 149 | |
| 150 | return new LispAsLcafAddress(asNumber, address); |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | /** |
| 155 | * AS number LCAF address reader class. |
| 156 | */ |
| 157 | public static class AsLcafAddressReader |
| 158 | implements LispAddressReader<LispAsLcafAddress> { |
| 159 | |
| 160 | @Override |
| 161 | public LispAsLcafAddress readFrom(ByteBuf byteBuf) |
| 162 | throws LispParseError, LispReaderException { |
| 163 | |
| 164 | LispLcafAddress.deserializeCommon(byteBuf); |
| 165 | |
| 166 | int asNumber = (int) byteBuf.readUnsignedInt(); |
Jian Li | f31019a | 2017-02-05 07:57:46 +0900 | [diff] [blame] | 167 | LispAfiAddress address = new LispAfiAddress.AfiAddressReader().readFrom(byteBuf); |
Jian Li | 01f75b9 | 2016-12-12 03:05:07 +0900 | [diff] [blame] | 168 | |
| 169 | return new AsAddressBuilder() |
| 170 | .withAsNumber(asNumber) |
| 171 | .withAddress(address) |
| 172 | .build(); |
| 173 | } |
| 174 | } |
| 175 | |
| 176 | /** |
| 177 | * AS number LCAF address writer class. |
| 178 | */ |
| 179 | public static class AsLcafAddressWriter |
| 180 | implements LispAddressWriter<LispAsLcafAddress> { |
| 181 | |
| 182 | @Override |
| 183 | public void writeTo(ByteBuf byteBuf, LispAsLcafAddress address) |
| 184 | throws LispWriterException { |
| 185 | |
| 186 | int lcafIndex = byteBuf.writerIndex(); |
| 187 | LispLcafAddress.serializeCommon(byteBuf, address); |
| 188 | |
| 189 | byteBuf.writeInt(address.getAsNumber()); |
| 190 | |
Jian Li | f31019a | 2017-02-05 07:57:46 +0900 | [diff] [blame] | 191 | new LispAfiAddress.AfiAddressWriter().writeTo(byteBuf, address.getAddress()); |
Jian Li | 01f75b9 | 2016-12-12 03:05:07 +0900 | [diff] [blame] | 192 | |
| 193 | LispLcafAddress.updateLength(lcafIndex, byteBuf); |
| 194 | } |
| 195 | } |
| 196 | } |