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.OspfParameters; |
| 23 | import org.onosproject.ospf.protocol.util.OspfUtil; |
| 24 | |
| 25 | import java.util.ArrayList; |
| 26 | import java.util.List; |
| 27 | |
| 28 | /** |
| 29 | * Representation of an unreserved band width TE value. |
| 30 | */ |
| 31 | public class UnreservedBandwidth extends TlvHeader implements LinkSubType { |
| 32 | private List<Float> unReservedBandwidth = new ArrayList<>(); |
| 33 | |
| 34 | /** |
| 35 | * Creates an instance of unreserved band width. |
| 36 | * |
| 37 | * @param header tlv header instance |
| 38 | */ |
| 39 | public UnreservedBandwidth(TlvHeader header) { |
| 40 | this.setTlvType(header.tlvType()); |
| 41 | this.setTlvLength(header.tlvLength()); |
| 42 | } |
| 43 | |
| 44 | /** |
| 45 | * Adds value of un reserved bandwidth . |
| 46 | * |
| 47 | * @param unreservedBandwidth value of un reserved bandwidth |
| 48 | */ |
| 49 | public void addUnReservedBandwidth(float unreservedBandwidth) { |
| 50 | this.unReservedBandwidth.add(unreservedBandwidth); |
| 51 | } |
| 52 | |
| 53 | /** |
| 54 | * Gets list of un reserved bandwidth . |
| 55 | * |
| 56 | * @return List of un reserved bandwidth |
| 57 | */ |
| 58 | public List<Float> getUnReservedBandwidthValue() { |
| 59 | return this.unReservedBandwidth; |
| 60 | } |
| 61 | |
| 62 | /** |
| 63 | * Reads bytes from channel buffer . |
| 64 | * |
| 65 | * @param channelBuffer channel buffer instance |
| 66 | */ |
| 67 | public void readFrom(ChannelBuffer channelBuffer) { |
| 68 | while (channelBuffer.readableBytes() >= OspfUtil.FOUR_BYTES) { |
| 69 | int maxReversibleBandwidth = channelBuffer.readInt(); |
| 70 | this.addUnReservedBandwidth(Float.intBitsToFloat(maxReversibleBandwidth)); |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * Gets instance as byte array. |
| 76 | * |
| 77 | * @return instance as byte array |
| 78 | */ |
| 79 | public byte[] asBytes() { |
| 80 | byte[] linkSubType = null; |
| 81 | |
| 82 | byte[] linkSubTlvHeader = getTlvHeaderAsByteArray(); |
| 83 | byte[] linkSubTlvBody = getLinkSubTypeTlvBodyAsByteArray(); |
| 84 | linkSubType = Bytes.concat(linkSubTlvHeader, linkSubTlvBody); |
| 85 | |
| 86 | return linkSubType; |
| 87 | } |
| 88 | |
| 89 | /** |
| 90 | * Gets unreserved bandwidth as byte array. |
| 91 | * |
| 92 | * @return unreserved bandwidth as byte array |
| 93 | */ |
| 94 | public byte[] getLinkSubTypeTlvBodyAsByteArray() { |
| 95 | List<Byte> linkSubTypeBody = new ArrayList<>(); |
| 96 | if (this.unReservedBandwidth.size() < 8) { |
| 97 | int size = OspfUtil.EIGHT_BYTES - this.unReservedBandwidth.size(); |
| 98 | for (int i = 0; i < size; i++) { |
| 99 | linkSubTypeBody.addAll(Bytes.asList(OspfUtil.convertToFourBytes(OspfParameters.INITIAL_BANDWIDTH))); |
| 100 | } |
| 101 | } |
| 102 | for (Float unreservedBandwidth : this.unReservedBandwidth) { |
| 103 | int unresBandwidth = Float.floatToIntBits(unreservedBandwidth); |
| 104 | linkSubTypeBody.addAll(Bytes.asList(OspfUtil.convertToFourBytes(unresBandwidth))); |
| 105 | } |
| 106 | |
| 107 | return Bytes.toArray(linkSubTypeBody); |
| 108 | } |
| 109 | |
| 110 | @Override |
| 111 | public String toString() { |
| 112 | return MoreObjects.toStringHelper(getClass()) |
| 113 | .omitNullValues() |
| 114 | .add("unReservedBandwidth", unReservedBandwidth) |
| 115 | .toString(); |
| 116 | } |
| 117 | } |