Promote MessageRegistry to pcep-api
[bgpcep.git] / pcep / spi / src / main / java / org / opendaylight / protocol / pcep / spi / AbstractObjectWithTlvsParser.java
1 /*
2  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.protocol.pcep.spi;
9
10 import static com.google.common.base.Preconditions.checkArgument;
11 import static java.util.Objects.requireNonNull;
12
13 import io.netty.buffer.ByteBuf;
14 import io.netty.buffer.ByteBufUtil;
15 import java.util.ArrayList;
16 import java.util.List;
17 import java.util.Optional;
18 import org.opendaylight.protocol.pcep.PCEPDeserializerException;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.iana.rev130816.EnterpriseNumber;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.Tlv;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.vendor.information.tlvs.VendorInformationTlv;
22 import org.opendaylight.yangtools.yang.common.netty.ByteBufUtils;
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
25
26 public abstract class AbstractObjectWithTlvsParser<T> extends CommonObjectParser implements ObjectSerializer {
27     private static final Logger LOG = LoggerFactory.getLogger(AbstractObjectWithTlvsParser.class);
28
29     private final TlvRegistry tlvReg;
30     private final VendorInformationTlvRegistry viTlvReg;
31
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);
37     }
38
39     protected final void parseTlvs(final T builder, final ByteBuf bytes) throws PCEPDeserializerException {
40         checkArgument(bytes != null, "Array of bytes is mandatory. Can't be null.");
41         if (!bytes.isReadable()) {
42             return;
43         }
44         final List<VendorInformationTlv> viTlvs = new ArrayList<>();
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: <= "
50             + bytes.readableBytes()
51                     + ".");
52             }
53             final ByteBuf tlvBytes = bytes.readSlice(length);
54             LOG.trace("Parsing PCEP TLV : {}", ByteBufUtil.hexDump(tlvBytes));
55
56             if (VendorInformationUtil.isVendorInformationTlv(type)) {
57                 final EnterpriseNumber enterpriseNumber = new EnterpriseNumber(ByteBufUtils.readUint32(tlvBytes));
58                 final Optional<VendorInformationTlv> viTlv = this.viTlvReg.parseVendorInformationTlv(enterpriseNumber,
59                     tlvBytes);
60                 if (viTlv.isPresent()) {
61                     LOG.trace("Parsed VENDOR-INFORMATION TLV {}.", viTlv.get());
62                     viTlvs.add(viTlv.get());
63                 }
64             } else {
65                 final Tlv tlv = this.tlvReg.parseTlv(type, tlvBytes);
66                 if (tlv != null) {
67                     LOG.trace("Parsed PCEP TLV {}.", tlv);
68                     addTlv(builder, tlv);
69                 }
70             }
71             bytes.skipBytes(TlvUtil.getPadding(TlvUtil.HEADER_SIZE + length, TlvUtil.PADDED_TO));
72         }
73         addVendorInformationTlvs(builder, viTlvs);
74     }
75
76     protected final void serializeTlv(final Tlv tlv, final ByteBuf buffer) {
77         requireNonNull(tlv, "PCEP TLV is mandatory.");
78         LOG.trace("Serializing PCEP TLV {}", tlv);
79         this.tlvReg.serializeTlv(tlv, buffer);
80         LOG.trace("Serialized PCEP TLV : {}.", ByteBufUtil.hexDump(buffer));
81     }
82
83     protected void addTlv(final T builder, final Tlv tlv) {
84         // FIXME: No TLVs by default, fallback to augments
85     }
86
87     protected abstract void addVendorInformationTlvs(T builder, List<VendorInformationTlv> tlvs);
88
89     protected final void serializeVendorInformationTlvs(final List<VendorInformationTlv> tlvs, final ByteBuf buffer) {
90         if (tlvs != null) {
91             for (final VendorInformationTlv tlv : tlvs) {
92                 LOG.trace("Serializing VENDOR-INFORMATION TLV {}", tlv);
93                 this.viTlvReg.serializeVendorInformationTlv(tlv, buffer);
94                 LOG.trace("Serialized VENDOR-INFORMATION TLV : {}.", ByteBufUtil.hexDump(buffer));
95             }
96         }
97     }
98 }