blob: ce1d04d81d8c77b1bdd4c9651d079e52eb152245 [file] [log] [blame]
Sho SHIMIZUe81e4db2015-09-03 09:44:38 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2015-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 */
16
17package org.onosproject.pcepio.types;
18
Jonathan Hart51539b82015-10-29 09:53:04 -070019import com.google.common.base.MoreObjects;
20import com.google.common.base.MoreObjects.ToStringHelper;
Sho SHIMIZUe81e4db2015-09-03 09:44:38 -070021import org.jboss.netty.buffer.ChannelBuffer;
22import org.onosproject.pcepio.protocol.PcepVersion;
23import org.slf4j.Logger;
24import org.slf4j.LoggerFactory;
25
Saritha8539a2e2017-01-05 11:58:54 +053026import java.util.Arrays;
Sho SHIMIZUe81e4db2015-09-03 09:44:38 -070027
28/**
29 * NexthopIPv6addressTlv provides Ipv6 address of next hop.
30 */
31public class NexthopIPv6addressTlv implements PcepValueType {
32
33 /*
34 Reference: draft-zhao-pce-pcep-extension-for-pce-controller-01.
35
36 0 1 2 3
37 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
38 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
39 | Type=TBD | Length = 20 |
40 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
41 | |
42 // nexthop IPv6 address (16 bytes) //
43 | |
44 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
45
46 NEXTHOP-IPV6-ADDRESS TLV:
47
48 */
49 protected static final Logger log = LoggerFactory.getLogger(NexthopIPv6addressTlv.class);
50
51 public static final short TYPE = 100; //to be defined
52 //Length is header + value
53 public static final short LENGTH = 20;
54 public static final short VALUE_LENGTH = 16;
55
56 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 };
57 public static final NexthopIPv6addressTlv NONE = new NexthopIPv6addressTlv(NONE_VAL);
58
59 private static final byte[] NO_MASK_VAL = {(byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
60 (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
61 (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF };
62 public static final NexthopIPv6addressTlv NO_MASK = new NexthopIPv6addressTlv(NO_MASK_VAL);
63 public static final NexthopIPv6addressTlv FULL_MASK = NONE;
64
65 private final byte[] rawValue;
66
67 /**
68 * Constructor to initialize IP address for next hop IPv6 address tlv.
69 *
70 * @param rawValue value of Next hop ipAddress
71 */
72 public NexthopIPv6addressTlv(byte[] rawValue) {
73 log.debug("NexthopIPv6addressTlv");
74 this.rawValue = rawValue;
75 }
76
77 /**
78 * Creates next hop IPv6 address tlv.
79 *
80 * @param raw value of Next hop ipAddress
81 * @return object of NexthopIPv6addressTlv
82 */
83 //logic to be checked
84 public static NexthopIPv6addressTlv of(final byte[] raw) {
85 //check NONE_VAL
Jonathan Hart51539b82015-10-29 09:53:04 -070086 boolean bFoundNone = true;
Sho SHIMIZUe81e4db2015-09-03 09:44:38 -070087 //value starts from 3rd byte.
88 for (int i = 5; i < 20; ++i) {
89 if (NONE_VAL[i] != raw[i]) {
Jonathan Hart51539b82015-10-29 09:53:04 -070090 bFoundNone = false;
Sho SHIMIZUe81e4db2015-09-03 09:44:38 -070091 }
92 }
93
Jonathan Hart51539b82015-10-29 09:53:04 -070094 if (bFoundNone) {
Sho SHIMIZUe81e4db2015-09-03 09:44:38 -070095 return NONE;
96 }
97
98 //check NO_MASK_VAL
99 boolean bFoundNoMask = true;
100 //value starts from 3rd byte.
101 for (int i = 5; i < 20; ++i) {
Yuta HIGUCHI1edc36b2018-01-24 23:39:06 -0800102 if ((byte) 0xFF != raw[i]) {
Sho SHIMIZUe81e4db2015-09-03 09:44:38 -0700103 bFoundNoMask = false;
104 }
105 }
106 if (bFoundNoMask) {
107 return NO_MASK;
108 }
109 return new NexthopIPv6addressTlv(raw);
110 }
111
112 /**
113 * Returns next hop IPv6 address.
114 *
115 * @return next hop IPv6 address
116 */
117 public byte[] getBytes() {
118 return rawValue;
119 }
120
121 @Override
122 public PcepVersion getVersion() {
123 return PcepVersion.PCEP_1;
124 }
125
126 @Override
127 public short getType() {
128 return TYPE;
129 }
130
131 @Override
132 public short getLength() {
133 return LENGTH;
134 }
135
136 @Override
137 public int hashCode() {
Saritha8539a2e2017-01-05 11:58:54 +0530138 return Arrays.hashCode(rawValue);
Sho SHIMIZUe81e4db2015-09-03 09:44:38 -0700139 }
140
141 @Override
142 public boolean equals(Object obj) {
143 if (this == obj) {
144 return true;
145 }
146 if (obj instanceof NexthopIPv6addressTlv) {
147 NexthopIPv6addressTlv other = (NexthopIPv6addressTlv) obj;
Saritha8539a2e2017-01-05 11:58:54 +0530148 return Arrays.equals(this.rawValue, other.rawValue);
Sho SHIMIZUe81e4db2015-09-03 09:44:38 -0700149 }
150 return false;
151 }
152
153 @Override
154 public int write(ChannelBuffer c) {
155 int iStartIndex = c.writerIndex();
156 c.writeShort(TYPE);
157 c.writeShort(LENGTH);
158 c.writeBytes(rawValue);
159 return c.writerIndex() - iStartIndex;
160 }
161
162 /**
163 * Reads the channel buffer and returns object of NexthopIPv6addressTlv.
164 *
165 * @param c type of channel buffer
166 * @return object of NexthopIPv6addressTlv
167 */
168 public static NexthopIPv6addressTlv read(ChannelBuffer c) {
169 byte[] yTemp = new byte[20];
170 c.readBytes(yTemp, 0, 20);
171 return NexthopIPv6addressTlv.of(yTemp);
172 }
173
174 @Override
175 public String toString() {
176 ToStringHelper toStrHelper = MoreObjects.toStringHelper(getClass());
177
178 toStrHelper.add("Type", TYPE);
179 toStrHelper.add("Length", LENGTH);
180
181 StringBuffer result = new StringBuffer();
182 for (byte b : rawValue) {
183 result.append(String.format("%02X ", b));
184 }
185 toStrHelper.add("IpAddress", result);
186
187 return toStrHelper.toString();
188 }
189}