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 Neighbor Address. Reference :[RFC6119]/4.3.
31 public class IPv6NeighborAddressTlv implements PcepValueType {
32 protected static final Logger log = LoggerFactory.getLogger(IPv6NeighborAddressTlv.class);
34 public static final short TYPE = 13; // TDB19
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 IPv6NeighborAddressTlv NONE = new IPv6NeighborAddressTlv(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 IPv6NeighborAddressTlv NO_MASK = new IPv6NeighborAddressTlv(NO_MASK_VAL);
45 public static final IPv6NeighborAddressTlv FULL_MASK = NONE;
47 private final byte[] rawValue;
50 * Constructor to initialize rawValue.
52 * @param rawValue IPv6 Neighbor Address Tlv
54 public IPv6NeighborAddressTlv(byte[] rawValue) {
55 this.rawValue = rawValue;
59 * Returns newly created IPv6NeighborAddressTlv object.
61 * @param raw IPv6 Neighbor Address
62 * @return object of IPv6 Neighbor Address Tlv
64 public static IPv6NeighborAddressTlv of(final byte[] raw) {
66 boolean bFoundNONE = true;
67 //value starts from 3rd byte.
68 for (int i = 2; i < 20; ++i) {
69 if (NONE_VAL[i] != raw[i]) {
79 boolean bFoundNoMask = true;
80 //value starts from 3rd byte.
81 for (int i = 2; i < 20; ++i) {
90 return new IPv6NeighborAddressTlv(raw);
94 * Returns value of IPv6 Neighbor Address.
96 * @return rawValue raw value
98 public byte[] getBytes() {
103 * Returns value of IPv6 Neighbor Address.
105 * @return rawValue raw value
107 public byte[] getValue() {
112 public PcepVersion getVersion() {
113 return PcepVersion.PCEP_1;
117 public short getType() {
122 public short getLength() {
127 public int hashCode() {
128 return Objects.hash(rawValue);
132 public boolean equals(Object obj) {
136 if (obj instanceof IPv6NeighborAddressTlv) {
137 IPv6NeighborAddressTlv other = (IPv6NeighborAddressTlv) obj;
138 return Objects.equals(rawValue, other.rawValue);
144 public int write(ChannelBuffer c) {
145 int iStartIndex = c.writerIndex();
147 c.writeShort(LENGTH);
148 c.writeBytes(rawValue);
149 return c.writerIndex() - iStartIndex;
153 * Reads the channel buffer and returns object of IPv6NeighborAddressTlv.
155 * @param c input channel buffer
156 * @return object of IPv6NeighborAddressTlv
158 public static IPv6NeighborAddressTlv read20Bytes(ChannelBuffer c) {
159 byte[] yTemp = new byte[20];
160 c.readBytes(yTemp, 0, 20);
161 return IPv6NeighborAddressTlv.of(yTemp);
165 public String toString() {
166 ToStringHelper toStrHelper = MoreObjects.toStringHelper(getClass());
168 toStrHelper.add("Type", TYPE);
169 toStrHelper.add("Length", LENGTH);
171 StringBuffer result = new StringBuffer();
172 for (byte b : rawValue) {
173 result.append(String.format("%02X ", b));
175 toStrHelper.add("Value", result);
177 return toStrHelper.toString();