2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.protocol.pcep.impl.tlv;
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;
19 * Parser for {@link org.opendaylight.protocol.pcep.tlv.LSPIdentifiersTlv LSPIdentifiersTlv}
20 * parameterized as IPv4Address
22 public class LSPIdentifierIPv4TlvParser {
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;
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;
34 private static final int SIZE = EX_TUNNEL_ID_F_OFFSET + EX_TUNNEL_ID_F_LENGTH;
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 + ".");
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))));
48 public static byte[] put(IPv4LSPIdentifiersTlv objToSerialize) {
49 if (objToSerialize == null)
50 throw new IllegalArgumentException("IPv4LSPIdentifiersTlv is mandatory.");
52 final byte[] retBytes = new byte[SIZE];
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);