blob: 9157532100428b8e594778de141af5b1f9a9a522 [file] [log] [blame]
sunishvka1dfc3e2016-04-16 12:24:47 +05301/*
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.isis.io.isispacket.tlv;
17
18import com.google.common.base.MoreObjects;
19import com.google.common.primitives.Bytes;
20import org.jboss.netty.buffer.ChannelBuffer;
21import org.onosproject.isis.io.util.IsisUtil;
22
23import java.util.ArrayList;
24import java.util.List;
25
26/**
27 * Representation of metric of reachability.
28 */
29public class MetricsOfReachability {
30 private final String value1 = "10000000";
31 private final String value2 = "00000000";
32 private String neighborId;
33 private byte defaultMetric;
34 private byte delayMetric;
35 private byte expenseMetric;
36 private byte errorMetric;
37 private boolean delayMetricSupported;
38 private boolean expenseMetricSupported;
39 private boolean errorMetricSupported;
40 private boolean defaultIsInternal;
41 private boolean delayIsInternal;
42 private boolean expenseIsInternal;
43 private boolean errorIsInternal;
44
45 /**
46 * Returns delay metric is internal or not.
47 *
48 * @return true if internal else false
49 */
50 public boolean isDelayIsInternal() {
51 return delayIsInternal;
52 }
53
54 /**
55 * Sets delay metric is internal or not.
56 *
57 * @param delayIsInternal true if internal else false
58 */
59 public void setDelayIsInternal(boolean delayIsInternal) {
60 this.delayIsInternal = delayIsInternal;
61 }
62
63 /**
64 * Returns expense metric is internal or not.
65 *
66 * @return true if internal else false
67 */
68 public boolean isExpenseIsInternal() {
69 return expenseIsInternal;
70 }
71
72 /**
73 * Sets expense metric is internal or not.
74 *
75 * @param expenseIsInternal true if internal else false
76 */
77 public void setExpenseIsInternal(boolean expenseIsInternal) {
78 this.expenseIsInternal = expenseIsInternal;
79 }
80
81 /**
82 * Returns error metric is internal or not.
83 *
84 * @return true if internal else false
85 */
86 public boolean isErrorIsInternal() {
87 return errorIsInternal;
88 }
89
90 /**
91 * Sets error metric is internal or not.
92 *
93 * @param errorIsInternal true if internal else false
94 */
95 public void setErrorIsInternal(boolean errorIsInternal) {
96 this.errorIsInternal = errorIsInternal;
97 }
98
99 /**
100 * Returns default metric is internal or not.
101 *
102 * @return true if internal else false
103 */
104 public boolean isDefaultIsInternal() {
105 return defaultIsInternal;
106 }
107
108 /**
109 * Sets default metric is internal or not.
110 *
111 * @param defaultIsInternal true if internal else false
112 */
113 public void setDefaultIsInternal(boolean defaultIsInternal) {
114 this.defaultIsInternal = defaultIsInternal;
115 }
116
117 /**
118 * Returns delay metric is supported or not.
119 *
120 * @return true if supported else false
121 */
122 public boolean isDelayMetricSupported() {
123 return delayMetricSupported;
124 }
125
126 /**
127 * Sets delay metric is supported or not.
128 *
129 * @param delayMetricSupported true if supported else false
130 */
131 public void setDelayMetricSupported(boolean delayMetricSupported) {
132 this.delayMetricSupported = delayMetricSupported;
133 }
134
135 /**
136 * Returns expense metric is supported or not.
137 *
138 * @return true if supported else false
139 */
140 public boolean isExpenseMetricSupported() {
141 return expenseMetricSupported;
142 }
143
144 /**
145 * Sets expense metric is supported or not.
146 *
147 * @param expenseMetricSupported true if supported else false
148 */
149 public void setExpenseMetricSupported(boolean expenseMetricSupported) {
150 this.expenseMetricSupported = expenseMetricSupported;
151 }
152
153 /**
154 * Returns error metric is supported or not.
155 *
156 * @return true if supported else false
157 */
158 public boolean isErrorMetricSupported() {
159 return errorMetricSupported;
160 }
161
162 /**
163 * Sets error metric is supported or not.
164 *
165 * @param errorMetricSupported true if supported else false
166 */
167 public void setErrorMetricSupported(boolean errorMetricSupported) {
168 this.errorMetricSupported = errorMetricSupported;
169 }
170
171 /**
172 * Returns neighbor ID of metric of reachability.
173 *
174 * @return neighbor ID
175 */
176 public String neighborId() {
177 return neighborId;
178 }
179
180 /**
181 * Sets neighbor ID for metric of reachability.
182 *
183 * @param neighborId neighbor ID
184 */
185 public void setNeighborId(String neighborId) {
186 this.neighborId = neighborId;
187 }
188
189
190 /**
191 * Returns default metric of metric of reachability.
192 *
193 * @return default metric
194 */
195 public byte defaultMetric() {
196 return defaultMetric;
197 }
198
199 /**
200 * Sets default metric for of reachability.
201 *
202 * @param defaultMetric default metric
203 */
204 public void setDefaultMetric(byte defaultMetric) {
205 this.defaultMetric = defaultMetric;
206 }
207
208 /**
209 * Returns delay metric of metric of reachability.
210 *
211 * @return delay metric
212 */
213 public byte delayMetric() {
214 return delayMetric;
215 }
216
217 /**
218 * Sets delay metric for metric of reachability.
219 *
Dhruv Dhodye64b93e2016-04-20 19:26:55 +0530220 * @param delayMetric delay metric
sunishvka1dfc3e2016-04-16 12:24:47 +0530221 */
222 public void setDelayMetric(byte delayMetric) {
223 this.delayMetric = delayMetric;
224 }
225
226 /**
227 * Returns Expense metric of metric of reachability.
228 *
229 * @return Expense metric
230 */
231 public byte expenseMetric() {
232 return expenseMetric;
233 }
234
235 /**
236 * Sets Expense metric for metric of reachability.
237 *
Dhruv Dhodye64b93e2016-04-20 19:26:55 +0530238 * @param expenseMetric Expense metric
sunishvka1dfc3e2016-04-16 12:24:47 +0530239 */
240 public void setExpenseMetric(byte expenseMetric) {
241 this.expenseMetric = expenseMetric;
242 }
243
244 /**
245 * Returns Error metric of metric of reachability.
246 *
247 * @return Error metric
248 */
249 public byte errorMetric() {
250 return errorMetric;
251 }
252
253 /**
254 * Sets Error metric for metric of reachability.
255 *
Dhruv Dhodye64b93e2016-04-20 19:26:55 +0530256 * @param errorMetric Error metric
sunishvka1dfc3e2016-04-16 12:24:47 +0530257 */
258 public void setErrorMetric(byte errorMetric) {
259 this.errorMetric = errorMetric;
260 }
261
262 /**
263 * Sets the metric of reachability values for metric of reachability from byte buffer.
264 *
265 * @param channelBuffer channel Buffer instance
266 */
267 public void readFrom(ChannelBuffer channelBuffer) {
268 byte metric = channelBuffer.readByte();
269 String metricInBinary = Integer.toBinaryString(Byte.toUnsignedInt(metric));
270 metricInBinary = IsisUtil.toEightBitBinary(metricInBinary);
271 this.setDefaultMetric(metric);
272 this.setDelayMetric(metric);
273 this.setExpenseMetric(metric);
274 this.setErrorMetric(metric);
275 this.setDelayMetric(metric);
276 if (metricInBinary.charAt(0) == 0) {
277 this.setDefaultIsInternal(true);
278 } else {
279 this.setDefaultIsInternal(false);
280 }
281 if (metricInBinary.charAt(0) == 0) {
282 this.setDelayMetricSupported(true);
283 this.setExpenseMetricSupported(true);
284 this.setErrorMetricSupported(true);
285 } else {
286 this.setDelayMetricSupported(false);
287 this.setExpenseMetricSupported(false);
288 this.setErrorMetricSupported(false);
289 }
290 byte temp = channelBuffer.readByte();
291 String internalBit = Integer.toBinaryString(Byte.toUnsignedInt(temp));
292 internalBit = IsisUtil.toEightBitBinary(internalBit);
293 if (internalBit.charAt(1) == 0) {
294 this.setDelayIsInternal(true);
295 }
296 temp = channelBuffer.readByte();
297 internalBit = Integer.toBinaryString(Byte.toUnsignedInt(temp));
298 internalBit = IsisUtil.toEightBitBinary(internalBit);
299 if (internalBit.charAt(1) == 0) {
300 this.setExpenseIsInternal(true);
301 }
302 temp = channelBuffer.readByte();
303 internalBit = Integer.toBinaryString(Byte.toUnsignedInt(temp));
304 internalBit = IsisUtil.toEightBitBinary(internalBit);
305 if (internalBit.charAt(1) == 0) {
306 this.setErrorIsInternal(true);
307 }
308 byte[] tempByteArray = new byte[IsisUtil.ID_PLUS_ONE_BYTE];
309 channelBuffer.readBytes(tempByteArray, 0, IsisUtil.ID_PLUS_ONE_BYTE);
310 this.setNeighborId(IsisUtil.systemIdPlus(tempByteArray));
311 }
312
313 /**
314 * Returns metric of reachability values as bytes of metric of reachability.
315 *
316 * @return byteArray metric of reachability values as bytes of metric of reachability
317 */
318 public byte[] asBytes() {
319 List<Byte> bytes = new ArrayList<>();
320 bytes.add((byte) this.defaultMetric());
321 if (this.isDelayIsInternal()) {
322 bytes.add((byte) Integer.parseInt(value1));
323 } else {
324 bytes.add((byte) Integer.parseInt(value2));
325 }
326 if (this.isExpenseIsInternal()) {
327 bytes.add((byte) Integer.parseInt(value1));
328 } else {
329 bytes.add((byte) Integer.parseInt(value2));
330 }
331 if (this.isErrorIsInternal()) {
332 bytes.add((byte) Integer.parseInt(value1));
333 } else {
334 bytes.add((byte) Integer.parseInt(value2));
335 }
336 bytes.addAll(IsisUtil.sourceAndLanIdToBytes(this.neighborId()));
337 return Bytes.toArray(bytes);
338 }
339
340 @Override
341 public String toString() {
342 return MoreObjects.toStringHelper(getClass())
343 .omitNullValues()
344 .add("neighborId", neighborId)
345 .add("defaultMetric", defaultMetric)
346 .add("delayMetric", delayMetric)
347 .add("expenseMetric", expenseMetric)
348 .add("errorMetric", errorMetric)
349 .add("delayMetricSupported", delayMetricSupported)
350 .add("expenseMetricSupported", expenseMetricSupported)
351 .add("errorMetricSupported", errorMetricSupported)
352 .add("defaultIsInternal", defaultIsInternal)
353 .add("delayIsInternal", delayIsInternal)
354 .add("expenseIsInternal", expenseIsInternal)
355 .add("errorIsInternal", errorIsInternal)
356 .toString();
357 }
358}