Bug 187 - PCEP: object parser - report correctly unknown objects
[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 java.util.Arrays;
11
12 import org.opendaylight.protocol.util.ByteArray;
13 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Tlv;
14 import org.slf4j.Logger;
15 import org.slf4j.LoggerFactory;
16
17 import com.google.common.base.Preconditions;
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 byte[] bytes) throws PCEPDeserializerException {
36                 if (bytes == null) {
37                         throw new IllegalArgumentException("Byte array is mandatory.");
38                 }
39                 if (bytes.length == 0) {
40                         return;
41                 }
42
43                 int length;
44                 int byteOffset = 0;
45                 int type = 0;
46
47                 while (byteOffset < bytes.length) {
48                         type = ByteArray.bytesToInt(ByteArray.subByte(bytes, byteOffset, TLV_TYPE_F_LENGTH));
49                         byteOffset += TLV_TYPE_F_LENGTH;
50                         length = ByteArray.bytesToInt(ByteArray.subByte(bytes, byteOffset, TLV_LENGTH_F_LENGTH));
51                         byteOffset += TLV_LENGTH_F_LENGTH;
52
53                         if (TLV_HEADER_LENGTH + length > bytes.length) {
54                                 throw new PCEPDeserializerException("Wrong length specified. Passed: " + (TLV_HEADER_LENGTH + length) + "; Expected: <= "
55                                                 + (bytes.length - byteOffset) + ".");
56                         }
57
58                         final byte[] tlvBytes = ByteArray.subByte(bytes, byteOffset, length);
59
60                         LOG.trace("Attempt to parse tlv from bytes: {}", ByteArray.bytesToHexString(tlvBytes));
61                         final Tlv tlv = this.tlvReg.parseTlv(type, tlvBytes);
62                         LOG.trace("Tlv was parsed. {}", tlv);
63                         addTlv(builder, tlv);
64                         byteOffset += length + getPadding(TLV_HEADER_LENGTH + length, PADDED_TO);
65                 }
66         }
67
68         protected final byte[] serializeTlv(final Tlv tlv) {
69                 Preconditions.checkNotNull(tlv, "PCEP TLV is mandatory.");
70                 LOG.trace("Serializing PCEP TLV {}", tlv);
71                 final byte[] ret = this.tlvReg.serializeTlv(tlv);
72                 if (ret == null) {
73                         LOG.warn("TLV serializer for type {} could not be found.", tlv);
74                 }
75                 LOG.trace("Serialized PCEP TLV {}.", Arrays.toString(ret));
76                 return ret;
77         }
78
79         protected void addTlv(final T builder, final Tlv tlv) {
80                 // FIXME: No TLVs by default, fallback to augments
81         }
82
83         public static int getPadding(final int length, final int padding) {
84                 return (padding - (length % padding)) % padding;
85         }
86 }