Merge changes Ic58ee772,Id447e440
[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 org.opendaylight.protocol.util.ByteArray;
11 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Tlv;
12 import org.slf4j.Logger;
13 import org.slf4j.LoggerFactory;
14
15 import com.google.common.base.Preconditions;
16
17 public abstract class AbstractObjectWithTlvsParser<T> implements ObjectParser, ObjectSerializer {
18
19         private static final Logger LOG = LoggerFactory.getLogger(AbstractObjectWithTlvsParser.class);
20
21         private static final int TLV_TYPE_F_LENGTH = 2;
22         private static final int TLV_LENGTH_F_LENGTH = 2;
23         private static final int TLV_HEADER_LENGTH = TLV_LENGTH_F_LENGTH + TLV_TYPE_F_LENGTH;
24
25         protected static final int PADDED_TO = 4;
26
27         private final TlvHandlerRegistry tlvReg;
28
29         protected AbstractObjectWithTlvsParser(final TlvHandlerRegistry tlvReg) {
30                 this.tlvReg = Preconditions.checkNotNull(tlvReg);
31         }
32
33         protected final void parseTlvs(final T builder, final byte[] bytes) throws PCEPDeserializerException {
34                 if (bytes == null) {
35                         throw new IllegalArgumentException("Byte array is mandatory.");
36                 }
37                 if (bytes.length == 0) {
38                         return;
39                 }
40
41                 int length;
42                 int byteOffset = 0;
43                 int type = 0;
44
45                 while (byteOffset < bytes.length) {
46                         type = ByteArray.bytesToInt(ByteArray.subByte(bytes, byteOffset, TLV_TYPE_F_LENGTH));
47                         byteOffset += TLV_TYPE_F_LENGTH;
48                         length = ByteArray.bytesToInt(ByteArray.subByte(bytes, byteOffset, TLV_LENGTH_F_LENGTH));
49                         byteOffset += TLV_LENGTH_F_LENGTH;
50
51                         if (TLV_HEADER_LENGTH + length > bytes.length) {
52                                 throw new PCEPDeserializerException("Wrong length specified. Passed: " + (TLV_HEADER_LENGTH + length) + "; Expected: <= "
53                                                 + (bytes.length - byteOffset) + ".");
54                         }
55
56                         final byte[] tlvBytes = ByteArray.subByte(bytes, byteOffset, length);
57
58                         LOG.trace("Attempt to parse tlv from bytes: {}", ByteArray.bytesToHexString(tlvBytes));
59                         final TlvParser parser = this.tlvReg.getTlvParser(type);
60                         if (parser != null) {
61                                 final Tlv tlv = parser.parseTlv(tlvBytes);
62                                 LOG.trace("Tlv was parsed. {}", tlv);
63                                 addTlv(builder, tlv);
64                         } else {
65                                 LOG.warn("Unknown TLV received. Type {}. Ignoring it.", type);
66                         }
67                         byteOffset += length + getPadding(TLV_HEADER_LENGTH + length, PADDED_TO);
68                 }
69         }
70
71         protected final byte[] serializeTlv(final Tlv tlv) {
72
73                 final TlvSerializer serializer = this.tlvReg.getTlvSerializer(tlv);
74
75                 final byte[] typeBytes = ByteArray.intToBytes(serializer.getType(), TLV_TYPE_F_LENGTH);
76
77                 final byte[] valueBytes = serializer.serializeTlv(tlv);
78
79                 final byte[] lengthBytes = ByteArray.intToBytes(valueBytes.length, TLV_LENGTH_F_LENGTH);
80
81                 final byte[] bytes = new byte[TLV_HEADER_LENGTH + valueBytes.length + getPadding(TLV_HEADER_LENGTH + valueBytes.length, PADDED_TO)];
82
83                 int byteOffset = 0;
84                 System.arraycopy(typeBytes, 0, bytes, byteOffset, TLV_TYPE_F_LENGTH);
85                 byteOffset += TLV_TYPE_F_LENGTH;
86                 System.arraycopy(lengthBytes, 0, bytes, byteOffset, TLV_LENGTH_F_LENGTH);
87                 byteOffset += TLV_LENGTH_F_LENGTH;
88                 System.arraycopy(valueBytes, 0, bytes, byteOffset, valueBytes.length);
89                 return bytes;
90         }
91
92         public abstract void addTlv(final T builder, final Tlv tlv);
93
94         protected static int getPadding(final int length, final int padding) {
95                 return (padding - (length % padding)) % padding;
96         }
97 }