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 java.util.BitSet;
12 import org.opendaylight.protocol.concepts.IPv4Address;
13 import org.opendaylight.protocol.pcep.PCEPDeserializerException;
14 import org.opendaylight.protocol.pcep.tlv.RSVPErrorSpecTlv;
15 import org.opendaylight.protocol.util.ByteArray;
18 * Parser for {@link org.opendaylight.protocol.pcep.tlv.RSVPErrorSpecTlv RSVPErrorSpecTlv}
19 * parameterized as IPv4Address
21 public class RSVPErrorSpecIPv4TlvParser {
23 private static final int IP_F_LENGTH = 4;
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;
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;
33 private static final int SIZE = ERROR_VALUE_F_OFFSET + ERROR_VALUE_F_LENGTH;
36 * flags offsets inside flags field in bits
38 private static final int IN_PLACE_FLAG_OFFSET = 7;
39 private static final int NOT_GUILTY_FLAGS_OFFSET = 6;
41 public static RSVPErrorSpecTlv<IPv4Address> 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 + ".");
47 final BitSet flags = ByteArray.bytesToBitSet(ByteArray.subByte(valueBytes, FLAGS_F_OFFSET, FLAGS_F_LENGTH));
49 return new RSVPErrorSpecTlv<IPv4Address>(new IPv4Address(
50 ByteArray.subByte(valueBytes, IP_F_OFFSET, IP_F_LENGTH)), flags.get(IN_PLACE_FLAG_OFFSET), flags.get(NOT_GUILTY_FLAGS_OFFSET),
51 valueBytes[ERROR_CODE_F_OFFSET] & 0xFF,
52 ByteArray.bytesToShort(ByteArray.subByte(valueBytes, ERROR_VALUE_F_OFFSET, ERROR_VALUE_F_LENGTH)) & 0xFFFF);
55 public static byte[] put(RSVPErrorSpecTlv<?> objToSerialize) {
56 if (objToSerialize == null)
57 throw new IllegalArgumentException("RSVPErrorSpecTlv is mandatory.");
59 if (!(((RSVPErrorSpecTlv<?>) objToSerialize).getErrorNodeAddress() instanceof IPv4Address))
60 throw new IllegalArgumentException("Unknown parametrized type of RSVPErrorSpecTlv. Passed "
61 + ((RSVPErrorSpecTlv<?>) objToSerialize).getErrorNodeAddress().getClass() + ". Needed IPv4Address.");
63 final BitSet flags = new BitSet(FLAGS_F_LENGTH * Byte.SIZE);
64 flags.set(IN_PLACE_FLAG_OFFSET, objToSerialize.isInPlace());
65 flags.set(NOT_GUILTY_FLAGS_OFFSET, objToSerialize.isGuilty());
67 final byte[] retBytes = new byte[SIZE];
69 ByteArray.copyWhole(((IPv4Address) objToSerialize.getErrorNodeAddress()).getAddress(), retBytes, IP_F_OFFSET);
70 retBytes[ERROR_CODE_F_OFFSET] = ByteArray.intToBytes(objToSerialize.getErrorCode())[Integer.SIZE / Byte.SIZE - 1];
71 System.arraycopy(ByteArray.intToBytes(objToSerialize.getErrorValue()), Integer.SIZE / Byte.SIZE - ERROR_VALUE_F_LENGTH, retBytes, ERROR_VALUE_F_OFFSET,
72 ERROR_VALUE_F_LENGTH);
73 ByteArray.copyWhole(ByteArray.bitSetToBytes(flags, FLAGS_F_LENGTH), retBytes, FLAGS_F_OFFSET);