blob: d318ca33fc4a9e73ea42646702065b61e1299df5 [file] [log] [blame]
Sho SHIMIZUe81e4db2015-09-03 09:44:38 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2016-present Open Networking Foundation
Sho SHIMIZUe81e4db2015-09-03 09:44:38 -07003 *
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 */
Mahesh Poojary Sf1bbd362016-02-25 18:19:59 +053030public class IPv6InterfaceAddressSubTlv implements PcepValueType {
Sho SHIMIZUe81e4db2015-09-03 09:44:38 -070031
Ray Milkey9c9cde42018-01-12 14:22:06 -080032 private static final Logger log = LoggerFactory.getLogger(IPv6InterfaceAddressSubTlv.class);
Sho SHIMIZUe81e4db2015-09-03 09:44:38 -070033
Mahesh Poojary Sf1bbd362016-02-25 18:19:59 +053034 public static final short TYPE = 9;
Sho SHIMIZUe81e4db2015-09-03 09:44:38 -070035 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};
Mahesh Poojary Sf1bbd362016-02-25 18:19:59 +053039 public static final IPv6InterfaceAddressSubTlv NONE = new IPv6InterfaceAddressSubTlv(NONE_VAL);
Sho SHIMIZUe81e4db2015-09-03 09:44:38 -070040
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};
Mahesh Poojary Sf1bbd362016-02-25 18:19:59 +053044 public static final IPv6InterfaceAddressSubTlv NO_MASK = new IPv6InterfaceAddressSubTlv(NO_MASK_VAL);
45 public static final IPv6InterfaceAddressSubTlv FULL_MASK = NONE;
Sho SHIMIZUe81e4db2015-09-03 09:44:38 -070046
47 private final byte[] rawValue;
48
49 /**
50 * Constructor to initialize rawValue.
51 *
52 * @param rawValue IPv6 Interface Address Tlv
53 */
Mahesh Poojary Sf1bbd362016-02-25 18:19:59 +053054 public IPv6InterfaceAddressSubTlv(byte[] rawValue) {
Sho SHIMIZUe81e4db2015-09-03 09:44:38 -070055 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 */
Mahesh Poojary Sf1bbd362016-02-25 18:19:59 +053065 public static IPv6InterfaceAddressSubTlv of(final byte[] raw) {
Sho SHIMIZUe81e4db2015-09-03 09:44:38 -070066 //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) {
Yuta HIGUCHIfbd9ae92018-01-24 23:39:06 -080083 if ((byte) 0xFF != raw[i]) {
Sho SHIMIZUe81e4db2015-09-03 09:44:38 -070084 bFoundNoMask = false;
85 }
86 }
87 if (bFoundNoMask) {
88 return NO_MASK;
89 }
90
Mahesh Poojary Sf1bbd362016-02-25 18:19:59 +053091 return new IPv6InterfaceAddressSubTlv(raw);
Sho SHIMIZUe81e4db2015-09-03 09:44:38 -070092 }
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 }
Mahesh Poojary Sf1bbd362016-02-25 18:19:59 +0530137 if (obj instanceof IPv6InterfaceAddressSubTlv) {
138 IPv6InterfaceAddressSubTlv other = (IPv6InterfaceAddressSubTlv) 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 */
Mahesh Poojary Sf1bbd362016-02-25 18:19:59 +0530159 public static IPv6InterfaceAddressSubTlv read20Bytes(ChannelBuffer c) {
Sho SHIMIZUe81e4db2015-09-03 09:44:38 -0700160 byte[] yTemp = new byte[20];
161 c.readBytes(yTemp, 0, 20);
Mahesh Poojary Sf1bbd362016-02-25 18:19:59 +0530162 return IPv6InterfaceAddressSubTlv.of(yTemp);
Sho SHIMIZUe81e4db2015-09-03 09:44:38 -0700163 }
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}