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.spi;
10 import org.opendaylight.protocol.util.ByteArray;
11 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.iana.rev130816.EnterpriseNumber;
12 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Tlv;
13 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.vs.tlv.VsTlv;
14 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.vs.tlv.VsTlvBuilder;
15 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.vs.tlv.vs.tlv.VendorPayload;
17 abstract public class AbstractVendorSpecificTlvParser implements TlvParser, TlvSerializer {
19 public static final int TYPE = 27;
21 protected static final int ENTERPRISE_NUM_LENGTH = 4;
24 public int getType() {
29 public byte[] serializeTlv(Tlv tlv) {
31 throw new IllegalArgumentException("Vendor Specific Tlv is mandatory.");
33 final VsTlv vsTlv = (VsTlv) tlv;
34 if (vsTlv.getEnterpriseNumber().getValue() == getEnterpriseNumber()) {
35 final byte[] payloadBytes = serializeVendorPayload(vsTlv.getVendorPayload());
36 final byte[] ianaNumBytes = ByteArray.longToBytes(vsTlv.getEnterpriseNumber().getValue(),
37 ENTERPRISE_NUM_LENGTH);
39 final byte[] bytes = new byte[ianaNumBytes.length + payloadBytes.length];
40 System.arraycopy(ianaNumBytes, 0, bytes, 0, ENTERPRISE_NUM_LENGTH);
41 System.arraycopy(payloadBytes, 0, bytes, ENTERPRISE_NUM_LENGTH, payloadBytes.length);
48 public VsTlv parseTlv(byte[] valueBytes) throws PCEPDeserializerException {
49 VsTlvBuilder vsTlvBuider = new VsTlvBuilder();
50 if (valueBytes == null || valueBytes.length == 0) {
51 throw new IllegalArgumentException("Array of bytes is mandatory. Can't be null or empty.");
54 byte[] enBytes = ByteArray.subByte(valueBytes, 0, ENTERPRISE_NUM_LENGTH);
55 long en = ByteArray.bytesToLong(enBytes);
56 if (en == getEnterpriseNumber()) {
57 vsTlvBuider.setEnterpriseNumber(new EnterpriseNumber(getEnterpriseNumber()));
58 int byteOffset = ENTERPRISE_NUM_LENGTH;
59 int payloadLength = valueBytes.length - byteOffset;
60 VendorPayload vendorPayload = null;
61 if (payloadLength > 0) {
62 byte[] payloadBytes = ByteArray.subByte(valueBytes, byteOffset, payloadLength);
63 vendorPayload = parseVendorPayload(payloadBytes);
64 if (vendorPayload != null) {
65 vsTlvBuider.setVendorPayload(vendorPayload);
69 return vsTlvBuider.build();
72 abstract protected byte[] serializeVendorPayload(VendorPayload payload);
74 abstract protected long getEnterpriseNumber();
76 abstract protected VendorPayload parseVendorPayload(byte[] payloadBytes) throws PCEPDeserializerException;
78 protected static int getPadding(final int length, final int padding) {
79 return (padding - (length % padding)) % padding;