blob: 649b2dc0e888e22601aa394fd28d7d4323f6a53b [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 */
16
17package org.onosproject.isis.io.isispacket.tlv;
18
19import com.google.common.base.MoreObjects;
20import com.google.common.primitives.Bytes;
21import org.jboss.netty.buffer.ChannelBuffer;
22import org.onosproject.isis.io.isispacket.tlv.subtlv.SubTlvFinder;
23import org.onosproject.isis.io.isispacket.tlv.subtlv.SubTlvToBytes;
Dhruv Dhodye64b93e2016-04-20 19:26:55 +053024import org.onosproject.isis.io.isispacket.tlv.subtlv.SubTlvType;
sunishvka1dfc3e2016-04-16 12:24:47 +053025import org.onosproject.isis.io.isispacket.tlv.subtlv.TrafficEngineeringSubTlv;
26import org.onosproject.isis.io.util.IsisUtil;
27
28import java.util.ArrayList;
29import java.util.List;
30
31/**
32 * Representation of IP extended reachability TLV.
33 */
34public class IpExtendedReachabilityTlv extends TlvHeader implements IsisTlv {
35
Dhruv Dhodye64b93e2016-04-20 19:26:55 +053036 private boolean down;
37 private boolean subTlvPresence;
38 private int prefixLength;
39 private int metric;
sunishvka1dfc3e2016-04-16 12:24:47 +053040 private byte subTlvLength;
Dhruv Dhodye64b93e2016-04-20 19:26:55 +053041 private String prefix;
sunishvka1dfc3e2016-04-16 12:24:47 +053042 private List<TrafficEngineeringSubTlv> trafEnginSubTlv = new ArrayList<>();
43
44 /**
45 * Creates an instance of IP external reachability TLV.
46 *
Dhruv Dhodye64b93e2016-04-20 19:26:55 +053047 * @param tlvHeader TLV header
sunishvka1dfc3e2016-04-16 12:24:47 +053048 */
49 public IpExtendedReachabilityTlv(TlvHeader tlvHeader) {
50 this.setTlvType(tlvHeader.tlvType());
51 this.setTlvLength(tlvHeader.tlvLength());
52 }
53
54 /**
sunish vk7bdf4d42016-06-24 12:29:43 +053055 * Returns list of traffic engineering sub tlvs.
56 *
57 * @return trafEnginSubTlv
58 */
59 public List<TrafficEngineeringSubTlv> teTlvs() {
60 return this.trafEnginSubTlv;
61 }
62
63 /**
Dhruv Dhodye64b93e2016-04-20 19:26:55 +053064 * Returns the prefix of IP external reachability TLV.
sunishvka1dfc3e2016-04-16 12:24:47 +053065 *
Dhruv Dhodye64b93e2016-04-20 19:26:55 +053066 * @return prefix
sunishvka1dfc3e2016-04-16 12:24:47 +053067 */
Dhruv Dhodye64b93e2016-04-20 19:26:55 +053068 public String prefix() {
69 return prefix;
sunishvka1dfc3e2016-04-16 12:24:47 +053070 }
71
72 /**
Dhruv Dhodye64b93e2016-04-20 19:26:55 +053073 * Sets the prefix of IP external reachability TLV.
sunishvka1dfc3e2016-04-16 12:24:47 +053074 *
Dhruv Dhodye64b93e2016-04-20 19:26:55 +053075 * @param prefix prefix
sunishvka1dfc3e2016-04-16 12:24:47 +053076 */
Dhruv Dhodye64b93e2016-04-20 19:26:55 +053077 public void setPrefix(String prefix) {
78 this.prefix = prefix;
79 }
80
81 /**
82 * Returns if down true else false of IP external reachability TLV.
83 *
84 * @return if down true else false
85 */
86 public boolean isDown() {
87 return down;
88 }
89
90 /**
91 * Sets if down true else false of IP external reachability TLV.
92 *
93 * @param upOrDown if down true else false
94 */
95 public void setDown(boolean upOrDown) {
96 this.down = upOrDown;
97 }
98
99 /**
100 * Returns true if sub TLV present else false of IP external reachability TLV.
101 *
102 * @return true if present else false
103 */
104 public boolean isSubTlvPresence() {
105 return subTlvPresence;
106 }
107
108 /**
109 * Sets true if sub TLV present else false of IP external reachability TLV.
110 *
111 * @param subTlvPresence true if present else false
112 */
113 public void setSubTlvPresence(boolean subTlvPresence) {
114 this.subTlvPresence = subTlvPresence;
115 }
116
117 /**
118 * Sets the prefix length of IP external reachability TLV.
119 *
120 * @return prefix length
121 */
122 public int prefixLength() {
123 return prefixLength;
124 }
125
126 /**
127 * Returns the prefix length of IP external reachability TLV.
128 *
129 * @param prefixLength the prefix length of IP external reachability TLV
130 */
131 public void setPrefixLength(int prefixLength) {
132 this.prefixLength = prefixLength;
sunishvka1dfc3e2016-04-16 12:24:47 +0530133 }
134
135 /**
136 * Adds the traffic engineering sub TLV to IP external reachability TLV.
137 *
138 * @param trafEnginSubTlv traffic engineering sub TLV
139 */
140 public void addSubTlv(TrafficEngineeringSubTlv trafEnginSubTlv) {
141 this.trafEnginSubTlv.add(trafEnginSubTlv);
142 }
143
144 /**
145 * Returns the sub TLV length of IP external reachability TLV.
146 *
147 * @return sub TLV length
148 */
149 public byte subTlvLength() {
150 return subTlvLength;
151 }
152
153 /**
154 * Sets the sub TLV length for IP external reachability TLV.
155 *
156 * @param subTlvLength sub TLV length
157 */
158 public void setSubTlvLength(byte subTlvLength) {
159 this.subTlvLength = subTlvLength;
160 }
161
162 /**
Dhruv Dhodye64b93e2016-04-20 19:26:55 +0530163 * Returns metric of IP external reachability TLV.
sunishvka1dfc3e2016-04-16 12:24:47 +0530164 *
Dhruv Dhodye64b93e2016-04-20 19:26:55 +0530165 * @return metric
sunishvka1dfc3e2016-04-16 12:24:47 +0530166 */
Dhruv Dhodye64b93e2016-04-20 19:26:55 +0530167 public int metric() {
168 return metric;
sunishvka1dfc3e2016-04-16 12:24:47 +0530169 }
170
171 /**
172 * Sets default metric for IP external reachability TLV.
173 *
Dhruv Dhodye64b93e2016-04-20 19:26:55 +0530174 * @param metric default metric
sunishvka1dfc3e2016-04-16 12:24:47 +0530175 */
Dhruv Dhodye64b93e2016-04-20 19:26:55 +0530176 public void setMetric(int metric) {
177 this.metric = metric;
sunishvka1dfc3e2016-04-16 12:24:47 +0530178 }
179
180 @Override
181 public void readFrom(ChannelBuffer channelBuffer) {
Dhruv Dhodye64b93e2016-04-20 19:26:55 +0530182 this.setMetric(channelBuffer.readInt());
183 int controlInfo = channelBuffer.readByte();
184 byte[] tempByteArray = null;
185
186 String string = IsisUtil.toEightBitBinary(Integer.toBinaryString(controlInfo));
187 if (string.charAt(0) == '0') {
188 this.setDown(false);
189 }
190 if (string.charAt(1) == '1') {
191 this.setSubTlvPresence(true);
192 }
193 this.setPrefixLength(Integer.parseInt(string.substring(2, string.length()), 2));
194 if (this.prefixLength >= 0 && this.prefixLength <= 8) {
195 channelBuffer.readByte();
196 } else if (this.prefixLength >= 8 && this.prefixLength <= 16) {
197 tempByteArray = new byte[IsisUtil.TWO_BYTES];
198 channelBuffer.readBytes(tempByteArray, 0, IsisUtil.TWO_BYTES);
199 this.setPrefix(IsisUtil.prefixConversion(tempByteArray));
200 } else if (this.prefixLength >= 17 && this.prefixLength <= 24) {
201 tempByteArray = new byte[IsisUtil.THREE_BYTES];
202 channelBuffer.readBytes(tempByteArray, 0, IsisUtil.THREE_BYTES);
203 this.setPrefix(IsisUtil.prefixConversion(tempByteArray));
204 } else if (this.prefixLength >= 24 && this.prefixLength <= 32) {
205 tempByteArray = new byte[IsisUtil.FOUR_BYTES];
206 channelBuffer.readBytes(tempByteArray, 0, IsisUtil.FOUR_BYTES);
207 this.setPrefix(IsisUtil.prefixConversion(tempByteArray));
208 }
209 if (this.isSubTlvPresence()) {
210 this.setSubTlvLength(channelBuffer.readByte());
211 while (channelBuffer.readableBytes() > 0) {
212 TlvHeader tlvHeader = new TlvHeader();
213 tlvHeader.setTlvType(channelBuffer.readByte());
214 tlvHeader.setTlvLength(channelBuffer.readByte());
215 SubTlvType tlvValue = SubTlvType.get(tlvHeader.tlvType());
216 if (tlvValue != null) {
sunish vkc3824e82016-05-11 19:38:24 +0530217 TrafficEngineeringSubTlv subTlv =
218 SubTlvFinder.findSubTlv(tlvHeader, channelBuffer.readBytes(tlvHeader.tlvLength()));
219 if (subTlv != null) {
220 this.addSubTlv(subTlv);
221 }
Dhruv Dhodye64b93e2016-04-20 19:26:55 +0530222 } else {
223 channelBuffer.readBytes(tlvHeader.tlvLength());
224 }
225 }
sunishvka1dfc3e2016-04-16 12:24:47 +0530226 }
227 }
228
Dhruv Dhodye64b93e2016-04-20 19:26:55 +0530229
sunishvka1dfc3e2016-04-16 12:24:47 +0530230 @Override
231 public byte[] asBytes() {
232 byte[] bytes = null;
233 byte[] tlvHeader = tlvHeaderAsByteArray();
234 byte[] tlvBody = tlvBodyAsBytes();
sunishvka1dfc3e2016-04-16 12:24:47 +0530235 tlvHeader[1] = (byte) tlvBody.length;
236 bytes = Bytes.concat(tlvHeader, tlvBody);
tejeshwer degala3fe1ed52016-04-22 17:04:01 +0530237
sunishvka1dfc3e2016-04-16 12:24:47 +0530238 return bytes;
239 }
240
241 /**
242 * Returns TLV body of IP external reachability TLV.
243 *
244 * @return byteArray TLV body of IP external reachability TLV.
245 */
246 private byte[] tlvBodyAsBytes() {
247 List<Byte> bodyLst = new ArrayList<>();
Dhruv Dhodye64b93e2016-04-20 19:26:55 +0530248 bodyLst.addAll(Bytes.asList(IsisUtil.convertToFourBytes(this.metric())));
249 String controlInfo = "";
250 if (this.isDown()) {
251 controlInfo = controlInfo + "1";
252 } else {
253 controlInfo = controlInfo + "0";
254 }
255 if (this.isSubTlvPresence()) {
256 controlInfo = controlInfo + "1";
257 } else {
258 controlInfo = controlInfo + "0";
259 }
tejeshwer degala3fe1ed52016-04-22 17:04:01 +0530260 String prefixLength = IsisUtil.toEightBitBinary(Integer.toBinaryString(this.prefixLength()));
261 controlInfo = controlInfo + prefixLength.substring(2, prefixLength.length());
Dhruv Dhodye64b93e2016-04-20 19:26:55 +0530262 bodyLst.add(Byte.parseByte(controlInfo, 2));
263 if (this.isSubTlvPresence()) {
264 bodyLst.add(this.subTlvLength());
265 for (TrafficEngineeringSubTlv trafficEngineeringSubTlv : this.trafEnginSubTlv) {
266 bodyLst.addAll(SubTlvToBytes.tlvToBytes(trafficEngineeringSubTlv));
267 }
sunishvka1dfc3e2016-04-16 12:24:47 +0530268 }
tejeshwer degala3fe1ed52016-04-22 17:04:01 +0530269 bodyLst.addAll(Bytes.asList(IsisUtil.prefixToBytes(this.prefix())));
270
sunishvka1dfc3e2016-04-16 12:24:47 +0530271 return Bytes.toArray(bodyLst);
272 }
273
274 @Override
275 public String toString() {
276 return MoreObjects.toStringHelper(getClass())
277 .omitNullValues()
Dhruv Dhodye64b93e2016-04-20 19:26:55 +0530278 .add("down", down)
279 .add("subTlvPresence", subTlvPresence)
280 .add("prefixLength", prefixLength)
281 .add("metric", metric)
sunishvka1dfc3e2016-04-16 12:24:47 +0530282 .add("subTlvLength", subTlvLength)
Dhruv Dhodye64b93e2016-04-20 19:26:55 +0530283 .add("prefix", prefix)
sunishvka1dfc3e2016-04-16 12:24:47 +0530284 .add("trafEnginSubTlv", trafEnginSubTlv)
285 .toString();
286 }
287}