1ae7ecc52d103aa328dcb21b84932c26a4458a3e
[onosfw.git] /
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
133     @Override
134     public int compareTo(Object o) {
135         // TODO Auto-generated method stub
136         return 0;
137     }
138 }