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.Objects;
20 import org.jboss.netty.buffer.ChannelBuffer;
21 import org.onosproject.bgpio.exceptions.BgpParseException;
22 import org.onosproject.bgpio.types.BgpErrorType;
23 import org.onosproject.bgpio.types.BgpValueType;
24 import org.onosproject.bgpio.util.Validation;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
28 import com.google.common.base.MoreObjects;
31 * Implements BGP attribute Max Link bandwidth.
33 public final class BgpLinkAttrMaxLinkBandwidth implements BgpValueType {
35 protected static final Logger log = LoggerFactory
36 .getLogger(BgpLinkAttrMaxLinkBandwidth.class);
38 public static final int MAX_BANDWIDTH_LEN = 4;
39 public static final int NO_OF_BITS = 8;
43 /* ISIS administrative group */
44 private final float maxBandwidth;
47 * Constructor to initialize the values.
49 * @param maxBandwidth Maximum link bandwidth.
50 * @param type TLV type
52 private BgpLinkAttrMaxLinkBandwidth(float maxBandwidth, short type) {
53 this.maxBandwidth = maxBandwidth;
58 * Returns object of this class with specified values.
60 * @param maxBandwidth Maximum link bandwidth.
61 * @param type TLV type
62 * @return object of BgpLinkAttrMaxLinkBandwidth
64 public static BgpLinkAttrMaxLinkBandwidth of(final float maxBandwidth,
66 return new BgpLinkAttrMaxLinkBandwidth(maxBandwidth, type);
70 * Reads the BGP link attributes of Maximum link bandwidth.
72 * @param cb Channel buffer
73 * @param type type of this tlv
74 * @return object of type BgpLinkAttrMaxLinkBandwidth
75 * @throws BgpParseException while parsing BgpLinkAttrMaxLinkBandwidth
77 public static BgpLinkAttrMaxLinkBandwidth read(ChannelBuffer cb, short type)
78 throws BgpParseException {
80 short lsAttrLength = cb.readShort();
82 if ((lsAttrLength != MAX_BANDWIDTH_LEN)
83 || (cb.readableBytes() < lsAttrLength)) {
84 Validation.validateLen(BgpErrorType.UPDATE_MESSAGE_ERROR,
85 BgpErrorType.ATTRIBUTE_LENGTH_ERROR,
89 maxBandwidth = ieeeToFloatRead(cb.readInt()) * NO_OF_BITS;
91 return BgpLinkAttrMaxLinkBandwidth.of(maxBandwidth, type);
95 * Returns Maximum link bandwidth.
97 * @return Maximum link bandwidth
99 float linkAttrMaxLinkBandwidth() {
104 * Parse the IEEE floating point notation and returns it in normal float.
106 * @param iVal IEEE floating point number
107 * @return normal float
109 static float ieeeToFloatRead(int iVal) {
110 iVal = (((iVal & 0xFF) << 24) | ((iVal & 0xFF00) << 8)
111 | ((iVal & 0xFF0000) >> 8) | ((iVal >> 24) & 0xFF));
113 return Float.intBitsToFloat(iVal);
117 public short getType() {
122 public int hashCode() {
123 return Objects.hash(maxBandwidth);
127 public boolean equals(Object obj) {
132 if (obj instanceof BgpLinkAttrMaxLinkBandwidth) {
133 BgpLinkAttrMaxLinkBandwidth other = (BgpLinkAttrMaxLinkBandwidth) obj;
134 return Objects.equals(maxBandwidth, other.maxBandwidth);
140 public int write(ChannelBuffer cb) {
141 // TODO This will be implemented in the next version
146 public String toString() {
147 return MoreObjects.toStringHelper(getClass())
148 .add("maxBandwidth", maxBandwidth).toString();
152 public int compareTo(Object o) {
153 // TODO Auto-generated method stub