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.
17 package org.onosproject.pcepio.protocol.ver1;
19 import java.util.LinkedList;
20 import java.util.ListIterator;
22 import org.jboss.netty.buffer.ChannelBuffer;
23 import org.onosproject.pcepio.exceptions.PcepParseException;
24 import org.onosproject.pcepio.protocol.PcepAttribute;
25 import org.onosproject.pcepio.protocol.PcepBandwidthObject;
26 import org.onosproject.pcepio.protocol.PcepIroObject;
27 import org.onosproject.pcepio.protocol.PcepLspaObject;
28 import org.onosproject.pcepio.protocol.PcepMetricObject;
29 import org.onosproject.pcepio.types.PcepObjectHeader;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
33 import com.google.common.base.MoreObjects;
36 * Provides PCEP Attribute List.
38 public class PcepAttributeVer1 implements PcepAttribute {
40 /* Reference : RFC5440
42 * <attribute-list> ::=[<LSPA>]
47 * <metric-list> ::=<METRIC>[<metric-list>]
49 protected static final Logger log = LoggerFactory.getLogger(PcepAttributeVer1.class);
51 public static final int OBJECT_HEADER_LENGTH = 4;
54 private PcepLspaObject lspaObject;
55 private boolean isLspaObjectSet;
57 //PCEP Bandwidth Object
58 private PcepBandwidthObject bandwidthObject;
59 private boolean isBandwidthObjectSet;
62 private LinkedList<PcepMetricObject> llMetricList;
63 private boolean isMetricListSet;
66 private PcepIroObject iroObject;
67 private boolean isIroObjectSet;
70 * Default constructor to initialize member variables.
72 public PcepAttributeVer1() {
75 bandwidthObject = null;
78 this.isLspaObjectSet = false;
79 this.isBandwidthObjectSet = false;
80 this.isMetricListSet = false;
81 this.isIroObjectSet = false;
85 * Constructor to initialize all parameters for PCEP attribute.
87 * @param lspaObject PCEP lspa Object.
88 * @param bandwidthObject PCEP bandwidth object.
89 * @param llMetricList list of PCEP metric objects.
90 * @param iroObject PCEP iro object.
92 public PcepAttributeVer1(PcepLspaObject lspaObject, PcepBandwidthObject bandwidthObject,
93 LinkedList<PcepMetricObject> llMetricList, PcepIroObject iroObject) {
95 this.lspaObject = lspaObject;
96 this.bandwidthObject = bandwidthObject;
97 this.llMetricList = llMetricList;
98 this.iroObject = iroObject;
99 if (lspaObject == null) {
100 this.isLspaObjectSet = false;
102 this.isLspaObjectSet = true;
104 if (bandwidthObject == null) {
105 this.isBandwidthObjectSet = false;
107 this.isBandwidthObjectSet = true;
109 if (llMetricList == null) {
110 this.isMetricListSet = false;
112 this.isMetricListSet = true;
114 if (iroObject == null) {
115 this.isIroObjectSet = false;
117 this.isIroObjectSet = true;
122 * constructor to initialize bandwidthObject.
124 * @param bandwidthObject bandwidth object
126 public PcepAttributeVer1(PcepBandwidthObject bandwidthObject) {
127 this.isLspaObjectSet = false;
129 this.bandwidthObject = bandwidthObject;
130 this.isBandwidthObjectSet = true;
132 this.isMetricListSet = false;
134 this.isIroObjectSet = false;
138 * Parse list for MeticObject.
140 * @param cb of type channel buffer
141 * @return true if parsing metric list is success
142 * @throws PcepParseException when a non metric object is received
144 public boolean parseMetricList(ChannelBuffer cb) throws PcepParseException {
146 if (llMetricList == null) {
147 llMetricList = new LinkedList<>();
150 PcepMetricObject metriclist;
152 //caller should verify for metric object
153 byte yObjClass = PcepMetricObjectVer1.METRIC_OBJ_CLASS;
154 byte yObjType = PcepMetricObjectVer1.METRIC_OBJ_TYPE;
156 while ((yObjClass == PcepMetricObjectVer1.METRIC_OBJ_CLASS)
157 && (yObjType == PcepMetricObjectVer1.METRIC_OBJ_TYPE)) {
159 metriclist = PcepMetricObjectVer1.read(cb);
160 llMetricList.add(metriclist);
164 if (cb.readableBytes() > OBJECT_HEADER_LENGTH) {
165 cb.markReaderIndex();
166 PcepObjectHeader tempObjHeader = PcepObjectHeader.read(cb);
167 cb.resetReaderIndex();
168 yObjClass = tempObjHeader.getObjClass();
169 yObjType = tempObjHeader.getObjType();
176 * Reads lspa , bandwidth , Metriclist and Iro objects and sets the objects.
178 * @param cb of type channel buffer
179 * @return instance of Pcep Attribute
180 * @throws PcepParseException while parsing Pcep Attributes from channel buffer
183 public static PcepAttribute read(ChannelBuffer cb) throws PcepParseException {
184 if (cb.readableBytes() < OBJECT_HEADER_LENGTH) {
187 //check whether any pcep attribute is present
188 cb.markReaderIndex();
189 PcepObjectHeader tempObjHeader = PcepObjectHeader.read(cb);
190 cb.resetReaderIndex();
191 byte yObjClass = tempObjHeader.getObjClass();
193 if (PcepLspaObjectVer1.LSPA_OBJ_CLASS != yObjClass && PcepBandwidthObjectVer1.BANDWIDTH_OBJ_CLASS != yObjClass
194 && PcepMetricObjectVer1.METRIC_OBJ_CLASS != yObjClass && PcepIroObjectVer1.IRO_OBJ_CLASS != yObjClass) {
195 //No PCEP attribute is present
199 PcepAttributeVer1 pcepAttribute = new PcepAttributeVer1();
201 //If LSPA present then store it.LSPA is optional
202 if (yObjClass == PcepLspaObjectVer1.LSPA_OBJ_CLASS) {
203 pcepAttribute.setLspaObject(PcepLspaObjectVer1.read(cb));
204 yObjClass = checkNextObject(cb);
207 //If BANDWIDTH present then store it.BANDWIDTH is optional
208 if (yObjClass == PcepBandwidthObjectVer1.BANDWIDTH_OBJ_CLASS) {
209 pcepAttribute.setBandwidthObject(PcepBandwidthObjectVer1.read(cb));
210 yObjClass = checkNextObject(cb);
213 //If Metric list present then store it.MetricList is optional
214 if (yObjClass == PcepMetricObjectVer1.METRIC_OBJ_CLASS) {
215 pcepAttribute.parseMetricList(cb);
216 yObjClass = checkNextObject(cb);
219 //If IRO present then store it.IRO is optional
220 if (yObjClass == PcepIroObjectVer1.IRO_OBJ_CLASS) {
221 pcepAttribute.setIroObject(PcepIroObjectVer1.read(cb));
224 PcepLspaObject lspaObject = pcepAttribute.getLspaObject();
225 PcepBandwidthObject bandwidthObject = pcepAttribute.getBandwidthObject();
226 LinkedList<PcepMetricObject> metriclist = pcepAttribute.llMetricList;
227 PcepIroObject iroObject = pcepAttribute.getIroObject();
229 return new PcepAttributeVer1(lspaObject, bandwidthObject, metriclist, iroObject);
233 * Checks whether there is a more object or not.
235 * @param cb of type channel buffer
236 * @return instance of object header
238 private static byte checkNextObject(ChannelBuffer cb) {
239 if (cb.readableBytes() < OBJECT_HEADER_LENGTH) {
242 cb.markReaderIndex();
243 PcepObjectHeader tempObjHeader = PcepObjectHeader.read(cb);
244 cb.resetReaderIndex();
245 return tempObjHeader.getObjClass();
249 public int write(ChannelBuffer cb) throws PcepParseException {
250 int iLenStartIndex = cb.writerIndex();
251 //PCEP LSPA object is optional
252 if (this.isLspaObjectSet) {
253 this.lspaObject.write(cb);
256 //PCEP BANDWIDTH object is optional
257 if (this.isBandwidthObjectSet) {
258 this.bandwidthObject.write(cb);
261 //PCEP Metric list is optional
262 if (this.isMetricListSet) {
263 ListIterator<PcepMetricObject> listIterator = this.llMetricList.listIterator();
264 while (listIterator.hasNext()) {
265 listIterator.next().write(cb);
269 //PCEP IRO object is optional
270 if (this.isIroObjectSet) {
271 this.iroObject.write(cb);
273 return cb.writerIndex() - iLenStartIndex;
277 public PcepLspaObject getLspaObject() {
282 public PcepBandwidthObject getBandwidthObject() {
283 return bandwidthObject;
287 public LinkedList<PcepMetricObject> getMetricObjectList() {
292 public PcepIroObject getIroObject() {
297 public void setBandwidthObject(PcepBandwidthObject bandwidthObject) {
298 this.isBandwidthObjectSet = true;
299 this.bandwidthObject = bandwidthObject;
303 public void setMetricObjectList(LinkedList<PcepMetricObject> llMetricList) {
304 this.isMetricListSet = true;
305 this.llMetricList = llMetricList;
310 public void setLspaObject(PcepLspaObject lspaObject) {
311 this.isLspaObjectSet = true;
312 this.lspaObject = lspaObject;
316 public void setIroObject(PcepIroObject iroObject) {
317 this.isIroObjectSet = true;
318 this.iroObject = iroObject;
322 * Builder class for PCEP attributes.
324 public static class Builder implements PcepAttribute.Builder {
327 private PcepLspaObject lspaObject;
328 private boolean isLspaObjectSet;
330 //PCEP BANDWIDTH Object
331 private PcepBandwidthObject bandwidthObject;
332 private boolean isBandwidthObjectSet;
335 private LinkedList<PcepMetricObject> llMetricList;
336 private boolean isMetricListSet;
339 private PcepIroObject iroObject;
340 private boolean isIroObjectSet;
343 public PcepAttribute build() {
346 PcepLspaObject lspaObject = null;
348 //PCEP BANDWIDTH Object
349 PcepBandwidthObject bandwidthObject = null;
352 LinkedList<PcepMetricObject> llMetricList = null;
355 PcepIroObject iroObject = null;
357 if (this.isLspaObjectSet) {
358 lspaObject = this.lspaObject;
360 if (this.isBandwidthObjectSet) {
361 bandwidthObject = this.bandwidthObject;
363 if (this.isMetricListSet) {
364 llMetricList = this.llMetricList;
366 if (this.isIroObjectSet) {
367 iroObject = this.iroObject;
369 return new PcepAttributeVer1(lspaObject, bandwidthObject, llMetricList, iroObject);
373 public PcepLspaObject getLspaObject() {
374 return this.lspaObject;
378 public PcepBandwidthObject getBandwidthObject() {
379 return this.bandwidthObject;
383 public LinkedList<PcepMetricObject> getMetricObjectList() {
384 return this.llMetricList;
388 public PcepIroObject getIroObject() {
389 return this.iroObject;
393 public Builder setBandwidthObject(PcepBandwidthObject bandwidthObject) {
394 this.isBandwidthObjectSet = true;
395 this.bandwidthObject = bandwidthObject;
400 public Builder setMetricObjectList(LinkedList<PcepMetricObject> llMetricList) {
401 this.isMetricListSet = true;
402 this.llMetricList = llMetricList;
407 public Builder setLspaObject(PcepLspaObject lspaObject) {
408 this.isLspaObjectSet = true;
409 this.lspaObject = lspaObject;
414 public Builder setIroObject(PcepIroObject iroObject) {
415 this.isIroObjectSet = true;
416 this.iroObject = iroObject;
422 public String toString() {
423 return MoreObjects.toStringHelper(getClass())
425 .add("lspaObject", lspaObject)
426 .add("bandwidthObject", bandwidthObject)
427 .add("MetricObjectList", llMetricList)
428 .add("IroObject", iroObject)