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