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.PcepMessageReader;
25 import org.onosproject.pcepio.protocol.PcepMessageWriter;
26 import org.onosproject.pcepio.protocol.PcepTEObject;
27 import org.onosproject.pcepio.protocol.PcepTEReportMsg;
28 import org.onosproject.pcepio.protocol.PcepType;
29 import org.onosproject.pcepio.protocol.PcepVersion;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
33 import com.google.common.base.MoreObjects;
36 * Provides PCEP TE Report Message.
38 class PcepTEReportMsgVer1 implements PcepTEReportMsg {
41 * Ref : draft-dhodylee-pce-pcep-te-data-extn-02, section 8.1
43 <TERpt Message> ::= <Common Header>
46 <te-report-list> ::= <TE>[<te-report-list>]
49 private static final Logger log = LoggerFactory.getLogger(PcepTEReportMsgVer1.class);
50 //PACKET_MINIMUM_LENGTH = CommonHeaderLen(4)+TEObjMinLen(12)
51 public static final int PACKET_MINIMUM_LENGTH = 16;
52 public static final PcepType MSG_TYPE = PcepType.TE_REPORT;
54 private LinkedList<PcepTEObject> teReportList;
56 public static final PcepTEReportMsgVer1.Reader READER = new Reader();
59 * Reader class for reading PCPE te report message form channel buffer.
61 static class Reader implements PcepMessageReader<PcepTEReportMsg> {
63 LinkedList<PcepTEObject> teReportList;
66 public PcepTEReportMsg readFrom(ChannelBuffer cb) throws PcepParseException {
68 if (cb.readableBytes() < PACKET_MINIMUM_LENGTH) {
72 teReportList = new LinkedList<>();
74 byte version = cb.readByte();
75 version = (byte) (version >> PcepMessageVer1.SHIFT_FLAG);
76 if (version != PcepMessageVer1.PACKET_VERSION) {
77 throw new PcepParseException("Wrong version. Expected=PcepVersion.PCEP_1(1), got=" + version);
80 byte type = cb.readByte();
81 if (type != MSG_TYPE.getType()) {
82 throw new PcepParseException("Wrong type. Expected=PcepType.TE_REPORT(14), got=" + type);
85 short length = cb.readShort();
86 if (length < PACKET_MINIMUM_LENGTH) {
87 throw new PcepParseException(
88 "Wrong length. Expected to be >= " + PACKET_MINIMUM_LENGTH + ", is: " + length);
91 // Parse state report list
92 parseTEReportList(cb);
94 return new PcepTEReportMsgVer1(teReportList);
98 * Parse te-report-list.
100 * @param cb input Channel Buffer
101 * @throws PcepParseException when fails to parse TE Report list.
103 public void parseTEReportList(ChannelBuffer cb) throws PcepParseException {
104 // <te-report-list> ::= <TE>[<te-report-list>]
106 while (0 < cb.readableBytes()) {
108 if (!teReportList.add(PcepTEObjectVer1.read(cb))) {
109 throw new PcepParseException("Failed to add TE object to TE report list");
116 * Constructor to initialize TE Report List.
118 * @param teReportList list of PCEP TE Object
120 PcepTEReportMsgVer1(LinkedList<PcepTEObject> teReportList) {
121 this.teReportList = teReportList;
125 * Builder class for PCEP te report message.
127 static class Builder implements PcepTEReportMsg.Builder {
128 // PCEP TE Report message fields
129 LinkedList<PcepTEObject> teReportList;
132 public PcepVersion getVersion() {
133 return PcepVersion.PCEP_1;
137 public PcepType getType() {
138 return PcepType.TE_REPORT;
142 public PcepTEReportMsg build() {
143 return new PcepTEReportMsgVer1(this.teReportList);
147 public LinkedList<PcepTEObject> getTEReportList() {
148 return this.teReportList;
152 public Builder setTEReportList(LinkedList<PcepTEObject> ll) {
153 this.teReportList = ll;
159 public void writeTo(ChannelBuffer bb) throws PcepParseException {
160 WRITER.write(bb, this);
163 static final Writer WRITER = new Writer();
166 * Writer class for writing PCEP te report message to channel buffer.
168 static class Writer implements PcepMessageWriter<PcepTEReportMsgVer1> {
171 public void write(ChannelBuffer bb, PcepTEReportMsgVer1 message) throws PcepParseException {
173 int startIndex = bb.writerIndex();
175 // first 3 bits set to version
176 bb.writeByte((byte) (PcepMessageVer1.PACKET_VERSION << PcepMessageVer1.SHIFT_FLAG));
179 bb.writeByte(MSG_TYPE.getType());
181 // Length of the message will be updated at the end
182 // First write with 0s
183 int msgLenIndex = bb.writerIndex();
184 bb.writeShort((short) 0);
186 ListIterator<PcepTEObject> listIterator = message.teReportList.listIterator();
188 while (listIterator.hasNext()) {
189 PcepTEObject teObj = listIterator.next();
193 // update message length field
194 int length = bb.writerIndex() - startIndex;
195 bb.setShort(msgLenIndex, (short) length);
200 public PcepVersion getVersion() {
201 return PcepVersion.PCEP_1;
205 public PcepType getType() {
210 public LinkedList<PcepTEObject> getTEReportList() {
211 return this.teReportList;
215 public void setTEReportList(LinkedList<PcepTEObject> ll) {
216 this.teReportList = ll;
220 public String toString() {
221 return MoreObjects.toStringHelper(getClass())
222 .add("TeReportList", teReportList)