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