Register PCEP session to stats handler only after it is fully initialized
[bgpcep.git] / pcep / spi / src / main / java / org / opendaylight / protocol / pcep / spi / AbstractObjectWithTlvsParser.java
index b4ec3f75b6e057ee0e005f5fe27c133027c99bd0..54414d26f86f8a9d021f450f687e63ff7cd30e7e 100644 (file)
@@ -7,84 +7,93 @@
  */
 package org.opendaylight.protocol.pcep.spi;
 
-import org.opendaylight.protocol.pcep.PCEPDeserializerException;
-import org.opendaylight.protocol.util.ByteArray;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Tlv;
+import static com.google.common.base.Preconditions.checkArgument;
+import static java.util.Objects.requireNonNull;
+
+import io.netty.buffer.ByteBuf;
+import io.netty.buffer.ByteBufUtil;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Optional;
+import org.opendaylight.protocol.util.ByteBufUtils;
+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.rev181109.Tlv;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.vendor.information.tlvs.VendorInformationTlv;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
-import com.google.common.base.Preconditions;
-
-public abstract class AbstractObjectWithTlvsParser<BUILDER> implements ObjectParser, ObjectSerializer {
-
-       private static final Logger logger = LoggerFactory.getLogger(AbstractObjectWithTlvsParser.class);
-
-       private static final int TLV_TYPE_F_LENGTH = 2;
-       private static final int TLV_LENGTH_F_LENGTH = 2;
-       private static final int TLV_HEADER_LENGTH = TLV_LENGTH_F_LENGTH + TLV_TYPE_F_LENGTH;
-
-       protected static final int PADDED_TO = 4;
-
-       private final TlvHandlerRegistry tlvReg;
-
-       protected AbstractObjectWithTlvsParser(final TlvHandlerRegistry tlvReg) {
-               this.tlvReg = Preconditions.checkNotNull(tlvReg);
-       }
-
-       protected final void parseTlvs(final BUILDER builder, final byte[] bytes) throws PCEPDeserializerException {
-               if (bytes == null) {
-                       throw new IllegalArgumentException("Byte array is mandatory.");
-               }
-
-               int length;
-               int byteOffset = 0;
-               int type = 0;
-
-               while (byteOffset < bytes.length) {
-                       type = ByteArray.bytesToInt(ByteArray.subByte(bytes, byteOffset, TLV_TYPE_F_LENGTH));
-                       byteOffset += TLV_TYPE_F_LENGTH;
-                       length = ByteArray.bytesToInt(ByteArray.subByte(bytes, byteOffset, TLV_LENGTH_F_LENGTH));
-                       byteOffset += TLV_LENGTH_F_LENGTH;
-
-                       if (TLV_HEADER_LENGTH + length > bytes.length - byteOffset) {
-                               throw new PCEPDeserializerException("Wrong length specified. Passed: " + (TLV_HEADER_LENGTH + length) + "; Expected: <= "
-                                               + (bytes.length - byteOffset) + ".");
-                       }
-
-                       final byte[] tlvBytes = ByteArray.subByte(bytes, byteOffset, length);
-
-                       logger.trace("Attempt to parse tlv from bytes: {}", ByteArray.bytesToHexString(tlvBytes));
-                       final Tlv tlv = this.tlvReg.getTlvParser(type).parseTlv(tlvBytes);
-                       logger.trace("Tlv was parsed. {}", tlv);
-
-                       addTlv(builder, tlv);
-
-                       byteOffset += length + getPadding(TLV_HEADER_LENGTH + length, PADDED_TO);
-               }
-       }
-
-       protected final byte[] serializeTlv(final Tlv tlv) {
-
-               final TlvSerializer serializer = this.tlvReg.getTlvSerializer(tlv);
-
-               final byte[] typeBytes = (ByteArray.cutBytes(ByteArray.intToBytes(serializer.getType()), (Integer.SIZE / 8) - TLV_TYPE_F_LENGTH));
-
-               final byte[] valueBytes = serializer.serializeTlv(tlv);
-
-               final byte[] lengthBytes = ByteArray.cutBytes(ByteArray.intToBytes(valueBytes.length), (Integer.SIZE / 8) - TLV_LENGTH_F_LENGTH);
-
-               final byte[] bytes = new byte[TLV_HEADER_LENGTH + valueBytes.length + getPadding(TLV_HEADER_LENGTH + valueBytes.length, PADDED_TO)];
-
-               int byteOffset = 0;
-               System.arraycopy(typeBytes, 0, bytes, byteOffset, TLV_TYPE_F_LENGTH);
-               System.arraycopy(lengthBytes, 0, bytes, byteOffset += TLV_TYPE_F_LENGTH, TLV_LENGTH_F_LENGTH);
-               System.arraycopy(valueBytes, 0, bytes, byteOffset += TLV_LENGTH_F_LENGTH, valueBytes.length);
-               return bytes;
-       }
-
-       public abstract void addTlv(final BUILDER builder, final Tlv tlv);
-
-       private static int getPadding(final int length, final int padding) {
-               return (padding - (length % padding)) % padding;
-       }
+public abstract class AbstractObjectWithTlvsParser<T> extends CommonObjectParser implements ObjectSerializer {
+
+    private static final Logger LOG = LoggerFactory.getLogger(AbstractObjectWithTlvsParser.class);
+
+    private final TlvRegistry tlvReg;
+
+    private final VendorInformationTlvRegistry viTlvReg;
+
+    protected AbstractObjectWithTlvsParser(final TlvRegistry tlvReg, final VendorInformationTlvRegistry viTlvReg,
+        final int objectClass, final int objectType) {
+        super(objectClass, objectType);
+        this.tlvReg = requireNonNull(tlvReg);
+        this.viTlvReg = requireNonNull(viTlvReg);
+    }
+
+    protected final void parseTlvs(final T builder, final ByteBuf bytes) throws PCEPDeserializerException {
+        checkArgument(bytes != null, "Array of bytes is mandatory. Can't be null.");
+        if (!bytes.isReadable()) {
+            return;
+        }
+        final List<VendorInformationTlv> viTlvs = new ArrayList<>();
+        while (bytes.isReadable()) {
+            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.readSlice(length);
+            LOG.trace("Parsing PCEP TLV : {}", ByteBufUtil.hexDump(tlvBytes));
+
+            if (VendorInformationUtil.isVendorInformationTlv(type)) {
+                final EnterpriseNumber enterpriseNumber = new EnterpriseNumber(ByteBufUtils.readUint32(tlvBytes));
+                final Optional<VendorInformationTlv> viTlv = this.viTlvReg.parseVendorInformationTlv(enterpriseNumber,
+                    tlvBytes);
+                if (viTlv.isPresent()) {
+                    LOG.trace("Parsed VENDOR-INFORMATION TLV {}.", viTlv.get());
+                    viTlvs.add(viTlv.get());
+                }
+            } else {
+                final Tlv tlv = this.tlvReg.parseTlv(type, tlvBytes);
+                if (tlv != null) {
+                    LOG.trace("Parsed PCEP TLV {}.", tlv);
+                    addTlv(builder, tlv);
+                }
+            }
+            bytes.skipBytes(TlvUtil.getPadding(TlvUtil.HEADER_SIZE + length, TlvUtil.PADDED_TO));
+        }
+        addVendorInformationTlvs(builder, viTlvs);
+    }
+
+    protected final void serializeTlv(final Tlv tlv, final ByteBuf buffer) {
+        requireNonNull(tlv, "PCEP TLV is mandatory.");
+        LOG.trace("Serializing PCEP TLV {}", tlv);
+        this.tlvReg.serializeTlv(tlv, buffer);
+        LOG.trace("Serialized PCEP TLV : {}.", ByteBufUtil.hexDump(buffer));
+    }
+
+    protected void addTlv(final T builder, final Tlv tlv) {
+        // FIXME: No TLVs by default, fallback to augments
+    }
+
+    protected abstract void addVendorInformationTlvs(T builder, List<VendorInformationTlv> tlvs);
+
+    protected final void serializeVendorInformationTlvs(final List<VendorInformationTlv> tlvs, final ByteBuf buffer) {
+        if (tlvs != null) {
+            for (final VendorInformationTlv tlv : tlvs) {
+                LOG.trace("Serializing VENDOR-INFORMATION TLV {}", tlv);
+                this.viTlvReg.serializeVendorInformationTlv(tlv, buffer);
+                LOG.trace("Serialized VENDOR-INFORMATION TLV : {}.", ByteBufUtil.hexDump(buffer));
+            }
+        }
+    }
 }