Thejaswi N K | f58916e | 2015-10-15 16:30:16 +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 | f58916e | 2015-10-15 16:30:16 +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 | f58916e | 2015-10-15 16:30:16 +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 link IGP metric attribute. |
| 32 | */ |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 33 | public class BgpLinkAttrIgpMetric implements BgpValueType { |
Thejaswi N K | f58916e | 2015-10-15 16:30:16 +0530 | [diff] [blame] | 34 | |
| 35 | protected static final Logger log = LoggerFactory |
| 36 | .getLogger(BgpLinkAttrIgpMetric.class); |
| 37 | |
| 38 | public static final int ATTRLINK_IGPMETRIC = 1095; |
| 39 | public static final int ATTRLINK_MAX_LEN = 3; |
| 40 | |
| 41 | /* Variable metric length based on protocol */ |
| 42 | public static final int ISIS_SMALL_METRIC = 1; |
| 43 | public static final int OSPF_LINK_METRIC = 2; |
| 44 | public static final int ISIS_WIDE_METRIC = 3; |
| 45 | |
| 46 | /* IGP Metric */ |
| 47 | private final int igpMetric; |
| 48 | private final int igpMetricLen; |
| 49 | |
| 50 | /** |
| 51 | * Constructor to initialize the value. |
| 52 | * |
| 53 | * @param igpMetric 3 byte IGP metric data. |
| 54 | * @param igpMetricLen length of IGP metric data. |
| 55 | */ |
| 56 | public BgpLinkAttrIgpMetric(final int igpMetric, final int igpMetricLen) { |
| 57 | this.igpMetric = igpMetric; |
| 58 | this.igpMetricLen = igpMetricLen; |
| 59 | } |
| 60 | |
| 61 | /** |
| 62 | * Returns object of this class with specified values. |
| 63 | * |
| 64 | * @param igpMetric 3 byte IGP metric data. |
| 65 | * @param igpMetricLen length of IGP metric data. |
| 66 | * @return object of BgpLinkAttrIgpMetric |
| 67 | */ |
| 68 | public static BgpLinkAttrIgpMetric of(final int igpMetric, |
| 69 | final int igpMetricLen) { |
| 70 | return new BgpLinkAttrIgpMetric(igpMetric, igpMetricLen); |
| 71 | } |
| 72 | |
| 73 | /** |
| 74 | * Reads the BGP link attributes IGP Metric. |
| 75 | * |
| 76 | * @param cb Channel buffer |
| 77 | * @return object of type BgpLinkAttrIgpMetric |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 78 | * @throws BgpParseException while parsing BgpLinkAttrIgpMetric |
Thejaswi N K | f58916e | 2015-10-15 16:30:16 +0530 | [diff] [blame] | 79 | */ |
| 80 | public static BgpLinkAttrIgpMetric read(ChannelBuffer cb) |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 81 | throws BgpParseException { |
Thejaswi N K | f58916e | 2015-10-15 16:30:16 +0530 | [diff] [blame] | 82 | |
Thejaswi N K | 55204f8 | 2015-12-15 21:50:12 +0530 | [diff] [blame] | 83 | int linkigp; |
Thejaswi N K | f58916e | 2015-10-15 16:30:16 +0530 | [diff] [blame] | 84 | int igpMetric = 0; |
| 85 | int igpMetricLen = 0; |
| 86 | |
| 87 | short lsAttrLength = cb.readShort(); |
| 88 | |
| 89 | if (cb.readableBytes() < lsAttrLength |
| 90 | || lsAttrLength > ATTRLINK_MAX_LEN) { |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 91 | Validation.validateLen(BgpErrorType.UPDATE_MESSAGE_ERROR, |
| 92 | BgpErrorType.ATTRIBUTE_LENGTH_ERROR, |
Thejaswi N K | f58916e | 2015-10-15 16:30:16 +0530 | [diff] [blame] | 93 | lsAttrLength); |
| 94 | } |
| 95 | |
| 96 | switch (lsAttrLength) { |
| 97 | case ISIS_SMALL_METRIC: |
| 98 | igpMetric = cb.readByte(); |
| 99 | igpMetricLen = ISIS_SMALL_METRIC; |
| 100 | break; |
| 101 | case OSPF_LINK_METRIC: |
| 102 | igpMetric = cb.readShort(); |
| 103 | igpMetricLen = OSPF_LINK_METRIC; |
| 104 | break; |
| 105 | case ISIS_WIDE_METRIC: |
| 106 | linkigp = cb.readShort(); |
| 107 | igpMetric = cb.readByte(); |
Thejaswi N K | 55204f8 | 2015-12-15 21:50:12 +0530 | [diff] [blame] | 108 | igpMetric = (linkigp << 8) | igpMetric; |
Thejaswi N K | f58916e | 2015-10-15 16:30:16 +0530 | [diff] [blame] | 109 | igpMetricLen = ISIS_WIDE_METRIC; |
| 110 | break; |
| 111 | default: // validation is already in place |
| 112 | break; |
| 113 | } |
| 114 | |
| 115 | return BgpLinkAttrIgpMetric.of(igpMetric, igpMetricLen); |
| 116 | } |
| 117 | |
| 118 | /** |
| 119 | * Returns the variable length IGP metric data. |
| 120 | * |
| 121 | * @return IGP metric data |
| 122 | */ |
| 123 | public int attrLinkIgpMetric() { |
| 124 | return igpMetric; |
| 125 | } |
| 126 | |
| 127 | /** |
| 128 | * Returns IGP metric data length. |
| 129 | * |
| 130 | * @return IGP metric length |
| 131 | */ |
| 132 | public int attrLinkIgpMetricLength() { |
| 133 | return igpMetricLen; |
| 134 | } |
| 135 | |
| 136 | @Override |
| 137 | public short getType() { |
| 138 | return ATTRLINK_IGPMETRIC; |
| 139 | } |
| 140 | |
| 141 | @Override |
| 142 | public int hashCode() { |
| 143 | return Objects.hash(igpMetric, igpMetricLen); |
| 144 | } |
| 145 | |
| 146 | @Override |
| 147 | public boolean equals(Object obj) { |
| 148 | if (this == obj) { |
| 149 | return true; |
| 150 | } |
| 151 | |
| 152 | if (obj instanceof BgpLinkAttrIgpMetric) { |
| 153 | BgpLinkAttrIgpMetric other = (BgpLinkAttrIgpMetric) obj; |
| 154 | return Objects.equals(igpMetric, other.igpMetric) |
| 155 | && Objects.equals(igpMetricLen, other.igpMetricLen); |
| 156 | } |
| 157 | return false; |
| 158 | } |
| 159 | |
| 160 | @Override |
| 161 | public int write(ChannelBuffer cb) { |
| 162 | // TODO This will be implemented in the next version |
| 163 | return 0; |
| 164 | } |
| 165 | |
| 166 | @Override |
| 167 | public String toString() { |
| 168 | return MoreObjects.toStringHelper(getClass()) |
| 169 | .add("igpMetric", igpMetric).add("igpMetricLen", igpMetricLen) |
| 170 | .toString(); |
| 171 | } |
Priyanka B | 0204073 | 2015-11-29 11:30:29 +0530 | [diff] [blame] | 172 | |
| 173 | @Override |
| 174 | public int compareTo(Object o) { |
| 175 | // TODO Auto-generated method stub |
| 176 | return 0; |
| 177 | } |
Thejaswi N K | f58916e | 2015-10-15 16:30:16 +0530 | [diff] [blame] | 178 | } |