Move pcep base parser Activator to its own bundle
[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.rev131005.Tlv;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.vendor.information.EnterpriseSpecificInformation;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.vendor.information.tlvs.VendorInformationTlv;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.vendor.information.tlvs.VendorInformationTlvBuilder;
26
27 public abstract class AbstractVendorInformationTlvParser implements TlvSerializer, TlvParser, EnterpriseSpecificInformationParser {
28
29     @Override
30     public final void serializeTlv(final Tlv tlv, final ByteBuf buffer) {
31         Preconditions.checkArgument(tlv instanceof VendorInformationTlv, "Vendor Specific Tlv is mandatory.");
32         final VendorInformationTlv viTlv = (VendorInformationTlv) tlv;
33         final ByteBuf body = Unpooled.buffer();
34         writeUnsignedInt(getEnterpriseNumber().getValue(), body);
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         viTlvBuider.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 }