blob: c358fc5d6f443f2d36957f29fdfd763e18374a4c [file] [log] [blame]
Thejaswi N Kbbbefca2015-11-12 00:11:21 +05301/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2015-present Open Networking Foundation
Thejaswi N Kbbbefca2015-11-12 00:11:21 +05303 *
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
Ray Milkey9c9cde42018-01-12 14:22:06 -080035 private static final Logger log = LoggerFactory
Thejaswi N Kbbbefca2015-11-12 00:11:21 +053036 .getLogger(BgpLinkAttrMaxLinkBandwidth.class);
37
38 public static final int MAX_BANDWIDTH_LEN = 4;
39 public static final int NO_OF_BITS = 8;
40
Ray Milkeye4afdb52017-04-05 09:42:04 -070041 private short type;
Thejaswi N Kbbbefca2015-11-12 00:11:21 +053042
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 */
Priyanka B658aa6982016-05-27 21:34:49 +053099 public float linkAttrMaxLinkBandwidth() {
Thejaswi N Kbbbefca2015-11-12 00:11:21 +0530100 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) {
Thejaswi N Kbbbefca2015-11-12 00:11:21 +0530110
111 return Float.intBitsToFloat(iVal);
112 }
113
114 @Override
115 public short getType() {
116 return this.type;
117 }
118
119 @Override
120 public int hashCode() {
121 return Objects.hash(maxBandwidth);
122 }
123
124 @Override
125 public boolean equals(Object obj) {
126 if (this == obj) {
127 return true;
128 }
129
130 if (obj instanceof BgpLinkAttrMaxLinkBandwidth) {
131 BgpLinkAttrMaxLinkBandwidth other = (BgpLinkAttrMaxLinkBandwidth) obj;
132 return Objects.equals(maxBandwidth, other.maxBandwidth);
133 }
134 return false;
135 }
136
137 @Override
138 public int write(ChannelBuffer cb) {
139 // TODO This will be implemented in the next version
140 return 0;
141 }
142
143 @Override
144 public String toString() {
145 return MoreObjects.toStringHelper(getClass())
146 .add("maxBandwidth", maxBandwidth).toString();
147 }
Priyanka B02040732015-11-29 11:30:29 +0530148
149 @Override
150 public int compareTo(Object o) {
151 // TODO Auto-generated method stub
152 return 0;
153 }
Thejaswi N Kbbbefca2015-11-12 00:11:21 +0530154}