Thejaswi N K | bbbefca | 2015-11-12 00:11:21 +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 | bbbefca | 2015-11-12 00:11:21 +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 | |
| 18 | import java.util.Objects; |
| 19 | |
| 20 | import org.jboss.netty.buffer.ChannelBuffer; |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 21 | import org.onosproject.bgpio.exceptions.BgpParseException; |
| 22 | import org.onosproject.bgpio.types.BgpErrorType; |
| 23 | import org.onosproject.bgpio.types.BgpValueType; |
Thejaswi N K | bbbefca | 2015-11-12 00:11:21 +0530 | [diff] [blame] | 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 | */ |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 33 | public final class BgpLinkAttrMaxLinkBandwidth implements BgpValueType { |
Thejaswi N K | bbbefca | 2015-11-12 00:11:21 +0530 | [diff] [blame] | 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 |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 75 | * @throws BgpParseException while parsing BgpLinkAttrMaxLinkBandwidth |
Thejaswi N K | bbbefca | 2015-11-12 00:11:21 +0530 | [diff] [blame] | 76 | */ |
| 77 | public static BgpLinkAttrMaxLinkBandwidth read(ChannelBuffer cb, short type) |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 78 | throws BgpParseException { |
Thejaswi N K | bbbefca | 2015-11-12 00:11:21 +0530 | [diff] [blame] | 79 | float maxBandwidth; |
| 80 | short lsAttrLength = cb.readShort(); |
| 81 | |
| 82 | if ((lsAttrLength != MAX_BANDWIDTH_LEN) |
| 83 | || (cb.readableBytes() < lsAttrLength)) { |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 84 | Validation.validateLen(BgpErrorType.UPDATE_MESSAGE_ERROR, |
| 85 | BgpErrorType.ATTRIBUTE_LENGTH_ERROR, |
Thejaswi N K | bbbefca | 2015-11-12 00:11:21 +0530 | [diff] [blame] | 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 | */ |
Priyanka B | 658aa698 | 2016-05-27 21:34:49 +0530 | [diff] [blame] | 99 | public float linkAttrMaxLinkBandwidth() { |
Thejaswi N K | bbbefca | 2015-11-12 00:11:21 +0530 | [diff] [blame] | 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) { |
Thejaswi N K | bbbefca | 2015-11-12 00:11:21 +0530 | [diff] [blame] | 110 | |
| 111 | return Float.intBitsToFloat(iVal); |
| 112 | } |
| 113 | |
| 114 | @Override |
| 115 | public short getType() { |
| 116 | return this.type; |
| 117 | } |
| 118 | |
| 119 | @Override |
| 120 | public int hashCode() { |
| 121 | return Objects.hash(maxBandwidth); |
| 122 | } |
| 123 | |
| 124 | @Override |
| 125 | public boolean equals(Object obj) { |
| 126 | if (this == obj) { |
| 127 | return true; |
| 128 | } |
| 129 | |
| 130 | if (obj instanceof BgpLinkAttrMaxLinkBandwidth) { |
| 131 | BgpLinkAttrMaxLinkBandwidth other = (BgpLinkAttrMaxLinkBandwidth) obj; |
| 132 | return Objects.equals(maxBandwidth, other.maxBandwidth); |
| 133 | } |
| 134 | return false; |
| 135 | } |
| 136 | |
| 137 | @Override |
| 138 | public int write(ChannelBuffer cb) { |
| 139 | // TODO This will be implemented in the next version |
| 140 | return 0; |
| 141 | } |
| 142 | |
| 143 | @Override |
| 144 | public String toString() { |
| 145 | return MoreObjects.toStringHelper(getClass()) |
| 146 | .add("maxBandwidth", maxBandwidth).toString(); |
| 147 | } |
Priyanka B | 0204073 | 2015-11-29 11:30:29 +0530 | [diff] [blame] | 148 | |
| 149 | @Override |
| 150 | public int compareTo(Object o) { |
| 151 | // TODO Auto-generated method stub |
| 152 | return 0; |
| 153 | } |
Thejaswi N K | bbbefca | 2015-11-12 00:11:21 +0530 | [diff] [blame] | 154 | } |