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.PcepLabelRange;
25 import org.onosproject.pcepio.protocol.PcepLabelRangeObject;
26 import org.onosproject.pcepio.protocol.PcepSrpObject;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
30 import com.google.common.base.MoreObjects;
33 * Provides PCEP Label Range.
35 public class PcepLabelRangeVer1 implements PcepLabelRange {
37 protected static final Logger log = LoggerFactory.getLogger(PcepLabelRangeVer1.class);
40 <label-range> ::= <SRP>
43 <labelrange-list>::=<LABEL-RANGE>[<labelrange-list>]
47 private PcepSrpObject srpObject;
48 //<labelrange-list> of type PcepLabelRangeObject.
49 private LinkedList<PcepLabelRangeObject> llLabelRangeList;
52 * Default Constructor.
54 public PcepLabelRangeVer1() {
56 llLabelRangeList = null;
60 * Constructor to initialize objects.
62 * @param srpObj PCEP Srp object.
63 * @param llLabelRangeList list of PcepLabelRangeObject.
65 PcepLabelRangeVer1(PcepSrpObject srpObj, LinkedList<PcepLabelRangeObject> llLabelRangeList) {
66 this.srpObject = srpObj;
67 this.llLabelRangeList = llLabelRangeList;
71 public PcepSrpObject getSrpObject() {
76 public void setSrpObject(PcepSrpObject srpObject) {
77 this.srpObject = srpObject;
82 public LinkedList<PcepLabelRangeObject> getLabelRangeList() {
83 return llLabelRangeList;
87 public void setLabelRangeList(LinkedList<PcepLabelRangeObject> ll) {
88 this.llLabelRangeList = ll;
92 * Reads channel buffer and returns object of PcepLabelRange.
94 * @param cb of type channel buffer.
95 * @return object of PcepLabelRange
96 * @throws PcepParseException when fails to read from channel buffer
98 public static PcepLabelRange read(ChannelBuffer cb) throws PcepParseException {
100 //parse and store SRP mandatory object
101 PcepSrpObject srpObj = null;
102 srpObj = PcepSrpObjectVer1.read(cb);
103 if (srpObj == null) {
104 throw new PcepParseException("Exception while parsing srp object");
107 LinkedList<PcepLabelRangeObject> llLabelRangeList = new LinkedList<>();
108 boolean bFoundLabelRangeObj = false;
109 while (0 < cb.readableBytes()) {
110 //parse and store <labelrange-list>
111 PcepLabelRangeObject lrObj;
112 lrObj = PcepLabelRangeObjectVer1.read(cb);
114 throw new PcepParseException("Exception while parsing label range object");
116 llLabelRangeList.add(lrObj);
117 bFoundLabelRangeObj = true;
121 if (!bFoundLabelRangeObj) {
122 throw new PcepParseException("At least one LABEL-RANGE MUST be present.");
124 return new PcepLabelRangeVer1(srpObj, llLabelRangeList);
128 public int write(ChannelBuffer cb) throws PcepParseException {
129 //write Object header
130 int objStartIndex = cb.writerIndex();
133 int objLenIndex = srpObject.write(cb);
135 if (objLenIndex <= 0) {
136 throw new PcepParseException("bjectLength is " + objLenIndex);
139 //write <labelrange-list>
140 ListIterator<PcepLabelRangeObject> listIterator = llLabelRangeList.listIterator();
141 while (listIterator.hasNext()) {
142 listIterator.next().write(cb);
145 //Update object length now
146 int length = cb.writerIndex() - objStartIndex;
147 // As per RFC the length of object should be
149 int pad = length % 4;
152 for (int i = 0; i < pad; i++) {
153 cb.writeByte((byte) 0);
155 length = length + pad;
157 cb.setShort(objLenIndex, (short) length);
162 public String toString() {
163 return MoreObjects.toStringHelper(getClass())
164 .add("srpObject", srpObject)
165 .add("LabelRangeList", llLabelRangeList)