Jian Li | 3ff578d | 2017-02-05 15:34:37 +0900 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2017-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.lcaf; |
| 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; |
| 22 | import org.onosproject.lisp.msg.types.LispAddressReader; |
| 23 | import org.onosproject.lisp.msg.types.LispAddressWriter; |
| 24 | import org.onosproject.lisp.msg.types.LispAfiAddress; |
| 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 | * Nonce locator data type LCAF address class. |
| 33 | * <p> |
| 34 | * Nonce locator data 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-32 |
Yuta HIGUCHI | d1ce4bc | 2017-06-03 01:05:33 -0700 | [diff] [blame^] | 36 | * |
Jian Li | 3ff578d | 2017-02-05 15:34:37 +0900 | [diff] [blame] | 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 = 8 | Rsvd2 | Length | |
| 45 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 46 | * | Reserved | Nonce | |
| 47 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 48 | * | AFI = x | Address ... | |
| 49 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 50 | * }</pre> |
| 51 | */ |
| 52 | public final class LispNonceLcafAddress extends LispLcafAddress { |
| 53 | |
| 54 | private final int nonce; |
| 55 | private final LispAfiAddress address; |
| 56 | |
| 57 | /** |
| 58 | * Initializes nonce locator data type LCAF address. |
| 59 | * |
| 60 | * @param nonce nonce |
| 61 | * @param address address |
| 62 | */ |
| 63 | private LispNonceLcafAddress(int nonce, LispAfiAddress address) { |
| 64 | super(LispCanonicalAddressFormatEnum.NONCE); |
| 65 | this.nonce = nonce; |
| 66 | this.address = address; |
| 67 | } |
| 68 | |
| 69 | /** |
| 70 | * Obtains nonce. |
| 71 | * |
| 72 | * @return nonce |
| 73 | */ |
| 74 | public int getNonce() { |
| 75 | return nonce; |
| 76 | } |
| 77 | |
| 78 | /** |
| 79 | * Obtains address. |
| 80 | * |
| 81 | * @return address |
| 82 | */ |
| 83 | public LispAfiAddress getAddress() { |
| 84 | return address; |
| 85 | } |
| 86 | |
| 87 | @Override |
| 88 | public int hashCode() { |
| 89 | return Objects.hash(nonce, address); |
| 90 | } |
| 91 | |
| 92 | @Override |
| 93 | public boolean equals(Object obj) { |
| 94 | if (this == obj) { |
| 95 | return true; |
| 96 | } |
| 97 | |
| 98 | if (obj instanceof LispNonceLcafAddress) { |
| 99 | final LispNonceLcafAddress other = (LispNonceLcafAddress) obj; |
| 100 | return Objects.equals(this.nonce, other.nonce) && |
| 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("nonce", nonce) |
| 110 | .add("address", address) |
| 111 | .toString(); |
| 112 | } |
| 113 | |
| 114 | public static final class NonceAddressBuilder |
| 115 | extends LcafAddressBuilder<NonceAddressBuilder> { |
| 116 | private int nonce; |
| 117 | private LispAfiAddress address; |
| 118 | |
| 119 | /** |
| 120 | * Sets nonce. |
| 121 | * |
| 122 | * @param nonce nonce |
| 123 | * @return NonceAddressBuilder object |
| 124 | */ |
| 125 | public NonceAddressBuilder withNonce(int nonce) { |
| 126 | this.nonce = nonce; |
| 127 | return this; |
| 128 | } |
| 129 | |
| 130 | /** |
| 131 | * Sets address. |
| 132 | * |
| 133 | * @param address address |
| 134 | * @return NonceAddressBuilder object |
| 135 | */ |
| 136 | public NonceAddressBuilder withAddress(LispAfiAddress address) { |
| 137 | this.address = address; |
| 138 | return this; |
| 139 | } |
| 140 | |
| 141 | /** |
| 142 | * Builds LispNonceLcafAddress instance. |
| 143 | * |
| 144 | * @return LispNonceLcafAddress instance |
| 145 | */ |
Yuta HIGUCHI | d1ce4bc | 2017-06-03 01:05:33 -0700 | [diff] [blame^] | 146 | @Override |
Jian Li | 3ff578d | 2017-02-05 15:34:37 +0900 | [diff] [blame] | 147 | public LispNonceLcafAddress build() { |
| 148 | |
| 149 | checkNotNull(address, "Must specify an address"); |
| 150 | |
| 151 | return new LispNonceLcafAddress(nonce, address); |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | /** |
| 156 | * Nonce LCAF address reader class. |
| 157 | */ |
| 158 | public static class NonceLcafAddressReader |
| 159 | implements LispAddressReader<LispNonceLcafAddress> { |
| 160 | |
| 161 | private static final int NONCE_SHIFT_BIT = 16; |
| 162 | private static final int RESERVED_SKIP_LENGTH = 1; |
| 163 | |
| 164 | @Override |
| 165 | public LispNonceLcafAddress readFrom(ByteBuf byteBuf) |
| 166 | throws LispParseError, LispReaderException { |
| 167 | |
| 168 | deserializeCommon(byteBuf); |
| 169 | |
| 170 | byteBuf.skipBytes(RESERVED_SKIP_LENGTH); |
| 171 | |
| 172 | // nonce -> 24 bits |
| 173 | int nonceFirst = byteBuf.readShort() << NONCE_SHIFT_BIT; |
| 174 | int nonceSecond = byteBuf.readInt(); |
| 175 | int nonce = nonceFirst + nonceSecond; |
| 176 | |
| 177 | LispAfiAddress address = new AfiAddressReader().readFrom(byteBuf); |
| 178 | |
| 179 | return new NonceAddressBuilder() |
| 180 | .withNonce(nonce) |
| 181 | .withAddress(address) |
| 182 | .build(); |
| 183 | } |
| 184 | } |
| 185 | |
| 186 | /** |
| 187 | * Nonce LCAF address writer class. |
| 188 | */ |
| 189 | public static class NonceLcafAddressWriter |
| 190 | implements LispAddressWriter<LispNonceLcafAddress> { |
| 191 | |
| 192 | private static final int UNUSED_ZERO = 0; |
| 193 | private static final int NONCE_SHIFT_BIT = 16; |
| 194 | |
| 195 | @Override |
| 196 | public void writeTo(ByteBuf byteBuf, LispNonceLcafAddress address) |
| 197 | throws LispWriterException { |
| 198 | int lcafIndex = byteBuf.writerIndex(); |
| 199 | LispLcafAddress.serializeCommon(byteBuf, address); |
| 200 | |
| 201 | // reserved field |
| 202 | byteBuf.writeByte(UNUSED_ZERO); |
| 203 | |
| 204 | // nonce field |
| 205 | int nonceFirst = address.getNonce() >> NONCE_SHIFT_BIT; |
| 206 | byteBuf.writeShort((short) nonceFirst); |
| 207 | int nonceSecond = address.getNonce() - (nonceFirst << NONCE_SHIFT_BIT); |
| 208 | byteBuf.writeInt(nonceSecond); |
| 209 | |
| 210 | // address |
| 211 | AfiAddressWriter writer = new AfiAddressWriter(); |
| 212 | writer.writeTo(byteBuf, address.getAddress()); |
| 213 | |
| 214 | LispLcafAddress.updateLength(lcafIndex, byteBuf); |
| 215 | } |
| 216 | } |
| 217 | } |