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 Interface Address. REFERENCE :[RFC6119]/4.2.
31 public class IPv6InterfaceAddressTlv implements PcepValueType {
33 protected static final Logger log = LoggerFactory.getLogger(IPv6InterfaceAddressTlv.class);
35 public static final short TYPE = 12; //TDB18
36 public static final short LENGTH = 20;
37 public static final byte VALUE_LENGTH = 18;
39 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};
40 public static final IPv6InterfaceAddressTlv NONE = new IPv6InterfaceAddressTlv(NONE_VAL);
42 private static final byte[] NO_MASK_VAL = {(byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
43 (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
44 (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF};
45 public static final IPv6InterfaceAddressTlv NO_MASK = new IPv6InterfaceAddressTlv(NO_MASK_VAL);
46 public static final IPv6InterfaceAddressTlv FULL_MASK = NONE;
48 private final byte[] rawValue;
51 * Constructor to initialize rawValue.
53 * @param rawValue IPv6 Interface Address Tlv
55 public IPv6InterfaceAddressTlv(byte[] rawValue) {
56 log.debug("IPv6InterfaceAddressTlv");
57 this.rawValue = rawValue;
61 * Returns newly created IPv6InterfaceAddressTlv object.
63 * @param raw IPv6 Interface Address
64 * @return object of IPv6InterfaceAddressTlv
66 public static IPv6InterfaceAddressTlv of(final byte[] raw) {
68 boolean bFoundNONE = true;
69 //value starts from 3rd byte.
70 for (int i = 2; i < 20; ++i) {
71 if (NONE_VAL[i] != raw[i]) {
81 boolean bFoundNoMask = true;
82 //value starts from 3rd byte.
83 for (int i = 2; i < 20; ++i) {
92 return new IPv6InterfaceAddressTlv(raw);
96 * Returns value of IPv6 Interface Address.
98 * @return rawValue raw value
100 public byte[] getBytes() {
105 * Returns value of IPv6 Interface Address.
107 * @return rawValue raw value
109 public byte[] getValue() {
114 public PcepVersion getVersion() {
115 return PcepVersion.PCEP_1;
119 public short getType() {
124 public short getLength() {
129 public int hashCode() {
130 return Objects.hash(rawValue);
134 public boolean equals(Object obj) {
138 if (obj instanceof IPv6InterfaceAddressTlv) {
139 IPv6InterfaceAddressTlv other = (IPv6InterfaceAddressTlv) obj;
140 return Objects.equals(rawValue, other.rawValue);
146 public int write(ChannelBuffer c) {
147 int iLenStartIndex = c.writerIndex();
149 c.writeShort(LENGTH);
150 c.writeBytes(rawValue);
151 return c.writerIndex() - iLenStartIndex;
155 * Reads the channel buffer and returns object of IPv6InterfaceAddressTlv.
157 * @param c input channel buffer
158 * @return object of IPv6InterfaceAddressTlv
160 public static IPv6InterfaceAddressTlv read20Bytes(ChannelBuffer c) {
161 byte[] yTemp = new byte[20];
162 c.readBytes(yTemp, 0, 20);
163 return IPv6InterfaceAddressTlv.of(yTemp);
167 public String toString() {
168 ToStringHelper toStrHelper = MoreObjects.toStringHelper(getClass());
170 toStrHelper.add("Type", TYPE);
171 toStrHelper.add("Length", LENGTH);
173 StringBuffer result = new StringBuffer();
174 for (byte b : rawValue) {
175 result.append(String.format("%02X ", b));
177 toStrHelper.add("Value", result);
179 return toStrHelper.toString();