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.PcepLspObject;
25 import org.onosproject.pcepio.protocol.PcepMessageReader;
26 import org.onosproject.pcepio.protocol.PcepMessageWriter;
27 import org.onosproject.pcepio.protocol.PcepReportMsg;
28 import org.onosproject.pcepio.protocol.PcepSrpObject;
29 import org.onosproject.pcepio.protocol.PcepStateReport;
30 import org.onosproject.pcepio.protocol.PcepType;
31 import org.onosproject.pcepio.protocol.PcepVersion;
32 import org.onosproject.pcepio.types.PcepObjectHeader;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
36 import com.google.common.base.MoreObjects;
39 * Provides PCEP report message.
41 class PcepReportMsgVer1 implements PcepReportMsg {
46 * The format of the PCRpt message is as follows:
47 * <PCRpt Message> ::= <Common Header>
50 * <state-report-list> ::= <state-report>[<state-report-list>]
51 * <state-report> ::= [<SRP>]
55 * <path> ::= <ERO><attribute-list>[<RRO>]
57 * <attribute-list> is defined in [RFC5440] and extended by PCEP extensions.
59 * <attribute-list> ::=[<LSPA>]
63 * <metric-list> ::=<METRIC>[<metric-list>]
65 protected static final Logger log = LoggerFactory.getLogger(PcepReportMsgVer1.class);
67 public static final byte PACKET_VERSION = 1;
68 //PACKET_MINIMUM_LENGTH = CommonHeaderLen(4)+LspObjMinLen(8)+EroObjMinLen(12)
69 public static final int PACKET_MINIMUM_LENGTH = 24;
70 public static final PcepType MSG_TYPE = PcepType.REPORT;
71 public static final byte REPORT_OBJ_TYPE = 1;
73 private LinkedList<PcepStateReport> llStateReportList;
75 public static final PcepReportMsgVer1.Reader READER = new Reader();
78 * Reader class for reading PCEP report message from channel buffer.
80 static class Reader implements PcepMessageReader<PcepReportMsg> {
82 LinkedList<PcepStateReport> llStateReportList;
85 public PcepReportMsg readFrom(ChannelBuffer cb) throws PcepParseException {
87 if (cb.readableBytes() < PACKET_MINIMUM_LENGTH) {
88 throw new PcepParseException("Received packet size " + cb.readableBytes()
89 + " is less than the expected size: " + PACKET_MINIMUM_LENGTH);
91 llStateReportList = new LinkedList<>();
92 byte version = cb.readByte();
93 version = (byte) (version >> PcepMessageVer1.SHIFT_FLAG);
95 if (version != PACKET_VERSION) {
96 throw new PcepParseException(" Invalid version: " + version);
99 byte type = cb.readByte();
101 if (type != MSG_TYPE.getType()) {
102 throw new PcepParseException("Unexpected type: " + type);
105 short length = cb.readShort();
107 if (length < PACKET_MINIMUM_LENGTH) {
108 throw new PcepParseException("Wrong length. Expected to be >= " + PACKET_MINIMUM_LENGTH + ", was: "
111 // parse state report list
112 parseStateReportList(cb);
113 return new PcepReportMsgVer1(llStateReportList);
116 // Parse State Report list
117 public void parseStateReportList(ChannelBuffer cb) throws PcepParseException {
122 <state-report-list> ::= <state-report>[<state-report-list>]
123 <state-report> ::= [<SRP>]
127 <path> ::= <ERO><attribute-list>[<RRO>]
129 <attribute-list> is defined in [RFC5440] and extended by PCEP extensions.
133 while (0 < cb.readableBytes()) {
135 PcepStateReport pcestateReq = new PcepStateReportVer1();
139 * Check whether SRP Object is available, if yes store it.
140 * First read common object header and check the Object Class whether it is SRP or LSP
141 * If it is LSP then store only LSP. So, SRP is optional. then read path and store.
142 * If it is SRP then store SRP and then read LSP, path and store them.
145 //mark the reader index to reset
146 cb.markReaderIndex();
147 PcepObjectHeader tempObjHeader = PcepObjectHeader.read(cb);
149 byte yObjectClass = tempObjHeader.getObjClass();
150 byte yObjectType = tempObjHeader.getObjType();
153 cb.resetReaderIndex();
154 //If SRP present then store it.
155 if ((PcepSrpObjectVer1.SRP_OBJ_CLASS == yObjectClass)
156 && (PcepSrpObjectVer1.SRP_OBJ_TYPE == yObjectType)) {
157 PcepSrpObject srpObj;
158 srpObj = PcepSrpObjectVer1.read(cb);
159 pcestateReq.setSrpObject(srpObj);
163 PcepLspObject lspObj;
164 lspObj = PcepLspObjectVer1.read(cb);
165 pcestateReq.setLspObject(lspObj);
168 PcepStateReport.PcepMsgPath msgPath = new PcepStateReportVer1().new PcepMsgPath().read(cb);
169 pcestateReq.setMsgPath(msgPath);
171 llStateReportList.add(pcestateReq);
177 * Constructor to initialize State Report List.
179 * @param llStateReportList list of type Pcep state report
181 PcepReportMsgVer1(LinkedList<PcepStateReport> llStateReportList) {
182 this.llStateReportList = llStateReportList;
186 * Builder class for PCEP Report message.
188 static class Builder implements PcepReportMsg.Builder {
189 // Pcep report message fields
190 LinkedList<PcepStateReport> llStateReportList;
193 public PcepVersion getVersion() {
194 return PcepVersion.PCEP_1;
198 public PcepType getType() {
199 return PcepType.REPORT;
203 public PcepReportMsg build() {
204 return new PcepReportMsgVer1(this.llStateReportList);
208 public LinkedList<PcepStateReport> getStateReportList() {
209 return this.llStateReportList;
213 public Builder setStateReportList(LinkedList<PcepStateReport> ll) {
214 this.llStateReportList = ll;
220 public void writeTo(ChannelBuffer cb) throws PcepParseException {
221 WRITER.write(cb, this);
224 static final Writer WRITER = new Writer();
227 * Writer class for writing PCEP report message to channel buffer.
229 static class Writer implements PcepMessageWriter<PcepReportMsgVer1> {
232 public void write(ChannelBuffer cb, PcepReportMsgVer1 message) throws PcepParseException {
234 int startIndex = cb.writerIndex();
236 // first 3 bits set to version
237 cb.writeByte((byte) (PACKET_VERSION << PcepMessageVer1.SHIFT_FLAG));
240 cb.writeByte(MSG_TYPE.getType());
242 // length is length of variable message, will be updated at the end
243 // Store the position of message
245 int msgLenIndex = cb.writerIndex();
247 cb.writeShort((short) 0);
248 ListIterator<PcepStateReport> listIterator = message.llStateReportList.listIterator();
250 while (listIterator.hasNext()) {
252 PcepStateReport stateRpt = listIterator.next();
253 PcepSrpObject srpObj = stateRpt.getSrpObject();
255 //SRP object is optional
256 if (srpObj != null) {
260 //LSP object is mandatory
261 PcepLspObject lspObj = stateRpt.getLspObject();
262 if (lspObj == null) {
263 throw new PcepParseException("LSP Object is mandatory object for PcRpt message.");
269 PcepStateReport.PcepMsgPath msgPath = stateRpt.getMsgPath();
270 if (msgPath == null) {
271 throw new PcepParseException("Message path is mandatory object for PcRpt message.");
277 // update message length field
278 int length = cb.writerIndex() - startIndex;
279 cb.setShort(msgLenIndex, (short) length);
284 public PcepVersion getVersion() {
285 return PcepVersion.PCEP_1;
289 public PcepType getType() {
294 public LinkedList<PcepStateReport> getStateReportList() {
295 return this.llStateReportList;
299 public void setStateReportList(LinkedList<PcepStateReport> ll) {
300 this.llStateReportList = ll;
304 public String toString() {
305 return MoreObjects.toStringHelper(getClass())
306 .add("StateReportList", llStateReportList)