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 java.util.Objects.requireNonNull;
12 import com.google.common.base.Preconditions;
13 import com.google.common.collect.Lists;
14 import io.netty.buffer.ByteBuf;
15 import io.netty.buffer.ByteBufUtil;
16 import java.util.List;
17 import java.util.Optional;
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.slf4j.Logger;
22 import org.slf4j.LoggerFactory;
24 public abstract class AbstractObjectWithTlvsParser<T> extends CommonObjectParser implements ObjectSerializer {
26 private static final Logger LOG = LoggerFactory.getLogger(AbstractObjectWithTlvsParser.class);
28 private final TlvRegistry tlvReg;
30 private final VendorInformationTlvRegistry viTlvReg;
32 protected AbstractObjectWithTlvsParser(final TlvRegistry tlvReg, final VendorInformationTlvRegistry viTlvReg,
33 final int objectClass, final int objectType) {
34 super(objectClass, objectType);
35 this.tlvReg = requireNonNull(tlvReg);
36 this.viTlvReg = requireNonNull(viTlvReg);
39 protected final void parseTlvs(final T builder, final ByteBuf bytes) throws PCEPDeserializerException {
40 Preconditions.checkArgument(bytes != null, "Array of bytes is mandatory. Can't be null.");
41 if (!bytes.isReadable()) {
44 final List<VendorInformationTlv> viTlvs = Lists.newArrayList();
45 while (bytes.isReadable()) {
46 final int type = bytes.readUnsignedShort();
47 final int length = bytes.readUnsignedShort();
48 if (length > bytes.readableBytes()) {
49 throw new PCEPDeserializerException("Wrong length specified. Passed: " + length + "; Expected: <= " + bytes.readableBytes()
52 final ByteBuf tlvBytes = bytes.readSlice(length);
53 LOG.trace("Parsing PCEP TLV : {}", ByteBufUtil.hexDump(tlvBytes));
55 if (VendorInformationUtil.isVendorInformationTlv(type)) {
56 final EnterpriseNumber enterpriseNumber = new EnterpriseNumber(tlvBytes.readUnsignedInt());
57 final Optional<VendorInformationTlv> viTlv = this.viTlvReg.parseVendorInformationTlv(enterpriseNumber, tlvBytes);
58 if(viTlv.isPresent()) {
59 LOG.trace("Parsed VENDOR-INFORMATION TLV {}.", viTlv.get());
60 viTlvs.add(viTlv.get());
63 final Tlv tlv = this.tlvReg.parseTlv(type, tlvBytes);
65 LOG.trace("Parsed PCEP TLV {}.", tlv);
69 bytes.skipBytes(TlvUtil.getPadding(TlvUtil.HEADER_SIZE + length, TlvUtil.PADDED_TO));
71 addVendorInformationTlvs(builder, viTlvs);
74 protected final void serializeTlv(final Tlv tlv, final ByteBuf buffer) {
75 requireNonNull(tlv, "PCEP TLV is mandatory.");
76 LOG.trace("Serializing PCEP TLV {}", tlv);
77 this.tlvReg.serializeTlv(tlv, buffer);
78 LOG.trace("Serialized PCEP TLV : {}.", ByteBufUtil.hexDump(buffer));
81 protected void addTlv(final T builder, final Tlv tlv) {
82 // FIXME: No TLVs by default, fallback to augments
85 protected abstract void addVendorInformationTlvs(final T builder, final List<VendorInformationTlv> tlvs);
87 protected final void serializeVendorInformationTlvs(final List<VendorInformationTlv> tlvs, final ByteBuf buffer) {
89 for (final VendorInformationTlv tlv : tlvs) {
90 LOG.trace("Serializing VENDOR-INFORMATION TLV {}", tlv);
91 this.viTlvReg.serializeVendorInformationTlv(tlv, buffer);
92 LOG.trace("Serialized VENDOR-INFORMATION TLV : {}.", ByteBufUtil.hexDump(buffer));