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