Bharat saraswal | 3077717 | 2015-08-21 15:51:10 +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.PcepLabelObject; |
| 25 | import org.onosproject.pcepio.types.NexthopIPv4addressTlv; |
| 26 | import org.onosproject.pcepio.types.NexthopIPv6addressTlv; |
| 27 | import org.onosproject.pcepio.types.NexthopUnnumberedIPv4IDTlv; |
| 28 | import org.onosproject.pcepio.types.PcepObjectHeader; |
| 29 | import org.onosproject.pcepio.types.PcepValueType; |
| 30 | import org.slf4j.Logger; |
| 31 | import org.slf4j.LoggerFactory; |
| 32 | |
| 33 | import com.google.common.base.MoreObjects; |
| 34 | |
Phanendra Manda | 51fb9c2 | 2015-09-01 16:17:41 +0530 | [diff] [blame] | 35 | /** |
| 36 | * Provides PCEP label object. |
| 37 | */ |
| 38 | public class PcepLabelObjectVer1 implements PcepLabelObject { |
| 39 | |
| 40 | /* |
| 41 | * ref : draft-zhao-pce-pcep-extension-for-pce-controller-01 , section : 7.4. |
Bharat saraswal | 3077717 | 2015-08-21 15:51:10 +0530 | [diff] [blame] | 42 | |
| 43 | 0 1 2 3 |
| 44 | 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 |
| 45 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 46 | | Reserved | Flags |O| |
| 47 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 48 | | Label | |
| 49 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 50 | | | |
| 51 | // Optional TLV // |
| 52 | | | |
| 53 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 54 | The LABEL Object format |
Phanendra Manda | 51fb9c2 | 2015-09-01 16:17:41 +0530 | [diff] [blame] | 55 | */ |
Bharat saraswal | 3077717 | 2015-08-21 15:51:10 +0530 | [diff] [blame] | 56 | protected static final Logger log = LoggerFactory.getLogger(PcepLspObjectVer1.class); |
| 57 | |
| 58 | public static final byte LABEL_OBJ_TYPE = 1; |
| 59 | public static final byte LABEL_OBJ_CLASS = 35; //TBD : to be defined |
| 60 | public static final byte LABEL_OBJECT_VERSION = 1; |
| 61 | public static final byte OBJECT_HEADER_LENGTH = 4; |
| 62 | public static final boolean DEFAULT_OFLAG = false; |
| 63 | |
| 64 | // LSP_OBJ_MINIMUM_LENGTH = CommonHeaderLen(4)+ LspObjectHeaderLen(8) |
| 65 | public static final short LABEL_OBJ_MINIMUM_LENGTH = 12; |
| 66 | |
| 67 | public static final int OFLAG_SET = 1; |
| 68 | public static final int OFLAG_RESET = 0; |
| 69 | public static final int MINIMUM_COMMON_HEADER_LENGTH = 4; |
| 70 | |
| 71 | static final PcepObjectHeader DEFAULT_LABEL_OBJECT_HEADER = new PcepObjectHeader(LABEL_OBJ_CLASS, LABEL_OBJ_TYPE, |
| 72 | PcepObjectHeader.REQ_OBJ_OPTIONAL_PROCESS, PcepObjectHeader.RSP_OBJ_PROCESSED, LABEL_OBJ_MINIMUM_LENGTH); |
| 73 | |
| 74 | private PcepObjectHeader labelObjHeader; |
| 75 | private boolean bOFlag; |
| 76 | private int label; |
| 77 | // Optional TLV |
| 78 | private LinkedList<PcepValueType> llOptionalTlv; |
| 79 | |
| 80 | /** |
| 81 | * Constructor to initialize parameters for PCEP label object. |
| 82 | * |
| 83 | * @param labelObjHeader label object header |
| 84 | * @param bOFlag O flag |
| 85 | * @param label label |
| 86 | * @param llOptionalTlv list of optional tlvs |
| 87 | */ |
| 88 | public PcepLabelObjectVer1(PcepObjectHeader labelObjHeader, boolean bOFlag, int label, |
| 89 | LinkedList<PcepValueType> llOptionalTlv) { |
| 90 | this.labelObjHeader = labelObjHeader; |
| 91 | this.bOFlag = bOFlag; |
| 92 | this.label = label; |
| 93 | this.llOptionalTlv = llOptionalTlv; |
| 94 | } |
| 95 | |
| 96 | @Override |
| 97 | public LinkedList<PcepValueType> getOptionalTlv() { |
| 98 | return this.llOptionalTlv; |
| 99 | } |
| 100 | |
| 101 | @Override |
| 102 | public void setOptionalTlv(LinkedList<PcepValueType> llOptionalTlv) { |
| 103 | this.llOptionalTlv = llOptionalTlv; |
| 104 | } |
| 105 | |
| 106 | @Override |
| 107 | public boolean getOFlag() { |
| 108 | return this.bOFlag; |
| 109 | } |
| 110 | |
| 111 | @Override |
| 112 | public void setOFlag(boolean value) { |
| 113 | this.bOFlag = value; |
| 114 | } |
| 115 | |
| 116 | @Override |
| 117 | public int getLabel() { |
| 118 | return this.label; |
| 119 | } |
| 120 | |
| 121 | @Override |
| 122 | public void setLabel(int value) { |
| 123 | this.label = value; |
| 124 | } |
| 125 | |
| 126 | /** |
| 127 | * Reads form channel buffer and returns objects of PcepLabelObject. |
| 128 | * |
| 129 | * @param cb of type channel buffer |
| 130 | * @return objects of PcepLabelObject |
| 131 | * @throws PcepParseException when fails to read from channel buffer |
| 132 | */ |
| 133 | public static PcepLabelObject read(ChannelBuffer cb) throws PcepParseException { |
| 134 | |
| 135 | PcepObjectHeader labelObjHeader; |
| 136 | |
| 137 | boolean bOFlag; |
| 138 | int label; |
| 139 | |
| 140 | // Optional TLV |
| 141 | LinkedList<PcepValueType> llOptionalTlv = new LinkedList<PcepValueType>(); |
| 142 | labelObjHeader = PcepObjectHeader.read(cb); |
| 143 | |
| 144 | //take only LspObject buffer. |
| 145 | ChannelBuffer tempCb = cb.readBytes(labelObjHeader.getObjLen() - OBJECT_HEADER_LENGTH); |
| 146 | |
| 147 | int iTemp = tempCb.readInt(); |
| 148 | bOFlag = (iTemp & (byte) 0x01) == 1 ? true : false; |
| 149 | label = tempCb.readInt(); |
| 150 | |
| 151 | // parse optional TLV |
| 152 | llOptionalTlv = parseOptionalTlv(tempCb); |
| 153 | return new PcepLabelObjectVer1(labelObjHeader, bOFlag, label, llOptionalTlv); |
| 154 | } |
| 155 | |
| 156 | @Override |
| 157 | public int write(ChannelBuffer cb) throws PcepParseException { |
| 158 | |
| 159 | //write Object header |
| 160 | int objStartIndex = cb.writerIndex(); |
| 161 | int objLenIndex = labelObjHeader.write(cb); |
| 162 | |
| 163 | if (objLenIndex <= 0) { |
| 164 | throw new PcepParseException(" ObjectLength Index is " + objLenIndex); |
| 165 | } |
| 166 | |
| 167 | byte oFlag; |
| 168 | |
| 169 | oFlag = (byte) ((bOFlag) ? OFLAG_SET : OFLAG_RESET); |
| 170 | cb.writeInt(oFlag); |
| 171 | cb.writeInt(label); |
| 172 | |
| 173 | // Add optional TLV |
| 174 | packOptionalTlv(cb); |
| 175 | |
| 176 | //Update object length now |
| 177 | int length = cb.writerIndex() - objStartIndex; |
| 178 | |
| 179 | //will be helpful during print(). |
| 180 | labelObjHeader.setObjLen((short) length); |
| 181 | cb.setShort(objLenIndex, (short) length); |
| 182 | return cb.writerIndex(); |
| 183 | } |
| 184 | |
| 185 | /** |
| 186 | * Returns list of optional tlvs. |
| 187 | * |
| 188 | * @param cb of type channel buffer |
| 189 | * @return list of optional tlvs. |
| 190 | * @throws PcepParseException when fails to parse list of optional tlvs |
| 191 | */ |
| 192 | protected static LinkedList<PcepValueType> parseOptionalTlv(ChannelBuffer cb) throws PcepParseException { |
| 193 | |
| 194 | LinkedList<PcepValueType> llOutOptionalTlv = new LinkedList<PcepValueType>(); |
| 195 | |
| 196 | while (MINIMUM_COMMON_HEADER_LENGTH <= cb.readableBytes()) { |
| 197 | |
| 198 | PcepValueType tlv; |
| 199 | short hType = cb.readShort(); |
| 200 | short hLength = cb.readShort(); |
| 201 | int iValue = 0; |
| 202 | |
| 203 | switch (hType) { |
| 204 | |
| 205 | case NexthopIPv4addressTlv.TYPE: |
| 206 | iValue = cb.readInt(); |
| 207 | tlv = new NexthopIPv4addressTlv(iValue); |
| 208 | break; |
| 209 | case NexthopIPv6addressTlv.TYPE: |
| 210 | byte[] ipv6Value = new byte[NexthopIPv6addressTlv.VALUE_LENGTH]; |
| 211 | cb.readBytes(ipv6Value, 0, NexthopIPv6addressTlv.VALUE_LENGTH); |
| 212 | tlv = new NexthopIPv6addressTlv(ipv6Value); |
| 213 | break; |
| 214 | case NexthopUnnumberedIPv4IDTlv.TYPE: |
| 215 | tlv = NexthopUnnumberedIPv4IDTlv.read(cb); |
| 216 | break; |
| 217 | default: |
| 218 | throw new PcepParseException("Unsupported TLV type :" + hType); |
| 219 | } |
| 220 | |
| 221 | // Check for the padding |
| 222 | int pad = hLength % 4; |
| 223 | if (0 < pad) { |
| 224 | pad = 4 - pad; |
| 225 | if (pad <= cb.readableBytes()) { |
| 226 | cb.skipBytes(pad); |
| 227 | } |
| 228 | } |
| 229 | |
| 230 | llOutOptionalTlv.add(tlv); |
| 231 | } |
| 232 | |
| 233 | if (0 < cb.readableBytes()) { |
| 234 | |
| 235 | throw new PcepParseException("Optional Tlv parsing error. Extra bytes received."); |
| 236 | } |
| 237 | return llOutOptionalTlv; |
| 238 | } |
| 239 | |
| 240 | /** |
| 241 | * Returns the writer index. |
| 242 | * |
| 243 | * @param cb of channel buffer. |
| 244 | * @return writer index |
| 245 | */ |
| 246 | protected int packOptionalTlv(ChannelBuffer cb) { |
| 247 | |
| 248 | ListIterator<PcepValueType> listIterator = llOptionalTlv.listIterator(); |
| 249 | |
| 250 | while (listIterator.hasNext()) { |
| 251 | PcepValueType tlv = listIterator.next(); |
| 252 | |
| 253 | if (null == tlv) { |
| 254 | log.debug("tlv is null from OptionalTlv list"); |
| 255 | continue; |
| 256 | } |
| 257 | tlv.write(cb); |
| 258 | } |
| 259 | return cb.writerIndex(); |
| 260 | } |
| 261 | |
| 262 | /** |
| 263 | * Builder class for PCEP label object. |
| 264 | */ |
| 265 | public static class Builder implements PcepLabelObject.Builder { |
| 266 | |
| 267 | private boolean bIsHeaderSet = false; |
| 268 | private boolean bIsOFlagSet = false; |
| 269 | private boolean bIsLabelSet = false; |
| 270 | |
| 271 | private PcepObjectHeader labelObjHeader; |
| 272 | private boolean bOFlag; |
| 273 | private int label; |
| 274 | |
| 275 | LinkedList<PcepValueType> llOptionalTlv = new LinkedList<PcepValueType>(); |
| 276 | |
| 277 | private boolean bIsPFlagSet = false; |
| 278 | private boolean bPFlag; |
| 279 | |
| 280 | private boolean bIsIFlagSet = false; |
| 281 | private boolean bIFlag; |
| 282 | |
| 283 | @Override |
| 284 | public PcepLabelObject build() throws PcepParseException { |
| 285 | PcepObjectHeader labelObjHeader = this.bIsHeaderSet ? this.labelObjHeader : DEFAULT_LABEL_OBJECT_HEADER; |
| 286 | boolean bOFlag = this.bIsOFlagSet ? this.bOFlag : DEFAULT_OFLAG; |
| 287 | |
| 288 | if (!this.bIsLabelSet) { |
| 289 | throw new PcepParseException(" Label NOT Set while building PcepLabelObject."); |
| 290 | } |
| 291 | if (bIsPFlagSet) { |
| 292 | labelObjHeader.setPFlag(bPFlag); |
| 293 | } |
| 294 | if (bIsIFlagSet) { |
| 295 | labelObjHeader.setIFlag(bIFlag); |
| 296 | } |
| 297 | return new PcepLabelObjectVer1(labelObjHeader, bOFlag, this.label, this.llOptionalTlv); |
| 298 | } |
| 299 | |
| 300 | @Override |
| 301 | public PcepObjectHeader getLabelObjHeader() { |
| 302 | return this.labelObjHeader; |
| 303 | } |
| 304 | |
| 305 | @Override |
| 306 | public Builder setLabelObjHeader(PcepObjectHeader obj) { |
| 307 | this.labelObjHeader = obj; |
| 308 | this.bIsHeaderSet = true; |
| 309 | return this; |
| 310 | } |
| 311 | |
| 312 | @Override |
| 313 | public boolean getOFlag() { |
| 314 | return this.bOFlag; |
| 315 | } |
| 316 | |
| 317 | @Override |
| 318 | public Builder setOFlag(boolean value) { |
| 319 | this.bOFlag = value; |
| 320 | this.bIsOFlagSet = true; |
| 321 | return this; |
| 322 | } |
| 323 | |
| 324 | @Override |
| 325 | public int getLabel() { |
| 326 | return this.label; |
| 327 | } |
| 328 | |
| 329 | @Override |
| 330 | public Builder setLabel(int value) { |
| 331 | this.label = value; |
| 332 | this.bIsLabelSet = true; |
| 333 | return this; |
| 334 | } |
| 335 | |
| 336 | @Override |
| 337 | public LinkedList<PcepValueType> getOptionalTlv() { |
| 338 | return this.llOptionalTlv; |
| 339 | } |
| 340 | |
| 341 | @Override |
| 342 | public Builder setOptionalTlv(LinkedList<PcepValueType> llOptionalTlv) { |
| 343 | this.llOptionalTlv = llOptionalTlv; |
| 344 | return this; |
| 345 | } |
| 346 | |
| 347 | @Override |
| 348 | public Builder setPFlag(boolean value) { |
| 349 | this.bPFlag = value; |
| 350 | this.bIsPFlagSet = true; |
| 351 | return this; |
| 352 | } |
| 353 | |
| 354 | @Override |
| 355 | public Builder setIFlag(boolean value) { |
| 356 | this.bIFlag = value; |
| 357 | this.bIsIFlagSet = true; |
| 358 | return this; |
| 359 | } |
| 360 | } |
| 361 | |
| 362 | @Override |
| 363 | public String toString() { |
| 364 | return MoreObjects.toStringHelper(getClass()).add("OFlag", bOFlag).add("label", label) |
| 365 | .add("OptionalTlvList", llOptionalTlv).toString(); |
| 366 | } |
| 367 | } |