blob: 09a76f9a3213061ea1e53565e2d2e1efba107f06 [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 Remote Node. Reference :[RFC6119]/4.1.
29 */
Mahesh Poojary Sf1bbd362016-02-25 18:19:59 +053030public class IPv6RouterIdofRemoteNodeSubTlv implements PcepValueType {
31 protected static final Logger log = LoggerFactory.getLogger(IPv6RouterIdofRemoteNodeSubTlv.class);
Sho SHIMIZUe81e4db2015-09-03 09:44:38 -070032
Mahesh Poojary Sf1bbd362016-02-25 18:19:59 +053033 public static final short TYPE = 20;
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 IPv6RouterIdofRemoteNodeSubTlv NONE = new IPv6RouterIdofRemoteNodeSubTlv(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 IPv6RouterIdofRemoteNodeSubTlv NO_MASK = new IPv6RouterIdofRemoteNodeSubTlv(NO_MASK_VAL);
44 public static final IPv6RouterIdofRemoteNodeSubTlv 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 IPv6RouterIdofRemoteNodeTlv
Sho SHIMIZUe81e4db2015-09-03 09:44:38 -070052 */
Mahesh Poojary Sf1bbd362016-02-25 18:19:59 +053053 public IPv6RouterIdofRemoteNodeSubTlv(byte[] rawValue) {
54 log.debug("IPv6RouterIdofRemoteNodeTlv");
Sho SHIMIZUe81e4db2015-09-03 09:44:38 -070055 this.rawValue = rawValue;
56 }
57
58 /**
Mahesh Poojary Sf1bbd362016-02-25 18:19:59 +053059 * Returns newly created IPv6RouterIdofRemoteNodeTlv object.
Sho SHIMIZUe81e4db2015-09-03 09:44:38 -070060 *
61 * @param raw IPv6 TE Router Id of RemoteNode
Mahesh Poojary Sf1bbd362016-02-25 18:19:59 +053062 * @return object of IPv6RouterIdofRemoteNodeTlv
Sho SHIMIZUe81e4db2015-09-03 09:44:38 -070063 */
Mahesh Poojary Sf1bbd362016-02-25 18:19:59 +053064 public static IPv6RouterIdofRemoteNodeSubTlv of(final byte[] raw) {
Sho SHIMIZUe81e4db2015-09-03 09:44:38 -070065 //check NONE_VAL
Jonathan Hart51539b82015-10-29 09:53:04 -070066 boolean bFoundNone = true;
Sho SHIMIZUe81e4db2015-09-03 09:44:38 -070067 //value starts from 3rd byte.
68 for (int i = 2; i < 20; ++i) {
69 if (NONE_VAL[i] != raw[i]) {
Jonathan Hart51539b82015-10-29 09:53:04 -070070 bFoundNone = false;
Sho SHIMIZUe81e4db2015-09-03 09:44:38 -070071 }
72 }
73
Jonathan Hart51539b82015-10-29 09:53:04 -070074 if (bFoundNone) {
Sho SHIMIZUe81e4db2015-09-03 09:44:38 -070075 return NONE;
76 }
77
78 //check NO_MASK_VAL
79 boolean bFoundNoMask = true;
80 //value starts from 3rd byte.
81 for (int i = 2; i < 20; ++i) {
Yuta HIGUCHI1edc36b2018-01-24 23:39:06 -080082 if ((byte) 0xFF != raw[i]) {
Sho SHIMIZUe81e4db2015-09-03 09:44:38 -070083 bFoundNoMask = false;
84 }
85 }
86 if (bFoundNoMask) {
87 return NO_MASK;
88 }
89
Mahesh Poojary Sf1bbd362016-02-25 18:19:59 +053090 return new IPv6RouterIdofRemoteNodeSubTlv(raw);
Sho SHIMIZUe81e4db2015-09-03 09:44:38 -070091 }
92
93 /**
94 * Returns value of IPv6 TE Router Id of Remote Node.
95 *
96 * @return byte array value of rawValue
97 */
98 public byte[] getBytes() {
99 return rawValue;
100 }
101
102 @Override
103 public PcepVersion getVersion() {
104 return PcepVersion.PCEP_1;
105 }
106
107 @Override
108 public short getType() {
109 return TYPE;
110 }
111
112 @Override
113 public short getLength() {
114 return LENGTH;
115 }
116
117 @Override
118 public int hashCode() {
Satish Kcd53ae52015-11-27 13:09:17 +0530119 return Arrays.hashCode(rawValue);
Sho SHIMIZUe81e4db2015-09-03 09:44:38 -0700120 }
121
122 @Override
123 public boolean equals(Object obj) {
124 if (this == obj) {
125 return true;
126 }
Mahesh Poojary Sf1bbd362016-02-25 18:19:59 +0530127 if (obj instanceof IPv6RouterIdofRemoteNodeSubTlv) {
128 IPv6RouterIdofRemoteNodeSubTlv other = (IPv6RouterIdofRemoteNodeSubTlv) obj;
Satish Kcd53ae52015-11-27 13:09:17 +0530129 return Arrays.equals(rawValue, other.rawValue);
Sho SHIMIZUe81e4db2015-09-03 09:44:38 -0700130 }
131 return false;
132 }
133
134 @Override
135 public int write(ChannelBuffer c) {
136 int iStartIndex = c.writerIndex();
137 c.writeShort(TYPE);
138 c.writeShort(LENGTH);
139 c.writeBytes(rawValue);
140 return c.writerIndex() - iStartIndex;
141 }
142
143 /**
Mahesh Poojary Sf1bbd362016-02-25 18:19:59 +0530144 * Reads the channel buffer and returns object of IPv6RouterIdofRemoteNodeTlv.
Sho SHIMIZUe81e4db2015-09-03 09:44:38 -0700145 *
146 * @param c input channel buffer
Mahesh Poojary Sf1bbd362016-02-25 18:19:59 +0530147 * @return object of IPv6RouterIdofRemoteNodeTlv
Sho SHIMIZUe81e4db2015-09-03 09:44:38 -0700148 */
Mahesh Poojary Sf1bbd362016-02-25 18:19:59 +0530149 public static IPv6RouterIdofRemoteNodeSubTlv read20Bytes(ChannelBuffer c) {
Sho SHIMIZUe81e4db2015-09-03 09:44:38 -0700150 byte[] yTemp = new byte[20];
151 c.readBytes(yTemp, 0, 20);
Mahesh Poojary Sf1bbd362016-02-25 18:19:59 +0530152 return IPv6RouterIdofRemoteNodeSubTlv.of(yTemp);
Sho SHIMIZUe81e4db2015-09-03 09:44:38 -0700153 }
154
155 @Override
156 public String toString() {
157 ToStringHelper toStrHelper = MoreObjects.toStringHelper(getClass());
158
159 toStrHelper.add("Type", TYPE);
160 toStrHelper.add("Length", LENGTH);
161
162 StringBuffer result = new StringBuffer();
163 for (byte b : rawValue) {
164 result.append(String.format("%02X ", b));
165 }
166 toStrHelper.add("Value", result);
167
168 return toStrHelper.toString();
169 }
170}