Sho SHIMIZU | e81e4db | 2015-09-03 09:44:38 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 Open Networking Laboratory |
| 3 | * |
| 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 | */ |
| 16 | package org.onosproject.pcepio.protocol.ver1; |
| 17 | |
| 18 | import java.util.LinkedList; |
Mahesh Poojary S | f1bbd36 | 2016-02-25 18:19:59 +0530 | [diff] [blame^] | 19 | import java.util.List; |
Sho SHIMIZU | e81e4db | 2015-09-03 09:44:38 -0700 | [diff] [blame] | 20 | import java.util.ListIterator; |
| 21 | |
| 22 | import org.jboss.netty.buffer.ChannelBuffer; |
| 23 | import org.onosproject.pcepio.exceptions.PcepParseException; |
| 24 | import org.onosproject.pcepio.protocol.PcepError; |
| 25 | import org.onosproject.pcepio.protocol.PcepErrorInfo; |
| 26 | import org.onosproject.pcepio.protocol.PcepErrorObject; |
| 27 | import org.onosproject.pcepio.protocol.PcepRPObject; |
Mahesh Poojary S | f1bbd36 | 2016-02-25 18:19:59 +0530 | [diff] [blame^] | 28 | import org.onosproject.pcepio.protocol.PcepLSObject; |
Sho SHIMIZU | e81e4db | 2015-09-03 09:44:38 -0700 | [diff] [blame] | 29 | import org.onosproject.pcepio.types.PcepObjectHeader; |
| 30 | import org.slf4j.Logger; |
| 31 | import org.slf4j.LoggerFactory; |
| 32 | |
| 33 | import com.google.common.base.MoreObjects; |
| 34 | |
| 35 | /** |
| 36 | * Provides PCEP Error Info. |
Mahesh Poojary S | f1bbd36 | 2016-02-25 18:19:59 +0530 | [diff] [blame^] | 37 | * Reference : draft-dhodylee-pce-pcep-ls-01, section 8.2. |
Sho SHIMIZU | e81e4db | 2015-09-03 09:44:38 -0700 | [diff] [blame] | 38 | */ |
| 39 | public class PcepErrorInfoVer1 implements PcepErrorInfo { |
| 40 | |
| 41 | protected static final Logger log = LoggerFactory.getLogger(PcepErrorInfoVer1.class); |
| 42 | //Error list is optional |
Mahesh Poojary S | f1bbd36 | 2016-02-25 18:19:59 +0530 | [diff] [blame^] | 43 | private List<PcepError> errList; |
Sho SHIMIZU | e81e4db | 2015-09-03 09:44:38 -0700 | [diff] [blame] | 44 | |
| 45 | /** |
| 46 | * Constructor to add PCEP error object to the list. |
| 47 | * |
| 48 | * @param llRPObjList list of PCEP RP object |
Mahesh Poojary S | f1bbd36 | 2016-02-25 18:19:59 +0530 | [diff] [blame^] | 49 | * @param llLSObjList list of PCEP LS object |
Sho SHIMIZU | e81e4db | 2015-09-03 09:44:38 -0700 | [diff] [blame] | 50 | * @param llErrObjList list of PCEP error object |
| 51 | */ |
Mahesh Poojary S | f1bbd36 | 2016-02-25 18:19:59 +0530 | [diff] [blame^] | 52 | public PcepErrorInfoVer1(List<PcepRPObject> llRPObjList, List<PcepLSObject> llLSObjList, |
| 53 | List<PcepErrorObject> llErrObjList) { |
Sho SHIMIZU | 9b8274c | 2015-09-04 15:54:24 -0700 | [diff] [blame] | 54 | this.errList = new LinkedList<>(); |
Sho SHIMIZU | 28c91c8 | 2015-09-03 11:40:02 -0700 | [diff] [blame] | 55 | if ((llErrObjList != null) && (!llErrObjList.isEmpty())) { |
Mahesh Poojary S | f1bbd36 | 2016-02-25 18:19:59 +0530 | [diff] [blame^] | 56 | this.errList.add(new PcepErrorVer1(llRPObjList, llLSObjList, llErrObjList)); |
Sho SHIMIZU | e81e4db | 2015-09-03 09:44:38 -0700 | [diff] [blame] | 57 | } |
| 58 | } |
| 59 | |
| 60 | /** |
| 61 | * Constructor to initialize error info. |
| 62 | * |
| 63 | * @param errll linked list or pcep error |
| 64 | */ |
Mahesh Poojary S | f1bbd36 | 2016-02-25 18:19:59 +0530 | [diff] [blame^] | 65 | public PcepErrorInfoVer1(List<PcepError> errll) { |
Sho SHIMIZU | e81e4db | 2015-09-03 09:44:38 -0700 | [diff] [blame] | 66 | this.errList = errll; |
| 67 | } |
| 68 | |
| 69 | @Override |
| 70 | public boolean isErrorInfoPresent() { |
Phanendra Manda | f346ea8 | 2015-09-04 15:21:39 -0700 | [diff] [blame] | 71 | return !this.errList.isEmpty(); |
Sho SHIMIZU | e81e4db | 2015-09-03 09:44:38 -0700 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | @Override |
| 75 | public void read(ChannelBuffer cb) throws PcepParseException { |
| 76 | PcepObjectHeader tempObjHeader; |
| 77 | |
| 78 | while (0 < cb.readableBytes()) { |
| 79 | cb.markReaderIndex(); |
| 80 | tempObjHeader = PcepObjectHeader.read(cb); |
| 81 | cb.resetReaderIndex(); |
| 82 | byte yObjClass = tempObjHeader.getObjClass(); |
Mahesh Poojary S | f1bbd36 | 2016-02-25 18:19:59 +0530 | [diff] [blame^] | 83 | if ((yObjClass != PcepRPObjectVer1.RP_OBJ_CLASS) && (yObjClass != PcepLSObjectVer1.LS_OBJ_CLASS) |
Sho SHIMIZU | e81e4db | 2015-09-03 09:44:38 -0700 | [diff] [blame] | 84 | && (yObjClass != PcepErrorObjectVer1.ERROR_OBJ_CLASS)) { |
| 85 | throw new PcepParseException("Unknown Object is present in PCEP-ERROR. Object Class: " + yObjClass); |
| 86 | } |
| 87 | |
| 88 | this.errList.add(PcepErrorVer1.read(cb)); |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | @Override |
| 93 | public void write(ChannelBuffer cb) throws PcepParseException { |
| 94 | //write <error> |
| 95 | ListIterator<PcepError> listIterator = errList.listIterator(); |
| 96 | while (listIterator.hasNext()) { |
| 97 | PcepError pcepError = listIterator.next(); |
| 98 | |
| 99 | //RP Object list is optional |
Mahesh Poojary S | f1bbd36 | 2016-02-25 18:19:59 +0530 | [diff] [blame^] | 100 | List<PcepRPObject> llRPObjList = pcepError.getRPObjList(); |
Sho SHIMIZU | e81e4db | 2015-09-03 09:44:38 -0700 | [diff] [blame] | 101 | if (llRPObjList != null) { |
| 102 | ListIterator<PcepRPObject> rpListIterator = llRPObjList.listIterator(); |
| 103 | while (rpListIterator.hasNext()) { |
| 104 | rpListIterator.next().write(cb); |
| 105 | } |
| 106 | } |
| 107 | |
Mahesh Poojary S | f1bbd36 | 2016-02-25 18:19:59 +0530 | [diff] [blame^] | 108 | //LS Object list is optional |
| 109 | List<PcepLSObject> llLSObjList = pcepError.getLSObjList(); |
| 110 | if (llLSObjList != null) { |
| 111 | ListIterator<PcepLSObject> teListIterator = llLSObjList.listIterator(); |
Sho SHIMIZU | e81e4db | 2015-09-03 09:44:38 -0700 | [diff] [blame] | 112 | while (teListIterator.hasNext()) { |
| 113 | teListIterator.next().write(cb); |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | // <error-obj-list> is mandatory |
| 118 | boolean bIsErrorObjListFound = false; |
| 119 | |
Mahesh Poojary S | f1bbd36 | 2016-02-25 18:19:59 +0530 | [diff] [blame^] | 120 | List<PcepErrorObject> llErrObjList = pcepError.getErrorObjList(); |
Sho SHIMIZU | e81e4db | 2015-09-03 09:44:38 -0700 | [diff] [blame] | 121 | if (llErrObjList != null) { |
| 122 | ListIterator<PcepErrorObject> errObjListIterator = llErrObjList.listIterator(); |
| 123 | while (errObjListIterator.hasNext()) { |
| 124 | errObjListIterator.next().write(cb); |
| 125 | bIsErrorObjListFound = true; |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | if (!bIsErrorObjListFound) { |
| 130 | throw new PcepParseException("<error-obj-list> is mandatory."); |
| 131 | } |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | @Override |
Mahesh Poojary S | f1bbd36 | 2016-02-25 18:19:59 +0530 | [diff] [blame^] | 136 | public List<Integer> getErrorType() { |
| 137 | List<Integer> errorType = new LinkedList<>(); |
Sho SHIMIZU | e81e4db | 2015-09-03 09:44:38 -0700 | [diff] [blame] | 138 | ListIterator<PcepError> listIterator = errList.listIterator(); |
| 139 | PcepErrorObject errObj; |
| 140 | int error; |
| 141 | while (listIterator.hasNext()) { |
| 142 | PcepError pcepError = listIterator.next(); |
Mahesh Poojary S | f1bbd36 | 2016-02-25 18:19:59 +0530 | [diff] [blame^] | 143 | List<PcepErrorObject> llErrObjList = pcepError.getErrorObjList(); |
Sho SHIMIZU | e81e4db | 2015-09-03 09:44:38 -0700 | [diff] [blame] | 144 | if (llErrObjList != null) { |
| 145 | ListIterator<PcepErrorObject> errObjListIterator = llErrObjList.listIterator(); |
| 146 | while (errObjListIterator.hasNext()) { |
| 147 | errObj = errObjListIterator.next(); |
| 148 | error = errObj.getErrorType(); |
| 149 | errorType.add(error); |
| 150 | } |
| 151 | } |
| 152 | } |
| 153 | return errorType; |
| 154 | } |
| 155 | |
| 156 | @Override |
Mahesh Poojary S | f1bbd36 | 2016-02-25 18:19:59 +0530 | [diff] [blame^] | 157 | public List<Integer> getErrorValue() { |
| 158 | List<Integer> errorValue = new LinkedList<>(); |
Sho SHIMIZU | e81e4db | 2015-09-03 09:44:38 -0700 | [diff] [blame] | 159 | ListIterator<PcepError> listIterator = errList.listIterator(); |
| 160 | PcepErrorObject errObj; |
| 161 | int error; |
| 162 | while (listIterator.hasNext()) { |
| 163 | PcepError pcepError = listIterator.next(); |
Mahesh Poojary S | f1bbd36 | 2016-02-25 18:19:59 +0530 | [diff] [blame^] | 164 | List<PcepErrorObject> llErrObjList = pcepError.getErrorObjList(); |
Sho SHIMIZU | e81e4db | 2015-09-03 09:44:38 -0700 | [diff] [blame] | 165 | if (llErrObjList != null) { |
| 166 | ListIterator<PcepErrorObject> errObjListIterator = llErrObjList.listIterator(); |
| 167 | while (errObjListIterator.hasNext()) { |
| 168 | errObj = errObjListIterator.next(); |
| 169 | error = errObj.getErrorValue(); |
| 170 | errorValue.add(error); |
| 171 | } |
| 172 | } |
| 173 | } |
| 174 | return errorValue; |
| 175 | } |
| 176 | |
| 177 | /** |
| 178 | * Builder class for PCEP error info. |
| 179 | */ |
| 180 | public static class Builder implements PcepErrorInfo.Builder { |
Mahesh Poojary S | f1bbd36 | 2016-02-25 18:19:59 +0530 | [diff] [blame^] | 181 | private List<PcepError> errll; |
Sho SHIMIZU | e81e4db | 2015-09-03 09:44:38 -0700 | [diff] [blame] | 182 | |
| 183 | @Override |
| 184 | public PcepErrorInfo build() { |
| 185 | return new PcepErrorInfoVer1(errll); |
| 186 | } |
| 187 | |
| 188 | @Override |
Mahesh Poojary S | f1bbd36 | 2016-02-25 18:19:59 +0530 | [diff] [blame^] | 189 | public List<PcepError> getPcepErrorList() { |
Sho SHIMIZU | e81e4db | 2015-09-03 09:44:38 -0700 | [diff] [blame] | 190 | return this.errll; |
| 191 | } |
| 192 | |
| 193 | @Override |
Mahesh Poojary S | f1bbd36 | 2016-02-25 18:19:59 +0530 | [diff] [blame^] | 194 | public Builder setPcepErrorList(List<PcepError> errll) { |
Sho SHIMIZU | e81e4db | 2015-09-03 09:44:38 -0700 | [diff] [blame] | 195 | this.errll = errll; |
| 196 | return this; |
| 197 | } |
| 198 | } |
| 199 | |
| 200 | @Override |
| 201 | public String toString() { |
Sho SHIMIZU | 7cdbcf7 | 2015-09-03 14:43:05 -0700 | [diff] [blame] | 202 | return MoreObjects.toStringHelper(getClass()) |
| 203 | .add("ErrorList", errList).toString(); |
Sho SHIMIZU | e81e4db | 2015-09-03 09:44:38 -0700 | [diff] [blame] | 204 | } |
| 205 | } |