2 * Copyright 2015 Open Networking Laboratory
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package org.onosproject.bgpio.types.attr;
18 import java.util.ArrayList;
19 import java.util.List;
20 import java.util.Objects;
22 import org.jboss.netty.buffer.ChannelBuffer;
23 import org.onosproject.bgpio.exceptions.BgpParseException;
24 import org.onosproject.bgpio.types.BgpErrorType;
25 import org.onosproject.bgpio.types.BgpValueType;
26 import org.onosproject.bgpio.util.Validation;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
30 import com.google.common.base.MoreObjects;
33 * Implements BGP unreserved bandwidth attribute.
35 public class BgpLinkAttrUnRsrvdLinkBandwidth implements BgpValueType {
37 protected static final Logger log = LoggerFactory
38 .getLogger(BgpLinkAttrUnRsrvdLinkBandwidth.class);
40 public static final int MAX_BANDWIDTH_LEN = 4;
41 public static final int NO_OF_BITS = 8;
42 public static final int NO_OF_PRIORITY = 8;
46 /* ISIS administrative group */
47 private List<Float> maxUnResBandwidth = new ArrayList<Float>();
50 * Constructor to initialize the values.
52 * @param maxUnResBandwidth Maximum Unreserved bandwidth
53 * @param sType returns the tag value
55 public BgpLinkAttrUnRsrvdLinkBandwidth(List<Float> maxUnResBandwidth,
57 this.maxUnResBandwidth = maxUnResBandwidth;
62 * Returns object of this class with specified values.
64 * @param linkPfxMetric Prefix Metric
65 * @param sType returns the tag value
66 * @return object of BgpLinkAttrUnRsrvdLinkBandwidth
68 public static BgpLinkAttrUnRsrvdLinkBandwidth of(List<Float> linkPfxMetric, short sType) {
69 return new BgpLinkAttrUnRsrvdLinkBandwidth(linkPfxMetric, sType);
73 * Reads the BGP link attributes of Maximum link bandwidth.
75 * @param cb Channel buffer
76 * @return object of type BgpLinkAttrMaxLinkBandwidth
77 * @throws BgpParseException while parsing BgpLinkAttrMaxLinkBandwidth
79 public static BgpLinkAttrUnRsrvdLinkBandwidth read(ChannelBuffer cb,
81 throws BgpParseException {
82 ArrayList<Float> maxUnResBandwidth = new ArrayList<Float>();
84 short lsAttrLength = cb.readShort();
86 if ((lsAttrLength != MAX_BANDWIDTH_LEN * NO_OF_PRIORITY)
87 || (cb.readableBytes() < lsAttrLength)) {
88 Validation.validateLen(BgpErrorType.UPDATE_MESSAGE_ERROR,
89 BgpErrorType.ATTRIBUTE_LENGTH_ERROR,
93 for (int i = 0; i < NO_OF_PRIORITY; i++) {
94 tmp = ieeeToFloatRead(cb.readInt()) * NO_OF_BITS;
95 maxUnResBandwidth.add(new Float(tmp));
98 return BgpLinkAttrUnRsrvdLinkBandwidth.of(maxUnResBandwidth, sType);
102 * Returns maximum unreserved bandwidth.
104 * @return unreserved bandwidth.
106 public List<Float> getLinkAttrUnRsrvdLinkBandwidth() {
107 return maxUnResBandwidth;
111 * Parse the IEEE floating point notation and returns it in normal float.
113 * @param iVal IEEE floating point number
114 * @return normal float
116 static float ieeeToFloatRead(int iVal) {
117 iVal = (((iVal & 0xFF) << 24) | ((iVal & 0xFF00) << 8)
118 | ((iVal & 0xFF0000) >> 8) | ((iVal >> 24) & 0xFF));
120 return Float.intBitsToFloat(iVal);
124 public short getType() {
129 public int hashCode() {
130 return Objects.hash(maxUnResBandwidth);
134 public boolean equals(Object obj) {
139 if (obj instanceof BgpLinkAttrUnRsrvdLinkBandwidth) {
140 BgpLinkAttrUnRsrvdLinkBandwidth other = (BgpLinkAttrUnRsrvdLinkBandwidth) obj;
141 return Objects.equals(maxUnResBandwidth, other.maxUnResBandwidth);
147 public int write(ChannelBuffer cb) {
148 // TODO This will be implemented in the next version
153 public String toString() {
154 return MoreObjects.toStringHelper(getClass()).omitNullValues()
155 .add("maxUnResBandwidth", maxUnResBandwidth).toString();
159 public int compareTo(Object o) {
160 // TODO Auto-generated method stub