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.pcep.PCEPDeserializerException;
13 import org.opendaylight.protocol.pcep.tlv.PCEStatefulCapabilityTlv;
14 import org.opendaylight.protocol.util.ByteArray;
17 * Parser for {@link org.opendaylight.protocol.pcep.tlv.PCEStatefulCapabilityTlv
18 * PCEStatefulCapabilityTlv}
21 * href="http://www.ietf.org/id/draft-crabbe-pce-pce-initiated-lsp-00.txt#section-4.1">
22 * Stateful PCE Capability TLV</a>
24 public final class PCEStatefulCapabilityTlvParser {
26 * Flags field length in Bytes
28 public static final int FLAGS_F_LENGTH = 4;
31 * Offsets inside flags field in bits;
33 public static final int I_FLAG_OFFSET = 29;
34 public static final int S_FLAG_OFFSET = 30;
35 public static final int U_FLAG_OFFSET = 31;
37 public static PCEStatefulCapabilityTlv deserializeValueField(byte[] valueBytes) throws PCEPDeserializerException {
38 if (valueBytes == null || valueBytes.length == 0)
39 throw new IllegalArgumentException("Value bytes array is mandatory. Can't be null or empty.");
40 if (valueBytes.length < FLAGS_F_LENGTH)
41 throw new PCEPDeserializerException("Wrong length of array of bytes. Passed: " + valueBytes.length + "; Expected: >= " + FLAGS_F_LENGTH + ".");
43 final BitSet flags = ByteArray.bytesToBitSet(ByteArray.subByte(valueBytes, 0, FLAGS_F_LENGTH));
44 return new PCEStatefulCapabilityTlv(flags.get(I_FLAG_OFFSET), flags.get(U_FLAG_OFFSET), flags.get(S_FLAG_OFFSET));
47 public static byte[] serializeValueField(PCEStatefulCapabilityTlv objToSerialize) {
48 if (objToSerialize == null)
49 throw new IllegalArgumentException("PCEStatefulCapabilityTlv is mandatory.");
51 final BitSet flags = new BitSet(FLAGS_F_LENGTH * Byte.SIZE);
52 flags.set(I_FLAG_OFFSET, objToSerialize.isInstantiated());
53 flags.set(U_FLAG_OFFSET, objToSerialize.isUpdate());
54 flags.set(S_FLAG_OFFSET, objToSerialize.isVersioned());
56 return ByteArray.bitSetToBytes(flags, FLAGS_F_LENGTH);