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 | import java.util.ArrayList; |
| 25 | import java.util.List; |
| 26 | |
| 27 | /** |
| 28 | * Representation of an unreserved band width TE value. |
| 29 | */ |
| 30 | public 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 | /** |
| 53 | * Gets list of un reserved bandwidth . |
| 54 | * |
| 55 | * @return List of un reserved bandwidth |
| 56 | */ |
| 57 | public List<Float> getUnReservedBandwidthValue() { |
| 58 | 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 | /** |
| 74 | * Gets instance as byte array. |
| 75 | * |
| 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 | /** |
| 89 | * Gets unreserved bandwidth as byte array. |
| 90 | * |
| 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 | } |