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 java.util.LinkedList;
20 import java.util.ListIterator;
22 import org.jboss.netty.buffer.ChannelBuffer;
23 import org.onosproject.pcepio.exceptions.PcepParseException;
24 import org.onosproject.pcepio.protocol.PcepIroObject;
25 import org.onosproject.pcepio.types.IPv4SubObject;
26 import org.onosproject.pcepio.types.PcepObjectHeader;
27 import org.onosproject.pcepio.types.PcepValueType;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
31 import com.google.common.base.MoreObjects;
34 * Provides PCEP iro object.
36 public class PcepIroObjectVer1 implements PcepIroObject {
40 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
41 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
45 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
52 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
53 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
54 |L| Type | Length | IPv4 address (4 bytes) |
55 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
56 | IPv4 address (continued) | Prefix Length | Resvd |
57 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
59 protected static final Logger log = LoggerFactory.getLogger(PcepIroObjectVer1.class);
61 public static final byte IRO_OBJ_TYPE = 1;
62 public static final byte IRO_OBJ_CLASS = 10;
63 public static final byte IRO_OBJECT_VERSION = 1;
64 public static final short IRO_OBJ_MINIMUM_LENGTH = 12;
65 public static final int OBJECT_HEADER_LENGTH = 4;
66 public static final int YTYPE_SHIFT_VALUE = 0x7F;
68 public static final PcepObjectHeader DEFAULT_IRO_OBJECT_HEADER = new PcepObjectHeader(IRO_OBJ_CLASS, IRO_OBJ_TYPE,
69 PcepObjectHeader.REQ_OBJ_OPTIONAL_PROCESS, PcepObjectHeader.RSP_OBJ_PROCESSED, IRO_OBJ_MINIMUM_LENGTH);
71 private short iroObjType = 0;
73 private byte yPrefixLength;
75 private PcepObjectHeader iroObjHeader;
76 private LinkedList<PcepValueType> llSubObjects = new LinkedList<>();
79 * Default constructor.
81 public PcepIroObjectVer1() {
82 this.iroObjHeader = null;
88 * Constructor to initialize member variables.
90 * @param iroObjHeader IRO object header
91 * @param llSubObjects list of sub-objects
93 public PcepIroObjectVer1(PcepObjectHeader iroObjHeader, LinkedList<PcepValueType> llSubObjects) {
94 this.iroObjHeader = iroObjHeader;
95 this.llSubObjects = llSubObjects;
99 * Returns object header.
101 * @return iroObjHeader IRO object header
103 public PcepObjectHeader getIroObjHeader() {
104 return this.iroObjHeader;
108 * Sets IRO Object Header.
110 * @param obj IRO object header
112 public void setIroObjHeader(PcepObjectHeader obj) {
113 this.iroObjHeader = obj;
117 public LinkedList<PcepValueType> getSubObjects() {
118 return this.llSubObjects;
122 public void setSubObjects(LinkedList<PcepValueType> llSubObjects) {
123 this.llSubObjects = llSubObjects;
127 * Reads from channel buffer and return object of PcepIroObject.
129 * @param cb of type channel buffer
130 * @return object of PcepIroObject
131 * @throws PcepParseException while parsing from channel buffer
133 public static PcepIroObject read(ChannelBuffer cb) throws PcepParseException {
135 PcepObjectHeader iroObjHeader;
136 LinkedList<PcepValueType> llSubObjects;
138 iroObjHeader = PcepObjectHeader.read(cb);
140 //take only IroObject buffer.
141 ChannelBuffer tempCb = cb.readBytes(iroObjHeader.getObjLen() - OBJECT_HEADER_LENGTH);
142 llSubObjects = parseSubObjects(tempCb);
143 return new PcepIroObjectVer1(iroObjHeader, llSubObjects);
147 * Returns linked list of sub objects.
149 * @param cb of type channel buffer
150 * @return linked list of sub objects
151 * @throws PcepParseException while parsing subobjects from channel buffer
153 protected static LinkedList<PcepValueType> parseSubObjects(ChannelBuffer cb) throws PcepParseException {
155 LinkedList<PcepValueType> llSubObjects = new LinkedList<>();
157 while (0 < cb.readableBytes()) {
159 //check the Type of the Subobjects.
160 byte yType = cb.readByte();
161 yType = (byte) (yType & (YTYPE_SHIFT_VALUE));
162 byte hLength = cb.readByte();
164 PcepValueType subObj;
167 case IPv4SubObject.TYPE:
168 subObj = IPv4SubObject.read(cb);
172 throw new PcepParseException("Invalid sub object. Type: " + (int) yType);
175 // Check for the padding
176 int pad = hLength % 4;
179 if (pad <= cb.readableBytes()) {
183 llSubObjects.add(subObj);
189 public int write(ChannelBuffer cb) throws PcepParseException {
190 //write Object header
191 int objStartIndex = cb.writerIndex();
193 int objLenIndex = iroObjHeader.write(cb);
195 if (objLenIndex <= 0) {
196 throw new PcepParseException(" ObjectLength is " + objLenIndex);
199 ListIterator<PcepValueType> listIterator = llSubObjects.listIterator();
200 while (listIterator.hasNext()) {
201 listIterator.next().write(cb);
204 //Update object length now
205 int length = cb.writerIndex() - objStartIndex;
206 //will be helpful during print().
207 iroObjHeader.setObjLen((short) length);
208 // As per RFC the length of object should be
210 int pad = length % 4;
213 for (int i = 0; i < pad; i++) {
214 cb.writeByte((byte) 0);
216 length = length + pad;
218 cb.setShort(objLenIndex, (short) length);
219 objLenIndex = cb.writerIndex();
224 * Builder class for PCEP iro object.
226 public static class Builder implements PcepIroObject.Builder {
228 private boolean bIsHeaderSet = false;
230 private PcepObjectHeader iroObjHeader;
231 LinkedList<PcepValueType> llSubObjects = new LinkedList<>();
233 private boolean bIsPFlagSet = false;
234 private boolean bPFlag;
236 private boolean bIsIFlagSet = false;
237 private boolean bIFlag;
240 public PcepIroObject build() {
242 PcepObjectHeader iroObjHeader = this.bIsHeaderSet ? this.iroObjHeader : DEFAULT_IRO_OBJECT_HEADER;
245 iroObjHeader.setPFlag(bPFlag);
249 iroObjHeader.setIFlag(bIFlag);
252 return new PcepIroObjectVer1(iroObjHeader, this.llSubObjects);
256 public PcepObjectHeader getIroObjHeader() {
257 return this.iroObjHeader;
261 public Builder setIroObjHeader(PcepObjectHeader obj) {
262 this.iroObjHeader = obj;
263 this.bIsHeaderSet = true;
268 public LinkedList<PcepValueType> getSubObjects() {
269 return this.llSubObjects;
273 public Builder setSubObjects(LinkedList<PcepValueType> llSubObjects) {
274 this.llSubObjects = llSubObjects;
279 public Builder setPFlag(boolean value) {
281 this.bIsPFlagSet = true;
286 public Builder setIFlag(boolean value) {
288 this.bIsIFlagSet = true;
294 public String toString() {
295 return MoreObjects.toStringHelper(getClass())
296 .add("IroObjectHeader", iroObjHeader)
297 .add("SubObjects", llSubObjects).toString();