blob: 62a5588d34d7941404b2e70ce1dd89b7d98271d8 [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
Satish Kff6c11c2015-11-22 13:13:28 +053018import java.util.Arrays;
Sho SHIMIZUe81e4db2015-09-03 09:44:38 -070019
20import org.jboss.netty.buffer.ChannelBuffer;
21import org.onosproject.pcepio.protocol.PcepVersion;
22import org.slf4j.Logger;
23import org.slf4j.LoggerFactory;
24
25import com.google.common.base.MoreObjects;
26import com.google.common.base.MoreObjects.ToStringHelper;
27
28/**
29 * Provides IPv6 Interface Address. REFERENCE :[RFC6119]/4.2.
30 */
31public class IPv6InterfaceAddressTlv implements PcepValueType {
32
33 protected static final Logger log = LoggerFactory.getLogger(IPv6InterfaceAddressTlv.class);
34
35 public static final short TYPE = 12; //TDB18
36 public static final short LENGTH = 20;
37 public static final byte VALUE_LENGTH = 18;
38
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);
41
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;
47
48 private final byte[] rawValue;
49
50 /**
51 * Constructor to initialize rawValue.
52 *
53 * @param rawValue IPv6 Interface Address Tlv
54 */
55 public IPv6InterfaceAddressTlv(byte[] rawValue) {
56 log.debug("IPv6InterfaceAddressTlv");
57 this.rawValue = rawValue;
58 }
59
60 /**
61 * Returns newly created IPv6InterfaceAddressTlv object.
62 *
63 * @param raw IPv6 Interface Address
64 * @return object of IPv6InterfaceAddressTlv
65 */
66 public static IPv6InterfaceAddressTlv of(final byte[] raw) {
67 //check NONE_VAL
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]) {
72 bFoundNONE = false;
73 }
74 }
75
76 if (bFoundNONE) {
77 return NONE;
78 }
79
80 //check NO_MASK_VAL
81 boolean bFoundNoMask = true;
82 //value starts from 3rd byte.
83 for (int i = 2; i < 20; ++i) {
84 if (0xFF != raw[i]) {
85 bFoundNoMask = false;
86 }
87 }
88 if (bFoundNoMask) {
89 return NO_MASK;
90 }
91
92 return new IPv6InterfaceAddressTlv(raw);
93 }
94
95 /**
96 * Returns value of IPv6 Interface Address.
97 *
98 * @return rawValue raw value
99 */
100 public byte[] getBytes() {
101 return rawValue;
102 }
103
104 /**
105 * Returns value of IPv6 Interface Address.
106 *
107 * @return rawValue raw value
108 */
109 public byte[] getValue() {
110 return rawValue;
111 }
112
113 @Override
114 public PcepVersion getVersion() {
115 return PcepVersion.PCEP_1;
116 }
117
118 @Override
119 public short getType() {
120 return TYPE;
121 }
122
123 @Override
124 public short getLength() {
125 return LENGTH;
126 }
127
128 @Override
129 public int hashCode() {
Satish Kff6c11c2015-11-22 13:13:28 +0530130 return Arrays.hashCode(rawValue);
Sho SHIMIZUe81e4db2015-09-03 09:44:38 -0700131 }
132
133 @Override
134 public boolean equals(Object obj) {
135 if (this == obj) {
136 return true;
137 }
138 if (obj instanceof IPv6InterfaceAddressTlv) {
139 IPv6InterfaceAddressTlv other = (IPv6InterfaceAddressTlv) obj;
Satish Kff6c11c2015-11-22 13:13:28 +0530140 return Arrays.equals(rawValue, other.rawValue);
Sho SHIMIZUe81e4db2015-09-03 09:44:38 -0700141 }
142 return false;
143 }
144
145 @Override
146 public int write(ChannelBuffer c) {
147 int iLenStartIndex = c.writerIndex();
148 c.writeShort(TYPE);
149 c.writeShort(LENGTH);
150 c.writeBytes(rawValue);
151 return c.writerIndex() - iLenStartIndex;
152 }
153
154 /**
155 * Reads the channel buffer and returns object of IPv6InterfaceAddressTlv.
156 *
157 * @param c input channel buffer
158 * @return object of IPv6InterfaceAddressTlv
159 */
160 public static IPv6InterfaceAddressTlv read20Bytes(ChannelBuffer c) {
161 byte[] yTemp = new byte[20];
162 c.readBytes(yTemp, 0, 20);
163 return IPv6InterfaceAddressTlv.of(yTemp);
164 }
165
166 @Override
167 public String toString() {
168 ToStringHelper toStrHelper = MoreObjects.toStringHelper(getClass());
169
170 toStrHelper.add("Type", TYPE);
171 toStrHelper.add("Length", LENGTH);
172
173 StringBuffer result = new StringBuffer();
174 for (byte b : rawValue) {
175 result.append(String.format("%02X ", b));
176 }
177 toStrHelper.add("Value", result);
178
179 return toStrHelper.toString();
180 }
181}