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