Thejaswi N K | 73b79a6 | 2015-10-13 21:59:00 +0530 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Laboratory |
Thejaswi N K | 73b79a6 | 2015-10-13 21:59:00 +0530 | [diff] [blame] | 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 | |
Thejaswi N K | d68d20d | 2015-11-20 21:08:38 +0530 | [diff] [blame] | 18 | import java.util.ArrayList; |
| 19 | import java.util.List; |
Thejaswi N K | 73b79a6 | 2015-10-13 21:59:00 +0530 | [diff] [blame] | 20 | import java.util.Objects; |
| 21 | |
| 22 | import org.jboss.netty.buffer.ChannelBuffer; |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 23 | import org.onosproject.bgpio.exceptions.BgpParseException; |
| 24 | import org.onosproject.bgpio.types.BgpErrorType; |
| 25 | import org.onosproject.bgpio.types.BgpValueType; |
Thejaswi N K | 73b79a6 | 2015-10-13 21:59:00 +0530 | [diff] [blame] | 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 | */ |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 35 | public class BgpLinkAttrUnRsrvdLinkBandwidth implements BgpValueType { |
Thejaswi N K | 73b79a6 | 2015-10-13 21:59:00 +0530 | [diff] [blame] | 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 */ |
Thejaswi N K | d68d20d | 2015-11-20 21:08:38 +0530 | [diff] [blame] | 47 | private List<Float> maxUnResBandwidth = new ArrayList<Float>(); |
Thejaswi N K | 73b79a6 | 2015-10-13 21:59:00 +0530 | [diff] [blame] | 48 | |
| 49 | /** |
| 50 | * Constructor to initialize the values. |
| 51 | * |
| 52 | * @param maxUnResBandwidth Maximum Unreserved bandwidth |
| 53 | * @param sType returns the tag value |
| 54 | */ |
Thejaswi N K | d68d20d | 2015-11-20 21:08:38 +0530 | [diff] [blame] | 55 | public BgpLinkAttrUnRsrvdLinkBandwidth(List<Float> maxUnResBandwidth, |
| 56 | short sType) { |
| 57 | this.maxUnResBandwidth = maxUnResBandwidth; |
Thejaswi N K | 73b79a6 | 2015-10-13 21:59:00 +0530 | [diff] [blame] | 58 | this.sType = sType; |
| 59 | } |
| 60 | |
| 61 | /** |
Thejaswi N K | d68d20d | 2015-11-20 21:08:38 +0530 | [diff] [blame] | 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 | /** |
Thejaswi N K | 73b79a6 | 2015-10-13 21:59:00 +0530 | [diff] [blame] | 73 | * Reads the BGP link attributes of Maximum link bandwidth. |
| 74 | * |
| 75 | * @param cb Channel buffer |
Bharat saraswal | 6c88695 | 2015-12-11 01:43:11 +0530 | [diff] [blame] | 76 | * @param sType returns the tag value |
Thejaswi N K | 73b79a6 | 2015-10-13 21:59:00 +0530 | [diff] [blame] | 77 | * @return object of type BgpLinkAttrMaxLinkBandwidth |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 78 | * @throws BgpParseException while parsing BgpLinkAttrMaxLinkBandwidth |
Thejaswi N K | 73b79a6 | 2015-10-13 21:59:00 +0530 | [diff] [blame] | 79 | */ |
| 80 | public static BgpLinkAttrUnRsrvdLinkBandwidth read(ChannelBuffer cb, |
| 81 | short sType) |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 82 | throws BgpParseException { |
Thejaswi N K | d68d20d | 2015-11-20 21:08:38 +0530 | [diff] [blame] | 83 | ArrayList<Float> maxUnResBandwidth = new ArrayList<Float>(); |
| 84 | float tmp; |
Thejaswi N K | 73b79a6 | 2015-10-13 21:59:00 +0530 | [diff] [blame] | 85 | short lsAttrLength = cb.readShort(); |
| 86 | |
| 87 | if ((lsAttrLength != MAX_BANDWIDTH_LEN * NO_OF_PRIORITY) |
| 88 | || (cb.readableBytes() < lsAttrLength)) { |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 89 | Validation.validateLen(BgpErrorType.UPDATE_MESSAGE_ERROR, |
| 90 | BgpErrorType.ATTRIBUTE_LENGTH_ERROR, |
Thejaswi N K | 73b79a6 | 2015-10-13 21:59:00 +0530 | [diff] [blame] | 91 | lsAttrLength); |
| 92 | } |
| 93 | |
Thejaswi N K | 73b79a6 | 2015-10-13 21:59:00 +0530 | [diff] [blame] | 94 | for (int i = 0; i < NO_OF_PRIORITY; i++) { |
Thejaswi N K | d68d20d | 2015-11-20 21:08:38 +0530 | [diff] [blame] | 95 | tmp = ieeeToFloatRead(cb.readInt()) * NO_OF_BITS; |
| 96 | maxUnResBandwidth.add(new Float(tmp)); |
Thejaswi N K | 73b79a6 | 2015-10-13 21:59:00 +0530 | [diff] [blame] | 97 | } |
| 98 | |
Thejaswi N K | d68d20d | 2015-11-20 21:08:38 +0530 | [diff] [blame] | 99 | return BgpLinkAttrUnRsrvdLinkBandwidth.of(maxUnResBandwidth, sType); |
Thejaswi N K | 73b79a6 | 2015-10-13 21:59:00 +0530 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | /** |
| 103 | * Returns maximum unreserved bandwidth. |
| 104 | * |
| 105 | * @return unreserved bandwidth. |
| 106 | */ |
Thejaswi N K | d68d20d | 2015-11-20 21:08:38 +0530 | [diff] [blame] | 107 | public List<Float> getLinkAttrUnRsrvdLinkBandwidth() { |
Thejaswi N K | 73b79a6 | 2015-10-13 21:59:00 +0530 | [diff] [blame] | 108 | 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) { |
Thejaswi N K | 73b79a6 | 2015-10-13 21:59:00 +0530 | [diff] [blame] | 118 | |
| 119 | return Float.intBitsToFloat(iVal); |
| 120 | } |
| 121 | |
| 122 | @Override |
| 123 | public short getType() { |
| 124 | return this.sType; |
| 125 | } |
| 126 | |
| 127 | @Override |
| 128 | public int hashCode() { |
| 129 | return Objects.hash(maxUnResBandwidth); |
| 130 | } |
| 131 | |
| 132 | @Override |
| 133 | public boolean equals(Object obj) { |
| 134 | if (this == obj) { |
| 135 | return true; |
| 136 | } |
| 137 | |
| 138 | if (obj instanceof BgpLinkAttrUnRsrvdLinkBandwidth) { |
| 139 | BgpLinkAttrUnRsrvdLinkBandwidth other = (BgpLinkAttrUnRsrvdLinkBandwidth) obj; |
| 140 | return Objects.equals(maxUnResBandwidth, other.maxUnResBandwidth); |
| 141 | } |
| 142 | return false; |
| 143 | } |
| 144 | |
| 145 | @Override |
| 146 | public int write(ChannelBuffer cb) { |
| 147 | // TODO This will be implemented in the next version |
| 148 | return 0; |
| 149 | } |
| 150 | |
| 151 | @Override |
| 152 | public String toString() { |
| 153 | return MoreObjects.toStringHelper(getClass()).omitNullValues() |
| 154 | .add("maxUnResBandwidth", maxUnResBandwidth).toString(); |
| 155 | } |
Priyanka B | 0204073 | 2015-11-29 11:30:29 +0530 | [diff] [blame] | 156 | |
| 157 | @Override |
| 158 | public int compareTo(Object o) { |
| 159 | // TODO Auto-generated method stub |
| 160 | return 0; |
| 161 | } |
Thejaswi N K | 73b79a6 | 2015-10-13 21:59:00 +0530 | [diff] [blame] | 162 | } |