BUG-612 : switched PCEP Tlvs 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 io.netty.buffer.ByteBuf;
11 import io.netty.buffer.ByteBufUtil;
12
13 import java.util.Arrays;
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 import com.google.common.base.Preconditions;
20
21 public abstract class AbstractObjectWithTlvsParser<T> implements ObjectParser, ObjectSerializer {
22
23         private static final Logger LOG = LoggerFactory.getLogger(AbstractObjectWithTlvsParser.class);
24
25         private static final int TLV_TYPE_F_LENGTH = 2;
26         private static final int TLV_LENGTH_F_LENGTH = 2;
27         private static final int TLV_HEADER_LENGTH = TLV_LENGTH_F_LENGTH + TLV_TYPE_F_LENGTH;
28
29         protected static final int PADDED_TO = 4;
30
31         private final TlvRegistry tlvReg;
32
33         protected AbstractObjectWithTlvsParser(final TlvRegistry tlvReg) {
34                 this.tlvReg = Preconditions.checkNotNull(tlvReg);
35         }
36
37         protected final void parseTlvs(final T builder, final ByteBuf bytes) throws PCEPDeserializerException {
38                 Preconditions.checkArgument(bytes != null, "Array of bytes is mandatory. Can't be null.");
39                 if (!bytes.isReadable()) {
40                         return;
41                 }
42                 while (bytes.isReadable()) {
43                         int type = bytes.readUnsignedShort();
44                         int length = bytes.readUnsignedShort();
45                         if (length > bytes.readableBytes()) {
46                                 throw new PCEPDeserializerException("Wrong length specified. Passed: " + length + "; Expected: <= "
47                                                 + bytes.readableBytes() + ".");
48                         }
49                         final ByteBuf tlvBytes = bytes.slice(bytes.readerIndex(), length);
50                         LOG.trace("Attempt to parse tlv from bytes: {}", ByteBufUtil.hexDump(tlvBytes));
51                         final Tlv tlv = this.tlvReg.parseTlv(type, tlvBytes);
52                         LOG.trace("Tlv was parsed. {}", tlv);
53                         addTlv(builder, tlv);
54                         bytes.readerIndex(bytes.readerIndex() + length + getPadding(TLV_HEADER_LENGTH + length, PADDED_TO));
55                 }
56         }
57
58         protected final byte[] serializeTlv(final Tlv tlv) {
59                 Preconditions.checkNotNull(tlv, "PCEP TLV is mandatory.");
60                 LOG.trace("Serializing PCEP TLV {}", tlv);
61                 final byte[] ret = this.tlvReg.serializeTlv(tlv);
62                 if (ret == null) {
63                         LOG.warn("TLV serializer for type {} could not be found.", tlv);
64                 }
65                 LOG.trace("Serialized PCEP TLV {}.", Arrays.toString(ret));
66                 return ret;
67         }
68
69         protected void addTlv(final T builder, final Tlv tlv) {
70                 // FIXME: No TLVs by default, fallback to augments
71         }
72
73         public static int getPadding(final int length, final int padding) {
74                 return (padding - (length % padding)) % padding;
75         }
76 }