blob: 1fc7124c9065edc5e0a2386f8555b3c5fcac09fb [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
18import java.util.Objects;
19
20import org.jboss.netty.buffer.ChannelBuffer;
21import org.onosproject.pcepio.protocol.PcepVersion;
22import org.slf4j.Logger;
23import org.slf4j.LoggerFactory;
24
25import com.google.common.base.MoreObjects;
26import com.google.common.base.MoreObjects.ToStringHelper;
27
28/**
29 * Provides IPv6 TE Router Id of Remote Node. Reference :[RFC6119]/4.1.
30 */
31public class IPv6TERouterIdofRemoteNodeTlv implements PcepValueType {
32 protected static final Logger log = LoggerFactory.getLogger(IPv6TERouterIdofRemoteNodeTlv.class);
33
34 public static final short TYPE = 1400; //TDB29
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 IPv6TERouterIdofRemoteNodeTlv NONE = new IPv6TERouterIdofRemoteNodeTlv(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 IPv6TERouterIdofRemoteNodeTlv NO_MASK = new IPv6TERouterIdofRemoteNodeTlv(NO_MASK_VAL);
45 public static final IPv6TERouterIdofRemoteNodeTlv FULL_MASK = NONE;
46
47 private final byte[] rawValue;
48
49 /**
50 * constructor to initialize rawValue.
51 *
52 * @param rawValue IPv6TERouterIdofRemoteNodeTlv
53 */
54 public IPv6TERouterIdofRemoteNodeTlv(byte[] rawValue) {
55 log.debug("IPv6TERouterIdofRemoteNodeTlv");
56 this.rawValue = rawValue;
57 }
58
59 /**
60 * Returns newly created IPv6TERouterIdofRemoteNodeTlv object.
61 *
62 * @param raw IPv6 TE Router Id of RemoteNode
63 * @return object of IPv6TERouterIdofRemoteNodeTlv
64 */
65 public static IPv6TERouterIdofRemoteNodeTlv of(final byte[] raw) {
66 //check NONE_VAL
67 boolean bFoundNONE = true;
68 //value starts from 3rd byte.
69 for (int i = 2; i < 20; ++i) {
70 if (NONE_VAL[i] != raw[i]) {
71 bFoundNONE = false;
72 }
73 }
74
75 if (bFoundNONE) {
76 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 IPv6TERouterIdofRemoteNodeTlv(raw);
92 }
93
94 /**
95 * Returns value of IPv6 TE Router Id of Remote Node.
96 *
97 * @return byte array value of rawValue
98 */
99 public byte[] getBytes() {
100 return rawValue;
101 }
102
103 @Override
104 public PcepVersion getVersion() {
105 return PcepVersion.PCEP_1;
106 }
107
108 @Override
109 public short getType() {
110 return TYPE;
111 }
112
113 @Override
114 public short getLength() {
115 return LENGTH;
116 }
117
118 @Override
119 public int hashCode() {
120 return Objects.hash(rawValue);
121 }
122
123 @Override
124 public boolean equals(Object obj) {
125 if (this == obj) {
126 return true;
127 }
128 if (obj instanceof IPv6TERouterIdofRemoteNodeTlv) {
129 IPv6TERouterIdofRemoteNodeTlv other = (IPv6TERouterIdofRemoteNodeTlv) obj;
130 return Objects.equals(rawValue, other.rawValue);
131 }
132 return false;
133 }
134
135 @Override
136 public int write(ChannelBuffer c) {
137 int iStartIndex = c.writerIndex();
138 c.writeShort(TYPE);
139 c.writeShort(LENGTH);
140 c.writeBytes(rawValue);
141 return c.writerIndex() - iStartIndex;
142 }
143
144 /**
145 * Reads the channel buffer and returns object of IPv6TERouterIdofRemoteNodeTlv.
146 *
147 * @param c input channel buffer
148 * @return object of IPv6TERouterIdofRemoteNodeTlv
149 */
150 public static IPv6TERouterIdofRemoteNodeTlv read20Bytes(ChannelBuffer c) {
151 byte[] yTemp = new byte[20];
152 c.readBytes(yTemp, 0, 20);
153 return IPv6TERouterIdofRemoteNodeTlv.of(yTemp);
154 }
155
156 @Override
157 public String toString() {
158 ToStringHelper toStrHelper = MoreObjects.toStringHelper(getClass());
159
160 toStrHelper.add("Type", TYPE);
161 toStrHelper.add("Length", LENGTH);
162
163 StringBuffer result = new StringBuffer();
164 for (byte b : rawValue) {
165 result.append(String.format("%02X ", b));
166 }
167 toStrHelper.add("Value", result);
168
169 return toStrHelper.toString();
170 }
171}