blob: 186f56de757f3d9de70aa23df8921b74743dbe68 [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 TE Router Id of Local Node. Reference :[RFC6119]/4.1.
29 */
Mahesh Poojary Sf1bbd362016-02-25 18:19:59 +053030public class IPv6RouterIdofLocalNodeSubTlv implements PcepValueType {
Ray Milkey9c9cde42018-01-12 14:22:06 -080031 private static final Logger log = LoggerFactory.getLogger(IPv6RouterIdofLocalNodeSubTlv.class);
Sho SHIMIZUe81e4db2015-09-03 09:44:38 -070032
Mahesh Poojary Sf1bbd362016-02-25 18:19:59 +053033 public static final short TYPE = 18;
Sho SHIMIZUe81e4db2015-09-03 09:44:38 -070034 public static final short LENGTH = 20;
35 public static final byte VALUE_LENGTH = 18;
36
37 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 +053038 public static final IPv6RouterIdofLocalNodeSubTlv NONE = new IPv6RouterIdofLocalNodeSubTlv(NONE_VAL);
Sho SHIMIZUe81e4db2015-09-03 09:44:38 -070039
40 private static final byte[] NO_MASK_VAL = {(byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
41 (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
42 (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF };
Mahesh Poojary Sf1bbd362016-02-25 18:19:59 +053043 public static final IPv6RouterIdofLocalNodeSubTlv NO_MASK = new IPv6RouterIdofLocalNodeSubTlv(NO_MASK_VAL);
44 public static final IPv6RouterIdofLocalNodeSubTlv FULL_MASK = NONE;
Sho SHIMIZUe81e4db2015-09-03 09:44:38 -070045
46 private final byte[] rawValue;
47
48 /**
49 * Constructor to initialize rawValue.
50 *
Mahesh Poojary Sf1bbd362016-02-25 18:19:59 +053051 * @param rawValue IPv6RouterIdofLocalNodeTlv
Sho SHIMIZUe81e4db2015-09-03 09:44:38 -070052 */
Mahesh Poojary Sf1bbd362016-02-25 18:19:59 +053053 public IPv6RouterIdofLocalNodeSubTlv(byte[] rawValue) {
Sho SHIMIZUe81e4db2015-09-03 09:44:38 -070054 this.rawValue = rawValue;
55 }
56
57 /**
Mahesh Poojary Sf1bbd362016-02-25 18:19:59 +053058 * Returns newly created IPv6RouterIdofLocalNodeTlv object.
Sho SHIMIZUe81e4db2015-09-03 09:44:38 -070059 *
60 * @param raw IPv6 TE Router Id of Local Node
Mahesh Poojary Sf1bbd362016-02-25 18:19:59 +053061 * @return object of IPv6RouterIdofLocalNodeTlv
Sho SHIMIZUe81e4db2015-09-03 09:44:38 -070062 */
Mahesh Poojary Sf1bbd362016-02-25 18:19:59 +053063 public static IPv6RouterIdofLocalNodeSubTlv of(final byte[] raw) {
Sho SHIMIZUe81e4db2015-09-03 09:44:38 -070064 //check NONE_VAL
Jonathan Hart51539b82015-10-29 09:53:04 -070065 boolean bFoundNone = true;
Sho SHIMIZUe81e4db2015-09-03 09:44:38 -070066 //value starts from 3rd byte.
67 for (int i = 2; i < 20; ++i) {
68 if (NONE_VAL[i] != raw[i]) {
Jonathan Hart51539b82015-10-29 09:53:04 -070069 bFoundNone = false;
Sho SHIMIZUe81e4db2015-09-03 09:44:38 -070070 }
71 }
72
Jonathan Hart51539b82015-10-29 09:53:04 -070073 if (bFoundNone) {
Sho SHIMIZUe81e4db2015-09-03 09:44:38 -070074 return NONE;
75 }
76
77 //check NO_MASK_VAL
78 boolean bFoundNoMask = true;
79 //value starts from 3rd byte.
80 for (int i = 2; i < 20; ++i) {
Yuta HIGUCHIfbd9ae92018-01-24 23:39:06 -080081 if ((byte) 0xFF != raw[i]) {
Sho SHIMIZUe81e4db2015-09-03 09:44:38 -070082 bFoundNoMask = false;
83 }
84 }
85 if (bFoundNoMask) {
86 return NO_MASK;
87 }
88
Mahesh Poojary Sf1bbd362016-02-25 18:19:59 +053089 return new IPv6RouterIdofLocalNodeSubTlv(raw);
Sho SHIMIZUe81e4db2015-09-03 09:44:38 -070090 }
91
92 /**
93 * Returns value of IPv6 TE Router Id of Local Node.
94 *
95 * @return byte array value of rawValue
96 */
97 public byte[] getBytes() {
98 return rawValue;
99 }
100
101 /**
102 * Returns value of IPv6 TE Router Id of Local Node.
103 *
104 * @return byte array value of rawValue
105 */
106 public byte[] getValue() {
107 return rawValue;
108 }
109
110 @Override
111 public PcepVersion getVersion() {
112 return PcepVersion.PCEP_1;
113 }
114
115 @Override
116 public short getType() {
117 return TYPE;
118 }
119
120 @Override
121 public short getLength() {
122 return LENGTH;
123 }
124
125 @Override
126 public int hashCode() {
Satish Kcd53ae52015-11-27 13:09:17 +0530127 return Arrays.hashCode(rawValue);
Sho SHIMIZUe81e4db2015-09-03 09:44:38 -0700128 }
129
130 @Override
131 public boolean equals(Object obj) {
132 if (this == obj) {
133 return true;
134 }
Mahesh Poojary Sf1bbd362016-02-25 18:19:59 +0530135 if (obj instanceof IPv6RouterIdofLocalNodeSubTlv) {
136 IPv6RouterIdofLocalNodeSubTlv other = (IPv6RouterIdofLocalNodeSubTlv) obj;
Satish Kcd53ae52015-11-27 13:09:17 +0530137 return Arrays.equals(rawValue, other.rawValue);
Sho SHIMIZUe81e4db2015-09-03 09:44:38 -0700138 }
139 return false;
140 }
141
142 @Override
143 public int write(ChannelBuffer c) {
144 int iStartIndex = c.writerIndex();
145 c.writeShort(TYPE);
146 c.writeShort(LENGTH);
147 c.writeBytes(rawValue);
148 return c.writerIndex() - iStartIndex;
149 }
150
151 /**
Mahesh Poojary Sf1bbd362016-02-25 18:19:59 +0530152 * Reads the channel buffer and returns object of IPv6RouterIdofLocalNodeTlv.
Sho SHIMIZUe81e4db2015-09-03 09:44:38 -0700153 *
154 * @param c input channel buffer
Mahesh Poojary Sf1bbd362016-02-25 18:19:59 +0530155 * @return object of IPv6RouterIdofLocalNodeTlv
Sho SHIMIZUe81e4db2015-09-03 09:44:38 -0700156 */
Mahesh Poojary Sf1bbd362016-02-25 18:19:59 +0530157 public static IPv6RouterIdofLocalNodeSubTlv read20Bytes(ChannelBuffer c) {
Sho SHIMIZUe81e4db2015-09-03 09:44:38 -0700158 byte[] yTemp = new byte[20];
159 c.readBytes(yTemp, 0, 20);
Mahesh Poojary Sf1bbd362016-02-25 18:19:59 +0530160 return IPv6RouterIdofLocalNodeSubTlv.of(yTemp);
Sho SHIMIZUe81e4db2015-09-03 09:44:38 -0700161 }
162
163 @Override
164 public String toString() {
165 ToStringHelper toStrHelper = MoreObjects.toStringHelper(getClass());
166
167 toStrHelper.add("Type", TYPE);
168 toStrHelper.add("Length", LENGTH);
169
170 StringBuffer result = new StringBuffer();
171 for (byte b : rawValue) {
172 result.append(String.format("%02X ", b));
173 }
174 toStrHelper.add("Value", result);
175
176 return toStrHelper.toString();
177 }
178}