Thejaswi N K
Committed by Gerrit Code Review

[Emu][onos-2603] - Implement LinkState Link attributes

Change-Id: I452675ab9e3cf884924212b7114659dfc9118ef3
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.Arrays;
19 +import java.util.Objects;
20 +
21 +import org.jboss.netty.buffer.ChannelBuffer;
22 +import org.onosproject.bgpio.exceptions.BGPParseException;
23 +import org.onosproject.bgpio.types.BGPErrorType;
24 +import org.onosproject.bgpio.types.BGPValueType;
25 +import org.onosproject.bgpio.util.Validation;
26 +import org.slf4j.Logger;
27 +import org.slf4j.LoggerFactory;
28 +
29 +import com.google.common.base.MoreObjects;
30 +
31 +/**
32 + * Implements BGP unreserved bandwidth attribute.
33 + */
34 +public class BgpLinkAttrUnRsrvdLinkBandwidth implements BGPValueType {
35 +
36 + protected static final Logger log = LoggerFactory
37 + .getLogger(BgpLinkAttrUnRsrvdLinkBandwidth.class);
38 +
39 + public static final int MAX_BANDWIDTH_LEN = 4;
40 + public static final int NO_OF_BITS = 8;
41 + public static final int NO_OF_PRIORITY = 8;
42 +
43 + public short sType;
44 +
45 + /* ISIS administrative group */
46 + private float[] maxUnResBandwidth;
47 +
48 + /**
49 + * Constructor to initialize the values.
50 + *
51 + * @param maxUnResBandwidth Maximum Unreserved bandwidth
52 + * @param sType returns the tag value
53 + */
54 + BgpLinkAttrUnRsrvdLinkBandwidth(float[] maxUnResBandwidth, short sType) {
55 + this.maxUnResBandwidth = Arrays.copyOf(maxUnResBandwidth,
56 + maxUnResBandwidth.length);
57 + this.sType = sType;
58 + }
59 +
60 + /**
61 + * Reads the BGP link attributes of Maximum link bandwidth.
62 + *
63 + * @param cb Channel buffer
64 + * @return object of type BgpLinkAttrMaxLinkBandwidth
65 + * @throws BGPParseException while parsing BgpLinkAttrMaxLinkBandwidth
66 + */
67 + public static BgpLinkAttrUnRsrvdLinkBandwidth read(ChannelBuffer cb,
68 + short sType)
69 + throws BGPParseException {
70 + float[] maxUnResBandwidth;
71 + short lsAttrLength = cb.readShort();
72 +
73 + if ((lsAttrLength != MAX_BANDWIDTH_LEN * NO_OF_PRIORITY)
74 + || (cb.readableBytes() < lsAttrLength)) {
75 + Validation.validateLen(BGPErrorType.UPDATE_MESSAGE_ERROR,
76 + BGPErrorType.ATTRIBUTE_LENGTH_ERROR,
77 + lsAttrLength);
78 + }
79 +
80 + maxUnResBandwidth = new float[NO_OF_PRIORITY];
81 + for (int i = 0; i < NO_OF_PRIORITY; i++) {
82 + maxUnResBandwidth[i] = ieeeToFloatRead(cb.readInt()) * NO_OF_BITS;
83 + }
84 +
85 + return new BgpLinkAttrUnRsrvdLinkBandwidth(maxUnResBandwidth, sType);
86 + }
87 +
88 + /**
89 + * Returns maximum unreserved bandwidth.
90 + *
91 + * @return unreserved bandwidth.
92 + */
93 + float[] getLinkAttrUnRsrvdLinkBandwidth() {
94 + return maxUnResBandwidth;
95 + }
96 +
97 + /**
98 + * Parse the IEEE floating point notation and returns it in normal float.
99 + *
100 + * @param iVal IEEE floating point number
101 + * @return normal float
102 + */
103 + static float ieeeToFloatRead(int iVal) {
104 + iVal = (((iVal & 0xFF) << 24) | ((iVal & 0xFF00) << 8)
105 + | ((iVal & 0xFF0000) >> 8) | ((iVal >> 24) & 0xFF));
106 +
107 + return Float.intBitsToFloat(iVal);
108 + }
109 +
110 + @Override
111 + public short getType() {
112 + return this.sType;
113 + }
114 +
115 + @Override
116 + public int hashCode() {
117 + return Objects.hash(maxUnResBandwidth);
118 + }
119 +
120 + @Override
121 + public boolean equals(Object obj) {
122 + if (this == obj) {
123 + return true;
124 + }
125 +
126 + if (obj instanceof BgpLinkAttrUnRsrvdLinkBandwidth) {
127 + BgpLinkAttrUnRsrvdLinkBandwidth other = (BgpLinkAttrUnRsrvdLinkBandwidth) obj;
128 + return Objects.equals(maxUnResBandwidth, other.maxUnResBandwidth);
129 + }
130 + return false;
131 + }
132 +
133 + @Override
134 + public int write(ChannelBuffer cb) {
135 + // TODO This will be implemented in the next version
136 + return 0;
137 + }
138 +
139 + @Override
140 + public String toString() {
141 + return MoreObjects.toStringHelper(getClass()).omitNullValues()
142 + .add("maxUnResBandwidth", maxUnResBandwidth).toString();
143 + }
144 +}