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