blob: 5c9751da1509ff86f81f38ad457f8b60def6c3ce [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 Remote TE Node Descriptors TLV.
34 */
Mahesh Poojary Sf1bbd362016-02-25 18:19:59 +053035public class RemoteNodeDescriptorsTlv 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.3.
Sho SHIMIZUe81e4db2015-09-03 09:44:38 -070038 *
39 0 1 2 3
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 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
44 | |
45 // Node Descriptor Sub-TLVs (variable) //
46 | |
47 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
48 */
49
Ray Milkey9c9cde42018-01-12 14:22:06 -080050 private static final Logger log = LoggerFactory.getLogger(RemoteNodeDescriptorsTlv.class);
Sho SHIMIZUe81e4db2015-09-03 09:44:38 -070051
Mahesh Poojary Sf1bbd362016-02-25 18:19:59 +053052 public static final short TYPE = (short) 65283;
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> llRemoteTENodeDescriptorSubTLVs;
Sho SHIMIZUe81e4db2015-09-03 09:44:38 -070058
59 /**
60 * Constructor to initialize llRemoteTENodeDescriptorSubTLVs.
61 *
Mahesh Poojary Sf1bbd362016-02-25 18:19:59 +053062 * @param llRemoteTENodeDescriptorSubTLVs List of PcepValueType
Sho SHIMIZUe81e4db2015-09-03 09:44:38 -070063 */
Mahesh Poojary Sf1bbd362016-02-25 18:19:59 +053064 public RemoteNodeDescriptorsTlv(List<PcepValueType> llRemoteTENodeDescriptorSubTLVs) {
Sho SHIMIZUe81e4db2015-09-03 09:44:38 -070065 this.llRemoteTENodeDescriptorSubTLVs = llRemoteTENodeDescriptorSubTLVs;
66 }
67
68 /**
69 * Returns object of Remote TE Node Descriptors TLV.
70 *
Mahesh Poojary Sf1bbd362016-02-25 18:19:59 +053071 * @param llRemoteTENodeDescriptorSubTLVs List of PcepValueType
72 * @return object of RemoteNodeDescriptorsTlv
Sho SHIMIZUe81e4db2015-09-03 09:44:38 -070073 */
Mahesh Poojary Sf1bbd362016-02-25 18:19:59 +053074 public static RemoteNodeDescriptorsTlv of(final List<PcepValueType> llRemoteTENodeDescriptorSubTLVs) {
75 return new RemoteNodeDescriptorsTlv(llRemoteTENodeDescriptorSubTLVs);
Sho SHIMIZUe81e4db2015-09-03 09:44:38 -070076 }
77
78 /**
79 * Returns Remote TE Node Descriptor Sub TLVs.
80 *
81 * @return llRemoteTENodeDescriptorSubTLVs
82 */
Mahesh Poojary Sf1bbd362016-02-25 18:19:59 +053083 public List<PcepValueType> getllRemoteTENodeDescriptorSubTLVs() {
Sho SHIMIZUe81e4db2015-09-03 09:44:38 -070084 return llRemoteTENodeDescriptorSubTLVs;
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(llRemoteTENodeDescriptorSubTLVs.hashCode());
105 }
106
107 @Override
108 public boolean equals(Object obj) {
109 if (this == obj) {
110 return true;
111 }
112 /*
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.
119 */
Mahesh Poojary Sf1bbd362016-02-25 18:19:59 +0530120 if (obj instanceof RemoteNodeDescriptorsTlv) {
Sho SHIMIZUe81e4db2015-09-03 09:44:38 -0700121 int countObjSubTlv = 0;
122 int countOtherSubTlv = 0;
123 boolean isCommonSubTlv = true;
Mahesh Poojary Sf1bbd362016-02-25 18:19:59 +0530124 RemoteNodeDescriptorsTlv other = (RemoteNodeDescriptorsTlv) obj;
125 Iterator<PcepValueType> objListIterator = ((RemoteNodeDescriptorsTlv) obj).llRemoteTENodeDescriptorSubTLVs
Sho SHIMIZUe81e4db2015-09-03 09:44:38 -0700126 .iterator();
Mahesh Poojary Sf1bbd362016-02-25 18:19:59 +0530127 countObjSubTlv = ((RemoteNodeDescriptorsTlv) obj).llRemoteTENodeDescriptorSubTLVs.size();
Sho SHIMIZUe81e4db2015-09-03 09:44:38 -0700128 countOtherSubTlv = other.llRemoteTENodeDescriptorSubTLVs.size();
129 if (countObjSubTlv != countOtherSubTlv) {
130 return false;
131 } else {
132 while (objListIterator.hasNext() && isCommonSubTlv) {
133 PcepValueType subTlv = objListIterator.next();
134 isCommonSubTlv = Objects.equals(llRemoteTENodeDescriptorSubTLVs.contains(subTlv),
135 other.llRemoteTENodeDescriptorSubTLVs.contains(subTlv));
136 }
137 return isCommonSubTlv;
138 }
139 }
140 return false;
141 }
142
143 @Override
144 public int write(ChannelBuffer c) {
145
146 int tlvStartIndex = c.writerIndex();
147 c.writeShort(TYPE);
148 int tlvLenIndex = c.writerIndex();
149 hLength = 0;
150 c.writeShort(hLength);
151
152 ListIterator<PcepValueType> listIterator = llRemoteTENodeDescriptorSubTLVs.listIterator();
153
154 while (listIterator.hasNext()) {
155 PcepValueType tlv = listIterator.next();
156
Sho SHIMIZUde09fa02015-09-03 09:39:52 -0700157 if (tlv == null) {
Sho SHIMIZUe81e4db2015-09-03 09:44:38 -0700158 log.debug("TLV is null from subTlv list");
159 continue;
160 }
161 tlv.write(c);
162
163 // need to take care of padding
164 int pad = tlv.getLength() % 4;
165
166 if (0 != pad) {
167 pad = 4 - pad;
168 for (int i = 0; i < pad; ++i) {
169 c.writeByte((byte) 0);
170 }
171 }
172 }
173
174 hLength = (short) (c.writerIndex() - tlvStartIndex);
175 c.setShort(tlvLenIndex, (hLength - TLV_HEADER_LENGTH));
176
177 return c.writerIndex() - tlvStartIndex;
178 }
179
180 /**
181 * Reads channel buffer and returns object of Remote TE Node Descriptors TLV.
182 *
183 * @param c input channel buffer
Ray Milkey9b36d812015-09-09 15:24:54 -0700184 * @param length length of buffer
Mahesh Poojary Sf1bbd362016-02-25 18:19:59 +0530185 * @return object of RemoteNodeDescriptorsTlv
Sho SHIMIZUe81e4db2015-09-03 09:44:38 -0700186 * @throws PcepParseException if mandatory fields are missing
187 */
Jian Li68c4fc42016-01-11 16:07:03 -0800188 public static PcepValueType read(ChannelBuffer c, short length) throws PcepParseException {
Sho SHIMIZUe81e4db2015-09-03 09:44:38 -0700189
190 // Node Descriptor Sub-TLVs (variable)
Mahesh Poojary Sf1bbd362016-02-25 18:19:59 +0530191 List<PcepValueType> llRemoteTENodeDescriptorSubTLVs = new LinkedList<>();
Sho SHIMIZUe81e4db2015-09-03 09:44:38 -0700192
193 ChannelBuffer tempCb = c.readBytes(length);
194
195 while (TLV_HEADER_LENGTH <= tempCb.readableBytes()) {
196
197 PcepValueType tlv;
198 short hType = tempCb.readShort();
199 int iValue = 0;
200 short hLength = tempCb.readShort();
201 switch (hType) {
202
Mahesh Poojary Sf1bbd362016-02-25 18:19:59 +0530203 case AutonomousSystemSubTlv.TYPE:
Sho SHIMIZUe81e4db2015-09-03 09:44:38 -0700204 iValue = tempCb.readInt();
Mahesh Poojary Sf1bbd362016-02-25 18:19:59 +0530205 tlv = new AutonomousSystemSubTlv(iValue);
Sho SHIMIZUe81e4db2015-09-03 09:44:38 -0700206 break;
Mahesh Poojary Sf1bbd362016-02-25 18:19:59 +0530207 case BgpLsIdentifierSubTlv.TYPE:
Sho SHIMIZUe81e4db2015-09-03 09:44:38 -0700208 iValue = tempCb.readInt();
Mahesh Poojary Sf1bbd362016-02-25 18:19:59 +0530209 tlv = new BgpLsIdentifierSubTlv(iValue);
Sho SHIMIZUe81e4db2015-09-03 09:44:38 -0700210 break;
Jonathan Hart51539b82015-10-29 09:53:04 -0700211 case OspfAreaIdSubTlv.TYPE:
Sho SHIMIZUe81e4db2015-09-03 09:44:38 -0700212 iValue = tempCb.readInt();
Jonathan Hart51539b82015-10-29 09:53:04 -0700213 tlv = new OspfAreaIdSubTlv(iValue);
Sho SHIMIZUe81e4db2015-09-03 09:44:38 -0700214 break;
Mahesh Poojary Sf1bbd362016-02-25 18:19:59 +0530215 case IgpRouterIdSubTlv.TYPE:
216 tlv = IgpRouterIdSubTlv.read(tempCb, hLength);
Sho SHIMIZUe81e4db2015-09-03 09:44:38 -0700217 break;
218
219 default:
220 throw new PcepParseException("Unsupported Sub TLV type :" + hType);
221 }
222
223 // Check for the padding
224 int pad = hLength % 4;
225 if (0 < pad) {
226 pad = 4 - pad;
227 if (pad <= tempCb.readableBytes()) {
228 tempCb.skipBytes(pad);
229 }
230 }
231
232 llRemoteTENodeDescriptorSubTLVs.add(tlv);
233 }
234
235 if (0 < tempCb.readableBytes()) {
236
237 throw new PcepParseException("Sub Tlv parsing error. Extra bytes received.");
238 }
Mahesh Poojary Sf1bbd362016-02-25 18:19:59 +0530239 return new RemoteNodeDescriptorsTlv(llRemoteTENodeDescriptorSubTLVs);
Sho SHIMIZUe81e4db2015-09-03 09:44:38 -0700240 }
241
242 @Override
243 public String toString() {
Sho SHIMIZU7cdbcf72015-09-03 14:43:05 -0700244 return MoreObjects.toStringHelper(getClass())
245 .add("Type", TYPE)
246 .add("Length", hLength)
247 .add("RemoteTeNodeDescriptorSubTLVs", llRemoteTENodeDescriptorSubTLVs)
248 .toString();
Sho SHIMIZUe81e4db2015-09-03 09:44:38 -0700249 }
250}