blob: 36215727783b590648ad0529ad5c507efecfb1e2 [file] [log] [blame]
bharat saraswalf7364db2015-08-11 13:39:31 +05301/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
bharat saraswalf7364db2015-08-11 13:39:31 +05303 *
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 org.jboss.netty.buffer.ChannelBuffer;
20import org.onosproject.pcepio.exceptions.PcepParseException;
21import org.onosproject.pcepio.protocol.PcepBandwidthObject;
22import org.onosproject.pcepio.types.PcepObjectHeader;
23import org.slf4j.Logger;
24import org.slf4j.LoggerFactory;
25
26import com.google.common.base.MoreObjects;
27
28/**
29 * Provides PcepBandwidthObject.
30 */
31public class PcepBandwidthObjectVer1 implements PcepBandwidthObject {
32
33 /*
34 * RFC : 5440 , section : 7.7.
35 0 1 2 3
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 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
38 | Bandwidth |
39 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
40
41 The BANDWIDTH Object format
42 */
43
44 protected static final Logger log = LoggerFactory.getLogger(PcepBandwidthObjectVer1.class);
45 /*
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.
49 */
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;
55
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);
59
60 private PcepObjectHeader bandwidthObjHeader;
61 private int iBandwidth;
62
63 /**
64 * Constructor to bandwidth object header and bandwidth.
65 *
66 * @param bandwidthObjHeader bandwidth object header
67 * @param iBandwidth bandwidth value
68 */
69 public PcepBandwidthObjectVer1(PcepObjectHeader bandwidthObjHeader, int iBandwidth) {
70 this.bandwidthObjHeader = bandwidthObjHeader;
71 this.iBandwidth = iBandwidth;
72 }
73
74 /**
75 * Constructor to initialize bandwidth.
76 *
77 * @param iBandwidth bandwidth value
78 */
79 public PcepBandwidthObjectVer1(int iBandwidth) {
80 this.bandwidthObjHeader = DEFAULT_BANDWIDTH_OBJECT_HEADER;
81 this.iBandwidth = iBandwidth;
82 }
83
84 /**
85 * Returns Object Header.
86 *
87 * @return bandwidthObjHeader
88 */
89 public PcepObjectHeader getBandwidthObjHeader() {
90 return this.bandwidthObjHeader;
91 }
92
93 /**
94 * Sets Object Header.
95 *
96 * @param obj bandwidth object header
97 */
98 public void setBandwidthObjHeader(PcepObjectHeader obj) {
99 this.bandwidthObjHeader = obj;
100 }
101
102 @Override
103 public int getBandwidth() {
104 return this.iBandwidth;
105 }
106
107 @Override
108 public void setBandwidth(int iBandwidth) {
109 this.iBandwidth = iBandwidth;
110 }
111
112 /**
113 * Reads from channel buffer and returns object of PcepBandwidthObject.
114 *
115 * @param cb channel buffer to parse
116 * @return object of PcepBandwidthObject
117 * @throws PcepParseException while parsing channel buffer
118 */
119 public static PcepBandwidthObject read(ChannelBuffer cb) throws PcepParseException {
120
121 PcepObjectHeader bandwidthObjHeader;
122 int iBandwidth;
123
124 bandwidthObjHeader = PcepObjectHeader.read(cb);
125 iBandwidth = cb.readInt();
126
127 return new PcepBandwidthObjectVer1(bandwidthObjHeader, iBandwidth);
128 }
129
130 @Override
131 public int write(ChannelBuffer cb) throws PcepParseException {
132
133 //write Object header
134 int objStartIndex = cb.writerIndex();
135 int objLenIndex = bandwidthObjHeader.write(cb);
136
137 if (objLenIndex <= 0) {
138 throw new PcepParseException("Failed to write bandwidth object header. Index " + objLenIndex);
139 }
140
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);
146
147 return cb.writerIndex() - objStartIndex;
148 }
149
150 /**
151 * builder class for PCEP bandwidth object.
152 */
153 public static class Builder implements PcepBandwidthObject.Builder {
154
155 private PcepObjectHeader bandwidthObjHeader;
156 private boolean bIsHeaderSet = false;
157
158 private int iBandwidth;
159 private boolean bIsBandwidthSet = false;
160
161 private boolean bPFlag;
162 private boolean bIsPFlagSet = false;
163
164 private boolean bIFlag;
165 private boolean bIsIFlagSet = false;
166
167 @Override
168 public PcepBandwidthObject build() throws PcepParseException {
169
170 PcepObjectHeader bandwidthObjHeader = this.bIsHeaderSet ? this.bandwidthObjHeader
171 : DEFAULT_BANDWIDTH_OBJECT_HEADER;
172
173 if (bIsPFlagSet) {
174 bandwidthObjHeader.setPFlag(bPFlag);
175 }
176
177 if (bIsIFlagSet) {
178 bandwidthObjHeader.setIFlag(bIFlag);
179 }
180
181 if (!this.bIsBandwidthSet) {
182 throw new PcepParseException("bandwidth not Set while building Bandwidth Object.");
183 }
184
185 return new PcepBandwidthObjectVer1(bandwidthObjHeader, iBandwidth);
186 }
187
188 @Override
189 public int getBandwidth() {
190 return this.iBandwidth;
191 }
192
193 @Override
194 public PcepObjectHeader getBandwidthObjHeader() {
195 return this.bandwidthObjHeader;
196 }
197
198 @Override
199 public Builder setBandwidthObjHeader(PcepObjectHeader obj) {
200 this.bandwidthObjHeader = obj;
201 return this;
202 }
203
204 @Override
205 public Builder setBandwidth(int iBandwidth) {
206 this.iBandwidth = iBandwidth;
207 this.bIsBandwidthSet = true;
208 return this;
209 }
210
211 @Override
212 public Builder setPFlag(boolean value) {
213 this.bPFlag = value;
214 this.bIsPFlagSet = true;
215 return this;
216 }
217
218 @Override
219 public Builder setIFlag(boolean value) {
220 this.bIFlag = value;
221 this.bIsIFlagSet = true;
222 return this;
223 }
224
225 }
226
227 @Override
bharat saraswalf7364db2015-08-11 13:39:31 +0530228 public String toString() {
Sho SHIMIZU7cdbcf72015-09-03 14:43:05 -0700229 return MoreObjects.toStringHelper(getClass())
230 .add("BandwidthObjectHeader", bandwidthObjHeader)
bharat saraswale1806302015-08-21 12:16:46 +0530231 .add("Bandwidth", iBandwidth).toString();
bharat saraswalf7364db2015-08-11 13:39:31 +0530232 }
233}