blob: b248decdda12c10e85d359dc4dbd7a799fa2c698 [file] [log] [blame]
PRASHANTH SHIVANANJAPPA491b8af2016-04-27 19:23:24 +05301/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2016-present Open Networking Foundation
PRASHANTH SHIVANANJAPPA491b8af2016-04-27 19:23:24 +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.onosproject.isis.io.isispacket.tlv.subtlv.SubTlvFinder;
22import org.onosproject.isis.io.isispacket.tlv.subtlv.SubTlvToBytes;
23import org.onosproject.isis.io.isispacket.tlv.subtlv.SubTlvType;
24import org.onosproject.isis.io.isispacket.tlv.subtlv.TrafficEngineeringSubTlv;
25import org.onosproject.isis.io.util.IsisUtil;
26
27import java.util.ArrayList;
28import java.util.List;
29
30/**
31 * Representation of IS extended reachability neighbors.
32 */
33public class NeighborForExtendedIs {
34
35 private String neighborId;
36 private int metric;
37 private List<TrafficEngineeringSubTlv> teSubTlv = new ArrayList<>();
38
39 /**
40 * Returns neighbor ID.
41 *
42 * @return neighbor ID
43 */
44 public String neighborId() {
45 return neighborId;
46 }
47
48 /**
sunish vk7bdf4d42016-06-24 12:29:43 +053049 * Returns list of sub tlvs.
50 *
51 * @return teSubTlv list of sub tlvs
52 */
53 public List<TrafficEngineeringSubTlv> teSubTlv() {
54 return teSubTlv;
55 }
56
57 /**
PRASHANTH SHIVANANJAPPA491b8af2016-04-27 19:23:24 +053058 * Sets neighbor ID.
59 *
60 * @param neighborId neighbor ID
61 */
62 public void setNeighborId(String neighborId) {
63 this.neighborId = neighborId;
64 }
65
66 /**
67 * Returns metric.
68 *
69 * @return metric
70 */
71 public int metric() {
72 return metric;
73 }
74
75 /**
76 * Sets metric.
77 *
78 * @param metric metric
79 */
80 public void setMetric(int metric) {
81 this.metric = metric;
82 }
83
84 /**
85 * Adds the TE for extended IS instance to IS extended reachability TLV.
86 *
87 * @param trafficEngineeringSubTlv TE for extended IS instance
88 */
89 public void addSubTlv(TrafficEngineeringSubTlv trafficEngineeringSubTlv) {
90 this.teSubTlv.add(trafficEngineeringSubTlv);
91 }
92
93 /**
94 * Reads from the channel buffer and populate this instance.
95 *
96 * @param channelBuffer channel buffer instance
97 */
98 public void readFrom(ChannelBuffer channelBuffer) {
99 byte[] tempByteArray = new byte[IsisUtil.ID_PLUS_ONE_BYTE];
100 channelBuffer.readBytes(tempByteArray, 0, IsisUtil.ID_PLUS_ONE_BYTE);
101 this.setNeighborId(IsisUtil.systemIdPlus(tempByteArray));
102 this.setMetric(channelBuffer.readUnsignedMedium());
103 int nTlvPresent = channelBuffer.readByte();
104 if (nTlvPresent > 0) {
Chinmaya Agarwalc1184d72019-10-09 16:53:22 +0530105 /* ntlvPresent=0 infers that all Sub TLVs,
106 for a particular neighbor,
107 have been parsed and the loop has to be terminated
108 */
109 while (channelBuffer.readableBytes() > IsisUtil.TWO_BYTES && nTlvPresent > 0) {
PRASHANTH SHIVANANJAPPA491b8af2016-04-27 19:23:24 +0530110 TlvHeader tlvHeader = new TlvHeader();
111 tlvHeader.setTlvType(channelBuffer.readByte());
112 tlvHeader.setTlvLength(channelBuffer.readByte());
113 SubTlvType tlvValue = SubTlvType.get(tlvHeader.tlvType());
114 int tlvLength = tlvHeader.tlvLength();
115 if (tlvValue != null) {
116 if (channelBuffer.readableBytes() >= tlvLength) {
sunish vkc3824e82016-05-11 19:38:24 +0530117 TrafficEngineeringSubTlv subTlv =
118 SubTlvFinder.findSubTlv(tlvHeader, channelBuffer.readBytes(tlvHeader.tlvLength()));
119 if (subTlv != null) {
120 this.addSubTlv(subTlv);
121 }
Chinmaya Agarwalc1184d72019-10-09 16:53:22 +0530122 /*As one Sub TLV is parsed, its length is
123 subtracted from total length to be read
124 */
125 nTlvPresent = nTlvPresent - (tlvLength + IsisUtil.TWO_BYTES);
PRASHANTH SHIVANANJAPPA491b8af2016-04-27 19:23:24 +0530126 }
127 } else {
128 if (channelBuffer.readableBytes() >= tlvLength) {
129 channelBuffer.readBytes(tlvLength);
Chinmaya Agarwalc1184d72019-10-09 16:53:22 +0530130 /*As one Sub TLV is parsed, its length is
131 subtracted from total length to be read
132 */
133 nTlvPresent = nTlvPresent - (tlvLength + IsisUtil.TWO_BYTES);
PRASHANTH SHIVANANJAPPA491b8af2016-04-27 19:23:24 +0530134 }
135 }
136 }
137 }
138 }
139
140 /**
141 * Returns neighbor details of IS extended reachability TLV.
142 *
143 * @return neighbor details of IS extended reachability TLV.
144 */
145 public byte[] neighborBodyAsbytes() {
146 List<Byte> byteList = new ArrayList<>();
147 byteList.addAll(IsisUtil.sourceAndLanIdToBytes(this.neighborId()));
148 byteList.addAll(Bytes.asList(IsisUtil.convertToThreeBytes(this.metric())));
Jon Hallcbd1b392017-01-18 20:15:44 -0800149 if (!this.teSubTlv.isEmpty()) {
PRASHANTH SHIVANANJAPPA491b8af2016-04-27 19:23:24 +0530150 for (TrafficEngineeringSubTlv trafficEngineeringSubTlv : this.teSubTlv) {
151 byteList.addAll(SubTlvToBytes.tlvToBytes(trafficEngineeringSubTlv));
152 }
153 }
154 return Bytes.toArray(byteList);
155 }
156
157 @Override
158 public String toString() {
159 return MoreObjects.toStringHelper(getClass())
160 .omitNullValues()
161 .add("neighborId", neighborId)
162 .add("metric", metric)
163 .add("teSubTlv", teSubTlv)
164 .toString();
165 }
Chinmaya Agarwalc1184d72019-10-09 16:53:22 +0530166}