Revert "BUG-47 : unfinished PCEP migration to generated DTOs."
[bgpcep.git] / pcep / impl / src / main / java / org / opendaylight / protocol / pcep / impl / tlv / LSPIdentifierIPv4TlvParser.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 org.opendaylight.protocol.concepts.IPv4Address;
11 import org.opendaylight.protocol.pcep.PCEPDeserializerException;
12 import org.opendaylight.protocol.pcep.concepts.IPv4ExtendedTunnelIdentifier;
13 import org.opendaylight.protocol.pcep.concepts.LSPIdentifier;
14 import org.opendaylight.protocol.pcep.concepts.TunnelIdentifier;
15 import org.opendaylight.protocol.pcep.tlv.IPv4LSPIdentifiersTlv;
16 import org.opendaylight.protocol.util.ByteArray;
17
18 /**
19  * Parser for {@link org.opendaylight.protocol.pcep.tlv.LSPIdentifiersTlv LSPIdentifiersTlv}
20  * parameterized as IPv4Address
21  */
22 public class LSPIdentifierIPv4TlvParser {
23
24         private static final int IP_F_LENGTH = 4;
25         private static final int LSP_ID_F_LENGTH = 2;
26         private static final int TUNNEL_ID_F_LENGTH = 2;
27         private static final int EX_TUNNEL_ID_F_LENGTH = 4;
28
29         private static final int IP_F_OFFSET = 0;
30         private static final int LSP_ID_F_OFFSET = IP_F_OFFSET + IP_F_LENGTH;
31         private static final int TUNNLE_ID_F_OFFSET = LSP_ID_F_OFFSET + LSP_ID_F_LENGTH;
32         private static final int EX_TUNNEL_ID_F_OFFSET = TUNNLE_ID_F_OFFSET + TUNNEL_ID_F_LENGTH;
33
34         private static final int SIZE = EX_TUNNEL_ID_F_OFFSET + EX_TUNNEL_ID_F_LENGTH;
35
36         public static IPv4LSPIdentifiersTlv parse(byte[] valueBytes) throws PCEPDeserializerException {
37                 if (valueBytes == null || valueBytes.length == 0)
38                         throw new IllegalArgumentException("Value bytes array is mandatory. Can't be null or empty.");
39                 if (valueBytes.length != SIZE)
40                         throw new PCEPDeserializerException("Wrong length of array of bytes. Passed: " + valueBytes.length + "; Expected: " + SIZE + ".");
41
42                 return new IPv4LSPIdentifiersTlv(new IPv4Address(
43                                 ByteArray.subByte(valueBytes, IP_F_OFFSET, IP_F_LENGTH)), new LSPIdentifier(ByteArray.subByte(valueBytes, LSP_ID_F_OFFSET, LSP_ID_F_LENGTH)),
44                                 new TunnelIdentifier(ByteArray.subByte(valueBytes, TUNNLE_ID_F_OFFSET, TUNNEL_ID_F_LENGTH)),
45                                 new IPv4ExtendedTunnelIdentifier(new IPv4Address(ByteArray.subByte(valueBytes, EX_TUNNEL_ID_F_OFFSET, EX_TUNNEL_ID_F_LENGTH))));
46         }
47
48         public static byte[] put(IPv4LSPIdentifiersTlv objToSerialize) {
49                 if (objToSerialize == null)
50                         throw new IllegalArgumentException("IPv4LSPIdentifiersTlv is mandatory.");
51
52                 final byte[] retBytes = new byte[SIZE];
53
54                 ByteArray.copyWhole(objToSerialize.getSenderAddress().getAddress(), retBytes, IP_F_OFFSET);
55                 ByteArray.copyWhole(objToSerialize.getLspID().getLspId(), retBytes, LSP_ID_F_OFFSET);
56                 ByteArray.copyWhole(objToSerialize.getTunnelID().getBytes(), retBytes, TUNNLE_ID_F_OFFSET);
57                 ByteArray.copyWhole(objToSerialize.getExtendedTunnelID().getIdentifier().getAddress(), retBytes, EX_TUNNEL_ID_F_OFFSET);
58
59                 return retBytes;
60         }
61
62 }