Promote MessageRegistry to pcep-api
[bgpcep.git] / pcep / ietf-stateful / src / main / java / org / opendaylight / protocol / pcep / ietf / stateful / StatefulStatefulCapabilityTlvParser.java
1 /*
2  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.protocol.pcep.ietf.stateful;
9
10 import static com.google.common.base.Preconditions.checkArgument;
11
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;
22
23 /**
24  * Parser for {@link Stateful}.
25  */
26 public class StatefulStatefulCapabilityTlvParser implements TlvParser, TlvSerializer {
27
28     public static final int TYPE = 16;
29
30     protected static final int FLAGS_F_LENGTH = 32;
31
32     protected static final int U_FLAG_OFFSET = 31;
33
34     @Override
35     public Stateful parseTlv(final ByteBuf buffer) throws PCEPDeserializerException {
36         if (buffer == null) {
37             return null;
38         }
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 + ".");
42         }
43         final StatefulBuilder sb = new StatefulBuilder();
44         parseFlags(sb, buffer);
45         return sb.build();
46     }
47
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));
51     }
52
53     @Override
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);
58     }
59
60     protected BitArray serializeFlags(final Stateful sct) {
61         final BitArray flags = new BitArray(FLAGS_F_LENGTH);
62         flags.set(U_FLAG_OFFSET, sct.getLspUpdateCapability());
63         return flags;
64     }
65 }