blob: 5721cf6c0ccdf55839f974693f7456f18715b32b [file] [log] [blame]
Thejaswi N Kf58916e2015-10-15 16:30:16 +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
18import java.util.Objects;
19
20import org.jboss.netty.buffer.ChannelBuffer;
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053021import org.onosproject.bgpio.exceptions.BgpParseException;
22import org.onosproject.bgpio.types.BgpErrorType;
23import org.onosproject.bgpio.types.BgpValueType;
Thejaswi N Kf58916e2015-10-15 16:30:16 +053024import org.onosproject.bgpio.util.Validation;
25import org.slf4j.Logger;
26import org.slf4j.LoggerFactory;
27
28import com.google.common.base.MoreObjects;
29
30/**
31 * Implements BGP link IGP metric attribute.
32 */
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053033public class BgpLinkAttrIgpMetric implements BgpValueType {
Thejaswi N Kf58916e2015-10-15 16:30:16 +053034
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 VH5dd8dbe2015-11-26 13:22:18 +053078 * @throws BgpParseException while parsing BgpLinkAttrIgpMetric
Thejaswi N Kf58916e2015-10-15 16:30:16 +053079 */
80 public static BgpLinkAttrIgpMetric read(ChannelBuffer cb)
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053081 throws BgpParseException {
Thejaswi N Kf58916e2015-10-15 16:30:16 +053082
83 short linkigp;
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 VH5dd8dbe2015-11-26 13:22:18 +053091 Validation.validateLen(BgpErrorType.UPDATE_MESSAGE_ERROR,
92 BgpErrorType.ATTRIBUTE_LENGTH_ERROR,
Thejaswi N Kf58916e2015-10-15 16:30:16 +053093 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();
108 igpMetric = (igpMetric << 16) | linkigp;
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 B02040732015-11-29 11:30:29 +0530172
173 @Override
174 public int compareTo(Object o) {
175 // TODO Auto-generated method stub
176 return 0;
177 }
Thejaswi N Kf58916e2015-10-15 16:30:16 +0530178}