blob: 50591ecfddddc466c5fa26d74344798bcefc9f44 [file] [log] [blame]
Thejaswi N Kff6f6fc2015-10-14 21:51:28 +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;
19import java.util.Objects;
20
21import org.jboss.netty.buffer.ChannelBuffer;
22import org.onosproject.bgpio.exceptions.BGPParseException;
23import org.onosproject.bgpio.types.BGPErrorType;
24import org.onosproject.bgpio.types.BGPValueType;
25import org.onosproject.bgpio.util.Validation;
26import org.slf4j.Logger;
27import org.slf4j.LoggerFactory;
28
29import com.google.common.base.MoreObjects;
30
31/**
32 * Implements BGP link name attribute.
33 */
34public class BgpLinkAttrName implements BGPValueType {
35
36 protected static final Logger log = LoggerFactory
37 .getLogger(BgpLinkAttrName.class);
38
39 public static final int ATTRLINK_NAME = 1098;
40
41 /* Link Name */
42 private byte[] linkName;
43
44 /**
45 * Constructor to initialize the values.
46 *
47 * @param linkName link name
48 */
49 BgpLinkAttrName(byte[] linkName) {
50 this.linkName = Arrays.copyOf(linkName, linkName.length);
51 }
52
53 /**
54 * Reads the BGP link attributes Name.
55 *
56 * @param cb Channel buffer
57 * @return object of type BgpLinkAttrName
58 * @throws BGPParseException while parsing BgpLinkAttrName
59 */
60 public static BgpLinkAttrName read(ChannelBuffer cb)
61 throws BGPParseException {
62 byte[] linkName;
63 short lsAttrLength = cb.readShort();
64
65 if (cb.readableBytes() < lsAttrLength) {
66 Validation.validateLen(BGPErrorType.UPDATE_MESSAGE_ERROR,
67 BGPErrorType.ATTRIBUTE_LENGTH_ERROR,
68 lsAttrLength);
69 }
70
71 linkName = new byte[lsAttrLength];
72 cb.readBytes(linkName);
73 return new BgpLinkAttrName(linkName);
74 }
75
76 /**
77 * Returns the link name.
78 *
79 * @return link name
80 */
81 byte[] getAttrLinkName() {
82 return linkName;
83 }
84
85 @Override
86 public short getType() {
87 return ATTRLINK_NAME;
88 }
89
90 @Override
91 public int hashCode() {
92 return Objects.hash(linkName);
93 }
94
95 @Override
96 public boolean equals(Object obj) {
97 if (this == obj) {
98 return true;
99 }
100
101 if (obj instanceof BgpLinkAttrName) {
102 BgpLinkAttrName other = (BgpLinkAttrName) obj;
103 return Objects.equals(linkName, other.linkName);
104 }
105 return false;
106 }
107
108 @Override
109 public int write(ChannelBuffer cb) {
110 // TODO This will be implemented in the next version
111 return 0;
112 }
113
114 @Override
115 public String toString() {
116 return MoreObjects.toStringHelper(getClass()).omitNullValues()
117 .add("linkName", linkName).toString();
118 }
119}