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.IPv6Address;
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 IPv6Address
21 public class RSVPErrorSpecIPv6TlvParser {
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;
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<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 + ".");
48 final BitSet flags = ByteArray.bytesToBitSet(ByteArray.subByte(valueBytes, FLAGS_F_OFFSET, FLAGS_F_LENGTH));
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);
56 public static byte[] put(RSVPErrorSpecTlv<?> objToSerialize) {
57 if (objToSerialize == null)
58 throw new IllegalArgumentException("RSVPErrorSpecTlv is mandatory.");
60 if (!(((RSVPErrorSpecTlv<?>) objToSerialize).getErrorNodeAddress() instanceof IPv6Address))
61 throw new IllegalArgumentException("Unknown parametrized type of RSVPErrorSpecTlv. Passed "
62 + ((RSVPErrorSpecTlv<?>) objToSerialize).getErrorNodeAddress().getClass() + ". Needed IPv6Address.");
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());
68 final byte[] retBytes = new byte[SIZE];
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);