blob: 3f8f42aadd514e50de70cb5a87fc854f05109cd0 [file] [log] [blame]
Kiran Ramachandra959353a2016-02-16 22:12:07 +05301/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2016-present Open Networking Laboratory
Kiran Ramachandra959353a2016-02-16 22:12:07 +05303 *
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.ospf.protocol.lsa.linksubtype;
17
18import com.google.common.base.MoreObjects;
19import com.google.common.primitives.Bytes;
20import org.jboss.netty.buffer.ChannelBuffer;
21import org.onosproject.ospf.protocol.lsa.TlvHeader;
22import org.onosproject.ospf.protocol.util.OspfUtil;
23
24/**
25 * Representation of maximum reservable bandwidth TE value.
26 */
27public class MaximumReservableBandwidth extends TlvHeader implements LinkSubType {
28 private float maximumReservableBandwidth;
29
30 /**
31 * Creates an instance of maximum reservable bandwidth.
32 *
33 * @param header tlv header
34 */
35 public MaximumReservableBandwidth(TlvHeader header) {
36 this.setTlvType(header.tlvType());
37 this.setTlvLength(header.tlvLength());
38 }
39
40 /**
41 * Sets value of maximum reversible bandwidth.
42 *
43 * @param maximumBandwidth maximum reversible bandwidth
44 */
45 public void setMaximumBandwidth(float maximumBandwidth) {
46 this.maximumReservableBandwidth = maximumBandwidth;
47 }
48
49 /**
50 * Gets value of maximum reversible bandwidth.
51 *
52 * @return maximumBandwidth maximum reversible bandwidth
53 */
54 public float getMaximumBandwidthValue() {
55 return this.maximumReservableBandwidth;
56 }
57
58 /**
59 * Reads bytes from channel buffer.
60 *
61 * @param channelBuffer channel buffer instance
62 */
63 public void readFrom(ChannelBuffer channelBuffer) {
64 byte[] tempByteArray = new byte[tlvLength()];
65 channelBuffer.readBytes(tempByteArray, 0, tlvLength());
66 int maxBandwidth = (OspfUtil.byteToInteger(tempByteArray));
67 this.setMaximumBandwidth(Float.intBitsToFloat(maxBandwidth));
68 }
69
70 /**
71 * Returns byte array of maximum reservable bandwidth.
72 *
73 * @return byte array of maximum reservable bandwidth
74 */
75 public byte[] asBytes() {
76 byte[] linkSubType = null;
77
78 byte[] linkSubTlvHeader = getTlvHeaderAsByteArray();
79 byte[] linkSubTlvBody = getLinksubTypeTlvBodyAsByteArray();
80 linkSubType = Bytes.concat(linkSubTlvHeader, linkSubTlvBody);
81 return linkSubType;
82
83 }
84
85 /**
86 * Gets maximum reservable bandwidth sub tlv body as byte array.
87 *
88 * @return byte of maximum reservable bandwidth sub tlv body
89 */
90 public byte[] getLinksubTypeTlvBodyAsByteArray() {
91 byte[] linkSubTypeBody;
92 linkSubTypeBody = OspfUtil.convertToFourBytes(Float.floatToIntBits(this.maximumReservableBandwidth));
93 return linkSubTypeBody;
94 }
95
96 @Override
97 public String toString() {
98 return MoreObjects.toStringHelper(getClass())
99 .add("maximumReservableBandwidth", maximumReservableBandwidth)
100 .toString();
101 }
102}