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