f6999e16ece31bc8a7c36fc4695708a00400ccdd
[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 java.util.Objects.requireNonNull;
11
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;
23
24 public abstract class AbstractObjectWithTlvsParser<T> extends CommonObjectParser implements ObjectSerializer {
25
26     private static final Logger LOG = LoggerFactory.getLogger(AbstractObjectWithTlvsParser.class);
27
28     private final TlvRegistry tlvReg;
29
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         Preconditions.checkArgument(bytes != null, "Array of bytes is mandatory. Can't be null.");
41         if (!bytes.isReadable()) {
42             return;
43         }
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()
50                     + ".");
51             }
52             final ByteBuf tlvBytes = bytes.readSlice(length);
53             LOG.trace("Parsing PCEP TLV : {}", ByteBufUtil.hexDump(tlvBytes));
54
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());
61                 }
62             } else {
63                 final Tlv tlv = this.tlvReg.parseTlv(type, tlvBytes);
64                 if(tlv != null) {
65                     LOG.trace("Parsed PCEP TLV {}.", tlv);
66                     addTlv(builder, tlv);
67                 }
68             }
69             bytes.skipBytes(TlvUtil.getPadding(TlvUtil.HEADER_SIZE + length, TlvUtil.PADDED_TO));
70         }
71         addVendorInformationTlvs(builder, viTlvs);
72     }
73
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));
79     }
80
81     protected void addTlv(final T builder, final Tlv tlv) {
82         // FIXME: No TLVs by default, fallback to augments
83     }
84
85     protected abstract void addVendorInformationTlvs(final T builder, final List<VendorInformationTlv> tlvs);
86
87     protected final void serializeVendorInformationTlvs(final List<VendorInformationTlv> tlvs, final ByteBuf buffer) {
88         if (tlvs != null) {
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));
93             }
94         }
95     }
96 }