1886102c7bdf8297d81817f5e4bb3ed854258115
[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 prefix metric attribute.
32  */
33 public class BgpPrefixAttrMetric implements BgpValueType {
34
35     protected static final Logger log = LoggerFactory
36             .getLogger(BgpPrefixAttrMetric.class);
37
38     public static final int ATTR_PREFIX_METRIC = 1155;
39     public static final int ATTR_PREFIX_LEN = 4;
40
41     /* TE Default Metric */
42     private final int linkPfxMetric;
43
44     /**
45      * Constructor to initialize value.
46      *
47      * @param linkPfxMetric Prefix Metric
48      */
49     public BgpPrefixAttrMetric(int linkPfxMetric) {
50         this.linkPfxMetric = linkPfxMetric;
51     }
52
53     /**
54      * Returns object of this class with specified values.
55      *
56      * @param linkPfxMetric Prefix Metric
57      * @return object of BgpPrefixAttrMetric
58      */
59     public static BgpPrefixAttrMetric of(final int linkPfxMetric) {
60         return new BgpPrefixAttrMetric(linkPfxMetric);
61     }
62
63     /**
64      * Reads the Prefix Metric.
65      *
66      * @param cb ChannelBuffer
67      * @return object of BgpPrefixAttrMetric
68      * @throws BgpParseException while parsing BgpPrefixAttrMetric
69      */
70     public static BgpPrefixAttrMetric read(ChannelBuffer cb)
71             throws BgpParseException {
72         int linkPfxMetric;
73
74         short lsAttrLength = cb.readShort(); // 4 Bytes
75
76         if ((lsAttrLength != ATTR_PREFIX_LEN)
77                 || (cb.readableBytes() < lsAttrLength)) {
78             Validation.validateLen(BgpErrorType.UPDATE_MESSAGE_ERROR,
79                                    BgpErrorType.ATTRIBUTE_LENGTH_ERROR,
80                                    lsAttrLength);
81         }
82
83         linkPfxMetric = cb.readInt();
84
85         return BgpPrefixAttrMetric.of(linkPfxMetric);
86     }
87
88     /**
89      * Returns the Prefix Metric.
90      *
91      * @return Prefix Metric
92      */
93     public int attrPfxMetric() {
94         return linkPfxMetric;
95     }
96
97     @Override
98     public short getType() {
99         return ATTR_PREFIX_METRIC;
100     }
101
102     @Override
103     public int hashCode() {
104         return Objects.hash(linkPfxMetric);
105     }
106
107     @Override
108     public boolean equals(Object obj) {
109         if (this == obj) {
110             return true;
111         }
112
113         if (obj instanceof BgpPrefixAttrMetric) {
114             BgpPrefixAttrMetric other = (BgpPrefixAttrMetric) obj;
115             return Objects.equals(linkPfxMetric, other.linkPfxMetric);
116         }
117         return false;
118     }
119
120     @Override
121     public int write(ChannelBuffer cb) {
122         // TODO This will be implemented in the next version
123         return 0;
124     }
125
126     @Override
127     public String toString() {
128         return MoreObjects.toStringHelper(getClass())
129                 .add("linkPfxMetric", linkPfxMetric).toString();
130     }
131
132     @Override
133     public int compareTo(Object o) {
134         // TODO Auto-generated method stub
135         return 0;
136     }
137 }