2 * Copyright 2015 Open Networking Laboratory
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package org.onosproject.bgpio.types.attr;
18 import java.util.Objects;
20 import org.jboss.netty.buffer.ChannelBuffer;
21 import org.onosproject.bgpio.exceptions.BgpParseException;
22 import org.onosproject.bgpio.types.BgpErrorType;
23 import org.onosproject.bgpio.types.BgpValueType;
24 import org.onosproject.bgpio.util.Validation;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
28 import com.google.common.base.MoreObjects;
31 * Implements BGP link IGP metric attribute.
33 public class BgpLinkAttrIgpMetric implements BgpValueType {
35 protected static final Logger log = LoggerFactory
36 .getLogger(BgpLinkAttrIgpMetric.class);
38 public static final int ATTRLINK_IGPMETRIC = 1095;
39 public static final int ATTRLINK_MAX_LEN = 3;
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;
47 private final int igpMetric;
48 private final int igpMetricLen;
51 * Constructor to initialize the value.
53 * @param igpMetric 3 byte IGP metric data.
54 * @param igpMetricLen length of IGP metric data.
56 public BgpLinkAttrIgpMetric(final int igpMetric, final int igpMetricLen) {
57 this.igpMetric = igpMetric;
58 this.igpMetricLen = igpMetricLen;
62 * Returns object of this class with specified values.
64 * @param igpMetric 3 byte IGP metric data.
65 * @param igpMetricLen length of IGP metric data.
66 * @return object of BgpLinkAttrIgpMetric
68 public static BgpLinkAttrIgpMetric of(final int igpMetric,
69 final int igpMetricLen) {
70 return new BgpLinkAttrIgpMetric(igpMetric, igpMetricLen);
74 * Reads the BGP link attributes IGP Metric.
76 * @param cb Channel buffer
77 * @return object of type BgpLinkAttrIgpMetric
78 * @throws BgpParseException while parsing BgpLinkAttrIgpMetric
80 public static BgpLinkAttrIgpMetric read(ChannelBuffer cb)
81 throws BgpParseException {
87 short lsAttrLength = cb.readShort();
89 if (cb.readableBytes() < lsAttrLength
90 || lsAttrLength > ATTRLINK_MAX_LEN) {
91 Validation.validateLen(BgpErrorType.UPDATE_MESSAGE_ERROR,
92 BgpErrorType.ATTRIBUTE_LENGTH_ERROR,
96 switch (lsAttrLength) {
97 case ISIS_SMALL_METRIC:
98 igpMetric = cb.readByte();
99 igpMetricLen = ISIS_SMALL_METRIC;
101 case OSPF_LINK_METRIC:
102 igpMetric = cb.readShort();
103 igpMetricLen = OSPF_LINK_METRIC;
105 case ISIS_WIDE_METRIC:
106 linkigp = cb.readShort();
107 igpMetric = cb.readByte();
108 igpMetric = (igpMetric << 16) | linkigp;
109 igpMetricLen = ISIS_WIDE_METRIC;
111 default: // validation is already in place
115 return BgpLinkAttrIgpMetric.of(igpMetric, igpMetricLen);
119 * Returns the variable length IGP metric data.
121 * @return IGP metric data
123 public int attrLinkIgpMetric() {
128 * Returns IGP metric data length.
130 * @return IGP metric length
132 public int attrLinkIgpMetricLength() {
137 public short getType() {
138 return ATTRLINK_IGPMETRIC;
142 public int hashCode() {
143 return Objects.hash(igpMetric, igpMetricLen);
147 public boolean equals(Object obj) {
152 if (obj instanceof BgpLinkAttrIgpMetric) {
153 BgpLinkAttrIgpMetric other = (BgpLinkAttrIgpMetric) obj;
154 return Objects.equals(igpMetric, other.igpMetric)
155 && Objects.equals(igpMetricLen, other.igpMetricLen);
161 public int write(ChannelBuffer cb) {
162 // TODO This will be implemented in the next version
167 public String toString() {
168 return MoreObjects.toStringHelper(getClass())
169 .add("igpMetric", igpMetric).add("igpMetricLen", igpMetricLen)
174 public int compareTo(Object o) {
175 // TODO Auto-generated method stub