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 Remote TE Node Descriptors TLV.
34 public class RemoteTENodeDescriptorsTlv implements PcepValueType {
36 /* Reference :PCEP Extension for Transporting TE Data
37 draft-dhodylee-pce-pcep-te-data-extn-02
40 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
41 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
42 | Type=[TBD9] | Length |
43 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
45 // Node Descriptor Sub-TLVs (variable) //
47 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
50 protected static final Logger log = LoggerFactory.getLogger(RemoteTENodeDescriptorsTlv.class);
52 public static final short TYPE = 1003; //TODD:change this TBD9
55 public static final int TLV_HEADER_LENGTH = 4;
56 // Node Descriptor Sub-TLVs (variable)
57 private LinkedList<PcepValueType> llRemoteTENodeDescriptorSubTLVs;
60 * Constructor to initialize llRemoteTENodeDescriptorSubTLVs.
62 * @param llRemoteTENodeDescriptorSubTLVs LinkedList of PcepValueType
64 public RemoteTENodeDescriptorsTlv(LinkedList<PcepValueType> llRemoteTENodeDescriptorSubTLVs) {
65 this.llRemoteTENodeDescriptorSubTLVs = llRemoteTENodeDescriptorSubTLVs;
69 * Returns object of Remote TE Node Descriptors TLV.
71 * @param llRemoteTENodeDescriptorSubTLVs LinkedList of PcepValueType
72 * @return object of RemoteTENodeDescriptorsTLV
74 public static RemoteTENodeDescriptorsTlv of(final LinkedList<PcepValueType> llRemoteTENodeDescriptorSubTLVs) {
75 return new RemoteTENodeDescriptorsTlv(llRemoteTENodeDescriptorSubTLVs);
79 * Returns Remote TE Node Descriptor Sub TLVs.
81 * @return llRemoteTENodeDescriptorSubTLVs
83 public LinkedList<PcepValueType> getllRemoteTENodeDescriptorSubTLVs() {
84 return llRemoteTENodeDescriptorSubTLVs;
88 public PcepVersion getVersion() {
89 return PcepVersion.PCEP_1;
93 public short getType() {
98 public short getLength() {
103 public int hashCode() {
104 return Objects.hash(llRemoteTENodeDescriptorSubTLVs.hashCode());
108 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 RemoteTENodeDescriptorsTlv) {
121 int countObjSubTlv = 0;
122 int countOtherSubTlv = 0;
123 boolean isCommonSubTlv = true;
124 RemoteTENodeDescriptorsTlv other = (RemoteTENodeDescriptorsTlv) obj;
125 Iterator<PcepValueType> objListIterator = ((RemoteTENodeDescriptorsTlv) obj).llRemoteTENodeDescriptorSubTLVs
127 countObjSubTlv = ((RemoteTENodeDescriptorsTlv) obj).llRemoteTENodeDescriptorSubTLVs.size();
128 countOtherSubTlv = other.llRemoteTENodeDescriptorSubTLVs.size();
129 if (countObjSubTlv != countOtherSubTlv) {
132 while (objListIterator.hasNext() && isCommonSubTlv) {
133 PcepValueType subTlv = objListIterator.next();
134 isCommonSubTlv = Objects.equals(llRemoteTENodeDescriptorSubTLVs.contains(subTlv),
135 other.llRemoteTENodeDescriptorSubTLVs.contains(subTlv));
137 return isCommonSubTlv;
144 public int write(ChannelBuffer c) {
146 int tlvStartIndex = c.writerIndex();
148 int tlvLenIndex = c.writerIndex();
150 c.writeShort(hLength);
152 ListIterator<PcepValueType> listIterator = llRemoteTENodeDescriptorSubTLVs.listIterator();
154 while (listIterator.hasNext()) {
155 PcepValueType tlv = listIterator.next();
158 log.debug("TLV is null from subTlv list");
163 // need to take care of padding
164 int pad = tlv.getLength() % 4;
168 for (int i = 0; i < pad; ++i) {
169 c.writeByte((byte) 0);
174 hLength = (short) (c.writerIndex() - tlvStartIndex);
175 c.setShort(tlvLenIndex, (hLength - TLV_HEADER_LENGTH));
177 return c.writerIndex() - tlvStartIndex;
181 * Reads channel buffer and returns object of Remote TE Node Descriptors TLV.
183 * @param c input channel buffer
184 * @param length length of buffer
185 * @return object of RemoteTENodeDescriptorsTLV
186 * @throws PcepParseException if mandatory fields are missing
188 public static PcepValueType read(ChannelBuffer c , short length) throws PcepParseException {
190 // Node Descriptor Sub-TLVs (variable)
191 LinkedList<PcepValueType> llRemoteTENodeDescriptorSubTLVs = new LinkedList<>();
193 ChannelBuffer tempCb = c.readBytes(length);
195 while (TLV_HEADER_LENGTH <= tempCb.readableBytes()) {
198 short hType = tempCb.readShort();
200 short hLength = tempCb.readShort();
203 case AutonomousSystemTlv.TYPE:
204 iValue = tempCb.readInt();
205 tlv = new AutonomousSystemTlv(iValue);
207 case BGPLSidentifierTlv.TYPE:
208 iValue = tempCb.readInt();
209 tlv = new BGPLSidentifierTlv(iValue);
211 case OSPFareaIDsubTlv.TYPE:
212 iValue = tempCb.readInt();
213 tlv = new OSPFareaIDsubTlv(iValue);
215 case RouterIDSubTlv.TYPE:
216 tlv = RouterIDSubTlv.read(tempCb, hLength);
220 throw new PcepParseException("Unsupported Sub TLV type :" + hType);
223 // Check for the padding
224 int pad = hLength % 4;
227 if (pad <= tempCb.readableBytes()) {
228 tempCb.skipBytes(pad);
232 llRemoteTENodeDescriptorSubTLVs.add(tlv);
235 if (0 < tempCb.readableBytes()) {
237 throw new PcepParseException("Sub Tlv parsing error. Extra bytes received.");
239 return new RemoteTENodeDescriptorsTlv(llRemoteTENodeDescriptorSubTLVs);
243 public String toString() {
244 return MoreObjects.toStringHelper(getClass())
246 .add("Length", hLength)
247 .add("RemoteTeNodeDescriptorSubTLVs", llRemoteTENodeDescriptorSubTLVs)