blob: 2711ca946230c67ff6bc6f7a9ae2294a4cae6878 [file] [log] [blame]
Thejaswi N Kbbbefca2015-11-12 00:11:21 +05301/*
2 * Copyright 2015 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 */
16package org.onosproject.bgpio.types.attr;
17
18import java.util.Objects;
19
20import org.jboss.netty.buffer.ChannelBuffer;
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053021import org.onosproject.bgpio.exceptions.BgpParseException;
22import org.onosproject.bgpio.types.BgpErrorType;
23import org.onosproject.bgpio.types.BgpValueType;
Thejaswi N Kbbbefca2015-11-12 00:11:21 +053024import org.onosproject.bgpio.util.Validation;
25import org.slf4j.Logger;
26import org.slf4j.LoggerFactory;
27
28import com.google.common.base.MoreObjects;
29
30/**
31 * Implements BGP attribute Max Link bandwidth.
32 */
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053033public final class BgpLinkAttrMaxLinkBandwidth implements BgpValueType {
Thejaswi N Kbbbefca2015-11-12 00:11:21 +053034
35 protected static final Logger log = LoggerFactory
36 .getLogger(BgpLinkAttrMaxLinkBandwidth.class);
37
38 public static final int MAX_BANDWIDTH_LEN = 4;
39 public static final int NO_OF_BITS = 8;
40
41 public short type;
42
43 /* ISIS administrative group */
44 private final float maxBandwidth;
45
46 /**
47 * Constructor to initialize the values.
48 *
49 * @param maxBandwidth Maximum link bandwidth.
50 * @param type TLV type
51 */
52 private BgpLinkAttrMaxLinkBandwidth(float maxBandwidth, short type) {
53 this.maxBandwidth = maxBandwidth;
54 this.type = type;
55 }
56
57 /**
58 * Returns object of this class with specified values.
59 *
60 * @param maxBandwidth Maximum link bandwidth.
61 * @param type TLV type
62 * @return object of BgpLinkAttrMaxLinkBandwidth
63 */
64 public static BgpLinkAttrMaxLinkBandwidth of(final float maxBandwidth,
65 final short type) {
66 return new BgpLinkAttrMaxLinkBandwidth(maxBandwidth, type);
67 }
68
69 /**
70 * Reads the BGP link attributes of Maximum link bandwidth.
71 *
72 * @param cb Channel buffer
73 * @param type type of this tlv
74 * @return object of type BgpLinkAttrMaxLinkBandwidth
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053075 * @throws BgpParseException while parsing BgpLinkAttrMaxLinkBandwidth
Thejaswi N Kbbbefca2015-11-12 00:11:21 +053076 */
77 public static BgpLinkAttrMaxLinkBandwidth read(ChannelBuffer cb, short type)
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053078 throws BgpParseException {
Thejaswi N Kbbbefca2015-11-12 00:11:21 +053079 float maxBandwidth;
80 short lsAttrLength = cb.readShort();
81
82 if ((lsAttrLength != MAX_BANDWIDTH_LEN)
83 || (cb.readableBytes() < lsAttrLength)) {
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053084 Validation.validateLen(BgpErrorType.UPDATE_MESSAGE_ERROR,
85 BgpErrorType.ATTRIBUTE_LENGTH_ERROR,
Thejaswi N Kbbbefca2015-11-12 00:11:21 +053086 lsAttrLength);
87 }
88
89 maxBandwidth = ieeeToFloatRead(cb.readInt()) * NO_OF_BITS;
90
91 return BgpLinkAttrMaxLinkBandwidth.of(maxBandwidth, type);
92 }
93
94 /**
95 * Returns Maximum link bandwidth.
96 *
97 * @return Maximum link bandwidth
98 */
99 float linkAttrMaxLinkBandwidth() {
100 return maxBandwidth;
101 }
102
103 /**
104 * Parse the IEEE floating point notation and returns it in normal float.
105 *
106 * @param iVal IEEE floating point number
107 * @return normal float
108 */
109 static float ieeeToFloatRead(int iVal) {
110 iVal = (((iVal & 0xFF) << 24) | ((iVal & 0xFF00) << 8)
111 | ((iVal & 0xFF0000) >> 8) | ((iVal >> 24) & 0xFF));
112
113 return Float.intBitsToFloat(iVal);
114 }
115
116 @Override
117 public short getType() {
118 return this.type;
119 }
120
121 @Override
122 public int hashCode() {
123 return Objects.hash(maxBandwidth);
124 }
125
126 @Override
127 public boolean equals(Object obj) {
128 if (this == obj) {
129 return true;
130 }
131
132 if (obj instanceof BgpLinkAttrMaxLinkBandwidth) {
133 BgpLinkAttrMaxLinkBandwidth other = (BgpLinkAttrMaxLinkBandwidth) obj;
134 return Objects.equals(maxBandwidth, other.maxBandwidth);
135 }
136 return false;
137 }
138
139 @Override
140 public int write(ChannelBuffer cb) {
141 // TODO This will be implemented in the next version
142 return 0;
143 }
144
145 @Override
146 public String toString() {
147 return MoreObjects.toStringHelper(getClass())
148 .add("maxBandwidth", maxBandwidth).toString();
149 }
Priyanka B02040732015-11-29 11:30:29 +0530150
151 @Override
152 public int compareTo(Object o) {
153 // TODO Auto-generated method stub
154 return 0;
155 }
Thejaswi N Kbbbefca2015-11-12 00:11:21 +0530156}