blob: 40f89f4c45483e62786f3c2c589e315ce5113d20 [file] [log] [blame]
Thomas Vachuska58de4162015-09-10 16:15:33 -07001/*
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 */
Sho SHIMIZU74361c12015-08-11 12:31:48 -070016package org.onosproject.pcepio.types;
17
18import java.util.LinkedList;
19import java.util.ListIterator;
20
21import org.jboss.netty.buffer.ChannelBuffer;
22import org.onosproject.pcepio.exceptions.PcepParseException;
23import org.onosproject.pcepio.protocol.PcepErrorObject;
24import org.onosproject.pcepio.protocol.PcepOpenObject;
25import org.slf4j.Logger;
26import org.slf4j.LoggerFactory;
27
bharat saraswalf7364db2015-08-11 13:39:31 +053028import com.google.common.base.MoreObjects;
bharat saraswalf7364db2015-08-11 13:39:31 +053029
bharat saraswale1806302015-08-21 12:16:46 +053030/**
Sho SHIMIZU74361c12015-08-11 12:31:48 -070031 * Provide the error object list with open object.
32 */
33public class ErrorObjListWithOpen {
34 //errorObjList is mandatory
bharat saraswalf7364db2015-08-11 13:39:31 +053035 private LinkedList<PcepErrorObject> llerrorObjList;
Sho SHIMIZU74361c12015-08-11 12:31:48 -070036 // openObject is optional
bharat saraswalf7364db2015-08-11 13:39:31 +053037 private PcepOpenObject openObject;
Sho SHIMIZU74361c12015-08-11 12:31:48 -070038 // flag to check if open object is set or not
bharat saraswale1806302015-08-21 12:16:46 +053039 private boolean isOpenObjectSet;
Sho SHIMIZU74361c12015-08-11 12:31:48 -070040 protected static final Logger log = LoggerFactory.getLogger(ErrorObjListWithOpen.class);
41
bharat saraswale1806302015-08-21 12:16:46 +053042 /**
43 * Constructor to initialize Error and OPEN object.
Sho SHIMIZU74361c12015-08-11 12:31:48 -070044 *
bharat saraswale1806302015-08-21 12:16:46 +053045 * @param errObj ERROR object list
46 * @param openObj OPEN object
Sho SHIMIZU74361c12015-08-11 12:31:48 -070047 */
48 public ErrorObjListWithOpen(LinkedList<PcepErrorObject> errObj, PcepOpenObject openObj) {
49 this.llerrorObjList = errObj;
50 this.openObject = openObj;
51 if (openObj != null) {
52 isOpenObjectSet = true;
53 } else {
54 isOpenObjectSet = false;
55 }
56 }
57
bharat saraswale1806302015-08-21 12:16:46 +053058 /**
59 * Constructor to initialize ERROR Object.
Sho SHIMIZU74361c12015-08-11 12:31:48 -070060 *
bharat saraswale1806302015-08-21 12:16:46 +053061 * @param errObj ERROR Object list
Sho SHIMIZU74361c12015-08-11 12:31:48 -070062 */
63 public ErrorObjListWithOpen(LinkedList<PcepErrorObject> errObj) {
64 this.llerrorObjList = errObj;
65 this.openObject = null;
66 isOpenObjectSet = false;
67 }
68
bharat saraswalf7364db2015-08-11 13:39:31 +053069 /**
bharat saraswale1806302015-08-21 12:16:46 +053070 * Return list of Error Types.
bharat saraswalf7364db2015-08-11 13:39:31 +053071 *
bharat saraswale1806302015-08-21 12:16:46 +053072 * @return error types list
bharat saraswalf7364db2015-08-11 13:39:31 +053073 */
Sho SHIMIZU74361c12015-08-11 12:31:48 -070074 public LinkedList<Integer> getErrorType() {
Sho SHIMIZU9b8274c2015-09-04 15:54:24 -070075 LinkedList<Integer> errorType = new LinkedList<>();
Sho SHIMIZU74361c12015-08-11 12:31:48 -070076 if (llerrorObjList != null) {
77 ListIterator<PcepErrorObject> errObjListIterator = llerrorObjList.listIterator();
78 int error;
79 PcepErrorObject errorObj;
80 while (errObjListIterator.hasNext()) {
bharat saraswalf7364db2015-08-11 13:39:31 +053081 errorObj = errObjListIterator.next();
Sho SHIMIZU74361c12015-08-11 12:31:48 -070082 error = errorObj.getErrorType();
83 errorType.add(error);
84 }
85 }
86 return errorType;
87 }
88
bharat saraswalf7364db2015-08-11 13:39:31 +053089 /**
bharat saraswale1806302015-08-21 12:16:46 +053090 * Return list of Error Values.
bharat saraswalf7364db2015-08-11 13:39:31 +053091 *
bharat saraswale1806302015-08-21 12:16:46 +053092 * @return error values list
bharat saraswalf7364db2015-08-11 13:39:31 +053093 */
Sho SHIMIZU74361c12015-08-11 12:31:48 -070094 public LinkedList<Integer> getErrorValue() {
Sho SHIMIZU9b8274c2015-09-04 15:54:24 -070095 LinkedList<Integer> errorValue = new LinkedList<>();
Sho SHIMIZU74361c12015-08-11 12:31:48 -070096 if (llerrorObjList != null) {
97 ListIterator<PcepErrorObject> errObjListIterator = llerrorObjList.listIterator();
98 int error;
99 PcepErrorObject errorObj;
100 while (errObjListIterator.hasNext()) {
bharat saraswalf7364db2015-08-11 13:39:31 +0530101 errorObj = errObjListIterator.next();
Sho SHIMIZU74361c12015-08-11 12:31:48 -0700102 error = errorObj.getErrorValue();
103 errorValue.add(error);
Sho SHIMIZU74361c12015-08-11 12:31:48 -0700104 }
105 }
106 return errorValue;
107 }
bharat saraswalf7364db2015-08-11 13:39:31 +0530108
109 /**
bharat saraswale1806302015-08-21 12:16:46 +0530110 * Checks whether ERROR Object list is empty or not.
Sho SHIMIZU74361c12015-08-11 12:31:48 -0700111 *
bharat saraswale1806302015-08-21 12:16:46 +0530112 * @return true if ERROR Object list is empty otherwise false
Sho SHIMIZU74361c12015-08-11 12:31:48 -0700113 */
114 public boolean isErrorObjListWithOpenPresent() {
115 // ( <error-obj-list> [<Open>]
116 // At least in this case <error-obj-list> should be present.
Phanendra Mandaf346ea82015-09-04 15:21:39 -0700117 return !this.llerrorObjList.isEmpty();
Sho SHIMIZU74361c12015-08-11 12:31:48 -0700118 }
119
bharat saraswalf7364db2015-08-11 13:39:31 +0530120 /**
Sho SHIMIZU74361c12015-08-11 12:31:48 -0700121 * Write Error Object List and Open Object to channel buffer.
122 *
bharat saraswale1806302015-08-21 12:16:46 +0530123 * @param cb output channel buffer
124 * @return length of written Error object list with open
Sho SHIMIZU74361c12015-08-11 12:31:48 -0700125 * @throws PcepParseException when mandatory fields are not set
126 */
bharat saraswale1806302015-08-21 12:16:46 +0530127 public int write(ChannelBuffer cb) throws PcepParseException {
128 int iLenStartIndex = cb.writerIndex();
Sho SHIMIZU74361c12015-08-11 12:31:48 -0700129 boolean bIsErrObjListFound = false;
130
131 //<error-obj-list> is mandatory , if not present throw exception.
132 if (llerrorObjList != null) {
133 ListIterator<PcepErrorObject> errObjListIterator = llerrorObjList.listIterator();
134 while (errObjListIterator.hasNext()) {
bharat saraswale1806302015-08-21 12:16:46 +0530135 errObjListIterator.next().write(cb);
Sho SHIMIZU74361c12015-08-11 12:31:48 -0700136 bIsErrObjListFound = true;
137 }
138 }
139
140 if (!bIsErrObjListFound) {
bharat saraswale1806302015-08-21 12:16:46 +0530141 throw new PcepParseException("<error-obj-list> is mandatory.");
Sho SHIMIZU74361c12015-08-11 12:31:48 -0700142 }
143
144 //Open Object is optional , if present write.
145 if (openObject != null) {
bharat saraswale1806302015-08-21 12:16:46 +0530146 openObject.write(cb);
Sho SHIMIZU74361c12015-08-11 12:31:48 -0700147 }
148
bharat saraswale1806302015-08-21 12:16:46 +0530149 return cb.writerIndex() - iLenStartIndex;
Sho SHIMIZU74361c12015-08-11 12:31:48 -0700150 }
bharat saraswalf7364db2015-08-11 13:39:31 +0530151
152 @Override
153 public String toString() {
Sho SHIMIZU7cdbcf72015-09-03 14:43:05 -0700154 return MoreObjects.toStringHelper(getClass())
155 .omitNullValues()
156 .add("ErrorObjList", llerrorObjList)
157 .add("Open", openObject)
158 .toString();
bharat saraswalf7364db2015-08-11 13:39:31 +0530159 }
Sho SHIMIZU74361c12015-08-11 12:31:48 -0700160}