blob: a7e53a5959e7c400c0cacfd1329e50a498719e8f [file] [log] [blame]
Thejaswi N K0fc61342015-11-11 23:47:25 +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 K0fc61342015-11-11 23:47:25 +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 state Default TE metric link attribute.
32 */
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053033public class BgpLinkAttrTeDefaultMetric implements BgpValueType {
Thejaswi N K0fc61342015-11-11 23:47:25 +053034
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
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053069 * @throws BgpParseException while parsing BgpLinkAttrTeDefaultMetric
Thejaswi N K0fc61342015-11-11 23:47:25 +053070 */
71 public static BgpLinkAttrTeDefaultMetric read(ChannelBuffer cb)
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053072 throws BgpParseException {
Thejaswi N K0fc61342015-11-11 23:47:25 +053073 int linkTeMetric;
74
75 short lsAttrLength = cb.readShort();
76
77 if ((lsAttrLength != TE_DATA_LEN)
78 || (cb.readableBytes() < lsAttrLength)) {
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053079 Validation.validateLen(BgpErrorType.UPDATE_MESSAGE_ERROR,
80 BgpErrorType.ATTRIBUTE_LENGTH_ERROR,
Thejaswi N K0fc61342015-11-11 23:47:25 +053081 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}