Thejaswi N K
Committed by ThejaswiNK-Huawei

[onos-2603] - Implements TE default metric

Change-Id: I3d7eb016d7840cb2b556e42bde8c407de6af99b3
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 state Default TE metric link attribute.
32 + */
33 +public class BgpLinkAttrTeDefaultMetric implements BGPValueType {
34 +
35 + protected static final Logger log = LoggerFactory
36 + .getLogger(BgpLinkAttrTeDefaultMetric.class);
37 +
38 + public static final int ATTRLINK_TEDEFAULTMETRIC = 1092;
39 + public static final int TE_DATA_LEN = 4;
40 +
41 + /* TE Default Metric */
42 + private int linkTeMetric;
43 +
44 + /**
45 + * Constructor to initialize the value.
46 + *
47 + * @param linkTeMetric TE default metric
48 + *
49 + */
50 + public BgpLinkAttrTeDefaultMetric(int linkTeMetric) {
51 + this.linkTeMetric = linkTeMetric;
52 + }
53 +
54 + /**
55 + * Returns object of this class with specified values.
56 + *
57 + * @param linkTeMetric TE default metric
58 + * @return object of BgpLinkAttrTeDefaultMetric
59 + */
60 + public static BgpLinkAttrTeDefaultMetric of(final int linkTeMetric) {
61 + return new BgpLinkAttrTeDefaultMetric(linkTeMetric);
62 + }
63 +
64 + /**
65 + * Reads the BGP link attributes of TE default metric.
66 + *
67 + * @param cb Channel buffer
68 + * @return object of type BgpLinkAttrTeDefaultMetric
69 + * @throws BGPParseException while parsing BgpLinkAttrTeDefaultMetric
70 + */
71 + public static BgpLinkAttrTeDefaultMetric read(ChannelBuffer cb)
72 + throws BGPParseException {
73 + int linkTeMetric;
74 +
75 + short lsAttrLength = cb.readShort();
76 +
77 + if ((lsAttrLength != TE_DATA_LEN)
78 + || (cb.readableBytes() < lsAttrLength)) {
79 + Validation.validateLen(BGPErrorType.UPDATE_MESSAGE_ERROR,
80 + BGPErrorType.ATTRIBUTE_LENGTH_ERROR,
81 + lsAttrLength);
82 + }
83 +
84 + linkTeMetric = cb.readInt();
85 +
86 + return new BgpLinkAttrTeDefaultMetric(linkTeMetric);
87 + }
88 +
89 + /**
90 + * Returns the TE default metrics.
91 + *
92 + * @return link default metric
93 + */
94 + public int attrLinkDefTeMetric() {
95 + return linkTeMetric;
96 + }
97 +
98 + @Override
99 + public short getType() {
100 + return ATTRLINK_TEDEFAULTMETRIC;
101 + }
102 +
103 + @Override
104 + public int hashCode() {
105 + return Objects.hash(linkTeMetric);
106 + }
107 +
108 + @Override
109 + public boolean equals(Object obj) {
110 + if (this == obj) {
111 + return true;
112 + }
113 +
114 + if (obj instanceof BgpLinkAttrTeDefaultMetric) {
115 + BgpLinkAttrTeDefaultMetric other = (BgpLinkAttrTeDefaultMetric) obj;
116 + return Objects.equals(linkTeMetric, other.linkTeMetric);
117 + }
118 + return false;
119 + }
120 +
121 + @Override
122 + public int write(ChannelBuffer cb) {
123 + // TODO This will be implemented in the next version
124 + return 0;
125 + }
126 +
127 + @Override
128 + public String toString() {
129 + return MoreObjects.toStringHelper(getClass())
130 + .add("linkTEMetric", linkTeMetric).toString();
131 + }
132 +}
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.bgp;
17 +
18 +import org.junit.Test;
19 +import org.onosproject.bgpio.types.attr.BgpLinkAttrTeDefaultMetric;
20 +
21 +import com.google.common.testing.EqualsTester;
22 +
23 +/**
24 + * Test for BGP link TE default metric attribute.
25 + */
26 +public class BgpLinkAttrTeDefaultMetricTest {
27 + private final int val = 0x010203;
28 + private final int val1 = 0x01020304;
29 +
30 + private final BgpLinkAttrTeDefaultMetric data = BgpLinkAttrTeDefaultMetric
31 + .of(val);
32 + private final BgpLinkAttrTeDefaultMetric sameAsData = BgpLinkAttrTeDefaultMetric
33 + .of(val);
34 + private final BgpLinkAttrTeDefaultMetric diffData = BgpLinkAttrTeDefaultMetric
35 + .of(val1);
36 +
37 + @Test
38 + public void basics() {
39 + new EqualsTester().addEqualityGroup(data, sameAsData)
40 + .addEqualityGroup(diffData).testEquals();
41 + }
42 +}
...\ No newline at end of file ...\ No newline at end of file