Revert "BUG-47 : unfinished PCEP migration to generated DTOs."
[bgpcep.git] / pcep / impl / src / main / java / org / opendaylight / protocol / pcep / impl / tlv / RSVPErrorSpecIPv6TlvParser.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.tlv;
9
10 import java.util.BitSet;
11
12 import org.opendaylight.protocol.concepts.IPv6Address;
13 import org.opendaylight.protocol.pcep.PCEPDeserializerException;
14 import org.opendaylight.protocol.pcep.tlv.RSVPErrorSpecTlv;
15 import org.opendaylight.protocol.util.ByteArray;
16
17 /**
18  * Parser for {@link org.opendaylight.protocol.pcep.tlv.RSVPErrorSpecTlv RSVPErrorSpecTlv}
19  * parameterized as IPv6Address
20  */
21 public class RSVPErrorSpecIPv6TlvParser {
22
23         private static final int IP_F_LENGTH = 16;
24         private static final int FLAGS_F_LENGTH = 1;
25         private static final int ERROR_CODE_F_LENGTH = 1;
26         private static final int ERROR_VALUE_F_LENGTH = 2;
27
28         private static final int IP_F_OFFSET = 0;
29         private static final int FLAGS_F_OFFSET = IP_F_OFFSET + IP_F_LENGTH;
30         private static final int ERROR_CODE_F_OFFSET = FLAGS_F_OFFSET + FLAGS_F_LENGTH;
31         private static final int ERROR_VALUE_F_OFFSET = ERROR_CODE_F_OFFSET + ERROR_CODE_F_LENGTH;
32
33         private static final int SIZE = ERROR_VALUE_F_OFFSET + ERROR_VALUE_F_LENGTH;
34
35         /*
36          * flags offsets inside flags field in bits
37          */
38         private static final int IN_PLACE_FLAG_OFFSET = 7;
39         private static final int NOT_GUILTY_FLAGS_OFFSET = 6;
40
41         public static RSVPErrorSpecTlv<IPv6Address> parse(byte[] valueBytes) throws PCEPDeserializerException {
42                 if (valueBytes == null || valueBytes.length == 0)
43                         throw new IllegalArgumentException("Value bytes array is mandatory. Can't be null or empty.");
44                 if (valueBytes.length != SIZE) {
45                         throw new PCEPDeserializerException("Wrong length of array of bytes. Passed: " + valueBytes.length + "; Expected: " + SIZE + ".");
46                 }
47
48                 final BitSet flags = ByteArray.bytesToBitSet(ByteArray.subByte(valueBytes, FLAGS_F_OFFSET, FLAGS_F_LENGTH));
49
50                 return new RSVPErrorSpecTlv<IPv6Address>(new IPv6Address(
51                                 ByteArray.subByte(valueBytes, IP_F_OFFSET, IP_F_LENGTH)), flags.get(IN_PLACE_FLAG_OFFSET), flags.get(NOT_GUILTY_FLAGS_OFFSET),
52                                 valueBytes[ERROR_CODE_F_OFFSET] & 0xFF,
53                                 ByteArray.bytesToShort(ByteArray.subByte(valueBytes, ERROR_VALUE_F_OFFSET, ERROR_VALUE_F_LENGTH)) & 0xFFFF);
54         }
55
56         public static byte[] put(RSVPErrorSpecTlv<?> objToSerialize) {
57                 if (objToSerialize == null)
58                         throw new IllegalArgumentException("RSVPErrorSpecTlv is mandatory.");
59
60                 if (!(((RSVPErrorSpecTlv<?>) objToSerialize).getErrorNodeAddress() instanceof IPv6Address))
61                         throw new IllegalArgumentException("Unknown parametrized type of RSVPErrorSpecTlv. Passed "
62                                         + ((RSVPErrorSpecTlv<?>) objToSerialize).getErrorNodeAddress().getClass() + ". Needed IPv6Address.");
63
64                 final BitSet flags = new BitSet(FLAGS_F_LENGTH * Byte.SIZE);
65                 flags.set(IN_PLACE_FLAG_OFFSET, objToSerialize.isInPlace());
66                 flags.set(NOT_GUILTY_FLAGS_OFFSET, objToSerialize.isGuilty());
67
68                 final byte[] retBytes = new byte[SIZE];
69
70                 ByteArray.copyWhole(((IPv6Address) objToSerialize.getErrorNodeAddress()).getAddress(), retBytes, IP_F_OFFSET);
71                 retBytes[ERROR_CODE_F_OFFSET] = ByteArray.intToBytes(objToSerialize.getErrorCode())[Integer.SIZE / Byte.SIZE - 1];
72                 System.arraycopy(ByteArray.intToBytes(objToSerialize.getErrorValue()), Integer.SIZE / Byte.SIZE - ERROR_VALUE_F_LENGTH, retBytes, ERROR_VALUE_F_OFFSET,
73                                 ERROR_VALUE_F_LENGTH);
74                 ByteArray.copyWhole(ByteArray.bitSetToBytes(flags, FLAGS_F_LENGTH), retBytes, FLAGS_F_OFFSET);
75
76                 return retBytes;
77         }
78
79 }