Removing deprecated getType() method from serializers.
[bgpcep.git] / pcep / impl / src / main / java / org / opendaylight / protocol / pcep / impl / 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.impl.tlv;
9
10 import org.opendaylight.protocol.pcep.spi.PCEPDeserializerException;
11 import org.opendaylight.protocol.pcep.spi.TlvParser;
12 import org.opendaylight.protocol.pcep.spi.TlvSerializer;
13 import org.opendaylight.protocol.util.ByteArray;
14 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.iana.rev130816.EnterpriseNumber;
15 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Tlv;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.vs.tlv.VsTlv;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.vs.tlv.VsTlvBuilder;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.vs.tlv.vs.tlv.VendorPayload;
19
20 public abstract class AbstractVendorSpecificTlvParser implements TlvParser, TlvSerializer {
21
22         public static final int TYPE = 27;
23
24         protected static final int ENTERPRISE_NUM_LENGTH = 4;
25
26         @Override
27         public byte[] serializeTlv(final Tlv tlv) {
28                 if (tlv == null) {
29                         throw new IllegalArgumentException("Vendor Specific Tlv is mandatory.");
30                 }
31                 final VsTlv vsTlv = (VsTlv) tlv;
32                 if (vsTlv.getEnterpriseNumber().getValue() == getEnterpriseNumber()) {
33                         final byte[] payloadBytes = serializeVendorPayload(vsTlv.getVendorPayload());
34                         final byte[] ianaNumBytes = ByteArray.longToBytes(vsTlv.getEnterpriseNumber().getValue(),
35                                         ENTERPRISE_NUM_LENGTH);
36
37                         final byte[] bytes = new byte[ianaNumBytes.length + payloadBytes.length];
38                         System.arraycopy(ianaNumBytes, 0, bytes, 0, ENTERPRISE_NUM_LENGTH);
39                         System.arraycopy(payloadBytes, 0, bytes, ENTERPRISE_NUM_LENGTH, payloadBytes.length);
40                         return TlvUtil.formatTlv(TYPE, bytes);
41                 }
42                 return new byte[0];
43         }
44
45         @Override
46         public VsTlv parseTlv(final byte[] valueBytes) throws PCEPDeserializerException {
47                 VsTlvBuilder vsTlvBuider = new VsTlvBuilder();
48                 if (valueBytes == null || valueBytes.length == 0) {
49                         throw new IllegalArgumentException("Array of bytes is mandatory. Can't be null or empty.");
50                 }
51
52                 byte[] enBytes = ByteArray.subByte(valueBytes, 0, ENTERPRISE_NUM_LENGTH);
53                 long en = ByteArray.bytesToLong(enBytes);
54                 if (en == getEnterpriseNumber()) {
55                         vsTlvBuider.setEnterpriseNumber(new EnterpriseNumber(getEnterpriseNumber()));
56                         int byteOffset = ENTERPRISE_NUM_LENGTH;
57                         int payloadLength = valueBytes.length - byteOffset;
58                         VendorPayload vendorPayload = null;
59                         if (payloadLength > 0) {
60                                 byte[] payloadBytes = ByteArray.subByte(valueBytes, byteOffset, payloadLength);
61                                 vendorPayload = parseVendorPayload(payloadBytes);
62                                 if (vendorPayload != null) {
63                                         vsTlvBuider.setVendorPayload(vendorPayload);
64                                 }
65                         }
66                 }
67                 return vsTlvBuider.build();
68         }
69
70         protected abstract byte[] serializeVendorPayload(VendorPayload payload);
71
72         protected abstract long getEnterpriseNumber();
73
74         protected abstract VendorPayload parseVendorPayload(byte[] payloadBytes) throws PCEPDeserializerException;
75
76         protected static int getPadding(final int length, final int padding) {
77                 return (padding - (length % padding)) % padding;
78         }
79 }