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