2 * Copyright 2015 Open Networking Laboratory
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package org.onosproject.pcepio.types;
18 import java.util.Iterator;
19 import java.util.LinkedList;
20 import java.util.ListIterator;
21 import java.util.Objects;
23 import org.jboss.netty.buffer.ChannelBuffer;
24 import org.onosproject.pcepio.exceptions.PcepParseException;
25 import org.onosproject.pcepio.protocol.PcepVersion;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
29 import com.google.common.base.MoreObjects;
32 * Provides Local TE Node Descriptors TLV which contains Node Descriptor Sub-TLVs.
34 public class LocalTENodeDescriptorsTlv implements PcepValueType {
36 /* REFERENCE :draft-ietf-idr-ls-distribution-10
38 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
39 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
40 | Type=[TBD8] | Length |
41 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
43 // Node Descriptor Sub-TLVs (variable) //
45 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
46 Note: Length is including header here. Refer Routing Universe TLV.
49 protected static final Logger log = LoggerFactory.getLogger(LocalTENodeDescriptorsTlv.class);
51 public static final short TYPE = 1637; //TODD:change this TBD8
54 public static final int TLV_HEADER_LENGTH = 4;
55 // Node Descriptor Sub-TLVs (variable)
56 private LinkedList<PcepValueType> llNodeDescriptorSubTLVs;
59 * Constructor to initialize llNodeDescriptorSubTLVs.
61 * @param llNodeDescriptorSubTLVs LinkedList of PcepValueType
63 public LocalTENodeDescriptorsTlv(LinkedList<PcepValueType> llNodeDescriptorSubTLVs) {
64 this.llNodeDescriptorSubTLVs = llNodeDescriptorSubTLVs;
68 * Returns a new object of LocalTENodeDescriptorsTLV.
70 * @param llNodeDescriptorSubTLVs linked list of Node Descriptor Sub TLVs
71 * @return object of LocalTENodeDescriptorsTLV
73 public static LocalTENodeDescriptorsTlv of(final LinkedList<PcepValueType> llNodeDescriptorSubTLVs) {
74 return new LocalTENodeDescriptorsTlv(llNodeDescriptorSubTLVs);
78 * Returns Linked List of tlvs.
80 * @return llNodeDescriptorSubTLVs linked list of Node Descriptor Sub TLV
82 public LinkedList<PcepValueType> getllNodeDescriptorSubTLVs() {
83 return llNodeDescriptorSubTLVs;
87 public PcepVersion getVersion() {
88 return PcepVersion.PCEP_1;
92 public short getType() {
97 public short getLength() {
102 public int hashCode() {
103 return Objects.hash(llNodeDescriptorSubTLVs.hashCode());
107 public boolean equals(Object obj) {
113 * Here we have a list of Tlv so to compare each sub tlv between the object
114 * we have to take a list iterator so one by one we can get each sub tlv object
115 * and can compare them.
116 * it may be possible that the size of 2 lists is not equal so we have to first check
117 * the size, if both are same then we should check for the subtlv objects otherwise
118 * we should return false.
120 if (obj instanceof LocalTENodeDescriptorsTlv) {
121 int countObjSubTlv = 0;
122 int countOtherSubTlv = 0;
123 boolean isCommonSubTlv = true;
124 LocalTENodeDescriptorsTlv other = (LocalTENodeDescriptorsTlv) obj;
125 Iterator<PcepValueType> objListIterator = ((LocalTENodeDescriptorsTlv) obj).llNodeDescriptorSubTLVs
127 countObjSubTlv = ((LocalTENodeDescriptorsTlv) obj).llNodeDescriptorSubTLVs.size();
128 countOtherSubTlv = other.llNodeDescriptorSubTLVs.size();
129 if (countObjSubTlv != countOtherSubTlv) {
132 while (objListIterator.hasNext() && isCommonSubTlv) {
133 PcepValueType subTlv = objListIterator.next();
134 isCommonSubTlv = Objects.equals(llNodeDescriptorSubTLVs.contains(subTlv),
135 other.llNodeDescriptorSubTLVs.contains(subTlv));
137 return isCommonSubTlv;
144 public int write(ChannelBuffer c) {
145 int tlvStartIndex = c.writerIndex();
147 int tlvLenIndex = c.writerIndex();
151 ListIterator<PcepValueType> listIterator = llNodeDescriptorSubTLVs.listIterator();
153 while (listIterator.hasNext()) {
154 PcepValueType tlv = listIterator.next();
156 log.debug("TLV is null from subTlv list");
161 // need to take care of padding
162 int pad = tlv.getLength() % 4;
166 for (int i = 0; i < pad; ++i) {
167 c.writeByte((byte) 0);
171 hLength = (short) (c.writerIndex() - tlvStartIndex);
172 c.setShort(tlvLenIndex, (hLength - TLV_HEADER_LENGTH));
173 return c.writerIndex() - tlvStartIndex;
177 * Reads the channel buffer and returns object of AutonomousSystemTlv.
179 * @param c input channel buffer
180 * @param hLength length of subtlvs.
181 * @return object of AutonomousSystemTlv
182 * @throws PcepParseException if mandatory fields are missing
184 public static PcepValueType read(ChannelBuffer c, short hLength) throws PcepParseException {
186 // Node Descriptor Sub-TLVs (variable)
187 LinkedList<PcepValueType> llNodeDescriptorSubTLVs = new LinkedList<>();
189 ChannelBuffer tempCb = c.readBytes(hLength);
191 while (TLV_HEADER_LENGTH <= tempCb.readableBytes()) {
194 short hType = tempCb.readShort();
196 short length = tempCb.readShort();
200 case AutonomousSystemTlv.TYPE:
201 iValue = tempCb.readInt();
202 tlv = new AutonomousSystemTlv(iValue);
204 case BGPLSidentifierTlv.TYPE:
205 iValue = tempCb.readInt();
206 tlv = new BGPLSidentifierTlv(iValue);
208 case OSPFareaIDsubTlv.TYPE:
209 iValue = tempCb.readInt();
210 tlv = new OSPFareaIDsubTlv(iValue);
212 case RouterIDSubTlv.TYPE:
213 tlv = RouterIDSubTlv.read(tempCb, length);
217 throw new PcepParseException("Unsupported Sub TLV type :" + hType);
220 // Check for the padding
221 int pad = length % 4;
224 if (pad <= tempCb.readableBytes()) {
225 tempCb.skipBytes(pad);
229 llNodeDescriptorSubTLVs.add(tlv);
232 if (0 < tempCb.readableBytes()) {
233 throw new PcepParseException("Sub Tlv parsing error. Extra bytes received.");
235 return new LocalTENodeDescriptorsTlv(llNodeDescriptorSubTLVs);
239 public String toString() {
240 return MoreObjects.toStringHelper(getClass())
242 .add("Length", hLength)
243 .add("NodeDescriptorSubTLVs", llNodeDescriptorSubTLVs)