blob: 5e87887aed25bb25437330d84dace628e0ab5852 [file] [log] [blame]
Thejaswi N K73b79a62015-10-13 21:59:00 +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
Thejaswi N Kd68d20d2015-11-20 21:08:38 +053018import java.util.ArrayList;
19import java.util.List;
Thejaswi N K73b79a62015-10-13 21:59:00 +053020import java.util.Objects;
21
22import org.jboss.netty.buffer.ChannelBuffer;
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053023import org.onosproject.bgpio.exceptions.BgpParseException;
24import org.onosproject.bgpio.types.BgpErrorType;
25import org.onosproject.bgpio.types.BgpValueType;
Thejaswi N K73b79a62015-10-13 21:59:00 +053026import org.onosproject.bgpio.util.Validation;
27import org.slf4j.Logger;
28import org.slf4j.LoggerFactory;
29
30import com.google.common.base.MoreObjects;
31
32/**
33 * Implements BGP unreserved bandwidth attribute.
34 */
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053035public class BgpLinkAttrUnRsrvdLinkBandwidth implements BgpValueType {
Thejaswi N K73b79a62015-10-13 21:59:00 +053036
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 */
Thejaswi N Kd68d20d2015-11-20 21:08:38 +053047 private List<Float> maxUnResBandwidth = new ArrayList<Float>();
Thejaswi N K73b79a62015-10-13 21:59:00 +053048
49 /**
50 * Constructor to initialize the values.
51 *
52 * @param maxUnResBandwidth Maximum Unreserved bandwidth
53 * @param sType returns the tag value
54 */
Thejaswi N Kd68d20d2015-11-20 21:08:38 +053055 public BgpLinkAttrUnRsrvdLinkBandwidth(List<Float> maxUnResBandwidth,
56 short sType) {
57 this.maxUnResBandwidth = maxUnResBandwidth;
Thejaswi N K73b79a62015-10-13 21:59:00 +053058 this.sType = sType;
59 }
60
61 /**
Thejaswi N Kd68d20d2015-11-20 21:08:38 +053062 * 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 /**
Thejaswi N K73b79a62015-10-13 21:59:00 +053073 * Reads the BGP link attributes of Maximum link bandwidth.
74 *
75 * @param cb Channel buffer
Bharat saraswal6c886952015-12-11 01:43:11 +053076 * @param sType returns the tag value
Thejaswi N K73b79a62015-10-13 21:59:00 +053077 * @return object of type BgpLinkAttrMaxLinkBandwidth
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053078 * @throws BgpParseException while parsing BgpLinkAttrMaxLinkBandwidth
Thejaswi N K73b79a62015-10-13 21:59:00 +053079 */
80 public static BgpLinkAttrUnRsrvdLinkBandwidth read(ChannelBuffer cb,
81 short sType)
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053082 throws BgpParseException {
Thejaswi N Kd68d20d2015-11-20 21:08:38 +053083 ArrayList<Float> maxUnResBandwidth = new ArrayList<Float>();
84 float tmp;
Thejaswi N K73b79a62015-10-13 21:59:00 +053085 short lsAttrLength = cb.readShort();
86
87 if ((lsAttrLength != MAX_BANDWIDTH_LEN * NO_OF_PRIORITY)
88 || (cb.readableBytes() < lsAttrLength)) {
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053089 Validation.validateLen(BgpErrorType.UPDATE_MESSAGE_ERROR,
90 BgpErrorType.ATTRIBUTE_LENGTH_ERROR,
Thejaswi N K73b79a62015-10-13 21:59:00 +053091 lsAttrLength);
92 }
93
Thejaswi N K73b79a62015-10-13 21:59:00 +053094 for (int i = 0; i < NO_OF_PRIORITY; i++) {
Thejaswi N Kd68d20d2015-11-20 21:08:38 +053095 tmp = ieeeToFloatRead(cb.readInt()) * NO_OF_BITS;
96 maxUnResBandwidth.add(new Float(tmp));
Thejaswi N K73b79a62015-10-13 21:59:00 +053097 }
98
Thejaswi N Kd68d20d2015-11-20 21:08:38 +053099 return BgpLinkAttrUnRsrvdLinkBandwidth.of(maxUnResBandwidth, sType);
Thejaswi N K73b79a62015-10-13 21:59:00 +0530100 }
101
102 /**
103 * Returns maximum unreserved bandwidth.
104 *
105 * @return unreserved bandwidth.
106 */
Thejaswi N Kd68d20d2015-11-20 21:08:38 +0530107 public List<Float> getLinkAttrUnRsrvdLinkBandwidth() {
Thejaswi N K73b79a62015-10-13 21:59:00 +0530108 return maxUnResBandwidth;
109 }
110
111 /**
112 * Parse the IEEE floating point notation and returns it in normal float.
113 *
114 * @param iVal IEEE floating point number
115 * @return normal float
116 */
117 static float ieeeToFloatRead(int iVal) {
118 iVal = (((iVal & 0xFF) << 24) | ((iVal & 0xFF00) << 8)
119 | ((iVal & 0xFF0000) >> 8) | ((iVal >> 24) & 0xFF));
120
121 return Float.intBitsToFloat(iVal);
122 }
123
124 @Override
125 public short getType() {
126 return this.sType;
127 }
128
129 @Override
130 public int hashCode() {
131 return Objects.hash(maxUnResBandwidth);
132 }
133
134 @Override
135 public boolean equals(Object obj) {
136 if (this == obj) {
137 return true;
138 }
139
140 if (obj instanceof BgpLinkAttrUnRsrvdLinkBandwidth) {
141 BgpLinkAttrUnRsrvdLinkBandwidth other = (BgpLinkAttrUnRsrvdLinkBandwidth) obj;
142 return Objects.equals(maxUnResBandwidth, other.maxUnResBandwidth);
143 }
144 return false;
145 }
146
147 @Override
148 public int write(ChannelBuffer cb) {
149 // TODO This will be implemented in the next version
150 return 0;
151 }
152
153 @Override
154 public String toString() {
155 return MoreObjects.toStringHelper(getClass()).omitNullValues()
156 .add("maxUnResBandwidth", maxUnResBandwidth).toString();
157 }
Priyanka B02040732015-11-29 11:30:29 +0530158
159 @Override
160 public int compareTo(Object o) {
161 // TODO Auto-generated method stub
162 return 0;
163 }
Thejaswi N K73b79a62015-10-13 21:59:00 +0530164}