BUG-6647 Increase code coverage and clean up II
[bgpcep.git] / pcep / base-parser / src / main / java / org / opendaylight / protocol / pcep / parser / tlv / AbstractVendorSpecificTlvParser.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 org.opendaylight.protocol.util.ByteBufWriteUtil.writeUnsignedInt;
11
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;
24
25 /**
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.
29  */
30 @Deprecated
31 public abstract class AbstractVendorSpecificTlvParser implements TlvParser, TlvSerializer {
32
33     public static final int TYPE = 27;
34
35     @Override
36     public void serializeTlv(final Tlv tlv, final ByteBuf buffer) {
37         Preconditions.checkArgument(tlv instanceof VsTlv, "Vendor Specific Tlv is mandatory.");
38         final VsTlv vsTlv = (VsTlv) tlv;
39         final ByteBuf body = Unpooled.buffer();
40         if (vsTlv.getEnterpriseNumber().getValue() == getEnterpriseNumber()) {
41             Preconditions.checkArgument(vsTlv.getEnterpriseNumber() != null, "EnterpriseNumber is mandatory.");
42             writeUnsignedInt(vsTlv.getEnterpriseNumber().getValue(), body);
43             serializeVendorPayload(vsTlv.getVendorPayload(), body);
44             TlvUtil.formatTlv(TYPE, body, buffer);
45         }
46     }
47
48     @Override
49     public VsTlv parseTlv(final ByteBuf buffer) throws PCEPDeserializerException {
50         if (buffer == null) {
51             return null;
52         }
53         final VsTlvBuilder vsTlvBuider = new VsTlvBuilder();
54         final long en = buffer.readUnsignedInt();
55         if (en == getEnterpriseNumber()) {
56             vsTlvBuider.setEnterpriseNumber(new EnterpriseNumber(getEnterpriseNumber()));
57             VendorPayload vendorPayload = null;
58             if (buffer.isReadable()) {
59                 final ByteBuf payloadBytes = buffer.slice();
60                 vendorPayload = parseVendorPayload(payloadBytes);
61                 if (vendorPayload != null) {
62                     vsTlvBuider.setVendorPayload(vendorPayload);
63                 }
64             }
65         }
66         return vsTlvBuider.build();
67     }
68
69     protected abstract void serializeVendorPayload(final VendorPayload payload, final ByteBuf buffer);
70
71     protected abstract long getEnterpriseNumber();
72
73     protected abstract VendorPayload parseVendorPayload(final ByteBuf payloadBytes) throws PCEPDeserializerException;
74 }