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.impl.tlv;
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;
20 public abstract class AbstractVendorSpecificTlvParser implements TlvParser, TlvSerializer {
22 public static final int TYPE = 27;
24 protected static final int ENTERPRISE_NUM_LENGTH = 4;
27 public byte[] serializeTlv(final Tlv tlv) {
29 throw new IllegalArgumentException("Vendor Specific Tlv is mandatory.");
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);
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);
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.");
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);
67 return vsTlvBuider.build();
70 protected abstract byte[] serializeVendorPayload(VendorPayload payload);
72 protected abstract long getEnterpriseNumber();
74 protected abstract VendorPayload parseVendorPayload(byte[] payloadBytes) throws PCEPDeserializerException;
76 protected static int getPadding(final int length, final int padding) {
77 return (padding - (length % padding)) % padding;