blob: ffc75e1c0cdf0e1bf528a7fe9b522d354c601ea2 [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;
21
22import java.util.ArrayList;
23import java.util.List;
24
25/**
26 * Representation of IS reachability TLV.
27 */
28public class IsReachabilityTlv extends TlvHeader {
29
30 private int reserved;
31 private List<MetricsOfReachability> metricsOfReachabilities = new ArrayList<>();
32
33 /**
34 * Creates an instance of IS reachability TLV.
35 *
Dhruv Dhodye64b93e2016-04-20 19:26:55 +053036 * @param tlvHeader TLV header
sunishvka1dfc3e2016-04-16 12:24:47 +053037 */
38 public IsReachabilityTlv(TlvHeader tlvHeader) {
39 this.setTlvType(tlvHeader.tlvType());
40 this.setTlvLength(tlvHeader.tlvLength());
41 }
42
43 /**
44 * Returns the reserved value of IS reachability TLV.
45 *
46 * @return reserved
47 */
48 public int reserved() {
49 return reserved;
50 }
51
52 /**
53 * Sets the reserved value for IS reachability TLV.
54 *
55 * @param reserved reserved
56 */
57 public void setReserved(int reserved) {
58 this.reserved = reserved;
59 }
60
61 /**
62 * Adds the metric of reachability to IS reachability TLV..
63 *
64 * @param metricsOfReachability metric of reachability
65 */
66 public void addMeticsOfReachability(MetricsOfReachability metricsOfReachability) {
67 this.metricsOfReachabilities.add(metricsOfReachability);
68 }
69
70 @Override
71 public void readFrom(ChannelBuffer channelBuffer) {
72 this.setReserved(channelBuffer.readByte());
73 while (channelBuffer.readableBytes() > 0) {
74 MetricsOfReachability metricsOfReachability = new MetricsOfReachability();
75 metricsOfReachability.readFrom(channelBuffer);
76 this.metricsOfReachabilities.add(metricsOfReachability);
77 }
78 }
79
80 @Override
81 public byte[] asBytes() {
82 byte[] bytes = null;
83 byte[] tlvHeader = tlvHeaderAsByteArray();
84 byte[] tlvBody = tlvBodyAsBytes();
85 tlvHeader[1] = (byte) tlvBody.length;
86 bytes = Bytes.concat(tlvHeader, tlvBody);
87 return bytes;
88 }
89
90 /**
91 * Returns TLV body of IS reachability TLV.
92 *
93 * @return byteArray TLV body of area address TLV
94 */
95 private byte[] tlvBodyAsBytes() {
96 List<Byte> bytes = new ArrayList<>();
97 bytes.add((byte) this.reserved());
98 for (MetricsOfReachability metricsOfReachability : this.metricsOfReachabilities) {
99 bytes.addAll(Bytes.asList(metricsOfReachability.asBytes()));
100 }
101 return Bytes.toArray(bytes);
102 }
103
104 @Override
105 public String toString() {
106 return MoreObjects.toStringHelper(getClass())
107 .omitNullValues()
108 .add("metricsOfReachabilities", metricsOfReachabilities)
109 .toString();
110 }
111}