blob: c571d254ebfb8d0de48759c5d0148db638c50fc5 [file] [log] [blame]
bharat saraswalf7364db2015-08-11 13:39:31 +05301/*
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
17package org.onosproject.pcepio.protocol.ver1;
18
19import java.util.LinkedList;
20import java.util.ListIterator;
21
22import org.jboss.netty.buffer.ChannelBuffer;
23import org.onosproject.pcepio.exceptions.PcepParseException;
24import org.onosproject.pcepio.protocol.PcInitiatedLspRequest;
25import org.onosproject.pcepio.protocol.PcepAttribute;
26import org.onosproject.pcepio.protocol.PcepEndPointsObject;
27import org.onosproject.pcepio.protocol.PcepEroObject;
28import org.onosproject.pcepio.protocol.PcepInitiateMsg;
29import org.onosproject.pcepio.protocol.PcepLspObject;
30import org.onosproject.pcepio.protocol.PcepMessageReader;
31import org.onosproject.pcepio.protocol.PcepMessageWriter;
32import org.onosproject.pcepio.protocol.PcepSrpObject;
33import org.onosproject.pcepio.protocol.PcepType;
34import org.onosproject.pcepio.protocol.PcepVersion;
35import org.slf4j.Logger;
36import org.slf4j.LoggerFactory;
37
38import com.google.common.base.MoreObjects;
39
40class PcepInitiateMsgVer1 implements PcepInitiateMsg {
41
42 protected static final Logger log = LoggerFactory.getLogger(PcepInitiateMsgVer1.class);
43
44 // Ref : PCE initiated tunnel setup draft-ietf-pce-pce-initiated-lsp-03, section 5.1
45 /* <PCInitiate Message> ::= <Common Header>
46 * <PCE-initiated-lsp-list>
47 * Where:
48 * <PCE-initiated-lsp-list> ::= <PCE-initiated-lsp-request>[<PCE-initiated-lsp-list>]
49 * <PCE-initiated-lsp-request> ::= (<PCE-initiated-lsp-instantiation>|<PCE-initiated-lsp-deletion>)
50 * <PCE-initiated-lsp-instantiation> ::= <SRP>
51 * <LSP>
52 * <END-POINTS>
53 * <ERO>
54 * [<attribute-list>]
55 * <PCE-initiated-lsp-deletion> ::= <SRP>
56 * <LSP>
57 */
58
59 static final byte PACKET_VERSION = 1;
60 /* considering LspDelete Request PcInitiate msg will contain
61 * common header
62 * srp object
63 * lsp object
64 * so min length for this can be
65 * PACKET_MINIMUM_LENGTH = CommonHeaderLen(4)+SrpObjectMinLen(12)+LspObjectMinLen(8)
66 */
67 public static final short PACKET_MINIMUM_LENGTH = 24;
68 public static final short MINIMUM_COMMON_HEADER_LENGTH = 4;
69 public static final PcepType MSG_TYPE = PcepType.INITIATE;
70 private LinkedList<PcInitiatedLspRequest> llPcInitiatedLspRequestList;
71 public static final PcepInitiateMsgVer1.Reader READER = new Reader();
72
73 static class Reader implements PcepMessageReader<PcepInitiateMsg> {
74
75 LinkedList<PcInitiatedLspRequest> llPcInitiatedLspRequestList;
76
77 @Override
78 public PcepInitiateMsg readFrom(ChannelBuffer cb) throws PcepParseException {
79
80 if (cb.readableBytes() < PACKET_MINIMUM_LENGTH) {
81 return null;
82 }
83
84 llPcInitiatedLspRequestList = new LinkedList<PcInitiatedLspRequest>();
85
86 byte version = cb.readByte();
87 version = (byte) (version >> PcepMessageVer1.SHIFT_FLAG);
88 if (version != PACKET_VERSION) {
89 throw new PcepParseException("Wrong version. Expected=PcepVersion.PCEP_1(1), received=" + version);
90 }
91 byte type = cb.readByte();
92 if (type != MSG_TYPE.getType()) {
93 throw new PcepParseException("Wrong type. Expected=PcepType.INITIATE(12), recived=" + type);
94 }
95 short length = cb.readShort();
96
97 if (length < PACKET_MINIMUM_LENGTH) {
98 throw new PcepParseException("Wrong length. Initiate message length expected to be >= "
99 + PACKET_MINIMUM_LENGTH + ", but received=" + length);
100 }
101
102 log.debug("reading PcInitiate message of length " + length);
103
104 // parse Start initiate/deletion list
105 if (!parsePcInitiatedLspRequestList(cb)) {
106 throw new PcepParseException("Parsing PCE-initiated-lsp-Request-list failed");
107 }
108
109 return new PcepInitiateMsgVer1(llPcInitiatedLspRequestList);
110 }
111
112 /**
113 * To parse PcInitiatedLspRequestList from PcInitiate Message.
114 *
115 * @param cb of type channel buffer
116 * @return true if parsing PcInitiatedLspRequestList is success, false otherwise
117 * @throws PcepParseException while parsing from channel buffer
118 */
119 public boolean parsePcInitiatedLspRequestList(ChannelBuffer cb) throws PcepParseException {
120
121 boolean isDelLspRequest = false;
122
123 if (null == cb) {
124 throw new PcepParseException("Channel buffer is empty");
125 }
126
127 while (0 < cb.readableBytes()) {
128 PcInitiatedLspRequest pceInitLspReq = new PcInitiatedLspRequestVer1();
129
130 //store SRP object
131 PcepSrpObject srpObj;
132 srpObj = PcepSrpObjectVer1.read(cb);
133 pceInitLspReq.setSrpObject(srpObj);
134 isDelLspRequest = srpObj.getRFlag();
135
136 //store LSP object
137 PcepLspObject lspObj;
138 lspObj = PcepLspObjectVer1.read(cb);
139 pceInitLspReq.setLspObject(lspObj);
140
141 /* if R bit will be set then pcInitiate msg will contain only LSp and SRP objects
142 * so if R bit is not set then we should read for Ero and EndPoint objects also.
143 */
144 if (!isDelLspRequest) {
145
146 //store EndPoint object
147 PcepEndPointsObject endPointObj;
148 endPointObj = PcepEndPointsObjectVer1.read(cb);
149 pceInitLspReq.setEndPointsObject(endPointObj);
150
151 //store ERO object
152 PcepEroObject eroObj;
153 eroObj = PcepEroObjectVer1.read(cb);
154 pceInitLspReq.setEroObject(eroObj);
155
156 if (cb.readableBytes() > MINIMUM_COMMON_HEADER_LENGTH) {
157 pceInitLspReq.setPcepAttribute(PcepAttributeVer1.read(cb));
158 }
159 }
160 llPcInitiatedLspRequestList.add(pceInitLspReq);
161 }
162
163 return true;
164 }
165 }
166
167 /**
168 * Constructor to initialize PcInitiatedLspRequest.
169 *
170 * @param llPcInitiatedLspRequestList list of PcInitiatedLspRequest
171 */
172 PcepInitiateMsgVer1(LinkedList<PcInitiatedLspRequest> llPcInitiatedLspRequestList) {
173
174 if (llPcInitiatedLspRequestList == null) {
175 throw new NullPointerException("PcInitiatedLspRequestList cannot be null.");
176 }
177 this.llPcInitiatedLspRequestList = llPcInitiatedLspRequestList;
178 }
179
180 /**
181 * Builder class for PCEP initiate message.
182 */
183 static class Builder implements PcepInitiateMsg.Builder {
184
185 // Pcep initiate message fields
186 LinkedList<PcInitiatedLspRequest> llPcInitiatedLspRequestList;
187
188 @Override
189 public PcepVersion getVersion() {
190 return PcepVersion.PCEP_1;
191 }
192
193 @Override
194 public PcepType getType() {
195 return PcepType.INITIATE;
196 }
197
198 @Override
199 public PcepInitiateMsg build() {
200 return new PcepInitiateMsgVer1(this.llPcInitiatedLspRequestList);
201 }
202
203 @Override
204 public LinkedList<PcInitiatedLspRequest> getPcInitiatedLspRequestList() {
205 return this.llPcInitiatedLspRequestList;
206 }
207
208 @Override
209 public Builder setPcInitiatedLspRequestList(LinkedList<PcInitiatedLspRequest> ll) {
210 this.llPcInitiatedLspRequestList = ll;
211 return this;
212 }
213 }
214
215 @Override
216 public void writeTo(ChannelBuffer cb) throws PcepParseException {
217 WRITER.write(cb, this);
218 }
219
220 static final Writer WRITER = new Writer();
221
222 static class Writer implements PcepMessageWriter<PcepInitiateMsgVer1> {
223
224 @Override
225 public void write(ChannelBuffer cb, PcepInitiateMsgVer1 message) throws PcepParseException {
226
227 boolean isDelLspRequest = false;
228 int startIndex = cb.writerIndex();
229 // first 3 bits set to version
230 cb.writeByte((byte) (PACKET_VERSION << PcepMessageVer1.SHIFT_FLAG));
231 // message type 0xC
232 cb.writeByte(MSG_TYPE.getType());
233 // length is length of variable message, will be updated at the end
234 // Store the position of message
235 // length in buffer
236 int msgLenIndex = cb.writerIndex();
237 cb.writeShort(0);
238
239 ListIterator<PcInitiatedLspRequest> listIterator = message.llPcInitiatedLspRequestList.listIterator();
240
241 while (listIterator.hasNext()) {
242
243 PcInitiatedLspRequest listReq = listIterator.next();
244
245 //Srp Object is mandatory
246 PcepSrpObject srpObj = listReq.getSrpObject();
247 if (srpObj instanceof PcepSrpObject) {
248 isDelLspRequest = srpObj.getRFlag();
249 srpObj.write(cb);
250 } else {
251 throw new PcepParseException("SRP Object is mandatory for PcInitiate message.");
252 }
253
254 //LSP Object is mandatory
255 PcepLspObject lspObj = listReq.getLspObject();
256 if (lspObj instanceof PcepLspObject) {
257 lspObj.write(cb);
258 } else {
259 throw new PcepParseException("LSP Object is mandatory for PcInitiate message.");
260 }
261
262 /* if R bit will be set then pcInitiate msg will contain only LSp and SRP objects
263 * so if R bit is not set then we should read for Ero and EndPoint objects also.
264 */
265
266 if (!isDelLspRequest) {
267
268 //EndPoints object is mandatory
269 PcepEndPointsObject endPointObj = listReq.getEndPointsObject();
270 if (endPointObj instanceof PcepEndPointsObject) {
271 endPointObj.write(cb);
272 } else {
273 throw new PcepParseException("End points Object is mandatory for PcInitiate message.");
274 }
275
276 //Ero object is mandatory
277 PcepEroObject eroObj = listReq.getEroObject();
278 if (eroObj instanceof PcepEroObject) {
279 eroObj.write(cb);
280 } else {
281 throw new PcepParseException("ERO Object is mandatory for PcInitiate message.");
282 }
283
284 //PcepAttribute is optional
285 PcepAttribute pcepAttribute = listReq.getPcepAttribute();
286 if (pcepAttribute instanceof PcepAttribute) {
287 pcepAttribute.write(cb);
288 }
289 }
290 }
291
292 // PCInitiate message length field
293 int length = cb.writerIndex() - startIndex;
294 cb.setShort(msgLenIndex, (short) length);
295 }
296 }
297
298 @Override
299 public PcepVersion getVersion() {
300 return PcepVersion.PCEP_1;
301 }
302
303 @Override
304 public PcepType getType() {
305 return MSG_TYPE;
306 }
307
308 @Override
309 public LinkedList<PcInitiatedLspRequest> getPcInitiatedLspRequestList() {
310 return this.llPcInitiatedLspRequestList;
311 }
312
313 @Override
314 public void setPcInitiatedLspRequestList(LinkedList<PcInitiatedLspRequest> ll) {
315 this.llPcInitiatedLspRequestList = ll;
316 }
317
318 @Override
bharat saraswalf7364db2015-08-11 13:39:31 +0530319 public String toString() {
bharat saraswale1806302015-08-21 12:16:46 +0530320 return MoreObjects.toStringHelper(getClass()).add("PcInitiaitedLspRequestList", llPcInitiatedLspRequestList)
bharat saraswalf7364db2015-08-11 13:39:31 +0530321 .toString();
322 }
323}