Bharat saraswal
Committed by Thomas Vachuska

[ONOS-2359]Implementation of PCEP LabelReserve message

Change-Id: Ie74340f5fe5e93d9bb277dd3551ba175ac74494c
1 +/*
2 + * Copyright 2015 Open Networking Laboratory
3 + *
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
7 + *
8 + * http://www.apache.org/licenses/LICENSE-2.0
9 + *
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.
15 + */
16 +
17 +package org.onosproject.pcepio.protocol.ver1;
18 +
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;
29 +
30 +import com.google.common.base.MoreObjects;
31 +
32 +class PcepLabelRangeResvMsgVer1 implements PcepLabelRangeResvMsg {
33 +
34 + // Pcep version: 1
35 +
36 + /*
37 + The format of a PCLRResv message is as follows:
38 +
39 + PCLRResv Message>::= <Common Header>
40 + <label-range>
41 + Where:
42 +
43 + <label-range> ::= <SRP>
44 + <labelrange-list>
45 +
46 + Where
47 + <labelrange-list>::=<LABEL-RANGE>[<labelrange-list>]
48 + */
49 + protected static final Logger log = LoggerFactory.getLogger(PcepLabelRangeResvMsgVer1.class);
50 +
51 + public static final byte PACKET_VERSION = 1;
52 + // LabelRangeResvMsgMinLength = COMMON-HEADER(4)+SrpObjMinLentgh(12)+LABEL-RANGE-MIN-LENGTH(12)
53 + public static final int PACKET_MINIMUM_LENGTH = 28;
54 + public static final PcepType MSG_TYPE = PcepType.LABEL_RANGE_RESERV;
55 + //<label-range>
56 + PcepLabelRange labelRange;
57 +
58 + public static final PcepLabelRangeResvMsgVer1.Reader READER = new Reader();
59 +
60 + //Reader reads LabelRangeResv Message from the channel.
61 + static class Reader implements PcepMessageReader<PcepLabelRangeResvMsg> {
62 +
63 + @Override
64 + public PcepLabelRangeResvMsg readFrom(ChannelBuffer cb) throws PcepParseException {
65 +
66 + if (cb.readableBytes() < PACKET_MINIMUM_LENGTH) {
67 + throw new PcepParseException("Channel buffer has less readable bytes than Packet minimum length.");
68 + }
69 + // fixed value property version == 1
70 + byte version = cb.readByte();
71 + version = (byte) (version >> PcepMessageVer1.SHIFT_FLAG);
72 + if (version != PACKET_VERSION) {
73 + throw new PcepParseException("Wrong version. Expected=PcepVersion.PCEP_1(1), got=" + version);
74 + }
75 + // fixed value property type == 15
76 + byte type = cb.readByte();
77 + if (type != MSG_TYPE.getType()) {
78 + throw new PcepParseException("Wrong type. Expected=PcepType.LABEL_RANGE_RESERV(15), got=" + type);
79 + }
80 + short length = cb.readShort();
81 + if (length < PACKET_MINIMUM_LENGTH) {
82 + throw new PcepParseException("Wrong length.Expected to be >= " + PACKET_MINIMUM_LENGTH + ", is: "
83 + + length);
84 + }
85 + // parse <label-range>
86 + PcepLabelRange labelRange = PcepLabelRangeVer1.read(cb);
87 + return new PcepLabelRangeResvMsgVer1(labelRange);
88 + }
89 + }
90 +
91 + /**
92 + * Constructor to initialize PCEP label range.
93 + *
94 + * @param labelRange PCEP label range
95 + */
96 + PcepLabelRangeResvMsgVer1(PcepLabelRange labelRange) {
97 + this.labelRange = labelRange;
98 + }
99 +
100 + /**
101 + * Builder class for PCEP label range reserve message.
102 + */
103 + static class Builder implements PcepLabelRangeResvMsg.Builder {
104 +
105 + PcepLabelRange labelRange = new PcepLabelRangeVer1();
106 +
107 + @Override
108 + public PcepVersion getVersion() {
109 + return PcepVersion.PCEP_1;
110 + }
111 +
112 + @Override
113 + public PcepType getType() {
114 + return PcepType.LABEL_RANGE_RESERV;
115 + }
116 +
117 + @Override
118 + public PcepLabelRangeResvMsg build() {
119 + return new PcepLabelRangeResvMsgVer1(this.labelRange);
120 + }
121 +
122 + @Override
123 + public PcepLabelRange getLabelRange() {
124 + return this.labelRange;
125 + }
126 +
127 + @Override
128 + public Builder setLabelRange(PcepLabelRange labelRange) {
129 + this.labelRange = labelRange;
130 + return this;
131 + }
132 + }
133 +
134 + @Override
135 + public void writeTo(ChannelBuffer cb) throws PcepParseException {
136 + WRITER.write(cb, this);
137 + }
138 +
139 + static final Writer WRITER = new Writer();
140 +
141 + //Writer writes LabelRangeResv Message to the channel.
142 + static class Writer implements PcepMessageWriter<PcepLabelRangeResvMsgVer1> {
143 +
144 + @Override
145 + public void write(ChannelBuffer cb, PcepLabelRangeResvMsgVer1 message) throws PcepParseException {
146 +
147 + int startIndex = cb.writerIndex();
148 + // first 3 bits set to version
149 + cb.writeByte((byte) (PACKET_VERSION << PcepMessageVer1.SHIFT_FLAG));
150 + // message type
151 + cb.writeByte(MSG_TYPE.getType());
152 + // Length will be set after calculating length, but currently set it as 0.
153 + int msgLenIndex = cb.writerIndex();
154 +
155 + cb.writeShort((short) 0);
156 + //write Label Range
157 + message.labelRange.write(cb);
158 +
159 + // update message length field
160 + int length = cb.writerIndex() - startIndex;
161 + cb.setShort(msgLenIndex, (short) length);
162 + }
163 + }
164 +
165 + @Override
166 + public PcepVersion getVersion() {
167 + return PcepVersion.PCEP_1;
168 + }
169 +
170 + @Override
171 + public PcepType getType() {
172 + return MSG_TYPE;
173 + }
174 +
175 + @Override
176 + public PcepLabelRange getLabelRange() {
177 + return this.labelRange;
178 + }
179 +
180 + @Override
181 + public void setLabelRange(PcepLabelRange lr) {
182 + this.labelRange = lr;
183 + }
184 +
185 + @Override
186 + public String toString() {
187 + return MoreObjects.toStringHelper(getClass()).add("labelRange", labelRange).toString();
188 + }
189 +}
1 +/*
2 + * Copyright 2015 Open Networking Laboratory
3 + *
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
7 + *
8 + * http://www.apache.org/licenses/LICENSE-2.0
9 + *
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.
15 + */
16 +
17 +package org.onosproject.pcepio.protocol.ver1;
18 +
19 +import java.util.LinkedList;
20 +import java.util.ListIterator;
21 +
22 +import org.jboss.netty.buffer.ChannelBuffer;
23 +import org.onosproject.pcepio.exceptions.PcepParseException;
24 +import org.onosproject.pcepio.protocol.PcepLabelRange;
25 +import org.onosproject.pcepio.protocol.PcepLabelRangeObject;
26 +import org.onosproject.pcepio.protocol.PcepSrpObject;
27 +import org.slf4j.Logger;
28 +import org.slf4j.LoggerFactory;
29 +
30 +import com.google.common.base.MoreObjects;
31 +
32 +/**
33 + * Provides PCEP Label Range.
34 + */
35 +public class PcepLabelRangeVer1 implements PcepLabelRange {
36 +
37 + protected static final Logger log = LoggerFactory.getLogger(PcepLabelRangeVer1.class);
38 +
39 + /*
40 + <label-range> ::= <SRP>
41 + <labelrange-list>
42 + Where
43 + <labelrange-list>::=<LABEL-RANGE>[<labelrange-list>]
44 + */
45 +
46 + // PCEP SRP Object
47 + private PcepSrpObject srpObject;
48 + //<labelrange-list> of type PcepLabelRangeObject.
49 + private LinkedList<PcepLabelRangeObject> llLabelRangeList;
50 +
51 + /**
52 + * Default Constructor.
53 + */
54 + public PcepLabelRangeVer1() {
55 + srpObject = null;
56 + llLabelRangeList = null;
57 + }
58 +
59 + /**
60 + * Constructor to initialize objects.
61 + *
62 + * @param srpObj PCEP Srp object.
63 + * @param llLabelRangeList list of PcepLabelRangeObject.
64 + */
65 + PcepLabelRangeVer1(PcepSrpObject srpObj, LinkedList<PcepLabelRangeObject> llLabelRangeList) {
66 + this.srpObject = srpObj;
67 + this.llLabelRangeList = llLabelRangeList;
68 + }
69 +
70 + @Override
71 + public PcepSrpObject getSrpObject() {
72 + return srpObject;
73 + }
74 +
75 + @Override
76 + public void setSrpObject(PcepSrpObject srpObject) {
77 + this.srpObject = srpObject;
78 +
79 + }
80 +
81 + @Override
82 + public LinkedList<PcepLabelRangeObject> getLabelRangeList() {
83 + return llLabelRangeList;
84 + }
85 +
86 + @Override
87 + public void setLabelRangeList(LinkedList<PcepLabelRangeObject> ll) {
88 + this.llLabelRangeList = ll;
89 + }
90 +
91 + /**
92 + * Reads channel buffer and returns object of PcepLabelRange.
93 + *
94 + * @param cb of type channel buffer.
95 + * @return object of PcepLabelRange
96 + * @throws PcepParseException when fails to read from channel buffer
97 + */
98 + public static PcepLabelRange read(ChannelBuffer cb) throws PcepParseException {
99 +
100 + //parse and store SRP mandatory object
101 + PcepSrpObject srpObj = null;
102 + srpObj = PcepSrpObjectVer1.read(cb);
103 + if (srpObj == null) {
104 + throw new PcepParseException("Exception while parsing srp object");
105 + }
106 +
107 + LinkedList<PcepLabelRangeObject> llLabelRangeList = new LinkedList<PcepLabelRangeObject>();
108 + boolean bFoundLabelRangeObj = false;
109 + while (0 < cb.readableBytes()) {
110 + //parse and store <labelrange-list>
111 + PcepLabelRangeObject lrObj;
112 + lrObj = PcepLabelRangeObjectVer1.read(cb);
113 + if (lrObj == null) {
114 + throw new PcepParseException("Exception while parsing label range object");
115 + } else {
116 + llLabelRangeList.add(lrObj);
117 + bFoundLabelRangeObj = true;
118 + }
119 + }
120 +
121 + if (!bFoundLabelRangeObj) {
122 + throw new PcepParseException("At least one LABEL-RANGE MUST be present.");
123 + }
124 + return new PcepLabelRangeVer1(srpObj, llLabelRangeList);
125 + }
126 +
127 + @Override
128 + public int write(ChannelBuffer cb) throws PcepParseException {
129 + //write Object header
130 + int objStartIndex = cb.writerIndex();
131 +
132 + //write <SRP>
133 + int objLenIndex = srpObject.write(cb);
134 +
135 + if (objLenIndex <= 0) {
136 + throw new PcepParseException("bjectLength is " + objLenIndex);
137 + }
138 +
139 + //write <labelrange-list>
140 + ListIterator<PcepLabelRangeObject> listIterator = llLabelRangeList.listIterator();
141 + while (listIterator.hasNext()) {
142 + listIterator.next().write(cb);
143 + }
144 +
145 + //Update object length now
146 + int length = cb.writerIndex() - objStartIndex;
147 + // As per RFC the length of object should be
148 + // multiples of 4
149 + int pad = length % 4;
150 + if (pad != 0) {
151 + pad = 4 - pad;
152 + for (int i = 0; i < pad; i++) {
153 + cb.writeByte((byte) 0);
154 + }
155 + length = length + pad;
156 + }
157 + cb.setShort(objLenIndex, (short) length);
158 + return length;
159 + }
160 +
161 + @Override
162 + public String toString() {
163 + return MoreObjects.toStringHelper(getClass()).add("srpObject", srpObject)
164 + .add("LabelRangeList", llLabelRangeList).toString();
165 + }
166 +}
1 +package org.onosproject.pcepio.types;
2 +
3 +import java.util.Objects;
4 +
5 +import org.jboss.netty.buffer.ChannelBuffer;
6 +import org.onosproject.pcepio.protocol.PcepVersion;
7 +import org.slf4j.Logger;
8 +import org.slf4j.LoggerFactory;
9 +
10 +import com.google.common.base.MoreObjects;
11 +
12 +/**
13 + * Provides PcepSetup type tlv.
14 + */
15 +public class PathSetupTypeTlv implements PcepValueType {
16 +
17 + /*
18 + Reference : draft-sivabalan-pce-lsp-setup-type-02.
19 +
20 + 0 1 2 3
21 + 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
22 + +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
23 + | Type | Length |
24 + +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
25 + | Reserved | PST |
26 + +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
27 +
28 + Figure 1: PATH-SETUP-TYPE TLV
29 +
30 + */
31 + protected static final Logger log = LoggerFactory.getLogger(PathSetupTypeTlv.class);
32 +
33 + public static final short TYPE = 0; //TODO : need to reassign the value as per RFC
34 + public static final short LENGTH = 4;
35 +
36 + private final byte pst;
37 + private final int rawValue;
38 + private final boolean isRawValueSet;
39 +
40 + /**
41 + * Constructor to initialize parameters for path setup type tlv.
42 + *
43 + * @param rawValue parameter for path setup type tlv
44 + */
45 + public PathSetupTypeTlv(final int rawValue) {
46 + this.rawValue = rawValue;
47 + this.isRawValueSet = true;
48 + this.pst = (byte) rawValue;
49 + }
50 +
51 + /**
52 + * Constructor to initialize pst.
53 + *
54 + * @param pst PST
55 + */
56 + public PathSetupTypeTlv(byte pst) {
57 + this.pst = pst;
58 + this.rawValue = 0;
59 + this.isRawValueSet = false;
60 + }
61 +
62 + /**
63 + * Returns Object of path setup type tlv.
64 + *
65 + * @param raw parameter for path setup type tlv
66 + * @return object of PathSetupTypeTlv
67 + */
68 + public static PathSetupTypeTlv of(final int raw) {
69 + return new PathSetupTypeTlv(raw);
70 + }
71 +
72 + @Override
73 + public PcepVersion getVersion() {
74 + return PcepVersion.PCEP_1;
75 + }
76 +
77 + /**
78 + * Returns parameters for path setup type tlv.
79 + *
80 + * @return parameters for path setup type tlv
81 + */
82 + public int getInt() {
83 + return rawValue;
84 + }
85 +
86 + /**
87 + * Returns the pst value.
88 + *
89 + * @return pst value
90 + */
91 + public byte getPst() {
92 + return pst;
93 + }
94 +
95 + @Override
96 + public short getType() {
97 + return TYPE;
98 + }
99 +
100 + @Override
101 + public short getLength() {
102 + return LENGTH;
103 + }
104 +
105 + @Override
106 + public int hashCode() {
107 + return Objects.hash(pst);
108 + }
109 +
110 + @Override
111 + public boolean equals(Object obj) {
112 + if (this == obj) {
113 + return true;
114 + }
115 + if (obj instanceof PathSetupTypeTlv) {
116 + PathSetupTypeTlv other = (PathSetupTypeTlv) obj;
117 + return Objects.equals(this.pst, other.pst);
118 + }
119 + return false;
120 + }
121 +
122 + @Override
123 + public int write(ChannelBuffer c) {
124 + int iLenStartIndex = c.writerIndex();
125 + c.writeShort(TYPE);
126 + c.writeShort(LENGTH);
127 + c.writeInt(pst);
128 + return c.writerIndex() - iLenStartIndex;
129 + }
130 +
131 + /**
132 + * Returns the object of type PathSetupTypeTlv.
133 + *
134 + * @param c is type Channel buffer
135 + * @return object of PathSetupTypeTlv
136 + */
137 + public static PathSetupTypeTlv read(ChannelBuffer c) {
138 + return PathSetupTypeTlv.of(c.readInt());
139 + }
140 +
141 + @Override
142 + public String toString() {
143 + return MoreObjects.toStringHelper(getClass()).add("Type", TYPE).add("Length", LENGTH).add("PST", pst)
144 + .toString();
145 + }
146 +}