blob: 11c99bad8d8a1ec6ffdc2b1848c68dd85549a8d3 [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;
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 NK5b9278b2015-10-12 16:59:14 +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 attribute node name.
32 */
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053033public class BgpAttrNodeName implements BgpValueType {
Thejaswi NK5b9278b2015-10-12 16:59:14 +053034
35 protected static final Logger log = LoggerFactory
36 .getLogger(BgpAttrNodeName.class);
37
38 public static final int ATTRNODE_NAME = 1026;
39
40 /* Node Name */
41 private byte[] nodeName;
42
43 /**
44 * Constructor to initialize value.
45 *
46 * @param nodeName node name
47 */
48 public BgpAttrNodeName(byte[] nodeName) {
49 this.nodeName = Arrays.copyOf(nodeName, nodeName.length);
50 }
51
52 /**
53 * Returns object of this class with specified values.
54 *
55 * @param nodeName node name
56 * @return object of BgpAttrNodeName
57 */
58 public static BgpAttrNodeName of(final byte[] nodeName) {
59 return new BgpAttrNodeName(nodeName);
60 }
61
62 /**
63 * Reads the LS attribute node name.
64 *
65 * @param cb ChannelBuffer
66 * @return object of BgpAttrNodeName
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053067 * @throws BgpParseException while parsing BgpAttrNodeName
Thejaswi NK5b9278b2015-10-12 16:59:14 +053068 */
69 public static BgpAttrNodeName read(ChannelBuffer cb)
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053070 throws BgpParseException {
Thejaswi NK5b9278b2015-10-12 16:59:14 +053071 byte[] nodeName;
72
73 short lsAttrLength = cb.readShort();
74
75 if (cb.readableBytes() < lsAttrLength) {
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053076 Validation.validateLen(BgpErrorType.UPDATE_MESSAGE_ERROR,
77 BgpErrorType.ATTRIBUTE_LENGTH_ERROR,
Thejaswi NK5b9278b2015-10-12 16:59:14 +053078 lsAttrLength);
79 }
80
81 nodeName = new byte[lsAttrLength];
82 cb.readBytes(nodeName);
83 return BgpAttrNodeName.of(nodeName);
84 }
85
86 /**
87 * Returns LS attribute node name.
88 *
89 * @return node name
90 */
91 public byte[] attrNodeName() {
92 return nodeName;
93 }
94
95 @Override
96 public short getType() {
97 return ATTRNODE_NAME;
98 }
99
100 @Override
101 public int hashCode() {
102 return Arrays.hashCode(nodeName);
103 }
104
105 @Override
106 public boolean equals(Object obj) {
107 if (this == obj) {
108 return true;
109 }
110
111 if (obj instanceof BgpAttrNodeName) {
112 BgpAttrNodeName other = (BgpAttrNodeName) obj;
113 return Arrays.equals(nodeName, other.nodeName);
114 }
115 return false;
116 }
117
118 @Override
119 public int write(ChannelBuffer cb) {
120 // TODO This will be implemented in the next version
121 return 0;
122 }
123
124 @Override
125 public String toString() {
126 return MoreObjects.toStringHelper(getClass()).omitNullValues()
127 .add("nodeName", nodeName).toString();
128 }
129}