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