blob: 179b3e6a67415193a5a932aba465b95a5fff80fa [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.onlab.packet.Ip4Address;
22import org.onosproject.isis.io.util.IsisUtil;
23
24import java.util.ArrayList;
25import java.util.List;
26
27/**
28 * Representation of metric of internal reachability.
29 */
30public class MetricOfInternalReachability {
31 private Ip4Address ipAddress;
32 private Ip4Address subnetAddres;
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 defaultDistributionDown;
42 private boolean delayIsInternal;
43 private boolean expenseIsInternal;
44 private boolean errorIsInternal;
45
46 /**
47 * Returns the IP address of metric of internal reachability.
48 *
49 * @return ipAddress IP address of metric of internal reachability
50 */
51 public Ip4Address getIpAddress() {
52 return ipAddress;
53 }
54
55 /**
56 * Sets the IP address for metric of internal reachability.
57 *
58 * @param ipAddress ip address
59 */
60 public void setIpAddress(Ip4Address ipAddress) {
61 this.ipAddress = ipAddress;
62 }
63
64 /**
65 * Returns the subnet address of metric of internal reachability.
66 *
67 * @return subnetAddres subnet address of metric of internal reachability
68 */
69 public Ip4Address getSubnetAddres() {
70 return subnetAddres;
71 }
72
73 /**
74 * Sets the subnet address for metric of internal reachability.
75 *
76 * @param subnetAddres subnet address
77 */
78 public void setSubnetAddres(Ip4Address subnetAddres) {
79 this.subnetAddres = subnetAddres;
80 }
81
82 /**
83 * Returns error metric is internal or not.
84 *
85 * @return true if internal else false
86 */
87 public boolean isErrorIsInternal() {
88 return errorIsInternal;
89 }
90
91 /**
92 * Sets error metric is internal or not.
93 *
94 * @param errorIsInternal true if internal else false
95 */
96 public void setErrorIsInternal(boolean errorIsInternal) {
97 this.errorIsInternal = errorIsInternal;
98 }
99
100 /**
101 * Returns expense metric is internal or not.
102 *
103 * @return true if internal else false
104 */
105 public boolean isExpenseIsInternal() {
106 return expenseIsInternal;
107 }
108
109 /**
110 * Sets expense metric is internal or not.
111 *
112 * @param expenseIsInternal true if internal else false
113 */
114 public void setExpenseIsInternal(boolean expenseIsInternal) {
115 this.expenseIsInternal = expenseIsInternal;
116 }
117
118 /**
119 * Returns delays metric is internal or not.
120 *
121 * @return true if internal else false
122 */
123 public boolean isDelayIsInternal() {
124 return delayIsInternal;
125 }
126
127 /**
128 * Sets delay metric is internal or not.
129 *
130 * @param delayIsInternal true if internal else false
131 */
132 public void setDelayIsInternal(boolean delayIsInternal) {
133 this.delayIsInternal = delayIsInternal;
134 }
135
136 /**
137 * Returns is default distribution is up or down.
138 *
139 * @return true if down else false
140 */
141 public boolean isDefaultDistributionDown() {
142 return defaultDistributionDown;
143 }
144
145 /**
146 * Sets default distribution is up or down.
147 *
148 * @param defaultDistributionDown true if down else false
149 */
150 public void setDefaultDistributionDown(boolean defaultDistributionDown) {
151 this.defaultDistributionDown = defaultDistributionDown;
152 }
153
154 /**
155 * Returns is default metric is internal or not.
156 *
157 * @return true if internal else false
158 */
159 public boolean isDefaultIsInternal() {
160 return defaultIsInternal;
161 }
162
163 /**
164 * Sets default metric is internal or not.
165 *
166 * @param defaultIsInternal true is internal else false
167 */
168 public void setDefaultIsInternal(boolean defaultIsInternal) {
169 this.defaultIsInternal = defaultIsInternal;
170 }
171
172 /**
173 * Returns error metric is supported or not.
174 *
175 * @return true if supported else false
176 */
177 public boolean isErrorMetricSupported() {
178 return errorMetricSupported;
179 }
180
181 /**
182 * Sets error metric is supported or not.
183 *
184 * @param errorMetricSupported true if supported else false
185 */
186 public void setErrorMetricSupported(boolean errorMetricSupported) {
187 this.errorMetricSupported = errorMetricSupported;
188 }
189
190 /**
191 * Returns expense metric is supported or not.
192 *
193 * @return true if supported else false
194 */
195 public boolean isExpenseMetricSupported() {
196 return expenseMetricSupported;
197 }
198
199 /**
200 * Sets expense metric is supported or not.
201 *
202 * @param expenseMetricSupported true if supported else false
203 */
204 public void setExpenseMetricSupported(boolean expenseMetricSupported) {
205 this.expenseMetricSupported = expenseMetricSupported;
206 }
207
208 /**
209 * Returns delay metric is supported or not.
210 *
211 * @return true if supported else false
212 */
213 public boolean isDelayMetricSupported() {
214 return delayMetricSupported;
215 }
216
217 /**
218 * Sets delay metric is supported or not.
219 *
220 * @param delayMetricSupported true if supported else false
221 */
222 public void setDelayMetricSupported(boolean delayMetricSupported) {
223 this.delayMetricSupported = delayMetricSupported;
224 }
225
226 /**
227 * Returns error metric of metric of internal reachability.
228 *
Dhruv Dhodye64b93e2016-04-20 19:26:55 +0530229 * @return errorMetric error metric
sunishvka1dfc3e2016-04-16 12:24:47 +0530230 */
231 public byte errorMetric() {
232 return errorMetric;
233 }
234
235 /**
236 * Sets error metric for metric of internal reachability.
237 *
238 * @param errorMetric error metric
239 */
240 public void setErrorMetric(byte errorMetric) {
241 this.errorMetric = errorMetric;
242 }
243
244 /**
245 * Returns expense metric of metric of internal reachability.
246 *
247 * @return expense metric
248 */
249 public byte expenseMetric() {
250 return expenseMetric;
251 }
252
253 /**
254 * Sets expense metric for metric of internal reachability.
255 *
256 * @param expenseMetric expense metric
257 */
258 public void setExpenseMetric(byte expenseMetric) {
259 this.expenseMetric = expenseMetric;
260 }
261
262 /**
263 * Returns delay metric of metric of internal reachability.
264 *
265 * @return delay metric
266 */
267 public byte delayMetric() {
268 return delayMetric;
269 }
270
271 /**
272 * Sets delay metric for metric of internal reachability.
273 *
274 * @param delayMetric delay metric
275 */
276 public void setDelayMetric(byte delayMetric) {
277 this.delayMetric = delayMetric;
278 }
279
280 /**
281 * Returns default metric of metric of internal reachability.
282 *
283 * @return default metric
284 */
285 public byte defaultMetric() {
286 return defaultMetric;
287 }
288
289 /**
290 * Sets default metric for metric of internal reachability.
291 *
292 * @param defaultMetric default metric
293 */
294 public void setDefaultMetric(byte defaultMetric) {
295 this.defaultMetric = defaultMetric;
296 }
297
298 /**
299 * Sets the metric of internal reachability
300 * values for metric of internal reachability from byte buffer.
301 *
302 * @param channelBuffer channel Buffer instance
303 */
304 public void readFrom(ChannelBuffer channelBuffer) {
305 byte metric = channelBuffer.readByte();
306 this.setDefaultMetric(metric);
307 String metricInBinary = Integer.toBinaryString(Byte.toUnsignedInt(metric));
308 metricInBinary = IsisUtil.toEightBitBinary(metricInBinary);
309 if (metricInBinary.charAt(1) == 0) {
310 this.setDefaultIsInternal(true);
311 } else {
312 this.setDefaultIsInternal(false);
313 }
314 if (metricInBinary.charAt(0) == 0) {
315 this.setDefaultDistributionDown(true);
316 } else {
317 this.setDefaultDistributionDown(false);
318 }
319 byte delayMetric = channelBuffer.readByte();
320 metricInBinary = Integer.toBinaryString(Byte.toUnsignedInt(delayMetric));
321 metricInBinary = IsisUtil.toEightBitBinary(metricInBinary);
322 this.setDelayMetric(Byte.parseByte(metricInBinary.substring(5, metricInBinary.length()), 2));
323 if (metricInBinary.charAt(1) == 0) {
324 this.setDelayIsInternal(true);
325 } else {
326 this.setDelayIsInternal(false);
327 }
328 if (metricInBinary.charAt(0) == 0) {
329 this.setDelayMetricSupported(true);
330 } else {
331 this.setDelayMetricSupported(false);
332 }
333 byte expenseMetric = channelBuffer.readByte();
334 metricInBinary = Integer.toBinaryString(Byte.toUnsignedInt(expenseMetric));
335 metricInBinary = IsisUtil.toEightBitBinary(metricInBinary);
336 this.setExpenseMetric(Byte.parseByte(metricInBinary.substring(5, metricInBinary.length()), 2));
337 if (metricInBinary.charAt(1) == 0) {
338 this.setExpenseIsInternal(true);
339 } else {
340 this.setExpenseIsInternal(false);
341 }
342 if (metricInBinary.charAt(0) == 0) {
343 this.setExpenseMetricSupported(true);
344 } else {
345 this.setExpenseMetricSupported(false);
346 }
347 byte errorMetric = channelBuffer.readByte();
348 metricInBinary = Integer.toBinaryString(Byte.toUnsignedInt(errorMetric));
349 metricInBinary = IsisUtil.toEightBitBinary(metricInBinary);
350 this.setErrorMetric(Byte.parseByte(metricInBinary.substring(5, metricInBinary.length()), 2));
351 if (metricInBinary.charAt(1) == 0) {
352 this.setErrorIsInternal(true);
353 } else {
354 this.setErrorIsInternal(false);
355 }
356 if (metricInBinary.charAt(0) == 0) {
357 this.setErrorMetricSupported(true);
358 } else {
359 this.setErrorMetricSupported(false);
360 }
361 List<Byte> byteList = new ArrayList<>();
362 while (channelBuffer.readableBytes() > 0) {
363 byteList.add(channelBuffer.readByte());
364 }
365 byte[] tempByteArray = new byte[IsisUtil.FOUR_BYTES];
366 channelBuffer.readBytes(tempByteArray, 0, IsisUtil.FOUR_BYTES);
367 this.setIpAddress(Ip4Address.valueOf(tempByteArray));
368
369 tempByteArray = new byte[IsisUtil.FOUR_BYTES];
370 channelBuffer.readBytes(tempByteArray, 0, IsisUtil.FOUR_BYTES);
371 this.setSubnetAddres(Ip4Address.valueOf(tempByteArray));
372 }
373
374 /**
375 * Returns metric of internal reachability values as bytes of metric of internal reachability.
376 *
377 * @return byteArray metric of internal reachability values as bytes of metric of internal reachability
378 */
379 public byte[] asBytes() {
380 List<Byte> bytes = new ArrayList<>();
381 bytes.add(this.defaultMetric());
382 int temp = this.delayMetric();
383 String hsbBits = "";
384 if (this.isDelayMetricSupported()) {
385 hsbBits = "0" + hsbBits;
386 } else {
387 hsbBits = "1" + hsbBits;
388 }
389 if (this.isDelayIsInternal()) {
390 hsbBits = hsbBits + "0";
391 } else {
392 hsbBits = hsbBits + "1";
393 }
394 hsbBits = hsbBits + "00";
395 String binary = hsbBits + IsisUtil.toFourBitBinary(Integer.toBinaryString(temp));
396 bytes.add((byte) Integer.parseInt(binary, 2));
397
398 temp = this.expenseMetric();
399 hsbBits = "";
400 if (this.isExpenseMetricSupported()) {
401 hsbBits = "0" + hsbBits;
402 } else {
403 hsbBits = "1" + hsbBits;
404 }
405 if (this.isExpenseIsInternal()) {
406 hsbBits = hsbBits + "0";
407 } else {
408 hsbBits = hsbBits + "1";
409 }
410 hsbBits = hsbBits + "00";
411 binary = hsbBits + IsisUtil.toFourBitBinary(Integer.toBinaryString(temp));
412 bytes.add((byte) Integer.parseInt(binary, 2));
413
414 temp = this.errorMetric();
415 hsbBits = "";
416 if (this.isErrorMetricSupported()) {
417 hsbBits = "0" + hsbBits;
418 } else {
419 hsbBits = "1" + hsbBits;
420 }
421 if (this.isExpenseIsInternal()) {
422 hsbBits = hsbBits + "0";
423 } else {
424 hsbBits = hsbBits + "1";
425 }
426 hsbBits = hsbBits + "00";
427 binary = hsbBits + IsisUtil.toFourBitBinary(Integer.toBinaryString(temp));
428 bytes.add((byte) Integer.parseInt(binary, 2));
429
430 bytes.addAll(Bytes.asList(this.getIpAddress().toOctets()));
431 bytes.addAll(Bytes.asList(this.getSubnetAddres().toOctets()));
432 return Bytes.toArray(bytes);
433 }
434
435 @Override
436 public String toString() {
437 return MoreObjects.toStringHelper(getClass())
438 .omitNullValues()
439 .add("ipAddress", ipAddress)
440 .add("subnetAddres", subnetAddres)
441 .add("defaultMetric", defaultMetric)
442 .add("delayMetric", delayMetric)
443 .add("expenseMetric", expenseMetric)
444 .add("errorMetric", errorMetric)
445 .add("delayMetricSupported", delayMetricSupported)
446 .add("expenseMetricSupported", expenseMetricSupported)
447 .add("errorMetricSupported", errorMetricSupported)
448 .add("defaultIsInternal", defaultIsInternal)
449 .add("defaultDistributionDown", defaultDistributionDown)
450 .add("delayIsInternal", delayIsInternal)
451 .add("expenseIsInternal", expenseIsInternal)
452 .add("errorIsInternal", errorIsInternal)
453 .toString();
454 }
455}