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