blob: e5cba2b901bafd5c006a0a637b4215a4d6459a80 [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
24
25/**
26 * Representation of maximum bandwidth TE value.
27 */
28public class MaximumBandwidth extends TlvHeader implements TrafficEngineeringSubTlv {
29 private float maximumBandwidth;
30
31 /**
32 * Creates an instance of maximum bandwidth.
33 *
34 * @param header tlv header instance
35 */
36 public MaximumBandwidth(TlvHeader header) {
37 this.setTlvType(header.tlvType());
38 this.setTlvLength(header.tlvLength());
39 }
40
41 /**
42 * Sets value of maximum bandwidth.
43 *
44 * @param maximumBandwidth value of maximum bandwidth
45 */
46 public void setMaximumBandwidth(float maximumBandwidth) {
47 this.maximumBandwidth = maximumBandwidth;
48 }
49
50 /**
51 * Gets value of maximum bandwidth.
52 *
53 * @return maximumBandwidth value of maximum bandwidth
54 */
55 public float getMaximumBandwidthValue() {
56 return this.maximumBandwidth;
57 }
58
59 /**
60 * Reads bytes from channel buffer.
61 *
62 * @param channelBuffer channel buffer instance
63 */
64 public void readFrom(ChannelBuffer channelBuffer) {
65 byte[] tempByteArray = new byte[tlvLength()];
66 channelBuffer.readBytes(tempByteArray, 0, tlvLength());
67 int maxBandwidth = (IsisUtil.byteToInteger(tempByteArray));
68 this.setMaximumBandwidth(Float.intBitsToFloat(maxBandwidth));
69 }
70
71 /**
72 * Gets byte array of maximum bandwidth sub tlv.
73 *
74 * @return byte array of maximum bandwidth sub tlv
75 */
76 public byte[] asBytes() {
77 byte[] linkSubType = null;
78 byte[] linkSubTlvHeader = tlvHeaderAsByteArray();
79 byte[] linkSubTlvBody = tlvBodyAsBytes();
80 linkSubType = Bytes.concat(linkSubTlvHeader, linkSubTlvBody);
81
82 return linkSubType;
83 }
84
85 /**
86 * Gets maximum bandwidth sub tlv byte array.
87 *
88 * @return byte array of maximum bandwidth sub tlv
89 */
90 public byte[] tlvBodyAsBytes() {
91 byte[] linkSubTypeBody;
92 linkSubTypeBody = IsisUtil.convertToFourBytes(Float.floatToIntBits(this.maximumBandwidth));
93 return linkSubTypeBody;
94 }
95
96 public String toString() {
97 return MoreObjects.toStringHelper(getClass())
98 .add("maximumBandwidth", maximumBandwidth)
99 .toString();
100 }
101}