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.stateful;
10 import static com.google.common.base.Preconditions.checkArgument;
12 import io.netty.buffer.ByteBuf;
13 import io.netty.buffer.Unpooled;
14 import org.opendaylight.protocol.pcep.PCEPDeserializerException;
15 import org.opendaylight.protocol.pcep.spi.TlvParser;
16 import org.opendaylight.protocol.pcep.spi.TlvSerializer;
17 import org.opendaylight.protocol.pcep.spi.TlvUtil;
18 import org.opendaylight.protocol.util.BitArray;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev200720.stateful.capability.tlv.Stateful;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev200720.stateful.capability.tlv.StatefulBuilder;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.Tlv;
24 * Parser for {@link Stateful}.
26 public class StatefulStatefulCapabilityTlvParser implements TlvParser, TlvSerializer {
28 public static final int TYPE = 16;
30 protected static final int FLAGS_F_LENGTH = 32;
32 protected static final int U_FLAG_OFFSET = 31;
35 public Stateful parseTlv(final ByteBuf buffer) throws PCEPDeserializerException {
39 if (buffer.readableBytes() < FLAGS_F_LENGTH / Byte.SIZE) {
40 throw new PCEPDeserializerException("Wrong length of array of bytes. Passed: " + buffer.readableBytes()
41 + "; Expected: >= " + FLAGS_F_LENGTH / Byte.SIZE + ".");
43 final StatefulBuilder sb = new StatefulBuilder();
44 parseFlags(sb, buffer);
48 protected void parseFlags(final StatefulBuilder sb, final ByteBuf buffer) {
49 final BitArray flags = BitArray.valueOf(buffer, FLAGS_F_LENGTH);
50 sb.setLspUpdateCapability(flags.get(U_FLAG_OFFSET));
54 public void serializeTlv(final Tlv tlv, final ByteBuf buffer) {
55 checkArgument(tlv instanceof Stateful, "StatefulCapabilityTlv is mandatory.");
56 final Stateful sct = (Stateful) tlv;
57 TlvUtil.formatTlv(TYPE, Unpooled.wrappedBuffer(serializeFlags(sct).array()), buffer);
60 protected BitArray serializeFlags(final Stateful sct) {
61 final BitArray flags = new BitArray(FLAGS_F_LENGTH);
62 flags.set(U_FLAG_OFFSET, sct.getLspUpdateCapability());