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 org.jboss.netty.buffer.ChannelBuffer;
20 import org.onosproject.pcepio.exceptions.PcepParseException;
21 import org.onosproject.pcepio.protocol.PcepAttribute;
22 import org.onosproject.pcepio.protocol.PcepBandwidthObject;
23 import org.onosproject.pcepio.protocol.PcepEroObject;
24 import org.onosproject.pcepio.protocol.PcepLspObject;
25 import org.onosproject.pcepio.protocol.PcepRroObject;
26 import org.onosproject.pcepio.protocol.PcepSrpObject;
27 import org.onosproject.pcepio.protocol.PcepStateReport;
28 import org.onosproject.pcepio.types.PcepObjectHeader;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
32 import com.google.common.base.MoreObjects;
33 import com.google.common.base.MoreObjects.ToStringHelper;
36 * Provide the State Report for the Pcep Report Message.
37 * Reference :PCE extensions for stateful draft-ietf-pce-stateful-pce-10.
39 public class PcepStateReportVer1 implements PcepStateReport {
41 * <state-report> ::= [<SRP>]
45 <path> ::= <ERO><attribute-list>[<RRO>]
48 protected static final Logger log = LoggerFactory.getLogger(PcepStateReport.class);
50 public static final int OBJECT_HEADER_LENGTH = 4;
53 * Provides PCEP Message path for report message.
55 public class PcepMsgPath implements PcepStateReport.PcepMsgPath {
58 * <path> ::= <ERO><attribute-list>[<RRO>]
62 private PcepEroObject eroObj;
63 private boolean isEroObjectSet;
65 private PcepAttribute attrList;
66 private boolean isAttributeListSet;
68 private PcepRroObject rroObj;
69 private boolean isRroObjectSet;
70 private PcepBandwidthObject bandwidth;
71 private boolean isBandwidthObjectSet;
74 * Constructor to reset the parameters.
76 public PcepMsgPath() {
80 this.isEroObjectSet = false;
81 this.isAttributeListSet = false;
82 this.isRroObjectSet = false;
83 this.isBandwidthObjectSet = false;
87 * Constructor to initialize the parameters from PCEP Message path.
89 * @param eroObj PCEP ERO Object
90 * @param attrList PCEP Attribute
91 * @param rroObj PCEP Rro Object
92 * @param bandwidth PCEP bandwidth object
94 public PcepMsgPath(PcepEroObject eroObj, PcepAttribute attrList, PcepRroObject rroObj,
95 PcepBandwidthObject bandwidth) {
98 this.attrList = attrList;
100 this.bandwidth = bandwidth;
101 if (rroObj == null) {
102 this.isRroObjectSet = false;
104 this.isRroObjectSet = true;
106 if (eroObj == null) {
107 this.isEroObjectSet = false;
109 this.isEroObjectSet = true;
111 if (attrList == null) {
112 this.isAttributeListSet = false;
114 this.isAttributeListSet = true;
116 if (bandwidth == null) {
117 this.isBandwidthObjectSet = false;
119 this.isBandwidthObjectSet = true;
124 * Returns PcepEroObject.
126 * @return eroObj PCEP ERO Object
129 public PcepEroObject getEroObject() {
134 * Returns PCEP Attribute.
136 * @return attrList Attribute list
139 public PcepAttribute getPcepAttribute() {
140 return this.attrList;
144 * Returns PcepRroObject.
146 * @return rroObj PCEP RRO Object
149 public PcepRroObject getRroObject() {
154 public PcepBandwidthObject getBandwidthObject() {
155 return this.bandwidth;
159 public void setEroObject(PcepEroObject eroObj) {
160 this.eroObj = eroObj;
164 public void setPcepAttribute(PcepAttribute attrList) {
165 this.attrList = attrList;
169 public void setRroObject(PcepRroObject rroObj) {
170 this.rroObj = rroObj;
174 public void setBandwidthObject(PcepBandwidthObject bandwidth) {
175 this.bandwidth = bandwidth;
179 * Reads all the Objects for PCEP Message Path.
181 * @param bb of type channel buffer
182 * @return PCEP Message path
183 * @throws PcepParseException when fails to read pcep message path
186 public PcepMsgPath read(ChannelBuffer bb) throws PcepParseException {
188 PcepEroObject eroObj;
189 PcepAttribute attrList;
190 PcepRroObject rroObj = null;
191 PcepBandwidthObject bandwidth = null;
193 eroObj = PcepEroObjectVer1.read(bb);
194 attrList = PcepAttributeVer1.read(bb);
196 boolean bBreakWhile = false;
197 while (0 < bb.readableBytes()) {
199 if (bb.readableBytes() < OBJECT_HEADER_LENGTH) {
202 bb.markReaderIndex();
203 PcepObjectHeader tempObjHeader = PcepObjectHeader.read(bb);
204 bb.resetReaderIndex();
205 byte yObjClass = tempObjHeader.getObjClass();
208 case PcepRroObjectVer1.RRO_OBJ_CLASS:
209 rroObj = PcepRroObjectVer1.read(bb);
211 case PcepInterLayerObjectVer1.INTER_LAYER_OBJ_CLASS:
212 bb.skipBytes(tempObjHeader.getObjLen());
214 case PcepBandwidthObjectVer1.BANDWIDTH_OBJ_CLASS:
215 bandwidth = PcepBandwidthObjectVer1.read(bb);
218 //Otherthan above objects handle those objects in caller.
226 return new PcepMsgPath(eroObj, attrList, rroObj, bandwidth);
230 * Writes all the objects for PCEP message path.
232 * @param bb of type channel buffer.
233 * @return object length index
234 * @throws PcepParseException when fails to write to channel buffer
237 public int write(ChannelBuffer bb) throws PcepParseException {
238 int iLenStartIndex = bb.writerIndex();
240 //write Object header
241 if (this.isEroObjectSet) {
242 this.eroObj.write(bb);
244 throw new PcepParseException("Ero object is not set in path");
247 if (this.isAttributeListSet) {
248 this.attrList.write(bb);
251 // RRO is optional check and read
252 if (this.isRroObjectSet) {
253 this.rroObj.write(bb);
254 // bandwidth should come along with RRO.
255 if (this.isBandwidthObjectSet) {
256 this.bandwidth.write(bb);
259 return bb.writerIndex() - iLenStartIndex;
263 public String toString() {
264 ToStringHelper toStrHelper = MoreObjects.toStringHelper(getClass());
266 if (attrList != null) {
267 toStrHelper.add("AttributeList", attrList);
269 if (rroObj instanceof PcepRroObjectVer1) {
270 toStrHelper.add("RroObject", rroObj);
272 if (bandwidth instanceof PcepBandwidthObjectVer1) {
273 toStrHelper.add("bandwidthObject", bandwidth);
275 return toStrHelper.toString();
280 private PcepSrpObject srpObject;
282 private PcepLspObject lspObject;
284 private PcepStateReport.PcepMsgPath msgPath;
287 * Constructor to reset objects.
289 public PcepStateReportVer1() {
290 this.srpObject = null;
291 this.lspObject = null;
295 public PcepStateReportVer1(PcepSrpObject srpObject, PcepLspObject lspObject, PcepStateReport.PcepMsgPath msgPath) {
296 this.srpObject = srpObject;
297 this.lspObject = lspObject;
298 this.msgPath = msgPath;
302 public PcepSrpObject getSrpObject() {
307 public PcepLspObject getLspObject() {
312 public PcepStateReport.PcepMsgPath getMsgPath() {
317 public void setSrpObject(PcepSrpObject srpObj) {
318 this.srpObject = srpObj;
322 public void setLspObject(PcepLspObject lspObject) {
323 this.lspObject = lspObject;
327 public void setMsgPath(PcepStateReport.PcepMsgPath msgPath) {
328 this.msgPath = msgPath;
332 * Builder class for PCEP state report.
334 public static class Builder implements PcepStateReport.Builder {
336 private boolean bIsSRPObjectSet = false;
337 private boolean bIsLSPObjectSet = false;
338 private boolean bIsPcepMsgPathSet = false;
341 private PcepSrpObject srpObject;
343 private PcepLspObject lspObject;
344 //PCEP Attribute list
345 private PcepStateReport.PcepMsgPath msgPath;
348 public PcepStateReport build() throws PcepParseException {
351 PcepSrpObject srpObject = null;
353 PcepLspObject lspObject = null;
354 //PCEP Attribute list
355 PcepStateReport.PcepMsgPath msgPath = null;
357 if (this.bIsSRPObjectSet) {
358 srpObject = this.srpObject;
361 if (!this.bIsLSPObjectSet) {
362 throw new PcepParseException(" LSP Object NOT Set while building PcepStateReport.");
364 lspObject = this.lspObject;
366 if (!this.bIsPcepMsgPathSet) {
367 throw new PcepParseException(" Message Path NOT Set while building PcepStateReport.");
369 msgPath = this.msgPath;
372 return new PcepStateReportVer1(srpObject, lspObject, msgPath);
376 public PcepSrpObject getSrpObject() {
377 return this.srpObject;
381 public PcepLspObject getLspObject() {
382 return this.lspObject;
386 public PcepStateReport.PcepMsgPath getMsgPath() {
391 public Builder setSrpObject(PcepSrpObject srpobj) {
392 this.srpObject = srpobj;
393 this.bIsSRPObjectSet = true;
398 public Builder setLspObject(PcepLspObject lspObject) {
399 this.lspObject = lspObject;
400 this.bIsLSPObjectSet = true;
405 public Builder setMsgPath(PcepStateReport.PcepMsgPath msgPath) {
406 this.msgPath = msgPath;
407 this.bIsPcepMsgPathSet = true;
413 public String toString() {
414 return MoreObjects.toStringHelper(getClass())
416 .add("SrpObject", srpObject)
417 .add("LspObject", lspObject)
418 .add("MsgPath", msgPath)