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.ietf.stateful07;
10 import static org.opendaylight.protocol.util.ByteBufWriteUtil.writeBitSet;
12 import com.google.common.base.Preconditions;
13 import io.netty.buffer.ByteBuf;
14 import io.netty.buffer.Unpooled;
15 import java.util.BitSet;
16 import org.opendaylight.protocol.pcep.spi.PCEPDeserializerException;
17 import org.opendaylight.protocol.pcep.spi.TlvParser;
18 import org.opendaylight.protocol.pcep.spi.TlvSerializer;
19 import org.opendaylight.protocol.pcep.spi.TlvUtil;
20 import org.opendaylight.protocol.util.ByteArray;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.stateful.capability.tlv.Stateful;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.stateful.capability.tlv.StatefulBuilder;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Tlv;
26 * Parser for {@link Stateful}
28 public class Stateful07StatefulCapabilityTlvParser implements TlvParser, TlvSerializer {
30 public static final int TYPE = 16;
32 protected static final int FLAGS_F_LENGTH = 4;
34 protected static final int U_FLAG_OFFSET = 31;
37 public Stateful parseTlv(final ByteBuf buffer) throws PCEPDeserializerException {
41 if (buffer.readableBytes() < FLAGS_F_LENGTH) {
42 throw new PCEPDeserializerException("Wrong length of array of bytes. Passed: " + buffer.readableBytes() + "; Expected: >= "
43 + FLAGS_F_LENGTH + ".");
45 final BitSet flags = ByteArray.bytesToBitSet(ByteArray.readBytes(buffer, FLAGS_F_LENGTH));
47 final StatefulBuilder sb = new StatefulBuilder();
48 sb.setLspUpdateCapability(flags.get(U_FLAG_OFFSET));
53 public void serializeTlv(final Tlv tlv, final ByteBuf buffer) {
54 Preconditions.checkArgument(tlv instanceof Stateful, "StatefulCapabilityTlv is mandatory.");
55 final Stateful sct = (Stateful) tlv;
56 final ByteBuf body = Unpooled.buffer();
57 final BitSet flags = new BitSet(FLAGS_F_LENGTH * Byte.SIZE);
58 if (sct.isLspUpdateCapability() != null) {
59 flags.set(U_FLAG_OFFSET, sct.isLspUpdateCapability());
61 writeBitSet(flags, FLAGS_F_LENGTH, body);
62 TlvUtil.formatTlv(TYPE, body, buffer);