X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=blobdiff_plain;f=pcep%2Fspi%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fprotocol%2Fpcep%2Fspi%2FAbstractObjectWithTlvsParser.java;h=f6999e16ece31bc8a7c36fc4695708a00400ccdd;hb=1f18c032706004ce9bf0fcc648090ec5211b945a;hp=5a080c29a2e8036e3eaf9b245b746e709f160f5a;hpb=07d406116969ac1d96476f5d57c713d4c02a1c31;p=bgpcep.git diff --git a/pcep/spi/src/main/java/org/opendaylight/protocol/pcep/spi/AbstractObjectWithTlvsParser.java b/pcep/spi/src/main/java/org/opendaylight/protocol/pcep/spi/AbstractObjectWithTlvsParser.java index 5a080c29a2..f6999e16ec 100644 --- a/pcep/spi/src/main/java/org/opendaylight/protocol/pcep/spi/AbstractObjectWithTlvsParser.java +++ b/pcep/spi/src/main/java/org/opendaylight/protocol/pcep/spi/AbstractObjectWithTlvsParser.java @@ -7,72 +7,90 @@ */ package org.opendaylight.protocol.pcep.spi; +import static java.util.Objects.requireNonNull; + +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.Arrays; - -import org.opendaylight.protocol.util.ByteArray; -import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Tlv; +import java.util.List; +import java.util.Optional; +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 extends CommonObjectParser implements ObjectSerializer { -public abstract class AbstractObjectWithTlvsParser implements ObjectParser, ObjectSerializer { + private static final Logger LOG = LoggerFactory.getLogger(AbstractObjectWithTlvsParser.class); - private static final Logger LOG = LoggerFactory.getLogger(AbstractObjectWithTlvsParser.class); + private final TlvRegistry tlvReg; - 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; + private final VendorInformationTlvRegistry viTlvReg; - protected static final int PADDED_TO = 4; + 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); + } - private final TlvRegistry tlvReg; + protected final void parseTlvs(final T builder, final ByteBuf bytes) throws PCEPDeserializerException { + Preconditions.checkArgument(bytes != null, "Array of bytes is mandatory. Can't be null."); + if (!bytes.isReadable()) { + return; + } + final List viTlvs = Lists.newArrayList(); + 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)); - protected AbstractObjectWithTlvsParser(final TlvRegistry tlvReg) { - this.tlvReg = Preconditions.checkNotNull(tlvReg); - } + if (VendorInformationUtil.isVendorInformationTlv(type)) { + final EnterpriseNumber enterpriseNumber = new EnterpriseNumber(tlvBytes.readUnsignedInt()); + final Optional 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 parseTlvs(final T builder, final ByteBuf bytes) throws PCEPDeserializerException { - Preconditions.checkArgument(bytes != null, "Array of bytes is mandatory. Can't be null."); - if (!bytes.isReadable()) { - return; - } - while (bytes.isReadable()) { - int type = bytes.readUnsignedShort(); - 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); - LOG.trace("Attempt to parse tlv from bytes: {}", ByteBufUtil.hexDump(tlvBytes)); - //FIXME: switch to ByteBuf - final Tlv tlv = this.tlvReg.parseTlv(type, ByteArray.readAllBytes(tlvBytes)); - LOG.trace("Tlv was parsed. {}", tlv); - addTlv(builder, tlv); - bytes.readerIndex(bytes.readerIndex() + length + getPadding(TLV_HEADER_LENGTH + length, PADDED_TO)); - } - } + 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 final byte[] serializeTlv(final Tlv tlv) { - Preconditions.checkNotNull(tlv, "PCEP TLV is mandatory."); - LOG.trace("Serializing PCEP TLV {}", tlv); - final byte[] ret = this.tlvReg.serializeTlv(tlv); - if (ret == null) { - LOG.warn("TLV serializer for type {} could not be found.", tlv); - } - LOG.trace("Serialized PCEP TLV {}.", Arrays.toString(ret)); - return ret; - } + protected void addTlv(final T builder, final Tlv tlv) { + // FIXME: No TLVs by default, fallback to augments + } - protected void addTlv(final T builder, final Tlv tlv) { - // FIXME: No TLVs by default, fallback to augments - } + protected abstract void addVendorInformationTlvs(final T builder, final List tlvs); - public static int getPadding(final int length, final int padding) { - return (padding - (length % padding)) % padding; - } + protected final void serializeVendorInformationTlvs(final List 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)); + } + } + } }