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 org.jboss.netty.buffer.ChannelBuffer;
20 import org.onosproject.pcepio.exceptions.PcepParseException;
21 import org.onosproject.pcepio.protocol.PcepBandwidthObject;
22 import org.onosproject.pcepio.types.PcepObjectHeader;
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
26 import com.google.common.base.MoreObjects;
29 * Provides PcepBandwidthObject.
31 public class PcepBandwidthObjectVer1 implements PcepBandwidthObject {
34 * RFC : 5440 , section : 7.7.
36 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
37 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
39 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
41 The BANDWIDTH Object format
44 protected static final Logger log = LoggerFactory.getLogger(PcepBandwidthObjectVer1.class);
46 * Requested bandwidth: BANDWIDTH Object-Type is 1.
47 Bandwidth of an existing TE LSP for which a re-optimization is
48 requested. BANDWIDTH Object-Type is 2.
50 //Right now handling type 1
51 public static final byte BANDWIDTH_OBJ_TYPE = 1;
52 public static final byte BANDWIDTH_OBJ_CLASS = 5;
53 public static final byte BANDWIDTH_OBJECT_VERSION = 1;
54 public static final short BANDWIDTH_OBJ_MINIMUM_LENGTH = 8;
56 static final PcepObjectHeader DEFAULT_BANDWIDTH_OBJECT_HEADER = new PcepObjectHeader(BANDWIDTH_OBJ_CLASS,
57 BANDWIDTH_OBJ_TYPE, PcepObjectHeader.REQ_OBJ_OPTIONAL_PROCESS, PcepObjectHeader.RSP_OBJ_PROCESSED,
58 BANDWIDTH_OBJ_MINIMUM_LENGTH);
60 private PcepObjectHeader bandwidthObjHeader;
61 private int iBandwidth;
64 * Constructor to bandwidth object header and bandwidth.
66 * @param bandwidthObjHeader bandwidth object header
67 * @param iBandwidth bandwidth value
69 public PcepBandwidthObjectVer1(PcepObjectHeader bandwidthObjHeader, int iBandwidth) {
70 this.bandwidthObjHeader = bandwidthObjHeader;
71 this.iBandwidth = iBandwidth;
75 * Constructor to initialize bandwidth.
77 * @param iBandwidth bandwidth value
79 public PcepBandwidthObjectVer1(int iBandwidth) {
80 this.bandwidthObjHeader = DEFAULT_BANDWIDTH_OBJECT_HEADER;
81 this.iBandwidth = iBandwidth;
85 * Returns Object Header.
87 * @return bandwidthObjHeader
89 public PcepObjectHeader getBandwidthObjHeader() {
90 return this.bandwidthObjHeader;
96 * @param obj bandwidth object header
98 public void setBandwidthObjHeader(PcepObjectHeader obj) {
99 this.bandwidthObjHeader = obj;
103 public int getBandwidth() {
104 return this.iBandwidth;
108 public void setBandwidth(int iBandwidth) {
109 this.iBandwidth = iBandwidth;
113 * Reads from channel buffer and returns object of PcepBandwidthObject.
115 * @param cb channel buffer to parse
116 * @return object of PcepBandwidthObject
117 * @throws PcepParseException while parsing channel buffer
119 public static PcepBandwidthObject read(ChannelBuffer cb) throws PcepParseException {
121 PcepObjectHeader bandwidthObjHeader;
124 bandwidthObjHeader = PcepObjectHeader.read(cb);
125 iBandwidth = cb.readInt();
127 return new PcepBandwidthObjectVer1(bandwidthObjHeader, iBandwidth);
131 public int write(ChannelBuffer cb) throws PcepParseException {
133 //write Object header
134 int objStartIndex = cb.writerIndex();
135 int objLenIndex = bandwidthObjHeader.write(cb);
137 if (objLenIndex <= 0) {
138 throw new PcepParseException("Failed to write bandwidth object header. Index " + objLenIndex);
141 cb.writeInt(iBandwidth);
142 short hLength = (short) (cb.writerIndex() - objStartIndex);
143 cb.setShort(objLenIndex, hLength);
144 //will be helpful during print().
145 bandwidthObjHeader.setObjLen(hLength);
147 return cb.writerIndex() - objStartIndex;
151 * builder class for PCEP bandwidth object.
153 public static class Builder implements PcepBandwidthObject.Builder {
155 private PcepObjectHeader bandwidthObjHeader;
156 private boolean bIsHeaderSet = false;
158 private int iBandwidth;
159 private boolean bIsBandwidthSet = false;
161 private boolean bPFlag;
162 private boolean bIsPFlagSet = false;
164 private boolean bIFlag;
165 private boolean bIsIFlagSet = false;
168 public PcepBandwidthObject build() throws PcepParseException {
170 PcepObjectHeader bandwidthObjHeader = this.bIsHeaderSet ? this.bandwidthObjHeader
171 : DEFAULT_BANDWIDTH_OBJECT_HEADER;
174 bandwidthObjHeader.setPFlag(bPFlag);
178 bandwidthObjHeader.setIFlag(bIFlag);
181 if (!this.bIsBandwidthSet) {
182 throw new PcepParseException("bandwidth not Set while building Bandwidth Object.");
185 return new PcepBandwidthObjectVer1(bandwidthObjHeader, iBandwidth);
189 public int getBandwidth() {
190 return this.iBandwidth;
194 public PcepObjectHeader getBandwidthObjHeader() {
195 return this.bandwidthObjHeader;
199 public Builder setBandwidthObjHeader(PcepObjectHeader obj) {
200 this.bandwidthObjHeader = obj;
205 public Builder setBandwidth(int iBandwidth) {
206 this.iBandwidth = iBandwidth;
207 this.bIsBandwidthSet = true;
212 public Builder setPFlag(boolean value) {
214 this.bIsPFlagSet = true;
219 public Builder setIFlag(boolean value) {
221 this.bIsIFlagSet = true;
228 public String toString() {
229 return MoreObjects.toStringHelper(getClass())
230 .add("BandwidthObjectHeader", bandwidthObjHeader)
231 .add("Bandwidth", iBandwidth).toString();