blob: dd247885af8f0afd99b457091c9d6cf0e3cc67be [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 TE Router Id of Remote Node. Reference :[RFC6119]/4.1.
29 */
30public class IPv6TERouterIdofRemoteNodeTlv implements PcepValueType {
31 protected static final Logger log = LoggerFactory.getLogger(IPv6TERouterIdofRemoteNodeTlv.class);
32
33 public static final short TYPE = 1400; //TDB29
34 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};
38 public static final IPv6TERouterIdofRemoteNodeTlv NONE = new IPv6TERouterIdofRemoteNodeTlv(NONE_VAL);
39
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};
43 public static final IPv6TERouterIdofRemoteNodeTlv NO_MASK = new IPv6TERouterIdofRemoteNodeTlv(NO_MASK_VAL);
44 public static final IPv6TERouterIdofRemoteNodeTlv FULL_MASK = NONE;
45
46 private final byte[] rawValue;
47
48 /**
49 * constructor to initialize rawValue.
50 *
51 * @param rawValue IPv6TERouterIdofRemoteNodeTlv
52 */
53 public IPv6TERouterIdofRemoteNodeTlv(byte[] rawValue) {
54 log.debug("IPv6TERouterIdofRemoteNodeTlv");
55 this.rawValue = rawValue;
56 }
57
58 /**
59 * Returns newly created IPv6TERouterIdofRemoteNodeTlv object.
60 *
61 * @param raw IPv6 TE Router Id of RemoteNode
62 * @return object of IPv6TERouterIdofRemoteNodeTlv
63 */
64 public static IPv6TERouterIdofRemoteNodeTlv of(final byte[] raw) {
65 //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) {
82 if (0xFF != raw[i]) {
83 bFoundNoMask = false;
84 }
85 }
86 if (bFoundNoMask) {
87 return NO_MASK;
88 }
89
90 return new IPv6TERouterIdofRemoteNodeTlv(raw);
91 }
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 }
127 if (obj instanceof IPv6TERouterIdofRemoteNodeTlv) {
128 IPv6TERouterIdofRemoteNodeTlv other = (IPv6TERouterIdofRemoteNodeTlv) 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 /**
144 * Reads the channel buffer and returns object of IPv6TERouterIdofRemoteNodeTlv.
145 *
146 * @param c input channel buffer
147 * @return object of IPv6TERouterIdofRemoteNodeTlv
148 */
149 public static IPv6TERouterIdofRemoteNodeTlv read20Bytes(ChannelBuffer c) {
150 byte[] yTemp = new byte[20];
151 c.readBytes(yTemp, 0, 20);
152 return IPv6TERouterIdofRemoteNodeTlv.of(yTemp);
153 }
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}