815dd679fd43c85860cfcfdfed0c336aa63b6269
[bgpcep.git] / pcep / base-parser / src / main / java / org / opendaylight / protocol / pcep / parser / tlv / AbstractVendorInformationTlvParser.java
1 /*
2  * Copyright (c) 2014 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.parser.tlv;
9
10 import static com.google.common.base.Preconditions.checkArgument;
11 import static org.opendaylight.protocol.pcep.spi.VendorInformationUtil.VENDOR_INFORMATION_TLV_TYPE;
12
13 import io.netty.buffer.ByteBuf;
14 import io.netty.buffer.Unpooled;
15 import org.opendaylight.protocol.pcep.PCEPDeserializerException;
16 import org.opendaylight.protocol.pcep.spi.EnterpriseSpecificInformationParser;
17 import org.opendaylight.protocol.pcep.spi.TlvParser;
18 import org.opendaylight.protocol.pcep.spi.TlvSerializer;
19 import org.opendaylight.protocol.pcep.spi.TlvUtil;
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.EnterpriseSpecificInformation;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.vendor.information.tlvs.VendorInformationTlv;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.vendor.information.tlvs.VendorInformationTlvBuilder;
24 import org.opendaylight.yangtools.yang.common.netty.ByteBufUtils;
25
26 public abstract class AbstractVendorInformationTlvParser
27         implements TlvSerializer, TlvParser, EnterpriseSpecificInformationParser {
28
29     @Override
30     public final void serializeTlv(final Tlv tlv, final ByteBuf buffer) {
31         checkArgument(tlv instanceof VendorInformationTlv, "Vendor Specific Tlv is mandatory.");
32         final VendorInformationTlv viTlv = (VendorInformationTlv) tlv;
33         final ByteBuf body = Unpooled.buffer();
34         ByteBufUtils.write(body, getEnterpriseNumber().getValue());
35         serializeEnterpriseSpecificInformation(viTlv.getEnterpriseSpecificInformation(), body);
36         TlvUtil.formatTlv(VENDOR_INFORMATION_TLV_TYPE, body, buffer);
37     }
38
39     @Override
40     public final VendorInformationTlv parseTlv(final ByteBuf buffer) throws PCEPDeserializerException {
41         if (buffer == null) {
42             return null;
43         }
44         final VendorInformationTlvBuilder viTlvBuider = new VendorInformationTlvBuilder()
45                 .setEnterpriseNumber(getEnterpriseNumber());
46         if (buffer.isReadable()) {
47             final EnterpriseSpecificInformation esInformation = parseEnterpriseSpecificInformation(buffer.slice());
48             if (esInformation != null) {
49                 viTlvBuider.setEnterpriseSpecificInformation(esInformation);
50             }
51         }
52         return viTlvBuider.build();
53     }
54 }