1 package org.onosproject.pcepio.protocol.ver1;
3 import java.util.LinkedList;
5 import org.jboss.netty.buffer.ChannelBuffer;
6 import org.onosproject.pcepio.exceptions.PcepParseException;
7 import org.onosproject.pcepio.protocol.PcepErrorInfo;
8 import org.onosproject.pcepio.protocol.PcepErrorMsg;
9 import org.onosproject.pcepio.protocol.PcepErrorObject;
10 import org.onosproject.pcepio.protocol.PcepMessageReader;
11 import org.onosproject.pcepio.protocol.PcepMessageWriter;
12 import org.onosproject.pcepio.protocol.PcepOpenObject;
13 import org.onosproject.pcepio.protocol.PcepType;
14 import org.onosproject.pcepio.protocol.PcepVersion;
15 import org.onosproject.pcepio.types.ErrorObjListWithOpen;
16 import org.onosproject.pcepio.types.PcepObjectHeader;
17 import org.slf4j.Logger;
18 import org.slf4j.LoggerFactory;
20 import com.google.common.base.MoreObjects;
21 import com.google.common.base.MoreObjects.ToStringHelper;
24 * Provides PCEP Error Message.
26 public class PcepErrorMsgVer1 implements PcepErrorMsg {
29 * PCE Error message format.
31 <PCErr Message> ::= <Common Header>
32 ( <error-obj-list> [<Open>] ) | <error>
35 <error-obj-list> ::=<PCEP-ERROR>[<error-obj-list>]
37 <error> ::=[<request-id-list> | <te-id-list>]
40 <request-id-list> ::=<RP>[<request-id-list>]
42 <te-id-list> ::=<TE>[<te-id-list>]
44 <error-list> ::=<error>[<error-list>]
47 protected static final Logger log = LoggerFactory.getLogger(PcepOpenMsgVer1.class);
48 public static final byte PACKET_VERSION = 1;
49 public static final int PACKET_MINIMUM_LENGTH = 12;
50 public static final PcepType MSG_TYPE = PcepType.ERROR;
52 //Below either one should be present.
53 private ErrorObjListWithOpen errObjListWithOpen; //optional ( <error-obj-list> [<Open>] )
54 private PcepErrorInfo errInfo; //optional <error> [<error-list>]
56 public static final PcepErrorMsgVer1.Reader READER = new Reader();
59 * constructor to initialize variables.
61 public PcepErrorMsgVer1() {
62 errObjListWithOpen = null;
67 * Constructor to initialize variables.
69 * @param errObjListWithOpen error-object-list with open object
70 * @param errInfo error information
72 public PcepErrorMsgVer1(ErrorObjListWithOpen errObjListWithOpen, PcepErrorInfo errInfo) {
73 this.errObjListWithOpen = errObjListWithOpen;
74 this.errInfo = errInfo;
78 * Reader class for reading PCEP error Message from channel buffer.
80 public static class Reader implements PcepMessageReader<PcepErrorMsg> {
82 ErrorObjListWithOpen errObjListWithOpen;
83 PcepErrorInfo errInfo;
84 PcepObjectHeader tempObjHeader;
87 public PcepErrorMsg readFrom(ChannelBuffer cb) throws PcepParseException {
89 errObjListWithOpen = null;
93 if (cb.readableBytes() < PACKET_MINIMUM_LENGTH) {
94 throw new PcepParseException("Packet size is less than the minimum length.");
97 byte version = cb.readByte();
98 version = (byte) (version >> PcepMessageVer1.SHIFT_FLAG);
99 if (version != PACKET_VERSION) {
100 throw new PcepParseException("Wrong version: Expected=PcepVersion.PCEP_1(1), got=" + version);
102 // fixed value property type == 1
103 byte type = cb.readByte();
104 if (type != MSG_TYPE.getType()) {
105 throw new PcepParseException("Wrong type: Expected=PcepType.ERROR(6), got=" + type);
107 int length = cb.readShort();
108 if (length < PACKET_MINIMUM_LENGTH) {
109 throw new PcepParseException(
110 "Wrong length: Expected to be >= " + PACKET_MINIMUM_LENGTH + ", was: " + length);
113 //parse <PCErr Message>
116 // If other than RP or TE or PCEP-ERROR present then it is error.
117 if (0 < cb.readableBytes()) {
118 PcepObjectHeader tempObjHeader = PcepObjectHeader.read(cb);
119 throw new PcepParseException("Unexpected Object found. Object Class : " + tempObjHeader.getObjClass());
122 return new PcepErrorMsgVer1(errObjListWithOpen, errInfo);
126 * Parsing PCErr Message.
128 * @param cb channel buffer.
129 * @throws PcepParseException if mandatory fields are missing
130 * output: this.errObjListWithOpen, this.errInfo
132 public void parsePCErrMsg(ChannelBuffer cb) throws PcepParseException {
133 //If PCEP-ERROR list is followed by OPEN Object then store into ErrorObjListWithOpen.
134 // ( <error-obj-list> [<Open>]
135 //If PCEP-ERROR list is followed by RP or TE Object then store into errInfo. <error> [<error-list>]
136 //If only PCEP-ERROR list is present then store into ErrorObjListWithOpen.
137 PcepObjectHeader tempObjHeader;
138 LinkedList<PcepErrorObject> llErrObjList;
140 if (0 >= cb.readableBytes()) {
141 throw new PcepParseException("PCEP-ERROR message came with empty objects.");
144 //parse PCEP-ERROR list
145 llErrObjList = new LinkedList<>();
146 tempObjHeader = parseErrorObjectList(llErrObjList, cb);
148 //check whether OPEN-OBJECT is present.
149 if ((tempObjHeader != null)
150 && (tempObjHeader.getObjClass() == PcepOpenObjectVer1.OPEN_OBJ_CLASS)) {
152 if (llErrObjList.isEmpty()) {
153 throw new PcepParseException("<error-obj-list> should be present if OPEN-OBJECT exists");
156 PcepOpenObject pcepOpenObj = PcepOpenObjectVer1.read(cb);
157 this.errObjListWithOpen = new ErrorObjListWithOpen(llErrObjList, pcepOpenObj);
159 } else if ((tempObjHeader != null) //check whether RP or TE Object is present.
160 && ((tempObjHeader.getObjClass() == PcepRPObjectVer1.RP_OBJ_CLASS)
161 || (tempObjHeader.getObjClass() == PcepTEObjectVer1.TE_OBJ_CLASS))) {
163 this.errInfo = new PcepErrorInfoVer1(null, null, llErrObjList);
164 this.errInfo.read(cb);
166 } else if (!llErrObjList.isEmpty()) {
167 //If only PCEP-ERROR list is present then store it in errObjListWithOpen.
168 this.errObjListWithOpen = new ErrorObjListWithOpen(llErrObjList);
170 throw new PcepParseException("Empty PCEP-ERROR message.");
175 * Parse error-obj-list.
177 * @param llErrObjList error object list output
178 * @param cb channel buffer input
179 * @throws PcepParseException if mandatory fields are missing
180 * @return error object header
182 public PcepObjectHeader parseErrorObjectList(LinkedList<PcepErrorObject> llErrObjList, ChannelBuffer cb)
183 throws PcepParseException {
184 PcepObjectHeader tempObjHeader = null;
186 while (0 < cb.readableBytes()) {
187 cb.markReaderIndex();
188 tempObjHeader = PcepObjectHeader.read(cb);
189 cb.resetReaderIndex();
190 if (tempObjHeader.getObjClass() == PcepErrorObjectVer1.ERROR_OBJ_CLASS) {
191 llErrObjList.add(PcepErrorObjectVer1.read(cb));
196 return tempObjHeader;
201 * Builder class for PCEP error message.
203 public static class Builder implements PcepErrorMsg.Builder {
204 // Pcep error message fields
206 private ErrorObjListWithOpen errObjListWithOpen = null; //optional ( <error-obj-list> [<Open>] )
207 private PcepErrorInfo errInfo = null; //optional <error> [<error-list>]
210 public PcepVersion getVersion() {
211 return PcepVersion.PCEP_1;
215 public PcepType getType() {
216 return PcepType.ERROR;
220 public PcepErrorMsg build() {
221 return new PcepErrorMsgVer1(this.errObjListWithOpen, this.errInfo);
225 public ErrorObjListWithOpen getErrorObjListWithOpen() {
226 return this.errObjListWithOpen;
230 public Builder setErrorObjListWithOpen(ErrorObjListWithOpen errObjListWithOpen) {
231 this.errObjListWithOpen = errObjListWithOpen;
236 public PcepErrorInfo getPcepErrorInfo() {
241 public Builder setPcepErrorInfo(PcepErrorInfo errInfo) {
242 this.errInfo = errInfo;
248 public void writeTo(ChannelBuffer cb) throws PcepParseException {
249 WRITER.write(cb, this);
252 public static final Writer WRITER = new Writer();
255 * Writer class for writing PCEP error Message to channel buffer.
257 static class Writer implements PcepMessageWriter<PcepErrorMsgVer1> {
259 public void write(ChannelBuffer cb, PcepErrorMsgVer1 message) throws PcepParseException {
260 int startIndex = cb.writerIndex();
261 // first 3 bits set to version
262 cb.writeByte((byte) (PACKET_VERSION << PcepMessageVer1.SHIFT_FLAG));
264 cb.writeByte(MSG_TYPE.getType());
265 // length is length of variable message, will be updated at the end
266 // Store the position of message
268 int msgLenIndex = cb.writerIndex();
270 ErrorObjListWithOpen errObjListWithOpen = message.getErrorObjListWithOpen();
271 PcepErrorInfo errInfo = message.getPcepErrorInfo();
273 // write ( <error-obj-list> [<Open>] ) if exists.
274 // otherwise write <error> [<error-list>]
276 if ((errObjListWithOpen != null)
277 && (errObjListWithOpen.isErrorObjListWithOpenPresent())) {
278 errObjListWithOpen.write(cb);
279 } else if ((errInfo != null) && (errInfo.isErrorInfoPresent())) {
282 throw new PcepParseException("Empty PCEP-ERROR message.");
284 // PcepErrorMessage message length field
285 int length = cb.writerIndex() - startIndex;
286 cb.setShort(msgLenIndex, (short) length);
291 public PcepVersion getVersion() {
292 return PcepVersion.PCEP_1;
296 public PcepType getType() {
301 public ErrorObjListWithOpen getErrorObjListWithOpen() {
302 return this.errObjListWithOpen;
306 public void setErrorObjListWithOpen(ErrorObjListWithOpen errObjListWithOpen) {
307 this.errObjListWithOpen = errObjListWithOpen;
311 public PcepErrorInfo getPcepErrorInfo() {
316 public void setPcepErrorInfo(PcepErrorInfo errInfo) {
317 this.errInfo = errInfo;
321 * Return list of Error types.
323 * @return error types list
325 public LinkedList<Integer> getErrorType() {
326 LinkedList<Integer> llErrorType = new LinkedList<>();
327 if ((errObjListWithOpen != null)
328 && (errObjListWithOpen.isErrorObjListWithOpenPresent())) {
329 llErrorType = errObjListWithOpen.getErrorType();
330 } else if ((errInfo != null) && (errInfo.isErrorInfoPresent())) {
331 llErrorType = errInfo.getErrorType();
338 * Return list of Error values.
340 * @return error value list
342 public LinkedList<Integer> getErrorValue() {
343 LinkedList<Integer> llErrorValue = new LinkedList<>();
344 if ((errObjListWithOpen != null)
345 && (errObjListWithOpen.isErrorObjListWithOpenPresent())) {
346 llErrorValue = errObjListWithOpen.getErrorValue();
347 } else if ((errInfo != null) && (errInfo.isErrorInfoPresent())) {
348 llErrorValue = errInfo.getErrorValue();
355 public String toString() {
356 ToStringHelper toStrHelper = MoreObjects.toStringHelper(getClass());
358 if ((errObjListWithOpen != null)
359 && (errObjListWithOpen.isErrorObjListWithOpenPresent())) {
360 toStrHelper.add("ErrorObjectListWithOpen", errObjListWithOpen);
362 if ((errInfo != null) && (errInfo.isErrorInfoPresent())) {
363 toStrHelper.add("ErrorInfo", errInfo);
366 return toStrHelper.toString();