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.spi.TlvParser;
14 import org.opendaylight.protocol.pcep.spi.TlvSerializer;
15 import org.opendaylight.protocol.util.ByteArray;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.StatefulCapabilityTlv;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.StatefulCapabilityTlv.Flags;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Tlv;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.open.object.tlvs.StatefulBuilder;
22 * Parser for {@link StatefulCapabilityTlv}
24 public final class PCEStatefulCapabilityTlvParser implements TlvParser, TlvSerializer {
26 public static final int TYPE = 16;
28 private static final int FLAGS_F_LENGTH = 4;
30 private static final int I_FLAG_OFFSET = 29;
31 private static final int S_FLAG_OFFSET = 30;
32 private static final int U_FLAG_OFFSET = 31;
35 public StatefulCapabilityTlv parseTlv(final byte[] buffer) throws PCEPDeserializerException {
36 if (buffer == null || buffer.length == 0) {
37 throw new IllegalArgumentException("Value bytes array is mandatory. Can't be null or empty.");
39 if (buffer.length < FLAGS_F_LENGTH) {
40 throw new PCEPDeserializerException("Wrong length of array of bytes. Passed: " + buffer.length + "; Expected: >= "
41 + FLAGS_F_LENGTH + ".");
44 final BitSet flags = ByteArray.bytesToBitSet(ByteArray.subByte(buffer, 0, FLAGS_F_LENGTH));
45 return new StatefulBuilder().setFlags(new Flags(flags.get(S_FLAG_OFFSET), flags.get(I_FLAG_OFFSET), flags.get(U_FLAG_OFFSET))).build();
49 public byte[] serializeTlv(final Tlv tlv) {
51 throw new IllegalArgumentException("StatefulCapabilityTlv is mandatory.");
53 final StatefulCapabilityTlv sct = (StatefulCapabilityTlv) tlv;
55 final BitSet flags = new BitSet(FLAGS_F_LENGTH * Byte.SIZE);
56 flags.set(I_FLAG_OFFSET, sct.getFlags().isInitiation());
57 flags.set(U_FLAG_OFFSET, sct.getFlags().isLspUpdateCapability());
58 flags.set(S_FLAG_OFFSET, sct.getFlags().isIncludeDbVersion());
60 return ByteArray.bitSetToBytes(flags, FLAGS_F_LENGTH);
64 public int getType() {