BUG-5731: Send Error Message if LSP-IDENTIFIERS TLV is missing
[bgpcep.git] / pcep / spi / src / main / java / org / opendaylight / protocol / pcep / spi / AbstractObjectWithTlvsParser.java
index 69e891aa6ecbdf864886b5966cad03569d11a13f..3b5768716bdc229ebef6e89f7fc8bbfdee698a4c 100644 (file)
@@ -10,12 +10,9 @@ package org.opendaylight.protocol.pcep.spi;
 import com.google.common.base.Optional;
 import com.google.common.base.Preconditions;
 import com.google.common.collect.Lists;
-
 import io.netty.buffer.ByteBuf;
 import io.netty.buffer.ByteBufUtil;
-
 import java.util.List;
-
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.iana.rev130816.EnterpriseNumber;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Tlv;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.vendor.information.tlvs.VendorInformationTlv;
@@ -26,8 +23,6 @@ public abstract class AbstractObjectWithTlvsParser<T> implements ObjectParser, O
 
     private static final Logger LOG = LoggerFactory.getLogger(AbstractObjectWithTlvsParser.class);
 
-    public static final int PADDED_TO = 4;
-
     private final TlvRegistry tlvReg;
 
     private final VendorInformationTlvRegistry viTlvReg;
@@ -44,13 +39,13 @@ public abstract class AbstractObjectWithTlvsParser<T> implements ObjectParser, O
         }
         final List<VendorInformationTlv> viTlvs = Lists.newArrayList();
         while (bytes.isReadable()) {
-            int type = bytes.readUnsignedShort();
-            int length = bytes.readUnsignedShort();
+            final int type = bytes.readUnsignedShort();
+            final int length = bytes.readUnsignedShort();
             if (length > bytes.readableBytes()) {
                 throw new PCEPDeserializerException("Wrong length specified. Passed: " + length + "; Expected: <= " + bytes.readableBytes()
                     + ".");
             }
-            final ByteBuf tlvBytes = bytes.slice(bytes.readerIndex(), length);
+            final ByteBuf tlvBytes = bytes.readSlice(length);
             LOG.trace("Parsing PCEP TLV : {}", ByteBufUtil.hexDump(tlvBytes));
 
             if (VendorInformationUtil.isVendorInformationTlv(type)) {
@@ -67,7 +62,7 @@ public abstract class AbstractObjectWithTlvsParser<T> implements ObjectParser, O
                     addTlv(builder, tlv);
                 }
             }
-            bytes.skipBytes(length + TlvUtil.getPadding(TlvUtil.HEADER_SIZE + length, TlvUtil.PADDED_TO));
+            bytes.skipBytes(TlvUtil.getPadding(TlvUtil.HEADER_SIZE + length, TlvUtil.PADDED_TO));
         }
         addVendorInformationTlvs(builder, viTlvs);
     }
@@ -83,10 +78,10 @@ public abstract class AbstractObjectWithTlvsParser<T> implements ObjectParser, O
         // FIXME: No TLVs by default, fallback to augments
     }
 
-    abstract protected void addVendorInformationTlvs(final T builder, final List<VendorInformationTlv> tlvs);
+    protected abstract void addVendorInformationTlvs(final T builder, final List<VendorInformationTlv> tlvs);
 
     protected final void serializeVendorInformationTlvs(final List<VendorInformationTlv> tlvs, final ByteBuf buffer) {
-        if (tlvs != null && !tlvs.isEmpty()) {
+        if (tlvs != null) {
             for (final VendorInformationTlv tlv : tlvs) {
                 LOG.trace("Serializing VENDOR-INFORMATION TLV {}", tlv);
                 this.viTlvReg.serializeVendorInformationTlv(tlv, buffer);