blob: eb8f6d279e1233526122fac721ec5c4b973a7387 [file] [log] [blame]
sunishvka1dfc3e2016-04-16 12:24:47 +05301/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2016-present Open Networking Foundation
sunishvka1dfc3e2016-04-16 12:24:47 +05303 *
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;
Ray Milkeyc108a6b2017-08-23 15:23:50 -070032 private Ip4Address subnetAddress;
sunishvka1dfc3e2016-04-16 12:24:47 +053033 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 *
Ray Milkeyc108a6b2017-08-23 15:23:50 -070067 * @return subnetAddress subnet address of metric of internal reachability
sunishvka1dfc3e2016-04-16 12:24:47 +053068 */
Ray Milkeyc108a6b2017-08-23 15:23:50 -070069 public Ip4Address getSubnetAddress() {
70 return subnetAddress;
sunishvka1dfc3e2016-04-16 12:24:47 +053071 }
72
73 /**
74 * Sets the subnet address for metric of internal reachability.
75 *
Ray Milkeyc108a6b2017-08-23 15:23:50 -070076 * @param subnetAddress subnet address
sunishvka1dfc3e2016-04-16 12:24:47 +053077 */
Ray Milkeyc108a6b2017-08-23 15:23:50 -070078 public void setSubnetAddress(Ip4Address subnetAddress) {
79 this.subnetAddress = subnetAddress;
sunishvka1dfc3e2016-04-16 12:24:47 +053080 }
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 }
tejeshwer degala3fe1ed52016-04-22 17:04:01 +0530361
sunishvka1dfc3e2016-04-16 12:24:47 +0530362 byte[] tempByteArray = new byte[IsisUtil.FOUR_BYTES];
363 channelBuffer.readBytes(tempByteArray, 0, IsisUtil.FOUR_BYTES);
364 this.setIpAddress(Ip4Address.valueOf(tempByteArray));
365
366 tempByteArray = new byte[IsisUtil.FOUR_BYTES];
367 channelBuffer.readBytes(tempByteArray, 0, IsisUtil.FOUR_BYTES);
Ray Milkeyc108a6b2017-08-23 15:23:50 -0700368 this.setSubnetAddress(Ip4Address.valueOf(tempByteArray));
sunishvka1dfc3e2016-04-16 12:24:47 +0530369 }
370
371 /**
372 * Returns metric of internal reachability values as bytes of metric of internal reachability.
373 *
374 * @return byteArray metric of internal reachability values as bytes of metric of internal reachability
375 */
376 public byte[] asBytes() {
377 List<Byte> bytes = new ArrayList<>();
378 bytes.add(this.defaultMetric());
379 int temp = this.delayMetric();
380 String hsbBits = "";
381 if (this.isDelayMetricSupported()) {
382 hsbBits = "0" + hsbBits;
383 } else {
384 hsbBits = "1" + hsbBits;
385 }
386 if (this.isDelayIsInternal()) {
387 hsbBits = hsbBits + "0";
388 } else {
389 hsbBits = hsbBits + "1";
390 }
391 hsbBits = hsbBits + "00";
392 String binary = hsbBits + IsisUtil.toFourBitBinary(Integer.toBinaryString(temp));
393 bytes.add((byte) Integer.parseInt(binary, 2));
394
395 temp = this.expenseMetric();
396 hsbBits = "";
397 if (this.isExpenseMetricSupported()) {
398 hsbBits = "0" + hsbBits;
399 } else {
400 hsbBits = "1" + hsbBits;
401 }
402 if (this.isExpenseIsInternal()) {
403 hsbBits = hsbBits + "0";
404 } else {
405 hsbBits = hsbBits + "1";
406 }
407 hsbBits = hsbBits + "00";
408 binary = hsbBits + IsisUtil.toFourBitBinary(Integer.toBinaryString(temp));
409 bytes.add((byte) Integer.parseInt(binary, 2));
410
411 temp = this.errorMetric();
412 hsbBits = "";
413 if (this.isErrorMetricSupported()) {
414 hsbBits = "0" + hsbBits;
415 } else {
416 hsbBits = "1" + hsbBits;
417 }
418 if (this.isExpenseIsInternal()) {
419 hsbBits = hsbBits + "0";
420 } else {
421 hsbBits = hsbBits + "1";
422 }
423 hsbBits = hsbBits + "00";
424 binary = hsbBits + IsisUtil.toFourBitBinary(Integer.toBinaryString(temp));
425 bytes.add((byte) Integer.parseInt(binary, 2));
426
427 bytes.addAll(Bytes.asList(this.getIpAddress().toOctets()));
Ray Milkeyc108a6b2017-08-23 15:23:50 -0700428 bytes.addAll(Bytes.asList(this.getSubnetAddress().toOctets()));
sunishvka1dfc3e2016-04-16 12:24:47 +0530429 return Bytes.toArray(bytes);
430 }
431
432 @Override
433 public String toString() {
434 return MoreObjects.toStringHelper(getClass())
435 .omitNullValues()
436 .add("ipAddress", ipAddress)
Ray Milkeyc108a6b2017-08-23 15:23:50 -0700437 .add("subnetAddress", subnetAddress)
sunishvka1dfc3e2016-04-16 12:24:47 +0530438 .add("defaultMetric", defaultMetric)
439 .add("delayMetric", delayMetric)
440 .add("expenseMetric", expenseMetric)
441 .add("errorMetric", errorMetric)
442 .add("delayMetricSupported", delayMetricSupported)
443 .add("expenseMetricSupported", expenseMetricSupported)
444 .add("errorMetricSupported", errorMetricSupported)
445 .add("defaultIsInternal", defaultIsInternal)
446 .add("defaultDistributionDown", defaultDistributionDown)
447 .add("delayIsInternal", delayIsInternal)
448 .add("expenseIsInternal", expenseIsInternal)
449 .add("errorIsInternal", errorIsInternal)
450 .toString();
451 }
Ray Milkeyc108a6b2017-08-23 15:23:50 -0700452}