blob: 66ef81df83b255332770980893e9ffd0a2c92db5 [file] [log] [blame]
Jian Li47671902016-08-11 01:18:18 +09001/*
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 */
16package org.onosproject.lisp.msg.protocols;
17
18import com.google.common.base.Objects;
19import io.netty.buffer.ByteBuf;
20import org.onlab.util.ByteOperator;
21import org.onosproject.lisp.msg.exceptions.LispParseError;
Jian Lia7b394d2016-08-21 23:11:46 +090022import org.onosproject.lisp.msg.exceptions.LispReaderException;
Jian Li47671902016-08-11 01:18:18 +090023import org.onosproject.lisp.msg.types.LispAfiAddress;
24
25import static com.google.common.base.MoreObjects.toStringHelper;
26
27/**
28 * Default implementation of LispLocatorRecord.
29 */
30public final class DefaultLispLocatorRecord implements LispLocatorRecord {
31
32 private final byte priority;
33 private final byte weight;
34 private final byte multicastPriority;
35 private final byte multicastWeight;
36 private final boolean localLocator;
37 private final boolean rlocProbed;
38 private final boolean routed;
39 private final LispAfiAddress locatorAfi;
40
41 /**
42 * A private constructor that protects object instantiation from external.
43 *
44 * @param priority uni-cast priority
45 * @param weight uni-cast weight
46 * @param multicastPriority multi-cast priority
47 * @param multicastWeight multi-cast weight
48 * @param localLocator local locator flag
49 * @param rlocProbed RLOC probed flag
50 * @param routed routed flag
51 * @param locatorAfi locator AFI
52 */
53 private DefaultLispLocatorRecord(byte priority, byte weight, byte multicastPriority,
54 byte multicastWeight, boolean localLocator, boolean rlocProbed,
55 boolean routed, LispAfiAddress locatorAfi) {
56 this.priority = priority;
57 this.weight = weight;
58 this.multicastPriority = multicastPriority;
59 this.multicastWeight = multicastWeight;
60 this.localLocator = localLocator;
61 this.rlocProbed = rlocProbed;
62 this.routed = routed;
63 this.locatorAfi = locatorAfi;
64 }
65
66 @Override
67 public byte getPriority() {
68 return priority;
69 }
70
71 @Override
72 public byte getWeight() {
73 return weight;
74 }
75
76 @Override
77 public byte getMulticastPriority() {
78 return multicastPriority;
79 }
80
81 @Override
82 public byte getMulticastWeight() {
83 return multicastWeight;
84 }
85
86 @Override
87 public boolean isLocalLocator() {
88 return localLocator;
89 }
90
91 @Override
92 public boolean isRlocProbed() {
93 return rlocProbed;
94 }
95
96 @Override
97 public boolean isRouted() {
98 return routed;
99 }
100
101 @Override
102 public LispAfiAddress getLocatorAfi() {
103 return locatorAfi;
104 }
105
106 @Override
107 public void writeTo(ByteBuf byteBuf) {
108
109 }
110
111 @Override
112 public String toString() {
113 return toStringHelper(this)
114 .add("priority", priority)
115 .add("weight", weight)
116 .add("multi-cast priority", multicastPriority)
117 .add("multi-cast weight", multicastWeight)
118 .add("local locator", localLocator)
119 .add("RLOC probed", rlocProbed)
120 .add("routed", routed)
121 .add("locator AFI", locatorAfi).toString();
122 }
123
124 @Override
125 public boolean equals(Object o) {
126 if (this == o) {
127 return true;
128 }
129 if (o == null || getClass() != o.getClass()) {
130 return false;
131 }
132 DefaultLispLocatorRecord that = (DefaultLispLocatorRecord) o;
133 return Objects.equal(priority, that.priority) &&
134 Objects.equal(weight, that.weight) &&
135 Objects.equal(multicastPriority, that.multicastPriority) &&
136 Objects.equal(multicastWeight, that.multicastWeight) &&
137 Objects.equal(localLocator, that.localLocator) &&
138 Objects.equal(rlocProbed, that.rlocProbed) &&
139 Objects.equal(routed, that.routed) &&
140 Objects.equal(locatorAfi, that.locatorAfi);
141 }
142
143 @Override
144 public int hashCode() {
145 return Objects.hashCode(priority, weight, multicastPriority,
146 multicastWeight, localLocator, rlocProbed, routed, locatorAfi);
147 }
148
149 public static final class DefaultLocatorRecordBuilder implements LocatorRecordBuilder {
150
151 private byte priority;
152 private byte weight;
153 private byte multicastPriority;
154 private byte multicastWeight;
155 private boolean localLocator;
156 private boolean rlocProbed;
157 private boolean routed;
158 private LispAfiAddress locatorAfi;
159
160 @Override
161 public LocatorRecordBuilder withPriority(byte priority) {
162 this.priority = priority;
163 return this;
164 }
165
166 @Override
167 public LocatorRecordBuilder withWeight(byte weight) {
168 this.weight = weight;
169 return this;
170 }
171
172 @Override
173 public LocatorRecordBuilder withMulticastPriority(byte priority) {
174 this.multicastPriority = priority;
175 return this;
176 }
177
178 @Override
179 public LocatorRecordBuilder withMulticastWeight(byte weight) {
180 this.multicastWeight = weight;
181 return this;
182 }
183
184 @Override
185 public LocatorRecordBuilder withLocalLocator(boolean localLocator) {
186 this.localLocator = localLocator;
187 return this;
188 }
189
190 @Override
191 public LocatorRecordBuilder withRlocProbed(boolean rlocProbed) {
192 this.rlocProbed = rlocProbed;
193 return this;
194 }
195
196 @Override
197 public LocatorRecordBuilder withRouted(boolean routed) {
198 this.routed = routed;
199 return this;
200 }
201
202 @Override
203 public LocatorRecordBuilder withLocatorAfi(LispAfiAddress locatorAfi) {
204 this.locatorAfi = locatorAfi;
205 return this;
206 }
207
208 @Override
209 public LispLocatorRecord build() {
210 return new DefaultLispLocatorRecord(priority, weight, multicastPriority,
211 multicastWeight, localLocator, rlocProbed, routed, locatorAfi);
212 }
213 }
214
215 /**
216 * A LISP message reader for LocatorRecord portion.
217 */
218 public static final class LocatorRecordReader implements LispMessageReader<LispLocatorRecord> {
219
220 private static final int SKIP_UNUSED_FLAG_LENGTH = 1;
221 private static final int LOCAL_LOCATOR_INDEX = 2;
222 private static final int RLOC_PROBED_INDEX = 1;
223 private static final int ROUTED_INDEX = 0;
224
225 @Override
Jian Lia7b394d2016-08-21 23:11:46 +0900226 public LispLocatorRecord readFrom(ByteBuf byteBuf) throws LispParseError, LispReaderException {
Jian Li47671902016-08-11 01:18:18 +0900227
228 // priority -> 8 bits
229 byte priority = (byte) byteBuf.readUnsignedByte();
230
231 // weight -> 8 bits
232 byte weight = (byte) byteBuf.readUnsignedByte();
233
234 // multi-cast priority -> 8 bits
235 byte multicastPriority = (byte) byteBuf.readUnsignedByte();
236
237 // multi-cast weight -> 8 bits
238 byte multicastWeight = (byte) byteBuf.readUnsignedByte();
239
240 // let's skip unused flags
241 byteBuf.skipBytes(SKIP_UNUSED_FLAG_LENGTH);
242
243 byte flags = byteBuf.readByte();
244
245 // local locator flag -> 1 bit
246 boolean localLocator = ByteOperator.getBit(flags, LOCAL_LOCATOR_INDEX);
247
248 // rloc probe flag -> 1 bit
249 boolean rlocProbed = ByteOperator.getBit(flags, RLOC_PROBED_INDEX);
250
251 // routed flag -> 1 bit
252 boolean routed = ByteOperator.getBit(flags, ROUTED_INDEX);
253
Jian Lia7b394d2016-08-21 23:11:46 +0900254 LispAfiAddress address = new LispAfiAddress.AfiAddressReader().readFrom(byteBuf);
Jian Li47671902016-08-11 01:18:18 +0900255
256 return new DefaultLocatorRecordBuilder()
257 .withPriority(priority)
258 .withWeight(weight)
259 .withMulticastPriority(multicastPriority)
260 .withMulticastWeight(multicastWeight)
261 .withLocalLocator(localLocator)
262 .withRlocProbed(rlocProbed)
263 .withRouted(routed)
Jian Lia7b394d2016-08-21 23:11:46 +0900264 .withLocatorAfi(address)
Jian Li47671902016-08-11 01:18:18 +0900265 .build();
266 }
267 }
268}