Bharat saraswal | f1a52b8 | 2015-08-21 16:34:20 +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 java.util.LinkedList;
|
| 20 | import java.util.ListIterator;
|
| 21 |
|
| 22 | import org.jboss.netty.buffer.ChannelBuffer;
|
| 23 | import org.onosproject.pcepio.exceptions.PcepParseException;
|
| 24 | import org.onosproject.pcepio.protocol.PcepLabelRangeObject;
|
| 25 | import org.onosproject.pcepio.types.PathSetupTypeTlv;
|
| 26 | import org.onosproject.pcepio.types.PcepObjectHeader;
|
| 27 | import org.onosproject.pcepio.types.PcepValueType;
|
| 28 | import org.slf4j.Logger;
|
| 29 | import org.slf4j.LoggerFactory;
|
| 30 |
|
| 31 | import com.google.common.base.MoreObjects;
|
| 32 |
|
| 33 | public class PcepLabelRangeObjectVer1 implements PcepLabelRangeObject {
|
| 34 |
|
| 35 | /*
|
| 36 | * ref : draft-zhao-pce-pcep-extension-for-pce-controller-01, section : 7.2
|
| 37 | 0 1 2 3
|
| 38 | 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
|
| 39 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|
| 40 | | label type | range size |
|
| 41 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|
| 42 | | label base |
|
| 43 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|
| 44 | | |
|
| 45 | // Optional TLVs //
|
| 46 | | |
|
| 47 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|
| 48 |
|
| 49 | LABEL-RANGE Object
|
| 50 | */
|
| 51 | protected static final Logger log = LoggerFactory.getLogger(PcepLabelRangeObjectVer1.class);
|
| 52 |
|
| 53 | public static final byte LABEL_RANGE_OBJ_TYPE = 1;
|
| 54 | public static final byte LABEL_RANGE_OBJ_CLASS = 60; //to be defined
|
| 55 | public static final byte LABEL_RANGE_OBJECT_VERSION = 1;
|
| 56 | public static final short LABEL_RANGE_OBJ_MINIMUM_LENGTH = 12;
|
| 57 | public static final int MINIMUM_COMMON_HEADER_LENGTH = 4;
|
| 58 | //P flag and I flag must be set to 0
|
| 59 | static final PcepObjectHeader DEFAULT_LABELRANGE_OBJECT_HEADER = new PcepObjectHeader(LABEL_RANGE_OBJ_CLASS,
|
| 60 | LABEL_RANGE_OBJ_TYPE, PcepObjectHeader.REQ_OBJ_OPTIONAL_PROCESS, PcepObjectHeader.RSP_OBJ_PROCESSED,
|
| 61 | LABEL_RANGE_OBJ_MINIMUM_LENGTH);
|
| 62 |
|
| 63 | private PcepObjectHeader labelRangeObjHeader;
|
| 64 | private byte labelType;
|
| 65 | private int rangeSize;
|
| 66 | private int labelBase;
|
| 67 | //Optional TLV
|
| 68 | private LinkedList<PcepValueType> llOptionalTlv;
|
| 69 |
|
| 70 | /**
|
| 71 | * Constructor to initialize parameters for PCEP label range object.
|
| 72 | *
|
| 73 | * @param labelRangeObjHeader label range object header
|
| 74 | * @param labelType label type
|
| 75 | * @param rangeSize range size
|
| 76 | * @param labelBase label base
|
| 77 | * @param llOptionalTlv list of optional tlvs
|
| 78 | */
|
| 79 | public PcepLabelRangeObjectVer1(PcepObjectHeader labelRangeObjHeader, byte labelType, int rangeSize, int labelBase,
|
| 80 | LinkedList<PcepValueType> llOptionalTlv) {
|
| 81 | this.labelRangeObjHeader = labelRangeObjHeader;
|
| 82 | this.labelType = labelType;
|
| 83 | this.rangeSize = rangeSize;
|
| 84 | this.llOptionalTlv = llOptionalTlv;
|
| 85 | this.labelBase = labelBase;
|
| 86 | }
|
| 87 |
|
| 88 | @Override
|
| 89 | public void setLabelRangeObjHeader(PcepObjectHeader obj) {
|
| 90 | this.labelRangeObjHeader = obj;
|
| 91 | }
|
| 92 |
|
| 93 | @Override
|
| 94 | public void setLabelType(byte labelType) {
|
| 95 | this.labelType = labelType;
|
| 96 | }
|
| 97 |
|
| 98 | @Override
|
| 99 | public void setRangeSize(int rangeSize) {
|
| 100 | this.rangeSize = rangeSize;
|
| 101 | }
|
| 102 |
|
| 103 | @Override
|
| 104 | public void setLabelBase(int labelBase) {
|
| 105 | this.labelBase = labelBase;
|
| 106 | }
|
| 107 |
|
| 108 | @Override
|
| 109 | public PcepObjectHeader getLabelRangeObjHeader() {
|
| 110 | return this.labelRangeObjHeader;
|
| 111 | }
|
| 112 |
|
| 113 | @Override
|
| 114 | public byte getLabelType() {
|
| 115 | return this.labelType;
|
| 116 | }
|
| 117 |
|
| 118 | @Override
|
| 119 | public int getRangeSize() {
|
| 120 | return this.rangeSize;
|
| 121 | }
|
| 122 |
|
| 123 | @Override
|
| 124 | public int getLabelBase() {
|
| 125 | return this.labelBase;
|
| 126 | }
|
| 127 |
|
| 128 | /**
|
| 129 | * Reads from the channel buffer and returns object of PcepLabelRangeObject.
|
| 130 | *
|
| 131 | * @param cb of type channel buffer
|
| 132 | * @return object of PcepLabelRangeObject
|
| 133 | * @throws PcepParseException when fails to read from channel buffer
|
| 134 | */
|
| 135 | public static PcepLabelRangeObject read(ChannelBuffer cb) throws PcepParseException {
|
| 136 |
|
| 137 | PcepObjectHeader labelRangeObjHeader;
|
| 138 | byte labelType;
|
| 139 | int rangeSize;
|
| 140 | int labelBase;
|
| 141 |
|
| 142 | LinkedList<PcepValueType> llOptionalTlv = new LinkedList<PcepValueType>();
|
| 143 |
|
| 144 | labelRangeObjHeader = PcepObjectHeader.read(cb);
|
| 145 |
|
| 146 | //take only LabelRangeObject buffer.
|
| 147 | ChannelBuffer tempCb = cb.readBytes(labelRangeObjHeader.getObjLen() - MINIMUM_COMMON_HEADER_LENGTH);
|
| 148 | int temp = 0;
|
| 149 | temp = tempCb.readInt();
|
| 150 | rangeSize = temp & 0x00FFFFFF;
|
| 151 | labelType = (byte) (temp >> 24);
|
| 152 | labelBase = tempCb.readInt();
|
| 153 | llOptionalTlv = parseOptionalTlv(tempCb);
|
| 154 | return new PcepLabelRangeObjectVer1(labelRangeObjHeader, labelType, rangeSize, labelBase, llOptionalTlv);
|
| 155 | }
|
| 156 |
|
| 157 | @Override
|
| 158 | public int write(ChannelBuffer cb) throws PcepParseException {
|
| 159 |
|
| 160 | int objStartIndex = cb.writerIndex();
|
| 161 |
|
| 162 | //write common header
|
| 163 | int objLenIndex = labelRangeObjHeader.write(cb);
|
| 164 | int temp = 0;
|
| 165 | temp = labelType;
|
| 166 | temp = temp << 24;
|
| 167 | temp = temp | rangeSize;
|
| 168 | cb.writeInt(temp);
|
| 169 |
|
| 170 | // Add optional TLV
|
| 171 | if (!packOptionalTlv(cb)) {
|
| 172 | throw new PcepParseException("Error while writing Optional tlv.");
|
| 173 | }
|
| 174 |
|
| 175 | //now write LabelRange Object Length
|
| 176 | cb.setShort(objLenIndex, (short) (cb.writerIndex() - objStartIndex));
|
| 177 | return cb.writerIndex() - objStartIndex;
|
| 178 | }
|
| 179 |
|
| 180 | /**
|
| 181 | * Returns list of optional tlvs.
|
| 182 | *
|
| 183 | * @param cb of type channle buffer
|
| 184 | * @return list of optional tlvs
|
| 185 | * @throws PcepParseException whne fails to parse list of optional tlvs
|
| 186 | */
|
| 187 | public static LinkedList<PcepValueType> parseOptionalTlv(ChannelBuffer cb) throws PcepParseException {
|
| 188 |
|
| 189 | LinkedList<PcepValueType> llOutOptionalTlv = new LinkedList<PcepValueType>();
|
| 190 |
|
| 191 | while (MINIMUM_COMMON_HEADER_LENGTH <= cb.readableBytes()) {
|
| 192 |
|
| 193 | PcepValueType tlv;
|
| 194 | int iValue;
|
| 195 | short hType = cb.readShort();
|
| 196 | short hLength = cb.readShort();
|
| 197 |
|
| 198 | switch (hType) {
|
| 199 |
|
| 200 | case PathSetupTypeTlv.TYPE:
|
| 201 | iValue = cb.readInt();
|
| 202 | tlv = new PathSetupTypeTlv(iValue);
|
| 203 | break;
|
| 204 |
|
| 205 | default:
|
| 206 | throw new PcepParseException("Unsupported TLV in LabelRange Object.");
|
| 207 | }
|
| 208 |
|
| 209 | // Check for the padding
|
| 210 | int pad = hLength % 4;
|
| 211 | if (0 < pad) {
|
| 212 | pad = 4 - pad;
|
| 213 | if (pad <= cb.readableBytes()) {
|
| 214 | cb.skipBytes(pad);
|
| 215 | }
|
| 216 | }
|
| 217 | llOutOptionalTlv.add(tlv);
|
| 218 | }
|
| 219 | return llOutOptionalTlv;
|
| 220 | }
|
| 221 |
|
| 222 | /**
|
| 223 | * Pack optional tlvs.
|
| 224 | *
|
| 225 | * @param cb of channel buffer
|
| 226 | * @return true
|
| 227 | */
|
| 228 | protected boolean packOptionalTlv(ChannelBuffer cb) {
|
| 229 |
|
| 230 | ListIterator<PcepValueType> listIterator = llOptionalTlv.listIterator();
|
| 231 |
|
| 232 | while (listIterator.hasNext()) {
|
| 233 | PcepValueType tlv = listIterator.next();
|
| 234 |
|
| 235 | if (null == tlv) {
|
| 236 | log.debug("tlv is null from OptionalTlv list");
|
| 237 | continue;
|
| 238 | }
|
| 239 | tlv.write(cb);
|
| 240 |
|
| 241 | // need to take care of padding
|
| 242 | int pad = tlv.getLength() % 4;
|
| 243 | if (0 != pad) {
|
| 244 | pad = 4 - pad;
|
| 245 | for (int i = 0; i < pad; ++i) {
|
| 246 | cb.writeByte((byte) 0);
|
| 247 | }
|
| 248 | }
|
| 249 | }
|
| 250 | return true;
|
| 251 | }
|
| 252 |
|
| 253 | /**
|
| 254 | * Builder class for PCEP label range object.
|
| 255 | */
|
| 256 | public static class Builder implements PcepLabelRangeObject.Builder {
|
| 257 | private boolean bIsHeaderSet = false;
|
| 258 | private boolean bIsLabelType = false;
|
| 259 | private boolean bIsRangeSize = false;
|
| 260 | private boolean bIsLabelBase = false;
|
| 261 |
|
| 262 | byte labelType;
|
| 263 | int rangeSize;
|
| 264 | int labelBase;
|
| 265 | private boolean bIsPFlagSet = false;
|
| 266 | private boolean bPFlag;
|
| 267 |
|
| 268 | private boolean bIsIFlagSet = false;
|
| 269 | private boolean bIFlag;
|
| 270 | private PcepObjectHeader labelRangeObjHeader;
|
| 271 |
|
| 272 | LinkedList<PcepValueType> llOptionalTlv = new LinkedList<PcepValueType>();
|
| 273 |
|
| 274 | @Override
|
| 275 | public PcepLabelRangeObject build() throws PcepParseException {
|
| 276 | PcepObjectHeader labelRangeObjHeader = this.bIsHeaderSet ? this.labelRangeObjHeader
|
| 277 | : DEFAULT_LABELRANGE_OBJECT_HEADER;
|
| 278 |
|
| 279 | if (!this.bIsLabelType) {
|
| 280 | throw new PcepParseException("LabelType NOT Set while building label range object.");
|
| 281 | }
|
| 282 |
|
| 283 | if (!this.bIsRangeSize) {
|
| 284 | throw new PcepParseException("RangeSize NOT Set while building label range object.");
|
| 285 | }
|
| 286 |
|
| 287 | if (!this.bIsLabelBase) {
|
| 288 | throw new PcepParseException("LabelBase NOT Set while building label range object.");
|
| 289 | }
|
| 290 |
|
| 291 | if (bIsPFlagSet) {
|
| 292 | labelRangeObjHeader.setPFlag(bPFlag);
|
| 293 | }
|
| 294 |
|
| 295 | if (bIsIFlagSet) {
|
| 296 | labelRangeObjHeader.setIFlag(bIFlag);
|
| 297 | }
|
| 298 | return new PcepLabelRangeObjectVer1(labelRangeObjHeader, this.labelType, this.rangeSize, this.labelBase,
|
| 299 | this.llOptionalTlv);
|
| 300 | }
|
| 301 |
|
| 302 | @Override
|
| 303 | public PcepObjectHeader getLabelRangeObjHeader() {
|
| 304 | return this.labelRangeObjHeader;
|
| 305 | }
|
| 306 |
|
| 307 | @Override
|
| 308 | public Builder setLabelRangeObjHeader(PcepObjectHeader obj) {
|
| 309 | this.labelRangeObjHeader = obj;
|
| 310 | this.bIsHeaderSet = true;
|
| 311 | return this;
|
| 312 | }
|
| 313 |
|
| 314 | @Override
|
| 315 | public byte getLabelType() {
|
| 316 | return this.labelType;
|
| 317 | }
|
| 318 |
|
| 319 | @Override
|
| 320 | public Builder setLabelType(byte labelType) {
|
| 321 | this.labelType = labelType;
|
| 322 | this.bIsLabelType = true;
|
| 323 | return this;
|
| 324 | }
|
| 325 |
|
| 326 | @Override
|
| 327 | public int getRangeSize() {
|
| 328 | return this.rangeSize;
|
| 329 | }
|
| 330 |
|
| 331 | @Override
|
| 332 | public Builder setRangeSize(int rangeSize) {
|
| 333 | this.rangeSize = rangeSize;
|
| 334 | this.bIsRangeSize = true;
|
| 335 | return this;
|
| 336 | }
|
| 337 |
|
| 338 | @Override
|
| 339 | public int getLabelBase() {
|
| 340 | return this.labelBase;
|
| 341 | }
|
| 342 |
|
| 343 | @Override
|
| 344 | public Builder setLabelBase(int labelBase) {
|
| 345 | this.labelBase = labelBase;
|
| 346 | this.bIsLabelBase = true;
|
| 347 | return this;
|
| 348 | }
|
| 349 |
|
| 350 | @Override
|
| 351 | public Builder setPFlag(boolean value) {
|
| 352 | this.bPFlag = value;
|
| 353 | this.bIsPFlagSet = true;
|
| 354 | return this;
|
| 355 | }
|
| 356 |
|
| 357 | @Override
|
| 358 | public Builder setIFlag(boolean value) {
|
| 359 | this.bIFlag = value;
|
| 360 | this.bIsIFlagSet = true;
|
| 361 | return this;
|
| 362 | }
|
| 363 | }
|
| 364 |
|
| 365 | @Override
|
| 366 | public String toString() {
|
| 367 | return MoreObjects.toStringHelper(getClass()).add("LabelType", labelType).add("rangeSize", rangeSize)
|
| 368 | .add("labelBase", labelBase).add("optionalTlvList", llOptionalTlv).toString();
|
| 369 | }
|
| 370 | }
|