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.PcepLabelUpdate;
25 import org.onosproject.pcepio.protocol.PcepLabelUpdateMsg;
26 import org.onosproject.pcepio.protocol.PcepMessageReader;
27 import org.onosproject.pcepio.protocol.PcepMessageWriter;
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 lable update message.
38 class PcepLabelUpdateMsgVer1 implements PcepLabelUpdateMsg {
43 The format of the PCLabelUpd message:
45 <PCLabelUpd Message> ::= <Common Header>
46 <pce-label-update-list>
49 <pce-label-update-list> ::= <pce-label-update>
50 [<pce-label-update-list>]
51 <pce-label-update> ::= (<pce-label-download>|<pce-label-map>)
54 <pce-label-download> ::= <SRP>
58 <pce-label-map> ::= <SRP>
62 <label-list > ::= <LABEL>
66 protected static final Logger log = LoggerFactory.getLogger(PcepLabelUpdateMsgVer1.class);
68 public static final byte PACKET_VERSION = 1;
70 //LabelUpdateMsgMinLength = COMMON-HEADER(4)+SrpObjMinLentgh(12)+LabelObjectMinLength(12)+FECType1Object(8)
71 public static final int PACKET_MINIMUM_LENGTH = 36;
72 public static final PcepType MSG_TYPE = PcepType.LABEL_UPDATE;
73 //pce-label-update-list
74 private LinkedList<PcepLabelUpdate> llPcLabelUpdateList;
76 static final PcepLabelUpdateMsgVer1.Reader READER = new Reader();
79 * Reader reads LabelUpdate Message from the channel.
81 static class Reader implements PcepMessageReader<PcepLabelUpdateMsg> {
84 public PcepLabelUpdateMsg readFrom(ChannelBuffer cb) throws PcepParseException {
86 if (cb.readableBytes() < PACKET_MINIMUM_LENGTH) {
87 throw new PcepParseException("Readable bytes are less than Packet minimum length.");
90 // fixed value property version == 1
91 byte version = cb.readByte();
92 version = (byte) (version >> PcepMessageVer1.SHIFT_FLAG);
93 if (version != PACKET_VERSION) {
94 throw new PcepParseException("Wrong version.Expected=PcepVersion.PCEP_1(1), got=" + version);
96 // fixed value property type == 13
97 byte type = cb.readByte();
98 if (type != MSG_TYPE.getType()) {
99 throw new PcepParseException("Wrong type. Expected=PcepType.LABEL_UPDATE(13), got=" + type);
101 short length = cb.readShort();
102 if (length < PACKET_MINIMUM_LENGTH) {
103 throw new PcepParseException("Wrong length. Expected to be >= " + PACKET_MINIMUM_LENGTH + ", is: "
106 // parse <pce-label-download> / <pce-label-map>
107 LinkedList<PcepLabelUpdate> llPcLabelUpdateList = parsePcLabelUpdateList(cb);
108 return new PcepLabelUpdateMsgVer1(llPcLabelUpdateList);
112 * Returns list of PCEP Label Update object.
114 * @param cb of type channel buffer
115 * @return llPcLabelUpdateList list of PCEP label update object
116 * @throws PcepParseException when fails to parse list of PCEP label update object
118 public LinkedList<PcepLabelUpdate> parsePcLabelUpdateList(ChannelBuffer cb) throws PcepParseException {
120 LinkedList<PcepLabelUpdate> llPcLabelUpdateList;
121 llPcLabelUpdateList = new LinkedList<>();
123 while (0 < cb.readableBytes()) {
124 llPcLabelUpdateList.add(PcepLabelUpdateVer1.read(cb));
126 return llPcLabelUpdateList;
131 * Constructor to initialize PCEP Label Update List.
133 * @param llPcLabelUpdateList list of PCEP Label Update object
135 PcepLabelUpdateMsgVer1(LinkedList<PcepLabelUpdate> llPcLabelUpdateList) {
136 this.llPcLabelUpdateList = llPcLabelUpdateList;
140 * Builder class for PCEP label update message.
142 static class Builder implements PcepLabelUpdateMsg.Builder {
144 LinkedList<PcepLabelUpdate> llPcLabelUpdateList;
147 public PcepVersion getVersion() {
148 return PcepVersion.PCEP_1;
152 public PcepType getType() {
153 return PcepType.LABEL_UPDATE;
157 public PcepLabelUpdateMsg build() {
158 return new PcepLabelUpdateMsgVer1(this.llPcLabelUpdateList);
162 public LinkedList<PcepLabelUpdate> getPcLabelUpdateList() {
163 return this.llPcLabelUpdateList;
167 public Builder setPcLabelUpdateList(LinkedList<PcepLabelUpdate> ll) {
168 this.llPcLabelUpdateList = ll;
174 public void writeTo(ChannelBuffer cb) throws PcepParseException {
175 WRITER.write(cb, this);
178 static final Writer WRITER = new Writer();
181 * Writer writes LabelUpdate Message to the channel.
183 static class Writer implements PcepMessageWriter<PcepLabelUpdateMsgVer1> {
186 public void write(ChannelBuffer cb, PcepLabelUpdateMsgVer1 message) throws PcepParseException {
188 int startIndex = cb.writerIndex();
190 // first 3 bits set to version
191 cb.writeByte((byte) (PACKET_VERSION << PcepMessageVer1.SHIFT_FLAG));
194 cb.writeByte(MSG_TYPE.getType());
196 // Length will be set after calculating length, but currently set it as 0.
197 int msgLenIndex = cb.writerIndex();
199 cb.writeShort((short) 0);
200 ListIterator<PcepLabelUpdate> listIterator = message.llPcLabelUpdateList.listIterator();
202 while (listIterator.hasNext()) {
203 PcepLabelUpdate labelUpdate = listIterator.next();
204 labelUpdate.write(cb);
207 // update message length field
208 int length = cb.writerIndex() - startIndex;
209 cb.setShort(msgLenIndex, (short) length);
214 public PcepVersion getVersion() {
215 return PcepVersion.PCEP_1;
219 public PcepType getType() {
224 public LinkedList<PcepLabelUpdate> getPcLabelUpdateList() {
225 return this.llPcLabelUpdateList;
229 public void setPcLabelUpdateList(LinkedList<PcepLabelUpdate> ll) {
230 this.llPcLabelUpdateList = ll;
234 public String toString() {
235 return MoreObjects.toStringHelper(getClass())
236 .add("PcLabelUpdateList", llPcLabelUpdateList)