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.PcepMetricObject;
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 PCEP metric object.
31 public class PcepMetricObjectVer1 implements PcepMetricObject {
34 METRIC Object Body Format.
37 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
38 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
39 | Reserved | Flags |C|B| T |
40 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
42 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
45 protected static final Logger log = LoggerFactory.getLogger(PcepMetricObjectVer1.class);
47 public static final byte METRIC_OBJ_TYPE = 1;
48 public static final byte METRIC_OBJ_CLASS = 6;
49 public static final byte METRIC_OBJECT_VERSION = 1;
50 public static final short METRIC_OBJ_MINIMUM_LENGTH = 12;
51 public static final int OBJECT_HEADER_LENGTH = 4;
52 public static final int IFLAG_SHIFT_VALUE = 9;
53 public static final int BTYPE_SHIFT_VALUE = 8;
54 public static final int CFLAG_SET = 1;
55 public static final int CFLAG_RESET = 0;
56 public static final int BFLAG_SET = 1;
57 public static final int BFLAG_RESET = 0;
58 public static final byte CFLAG_CHECK = 0x02;
60 static final PcepObjectHeader DEFAULT_METRIC_OBJECT_HEADER = new PcepObjectHeader(METRIC_OBJ_CLASS,
61 METRIC_OBJ_TYPE, PcepObjectHeader.REQ_OBJ_OPTIONAL_PROCESS, PcepObjectHeader.RSP_OBJ_PROCESSED,
62 METRIC_OBJ_MINIMUM_LENGTH);
64 private PcepObjectHeader metricObjHeader;
65 private int iMetricVal;
66 private byte yFlag; // 6-flags
67 private boolean bCFlag;
68 private boolean bBFlag;
72 * Default constructor.
74 public PcepMetricObjectVer1() {
75 this.metricObjHeader = null;
85 * Constructor to initialize all member variables.
87 * @param metricObjHeader metric object header
88 * @param iMetricVal metric value
90 * @param bCFlag C flag
91 * @param bBFlag B flag
92 * @param bType Type value
94 public PcepMetricObjectVer1(PcepObjectHeader metricObjHeader, int iMetricVal, byte yFlag, boolean bCFlag,
95 boolean bBFlag, byte bType) {
97 this.metricObjHeader = metricObjHeader;
98 this.iMetricVal = iMetricVal;
100 this.bCFlag = bCFlag;
101 this.bBFlag = bBFlag;
107 public void setMetricVal(int value) {
108 this.iMetricVal = value;
113 public int getMetricVal() {
114 return this.iMetricVal;
118 public byte getYFlag() {
123 public void setYFlag(byte value) {
128 public boolean getCFlag() {
133 public void setCFlag(boolean value) {
138 public boolean getBFlag() {
143 public void setBFlag(boolean value) {
148 public byte getBType() {
153 public void setBType(byte value) {
158 * Sets metric Object Header.
160 * @param obj metric object header
162 public void setMetricObjHeader(PcepObjectHeader obj) {
163 this.metricObjHeader = obj;
167 * Returns metric Object Header.
169 * @return metricObjHeader
171 public PcepObjectHeader getMetricObjHeader() {
172 return this.metricObjHeader;
176 * Reads from channel buffer and returns object of PcepMetricObject.
178 * @param cb of channel buffer.
179 * @return object of PcepMetricObject
180 * @throws PcepParseException when metric object is not present in channel buffer
182 public static PcepMetricObject read(ChannelBuffer cb) throws PcepParseException {
184 log.debug("MetricObject::read");
185 PcepObjectHeader metricObjHeader;
187 byte yFlag; // 6-flags
192 metricObjHeader = PcepObjectHeader.read(cb);
194 if (metricObjHeader.getObjClass() != METRIC_OBJ_CLASS) {
195 throw new PcepParseException("This object is not a Metric Object. Object Class: "
196 + metricObjHeader.getObjClass());
199 //take only metric buffer.
200 ChannelBuffer tempCb = cb.readBytes(metricObjHeader.getObjLen() - OBJECT_HEADER_LENGTH);
203 yFlag = tempCb.readByte();
204 bType = tempCb.readByte();
205 bCFlag = (yFlag & CFLAG_CHECK) == CFLAG_CHECK;
206 bBFlag = (yFlag & BFLAG_SET) == BFLAG_SET;
207 iMetricVal = tempCb.readInt();
209 return new PcepMetricObjectVer1(metricObjHeader, iMetricVal, yFlag, bCFlag, bBFlag, bType);
213 public int write(ChannelBuffer cb) throws PcepParseException {
214 //write Object header
215 int objStartIndex = cb.writerIndex();
217 int objLenIndex = metricObjHeader.write(cb);
219 if (objLenIndex <= 0) {
220 throw new PcepParseException("Error: ObjectLength is " + objLenIndex);
223 int iFlag = (bCFlag) ? CFLAG_SET : CFLAG_RESET;
224 int iTemp = iFlag << IFLAG_SHIFT_VALUE;
225 iFlag = (bBFlag) ? BFLAG_SET : BFLAG_RESET;
226 iTemp = iTemp | (iFlag << BTYPE_SHIFT_VALUE);
227 iTemp = iTemp | bType;
229 cb.writeInt(iMetricVal);
231 short hLength = (short) (cb.writerIndex() - objStartIndex);
232 cb.setShort(objLenIndex, hLength);
233 //will be helpful during print().
234 metricObjHeader.setObjLen(hLength);
239 * Builder class for PCEP metric object.
241 public static class Builder implements PcepMetricObject.Builder {
243 private boolean bIsHeaderSet = false;
244 private PcepObjectHeader metricObjHeader;
245 private int iMetricVal;
246 private boolean bIsMetricValSet = false;
247 private byte yFlag; // 6-flags
248 private boolean bCFlag;
249 private boolean bBFlag;
251 private boolean bIsbTypeSet = false;
253 private boolean bIsPFlagSet = false;
254 private boolean bPFlag;
256 private boolean bIsIFlagSet = false;
257 private boolean bIFlag;
260 public PcepMetricObject build() throws PcepParseException {
262 PcepObjectHeader metricObjHeader = this.bIsHeaderSet ? this.metricObjHeader : DEFAULT_METRIC_OBJECT_HEADER;
264 if (!this.bIsMetricValSet) {
265 throw new PcepParseException(" Metric Value NOT Set while building PcepMetricObject.");
267 if (!this.bIsbTypeSet) {
268 throw new PcepParseException(" Type NOT Set while building PcepMetricObject.");
272 metricObjHeader.setPFlag(bPFlag);
276 metricObjHeader.setIFlag(bIFlag);
279 return new PcepMetricObjectVer1(metricObjHeader, iMetricVal, yFlag, bCFlag, bBFlag, bType);
283 public PcepObjectHeader getMetricObjHeader() {
284 return this.metricObjHeader;
288 public Builder setMetricObjHeader(PcepObjectHeader obj) {
289 this.metricObjHeader = obj;
290 this.bIsHeaderSet = true;
295 public int getMetricVal() {
296 return this.iMetricVal;
300 public Builder setMetricVal(int value) {
301 this.iMetricVal = value;
302 this.bIsMetricValSet = true;
307 public byte getYFlag() {
312 public Builder setYFlag(byte value) {
318 public boolean getCFlag() {
323 public Builder setCFlag(boolean value) {
329 public boolean getBFlag() {
334 public Builder setBFlag(boolean value) {
340 public byte getBType() {
345 public Builder setBType(byte value) {
347 this.bIsbTypeSet = true;
352 public Builder setPFlag(boolean value) {
354 this.bIsPFlagSet = true;
359 public Builder setIFlag(boolean value) {
361 this.bIsIFlagSet = true;
368 public String toString() {
369 return MoreObjects.toStringHelper(getClass())
370 .add("MetricValue", iMetricVal)
371 .add("BFlag", bBFlag)
372 .add("CFlag", bCFlag)