blob: 09305397645cc076e0320616fbe2e39f2de2cb3d [file] [log] [blame]
Jian Li3ff578d2017-02-05 15:34:37 +09001/*
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 */
16package org.onosproject.lisp.msg.types.lcaf;
17
18import io.netty.buffer.ByteBuf;
19import org.onosproject.lisp.msg.exceptions.LispParseError;
20import org.onosproject.lisp.msg.exceptions.LispReaderException;
21import org.onosproject.lisp.msg.exceptions.LispWriterException;
22import org.onosproject.lisp.msg.types.LispAddressReader;
23import org.onosproject.lisp.msg.types.LispAddressWriter;
24import org.onosproject.lisp.msg.types.LispAfiAddress;
25
26import java.util.Objects;
27
28import static com.google.common.base.MoreObjects.toStringHelper;
29import 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
35 * https://tools.ietf.org/html/draft-ietf-lisp-lcaf-22#page-30
36 * <p>
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 */
52public 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 */
146 public LispNonceLcafAddress build() {
147
148 checkNotNull(address, "Must specify an address");
149
150 return new LispNonceLcafAddress(nonce, address);
151 }
152 }
153
154 /**
155 * Nonce LCAF address reader class.
156 */
157 public static class NonceLcafAddressReader
158 implements LispAddressReader<LispNonceLcafAddress> {
159
160 private static final int NONCE_SHIFT_BIT = 16;
161 private static final int RESERVED_SKIP_LENGTH = 1;
162
163 @Override
164 public LispNonceLcafAddress readFrom(ByteBuf byteBuf)
165 throws LispParseError, LispReaderException {
166
167 deserializeCommon(byteBuf);
168
169 byteBuf.skipBytes(RESERVED_SKIP_LENGTH);
170
171 // nonce -> 24 bits
172 int nonceFirst = byteBuf.readShort() << NONCE_SHIFT_BIT;
173 int nonceSecond = byteBuf.readInt();
174 int nonce = nonceFirst + nonceSecond;
175
176 LispAfiAddress address = new AfiAddressReader().readFrom(byteBuf);
177
178 return new NonceAddressBuilder()
179 .withNonce(nonce)
180 .withAddress(address)
181 .build();
182 }
183 }
184
185 /**
186 * Nonce LCAF address writer class.
187 */
188 public static class NonceLcafAddressWriter
189 implements LispAddressWriter<LispNonceLcafAddress> {
190
191 private static final int UNUSED_ZERO = 0;
192 private static final int NONCE_SHIFT_BIT = 16;
193
194 @Override
195 public void writeTo(ByteBuf byteBuf, LispNonceLcafAddress address)
196 throws LispWriterException {
197 int lcafIndex = byteBuf.writerIndex();
198 LispLcafAddress.serializeCommon(byteBuf, address);
199
200 // reserved field
201 byteBuf.writeByte(UNUSED_ZERO);
202
203 // nonce field
204 int nonceFirst = address.getNonce() >> NONCE_SHIFT_BIT;
205 byteBuf.writeShort((short) nonceFirst);
206 int nonceSecond = address.getNonce() - (nonceFirst << NONCE_SHIFT_BIT);
207 byteBuf.writeInt(nonceSecond);
208
209 // address
210 AfiAddressWriter writer = new AfiAddressWriter();
211 writer.writeTo(byteBuf, address.getAddress());
212
213 LispLcafAddress.updateLength(lcafIndex, byteBuf);
214 }
215 }
216}