blob: a4ac87c3e9bbce912a810c9e5491eba3ff0b955f [file] [log] [blame]
Bharat saraswal30777172015-08-21 15:51:10 +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.PcepLabelObject;
25import org.onosproject.pcepio.types.NexthopIPv4addressTlv;
26import org.onosproject.pcepio.types.NexthopIPv6addressTlv;
27import org.onosproject.pcepio.types.NexthopUnnumberedIPv4IDTlv;
28import org.onosproject.pcepio.types.PcepObjectHeader;
29import org.onosproject.pcepio.types.PcepValueType;
30import org.slf4j.Logger;
31import org.slf4j.LoggerFactory;
32
33import com.google.common.base.MoreObjects;
34
Phanendra Manda51fb9c22015-09-01 16:17:41 +053035/**
36 * Provides PCEP label object.
37 */
38public class PcepLabelObjectVer1 implements PcepLabelObject {
39
40 /*
41 * ref : draft-zhao-pce-pcep-extension-for-pce-controller-01 , section : 7.4.
Bharat saraswal30777172015-08-21 15:51:10 +053042
43 0 1 2 3
44 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
45 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
46 | Reserved | Flags |O|
47 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
48 | Label |
49 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
50 | |
51 // Optional TLV //
52 | |
53 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
54 The LABEL Object format
Phanendra Manda51fb9c22015-09-01 16:17:41 +053055 */
Bharat saraswal30777172015-08-21 15:51:10 +053056 protected static final Logger log = LoggerFactory.getLogger(PcepLspObjectVer1.class);
57
58 public static final byte LABEL_OBJ_TYPE = 1;
59 public static final byte LABEL_OBJ_CLASS = 35; //TBD : to be defined
60 public static final byte LABEL_OBJECT_VERSION = 1;
61 public static final byte OBJECT_HEADER_LENGTH = 4;
62 public static final boolean DEFAULT_OFLAG = false;
63
64 // LSP_OBJ_MINIMUM_LENGTH = CommonHeaderLen(4)+ LspObjectHeaderLen(8)
65 public static final short LABEL_OBJ_MINIMUM_LENGTH = 12;
66
67 public static final int OFLAG_SET = 1;
68 public static final int OFLAG_RESET = 0;
69 public static final int MINIMUM_COMMON_HEADER_LENGTH = 4;
70
71 static final PcepObjectHeader DEFAULT_LABEL_OBJECT_HEADER = new PcepObjectHeader(LABEL_OBJ_CLASS, LABEL_OBJ_TYPE,
72 PcepObjectHeader.REQ_OBJ_OPTIONAL_PROCESS, PcepObjectHeader.RSP_OBJ_PROCESSED, LABEL_OBJ_MINIMUM_LENGTH);
73
74 private PcepObjectHeader labelObjHeader;
75 private boolean bOFlag;
76 private int label;
77 // Optional TLV
78 private LinkedList<PcepValueType> llOptionalTlv;
79
80 /**
81 * Constructor to initialize parameters for PCEP label object.
82 *
83 * @param labelObjHeader label object header
84 * @param bOFlag O flag
85 * @param label label
86 * @param llOptionalTlv list of optional tlvs
87 */
88 public PcepLabelObjectVer1(PcepObjectHeader labelObjHeader, boolean bOFlag, int label,
89 LinkedList<PcepValueType> llOptionalTlv) {
90 this.labelObjHeader = labelObjHeader;
91 this.bOFlag = bOFlag;
92 this.label = label;
93 this.llOptionalTlv = llOptionalTlv;
94 }
95
96 @Override
97 public LinkedList<PcepValueType> getOptionalTlv() {
98 return this.llOptionalTlv;
99 }
100
101 @Override
102 public void setOptionalTlv(LinkedList<PcepValueType> llOptionalTlv) {
103 this.llOptionalTlv = llOptionalTlv;
104 }
105
106 @Override
107 public boolean getOFlag() {
108 return this.bOFlag;
109 }
110
111 @Override
112 public void setOFlag(boolean value) {
113 this.bOFlag = value;
114 }
115
116 @Override
117 public int getLabel() {
118 return this.label;
119 }
120
121 @Override
122 public void setLabel(int value) {
123 this.label = value;
124 }
125
126 /**
127 * Reads form channel buffer and returns objects of PcepLabelObject.
128 *
129 * @param cb of type channel buffer
130 * @return objects of PcepLabelObject
131 * @throws PcepParseException when fails to read from channel buffer
132 */
133 public static PcepLabelObject read(ChannelBuffer cb) throws PcepParseException {
134
135 PcepObjectHeader labelObjHeader;
136
137 boolean bOFlag;
138 int label;
139
140 // Optional TLV
Sho SHIMIZU9b8274c2015-09-04 15:54:24 -0700141 LinkedList<PcepValueType> llOptionalTlv = new LinkedList<>();
Bharat saraswal30777172015-08-21 15:51:10 +0530142 labelObjHeader = PcepObjectHeader.read(cb);
143
144 //take only LspObject buffer.
145 ChannelBuffer tempCb = cb.readBytes(labelObjHeader.getObjLen() - OBJECT_HEADER_LENGTH);
146
147 int iTemp = tempCb.readInt();
Phanendra Mandaf346ea82015-09-04 15:21:39 -0700148 bOFlag = (iTemp & (byte) 0x01) == 1;
Bharat saraswal30777172015-08-21 15:51:10 +0530149 label = tempCb.readInt();
150
151 // parse optional TLV
152 llOptionalTlv = parseOptionalTlv(tempCb);
153 return new PcepLabelObjectVer1(labelObjHeader, bOFlag, label, llOptionalTlv);
154 }
155
156 @Override
157 public int write(ChannelBuffer cb) throws PcepParseException {
158
159 //write Object header
160 int objStartIndex = cb.writerIndex();
161 int objLenIndex = labelObjHeader.write(cb);
162
163 if (objLenIndex <= 0) {
164 throw new PcepParseException(" ObjectLength Index is " + objLenIndex);
165 }
166
167 byte oFlag;
168
169 oFlag = (byte) ((bOFlag) ? OFLAG_SET : OFLAG_RESET);
170 cb.writeInt(oFlag);
171 cb.writeInt(label);
172
173 // Add optional TLV
174 packOptionalTlv(cb);
175
176 //Update object length now
177 int length = cb.writerIndex() - objStartIndex;
178
179 //will be helpful during print().
180 labelObjHeader.setObjLen((short) length);
181 cb.setShort(objLenIndex, (short) length);
182 return cb.writerIndex();
183 }
184
185 /**
186 * Returns list of optional tlvs.
187 *
188 * @param cb of type channel buffer
189 * @return list of optional tlvs.
190 * @throws PcepParseException when fails to parse list of optional tlvs
191 */
192 protected static LinkedList<PcepValueType> parseOptionalTlv(ChannelBuffer cb) throws PcepParseException {
193
Sho SHIMIZU9b8274c2015-09-04 15:54:24 -0700194 LinkedList<PcepValueType> llOutOptionalTlv = new LinkedList<>();
Bharat saraswal30777172015-08-21 15:51:10 +0530195
196 while (MINIMUM_COMMON_HEADER_LENGTH <= cb.readableBytes()) {
197
198 PcepValueType tlv;
199 short hType = cb.readShort();
200 short hLength = cb.readShort();
201 int iValue = 0;
202
203 switch (hType) {
204
205 case NexthopIPv4addressTlv.TYPE:
206 iValue = cb.readInt();
207 tlv = new NexthopIPv4addressTlv(iValue);
208 break;
209 case NexthopIPv6addressTlv.TYPE:
210 byte[] ipv6Value = new byte[NexthopIPv6addressTlv.VALUE_LENGTH];
211 cb.readBytes(ipv6Value, 0, NexthopIPv6addressTlv.VALUE_LENGTH);
212 tlv = new NexthopIPv6addressTlv(ipv6Value);
213 break;
214 case NexthopUnnumberedIPv4IDTlv.TYPE:
215 tlv = NexthopUnnumberedIPv4IDTlv.read(cb);
216 break;
217 default:
218 throw new PcepParseException("Unsupported TLV type :" + hType);
219 }
220
221 // Check for the padding
222 int pad = hLength % 4;
223 if (0 < pad) {
224 pad = 4 - pad;
225 if (pad <= cb.readableBytes()) {
226 cb.skipBytes(pad);
227 }
228 }
229
230 llOutOptionalTlv.add(tlv);
231 }
232
233 if (0 < cb.readableBytes()) {
234
235 throw new PcepParseException("Optional Tlv parsing error. Extra bytes received.");
236 }
237 return llOutOptionalTlv;
238 }
239
240 /**
241 * Returns the writer index.
242 *
243 * @param cb of channel buffer.
244 * @return writer index
245 */
246 protected int packOptionalTlv(ChannelBuffer cb) {
247
248 ListIterator<PcepValueType> listIterator = llOptionalTlv.listIterator();
249
250 while (listIterator.hasNext()) {
251 PcepValueType tlv = listIterator.next();
252
Sho SHIMIZUde09fa02015-09-03 09:39:52 -0700253 if (tlv == null) {
Bharat saraswal30777172015-08-21 15:51:10 +0530254 log.debug("tlv is null from OptionalTlv list");
255 continue;
256 }
257 tlv.write(cb);
258 }
259 return cb.writerIndex();
260 }
261
262 /**
263 * Builder class for PCEP label object.
264 */
265 public static class Builder implements PcepLabelObject.Builder {
266
267 private boolean bIsHeaderSet = false;
268 private boolean bIsOFlagSet = false;
269 private boolean bIsLabelSet = false;
270
271 private PcepObjectHeader labelObjHeader;
272 private boolean bOFlag;
273 private int label;
274
Sho SHIMIZU9b8274c2015-09-04 15:54:24 -0700275 LinkedList<PcepValueType> llOptionalTlv = new LinkedList<>();
Bharat saraswal30777172015-08-21 15:51:10 +0530276
277 private boolean bIsPFlagSet = false;
278 private boolean bPFlag;
279
280 private boolean bIsIFlagSet = false;
281 private boolean bIFlag;
282
283 @Override
284 public PcepLabelObject build() throws PcepParseException {
285 PcepObjectHeader labelObjHeader = this.bIsHeaderSet ? this.labelObjHeader : DEFAULT_LABEL_OBJECT_HEADER;
286 boolean bOFlag = this.bIsOFlagSet ? this.bOFlag : DEFAULT_OFLAG;
287
288 if (!this.bIsLabelSet) {
289 throw new PcepParseException(" Label NOT Set while building PcepLabelObject.");
290 }
291 if (bIsPFlagSet) {
292 labelObjHeader.setPFlag(bPFlag);
293 }
294 if (bIsIFlagSet) {
295 labelObjHeader.setIFlag(bIFlag);
296 }
297 return new PcepLabelObjectVer1(labelObjHeader, bOFlag, this.label, this.llOptionalTlv);
298 }
299
300 @Override
301 public PcepObjectHeader getLabelObjHeader() {
302 return this.labelObjHeader;
303 }
304
305 @Override
306 public Builder setLabelObjHeader(PcepObjectHeader obj) {
307 this.labelObjHeader = obj;
308 this.bIsHeaderSet = true;
309 return this;
310 }
311
312 @Override
313 public boolean getOFlag() {
314 return this.bOFlag;
315 }
316
317 @Override
318 public Builder setOFlag(boolean value) {
319 this.bOFlag = value;
320 this.bIsOFlagSet = true;
321 return this;
322 }
323
324 @Override
325 public int getLabel() {
326 return this.label;
327 }
328
329 @Override
330 public Builder setLabel(int value) {
331 this.label = value;
332 this.bIsLabelSet = true;
333 return this;
334 }
335
336 @Override
337 public LinkedList<PcepValueType> getOptionalTlv() {
338 return this.llOptionalTlv;
339 }
340
341 @Override
342 public Builder setOptionalTlv(LinkedList<PcepValueType> llOptionalTlv) {
343 this.llOptionalTlv = llOptionalTlv;
344 return this;
345 }
346
347 @Override
348 public Builder setPFlag(boolean value) {
349 this.bPFlag = value;
350 this.bIsPFlagSet = true;
351 return this;
352 }
353
354 @Override
355 public Builder setIFlag(boolean value) {
356 this.bIFlag = value;
357 this.bIsIFlagSet = true;
358 return this;
359 }
360 }
361
362 @Override
363 public String toString() {
Sho SHIMIZU7cdbcf72015-09-03 14:43:05 -0700364 return MoreObjects.toStringHelper(getClass())
365 .add("OFlag", bOFlag)
366 .add("label", label)
367 .add("OptionalTlvList", llOptionalTlv)
368 .toString();
Bharat saraswal30777172015-08-21 15:51:10 +0530369 }
370}