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;
20 import org.jboss.netty.buffer.ChannelBuffer;
21 import org.onosproject.pcepio.protocol.PcepVersion;
22 import org.slf4j.Logger;
23 import org.slf4j.LoggerFactory;
25 import com.google.common.base.MoreObjects;
26 import com.google.common.base.MoreObjects.ToStringHelper;
29 * Provides IPv6 TE Router Id of Remote Node. Reference :[RFC6119]/4.1.
31 public class IPv6TERouterIdofRemoteNodeTlv implements PcepValueType {
32 protected static final Logger log = LoggerFactory.getLogger(IPv6TERouterIdofRemoteNodeTlv.class);
34 public static final short TYPE = 1400; //TDB29
35 public static final short LENGTH = 20;
36 public static final byte VALUE_LENGTH = 18;
38 private static final byte[] NONE_VAL = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
39 public static final IPv6TERouterIdofRemoteNodeTlv NONE = new IPv6TERouterIdofRemoteNodeTlv(NONE_VAL);
41 private static final byte[] NO_MASK_VAL = {(byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
42 (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
43 (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF};
44 public static final IPv6TERouterIdofRemoteNodeTlv NO_MASK = new IPv6TERouterIdofRemoteNodeTlv(NO_MASK_VAL);
45 public static final IPv6TERouterIdofRemoteNodeTlv FULL_MASK = NONE;
47 private final byte[] rawValue;
50 * constructor to initialize rawValue.
52 * @param rawValue IPv6TERouterIdofRemoteNodeTlv
54 public IPv6TERouterIdofRemoteNodeTlv(byte[] rawValue) {
55 log.debug("IPv6TERouterIdofRemoteNodeTlv");
56 this.rawValue = rawValue;
60 * Returns newly created IPv6TERouterIdofRemoteNodeTlv object.
62 * @param raw IPv6 TE Router Id of RemoteNode
63 * @return object of IPv6TERouterIdofRemoteNodeTlv
65 public static IPv6TERouterIdofRemoteNodeTlv of(final byte[] raw) {
67 boolean bFoundNONE = true;
68 //value starts from 3rd byte.
69 for (int i = 2; i < 20; ++i) {
70 if (NONE_VAL[i] != raw[i]) {
80 boolean bFoundNoMask = true;
81 //value starts from 3rd byte.
82 for (int i = 2; i < 20; ++i) {
91 return new IPv6TERouterIdofRemoteNodeTlv(raw);
95 * Returns value of IPv6 TE Router Id of Remote Node.
97 * @return byte array value of rawValue
99 public byte[] getBytes() {
104 public PcepVersion getVersion() {
105 return PcepVersion.PCEP_1;
109 public short getType() {
114 public short getLength() {
119 public int hashCode() {
120 return Objects.hash(rawValue);
124 public boolean equals(Object obj) {
128 if (obj instanceof IPv6TERouterIdofRemoteNodeTlv) {
129 IPv6TERouterIdofRemoteNodeTlv other = (IPv6TERouterIdofRemoteNodeTlv) obj;
130 return Objects.equals(rawValue, other.rawValue);
136 public int write(ChannelBuffer c) {
137 int iStartIndex = c.writerIndex();
139 c.writeShort(LENGTH);
140 c.writeBytes(rawValue);
141 return c.writerIndex() - iStartIndex;
145 * Reads the channel buffer and returns object of IPv6TERouterIdofRemoteNodeTlv.
147 * @param c input channel buffer
148 * @return object of IPv6TERouterIdofRemoteNodeTlv
150 public static IPv6TERouterIdofRemoteNodeTlv read20Bytes(ChannelBuffer c) {
151 byte[] yTemp = new byte[20];
152 c.readBytes(yTemp, 0, 20);
153 return IPv6TERouterIdofRemoteNodeTlv.of(yTemp);
157 public String toString() {
158 ToStringHelper toStrHelper = MoreObjects.toStringHelper(getClass());
160 toStrHelper.add("Type", TYPE);
161 toStrHelper.add("Length", LENGTH);
163 StringBuffer result = new StringBuffer();
164 for (byte b : rawValue) {
165 result.append(String.format("%02X ", b));
167 toStrHelper.add("Value", result);
169 return toStrHelper.toString();