2 * Copyright (c) 2013 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.pcep.PCEPDeserializerException;
11 import org.opendaylight.protocol.util.ByteArray;
12 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Tlv;
13 import org.slf4j.Logger;
14 import org.slf4j.LoggerFactory;
16 import com.google.common.base.Preconditions;
18 public abstract class AbstractObjectWithTlvsParser<BUILDER> implements ObjectParser, ObjectSerializer {
20 private static final Logger logger = LoggerFactory.getLogger(AbstractObjectWithTlvsParser.class);
22 private static final int TLV_TYPE_F_LENGTH = 2;
23 private static final int TLV_LENGTH_F_LENGTH = 2;
24 private static final int TLV_HEADER_LENGTH = TLV_LENGTH_F_LENGTH + TLV_TYPE_F_LENGTH;
26 protected static final int PADDED_TO = 4;
28 private final TlvHandlerRegistry tlvReg;
30 protected AbstractObjectWithTlvsParser(final TlvHandlerRegistry tlvReg) {
31 this.tlvReg = Preconditions.checkNotNull(tlvReg);
34 protected final void parseTlvs(final BUILDER builder, final byte[] bytes) throws PCEPDeserializerException {
36 throw new IllegalArgumentException("Byte array is mandatory.");
43 while (byteOffset < bytes.length) {
44 type = ByteArray.bytesToInt(ByteArray.subByte(bytes, byteOffset, TLV_TYPE_F_LENGTH));
45 byteOffset += TLV_TYPE_F_LENGTH;
46 length = ByteArray.bytesToInt(ByteArray.subByte(bytes, byteOffset, TLV_LENGTH_F_LENGTH));
47 byteOffset += TLV_LENGTH_F_LENGTH;
49 if (TLV_HEADER_LENGTH + length > bytes.length - byteOffset) {
50 throw new PCEPDeserializerException("Wrong length specified. Passed: " + (TLV_HEADER_LENGTH + length) + "; Expected: <= "
51 + (bytes.length - byteOffset) + ".");
54 final byte[] tlvBytes = ByteArray.subByte(bytes, byteOffset, length);
56 logger.trace("Attempt to parse tlv from bytes: {}", ByteArray.bytesToHexString(tlvBytes));
57 final Tlv tlv = this.tlvReg.getTlvParser(type).parseTlv(tlvBytes);
58 logger.trace("Tlv was parsed. {}", tlv);
62 byteOffset += length + getPadding(TLV_HEADER_LENGTH + length, PADDED_TO);
66 protected final byte[] serializeTlv(final Tlv tlv) {
68 final TlvSerializer serializer = this.tlvReg.getTlvSerializer(tlv);
70 final byte[] typeBytes = (ByteArray.cutBytes(ByteArray.intToBytes(serializer.getType()), (Integer.SIZE / 8) - TLV_TYPE_F_LENGTH));
72 final byte[] valueBytes = serializer.serializeTlv(tlv);
74 final byte[] lengthBytes = ByteArray.cutBytes(ByteArray.intToBytes(valueBytes.length), (Integer.SIZE / 8) - TLV_LENGTH_F_LENGTH);
76 final byte[] bytes = new byte[TLV_HEADER_LENGTH + valueBytes.length + getPadding(TLV_HEADER_LENGTH + valueBytes.length, PADDED_TO)];
79 System.arraycopy(typeBytes, 0, bytes, byteOffset, TLV_TYPE_F_LENGTH);
80 System.arraycopy(lengthBytes, 0, bytes, byteOffset += TLV_TYPE_F_LENGTH, TLV_LENGTH_F_LENGTH);
81 System.arraycopy(valueBytes, 0, bytes, byteOffset += TLV_LENGTH_F_LENGTH, valueBytes.length);
85 public abstract void addTlv(final BUILDER builder, final Tlv tlv);
87 private static int getPadding(final int length, final int padding) {
88 return (padding - (length % padding)) % padding;