Kiran Ramachandra | 959353a | 2016-02-16 22:12:07 +0530 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016 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.ospf.protocol.lsa.linksubtype; |
| 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.ospf.protocol.lsa.TlvHeader; |
| 22 | import org.onosproject.ospf.protocol.util.OspfUtil; |
| 23 | |
| 24 | /** |
| 25 | * Representation of maximum bandwidth TE value. |
| 26 | */ |
| 27 | public class MaximumBandwidth extends TlvHeader implements LinkSubType { |
| 28 | private float maximumBandwidth; |
| 29 | |
| 30 | /** |
| 31 | * Creates an instance of maximum bandwidth. |
| 32 | * |
| 33 | * @param header tlv header instance |
| 34 | */ |
| 35 | public MaximumBandwidth(TlvHeader header) { |
| 36 | this.setTlvType(header.tlvType()); |
| 37 | this.setTlvLength(header.tlvLength()); |
| 38 | } |
| 39 | |
| 40 | /** |
| 41 | * Sets value of maximum bandwidth. |
| 42 | * |
| 43 | * @param maximumBandwidth value of maximum bandwidth |
| 44 | */ |
| 45 | public void setMaximumBandwidth(float maximumBandwidth) { |
| 46 | this.maximumBandwidth = maximumBandwidth; |
| 47 | } |
| 48 | |
| 49 | /** |
| 50 | * Gets value of maximum bandwidth. |
| 51 | * |
| 52 | * @return maximumBandwidth value of maximum bandwidth |
| 53 | */ |
| 54 | public float getMaximumBandwidthValue() { |
| 55 | return this.maximumBandwidth; |
| 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 | * Gets byte array of maximum bandwidth sub tlv. |
| 72 | * |
| 73 | * @return byte array of maximum bandwidth sub tlv |
| 74 | */ |
| 75 | public byte[] asBytes() { |
| 76 | byte[] linkSubType = null; |
| 77 | byte[] linkSubTlvHeader = getTlvHeaderAsByteArray(); |
| 78 | byte[] linkSubTlvBody = getLinkSubTypeTlvBodyAsByteArray(); |
| 79 | linkSubType = Bytes.concat(linkSubTlvHeader, linkSubTlvBody); |
| 80 | |
| 81 | return linkSubType; |
| 82 | } |
| 83 | |
| 84 | /** |
| 85 | * Gets maximum bandwidth sub tlv byte array. |
| 86 | * |
| 87 | * @return byte array of maximum bandwidth sub tlv |
| 88 | */ |
| 89 | public byte[] getLinkSubTypeTlvBodyAsByteArray() { |
| 90 | byte[] linkSubTypeBody; |
| 91 | linkSubTypeBody = OspfUtil.convertToFourBytes(Float.floatToIntBits(this.maximumBandwidth)); |
| 92 | return linkSubTypeBody; |
| 93 | } |
| 94 | |
| 95 | public String toString() { |
| 96 | return MoreObjects.toStringHelper(getClass()) |
| 97 | .add("maximumBandwidth", maximumBandwidth) |
| 98 | .toString(); |
| 99 | } |
| 100 | } |