sunishvk | a1dfc3e | 2016-04-16 12:24:47 +0530 | [diff] [blame] | 1 | /* |
| 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 | package org.onosproject.isis.io.isispacket.tlv.subtlv; |
| 17 | |
| 18 | import com.google.common.base.MoreObjects; |
| 19 | import com.google.common.primitives.Bytes; |
| 20 | import org.jboss.netty.buffer.ChannelBuffer; |
| 21 | import org.onosproject.isis.io.isispacket.tlv.TlvHeader; |
| 22 | import org.onosproject.isis.io.util.IsisUtil; |
| 23 | |
| 24 | |
| 25 | /** |
| 26 | * Representation of maximum bandwidth TE value. |
| 27 | */ |
| 28 | public class MaximumBandwidth extends TlvHeader implements TrafficEngineeringSubTlv { |
| 29 | private float maximumBandwidth; |
| 30 | |
| 31 | /** |
| 32 | * Creates an instance of maximum bandwidth. |
| 33 | * |
Dhruv Dhody | e64b93e | 2016-04-20 19:26:55 +0530 | [diff] [blame] | 34 | * @param header TLV header instance |
sunishvk | a1dfc3e | 2016-04-16 12:24:47 +0530 | [diff] [blame] | 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 | /** |
Dhruv Dhody | e64b93e | 2016-04-20 19:26:55 +0530 | [diff] [blame] | 51 | * Returns value of maximum bandwidth. |
sunishvk | a1dfc3e | 2016-04-16 12:24:47 +0530 | [diff] [blame] | 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 | /** |
Dhruv Dhody | e64b93e | 2016-04-20 19:26:55 +0530 | [diff] [blame] | 72 | * Returns byte array of maximum bandwidth sub tlv. |
sunishvk | a1dfc3e | 2016-04-16 12:24:47 +0530 | [diff] [blame] | 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 | /** |
Dhruv Dhody | e64b93e | 2016-04-20 19:26:55 +0530 | [diff] [blame] | 86 | * Returns maximum bandwidth sub tlv byte array. |
sunishvk | a1dfc3e | 2016-04-16 12:24:47 +0530 | [diff] [blame] | 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 | } |
Dhruv Dhody | e64b93e | 2016-04-20 19:26:55 +0530 | [diff] [blame] | 95 | @Override |
sunishvk | a1dfc3e | 2016-04-16 12:24:47 +0530 | [diff] [blame] | 96 | public String toString() { |
| 97 | return MoreObjects.toStringHelper(getClass()) |
| 98 | .add("maximumBandwidth", maximumBandwidth) |
| 99 | .toString(); |
| 100 | } |
| 101 | } |