Thejaswi N K
Committed by Gerrit Code Review

[Emu][onos-2603] - Implement LinkState Link attributes-Link IGP Metric

Change-Id: I700922df22f5379679b3a5859cc3533f5532ae5a
1 +/*
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 + */
16 +package org.onosproject.bgpio.types.attr;
17 +
18 +import java.util.Objects;
19 +
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;
27 +
28 +import com.google.common.base.MoreObjects;
29 +
30 +/**
31 + * Implements BGP link IGP metric attribute.
32 + */
33 +public class BgpLinkAttrIgpMetric implements BGPValueType {
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
78 + * @throws BGPParseException while parsing BgpLinkAttrIgpMetric
79 + */
80 + public static BgpLinkAttrIgpMetric read(ChannelBuffer cb)
81 + throws BGPParseException {
82 +
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) {
91 + Validation.validateLen(BGPErrorType.UPDATE_MESSAGE_ERROR,
92 + BGPErrorType.ATTRIBUTE_LENGTH_ERROR,
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();
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 + }
172 +}