blob: 33ab692cb697c711debbd9c832b9e2e5c3cf4692 [file] [log] [blame]
Sho SHIMIZUe81e4db2015-09-03 09:44:38 -07001/*
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.pcepio.types;
17
18import java.util.Objects;
19import org.jboss.netty.buffer.ChannelBuffer;
20import org.onosproject.pcepio.protocol.PcepVersion;
21import org.slf4j.Logger;
22import org.slf4j.LoggerFactory;
23
24import com.google.common.base.MoreObjects;
25import com.google.common.base.MoreObjects.ToStringHelper;
26
27/**
28 * Provide the name for the node.
29 */
Mahesh Poojary Sf1bbd362016-02-25 18:19:59 +053030public class NodeNameSubTlv implements PcepValueType {
Sho SHIMIZUe81e4db2015-09-03 09:44:38 -070031
32 /* reference :[I-D.ietf-idr-ls-distribution]/3.3.1.3
33 * 0 1 2 3
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=[TBD23] | Length |
37 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
38 // Node Name (variable) //
39 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
40 */
41
Mahesh Poojary Sf1bbd362016-02-25 18:19:59 +053042 protected static final Logger log = LoggerFactory.getLogger(NodeNameSubTlv.class);
Sho SHIMIZUe81e4db2015-09-03 09:44:38 -070043
Mahesh Poojary Sf1bbd362016-02-25 18:19:59 +053044 public static final short TYPE = 15;
Sho SHIMIZUe81e4db2015-09-03 09:44:38 -070045 public final short hLength;
46
47 private final byte[] rawValue;
48
49 /**
50 * constructor to initialize rawValue.
51 *
52 * @param rawValue of Node Name
53 * @param hLength length
54 */
Mahesh Poojary Sf1bbd362016-02-25 18:19:59 +053055 public NodeNameSubTlv(byte[] rawValue, short hLength) {
Sho SHIMIZUe81e4db2015-09-03 09:44:38 -070056 log.debug("NodeNameTlv");
57 this.rawValue = rawValue;
58 if (0 == hLength) {
59 this.hLength = (short) rawValue.length;
60 } else {
61 this.hLength = hLength;
62 }
63 }
64
65 /**
66 * Returns newly created NodeNameTlv object.
67 *
68 * @param raw of NodeName
69 * @param hLength length
70 * @return new object of Node Name Tlv
71 */
Mahesh Poojary Sf1bbd362016-02-25 18:19:59 +053072 public static NodeNameSubTlv of(final byte[] raw, short hLength) {
73 return new NodeNameSubTlv(raw, hLength);
Sho SHIMIZUe81e4db2015-09-03 09:44:38 -070074 }
75
76 /**
77 * Returns RawValue for NodeName.
78 *
79 * @return rawValue raw value
80 */
81 public byte[] getValue() {
82 return rawValue;
83 }
84
85 @Override
86 public PcepVersion getVersion() {
87 return PcepVersion.PCEP_1;
88 }
89
90 @Override
91 public short getType() {
92 return TYPE;
93 }
94
95 @Override
96 public short getLength() {
97 return hLength;
98 }
99
100 @Override
101 public int hashCode() {
102 return Objects.hash(rawValue);
103 }
104
105 @Override
106 public boolean equals(Object obj) {
107 if (this == obj) {
108 return true;
109 }
Mahesh Poojary Sf1bbd362016-02-25 18:19:59 +0530110 if (obj instanceof NodeNameSubTlv) {
111 NodeNameSubTlv other = (NodeNameSubTlv) obj;
Sho SHIMIZUe81e4db2015-09-03 09:44:38 -0700112 return Objects.equals(this.rawValue, other.rawValue);
113 }
114 return false;
115 }
116
117 @Override
118 public int write(ChannelBuffer c) {
119 int iLenStartIndex = c.writerIndex();
120 c.writeShort(TYPE);
121 c.writeShort(hLength);
122 c.writeBytes(rawValue);
123 return c.writerIndex() - iLenStartIndex;
124 }
125
126 /**
127 * Reads the channel buffer and returns object of NodeNameTlv.
128 *
129 * @param c input channel buffer
130 * @param hLength length
131 * @return object of Node Name TLV
132 */
133 public static PcepValueType read(ChannelBuffer c, short hLength) {
134 byte[] iNodeName = new byte[hLength];
135 c.readBytes(iNodeName, 0, hLength);
Mahesh Poojary Sf1bbd362016-02-25 18:19:59 +0530136 return new NodeNameSubTlv(iNodeName, hLength);
Sho SHIMIZUe81e4db2015-09-03 09:44:38 -0700137 }
138
139 @Override
140 public String toString() {
141 ToStringHelper toStrHelper = MoreObjects.toStringHelper(getClass());
142
143 toStrHelper.add("Type", TYPE);
144 toStrHelper.add("Length", hLength);
145
146 StringBuffer result = new StringBuffer();
147 for (byte b : rawValue) {
148 result.append(String.format("%02X ", b));
149 }
150 toStrHelper.add("Value", result);
151
152 return toStrHelper.toString();
153 }
154}