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