Rework PCEPDeserializerException and move it into SPI
[bgpcep.git] / pcep / impl / src / main / java / org / opendaylight / protocol / pcep / impl / object / 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.object;
9
10 import org.opendaylight.protocol.pcep.spi.ObjectParser;
11 import org.opendaylight.protocol.pcep.spi.ObjectSerializer;
12 import org.opendaylight.protocol.pcep.spi.PCEPDeserializerException;
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<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 TlvHandlerRegistry tlvReg;
33
34         protected AbstractObjectWithTlvsParser(final TlvHandlerRegistry tlvReg) {
35                 this.tlvReg = Preconditions.checkNotNull(tlvReg);
36         }
37
38         protected final void parseTlvs(final T builder, final byte[] bytes) throws PCEPDeserializerException {
39                 if (bytes == null) {
40                         throw new IllegalArgumentException("Byte array is mandatory.");
41                 }
42                 if (bytes.length == 0) {
43                         return;
44                 }
45
46                 int length;
47                 int byteOffset = 0;
48                 int type = 0;
49
50                 while (byteOffset < bytes.length) {
51                         type = ByteArray.bytesToInt(ByteArray.subByte(bytes, byteOffset, TLV_TYPE_F_LENGTH));
52                         byteOffset += TLV_TYPE_F_LENGTH;
53                         length = ByteArray.bytesToInt(ByteArray.subByte(bytes, byteOffset, TLV_LENGTH_F_LENGTH));
54                         byteOffset += TLV_LENGTH_F_LENGTH;
55
56                         if (TLV_HEADER_LENGTH + length > bytes.length) {
57                                 throw new PCEPDeserializerException("Wrong length specified. Passed: " + (TLV_HEADER_LENGTH + length) + "; Expected: <= "
58                                                 + (bytes.length - byteOffset) + ".");
59                         }
60
61                         final byte[] tlvBytes = ByteArray.subByte(bytes, byteOffset, length);
62
63                         LOG.trace("Attempt to parse tlv from bytes: {}", ByteArray.bytesToHexString(tlvBytes));
64                         final Tlv tlv = this.tlvReg.getTlvParser(type).parseTlv(tlvBytes);
65                         LOG.trace("Tlv was parsed. {}", tlv);
66
67                         addTlv(builder, tlv);
68
69                         byteOffset += length + getPadding(TLV_HEADER_LENGTH + length, PADDED_TO);
70                 }
71         }
72
73         protected final byte[] serializeTlv(final Tlv tlv) {
74
75                 final TlvSerializer serializer = this.tlvReg.getTlvSerializer(tlv);
76
77                 final byte[] typeBytes = (ByteArray.cutBytes(ByteArray.intToBytes(serializer.getType()), (Integer.SIZE / 8) - TLV_TYPE_F_LENGTH));
78
79                 final byte[] valueBytes = serializer.serializeTlv(tlv);
80
81                 final byte[] lengthBytes = ByteArray.cutBytes(ByteArray.intToBytes(valueBytes.length), (Integer.SIZE / 8) - TLV_LENGTH_F_LENGTH);
82
83                 final byte[] bytes = new byte[TLV_HEADER_LENGTH + valueBytes.length + getPadding(TLV_HEADER_LENGTH + valueBytes.length, PADDED_TO)];
84
85                 int byteOffset = 0;
86                 System.arraycopy(typeBytes, 0, bytes, byteOffset, TLV_TYPE_F_LENGTH);
87                 byteOffset += TLV_TYPE_F_LENGTH;
88                 System.arraycopy(lengthBytes, 0, bytes, byteOffset, TLV_LENGTH_F_LENGTH);
89                 byteOffset += TLV_LENGTH_F_LENGTH;
90                 System.arraycopy(valueBytes, 0, bytes, byteOffset, valueBytes.length);
91                 return bytes;
92         }
93
94         public abstract void addTlv(final T builder, final Tlv tlv);
95
96         protected static int getPadding(final int length, final int padding) {
97                 return (padding - (length % padding)) % padding;
98         }
99 }