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.protocol.ver1;
18 import java.util.LinkedList;
19 import java.util.ListIterator;
21 import org.jboss.netty.buffer.ChannelBuffer;
22 import org.onosproject.pcepio.exceptions.PcepParseException;
23 import org.onosproject.pcepio.protocol.PcepError;
24 import org.onosproject.pcepio.protocol.PcepErrorInfo;
25 import org.onosproject.pcepio.protocol.PcepErrorObject;
26 import org.onosproject.pcepio.protocol.PcepRPObject;
27 import org.onosproject.pcepio.protocol.PcepTEObject;
28 import org.onosproject.pcepio.types.PcepObjectHeader;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
32 import com.google.common.base.MoreObjects;
35 * Provides PCEP Error Info.
36 * Reference :PCEP Extension for Transporting TE Data draft-dhodylee-pce-pcep-te-data-extn-02.
38 public class PcepErrorInfoVer1 implements PcepErrorInfo {
40 protected static final Logger log = LoggerFactory.getLogger(PcepErrorInfoVer1.class);
41 //Error list is optional
42 private LinkedList<PcepError> errList;
45 * Constructor to add PCEP error object to the list.
47 * @param llRPObjList list of PCEP RP object
48 * @param llTEObjList list of PCEP TE object
49 * @param llErrObjList list of PCEP error object
51 public PcepErrorInfoVer1(LinkedList<PcepRPObject> llRPObjList, LinkedList<PcepTEObject> llTEObjList,
52 LinkedList<PcepErrorObject> llErrObjList) {
53 this.errList = new LinkedList<>();
54 if ((llErrObjList != null) && (!llErrObjList.isEmpty())) {
55 this.errList.add(new PcepErrorVer1(llRPObjList, llTEObjList, llErrObjList));
60 * Constructor to initialize error info.
62 * @param errll linked list or pcep error
64 public PcepErrorInfoVer1(LinkedList<PcepError> errll) {
69 public boolean isErrorInfoPresent() {
70 return !this.errList.isEmpty();
74 public void read(ChannelBuffer cb) throws PcepParseException {
75 PcepObjectHeader tempObjHeader;
77 while (0 < cb.readableBytes()) {
79 tempObjHeader = PcepObjectHeader.read(cb);
80 cb.resetReaderIndex();
81 byte yObjClass = tempObjHeader.getObjClass();
82 if ((yObjClass != PcepRPObjectVer1.RP_OBJ_CLASS) && (yObjClass != PcepTEObjectVer1.TE_OBJ_CLASS)
83 && (yObjClass != PcepErrorObjectVer1.ERROR_OBJ_CLASS)) {
84 throw new PcepParseException("Unknown Object is present in PCEP-ERROR. Object Class: " + yObjClass);
87 this.errList.add(PcepErrorVer1.read(cb));
92 public void write(ChannelBuffer cb) throws PcepParseException {
94 ListIterator<PcepError> listIterator = errList.listIterator();
95 while (listIterator.hasNext()) {
96 PcepError pcepError = listIterator.next();
98 //RP Object list is optional
99 LinkedList<PcepRPObject> llRPObjList = pcepError.getRPObjList();
100 if (llRPObjList != null) {
101 ListIterator<PcepRPObject> rpListIterator = llRPObjList.listIterator();
102 while (rpListIterator.hasNext()) {
103 rpListIterator.next().write(cb);
107 //TE Object list is optional
108 LinkedList<PcepTEObject> llTEObjList = pcepError.getTEObjList();
109 if (llTEObjList != null) {
110 ListIterator<PcepTEObject> teListIterator = llTEObjList.listIterator();
111 while (teListIterator.hasNext()) {
112 teListIterator.next().write(cb);
116 // <error-obj-list> is mandatory
117 boolean bIsErrorObjListFound = false;
119 LinkedList<PcepErrorObject> llErrObjList = pcepError.getErrorObjList();
120 if (llErrObjList != null) {
121 ListIterator<PcepErrorObject> errObjListIterator = llErrObjList.listIterator();
122 while (errObjListIterator.hasNext()) {
123 errObjListIterator.next().write(cb);
124 bIsErrorObjListFound = true;
128 if (!bIsErrorObjListFound) {
129 throw new PcepParseException("<error-obj-list> is mandatory.");
135 public LinkedList<Integer> getErrorType() {
136 LinkedList<Integer> errorType = new LinkedList<>();
137 ListIterator<PcepError> listIterator = errList.listIterator();
138 PcepErrorObject errObj;
140 while (listIterator.hasNext()) {
141 PcepError pcepError = listIterator.next();
142 LinkedList<PcepErrorObject> llErrObjList = pcepError.getErrorObjList();
143 if (llErrObjList != null) {
144 ListIterator<PcepErrorObject> errObjListIterator = llErrObjList.listIterator();
145 while (errObjListIterator.hasNext()) {
146 errObj = errObjListIterator.next();
147 error = errObj.getErrorType();
148 errorType.add(error);
156 public LinkedList<Integer> getErrorValue() {
157 LinkedList<Integer> errorValue = new LinkedList<>();
158 ListIterator<PcepError> listIterator = errList.listIterator();
159 PcepErrorObject errObj;
161 while (listIterator.hasNext()) {
162 PcepError pcepError = listIterator.next();
163 LinkedList<PcepErrorObject> llErrObjList = pcepError.getErrorObjList();
164 if (llErrObjList != null) {
165 ListIterator<PcepErrorObject> errObjListIterator = llErrObjList.listIterator();
166 while (errObjListIterator.hasNext()) {
167 errObj = errObjListIterator.next();
168 error = errObj.getErrorValue();
169 errorValue.add(error);
177 * Builder class for PCEP error info.
179 public static class Builder implements PcepErrorInfo.Builder {
180 private LinkedList<PcepError> errll;
183 public PcepErrorInfo build() {
184 return new PcepErrorInfoVer1(errll);
188 public LinkedList<PcepError> getPcepErrorList() {
193 public Builder setPcepErrorList(LinkedList<PcepError> errll) {
200 public String toString() {
201 return MoreObjects.toStringHelper(getClass())
202 .add("ErrorList", errList).toString();