blob: ea7f277287f71122d06ed24d8fec1759081911b5 [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
18import java.util.Iterator;
19import java.util.LinkedList;
Mahesh Poojary Sf1bbd362016-02-25 18:19:59 +053020import java.util.List;
Sho SHIMIZUe81e4db2015-09-03 09:44:38 -070021import java.util.ListIterator;
22import java.util.Objects;
23
24import org.jboss.netty.buffer.ChannelBuffer;
25import org.onosproject.pcepio.exceptions.PcepParseException;
26import org.onosproject.pcepio.protocol.PcepVersion;
27import org.slf4j.Logger;
28import org.slf4j.LoggerFactory;
29
30import com.google.common.base.MoreObjects;
31
32/**
33 * Provides Local TE Node Descriptors TLV which contains Node Descriptor Sub-TLVs.
34 */
Mahesh Poojary Sf1bbd362016-02-25 18:19:59 +053035public class LocalNodeDescriptorsTlv implements PcepValueType {
Sho SHIMIZUe81e4db2015-09-03 09:44:38 -070036
Mahesh Poojary Sf1bbd362016-02-25 18:19:59 +053037 /* REFERENCE :draft-dhodylee-pce-pcep-ls-01, section 9.2.2
Sho SHIMIZUe81e4db2015-09-03 09:44:38 -070038 * 0 1 2 3
39 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
40 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
41 | Type=[TBD8] | Length |
42 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
43 | |
44 // Node Descriptor Sub-TLVs (variable) //
45 | |
46 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
47 Note: Length is including header here. Refer Routing Universe TLV.
48 */
49
Ray Milkey9c9cde42018-01-12 14:22:06 -080050 private static final Logger log = LoggerFactory.getLogger(LocalNodeDescriptorsTlv.class);
Sho SHIMIZUe81e4db2015-09-03 09:44:38 -070051
Mahesh Poojary Sf1bbd362016-02-25 18:19:59 +053052 public static final short TYPE = (short) 65282;
Ray Milkeye4afdb52017-04-05 09:42:04 -070053 short hLength;
Sho SHIMIZUe81e4db2015-09-03 09:44:38 -070054
55 public static final int TLV_HEADER_LENGTH = 4;
56 // Node Descriptor Sub-TLVs (variable)
Mahesh Poojary Sf1bbd362016-02-25 18:19:59 +053057 private List<PcepValueType> llNodeDescriptorSubTLVs;
Sho SHIMIZUe81e4db2015-09-03 09:44:38 -070058
59 /**
60 * Constructor to initialize llNodeDescriptorSubTLVs.
61 *
Mahesh Poojary Sf1bbd362016-02-25 18:19:59 +053062 * @param llNodeDescriptorSubTLVs List of PcepValueType
Sho SHIMIZUe81e4db2015-09-03 09:44:38 -070063 */
Mahesh Poojary Sf1bbd362016-02-25 18:19:59 +053064 public LocalNodeDescriptorsTlv(List<PcepValueType> llNodeDescriptorSubTLVs) {
Sho SHIMIZUe81e4db2015-09-03 09:44:38 -070065 this.llNodeDescriptorSubTLVs = llNodeDescriptorSubTLVs;
66 }
67
68 /**
Mahesh Poojary Sf1bbd362016-02-25 18:19:59 +053069 * Returns a new object of LocalNodeDescriptorsTLV.
Sho SHIMIZUe81e4db2015-09-03 09:44:38 -070070 *
71 * @param llNodeDescriptorSubTLVs linked list of Node Descriptor Sub TLVs
Mahesh Poojary Sf1bbd362016-02-25 18:19:59 +053072 * @return object of LocalNodeDescriptorsTLV
Sho SHIMIZUe81e4db2015-09-03 09:44:38 -070073 */
Mahesh Poojary Sf1bbd362016-02-25 18:19:59 +053074 public static LocalNodeDescriptorsTlv of(final List<PcepValueType> llNodeDescriptorSubTLVs) {
75 return new LocalNodeDescriptorsTlv(llNodeDescriptorSubTLVs);
Sho SHIMIZUe81e4db2015-09-03 09:44:38 -070076 }
77
78 /**
79 * Returns Linked List of tlvs.
80 *
81 * @return llNodeDescriptorSubTLVs linked list of Node Descriptor Sub TLV
82 */
Mahesh Poojary Sf1bbd362016-02-25 18:19:59 +053083 public List<PcepValueType> getllNodeDescriptorSubTLVs() {
Sho SHIMIZUe81e4db2015-09-03 09:44:38 -070084 return llNodeDescriptorSubTLVs;
85 }
86
87 @Override
88 public PcepVersion getVersion() {
89 return PcepVersion.PCEP_1;
90 }
91
92 @Override
93 public short getType() {
94 return TYPE;
95 }
96
97 @Override
98 public short getLength() {
99 return hLength;
100 }
101
102 @Override
103 public int hashCode() {
104 return Objects.hash(llNodeDescriptorSubTLVs.hashCode());
105 }
106
107 @Override
108 public boolean equals(Object obj) {
109 if (this == obj) {
110 return true;
111 }
112
113 /*
114 * Here we have a list of Tlv so to compare each sub tlv between the object
115 * we have to take a list iterator so one by one we can get each sub tlv object
116 * and can compare them.
117 * it may be possible that the size of 2 lists is not equal so we have to first check
118 * the size, if both are same then we should check for the subtlv objects otherwise
119 * we should return false.
120 */
Mahesh Poojary Sf1bbd362016-02-25 18:19:59 +0530121 if (obj instanceof LocalNodeDescriptorsTlv) {
Sho SHIMIZUe81e4db2015-09-03 09:44:38 -0700122 int countObjSubTlv = 0;
123 int countOtherSubTlv = 0;
124 boolean isCommonSubTlv = true;
Mahesh Poojary Sf1bbd362016-02-25 18:19:59 +0530125 LocalNodeDescriptorsTlv other = (LocalNodeDescriptorsTlv) obj;
126 Iterator<PcepValueType> objListIterator = ((LocalNodeDescriptorsTlv) obj).llNodeDescriptorSubTLVs
Sho SHIMIZUe81e4db2015-09-03 09:44:38 -0700127 .iterator();
Mahesh Poojary Sf1bbd362016-02-25 18:19:59 +0530128 countObjSubTlv = ((LocalNodeDescriptorsTlv) obj).llNodeDescriptorSubTLVs.size();
Sho SHIMIZUe81e4db2015-09-03 09:44:38 -0700129 countOtherSubTlv = other.llNodeDescriptorSubTLVs.size();
130 if (countObjSubTlv != countOtherSubTlv) {
131 return false;
132 } else {
133 while (objListIterator.hasNext() && isCommonSubTlv) {
134 PcepValueType subTlv = objListIterator.next();
135 isCommonSubTlv = Objects.equals(llNodeDescriptorSubTLVs.contains(subTlv),
136 other.llNodeDescriptorSubTLVs.contains(subTlv));
137 }
138 return isCommonSubTlv;
139 }
140 }
141 return false;
142 }
143
144 @Override
145 public int write(ChannelBuffer c) {
146 int tlvStartIndex = c.writerIndex();
147 c.writeShort(TYPE);
148 int tlvLenIndex = c.writerIndex();
149 hLength = 0;
150 c.writeShort(0);
151
152 ListIterator<PcepValueType> listIterator = llNodeDescriptorSubTLVs.listIterator();
153
154 while (listIterator.hasNext()) {
155 PcepValueType tlv = listIterator.next();
Sho SHIMIZUde09fa02015-09-03 09:39:52 -0700156 if (tlv == null) {
Sho SHIMIZUe81e4db2015-09-03 09:44:38 -0700157 log.debug("TLV is null from subTlv list");
158 continue;
159 }
160 tlv.write(c);
161
162 // need to take care of padding
163 int pad = tlv.getLength() % 4;
164
165 if (0 != pad) {
166 pad = 4 - pad;
167 for (int i = 0; i < pad; ++i) {
168 c.writeByte((byte) 0);
169 }
170 }
171 }
172 hLength = (short) (c.writerIndex() - tlvStartIndex);
173 c.setShort(tlvLenIndex, (hLength - TLV_HEADER_LENGTH));
174 return c.writerIndex() - tlvStartIndex;
175 }
176
177 /**
178 * Reads the channel buffer and returns object of AutonomousSystemTlv.
179 *
180 * @param c input channel buffer
181 * @param hLength length of subtlvs.
182 * @return object of AutonomousSystemTlv
183 * @throws PcepParseException if mandatory fields are missing
184 */
185 public static PcepValueType read(ChannelBuffer c, short hLength) throws PcepParseException {
186
187 // Node Descriptor Sub-TLVs (variable)
Mahesh Poojary Sf1bbd362016-02-25 18:19:59 +0530188 List<PcepValueType> llNodeDescriptorSubTLVs = new LinkedList<>();
Sho SHIMIZUe81e4db2015-09-03 09:44:38 -0700189
190 ChannelBuffer tempCb = c.readBytes(hLength);
191
192 while (TLV_HEADER_LENGTH <= tempCb.readableBytes()) {
193
194 PcepValueType tlv;
195 short hType = tempCb.readShort();
196 int iValue = 0;
197 short length = tempCb.readShort();
198
199 switch (hType) {
200
Mahesh Poojary Sf1bbd362016-02-25 18:19:59 +0530201 case AutonomousSystemSubTlv.TYPE:
Sho SHIMIZUe81e4db2015-09-03 09:44:38 -0700202 iValue = tempCb.readInt();
Mahesh Poojary Sf1bbd362016-02-25 18:19:59 +0530203 tlv = new AutonomousSystemSubTlv(iValue);
Sho SHIMIZUe81e4db2015-09-03 09:44:38 -0700204 break;
Mahesh Poojary Sf1bbd362016-02-25 18:19:59 +0530205 case BgpLsIdentifierSubTlv.TYPE:
Sho SHIMIZUe81e4db2015-09-03 09:44:38 -0700206 iValue = tempCb.readInt();
Mahesh Poojary Sf1bbd362016-02-25 18:19:59 +0530207 tlv = new BgpLsIdentifierSubTlv(iValue);
Sho SHIMIZUe81e4db2015-09-03 09:44:38 -0700208 break;
Jonathan Hart51539b82015-10-29 09:53:04 -0700209 case OspfAreaIdSubTlv.TYPE:
Sho SHIMIZUe81e4db2015-09-03 09:44:38 -0700210 iValue = tempCb.readInt();
Jonathan Hart51539b82015-10-29 09:53:04 -0700211 tlv = new OspfAreaIdSubTlv(iValue);
Sho SHIMIZUe81e4db2015-09-03 09:44:38 -0700212 break;
Mahesh Poojary Sf1bbd362016-02-25 18:19:59 +0530213 case IgpRouterIdSubTlv.TYPE:
214 tlv = IgpRouterIdSubTlv.read(tempCb, length);
Sho SHIMIZUe81e4db2015-09-03 09:44:38 -0700215 break;
216
217 default:
218 throw new PcepParseException("Unsupported Sub TLV type :" + hType);
219 }
220
221 // Check for the padding
222 int pad = length % 4;
223 if (0 < pad) {
224 pad = 4 - pad;
225 if (pad <= tempCb.readableBytes()) {
226 tempCb.skipBytes(pad);
227 }
228 }
229
230 llNodeDescriptorSubTLVs.add(tlv);
231 }
232
233 if (0 < tempCb.readableBytes()) {
234 throw new PcepParseException("Sub Tlv parsing error. Extra bytes received.");
235 }
Mahesh Poojary Sf1bbd362016-02-25 18:19:59 +0530236 return new LocalNodeDescriptorsTlv(llNodeDescriptorSubTLVs);
Sho SHIMIZUe81e4db2015-09-03 09:44:38 -0700237 }
238
239 @Override
240 public String toString() {
Sho SHIMIZU7cdbcf72015-09-03 14:43:05 -0700241 return MoreObjects.toStringHelper(getClass())
242 .add("Type", TYPE)
243 .add("Length", hLength)
244 .add("NodeDescriptorSubTLVs", llNodeDescriptorSubTLVs)
245 .toString();
Sho SHIMIZUe81e4db2015-09-03 09:44:38 -0700246 }
247}