Committed by
Thomas Vachuska
[ONOS-2359]Implementation of PCEP LabelReserve message
Change-Id: Ie74340f5fe5e93d9bb277dd3551ba175ac74494c
Showing
4 changed files
with
871 additions
and
0 deletions
pcep/pcepio/src/main/java/org/onosproject/pcepio/protocol/ver1/PcepLabelRangeObjectVer1.java
0 → 100644
| 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 | +} |
pcep/pcepio/src/main/java/org/onosproject/pcepio/protocol/ver1/PcepLabelRangeResvMsgVer1.java
0 → 100644
| 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.PcepLabelRange; | ||
| 22 | +import org.onosproject.pcepio.protocol.PcepLabelRangeResvMsg; | ||
| 23 | +import org.onosproject.pcepio.protocol.PcepMessageReader; | ||
| 24 | +import org.onosproject.pcepio.protocol.PcepMessageWriter; | ||
| 25 | +import org.onosproject.pcepio.protocol.PcepType; | ||
| 26 | +import org.onosproject.pcepio.protocol.PcepVersion; | ||
| 27 | +import org.slf4j.Logger; | ||
| 28 | +import org.slf4j.LoggerFactory; | ||
| 29 | + | ||
| 30 | +import com.google.common.base.MoreObjects; | ||
| 31 | + | ||
| 32 | +class PcepLabelRangeResvMsgVer1 implements PcepLabelRangeResvMsg { | ||
| 33 | + | ||
| 34 | + // Pcep version: 1 | ||
| 35 | + | ||
| 36 | + /* | ||
| 37 | + The format of a PCLRResv message is as follows: | ||
| 38 | + | ||
| 39 | + PCLRResv Message>::= <Common Header> | ||
| 40 | + <label-range> | ||
| 41 | + Where: | ||
| 42 | + | ||
| 43 | + <label-range> ::= <SRP> | ||
| 44 | + <labelrange-list> | ||
| 45 | + | ||
| 46 | + Where | ||
| 47 | + <labelrange-list>::=<LABEL-RANGE>[<labelrange-list>] | ||
| 48 | + */ | ||
| 49 | + protected static final Logger log = LoggerFactory.getLogger(PcepLabelRangeResvMsgVer1.class); | ||
| 50 | + | ||
| 51 | + public static final byte PACKET_VERSION = 1; | ||
| 52 | + // LabelRangeResvMsgMinLength = COMMON-HEADER(4)+SrpObjMinLentgh(12)+LABEL-RANGE-MIN-LENGTH(12) | ||
| 53 | + public static final int PACKET_MINIMUM_LENGTH = 28; | ||
| 54 | + public static final PcepType MSG_TYPE = PcepType.LABEL_RANGE_RESERV; | ||
| 55 | + //<label-range> | ||
| 56 | + PcepLabelRange labelRange; | ||
| 57 | + | ||
| 58 | + public static final PcepLabelRangeResvMsgVer1.Reader READER = new Reader(); | ||
| 59 | + | ||
| 60 | + //Reader reads LabelRangeResv Message from the channel. | ||
| 61 | + static class Reader implements PcepMessageReader<PcepLabelRangeResvMsg> { | ||
| 62 | + | ||
| 63 | + @Override | ||
| 64 | + public PcepLabelRangeResvMsg readFrom(ChannelBuffer cb) throws PcepParseException { | ||
| 65 | + | ||
| 66 | + if (cb.readableBytes() < PACKET_MINIMUM_LENGTH) { | ||
| 67 | + throw new PcepParseException("Channel buffer has less readable bytes than Packet minimum length."); | ||
| 68 | + } | ||
| 69 | + // fixed value property version == 1 | ||
| 70 | + byte version = cb.readByte(); | ||
| 71 | + version = (byte) (version >> PcepMessageVer1.SHIFT_FLAG); | ||
| 72 | + if (version != PACKET_VERSION) { | ||
| 73 | + throw new PcepParseException("Wrong version. Expected=PcepVersion.PCEP_1(1), got=" + version); | ||
| 74 | + } | ||
| 75 | + // fixed value property type == 15 | ||
| 76 | + byte type = cb.readByte(); | ||
| 77 | + if (type != MSG_TYPE.getType()) { | ||
| 78 | + throw new PcepParseException("Wrong type. Expected=PcepType.LABEL_RANGE_RESERV(15), got=" + type); | ||
| 79 | + } | ||
| 80 | + short length = cb.readShort(); | ||
| 81 | + if (length < PACKET_MINIMUM_LENGTH) { | ||
| 82 | + throw new PcepParseException("Wrong length.Expected to be >= " + PACKET_MINIMUM_LENGTH + ", is: " | ||
| 83 | + + length); | ||
| 84 | + } | ||
| 85 | + // parse <label-range> | ||
| 86 | + PcepLabelRange labelRange = PcepLabelRangeVer1.read(cb); | ||
| 87 | + return new PcepLabelRangeResvMsgVer1(labelRange); | ||
| 88 | + } | ||
| 89 | + } | ||
| 90 | + | ||
| 91 | + /** | ||
| 92 | + * Constructor to initialize PCEP label range. | ||
| 93 | + * | ||
| 94 | + * @param labelRange PCEP label range | ||
| 95 | + */ | ||
| 96 | + PcepLabelRangeResvMsgVer1(PcepLabelRange labelRange) { | ||
| 97 | + this.labelRange = labelRange; | ||
| 98 | + } | ||
| 99 | + | ||
| 100 | + /** | ||
| 101 | + * Builder class for PCEP label range reserve message. | ||
| 102 | + */ | ||
| 103 | + static class Builder implements PcepLabelRangeResvMsg.Builder { | ||
| 104 | + | ||
| 105 | + PcepLabelRange labelRange = new PcepLabelRangeVer1(); | ||
| 106 | + | ||
| 107 | + @Override | ||
| 108 | + public PcepVersion getVersion() { | ||
| 109 | + return PcepVersion.PCEP_1; | ||
| 110 | + } | ||
| 111 | + | ||
| 112 | + @Override | ||
| 113 | + public PcepType getType() { | ||
| 114 | + return PcepType.LABEL_RANGE_RESERV; | ||
| 115 | + } | ||
| 116 | + | ||
| 117 | + @Override | ||
| 118 | + public PcepLabelRangeResvMsg build() { | ||
| 119 | + return new PcepLabelRangeResvMsgVer1(this.labelRange); | ||
| 120 | + } | ||
| 121 | + | ||
| 122 | + @Override | ||
| 123 | + public PcepLabelRange getLabelRange() { | ||
| 124 | + return this.labelRange; | ||
| 125 | + } | ||
| 126 | + | ||
| 127 | + @Override | ||
| 128 | + public Builder setLabelRange(PcepLabelRange labelRange) { | ||
| 129 | + this.labelRange = labelRange; | ||
| 130 | + return this; | ||
| 131 | + } | ||
| 132 | + } | ||
| 133 | + | ||
| 134 | + @Override | ||
| 135 | + public void writeTo(ChannelBuffer cb) throws PcepParseException { | ||
| 136 | + WRITER.write(cb, this); | ||
| 137 | + } | ||
| 138 | + | ||
| 139 | + static final Writer WRITER = new Writer(); | ||
| 140 | + | ||
| 141 | + //Writer writes LabelRangeResv Message to the channel. | ||
| 142 | + static class Writer implements PcepMessageWriter<PcepLabelRangeResvMsgVer1> { | ||
| 143 | + | ||
| 144 | + @Override | ||
| 145 | + public void write(ChannelBuffer cb, PcepLabelRangeResvMsgVer1 message) throws PcepParseException { | ||
| 146 | + | ||
| 147 | + int startIndex = cb.writerIndex(); | ||
| 148 | + // first 3 bits set to version | ||
| 149 | + cb.writeByte((byte) (PACKET_VERSION << PcepMessageVer1.SHIFT_FLAG)); | ||
| 150 | + // message type | ||
| 151 | + cb.writeByte(MSG_TYPE.getType()); | ||
| 152 | + // Length will be set after calculating length, but currently set it as 0. | ||
| 153 | + int msgLenIndex = cb.writerIndex(); | ||
| 154 | + | ||
| 155 | + cb.writeShort((short) 0); | ||
| 156 | + //write Label Range | ||
| 157 | + message.labelRange.write(cb); | ||
| 158 | + | ||
| 159 | + // update message length field | ||
| 160 | + int length = cb.writerIndex() - startIndex; | ||
| 161 | + cb.setShort(msgLenIndex, (short) length); | ||
| 162 | + } | ||
| 163 | + } | ||
| 164 | + | ||
| 165 | + @Override | ||
| 166 | + public PcepVersion getVersion() { | ||
| 167 | + return PcepVersion.PCEP_1; | ||
| 168 | + } | ||
| 169 | + | ||
| 170 | + @Override | ||
| 171 | + public PcepType getType() { | ||
| 172 | + return MSG_TYPE; | ||
| 173 | + } | ||
| 174 | + | ||
| 175 | + @Override | ||
| 176 | + public PcepLabelRange getLabelRange() { | ||
| 177 | + return this.labelRange; | ||
| 178 | + } | ||
| 179 | + | ||
| 180 | + @Override | ||
| 181 | + public void setLabelRange(PcepLabelRange lr) { | ||
| 182 | + this.labelRange = lr; | ||
| 183 | + } | ||
| 184 | + | ||
| 185 | + @Override | ||
| 186 | + public String toString() { | ||
| 187 | + return MoreObjects.toStringHelper(getClass()).add("labelRange", labelRange).toString(); | ||
| 188 | + } | ||
| 189 | +} |
| 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.PcepLabelRange; | ||
| 25 | +import org.onosproject.pcepio.protocol.PcepLabelRangeObject; | ||
| 26 | +import org.onosproject.pcepio.protocol.PcepSrpObject; | ||
| 27 | +import org.slf4j.Logger; | ||
| 28 | +import org.slf4j.LoggerFactory; | ||
| 29 | + | ||
| 30 | +import com.google.common.base.MoreObjects; | ||
| 31 | + | ||
| 32 | +/** | ||
| 33 | + * Provides PCEP Label Range. | ||
| 34 | + */ | ||
| 35 | +public class PcepLabelRangeVer1 implements PcepLabelRange { | ||
| 36 | + | ||
| 37 | + protected static final Logger log = LoggerFactory.getLogger(PcepLabelRangeVer1.class); | ||
| 38 | + | ||
| 39 | + /* | ||
| 40 | + <label-range> ::= <SRP> | ||
| 41 | + <labelrange-list> | ||
| 42 | + Where | ||
| 43 | + <labelrange-list>::=<LABEL-RANGE>[<labelrange-list>] | ||
| 44 | + */ | ||
| 45 | + | ||
| 46 | + // PCEP SRP Object | ||
| 47 | + private PcepSrpObject srpObject; | ||
| 48 | + //<labelrange-list> of type PcepLabelRangeObject. | ||
| 49 | + private LinkedList<PcepLabelRangeObject> llLabelRangeList; | ||
| 50 | + | ||
| 51 | + /** | ||
| 52 | + * Default Constructor. | ||
| 53 | + */ | ||
| 54 | + public PcepLabelRangeVer1() { | ||
| 55 | + srpObject = null; | ||
| 56 | + llLabelRangeList = null; | ||
| 57 | + } | ||
| 58 | + | ||
| 59 | + /** | ||
| 60 | + * Constructor to initialize objects. | ||
| 61 | + * | ||
| 62 | + * @param srpObj PCEP Srp object. | ||
| 63 | + * @param llLabelRangeList list of PcepLabelRangeObject. | ||
| 64 | + */ | ||
| 65 | + PcepLabelRangeVer1(PcepSrpObject srpObj, LinkedList<PcepLabelRangeObject> llLabelRangeList) { | ||
| 66 | + this.srpObject = srpObj; | ||
| 67 | + this.llLabelRangeList = llLabelRangeList; | ||
| 68 | + } | ||
| 69 | + | ||
| 70 | + @Override | ||
| 71 | + public PcepSrpObject getSrpObject() { | ||
| 72 | + return srpObject; | ||
| 73 | + } | ||
| 74 | + | ||
| 75 | + @Override | ||
| 76 | + public void setSrpObject(PcepSrpObject srpObject) { | ||
| 77 | + this.srpObject = srpObject; | ||
| 78 | + | ||
| 79 | + } | ||
| 80 | + | ||
| 81 | + @Override | ||
| 82 | + public LinkedList<PcepLabelRangeObject> getLabelRangeList() { | ||
| 83 | + return llLabelRangeList; | ||
| 84 | + } | ||
| 85 | + | ||
| 86 | + @Override | ||
| 87 | + public void setLabelRangeList(LinkedList<PcepLabelRangeObject> ll) { | ||
| 88 | + this.llLabelRangeList = ll; | ||
| 89 | + } | ||
| 90 | + | ||
| 91 | + /** | ||
| 92 | + * Reads channel buffer and returns object of PcepLabelRange. | ||
| 93 | + * | ||
| 94 | + * @param cb of type channel buffer. | ||
| 95 | + * @return object of PcepLabelRange | ||
| 96 | + * @throws PcepParseException when fails to read from channel buffer | ||
| 97 | + */ | ||
| 98 | + public static PcepLabelRange read(ChannelBuffer cb) throws PcepParseException { | ||
| 99 | + | ||
| 100 | + //parse and store SRP mandatory object | ||
| 101 | + PcepSrpObject srpObj = null; | ||
| 102 | + srpObj = PcepSrpObjectVer1.read(cb); | ||
| 103 | + if (srpObj == null) { | ||
| 104 | + throw new PcepParseException("Exception while parsing srp object"); | ||
| 105 | + } | ||
| 106 | + | ||
| 107 | + LinkedList<PcepLabelRangeObject> llLabelRangeList = new LinkedList<PcepLabelRangeObject>(); | ||
| 108 | + boolean bFoundLabelRangeObj = false; | ||
| 109 | + while (0 < cb.readableBytes()) { | ||
| 110 | + //parse and store <labelrange-list> | ||
| 111 | + PcepLabelRangeObject lrObj; | ||
| 112 | + lrObj = PcepLabelRangeObjectVer1.read(cb); | ||
| 113 | + if (lrObj == null) { | ||
| 114 | + throw new PcepParseException("Exception while parsing label range object"); | ||
| 115 | + } else { | ||
| 116 | + llLabelRangeList.add(lrObj); | ||
| 117 | + bFoundLabelRangeObj = true; | ||
| 118 | + } | ||
| 119 | + } | ||
| 120 | + | ||
| 121 | + if (!bFoundLabelRangeObj) { | ||
| 122 | + throw new PcepParseException("At least one LABEL-RANGE MUST be present."); | ||
| 123 | + } | ||
| 124 | + return new PcepLabelRangeVer1(srpObj, llLabelRangeList); | ||
| 125 | + } | ||
| 126 | + | ||
| 127 | + @Override | ||
| 128 | + public int write(ChannelBuffer cb) throws PcepParseException { | ||
| 129 | + //write Object header | ||
| 130 | + int objStartIndex = cb.writerIndex(); | ||
| 131 | + | ||
| 132 | + //write <SRP> | ||
| 133 | + int objLenIndex = srpObject.write(cb); | ||
| 134 | + | ||
| 135 | + if (objLenIndex <= 0) { | ||
| 136 | + throw new PcepParseException("bjectLength is " + objLenIndex); | ||
| 137 | + } | ||
| 138 | + | ||
| 139 | + //write <labelrange-list> | ||
| 140 | + ListIterator<PcepLabelRangeObject> listIterator = llLabelRangeList.listIterator(); | ||
| 141 | + while (listIterator.hasNext()) { | ||
| 142 | + listIterator.next().write(cb); | ||
| 143 | + } | ||
| 144 | + | ||
| 145 | + //Update object length now | ||
| 146 | + int length = cb.writerIndex() - objStartIndex; | ||
| 147 | + // As per RFC the length of object should be | ||
| 148 | + // multiples of 4 | ||
| 149 | + int pad = length % 4; | ||
| 150 | + if (pad != 0) { | ||
| 151 | + pad = 4 - pad; | ||
| 152 | + for (int i = 0; i < pad; i++) { | ||
| 153 | + cb.writeByte((byte) 0); | ||
| 154 | + } | ||
| 155 | + length = length + pad; | ||
| 156 | + } | ||
| 157 | + cb.setShort(objLenIndex, (short) length); | ||
| 158 | + return length; | ||
| 159 | + } | ||
| 160 | + | ||
| 161 | + @Override | ||
| 162 | + public String toString() { | ||
| 163 | + return MoreObjects.toStringHelper(getClass()).add("srpObject", srpObject) | ||
| 164 | + .add("LabelRangeList", llLabelRangeList).toString(); | ||
| 165 | + } | ||
| 166 | +} |
| 1 | +package org.onosproject.pcepio.types; | ||
| 2 | + | ||
| 3 | +import java.util.Objects; | ||
| 4 | + | ||
| 5 | +import org.jboss.netty.buffer.ChannelBuffer; | ||
| 6 | +import org.onosproject.pcepio.protocol.PcepVersion; | ||
| 7 | +import org.slf4j.Logger; | ||
| 8 | +import org.slf4j.LoggerFactory; | ||
| 9 | + | ||
| 10 | +import com.google.common.base.MoreObjects; | ||
| 11 | + | ||
| 12 | +/** | ||
| 13 | + * Provides PcepSetup type tlv. | ||
| 14 | + */ | ||
| 15 | +public class PathSetupTypeTlv implements PcepValueType { | ||
| 16 | + | ||
| 17 | + /* | ||
| 18 | + Reference : draft-sivabalan-pce-lsp-setup-type-02. | ||
| 19 | + | ||
| 20 | + 0 1 2 3 | ||
| 21 | + 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 | ||
| 22 | + +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | ||
| 23 | + | Type | Length | | ||
| 24 | + +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | ||
| 25 | + | Reserved | PST | | ||
| 26 | + +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | ||
| 27 | + | ||
| 28 | + Figure 1: PATH-SETUP-TYPE TLV | ||
| 29 | + | ||
| 30 | + */ | ||
| 31 | + protected static final Logger log = LoggerFactory.getLogger(PathSetupTypeTlv.class); | ||
| 32 | + | ||
| 33 | + public static final short TYPE = 0; //TODO : need to reassign the value as per RFC | ||
| 34 | + public static final short LENGTH = 4; | ||
| 35 | + | ||
| 36 | + private final byte pst; | ||
| 37 | + private final int rawValue; | ||
| 38 | + private final boolean isRawValueSet; | ||
| 39 | + | ||
| 40 | + /** | ||
| 41 | + * Constructor to initialize parameters for path setup type tlv. | ||
| 42 | + * | ||
| 43 | + * @param rawValue parameter for path setup type tlv | ||
| 44 | + */ | ||
| 45 | + public PathSetupTypeTlv(final int rawValue) { | ||
| 46 | + this.rawValue = rawValue; | ||
| 47 | + this.isRawValueSet = true; | ||
| 48 | + this.pst = (byte) rawValue; | ||
| 49 | + } | ||
| 50 | + | ||
| 51 | + /** | ||
| 52 | + * Constructor to initialize pst. | ||
| 53 | + * | ||
| 54 | + * @param pst PST | ||
| 55 | + */ | ||
| 56 | + public PathSetupTypeTlv(byte pst) { | ||
| 57 | + this.pst = pst; | ||
| 58 | + this.rawValue = 0; | ||
| 59 | + this.isRawValueSet = false; | ||
| 60 | + } | ||
| 61 | + | ||
| 62 | + /** | ||
| 63 | + * Returns Object of path setup type tlv. | ||
| 64 | + * | ||
| 65 | + * @param raw parameter for path setup type tlv | ||
| 66 | + * @return object of PathSetupTypeTlv | ||
| 67 | + */ | ||
| 68 | + public static PathSetupTypeTlv of(final int raw) { | ||
| 69 | + return new PathSetupTypeTlv(raw); | ||
| 70 | + } | ||
| 71 | + | ||
| 72 | + @Override | ||
| 73 | + public PcepVersion getVersion() { | ||
| 74 | + return PcepVersion.PCEP_1; | ||
| 75 | + } | ||
| 76 | + | ||
| 77 | + /** | ||
| 78 | + * Returns parameters for path setup type tlv. | ||
| 79 | + * | ||
| 80 | + * @return parameters for path setup type tlv | ||
| 81 | + */ | ||
| 82 | + public int getInt() { | ||
| 83 | + return rawValue; | ||
| 84 | + } | ||
| 85 | + | ||
| 86 | + /** | ||
| 87 | + * Returns the pst value. | ||
| 88 | + * | ||
| 89 | + * @return pst value | ||
| 90 | + */ | ||
| 91 | + public byte getPst() { | ||
| 92 | + return pst; | ||
| 93 | + } | ||
| 94 | + | ||
| 95 | + @Override | ||
| 96 | + public short getType() { | ||
| 97 | + return TYPE; | ||
| 98 | + } | ||
| 99 | + | ||
| 100 | + @Override | ||
| 101 | + public short getLength() { | ||
| 102 | + return LENGTH; | ||
| 103 | + } | ||
| 104 | + | ||
| 105 | + @Override | ||
| 106 | + public int hashCode() { | ||
| 107 | + return Objects.hash(pst); | ||
| 108 | + } | ||
| 109 | + | ||
| 110 | + @Override | ||
| 111 | + public boolean equals(Object obj) { | ||
| 112 | + if (this == obj) { | ||
| 113 | + return true; | ||
| 114 | + } | ||
| 115 | + if (obj instanceof PathSetupTypeTlv) { | ||
| 116 | + PathSetupTypeTlv other = (PathSetupTypeTlv) obj; | ||
| 117 | + return Objects.equals(this.pst, other.pst); | ||
| 118 | + } | ||
| 119 | + return false; | ||
| 120 | + } | ||
| 121 | + | ||
| 122 | + @Override | ||
| 123 | + public int write(ChannelBuffer c) { | ||
| 124 | + int iLenStartIndex = c.writerIndex(); | ||
| 125 | + c.writeShort(TYPE); | ||
| 126 | + c.writeShort(LENGTH); | ||
| 127 | + c.writeInt(pst); | ||
| 128 | + return c.writerIndex() - iLenStartIndex; | ||
| 129 | + } | ||
| 130 | + | ||
| 131 | + /** | ||
| 132 | + * Returns the object of type PathSetupTypeTlv. | ||
| 133 | + * | ||
| 134 | + * @param c is type Channel buffer | ||
| 135 | + * @return object of PathSetupTypeTlv | ||
| 136 | + */ | ||
| 137 | + public static PathSetupTypeTlv read(ChannelBuffer c) { | ||
| 138 | + return PathSetupTypeTlv.of(c.readInt()); | ||
| 139 | + } | ||
| 140 | + | ||
| 141 | + @Override | ||
| 142 | + public String toString() { | ||
| 143 | + return MoreObjects.toStringHelper(getClass()).add("Type", TYPE).add("Length", LENGTH).add("PST", pst) | ||
| 144 | + .toString(); | ||
| 145 | + } | ||
| 146 | +} |
-
Please register or login to post a comment