blob: 7eb711cce18cccc4c1c4766be6622676bbd389d5 [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 */
16package org.onosproject.pcepio.protocol.ver1;
17
18import java.util.LinkedList;
Mahesh Poojary Sf1bbd362016-02-25 18:19:59 +053019import java.util.List;
Sho SHIMIZUe81e4db2015-09-03 09:44:38 -070020import java.util.ListIterator;
21
22import org.jboss.netty.buffer.ChannelBuffer;
23import org.onosproject.pcepio.exceptions.PcepParseException;
24import org.onosproject.pcepio.protocol.PcepError;
25import org.onosproject.pcepio.protocol.PcepErrorObject;
26import org.onosproject.pcepio.protocol.PcepRPObject;
Mahesh Poojary Sf1bbd362016-02-25 18:19:59 +053027import org.onosproject.pcepio.protocol.PcepLSObject;
Sho SHIMIZUe81e4db2015-09-03 09:44:38 -070028import org.onosproject.pcepio.types.PcepObjectHeader;
29import org.slf4j.Logger;
30import org.slf4j.LoggerFactory;
31
32import com.google.common.base.MoreObjects;
Sho SHIMIZUe81e4db2015-09-03 09:44:38 -070033
34/**
Mahesh Poojary Sf1bbd362016-02-25 18:19:59 +053035 * Provides PcepError list which contains RP or LS objects.
36 * Reference: draft-dhodylee-pce-pcep-ls-01, section 8.2.
Sho SHIMIZUe81e4db2015-09-03 09:44:38 -070037 */
38public class PcepErrorVer1 implements PcepError {
39
40 /*
Mahesh Poojary Sf1bbd362016-02-25 18:19:59 +053041 <error>::=[<request-id-list> | <ls-id-list>]
Sho SHIMIZUe81e4db2015-09-03 09:44:38 -070042 <error-obj-list>
43
44 <request-id-list>::=<RP>[<request-id-list>]
45
Mahesh Poojary Sf1bbd362016-02-25 18:19:59 +053046 <ls-id-list>::=<LS>[<ls-id-list>]
Sho SHIMIZUe81e4db2015-09-03 09:44:38 -070047 */
48
Ray Milkey9c9cde42018-01-12 14:22:06 -080049 private static final Logger log = LoggerFactory.getLogger(PcepErrorVer1.class);
Sho SHIMIZUe81e4db2015-09-03 09:44:38 -070050
51 private boolean isErroInfoSet;
52 //PcepErrorObject list
Mahesh Poojary Sf1bbd362016-02-25 18:19:59 +053053 private List<PcepErrorObject> errObjList;
Sho SHIMIZUe81e4db2015-09-03 09:44:38 -070054 //PcepRPObject list
Mahesh Poojary Sf1bbd362016-02-25 18:19:59 +053055 private List<PcepRPObject> rpObjList;
56 //PcepLSObject list
57 private List<PcepLSObject> lsObjList;
58 private boolean isLSObjListSet;
Sho SHIMIZUe81e4db2015-09-03 09:44:38 -070059
60 public static final int OBJECT_HEADER_LENGTH = 4;
61
62 /**
63 * Constructor to initialize variable.
64 */
65 public PcepErrorVer1() {
Mahesh Poojary Sf1bbd362016-02-25 18:19:59 +053066 this.rpObjList = null;
67 this.lsObjList = null;
68 this.errObjList = null;
Sho SHIMIZUe81e4db2015-09-03 09:44:38 -070069 }
70
71 /**
72 * Constructor to initialize variable.
73 *
Mahesh Poojary Sf1bbd362016-02-25 18:19:59 +053074 * @param rpObjList list of PcepRPObject
75 * @param lsObjList list of PcepLSObject
76 * @param errObjListObjList list of PcepErrorObject
Sho SHIMIZUe81e4db2015-09-03 09:44:38 -070077 */
Mahesh Poojary Sf1bbd362016-02-25 18:19:59 +053078 public PcepErrorVer1(List<PcepRPObject> rpObjList, List<PcepLSObject> lsObjList,
79 List<PcepErrorObject> errObjListObjList) {
80 this.rpObjList = rpObjList;
81 this.lsObjList = lsObjList;
82 this.errObjList = errObjListObjList;
Sho SHIMIZUe81e4db2015-09-03 09:44:38 -070083 }
84
85 /**
86 * Constructor to initialize PcepError.
87 *
Mahesh Poojary Sf1bbd362016-02-25 18:19:59 +053088 * @param errObjList list of PcepErrorObject
Sho SHIMIZUe81e4db2015-09-03 09:44:38 -070089 */
Mahesh Poojary Sf1bbd362016-02-25 18:19:59 +053090 public PcepErrorVer1(List<PcepErrorObject> errObjList) {
91 this.rpObjList = null;
92 this.lsObjList = null;
93 this.errObjList = errObjList;
Sho SHIMIZUe81e4db2015-09-03 09:44:38 -070094 }
95
96 @Override
Mahesh Poojary Sf1bbd362016-02-25 18:19:59 +053097 public List<PcepRPObject> getRPObjList() {
98 return this.rpObjList;
Sho SHIMIZUe81e4db2015-09-03 09:44:38 -070099 }
100
101 @Override
Mahesh Poojary Sf1bbd362016-02-25 18:19:59 +0530102 public List<PcepLSObject> getLSObjList() {
103 return this.lsObjList;
Sho SHIMIZUe81e4db2015-09-03 09:44:38 -0700104 }
105
106 @Override
Mahesh Poojary Sf1bbd362016-02-25 18:19:59 +0530107 public List<PcepErrorObject> getErrorObjList() {
108 return this.errObjList;
Sho SHIMIZUe81e4db2015-09-03 09:44:38 -0700109 }
110
111 /**
112 * Parse RP List from the channel buffer.
113 *
Sho SHIMIZUe81e4db2015-09-03 09:44:38 -0700114 * @param cb of type channel buffer
HIGUCHI Yuta0f3aaff2015-09-22 14:58:12 -0700115 * @throws PcepParseException if mandatory fields are missing
Sho SHIMIZUe81e4db2015-09-03 09:44:38 -0700116 */
117 public void parseRPList(ChannelBuffer cb) throws PcepParseException {
118 byte yObjClass;
119 byte yObjType;
120
Mahesh Poojary Sf1bbd362016-02-25 18:19:59 +0530121 rpObjList = new LinkedList<>();
Sho SHIMIZUe81e4db2015-09-03 09:44:38 -0700122
123 // caller should verify for RP object
124 if (cb.readableBytes() < OBJECT_HEADER_LENGTH) {
125 log.debug("Unable to find RP Object");
126 return;
127 }
128
129 cb.markReaderIndex();
130 PcepObjectHeader tempObjHeader = PcepObjectHeader.read(cb);
131 cb.resetReaderIndex();
132 yObjClass = tempObjHeader.getObjClass();
133 yObjType = tempObjHeader.getObjType();
134 PcepRPObject rpObj;
135 while ((yObjClass == PcepRPObjectVer1.RP_OBJ_CLASS) && (yObjType == PcepRPObjectVer1.RP_OBJ_TYPE)) {
136 rpObj = PcepRPObjectVer1.read(cb);
Mahesh Poojary Sf1bbd362016-02-25 18:19:59 +0530137 rpObjList.add(rpObj);
Sho SHIMIZUe81e4db2015-09-03 09:44:38 -0700138
139 if (cb.readableBytes() > OBJECT_HEADER_LENGTH) {
140 cb.markReaderIndex();
141 tempObjHeader = PcepObjectHeader.read(cb);
142 cb.resetReaderIndex();
143 yObjClass = tempObjHeader.getObjClass();
144 yObjType = tempObjHeader.getObjType();
145 } else {
146 break;
147 }
148 }
149 }
150
151 /**
Mahesh Poojary Sf1bbd362016-02-25 18:19:59 +0530152 * Parse LS List from the channel buffer.
Sho SHIMIZUe81e4db2015-09-03 09:44:38 -0700153 *
154 * @param cb of type channel buffer
155 * @throws PcepParseException if mandatory fields are missing
156 */
Mahesh Poojary Sf1bbd362016-02-25 18:19:59 +0530157 public void parseLSList(ChannelBuffer cb) throws PcepParseException {
Sho SHIMIZUe81e4db2015-09-03 09:44:38 -0700158 byte yObjClass;
159 byte yObjType;
160
Mahesh Poojary Sf1bbd362016-02-25 18:19:59 +0530161 lsObjList = new LinkedList<>();
Sho SHIMIZUe81e4db2015-09-03 09:44:38 -0700162
Mahesh Poojary Sf1bbd362016-02-25 18:19:59 +0530163 // caller should verify for LS object
Sho SHIMIZUe81e4db2015-09-03 09:44:38 -0700164 if (cb.readableBytes() < OBJECT_HEADER_LENGTH) {
Mahesh Poojary Sf1bbd362016-02-25 18:19:59 +0530165 log.debug("Unable to find LS Object");
Sho SHIMIZUe81e4db2015-09-03 09:44:38 -0700166 return;
167 }
168
169 cb.markReaderIndex();
170 PcepObjectHeader tempObjHeader = PcepObjectHeader.read(cb);
171 cb.resetReaderIndex();
172 yObjClass = tempObjHeader.getObjClass();
173 yObjType = tempObjHeader.getObjType();
Mahesh Poojary Sf1bbd362016-02-25 18:19:59 +0530174 PcepLSObject lsObj;
175 while ((yObjClass == PcepLSObjectVer1.LS_OBJ_CLASS) && ((yObjType == PcepLSObjectVer1.LS_OBJ_TYPE_NODE_VALUE)
176 || (yObjType == PcepLSObjectVer1.LS_OBJ_TYPE_LINK_VALUE))) {
177 lsObj = PcepLSObjectVer1.read(cb);
178 lsObjList.add(lsObj);
Sho SHIMIZUe81e4db2015-09-03 09:44:38 -0700179
180 if (cb.readableBytes() > OBJECT_HEADER_LENGTH) {
181 cb.markReaderIndex();
182 tempObjHeader = PcepObjectHeader.read(cb);
183 cb.resetReaderIndex();
184 yObjClass = tempObjHeader.getObjClass();
185 yObjType = tempObjHeader.getObjType();
186 } else {
187 break;
188 }
189 }
190 }
191
192 /**
193 * parseErrObjList from the channel buffer.
194 *
195 * @param cb of type channel buffer
196 * @throws PcepParseException if mandatory fields are missing
197 */
198 public void parseErrObjList(ChannelBuffer cb) throws PcepParseException {
199 byte yObjClass;
200 byte yObjType;
201 boolean bIsErrorObjFound = false;
202
Mahesh Poojary Sf1bbd362016-02-25 18:19:59 +0530203 errObjList = new LinkedList<>();
Sho SHIMIZUe81e4db2015-09-03 09:44:38 -0700204
205 // caller should verify for RP object
206 if (cb.readableBytes() < OBJECT_HEADER_LENGTH) {
207 throw new PcepParseException("Unable to find PCEP-ERROR Object");
208 }
209
210 cb.markReaderIndex();
211 PcepObjectHeader tempObjHeader = PcepObjectHeader.read(cb);
212 cb.resetReaderIndex();
213 yObjClass = tempObjHeader.getObjClass();
214 yObjType = tempObjHeader.getObjType();
215 PcepErrorObject errorObject;
216 while ((yObjClass == PcepErrorObjectVer1.ERROR_OBJ_CLASS) && (yObjType == PcepErrorObjectVer1.ERROR_OBJ_TYPE)) {
217 errorObject = PcepErrorObjectVer1.read(cb);
Mahesh Poojary Sf1bbd362016-02-25 18:19:59 +0530218 errObjList.add(errorObject);
Sho SHIMIZUe81e4db2015-09-03 09:44:38 -0700219 bIsErrorObjFound = true;
220
221 if (cb.readableBytes() > OBJECT_HEADER_LENGTH) {
222 cb.markReaderIndex();
223 tempObjHeader = PcepObjectHeader.read(cb);
224 cb.resetReaderIndex();
225 yObjClass = tempObjHeader.getObjClass();
226 yObjType = tempObjHeader.getObjType();
227 } else {
228 break;
229 }
230 }
231
232 if (!bIsErrorObjFound) {
233 throw new PcepParseException("At least one PCEP-ERROR Object should be present.");
234 }
235 }
236
237 /**
238 * Reads the byte stream of PcepError from channel buffer.
239 *
240 * @param cb of type channel buffer
241 * @return PcepError error part of PCEP-ERROR
242 * @throws PcepParseException if mandatory fields are missing
243 */
244 public static PcepErrorVer1 read(ChannelBuffer cb) throws PcepParseException {
245 if (cb.readableBytes() < OBJECT_HEADER_LENGTH) {
246 throw new PcepParseException("Unknown Object");
247 }
248
249 PcepErrorVer1 pcepError = new PcepErrorVer1();
250 // check whether any PCEP Error Info is present
251 cb.markReaderIndex();
252 PcepObjectHeader tempObjHeader = PcepObjectHeader.read(cb);
253 cb.resetReaderIndex();
254 byte yObjClass = tempObjHeader.getObjClass();
255
Mahesh Poojary Sf1bbd362016-02-25 18:19:59 +0530256 //If RPlist present then store it.RPList and LSList are optional
Sho SHIMIZUe81e4db2015-09-03 09:44:38 -0700257 if (yObjClass == PcepRPObjectVer1.RP_OBJ_CLASS) {
258 log.debug("RP_LIST");
259 pcepError.parseRPList(cb);
260 yObjClass = checkNextObject(cb);
Mahesh Poojary Sf1bbd362016-02-25 18:19:59 +0530261 } else if (yObjClass == PcepLSObjectVer1.LS_OBJ_CLASS) {
262 log.debug("LS_LIST");
263 pcepError.parseLSList(cb);
Sho SHIMIZUe81e4db2015-09-03 09:44:38 -0700264 yObjClass = checkNextObject(cb);
265 }
266
267 if (yObjClass == PcepErrorObjectVer1.ERROR_OBJ_CLASS) {
268 log.debug("PCEP-ERROR obj list");
269 pcepError.parseErrObjList(cb);
270 yObjClass = checkNextObject(cb);
271 }
272
273 return pcepError;
274 }
275
276 /**
277 * Checks Next Object.
278 *
279 * @param cb of type channel buffer.
280 * @return object type class.
281 */
282 private static byte checkNextObject(ChannelBuffer cb) {
283 if (cb.readableBytes() < OBJECT_HEADER_LENGTH) {
284 return 0;
285 }
286 cb.markReaderIndex();
287 PcepObjectHeader tempObjHeader = PcepObjectHeader.read(cb);
288 cb.resetReaderIndex();
289 return tempObjHeader.getObjClass();
290 }
291
292 /**
293 * Writes the byte stream of PCEP error to the channel buffer.
294 *
295 * @param cb of type channel buffer
296 * @return object length index
297 * @throws PcepParseException if mandatory fields are missing
298 */
299 @Override
300 public int write(ChannelBuffer cb) throws PcepParseException {
301 int iLenStartIndex = cb.writerIndex();
302
303 // RPlist is optional
304 if (this.isErroInfoSet) {
Mahesh Poojary Sf1bbd362016-02-25 18:19:59 +0530305 ListIterator<PcepRPObject> rpObjlistIterator = this.rpObjList.listIterator();
Sho SHIMIZUe81e4db2015-09-03 09:44:38 -0700306 while (rpObjlistIterator.hasNext()) {
307 rpObjlistIterator.next().write(cb);
308 }
309 }
310
Mahesh Poojary Sf1bbd362016-02-25 18:19:59 +0530311 // LSlist is optional
312 if (this.isLSObjListSet) {
313 ListIterator<PcepLSObject> teObjlistIterator = this.lsObjList.listIterator();
Sho SHIMIZUe81e4db2015-09-03 09:44:38 -0700314 while (teObjlistIterator.hasNext()) {
315 teObjlistIterator.next().write(cb);
316 }
317 }
318 //ErrList is mandatory
Mahesh Poojary Sf1bbd362016-02-25 18:19:59 +0530319 ListIterator<PcepErrorObject> errlistIterator = this.errObjList.listIterator();
Sho SHIMIZUe81e4db2015-09-03 09:44:38 -0700320 while (errlistIterator.hasNext()) {
321 errlistIterator.next().write(cb);
322 }
323
324 return cb.writerIndex() - iLenStartIndex;
325 }
326
327 /**
328 * Builder for error part of PCEP-ERROR.
329 */
330 public static class Builder implements PcepError.Builder {
331
Mahesh Poojary Sf1bbd362016-02-25 18:19:59 +0530332 private List<PcepRPObject> rpObjList;
333 private List<PcepLSObject> lsObjList;
334 private List<PcepErrorObject> errObjList;
Sho SHIMIZUe81e4db2015-09-03 09:44:38 -0700335
336 @Override
337 public PcepError build() {
Mahesh Poojary Sf1bbd362016-02-25 18:19:59 +0530338 return new PcepErrorVer1(rpObjList, lsObjList, errObjList);
Sho SHIMIZUe81e4db2015-09-03 09:44:38 -0700339 }
340
341 @Override
Mahesh Poojary Sf1bbd362016-02-25 18:19:59 +0530342 public List<PcepRPObject> getRPObjList() {
343 return this.rpObjList;
Sho SHIMIZUe81e4db2015-09-03 09:44:38 -0700344 }
345
346 @Override
Mahesh Poojary Sf1bbd362016-02-25 18:19:59 +0530347 public Builder setRPObjList(List<PcepRPObject> rpObjList) {
348 this.rpObjList = rpObjList;
Sho SHIMIZUe81e4db2015-09-03 09:44:38 -0700349 return this;
350 }
351
352 @Override
Mahesh Poojary Sf1bbd362016-02-25 18:19:59 +0530353 public List<PcepLSObject> getLSObjList() {
354 return this.lsObjList;
Sho SHIMIZUe81e4db2015-09-03 09:44:38 -0700355 }
356
357 @Override
Mahesh Poojary Sf1bbd362016-02-25 18:19:59 +0530358 public Builder setLSObjList(List<PcepLSObject> lsObjList) {
359 this.lsObjList = lsObjList;
Sho SHIMIZUe81e4db2015-09-03 09:44:38 -0700360 return this;
361 }
362
363 @Override
Mahesh Poojary Sf1bbd362016-02-25 18:19:59 +0530364 public List<PcepErrorObject> getErrorObjList() {
365 return this.errObjList;
Sho SHIMIZUe81e4db2015-09-03 09:44:38 -0700366 }
367
368 @Override
Mahesh Poojary Sf1bbd362016-02-25 18:19:59 +0530369 public Builder setErrorObjList(List<PcepErrorObject> errObjList) {
370 this.errObjList = errObjList;
Sho SHIMIZUe81e4db2015-09-03 09:44:38 -0700371 return this;
372 }
373
374 }
375
376 @Override
Mahesh Poojary Sf1bbd362016-02-25 18:19:59 +0530377 public void setRPObjList(List<PcepRPObject> rpObjList) {
378 this.rpObjList = rpObjList;
Sho SHIMIZUe81e4db2015-09-03 09:44:38 -0700379 }
380
381 @Override
Mahesh Poojary Sf1bbd362016-02-25 18:19:59 +0530382 public void setLSObjList(List<PcepLSObject> lsObjList) {
383 this.lsObjList = lsObjList;
Sho SHIMIZUe81e4db2015-09-03 09:44:38 -0700384 }
385
386 @Override
Mahesh Poojary Sf1bbd362016-02-25 18:19:59 +0530387 public void setErrorObjList(List<PcepErrorObject> errObjList) {
388 this.errObjList = errObjList;
Sho SHIMIZUe81e4db2015-09-03 09:44:38 -0700389 }
390
391 @Override
392 public String toString() {
Sho SHIMIZU7cdbcf72015-09-03 14:43:05 -0700393 return MoreObjects.toStringHelper(getClass())
394 .omitNullValues()
Mahesh Poojary Sf1bbd362016-02-25 18:19:59 +0530395 .add("RpObjectList", rpObjList)
396 .add("LsObjectList", lsObjList)
397 .add("ErrorObjectList", errObjList)
Sho SHIMIZU7cdbcf72015-09-03 14:43:05 -0700398 .toString();
Sho SHIMIZUe81e4db2015-09-03 09:44:38 -0700399 }
400}