blob: 0bc57b46e10cb2eba0b66951f6b5abfc857aca11 [file] [log] [blame]
PRASHANTH SHIVANANJAPPA491b8af2016-04-27 19:23:24 +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.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 /**
49 * Sets neighbor ID.
50 *
51 * @param neighborId neighbor ID
52 */
53 public void setNeighborId(String neighborId) {
54 this.neighborId = neighborId;
55 }
56
57 /**
58 * Returns metric.
59 *
60 * @return metric
61 */
62 public int metric() {
63 return metric;
64 }
65
66 /**
67 * Sets metric.
68 *
69 * @param metric metric
70 */
71 public void setMetric(int metric) {
72 this.metric = metric;
73 }
74
75 /**
76 * Adds the TE for extended IS instance to IS extended reachability TLV.
77 *
78 * @param trafficEngineeringSubTlv TE for extended IS instance
79 */
80 public void addSubTlv(TrafficEngineeringSubTlv trafficEngineeringSubTlv) {
81 this.teSubTlv.add(trafficEngineeringSubTlv);
82 }
83
84 /**
85 * Reads from the channel buffer and populate this instance.
86 *
87 * @param channelBuffer channel buffer instance
88 */
89 public void readFrom(ChannelBuffer channelBuffer) {
90 byte[] tempByteArray = new byte[IsisUtil.ID_PLUS_ONE_BYTE];
91 channelBuffer.readBytes(tempByteArray, 0, IsisUtil.ID_PLUS_ONE_BYTE);
92 this.setNeighborId(IsisUtil.systemIdPlus(tempByteArray));
93 this.setMetric(channelBuffer.readUnsignedMedium());
94 int nTlvPresent = channelBuffer.readByte();
95 if (nTlvPresent > 0) {
96 while (channelBuffer.readableBytes() > IsisUtil.TWO_BYTES) {
97 TlvHeader tlvHeader = new TlvHeader();
98 tlvHeader.setTlvType(channelBuffer.readByte());
99 tlvHeader.setTlvLength(channelBuffer.readByte());
100 SubTlvType tlvValue = SubTlvType.get(tlvHeader.tlvType());
101 int tlvLength = tlvHeader.tlvLength();
102 if (tlvValue != null) {
103 if (channelBuffer.readableBytes() >= tlvLength) {
sunish vkc3824e82016-05-11 19:38:24 +0530104 TrafficEngineeringSubTlv subTlv =
105 SubTlvFinder.findSubTlv(tlvHeader, channelBuffer.readBytes(tlvHeader.tlvLength()));
106 if (subTlv != null) {
107 this.addSubTlv(subTlv);
108 }
PRASHANTH SHIVANANJAPPA491b8af2016-04-27 19:23:24 +0530109 }
110 } else {
111 if (channelBuffer.readableBytes() >= tlvLength) {
112 channelBuffer.readBytes(tlvLength);
113 }
114 }
115 }
116 }
117 }
118
119 /**
120 * Returns neighbor details of IS extended reachability TLV.
121 *
122 * @return neighbor details of IS extended reachability TLV.
123 */
124 public byte[] neighborBodyAsbytes() {
125 List<Byte> byteList = new ArrayList<>();
126 byteList.addAll(IsisUtil.sourceAndLanIdToBytes(this.neighborId()));
127 byteList.addAll(Bytes.asList(IsisUtil.convertToThreeBytes(this.metric())));
128 if (this.teSubTlv.size() > 0) {
129 for (TrafficEngineeringSubTlv trafficEngineeringSubTlv : this.teSubTlv) {
130 byteList.addAll(SubTlvToBytes.tlvToBytes(trafficEngineeringSubTlv));
131 }
132 }
133 return Bytes.toArray(byteList);
134 }
135
136 @Override
137 public String toString() {
138 return MoreObjects.toStringHelper(getClass())
139 .omitNullValues()
140 .add("neighborId", neighborId)
141 .add("metric", metric)
142 .add("teSubTlv", teSubTlv)
143 .toString();
144 }
145}