blob: 4b3e7b65163dab60eced38e7915f2cd16c93ce58 [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 */
16package org.onosproject.isis.io.isispacket.tlv.subtlv;
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.TlvHeader;
22import org.onosproject.isis.io.util.IsisUtil;
23
24import java.util.ArrayList;
25import java.util.List;
26
27/**
28 * Representation of an unreserved band width TE value.
29 */
30public class UnreservedBandwidth extends TlvHeader implements TrafficEngineeringSubTlv {
31 private List<Float> unReservedBandwidth = new ArrayList<>();
32
33 /**
34 * Creates an instance of unreserved band width.
35 *
36 * @param header tlv header instance
37 */
38 public UnreservedBandwidth(TlvHeader header) {
39 this.setTlvType(header.tlvType());
40 this.setTlvLength(header.tlvLength());
41 }
42
43 /**
44 * Adds value of un reserved bandwidth .
45 *
46 * @param unreservedBandwidth value of un reserved bandwidth
47 */
48 public void addUnReservedBandwidth(float unreservedBandwidth) {
49 this.unReservedBandwidth.add(unreservedBandwidth);
50 }
51
52 /**
Dhruv Dhodye64b93e2016-04-20 19:26:55 +053053 * Returns list of un reserved bandwidth .
sunishvka1dfc3e2016-04-16 12:24:47 +053054 *
55 * @return List of un reserved bandwidth
56 */
sunish vk7bdf4d42016-06-24 12:29:43 +053057 public List<Float> unReservedBandwidthValue() {
sunishvka1dfc3e2016-04-16 12:24:47 +053058 return this.unReservedBandwidth;
59 }
60
61 /**
62 * Reads bytes from channel buffer .
63 *
64 * @param channelBuffer channel buffer instance
65 */
66 public void readFrom(ChannelBuffer channelBuffer) {
67 while (channelBuffer.readableBytes() >= IsisUtil.FOUR_BYTES) {
68 int maxReversibleBandwidth = channelBuffer.readInt();
69 this.addUnReservedBandwidth(Float.intBitsToFloat(maxReversibleBandwidth));
70 }
71 }
72
73 /**
Dhruv Dhodye64b93e2016-04-20 19:26:55 +053074 * Returns instance as byte array.
sunishvka1dfc3e2016-04-16 12:24:47 +053075 *
76 * @return instance as byte array
77 */
78 public byte[] asBytes() {
79 byte[] linkSubType = null;
80
81 byte[] linkSubTlvHeader = tlvHeaderAsByteArray();
82 byte[] linkSubTlvBody = tlvBodyAsBytes();
83 linkSubType = Bytes.concat(linkSubTlvHeader, linkSubTlvBody);
84
85 return linkSubType;
86 }
87
88 /**
Dhruv Dhodye64b93e2016-04-20 19:26:55 +053089 * Returns unreserved bandwidth as byte array.
sunishvka1dfc3e2016-04-16 12:24:47 +053090 *
91 * @return unreserved bandwidth as byte array
92 */
93 public byte[] tlvBodyAsBytes() {
94 List<Byte> linkSubTypeBody = new ArrayList<>();
95 if (this.unReservedBandwidth.size() < 8) {
96 int size = IsisUtil.EIGHT_BYTES - this.unReservedBandwidth.size();
97 for (int i = 0; i < size; i++) {
98 linkSubTypeBody.addAll(Bytes.asList(IsisUtil.convertToFourBytes(IsisUtil.INITIAL_BANDWIDTH)));
99 }
100 }
101 for (Float unreservedBandwidth : this.unReservedBandwidth) {
102 int unresBandwidth = Float.floatToIntBits(unreservedBandwidth);
103 linkSubTypeBody.addAll(Bytes.asList(IsisUtil.convertToFourBytes(unresBandwidth)));
104 }
105
106 return Bytes.toArray(linkSubTypeBody);
107 }
108
109 @Override
110 public String toString() {
111 return MoreObjects.toStringHelper(getClass())
112 .omitNullValues()
113 .add("unReservedBandwidth", unReservedBandwidth)
114 .toString();
115 }
116}