blob: 61993f4f2359fb7f05274ab180a12d2b148945e9 [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;
Thejaswi N Kff6f6fc2015-10-14 21:51:28 +053019
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 N Kff6f6fc2015-10-14 21:51:28 +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 link name attribute.
32 */
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053033public class BgpLinkAttrName implements BgpValueType {
Thejaswi N Kff6f6fc2015-10-14 21:51:28 +053034
35 protected static final Logger log = LoggerFactory
36 .getLogger(BgpLinkAttrName.class);
37
38 public static final int ATTRLINK_NAME = 1098;
39
40 /* Link Name */
41 private byte[] linkName;
42
43 /**
44 * Constructor to initialize the values.
45 *
46 * @param linkName link name
47 */
Thejaswi N Kde18d592015-11-18 19:07:54 +053048 public BgpLinkAttrName(byte[] linkName) {
Thejaswi N Kff6f6fc2015-10-14 21:51:28 +053049 this.linkName = Arrays.copyOf(linkName, linkName.length);
50 }
51
52 /**
Thejaswi N Kde18d592015-11-18 19:07:54 +053053 * Returns object of this class with specified values.
54 *
55 * @param linkName Prefix Metric
56 * @return object of BgpLinkAttrName
57 */
58 public static BgpLinkAttrName of(byte[] linkName) {
59 return new BgpLinkAttrName(linkName);
60 }
61
62 /**
Thejaswi N Kff6f6fc2015-10-14 21:51:28 +053063 * Reads the BGP link attributes Name.
64 *
65 * @param cb Channel buffer
66 * @return object of type BgpLinkAttrName
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053067 * @throws BgpParseException while parsing BgpLinkAttrName
Thejaswi N Kff6f6fc2015-10-14 21:51:28 +053068 */
69 public static BgpLinkAttrName read(ChannelBuffer cb)
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053070 throws BgpParseException {
Thejaswi N Kff6f6fc2015-10-14 21:51:28 +053071 byte[] linkName;
72 short lsAttrLength = cb.readShort();
73
74 if (cb.readableBytes() < lsAttrLength) {
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053075 Validation.validateLen(BgpErrorType.UPDATE_MESSAGE_ERROR,
76 BgpErrorType.ATTRIBUTE_LENGTH_ERROR,
Thejaswi N Kff6f6fc2015-10-14 21:51:28 +053077 lsAttrLength);
78 }
79
80 linkName = new byte[lsAttrLength];
81 cb.readBytes(linkName);
Thejaswi N Kde18d592015-11-18 19:07:54 +053082 return BgpLinkAttrName.of(linkName);
Thejaswi N Kff6f6fc2015-10-14 21:51:28 +053083 }
84
85 /**
86 * Returns the link name.
87 *
88 * @return link name
89 */
Thejaswi N Kde18d592015-11-18 19:07:54 +053090 public byte[] attrLinkName() {
Thejaswi N Kff6f6fc2015-10-14 21:51:28 +053091 return linkName;
92 }
93
94 @Override
95 public short getType() {
96 return ATTRLINK_NAME;
97 }
98
99 @Override
100 public int hashCode() {
Thejaswi N Kde18d592015-11-18 19:07:54 +0530101 return Arrays.hashCode(linkName);
Thejaswi N Kff6f6fc2015-10-14 21:51:28 +0530102 }
103
104 @Override
105 public boolean equals(Object obj) {
106 if (this == obj) {
107 return true;
108 }
109
110 if (obj instanceof BgpLinkAttrName) {
111 BgpLinkAttrName other = (BgpLinkAttrName) obj;
Thejaswi N Kde18d592015-11-18 19:07:54 +0530112 return Arrays.equals(linkName, other.linkName);
Thejaswi N Kff6f6fc2015-10-14 21:51:28 +0530113 }
114 return false;
115 }
116
117 @Override
118 public int write(ChannelBuffer cb) {
119 // TODO This will be implemented in the next version
120 return 0;
121 }
122
123 @Override
124 public String toString() {
125 return MoreObjects.toStringHelper(getClass()).omitNullValues()
126 .add("linkName", linkName).toString();
127 }
128}