2 * Copyright 2015 Open Networking Laboratory
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package org.onosproject.pcepio.types;
18 import java.util.Objects;
19 import org.jboss.netty.buffer.ChannelBuffer;
20 import org.onosproject.pcepio.protocol.PcepVersion;
21 import org.slf4j.Logger;
22 import org.slf4j.LoggerFactory;
24 import com.google.common.base.MoreObjects;
25 import com.google.common.base.MoreObjects.ToStringHelper;
28 * Provides Opaque node attributes.
30 public class OpaqueNodeAttributeTlv implements PcepValueType {
32 * Reference [I-D.ietf-idr-Properties ls-distribution] /3.3.1.5
34 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
35 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
36 | Type=[TBD22] | Length |
37 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
38 // Opaque node attributes (variable) //
39 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
42 protected static final Logger log = LoggerFactory.getLogger(OpaqueNodeAttributeTlv.class);
44 public static final short TYPE = 1001;
45 private final short hLength;
47 private final byte[] rawValue;
50 * constructor to initialize rawValue.
52 * @param rawValue Opaque Node Attribute
53 * @param hLength length
55 public OpaqueNodeAttributeTlv(byte[] rawValue, short hLength) {
57 this.rawValue = rawValue;
59 this.hLength = (short) rawValue.length;
61 this.hLength = hLength;
66 * Returns newly created OpaqueNodeAttributeTlv object.
68 * @param raw value of Opaque Node Attribute
69 * @param hLength length
70 * @return new object of Opaque Node Attribute Tlv
72 public static OpaqueNodeAttributeTlv of(final byte[] raw, short hLength) {
73 return new OpaqueNodeAttributeTlv(raw, hLength);
77 * Returns raw value of Opaque Node Attribute Tlv.
79 * @return rawValue of Opaque Node Attribute
81 public byte[] getValue() {
86 public PcepVersion getVersion() {
87 return PcepVersion.PCEP_1;
91 public short getType() {
96 public short getLength() {
101 public int hashCode() {
102 return Objects.hash(rawValue);
106 public boolean equals(Object obj) {
110 if (obj instanceof OpaqueLinkAttributeTlv) {
111 OpaqueNodeAttributeTlv other = (OpaqueNodeAttributeTlv) obj;
112 return Objects.equals(this.rawValue, other.rawValue);
118 public int write(ChannelBuffer c) {
119 int iLenStartIndex = c.writerIndex();
121 c.writeShort(hLength);
122 c.writeBytes(rawValue);
123 return c.writerIndex() - iLenStartIndex;
127 * Reads the channel buffer and returns object of Opaque Node Attribute Tlv.
129 * @param c input channel buffer
130 * @param hLength length
131 * @return object of OpaqueNodeAttributeTlv
133 public static PcepValueType read(ChannelBuffer c, short hLength) {
134 byte[] iOpaqueValue = new byte[hLength];
135 c.readBytes(iOpaqueValue, 0, hLength);
136 return new OpaqueNodeAttributeTlv(iOpaqueValue, hLength);
140 public String toString() {
141 ToStringHelper toStrHelper = MoreObjects.toStringHelper(getClass());
143 toStrHelper.add("Type", TYPE);
144 toStrHelper.add("Length", hLength);
146 StringBuffer result = new StringBuffer();
147 for (byte b : rawValue) {
148 result.append(String.format("%02X ", b));
150 toStrHelper.add("Value", result);
152 return toStrHelper.toString();