blob: 6e9f9ce3b6b5758f9098fb64fa32a9d63613b0e4 [file] [log] [blame]
Thejaswi NK5b9278b2015-10-12 16:59:14 +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.Arrays;
19
20import org.jboss.netty.buffer.ChannelBuffer;
21import org.onosproject.bgpio.exceptions.BGPParseException;
22import org.onosproject.bgpio.types.BGPErrorType;
23import org.onosproject.bgpio.types.BGPValueType;
24import 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 attribute ISIS Area Identifier.
32 */
33public 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}