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