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.PcepLabelRange;
22 import org.onosproject.pcepio.protocol.PcepLabelRangeResvMsg;
23 import org.onosproject.pcepio.protocol.PcepMessageReader;
24 import org.onosproject.pcepio.protocol.PcepMessageWriter;
25 import org.onosproject.pcepio.protocol.PcepType;
26 import org.onosproject.pcepio.protocol.PcepVersion;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
30 import com.google.common.base.MoreObjects;
33 * Provides PCEP label range reserve message.
35 class PcepLabelRangeResvMsgVer1 implements PcepLabelRangeResvMsg {
40 The format of a PCLRResv message is as follows:
42 PCLRResv Message>::= <Common Header>
46 <label-range> ::= <SRP>
50 <labelrange-list>::=<LABEL-RANGE>[<labelrange-list>]
52 protected static final Logger log = LoggerFactory.getLogger(PcepLabelRangeResvMsgVer1.class);
54 public static final byte PACKET_VERSION = 1;
55 // LabelRangeResvMsgMinLength = COMMON-HEADER(4)+SrpObjMinLentgh(12)+LABEL-RANGE-MIN-LENGTH(12)
56 public static final int PACKET_MINIMUM_LENGTH = 28;
57 public static final PcepType MSG_TYPE = PcepType.LABEL_RANGE_RESERV;
59 PcepLabelRange labelRange;
61 public static final PcepLabelRangeResvMsgVer1.Reader READER = new Reader();
64 * Reader reads LabelRangeResv Message from the channel.
66 static class Reader implements PcepMessageReader<PcepLabelRangeResvMsg> {
69 public PcepLabelRangeResvMsg readFrom(ChannelBuffer cb) throws PcepParseException {
71 if (cb.readableBytes() < PACKET_MINIMUM_LENGTH) {
72 throw new PcepParseException("Channel buffer has less readable bytes than Packet minimum length.");
74 // fixed value property version == 1
75 byte version = cb.readByte();
76 version = (byte) (version >> PcepMessageVer1.SHIFT_FLAG);
77 if (version != PACKET_VERSION) {
78 throw new PcepParseException("Wrong version. Expected=PcepVersion.PCEP_1(1), got=" + version);
80 // fixed value property type == 15
81 byte type = cb.readByte();
82 if (type != MSG_TYPE.getType()) {
83 throw new PcepParseException("Wrong type. Expected=PcepType.LABEL_RANGE_RESERV(15), got=" + type);
85 short length = cb.readShort();
86 if (length < PACKET_MINIMUM_LENGTH) {
87 throw new PcepParseException("Wrong length.Expected to be >= " + PACKET_MINIMUM_LENGTH + ", is: "
90 // parse <label-range>
91 PcepLabelRange labelRange = PcepLabelRangeVer1.read(cb);
92 return new PcepLabelRangeResvMsgVer1(labelRange);
97 * Constructor to initialize PCEP label range.
99 * @param labelRange PCEP label range
101 PcepLabelRangeResvMsgVer1(PcepLabelRange labelRange) {
102 this.labelRange = labelRange;
106 * Builder class for PCEP label range reserve message.
108 static class Builder implements PcepLabelRangeResvMsg.Builder {
110 PcepLabelRange labelRange = new PcepLabelRangeVer1();
113 public PcepVersion getVersion() {
114 return PcepVersion.PCEP_1;
118 public PcepType getType() {
119 return PcepType.LABEL_RANGE_RESERV;
123 public PcepLabelRangeResvMsg build() {
124 return new PcepLabelRangeResvMsgVer1(this.labelRange);
128 public PcepLabelRange getLabelRange() {
129 return this.labelRange;
133 public Builder setLabelRange(PcepLabelRange labelRange) {
134 this.labelRange = labelRange;
140 public void writeTo(ChannelBuffer cb) throws PcepParseException {
141 WRITER.write(cb, this);
144 static final Writer WRITER = new Writer();
147 * Writer writes LabelRangeResv Message to the channel.
149 static class Writer implements PcepMessageWriter<PcepLabelRangeResvMsgVer1> {
152 public void write(ChannelBuffer cb, PcepLabelRangeResvMsgVer1 message) throws PcepParseException {
154 int startIndex = cb.writerIndex();
155 // first 3 bits set to version
156 cb.writeByte((byte) (PACKET_VERSION << PcepMessageVer1.SHIFT_FLAG));
158 cb.writeByte(MSG_TYPE.getType());
159 // Length will be set after calculating length, but currently set it as 0.
160 int msgLenIndex = cb.writerIndex();
162 cb.writeShort((short) 0);
164 message.labelRange.write(cb);
166 // update message length field
167 int length = cb.writerIndex() - startIndex;
168 cb.setShort(msgLenIndex, (short) length);
173 public PcepVersion getVersion() {
174 return PcepVersion.PCEP_1;
178 public PcepType getType() {
183 public PcepLabelRange getLabelRange() {
184 return this.labelRange;
188 public void setLabelRange(PcepLabelRange lr) {
189 this.labelRange = lr;
193 public String toString() {
194 return MoreObjects.toStringHelper(getClass())
195 .add("labelRange", labelRange)