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