Thejaswi N K
Committed by Gerrit Code Review

[onos-2603] - Implements Max link bandwidth

Change-Id: I3861931b62b60518962c528b3f47a464cf08c314
1 +/*
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 + */
16 +package org.onosproject.bgpio.types.attr;
17 +
18 +import java.util.Objects;
19 +
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;
27 +
28 +import com.google.common.base.MoreObjects;
29 +
30 +/**
31 + * Implements BGP attribute Max Link bandwidth.
32 + */
33 +public final class BgpLinkAttrMaxLinkBandwidth implements BGPValueType {
34 +
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
75 + * @throws BGPParseException while parsing BgpLinkAttrMaxLinkBandwidth
76 + */
77 + public static BgpLinkAttrMaxLinkBandwidth read(ChannelBuffer cb, short type)
78 + throws BGPParseException {
79 + float maxBandwidth;
80 + short lsAttrLength = cb.readShort();
81 +
82 + if ((lsAttrLength != MAX_BANDWIDTH_LEN)
83 + || (cb.readableBytes() < lsAttrLength)) {
84 + Validation.validateLen(BGPErrorType.UPDATE_MESSAGE_ERROR,
85 + BGPErrorType.ATTRIBUTE_LENGTH_ERROR,
86 + 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 + }
150 +}
1 +/*
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 + */
16 +package org.onosproject.bgp;
17 +
18 +import org.junit.Test;
19 +import org.onosproject.bgpio.types.attr.BgpLinkAttrMaxLinkBandwidth;
20 +
21 +import com.google.common.testing.EqualsTester;
22 +
23 +/**
24 + * Test for BGP link max bandwidth attribute.
25 + */
26 +public class BgpLinkAttrMaxLinkBandwidthTest {
27 + private final float val = 0x010203;
28 + private final short valLen = 3;
29 + private final float val1 = 0x01020304;
30 + private final short val1Len = 4;
31 +
32 + private final BgpLinkAttrMaxLinkBandwidth data = BgpLinkAttrMaxLinkBandwidth
33 + .of(val, valLen);
34 + private final BgpLinkAttrMaxLinkBandwidth sameAsData = BgpLinkAttrMaxLinkBandwidth
35 + .of(val, valLen);
36 + private final BgpLinkAttrMaxLinkBandwidth diffData = BgpLinkAttrMaxLinkBandwidth
37 + .of(val1, val1Len);
38 +
39 + @Test
40 + public void basics() {
41 + new EqualsTester().addEqualityGroup(data, sameAsData)
42 + .addEqualityGroup(diffData).testEquals();
43 + }
44 +}
...\ No newline at end of file ...\ No newline at end of file