Sho SHIMIZU | 74361c1 | 2015-08-11 12:31:48 -0700 | [diff] [blame^] | 1 | package org.onosproject.pcepio.types; |
| 2 | |
| 3 | import java.util.LinkedList; |
| 4 | import java.util.ListIterator; |
| 5 | |
| 6 | import org.jboss.netty.buffer.ChannelBuffer; |
| 7 | import org.onosproject.pcepio.exceptions.PcepParseException; |
| 8 | import org.onosproject.pcepio.protocol.PcepErrorObject; |
| 9 | import org.onosproject.pcepio.protocol.PcepOpenObject; |
| 10 | import org.slf4j.Logger; |
| 11 | import org.slf4j.LoggerFactory; |
| 12 | |
| 13 | /* |
| 14 | * Provide the error object list with open object. |
| 15 | */ |
| 16 | public class ErrorObjListWithOpen { |
| 17 | //errorObjList is mandatory |
| 18 | LinkedList<PcepErrorObject> llerrorObjList; |
| 19 | // openObject is optional |
| 20 | PcepOpenObject openObject; |
| 21 | // flag to check if open object is set or not |
| 22 | public boolean isOpenObjectSet; |
| 23 | protected static final Logger log = LoggerFactory.getLogger(ErrorObjListWithOpen.class); |
| 24 | |
| 25 | /* |
| 26 | * constructor to initialize errObj,openObj. |
| 27 | * |
| 28 | * @param errObj error object list |
| 29 | * @param openObj open object |
| 30 | */ |
| 31 | public ErrorObjListWithOpen(LinkedList<PcepErrorObject> errObj, PcepOpenObject openObj) { |
| 32 | this.llerrorObjList = errObj; |
| 33 | this.openObject = openObj; |
| 34 | if (openObj != null) { |
| 35 | isOpenObjectSet = true; |
| 36 | } else { |
| 37 | isOpenObjectSet = false; |
| 38 | } |
| 39 | } |
| 40 | |
| 41 | /* |
| 42 | * constructor to initialize errObj. |
| 43 | * |
| 44 | * @param errObj error object list |
| 45 | */ |
| 46 | public ErrorObjListWithOpen(LinkedList<PcepErrorObject> errObj) { |
| 47 | this.llerrorObjList = errObj; |
| 48 | this.openObject = null; |
| 49 | isOpenObjectSet = false; |
| 50 | } |
| 51 | |
| 52 | public LinkedList<Integer> getErrorType() { |
| 53 | LinkedList<Integer> errorType = new LinkedList<Integer>(); |
| 54 | if (llerrorObjList != null) { |
| 55 | ListIterator<PcepErrorObject> errObjListIterator = llerrorObjList.listIterator(); |
| 56 | int error; |
| 57 | PcepErrorObject errorObj; |
| 58 | while (errObjListIterator.hasNext()) { |
| 59 | errorObj = errObjListIterator.next(); |
| 60 | error = errorObj.getErrorType(); |
| 61 | errorType.add(error); |
| 62 | } |
| 63 | } |
| 64 | return errorType; |
| 65 | } |
| 66 | |
| 67 | public LinkedList<Integer> getErrorValue() { |
| 68 | LinkedList<Integer> errorValue = new LinkedList<Integer>(); |
| 69 | if (llerrorObjList != null) { |
| 70 | ListIterator<PcepErrorObject> errObjListIterator = llerrorObjList.listIterator(); |
| 71 | int error; |
| 72 | PcepErrorObject errorObj; |
| 73 | while (errObjListIterator.hasNext()) { |
| 74 | errorObj = errObjListIterator.next(); |
| 75 | error = errorObj.getErrorValue(); |
| 76 | errorValue.add(error); |
| 77 | |
| 78 | } |
| 79 | } |
| 80 | return errorValue; |
| 81 | } |
| 82 | /* |
| 83 | * Checks whether error object list is empty or not. |
| 84 | * |
| 85 | * @return whether error object list is empty or not |
| 86 | */ |
| 87 | public boolean isErrorObjListWithOpenPresent() { |
| 88 | // ( <error-obj-list> [<Open>] |
| 89 | // At least in this case <error-obj-list> should be present. |
| 90 | return (!this.llerrorObjList.isEmpty()) ? true : false; |
| 91 | } |
| 92 | |
| 93 | /* |
| 94 | * Write Error Object List and Open Object to channel buffer. |
| 95 | * |
| 96 | * @param bb of type channel buffer |
| 97 | * @throws PcepParseException when mandatory fields are not set |
| 98 | */ |
| 99 | public int write(ChannelBuffer bb) throws PcepParseException { |
| 100 | int iLenStartIndex = bb.writerIndex(); |
| 101 | boolean bIsErrObjListFound = false; |
| 102 | |
| 103 | //<error-obj-list> is mandatory , if not present throw exception. |
| 104 | if (llerrorObjList != null) { |
| 105 | ListIterator<PcepErrorObject> errObjListIterator = llerrorObjList.listIterator(); |
| 106 | while (errObjListIterator.hasNext()) { |
| 107 | errObjListIterator.next().write(bb); |
| 108 | bIsErrObjListFound = true; |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | if (!bIsErrObjListFound) { |
| 113 | throw new PcepParseException("Error: [ErrorObjListWithOpen::write] <error-obj-list> is mandatory."); |
| 114 | } |
| 115 | |
| 116 | //Open Object is optional , if present write. |
| 117 | if (openObject != null) { |
| 118 | openObject.write(bb); |
| 119 | } |
| 120 | |
| 121 | return bb.writerIndex() - iLenStartIndex; |
| 122 | } |
| 123 | |
| 124 | /* |
| 125 | * Prints the attributes of ErrorObject List with open Object. |
| 126 | */ |
| 127 | public void print() { |
| 128 | log.debug("ErrorObjListWithOpen:"); |
| 129 | ListIterator<PcepErrorObject> pcepErrorObjIterator = llerrorObjList.listIterator(); |
| 130 | log.debug("<error-obj-list> :"); |
| 131 | while (pcepErrorObjIterator.hasNext()) { |
| 132 | pcepErrorObjIterator.next().print(); |
| 133 | } |
| 134 | |
| 135 | log.debug("OpenObject:"); |
| 136 | if (openObject != null) { |
| 137 | openObject.print(); |
| 138 | } |
| 139 | } |
| 140 | } |