BUG-47 : switched subobjects to generated source code.
[bgpcep.git] / pcep / impl / src / main / java / org / opendaylight / protocol / pcep / impl / message / 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.impl.message;
9
10 import org.opendaylight.protocol.pcep.PCEPDeserializerException;
11 import org.opendaylight.protocol.pcep.spi.ObjectParser;
12 import org.opendaylight.protocol.pcep.spi.ObjectSerializer;
13 import org.opendaylight.protocol.pcep.spi.TlvHandlerRegistry;
14 import org.opendaylight.protocol.pcep.spi.TlvSerializer;
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<BUILDER> implements ObjectParser, ObjectSerializer {
23
24         private static final Logger logger = 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 TlvHandlerRegistry tlvReg;
33
34         protected AbstractObjectWithTlvsParser(final TlvHandlerRegistry tlvReg) {
35                 this.tlvReg = Preconditions.checkNotNull(tlvReg);
36         }
37
38         protected final void parseTlvs(final BUILDER builder, final byte[] bytes) throws PCEPDeserializerException {
39                 if (bytes == null) {
40                         throw new IllegalArgumentException("Byte array is mandatory.");
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 - byteOffset) {
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                         logger.trace("Attempt to parse tlv from bytes: {}", ByteArray.bytesToHexString(tlvBytes));
61                         final Tlv tlv = this.tlvReg.getTlvParser(type).parseTlv(tlvBytes);
62                         logger.trace("Tlv was parsed. {}", tlv);
63
64                         addTlv(builder, tlv);
65
66                         byteOffset += length + getPadding(TLV_HEADER_LENGTH + length, PADDED_TO);
67                 }
68         }
69
70         protected final byte[] serializeTlv(final Tlv tlv) {
71
72                 final TlvSerializer serializer = this.tlvReg.getTlvSerializer(tlv);
73
74                 final byte[] typeBytes = (ByteArray.cutBytes(ByteArray.intToBytes(serializer.getType()), (Integer.SIZE / 8) - TLV_TYPE_F_LENGTH));
75
76                 final byte[] valueBytes = serializer.serializeTlv(tlv);
77
78                 final byte[] lengthBytes = ByteArray.cutBytes(ByteArray.intToBytes(valueBytes.length), (Integer.SIZE / 8) - TLV_LENGTH_F_LENGTH);
79
80                 final byte[] bytes = new byte[TLV_HEADER_LENGTH + valueBytes.length + getPadding(TLV_HEADER_LENGTH + valueBytes.length, PADDED_TO)];
81
82                 int byteOffset = 0;
83                 System.arraycopy(typeBytes, 0, bytes, byteOffset, TLV_TYPE_F_LENGTH);
84                 System.arraycopy(lengthBytes, 0, bytes, byteOffset += TLV_TYPE_F_LENGTH, TLV_LENGTH_F_LENGTH);
85                 System.arraycopy(valueBytes, 0, bytes, byteOffset += TLV_LENGTH_F_LENGTH, valueBytes.length);
86                 return bytes;
87         }
88
89         public abstract void addTlv(final BUILDER builder, final Tlv tlv);
90
91         private static int getPadding(final int length, final int padding) {
92                 return (padding - (length % padding)) % padding;
93         }
94 }