blob: 14962d8e1a8051eabb0ae9995676647968eedd61 [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 /**
Dhruv Dhodye64b93e2016-04-20 19:26:55 +053055 * Returns the prefix of IP external reachability TLV.
sunishvka1dfc3e2016-04-16 12:24:47 +053056 *
Dhruv Dhodye64b93e2016-04-20 19:26:55 +053057 * @return prefix
sunishvka1dfc3e2016-04-16 12:24:47 +053058 */
Dhruv Dhodye64b93e2016-04-20 19:26:55 +053059 public String prefix() {
60 return prefix;
sunishvka1dfc3e2016-04-16 12:24:47 +053061 }
62
63 /**
Dhruv Dhodye64b93e2016-04-20 19:26:55 +053064 * Sets the prefix of IP external reachability TLV.
sunishvka1dfc3e2016-04-16 12:24:47 +053065 *
Dhruv Dhodye64b93e2016-04-20 19:26:55 +053066 * @param prefix prefix
sunishvka1dfc3e2016-04-16 12:24:47 +053067 */
Dhruv Dhodye64b93e2016-04-20 19:26:55 +053068 public void setPrefix(String prefix) {
69 this.prefix = prefix;
70 }
71
72 /**
73 * Returns if down true else false of IP external reachability TLV.
74 *
75 * @return if down true else false
76 */
77 public boolean isDown() {
78 return down;
79 }
80
81 /**
82 * Sets if down true else false of IP external reachability TLV.
83 *
84 * @param upOrDown if down true else false
85 */
86 public void setDown(boolean upOrDown) {
87 this.down = upOrDown;
88 }
89
90 /**
91 * Returns true if sub TLV present else false of IP external reachability TLV.
92 *
93 * @return true if present else false
94 */
95 public boolean isSubTlvPresence() {
96 return subTlvPresence;
97 }
98
99 /**
100 * Sets true if sub TLV present else false of IP external reachability TLV.
101 *
102 * @param subTlvPresence true if present else false
103 */
104 public void setSubTlvPresence(boolean subTlvPresence) {
105 this.subTlvPresence = subTlvPresence;
106 }
107
108 /**
109 * Sets the prefix length of IP external reachability TLV.
110 *
111 * @return prefix length
112 */
113 public int prefixLength() {
114 return prefixLength;
115 }
116
117 /**
118 * Returns the prefix length of IP external reachability TLV.
119 *
120 * @param prefixLength the prefix length of IP external reachability TLV
121 */
122 public void setPrefixLength(int prefixLength) {
123 this.prefixLength = prefixLength;
sunishvka1dfc3e2016-04-16 12:24:47 +0530124 }
125
126 /**
127 * Adds the traffic engineering sub TLV to IP external reachability TLV.
128 *
129 * @param trafEnginSubTlv traffic engineering sub TLV
130 */
131 public void addSubTlv(TrafficEngineeringSubTlv trafEnginSubTlv) {
132 this.trafEnginSubTlv.add(trafEnginSubTlv);
133 }
134
135 /**
136 * Returns the sub TLV length of IP external reachability TLV.
137 *
138 * @return sub TLV length
139 */
140 public byte subTlvLength() {
141 return subTlvLength;
142 }
143
144 /**
145 * Sets the sub TLV length for IP external reachability TLV.
146 *
147 * @param subTlvLength sub TLV length
148 */
149 public void setSubTlvLength(byte subTlvLength) {
150 this.subTlvLength = subTlvLength;
151 }
152
153 /**
Dhruv Dhodye64b93e2016-04-20 19:26:55 +0530154 * Returns metric of IP external reachability TLV.
sunishvka1dfc3e2016-04-16 12:24:47 +0530155 *
Dhruv Dhodye64b93e2016-04-20 19:26:55 +0530156 * @return metric
sunishvka1dfc3e2016-04-16 12:24:47 +0530157 */
Dhruv Dhodye64b93e2016-04-20 19:26:55 +0530158 public int metric() {
159 return metric;
sunishvka1dfc3e2016-04-16 12:24:47 +0530160 }
161
162 /**
163 * Sets default metric for IP external reachability TLV.
164 *
Dhruv Dhodye64b93e2016-04-20 19:26:55 +0530165 * @param metric default metric
sunishvka1dfc3e2016-04-16 12:24:47 +0530166 */
Dhruv Dhodye64b93e2016-04-20 19:26:55 +0530167 public void setMetric(int metric) {
168 this.metric = metric;
sunishvka1dfc3e2016-04-16 12:24:47 +0530169 }
170
171 @Override
172 public void readFrom(ChannelBuffer channelBuffer) {
Dhruv Dhodye64b93e2016-04-20 19:26:55 +0530173 this.setMetric(channelBuffer.readInt());
174 int controlInfo = channelBuffer.readByte();
175 byte[] tempByteArray = null;
176
177 String string = IsisUtil.toEightBitBinary(Integer.toBinaryString(controlInfo));
178 if (string.charAt(0) == '0') {
179 this.setDown(false);
180 }
181 if (string.charAt(1) == '1') {
182 this.setSubTlvPresence(true);
183 }
184 this.setPrefixLength(Integer.parseInt(string.substring(2, string.length()), 2));
185 if (this.prefixLength >= 0 && this.prefixLength <= 8) {
186 channelBuffer.readByte();
187 } else if (this.prefixLength >= 8 && this.prefixLength <= 16) {
188 tempByteArray = new byte[IsisUtil.TWO_BYTES];
189 channelBuffer.readBytes(tempByteArray, 0, IsisUtil.TWO_BYTES);
190 this.setPrefix(IsisUtil.prefixConversion(tempByteArray));
191 } else if (this.prefixLength >= 17 && this.prefixLength <= 24) {
192 tempByteArray = new byte[IsisUtil.THREE_BYTES];
193 channelBuffer.readBytes(tempByteArray, 0, IsisUtil.THREE_BYTES);
194 this.setPrefix(IsisUtil.prefixConversion(tempByteArray));
195 } else if (this.prefixLength >= 24 && this.prefixLength <= 32) {
196 tempByteArray = new byte[IsisUtil.FOUR_BYTES];
197 channelBuffer.readBytes(tempByteArray, 0, IsisUtil.FOUR_BYTES);
198 this.setPrefix(IsisUtil.prefixConversion(tempByteArray));
199 }
200 if (this.isSubTlvPresence()) {
201 this.setSubTlvLength(channelBuffer.readByte());
202 while (channelBuffer.readableBytes() > 0) {
203 TlvHeader tlvHeader = new TlvHeader();
204 tlvHeader.setTlvType(channelBuffer.readByte());
205 tlvHeader.setTlvLength(channelBuffer.readByte());
206 SubTlvType tlvValue = SubTlvType.get(tlvHeader.tlvType());
207 if (tlvValue != null) {
208 this.addSubTlv(SubTlvFinder.findSubTlv(tlvHeader,
209 channelBuffer.readBytes(tlvHeader.tlvLength())));
210 } else {
211 channelBuffer.readBytes(tlvHeader.tlvLength());
212 }
213 }
sunishvka1dfc3e2016-04-16 12:24:47 +0530214 }
215 }
216
Dhruv Dhodye64b93e2016-04-20 19:26:55 +0530217
sunishvka1dfc3e2016-04-16 12:24:47 +0530218 @Override
219 public byte[] asBytes() {
220 byte[] bytes = null;
221 byte[] tlvHeader = tlvHeaderAsByteArray();
222 byte[] tlvBody = tlvBodyAsBytes();
223 //systemID + pseudo number+length of subtlv=11l
224 tlvBody[10] = (byte) (tlvBody.length - 11);
225 tlvHeader[1] = (byte) tlvBody.length;
226 bytes = Bytes.concat(tlvHeader, tlvBody);
227 return bytes;
228 }
229
230 /**
231 * Returns TLV body of IP external reachability TLV.
232 *
233 * @return byteArray TLV body of IP external reachability TLV.
234 */
235 private byte[] tlvBodyAsBytes() {
236 List<Byte> bodyLst = new ArrayList<>();
Dhruv Dhodye64b93e2016-04-20 19:26:55 +0530237 bodyLst.addAll(Bytes.asList(IsisUtil.convertToFourBytes(this.metric())));
238 String controlInfo = "";
239 if (this.isDown()) {
240 controlInfo = controlInfo + "1";
241 } else {
242 controlInfo = controlInfo + "0";
243 }
244 if (this.isSubTlvPresence()) {
245 controlInfo = controlInfo + "1";
246 } else {
247 controlInfo = controlInfo + "0";
248 }
249 String prefixlength = IsisUtil.toEightBitBinary(Integer.toBinaryString(this.prefixLength()));
250 controlInfo = controlInfo + prefixlength.substring(2, prefixlength.length());
251 bodyLst.add(Byte.parseByte(controlInfo, 2));
252 if (this.isSubTlvPresence()) {
253 bodyLst.add(this.subTlvLength());
254 for (TrafficEngineeringSubTlv trafficEngineeringSubTlv : this.trafEnginSubTlv) {
255 bodyLst.addAll(SubTlvToBytes.tlvToBytes(trafficEngineeringSubTlv));
256 }
sunishvka1dfc3e2016-04-16 12:24:47 +0530257 }
258 return Bytes.toArray(bodyLst);
259 }
260
261 @Override
262 public String toString() {
263 return MoreObjects.toStringHelper(getClass())
264 .omitNullValues()
Dhruv Dhodye64b93e2016-04-20 19:26:55 +0530265 .add("down", down)
266 .add("subTlvPresence", subTlvPresence)
267 .add("prefixLength", prefixLength)
268 .add("metric", metric)
sunishvka1dfc3e2016-04-16 12:24:47 +0530269 .add("subTlvLength", subTlvLength)
Dhruv Dhodye64b93e2016-04-20 19:26:55 +0530270 .add("prefix", prefix)
sunishvka1dfc3e2016-04-16 12:24:47 +0530271 .add("trafEnginSubTlv", trafEnginSubTlv)
272 .toString();
273 }
274}