BUG-612 : switched PCEP tlv serializers to ByteBuf
[bgpcep.git] / pcep / spi / src / main / java / org / opendaylight / protocol / pcep / spi / AbstractObjectWithTlvsParser.java
1 /*
2  * Copyright (c) 2013 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.spi;
9
10 import com.google.common.base.Preconditions;
11
12 import io.netty.buffer.ByteBuf;
13 import io.netty.buffer.ByteBufUtil;
14
15 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Tlv;
16 import org.slf4j.Logger;
17 import org.slf4j.LoggerFactory;
18
19 public abstract class AbstractObjectWithTlvsParser<T> implements ObjectParser, ObjectSerializer {
20
21     private static final Logger LOG = LoggerFactory.getLogger(AbstractObjectWithTlvsParser.class);
22
23     private static final int TLV_TYPE_F_LENGTH = 2;
24     private static final int TLV_LENGTH_F_LENGTH = 2;
25     private static final int TLV_HEADER_LENGTH = TLV_LENGTH_F_LENGTH + TLV_TYPE_F_LENGTH;
26
27     protected static final int PADDED_TO = 4;
28
29     private final TlvRegistry tlvReg;
30
31     protected AbstractObjectWithTlvsParser(final TlvRegistry tlvReg) {
32         this.tlvReg = Preconditions.checkNotNull(tlvReg);
33     }
34
35     protected final void parseTlvs(final T builder, final ByteBuf bytes) throws PCEPDeserializerException {
36         Preconditions.checkArgument(bytes != null, "Array of bytes is mandatory. Can't be null.");
37         if (!bytes.isReadable()) {
38             return;
39         }
40         while (bytes.isReadable()) {
41             int type = bytes.readUnsignedShort();
42             int length = bytes.readUnsignedShort();
43             if (length > bytes.readableBytes()) {
44                 throw new PCEPDeserializerException("Wrong length specified. Passed: " + length + "; Expected: <= " + bytes.readableBytes()
45                         + ".");
46             }
47             final ByteBuf tlvBytes = bytes.slice(bytes.readerIndex(), length);
48             LOG.trace("Attempt to parse tlv from bytes: {}", ByteBufUtil.hexDump(tlvBytes));
49             final Tlv tlv = this.tlvReg.parseTlv(type, tlvBytes);
50             LOG.trace("Tlv was parsed. {}", tlv);
51             addTlv(builder, tlv);
52             bytes.readerIndex(bytes.readerIndex() + length + getPadding(TLV_HEADER_LENGTH + length, PADDED_TO));
53         }
54     }
55
56     protected final void serializeTlv(final Tlv tlv, final ByteBuf buffer) {
57         Preconditions.checkNotNull(tlv, "PCEP TLV is mandatory.");
58         LOG.trace("Serializing PCEP TLV {}", tlv);
59         this.tlvReg.serializeTlv(tlv, buffer);
60         LOG.trace("Serialized PCEP TLV {}.", ByteBufUtil.hexDump(buffer));
61     }
62
63     protected void addTlv(final T builder, final Tlv tlv) {
64         // FIXME: No TLVs by default, fallback to augments
65     }
66
67     public static int getPadding(final int length, final int padding) {
68         return (padding - (length % padding)) % padding;
69     }
70 }