Bug-479: Implementation of Vendor-Information TLV
[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 com.google.common.base.Optional;
11 import com.google.common.base.Preconditions;
12 import com.google.common.collect.Lists;
13
14 import io.netty.buffer.ByteBuf;
15 import io.netty.buffer.ByteBufUtil;
16
17 import java.util.List;
18
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.rev131005.Tlv;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.vendor.information.tlvs.VendorInformationTlv;
22 import org.slf4j.Logger;
23 import org.slf4j.LoggerFactory;
24
25 public abstract class AbstractObjectWithTlvsParser<T> implements ObjectParser, ObjectSerializer {
26
27     private static final Logger LOG = LoggerFactory.getLogger(AbstractObjectWithTlvsParser.class);
28
29     public static final int PADDED_TO = 4;
30
31     private final TlvRegistry tlvReg;
32
33     private final VendorInformationTlvRegistry viTlvReg;
34
35     protected AbstractObjectWithTlvsParser(final TlvRegistry tlvReg, final VendorInformationTlvRegistry viTlvReg) {
36         this.tlvReg = Preconditions.checkNotNull(tlvReg);
37         this.viTlvReg = Preconditions.checkNotNull(viTlvReg);
38     }
39
40     protected final void parseTlvs(final T builder, final ByteBuf bytes) throws PCEPDeserializerException {
41         Preconditions.checkArgument(bytes != null, "Array of bytes is mandatory. Can't be null.");
42         if (!bytes.isReadable()) {
43             return;
44         }
45         final List<VendorInformationTlv> viTlvs = Lists.newArrayList();
46         while (bytes.isReadable()) {
47             int type = bytes.readUnsignedShort();
48             int length = bytes.readUnsignedShort();
49             if (length > bytes.readableBytes()) {
50                 throw new PCEPDeserializerException("Wrong length specified. Passed: " + length + "; Expected: <= " + bytes.readableBytes()
51                     + ".");
52             }
53             final ByteBuf tlvBytes = bytes.slice(bytes.readerIndex(), length);
54             LOG.trace("Parsing PCEP TLV : {}", ByteBufUtil.hexDump(tlvBytes));
55
56             if (VendorInformationUtil.isVendorInformationTlv(type)) {
57                 final EnterpriseNumber enterpriseNumber = new EnterpriseNumber(tlvBytes.readUnsignedInt());
58                 final Optional<VendorInformationTlv> viTlv = this.viTlvReg.parseVendorInformationTlv(enterpriseNumber, tlvBytes);
59                 if(viTlv.isPresent()) {
60                     LOG.trace("Parsed VENDOR-INFORMATION TLV {}.", viTlv.get());
61                     viTlvs.add(viTlv.get());
62                 }
63             } else {
64                 final Tlv tlv = this.tlvReg.parseTlv(type, tlvBytes);
65                 if(tlv != null) {
66                     LOG.trace("Parsed PCEP TLV {}.", tlv);
67                     addTlv(builder, tlv);
68                 }
69             }
70             bytes.skipBytes(length + TlvUtil.getPadding(TlvUtil.HEADER_SIZE + length, TlvUtil.PADDED_TO));
71         }
72         addVendorInformationTlvs(builder, viTlvs);
73     }
74
75     protected final void serializeTlv(final Tlv tlv, final ByteBuf buffer) {
76         Preconditions.checkNotNull(tlv, "PCEP TLV is mandatory.");
77         LOG.trace("Serializing PCEP TLV {}", tlv);
78         this.tlvReg.serializeTlv(tlv, buffer);
79         LOG.trace("Serialized PCEP TLV : {}.", ByteBufUtil.hexDump(buffer));
80     }
81
82     protected void addTlv(final T builder, final Tlv tlv) {
83         // FIXME: No TLVs by default, fallback to augments
84     }
85
86     abstract protected void addVendorInformationTlvs(final T builder, final List<VendorInformationTlv> tlvs);
87
88     protected final void serializeVendorInformationTlvs(final List<VendorInformationTlv> tlvs, final ByteBuf buffer) {
89         if (tlvs != null && !tlvs.isEmpty()) {
90             for (final VendorInformationTlv tlv : tlvs) {
91                 LOG.trace("Serializing VENDOR-INFORMATION TLV {}", tlv);
92                 this.viTlvReg.serializeVendorInformationTlv(tlv, buffer);
93                 LOG.trace("Serialized VENDOR-INFORMATION TLV : {}.", ByteBufUtil.hexDump(buffer));
94             }
95         }
96     }
97 }