blob: 83973b4d48ede02856b61112fdd65f3d86ac5653 [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;
Priyanka B413fbe82016-05-26 11:44:45 +053054 public static final int NO_OF_BITS = 8;
bharat saraswalf7364db2015-08-11 13:39:31 +053055 public static final short BANDWIDTH_OBJ_MINIMUM_LENGTH = 8;
56
57 static final PcepObjectHeader DEFAULT_BANDWIDTH_OBJECT_HEADER = new PcepObjectHeader(BANDWIDTH_OBJ_CLASS,
58 BANDWIDTH_OBJ_TYPE, PcepObjectHeader.REQ_OBJ_OPTIONAL_PROCESS, PcepObjectHeader.RSP_OBJ_PROCESSED,
59 BANDWIDTH_OBJ_MINIMUM_LENGTH);
60
61 private PcepObjectHeader bandwidthObjHeader;
Priyanka B413fbe82016-05-26 11:44:45 +053062 private float iBandwidth;
bharat saraswalf7364db2015-08-11 13:39:31 +053063
64 /**
65 * Constructor to bandwidth object header and bandwidth.
66 *
67 * @param bandwidthObjHeader bandwidth object header
68 * @param iBandwidth bandwidth value
69 */
Priyanka B413fbe82016-05-26 11:44:45 +053070 public PcepBandwidthObjectVer1(PcepObjectHeader bandwidthObjHeader, float iBandwidth) {
bharat saraswalf7364db2015-08-11 13:39:31 +053071 this.bandwidthObjHeader = bandwidthObjHeader;
72 this.iBandwidth = iBandwidth;
73 }
74
75 /**
76 * Constructor to initialize bandwidth.
77 *
78 * @param iBandwidth bandwidth value
79 */
Priyanka B413fbe82016-05-26 11:44:45 +053080 public PcepBandwidthObjectVer1(float iBandwidth) {
bharat saraswalf7364db2015-08-11 13:39:31 +053081 this.bandwidthObjHeader = DEFAULT_BANDWIDTH_OBJECT_HEADER;
82 this.iBandwidth = iBandwidth;
83 }
84
85 /**
86 * Returns Object Header.
87 *
88 * @return bandwidthObjHeader
89 */
90 public PcepObjectHeader getBandwidthObjHeader() {
91 return this.bandwidthObjHeader;
92 }
93
94 /**
95 * Sets Object Header.
96 *
97 * @param obj bandwidth object header
98 */
99 public void setBandwidthObjHeader(PcepObjectHeader obj) {
100 this.bandwidthObjHeader = obj;
101 }
102
103 @Override
Priyanka B413fbe82016-05-26 11:44:45 +0530104 public float getBandwidth() {
bharat saraswalf7364db2015-08-11 13:39:31 +0530105 return this.iBandwidth;
106 }
107
108 @Override
Priyanka B413fbe82016-05-26 11:44:45 +0530109 public void setBandwidth(float iBandwidth) {
bharat saraswalf7364db2015-08-11 13:39:31 +0530110 this.iBandwidth = iBandwidth;
111 }
112
113 /**
114 * Reads from channel buffer and returns object of PcepBandwidthObject.
115 *
116 * @param cb channel buffer to parse
117 * @return object of PcepBandwidthObject
118 * @throws PcepParseException while parsing channel buffer
119 */
120 public static PcepBandwidthObject read(ChannelBuffer cb) throws PcepParseException {
121
122 PcepObjectHeader bandwidthObjHeader;
Priyanka B413fbe82016-05-26 11:44:45 +0530123 float bandwidth;
bharat saraswalf7364db2015-08-11 13:39:31 +0530124
125 bandwidthObjHeader = PcepObjectHeader.read(cb);
Priyanka B413fbe82016-05-26 11:44:45 +0530126 bandwidth = ieeeToFloatRead(cb.readInt()) * NO_OF_BITS;
bharat saraswalf7364db2015-08-11 13:39:31 +0530127
Priyanka B413fbe82016-05-26 11:44:45 +0530128 return new PcepBandwidthObjectVer1(bandwidthObjHeader, bandwidth);
129 }
130
131 /**
132 * Parse the IEEE floating point notation and returns it in normal float.
133 *
134 * @param iVal IEEE floating point number
135 * @return normal float
136 */
137 public static float ieeeToFloatRead(int iVal) {
138 iVal = (((iVal & 0xFF) << 24) | ((iVal & 0xFF00) << 8)
139 | ((iVal & 0xFF0000) >> 8) | ((iVal >> 24) & 0xFF));
140
141 return Float.intBitsToFloat(iVal);
bharat saraswalf7364db2015-08-11 13:39:31 +0530142 }
143
144 @Override
145 public int write(ChannelBuffer cb) throws PcepParseException {
146
147 //write Object header
148 int objStartIndex = cb.writerIndex();
149 int objLenIndex = bandwidthObjHeader.write(cb);
150
151 if (objLenIndex <= 0) {
152 throw new PcepParseException("Failed to write bandwidth object header. Index " + objLenIndex);
153 }
154
Priyanka B413fbe82016-05-26 11:44:45 +0530155 cb.writeInt(Float.floatToIntBits(iBandwidth));
bharat saraswalf7364db2015-08-11 13:39:31 +0530156 short hLength = (short) (cb.writerIndex() - objStartIndex);
157 cb.setShort(objLenIndex, hLength);
158 //will be helpful during print().
159 bandwidthObjHeader.setObjLen(hLength);
160
161 return cb.writerIndex() - objStartIndex;
162 }
163
164 /**
165 * builder class for PCEP bandwidth object.
166 */
167 public static class Builder implements PcepBandwidthObject.Builder {
168
169 private PcepObjectHeader bandwidthObjHeader;
170 private boolean bIsHeaderSet = false;
171
172 private int iBandwidth;
173 private boolean bIsBandwidthSet = false;
174
175 private boolean bPFlag;
176 private boolean bIsPFlagSet = false;
177
178 private boolean bIFlag;
179 private boolean bIsIFlagSet = false;
180
181 @Override
182 public PcepBandwidthObject build() throws PcepParseException {
183
184 PcepObjectHeader bandwidthObjHeader = this.bIsHeaderSet ? this.bandwidthObjHeader
185 : DEFAULT_BANDWIDTH_OBJECT_HEADER;
186
187 if (bIsPFlagSet) {
188 bandwidthObjHeader.setPFlag(bPFlag);
189 }
190
191 if (bIsIFlagSet) {
192 bandwidthObjHeader.setIFlag(bIFlag);
193 }
194
195 if (!this.bIsBandwidthSet) {
196 throw new PcepParseException("bandwidth not Set while building Bandwidth Object.");
197 }
198
199 return new PcepBandwidthObjectVer1(bandwidthObjHeader, iBandwidth);
200 }
201
202 @Override
203 public int getBandwidth() {
204 return this.iBandwidth;
205 }
206
207 @Override
208 public PcepObjectHeader getBandwidthObjHeader() {
209 return this.bandwidthObjHeader;
210 }
211
212 @Override
213 public Builder setBandwidthObjHeader(PcepObjectHeader obj) {
214 this.bandwidthObjHeader = obj;
215 return this;
216 }
217
218 @Override
219 public Builder setBandwidth(int iBandwidth) {
220 this.iBandwidth = iBandwidth;
221 this.bIsBandwidthSet = true;
222 return this;
223 }
224
225 @Override
226 public Builder setPFlag(boolean value) {
227 this.bPFlag = value;
228 this.bIsPFlagSet = true;
229 return this;
230 }
231
232 @Override
233 public Builder setIFlag(boolean value) {
234 this.bIFlag = value;
235 this.bIsIFlagSet = true;
236 return this;
237 }
238
239 }
240
241 @Override
bharat saraswalf7364db2015-08-11 13:39:31 +0530242 public String toString() {
Sho SHIMIZU7cdbcf72015-09-03 14:43:05 -0700243 return MoreObjects.toStringHelper(getClass())
244 .add("BandwidthObjectHeader", bandwidthObjHeader)
bharat saraswale1806302015-08-21 12:16:46 +0530245 .add("Bandwidth", iBandwidth).toString();
bharat saraswalf7364db2015-08-11 13:39:31 +0530246 }
247}