2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.protocol.pcep.spi;
10 import static com.google.common.base.Preconditions.checkArgument;
11 import static java.util.Objects.requireNonNull;
13 import io.netty.buffer.ByteBuf;
14 import io.netty.buffer.ByteBufUtil;
15 import java.util.ArrayList;
16 import java.util.List;
17 import org.opendaylight.protocol.pcep.PCEPDeserializerException;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.iana.rev130816.EnterpriseNumber;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.Tlv;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.vendor.information.tlvs.VendorInformationTlv;
21 import org.opendaylight.yangtools.yang.common.netty.ByteBufUtils;
22 import org.slf4j.Logger;
23 import org.slf4j.LoggerFactory;
25 public abstract class AbstractObjectWithTlvsParser<T> extends CommonObjectParser implements ObjectSerializer {
26 private static final Logger LOG = LoggerFactory.getLogger(AbstractObjectWithTlvsParser.class);
28 private final TlvRegistry tlvReg;
29 private final VendorInformationTlvRegistry viTlvReg;
31 protected AbstractObjectWithTlvsParser(final TlvRegistry tlvReg, final VendorInformationTlvRegistry viTlvReg,
32 final int objectClass, final int objectType) {
33 super(objectClass, objectType);
34 this.tlvReg = requireNonNull(tlvReg);
35 this.viTlvReg = requireNonNull(viTlvReg);
38 protected final void parseTlvs(final T builder, final ByteBuf bytes) throws PCEPDeserializerException {
39 checkArgument(bytes != null, "Array of bytes is mandatory. Can't be null.");
40 if (!bytes.isReadable()) {
43 final var viTlvs = new ArrayList<VendorInformationTlv>();
44 while (bytes.isReadable()) {
45 final int type = bytes.readUnsignedShort();
46 final int length = bytes.readUnsignedShort();
47 if (length > bytes.readableBytes()) {
48 throw new PCEPDeserializerException("Wrong length specified. Passed: " + length + "; Expected: <= "
49 + bytes.readableBytes() + ".");
51 final var tlvBytes = bytes.readSlice(length);
52 LOG.trace("Parsing PCEP TLV : {}", ByteBufUtil.hexDump(tlvBytes));
54 if (VendorInformationUtil.isVendorInformationTlv(type)) {
55 final var enterpriseNumber = new EnterpriseNumber(ByteBufUtils.readUint32(tlvBytes));
56 viTlvReg.parseVendorInformationTlv(enterpriseNumber, tlvBytes).ifPresent(viTlv -> {
57 LOG.trace("Parsed VENDOR-INFORMATION TLV {}.", viTlv);
61 final Tlv tlv = tlvReg.parseTlv(type, tlvBytes);
63 LOG.trace("Parsed PCEP TLV {}.", tlv);
67 bytes.skipBytes(TlvUtil.getPadding(TlvUtil.HEADER_SIZE + length, TlvUtil.PADDED_TO));
69 addVendorInformationTlvs(builder, viTlvs);
72 protected final void serializeTlv(final Tlv tlv, final ByteBuf buffer) {
73 requireNonNull(tlv, "PCEP TLV is mandatory.");
74 LOG.trace("Serializing PCEP TLV {}", tlv);
75 tlvReg.serializeTlv(tlv, buffer);
76 LOG.trace("Serialized PCEP TLV : {}.", ByteBufUtil.hexDump(buffer));
79 protected void addTlv(final T builder, final Tlv tlv) {
80 // FIXME: No TLVs by default, fallback to augments
83 protected abstract void addVendorInformationTlvs(T builder, List<VendorInformationTlv> tlvs);
85 protected final void serializeVendorInformationTlvs(final List<VendorInformationTlv> tlvs, final ByteBuf buffer) {
87 for (var tlv : tlvs) {
88 LOG.trace("Serializing VENDOR-INFORMATION TLV {}", tlv);
89 viTlvReg.serializeVendorInformationTlv(tlv, buffer);
90 LOG.trace("Serialized VENDOR-INFORMATION TLV : {}.", ByteBufUtil.hexDump(buffer));