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 | /*
|
| 20 | * Provides PCEP Attribute List.
|
| 21 | */
|
| 22 | import java.util.LinkedList;
|
| 23 | import java.util.ListIterator;
|
| 24 |
|
| 25 | import org.jboss.netty.buffer.ChannelBuffer;
|
| 26 | import org.onosproject.pcepio.exceptions.PcepParseException;
|
| 27 | import org.onosproject.pcepio.protocol.PcepAttribute;
|
| 28 | import org.onosproject.pcepio.protocol.PcepBandwidthObject;
|
| 29 | import org.onosproject.pcepio.protocol.PcepIroObject;
|
| 30 | import org.onosproject.pcepio.protocol.PcepLspaObject;
|
| 31 | import org.onosproject.pcepio.protocol.PcepMetricObject;
|
| 32 | import org.onosproject.pcepio.types.PcepObjectHeader;
|
| 33 | import org.slf4j.Logger;
|
| 34 | import org.slf4j.LoggerFactory;
|
| 35 |
|
| 36 | import com.google.common.base.MoreObjects;
|
| 37 | import com.google.common.base.MoreObjects.ToStringHelper;
|
| 38 |
|
| 39 | /* Reference : RFC5440
|
| 40 | * where:
|
| 41 | * <attribute-list> ::=[<LSPA>]
|
| 42 | * [<BANDWIDTH>]
|
| 43 | * [<metric-list>]
|
| 44 | * [<IRO>]
|
| 45 | *
|
| 46 | * <metric-list> ::=<METRIC>[<metric-list>]
|
| 47 | */
|
| 48 |
|
| 49 | public class PcepAttributeVer1 implements PcepAttribute {
|
| 50 |
|
| 51 | protected static final Logger log = LoggerFactory.getLogger(PcepAttributeVer1.class);
|
| 52 |
|
| 53 | public static final int OBJECT_HEADER_LENGTH = 4;
|
| 54 |
|
| 55 | //PCEP LSPA Object
|
| 56 | private PcepLspaObject lspaObject;
|
| 57 | private boolean isLspaObjectSet;
|
| 58 |
|
| 59 | //PCEP Bandwidth Object
|
| 60 | private PcepBandwidthObject bandwidthObject;
|
| 61 | private boolean isBandwidthObjectSet;
|
| 62 |
|
| 63 | //PCEP Metric list
|
| 64 | private LinkedList<PcepMetricObject> llMetricList;
|
| 65 | private boolean isMetricListSet;
|
| 66 |
|
| 67 | //PCEP IRO object
|
| 68 | private PcepIroObject iroObject;
|
| 69 | private boolean isIroObjectSet;
|
| 70 |
|
| 71 | /**
|
| 72 | * Default constructor to initialize member variables.
|
| 73 | */
|
| 74 | public PcepAttributeVer1() {
|
| 75 |
|
| 76 | lspaObject = null;
|
| 77 | bandwidthObject = null;
|
| 78 | llMetricList = null;
|
| 79 | iroObject = null;
|
| 80 | this.isLspaObjectSet = false;
|
| 81 | this.isBandwidthObjectSet = false;
|
| 82 | this.isMetricListSet = false;
|
| 83 | this.isIroObjectSet = false;
|
| 84 | }
|
| 85 |
|
| 86 | /**
|
| 87 | * Constructor to initialize all parameters for PCEP attribute.
|
| 88 | *
|
| 89 | * @param lspaObject PCEP lspa Object.
|
| 90 | * @param bandwidthObject PCEP bandwidth object.
|
| 91 | * @param llMetricList list of PCEP metric objects.
|
| 92 | * @param iroObject PCEP iro object.
|
| 93 | */
|
| 94 | public PcepAttributeVer1(PcepLspaObject lspaObject, PcepBandwidthObject bandwidthObject,
|
| 95 | LinkedList<PcepMetricObject> llMetricList, PcepIroObject iroObject) {
|
| 96 |
|
| 97 | this.lspaObject = lspaObject;
|
| 98 | this.bandwidthObject = bandwidthObject;
|
| 99 | this.llMetricList = llMetricList;
|
| 100 | this.iroObject = iroObject;
|
| 101 | if (lspaObject == null) {
|
| 102 | this.isLspaObjectSet = false;
|
| 103 | } else {
|
| 104 | this.isLspaObjectSet = true;
|
| 105 | }
|
| 106 | if (bandwidthObject == null) {
|
| 107 | this.isBandwidthObjectSet = false;
|
| 108 | } else {
|
| 109 | this.isBandwidthObjectSet = true;
|
| 110 | }
|
| 111 | if (llMetricList == null) {
|
| 112 | this.isMetricListSet = false;
|
| 113 | } else {
|
| 114 | this.isMetricListSet = true;
|
| 115 | }
|
| 116 | if (iroObject == null) {
|
| 117 | this.isIroObjectSet = false;
|
| 118 | } else {
|
| 119 | this.isIroObjectSet = true;
|
| 120 | }
|
| 121 | }
|
| 122 |
|
| 123 | /**
|
| 124 | * constructor to initialize bandwidthObject.
|
| 125 | *
|
| 126 | * @param bandwidthObject bandwidth object
|
| 127 | */
|
| 128 | public PcepAttributeVer1(PcepBandwidthObject bandwidthObject) {
|
| 129 | this.isLspaObjectSet = false;
|
| 130 |
|
| 131 | this.bandwidthObject = bandwidthObject;
|
| 132 | this.isBandwidthObjectSet = true;
|
| 133 |
|
| 134 | this.isMetricListSet = false;
|
| 135 |
|
| 136 | this.isIroObjectSet = false;
|
| 137 | }
|
| 138 |
|
| 139 | /**
|
| 140 | * Parse list for MeticObject.
|
| 141 | *
|
| 142 | * @param cb of type channel buffer
|
| 143 | * @return true if parsing metric list is success
|
| 144 | * @throws PcepParseException when a non metric object is received
|
| 145 | */
|
| 146 | public boolean parseMetricList(ChannelBuffer cb) throws PcepParseException {
|
| 147 |
|
| 148 | if (null == llMetricList) {
|
| 149 | llMetricList = new LinkedList<PcepMetricObject>();
|
| 150 | }
|
| 151 |
|
| 152 | PcepMetricObject metriclist;
|
| 153 |
|
| 154 | //caller should verify for metric object
|
| 155 | byte yObjClass = PcepMetricObjectVer1.METRIC_OBJ_CLASS;
|
| 156 | byte yObjType = PcepMetricObjectVer1.METRIC_OBJ_TYPE;
|
| 157 |
|
| 158 | while ((yObjClass == PcepMetricObjectVer1.METRIC_OBJ_CLASS)
|
| 159 | && (yObjType == PcepMetricObjectVer1.METRIC_OBJ_TYPE)) {
|
| 160 |
|
| 161 | metriclist = PcepMetricObjectVer1.read(cb);
|
| 162 | llMetricList.add(metriclist);
|
| 163 | yObjClass = 0;
|
| 164 | yObjType = 0;
|
| 165 |
|
| 166 | if (cb.readableBytes() > OBJECT_HEADER_LENGTH) {
|
| 167 | cb.markReaderIndex();
|
| 168 | PcepObjectHeader tempObjHeader = PcepObjectHeader.read(cb);
|
| 169 | cb.resetReaderIndex();
|
| 170 | yObjClass = tempObjHeader.getObjClass();
|
| 171 | yObjType = tempObjHeader.getObjType();
|
| 172 | }
|
| 173 | }
|
| 174 | return true;
|
| 175 | }
|
| 176 |
|
| 177 | /**
|
| 178 | * Reads lspa , bandwidth , Metriclist and Iro objects and sets the objects.
|
| 179 | *
|
| 180 | * @param cb of type channel buffer
|
| 181 | * @return instance of Pcep Attribute
|
| 182 | * @throws PcepParseException while parsing Pcep Attributes from channel buffer
|
| 183 | */
|
| 184 |
|
| 185 | public static PcepAttribute read(ChannelBuffer cb) throws PcepParseException {
|
| 186 | if (cb.readableBytes() < OBJECT_HEADER_LENGTH) {
|
| 187 | return null;
|
| 188 | }
|
| 189 | //check whether any pcep attribute is present
|
| 190 | cb.markReaderIndex();
|
| 191 | PcepObjectHeader tempObjHeader = PcepObjectHeader.read(cb);
|
| 192 | cb.resetReaderIndex();
|
| 193 | byte yObjClass = tempObjHeader.getObjClass();
|
| 194 |
|
| 195 | if (PcepLspaObjectVer1.LSPA_OBJ_CLASS != yObjClass && PcepBandwidthObjectVer1.BANDWIDTH_OBJ_CLASS != yObjClass
|
| 196 | && PcepMetricObjectVer1.METRIC_OBJ_CLASS != yObjClass && PcepIroObjectVer1.IRO_OBJ_CLASS != yObjClass) {
|
| 197 | //No PCEP attribute is present
|
| 198 | return null;
|
| 199 | }
|
| 200 |
|
| 201 | PcepAttributeVer1 pcepAttribute = new PcepAttributeVer1();
|
| 202 |
|
| 203 | //If LSPA present then store it.LSPA is optional
|
| 204 | if (yObjClass == PcepLspaObjectVer1.LSPA_OBJ_CLASS) {
|
| 205 | pcepAttribute.setLspaObject(PcepLspaObjectVer1.read(cb));
|
| 206 | yObjClass = checkNextObject(cb);
|
| 207 | }
|
| 208 |
|
| 209 | //If BANDWIDTH present then store it.BANDWIDTH is optional
|
| 210 | if (yObjClass == PcepBandwidthObjectVer1.BANDWIDTH_OBJ_CLASS) {
|
| 211 | pcepAttribute.setBandwidthObject(PcepBandwidthObjectVer1.read(cb));
|
| 212 | yObjClass = checkNextObject(cb);
|
| 213 | }
|
| 214 |
|
| 215 | //If Metric list present then store it.MetricList is optional
|
| 216 | if (yObjClass == PcepMetricObjectVer1.METRIC_OBJ_CLASS) {
|
| 217 | pcepAttribute.parseMetricList(cb);
|
| 218 | yObjClass = checkNextObject(cb);
|
| 219 | }
|
| 220 |
|
| 221 | //If IRO present then store it.IRO is optional
|
| 222 | if (yObjClass == PcepIroObjectVer1.IRO_OBJ_CLASS) {
|
| 223 | pcepAttribute.setIroObject(PcepIroObjectVer1.read(cb));
|
| 224 | }
|
| 225 |
|
| 226 | PcepLspaObject lspaObject = pcepAttribute.getLspaObject();
|
| 227 | PcepBandwidthObject bandwidthObject = pcepAttribute.getBandwidthObject();
|
| 228 | LinkedList<PcepMetricObject> metriclist = pcepAttribute.llMetricList;
|
| 229 | PcepIroObject iroObject = pcepAttribute.getIroObject();
|
| 230 |
|
| 231 | return new PcepAttributeVer1(lspaObject, bandwidthObject, metriclist, iroObject);
|
| 232 | }
|
| 233 |
|
| 234 | /**
|
| 235 | * Checks whether there is a more object or not.
|
| 236 | *
|
| 237 | * @param cb of type channel buffer
|
| 238 | * @return instance of object header
|
| 239 | */
|
| 240 | private static byte checkNextObject(ChannelBuffer cb) {
|
| 241 | if (cb.readableBytes() < OBJECT_HEADER_LENGTH) {
|
| 242 | return 0;
|
| 243 | }
|
| 244 | cb.markReaderIndex();
|
| 245 | PcepObjectHeader tempObjHeader = PcepObjectHeader.read(cb);
|
| 246 | cb.resetReaderIndex();
|
| 247 | return tempObjHeader.getObjClass();
|
| 248 | }
|
| 249 |
|
| 250 | @Override
|
| 251 | public int write(ChannelBuffer cb) throws PcepParseException {
|
| 252 | int iLenStartIndex = cb.writerIndex();
|
| 253 | //PCEP LSPA object is optional
|
| 254 | if (this.isLspaObjectSet) {
|
| 255 | this.lspaObject.write(cb);
|
| 256 | }
|
| 257 |
|
| 258 | //PCEP BANDWIDTH object is optional
|
| 259 | if (this.isBandwidthObjectSet) {
|
| 260 | this.bandwidthObject.write(cb);
|
| 261 | }
|
| 262 |
|
| 263 | //PCEP Metric list is optional
|
| 264 | if (this.isMetricListSet) {
|
| 265 | ListIterator<PcepMetricObject> listIterator = this.llMetricList.listIterator();
|
| 266 | while (listIterator.hasNext()) {
|
| 267 | listIterator.next().write(cb);
|
| 268 | }
|
| 269 | }
|
| 270 |
|
| 271 | //PCEP IRO object is optional
|
| 272 | if (this.isIroObjectSet) {
|
| 273 | this.iroObject.write(cb);
|
| 274 | }
|
| 275 | return cb.writerIndex() - iLenStartIndex;
|
| 276 | }
|
| 277 |
|
| 278 | @Override
|
| 279 | public PcepLspaObject getLspaObject() {
|
| 280 | return lspaObject;
|
| 281 | }
|
| 282 |
|
| 283 | @Override
|
| 284 | public PcepBandwidthObject getBandwidthObject() {
|
| 285 | return bandwidthObject;
|
| 286 | }
|
| 287 |
|
| 288 | @Override
|
| 289 | public LinkedList<PcepMetricObject> getMetricObjectList() {
|
| 290 | return llMetricList;
|
| 291 | }
|
| 292 |
|
| 293 | @Override
|
| 294 | public PcepIroObject getIroObject() {
|
| 295 | return iroObject;
|
| 296 | }
|
| 297 |
|
| 298 | @Override
|
| 299 | public void setBandwidthObject(PcepBandwidthObject bandwidthObject) {
|
| 300 | this.isBandwidthObjectSet = true;
|
| 301 | this.bandwidthObject = bandwidthObject;
|
| 302 | }
|
| 303 |
|
| 304 | @Override
|
| 305 | public void setMetricObjectList(LinkedList<PcepMetricObject> llMetricList) {
|
| 306 | this.isMetricListSet = true;
|
| 307 | this.llMetricList = llMetricList;
|
| 308 |
|
| 309 | }
|
| 310 |
|
| 311 | @Override
|
| 312 | public void setLspaObject(PcepLspaObject lspaObject) {
|
| 313 | this.isLspaObjectSet = true;
|
| 314 | this.lspaObject = lspaObject;
|
| 315 | }
|
| 316 |
|
| 317 | @Override
|
| 318 | public void setIroObject(PcepIroObject iroObject) {
|
| 319 | this.isIroObjectSet = true;
|
| 320 | this.iroObject = iroObject;
|
| 321 | }
|
| 322 |
|
| 323 | /**
|
| 324 | * Builder class for PCEP attributes.
|
| 325 | */
|
| 326 | public static class Builder implements PcepAttribute.Builder {
|
| 327 |
|
| 328 | //PCEP LSPA Object
|
| 329 | private PcepLspaObject lspaObject;
|
| 330 | private boolean isLspaObjectSet;
|
| 331 |
|
| 332 | //PCEP BANDWIDTH Object
|
| 333 | private PcepBandwidthObject bandwidthObject;
|
| 334 | private boolean isBandwidthObjectSet;
|
| 335 |
|
| 336 | //PCEP Metric list
|
| 337 | private LinkedList<PcepMetricObject> llMetricList;
|
| 338 | private boolean isMetricListSet;
|
| 339 |
|
| 340 | //PCEP IRO object
|
| 341 | private PcepIroObject iroObject;
|
| 342 | private boolean isIroObjectSet;
|
| 343 |
|
| 344 | @Override
|
| 345 | public PcepAttribute build() {
|
| 346 |
|
| 347 | //PCEP LSPA Object
|
| 348 | PcepLspaObject lspaObject = null;
|
| 349 |
|
| 350 | //PCEP BANDWIDTH Object
|
| 351 | PcepBandwidthObject bandwidthObject = null;
|
| 352 |
|
| 353 | //PCEP Metric list
|
| 354 | LinkedList<PcepMetricObject> llMetricList = null;
|
| 355 |
|
| 356 | //PCEP IRO object
|
| 357 | PcepIroObject iroObject = null;
|
| 358 |
|
| 359 | if (this.isLspaObjectSet) {
|
| 360 | lspaObject = this.lspaObject;
|
| 361 | }
|
| 362 | if (this.isBandwidthObjectSet) {
|
| 363 | bandwidthObject = this.bandwidthObject;
|
| 364 | }
|
| 365 | if (this.isMetricListSet) {
|
| 366 | llMetricList = this.llMetricList;
|
| 367 | }
|
| 368 | if (this.isIroObjectSet) {
|
| 369 | iroObject = this.iroObject;
|
| 370 | }
|
| 371 | return new PcepAttributeVer1(lspaObject, bandwidthObject, llMetricList, iroObject);
|
| 372 | }
|
| 373 |
|
| 374 | @Override
|
| 375 | public PcepLspaObject getLspaObject() {
|
| 376 | return this.lspaObject;
|
| 377 | }
|
| 378 |
|
| 379 | @Override
|
| 380 | public PcepBandwidthObject getBandwidthObject() {
|
| 381 | return this.bandwidthObject;
|
| 382 | }
|
| 383 |
|
| 384 | @Override
|
| 385 | public LinkedList<PcepMetricObject> getMetricObjectList() {
|
| 386 | return this.llMetricList;
|
| 387 | }
|
| 388 |
|
| 389 | @Override
|
| 390 | public PcepIroObject getIroObject() {
|
| 391 | return this.iroObject;
|
| 392 | }
|
| 393 |
|
| 394 | @Override
|
| 395 | public Builder setBandwidthObject(PcepBandwidthObject bandwidthObject) {
|
| 396 | this.isBandwidthObjectSet = true;
|
| 397 | this.bandwidthObject = bandwidthObject;
|
| 398 | return this;
|
| 399 | }
|
| 400 |
|
| 401 | @Override
|
| 402 | public Builder setMetricObjectList(LinkedList<PcepMetricObject> llMetricList) {
|
| 403 | this.isMetricListSet = true;
|
| 404 | this.llMetricList = llMetricList;
|
| 405 | return this;
|
| 406 | }
|
| 407 |
|
| 408 | @Override
|
| 409 | public Builder setLspaObject(PcepLspaObject lspaObject) {
|
| 410 | this.isLspaObjectSet = true;
|
| 411 | this.lspaObject = lspaObject;
|
| 412 | return this;
|
| 413 | }
|
| 414 |
|
| 415 | @Override
|
| 416 | public Builder setIroObject(PcepIroObject iroObject) {
|
| 417 | this.isIroObjectSet = true;
|
| 418 | this.iroObject = iroObject;
|
| 419 | return this;
|
| 420 | }
|
| 421 | }
|
| 422 |
|
| 423 | @Override
|
bharat saraswal | f7364db | 2015-08-11 13:39:31 +0530 | [diff] [blame] | 424 | public String toString() {
|
| 425 | ToStringHelper toStrHelper = MoreObjects.toStringHelper(getClass());
|
| 426 |
|
| 427 | if (lspaObject instanceof PcepLspaObject) {
|
bharat saraswal | e180630 | 2015-08-21 12:16:46 +0530 | [diff] [blame^] | 428 | toStrHelper.add("lspaObject", lspaObject);
|
bharat saraswal | f7364db | 2015-08-11 13:39:31 +0530 | [diff] [blame] | 429 | }
|
| 430 | if (bandwidthObject instanceof PcepBandwidthObject) {
|
bharat saraswal | e180630 | 2015-08-21 12:16:46 +0530 | [diff] [blame^] | 431 | toStrHelper.add("bandwidthObject", bandwidthObject);
|
bharat saraswal | f7364db | 2015-08-11 13:39:31 +0530 | [diff] [blame] | 432 | }
|
| 433 | if (llMetricList instanceof PcepMetricObject) {
|
bharat saraswal | e180630 | 2015-08-21 12:16:46 +0530 | [diff] [blame^] | 434 | toStrHelper.add("MetricObjectList", llMetricList);
|
bharat saraswal | f7364db | 2015-08-11 13:39:31 +0530 | [diff] [blame] | 435 | }
|
| 436 | if (iroObject instanceof PcepIroObject) {
|
bharat saraswal | e180630 | 2015-08-21 12:16:46 +0530 | [diff] [blame^] | 437 | toStrHelper.add("IroObject", iroObject);
|
bharat saraswal | f7364db | 2015-08-11 13:39:31 +0530 | [diff] [blame] | 438 | }
|
| 439 | return toStrHelper.toString();
|
| 440 | }
|
| 441 | } |