2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.protocol.pcep.parser.tlv;
10 import static org.opendaylight.protocol.util.ByteBufWriteUtil.writeUnsignedInt;
12 import com.google.common.base.Preconditions;
13 import io.netty.buffer.ByteBuf;
14 import io.netty.buffer.Unpooled;
15 import org.opendaylight.protocol.pcep.spi.PCEPDeserializerException;
16 import org.opendaylight.protocol.pcep.spi.TlvParser;
17 import org.opendaylight.protocol.pcep.spi.TlvSerializer;
18 import org.opendaylight.protocol.pcep.spi.TlvUtil;
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.vs.tlv.VsTlv;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.vs.tlv.VsTlvBuilder;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.vs.tlv.vs.tlv.VendorPayload;
26 * Vs-tlv model has been deprecated, vendor-information-tlv should be used instead.
27 * Therefore parsers/serializers wont be need it any more and this class is slated
28 * for removal in a future release.
31 public abstract class AbstractVendorSpecificTlvParser implements TlvParser, TlvSerializer {
33 public static final int TYPE = 27;
35 protected static final int ENTERPRISE_NUM_LENGTH = 4;
38 public void serializeTlv(final Tlv tlv, final ByteBuf buffer) {
39 Preconditions.checkArgument(tlv instanceof VsTlv, "Vendor Specific Tlv is mandatory.");
40 final VsTlv vsTlv = (VsTlv) tlv;
41 final ByteBuf body = Unpooled.buffer();
42 if (vsTlv.getEnterpriseNumber().getValue() == getEnterpriseNumber()) {
43 Preconditions.checkArgument(vsTlv.getEnterpriseNumber() != null, "EnterpriseNumber is mandatory.");
44 writeUnsignedInt(vsTlv.getEnterpriseNumber().getValue(), body);
45 serializeVendorPayload(vsTlv.getVendorPayload(), body);
46 TlvUtil.formatTlv(TYPE, body, buffer);
51 public VsTlv parseTlv(final ByteBuf buffer) throws PCEPDeserializerException {
55 final VsTlvBuilder vsTlvBuider = new VsTlvBuilder();
56 final long en = buffer.readUnsignedInt();
57 if (en == getEnterpriseNumber()) {
58 vsTlvBuider.setEnterpriseNumber(new EnterpriseNumber(getEnterpriseNumber()));
59 VendorPayload vendorPayload = null;
60 if (buffer.isReadable()) {
61 final ByteBuf payloadBytes = buffer.slice();
62 vendorPayload = parseVendorPayload(payloadBytes);
63 if (vendorPayload != null) {
64 vsTlvBuider.setVendorPayload(vendorPayload);
68 return vsTlvBuider.build();
71 protected abstract void serializeVendorPayload(final VendorPayload payload, final ByteBuf buffer);
73 protected abstract long getEnterpriseNumber();
75 protected abstract VendorPayload parseVendorPayload(final ByteBuf payloadBytes) throws PCEPDeserializerException;