blob: c6c081ae5b49cf38d15a1a2aad3c70a34dda1ab8 [file] [log] [blame]
bharat saraswalf7364db2015-08-11 13:39:31 +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 org.jboss.netty.buffer.ChannelBuffer;
20import org.onosproject.pcepio.exceptions.PcepParseException;
21import org.onosproject.pcepio.protocol.PcepMetricObject;
22import org.onosproject.pcepio.types.PcepObjectHeader;
23import org.slf4j.Logger;
24import org.slf4j.LoggerFactory;
25
26import com.google.common.base.MoreObjects;
27
28/*
29 METRIC Object Body Format.
30
31 0 1 2 3
32 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
33 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
34 | Reserved | Flags |C|B| T |
35 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
36 | metric-value |
37 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
38 */
39
40public class PcepMetricObjectVer1 implements PcepMetricObject {
41
42 protected static final Logger log = LoggerFactory.getLogger(PcepMetricObjectVer1.class);
43
44 public static final byte METRIC_OBJ_TYPE = 1;
45 public static final byte METRIC_OBJ_CLASS = 6;
46 public static final byte METRIC_OBJECT_VERSION = 1;
47 public static final short METRIC_OBJ_MINIMUM_LENGTH = 12;
48 public static final int OBJECT_HEADER_LENGTH = 4;
49 public static final int IFLAG_SHIFT_VALUE = 9;
50 public static final int BTYPE_SHIFT_VALUE = 8;
51 public static final int CFLAG_SET = 1;
52 public static final int CFLAG_RESET = 0;
53 public static final int BFLAG_SET = 1;
54 public static final int BFLAG_RESET = 0;
55 public static final byte CFLAG_CHECK = 0x02;
56
57 static final PcepObjectHeader DEFAULT_METRIC_OBJECT_HEADER = new PcepObjectHeader(METRIC_OBJ_CLASS,
58 METRIC_OBJ_TYPE, PcepObjectHeader.REQ_OBJ_OPTIONAL_PROCESS, PcepObjectHeader.RSP_OBJ_PROCESSED,
59 METRIC_OBJ_MINIMUM_LENGTH);
60
61 private PcepObjectHeader metricObjHeader;
62 private int iMetricVal;
63 private byte yFlag; // 6-flags
64 private boolean bCFlag;
65 private boolean bBFlag;
66 private byte bType;
67
68 /**
69 * Default constructor.
70 */
71 public PcepMetricObjectVer1() {
72 this.metricObjHeader = null;
73 this.iMetricVal = 0;
74 this.yFlag = 0;
75 this.bCFlag = false;
76 this.bBFlag = false;
77 this.bType = 0;
78
79 }
80
81 /**
82 * Constructor to initialize all member variables.
83 *
84 * @param metricObjHeader metric object header
85 * @param iMetricVal metric value
86 * @param yFlag Y flag
87 * @param bCFlag C flag
88 * @param bBFlag B flag
89 * @param bType Type value
90 */
91 public PcepMetricObjectVer1(PcepObjectHeader metricObjHeader, int iMetricVal, byte yFlag, boolean bCFlag,
92 boolean bBFlag, byte bType) {
93
94 this.metricObjHeader = metricObjHeader;
95 this.iMetricVal = iMetricVal;
96 this.yFlag = yFlag;
97 this.bCFlag = bCFlag;
98 this.bBFlag = bBFlag;
99 this.bType = bType;
100
101 }
102
103 @Override
104 public void setMetricVal(int value) {
105 this.iMetricVal = value;
106
107 }
108
109 @Override
110 public int getMetricVal() {
111 return this.iMetricVal;
112 }
113
114 @Override
115 public byte getYFlag() {
116 return this.yFlag;
117 }
118
119 @Override
120 public void setYFlag(byte value) {
121 this.yFlag = value;
122 }
123
124 @Override
125 public boolean getCFlag() {
126 return this.bCFlag;
127 }
128
129 @Override
130 public void setCFlag(boolean value) {
131 this.bCFlag = value;
132 }
133
134 @Override
135 public boolean getBFlag() {
136 return this.bBFlag;
137 }
138
139 @Override
140 public void setBFlag(boolean value) {
141 this.bBFlag = value;
142 }
143
144 @Override
145 public byte getBType() {
146 return this.bType;
147 }
148
149 @Override
150 public void setBType(byte value) {
151 this.bType = value;
152 }
153
154 /**
155 * Sets metric Object Header.
156 *
157 * @param obj metric object header
158 */
159 public void setMetricObjHeader(PcepObjectHeader obj) {
160 this.metricObjHeader = obj;
161 }
162
163 /**
164 * Returns metric Object Header.
165 *
166 * @return metricObjHeader
167 */
168 public PcepObjectHeader getMetricObjHeader() {
169 return this.metricObjHeader;
170 }
171
172 /**
173 * Reads from channel buffer and returns object of PcepMetricObject.
174 *
175 * @param cb of channel buffer.
176 * @return object of PcepMetricObject
177 * @throws PcepParseException when metric object is not present in channel buffer
178 */
179 public static PcepMetricObject read(ChannelBuffer cb) throws PcepParseException {
180
181 log.debug("MetricObject::read");
182 PcepObjectHeader metricObjHeader;
183 int iMetricVal;
184 byte yFlag; // 6-flags
185 boolean bCFlag;
186 boolean bBFlag;
187 byte bType;
188
189 metricObjHeader = PcepObjectHeader.read(cb);
190
191 if (metricObjHeader.getObjClass() != METRIC_OBJ_CLASS) {
192 throw new PcepParseException("This object is not a Metric Object. Object Class: "
193 + metricObjHeader.getObjClass());
194 }
195
196 //take only metric buffer.
197 ChannelBuffer tempCb = cb.readBytes(metricObjHeader.getObjLen() - OBJECT_HEADER_LENGTH);
198
199 tempCb.readShort();
200 yFlag = tempCb.readByte();
201 bType = tempCb.readByte();
202 bCFlag = (yFlag & CFLAG_CHECK) == CFLAG_CHECK ? true : false;
203 bBFlag = (yFlag & BFLAG_SET) == BFLAG_SET ? true : false;
204 iMetricVal = tempCb.readInt();
205
206 return new PcepMetricObjectVer1(metricObjHeader, iMetricVal, yFlag, bCFlag, bBFlag, bType);
207 }
208
209 @Override
210 public int write(ChannelBuffer cb) throws PcepParseException {
211 //write Object header
212 int objStartIndex = cb.writerIndex();
213
214 int objLenIndex = metricObjHeader.write(cb);
215
216 if (objLenIndex <= 0) {
217 throw new PcepParseException("Error: ObjectLength is " + objLenIndex);
218 }
219
220 int iFlag = (bCFlag) ? CFLAG_SET : CFLAG_RESET;
221 int iTemp = iFlag << IFLAG_SHIFT_VALUE;
222 iFlag = (bBFlag) ? BFLAG_SET : BFLAG_RESET;
223 iTemp = iTemp | (iFlag << BTYPE_SHIFT_VALUE);
224 iTemp = iTemp | bType;
225 cb.writeInt(iTemp);
226 cb.writeInt(iMetricVal);
227
228 short hLength = (short) (cb.writerIndex() - objStartIndex);
229 cb.setShort(objLenIndex, hLength);
230 //will be helpful during print().
231 metricObjHeader.setObjLen(hLength);
232 return hLength;
233 }
234
235 /**
236 * Builder class for PCEP metric object.
237 */
238 public static class Builder implements PcepMetricObject.Builder {
239
240 private boolean bIsHeaderSet = false;
241 private PcepObjectHeader metricObjHeader;
242 private int iMetricVal;
243 private boolean bIsMetricValSet = false;
244 private byte yFlag; // 6-flags
245 private boolean bCFlag;
246 private boolean bBFlag;
247 private byte bType;
248 private boolean bIsbTypeSet = false;
249
250 private boolean bIsPFlagSet = false;
251 private boolean bPFlag;
252
253 private boolean bIsIFlagSet = false;
254 private boolean bIFlag;
255
256 @Override
257 public PcepMetricObject build() throws PcepParseException {
258
259 PcepObjectHeader metricObjHeader = this.bIsHeaderSet ? this.metricObjHeader : DEFAULT_METRIC_OBJECT_HEADER;
260
261 if (!this.bIsMetricValSet) {
262 throw new PcepParseException(" Metric Value NOT Set while building PcepMetricObject.");
263 }
264 if (!this.bIsbTypeSet) {
265 throw new PcepParseException(" Type NOT Set while building PcepMetricObject.");
266 }
267
268 if (bIsPFlagSet) {
269 metricObjHeader.setPFlag(bPFlag);
270 }
271
272 if (bIsIFlagSet) {
273 metricObjHeader.setIFlag(bIFlag);
274 }
275
276 return new PcepMetricObjectVer1(metricObjHeader, iMetricVal, yFlag, bCFlag, bBFlag, bType);
277 }
278
279 @Override
280 public PcepObjectHeader getMetricObjHeader() {
281 return this.metricObjHeader;
282 }
283
284 @Override
285 public Builder setMetricObjHeader(PcepObjectHeader obj) {
286 this.metricObjHeader = obj;
287 this.bIsHeaderSet = true;
288 return this;
289 }
290
291 @Override
292 public int getMetricVal() {
293 return this.iMetricVal;
294 }
295
296 @Override
297 public Builder setMetricVal(int value) {
298 this.iMetricVal = value;
299 this.bIsMetricValSet = true;
300 return this;
301 }
302
303 @Override
304 public byte getYFlag() {
305 return this.yFlag;
306 }
307
308 @Override
309 public Builder setYFlag(byte value) {
310 this.yFlag = value;
311 return this;
312 }
313
314 @Override
315 public boolean getCFlag() {
316 return this.bCFlag;
317 }
318
319 @Override
320 public Builder setCFlag(boolean value) {
321 this.bCFlag = value;
322 return this;
323 }
324
325 @Override
326 public boolean getBFlag() {
327 return this.bBFlag;
328 }
329
330 @Override
331 public Builder setBFlag(boolean value) {
332 this.bBFlag = value;
333 return this;
334 }
335
336 @Override
337 public byte getBType() {
338 return this.bType;
339 }
340
341 @Override
342 public Builder setBType(byte value) {
343 this.bType = value;
344 this.bIsbTypeSet = true;
345 return this;
346 }
347
348 @Override
349 public Builder setPFlag(boolean value) {
350 this.bPFlag = value;
351 this.bIsPFlagSet = true;
352 return this;
353 }
354
355 @Override
356 public Builder setIFlag(boolean value) {
357 this.bIFlag = value;
358 this.bIsIFlagSet = true;
359 return this;
360 }
361
362 }
363
364 @Override
365 public void print() {
366
367 log.debug("METRIC OBJECT");
368 long lTemp = iMetricVal & 0xFFFFFFFF;
369 log.debug("iMetricVal: " + lTemp);
370 lTemp = (bBFlag) ? 1 : 0;
371 log.debug("B Flag: " + lTemp);
372 lTemp = (bCFlag) ? 1 : 0;
373 log.debug("C Flag: " + lTemp);
374 lTemp = bType & 0xFF;
375 log.debug("Type: " + lTemp);
376 }
377
378 @Override
379 public String toString() {
380 return MoreObjects.toStringHelper(getClass())
381 .add("Metric value", iMetricVal)
382 .add("B flag", bBFlag)
383 .add("C flag", bCFlag)
384 .add("B-type", bType)
385 .toString();
386 }
387}