Add new revision for pcep types model
[bgpcep.git] / pcep / ietf-stateful07 / src / main / java / org / opendaylight / protocol / pcep / ietf / stateful07 / Stateful07StatefulCapabilityTlvParser.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.stateful07;
9
10 import com.google.common.base.Preconditions;
11 import io.netty.buffer.ByteBuf;
12 import io.netty.buffer.Unpooled;
13 import org.opendaylight.protocol.pcep.spi.PCEPDeserializerException;
14 import org.opendaylight.protocol.pcep.spi.TlvParser;
15 import org.opendaylight.protocol.pcep.spi.TlvSerializer;
16 import org.opendaylight.protocol.pcep.spi.TlvUtil;
17 import org.opendaylight.protocol.util.BitArray;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev181109.stateful.capability.tlv.Stateful;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev181109.stateful.capability.tlv.StatefulBuilder;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.Tlv;
21
22 /**
23  * Parser for {@link Stateful}
24  */
25 public class Stateful07StatefulCapabilityTlvParser implements TlvParser, TlvSerializer {
26
27     public static final int TYPE = 16;
28
29     protected static final int FLAGS_F_LENGTH = 32;
30
31     protected static final int U_FLAG_OFFSET = 31;
32
33     @Override
34     public Stateful parseTlv(final ByteBuf buffer) throws PCEPDeserializerException {
35         if (buffer == null) {
36             return null;
37         }
38         if (buffer.readableBytes() < FLAGS_F_LENGTH / Byte.SIZE) {
39             throw new PCEPDeserializerException("Wrong length of array of bytes. Passed: " + buffer.readableBytes() + "; Expected: >= "
40                     + FLAGS_F_LENGTH / Byte.SIZE + ".");
41         }
42         final StatefulBuilder sb = new StatefulBuilder();
43         parseFlags(sb, buffer);
44         return sb.build();
45     }
46
47     protected void parseFlags(final StatefulBuilder sb, final ByteBuf buffer) {
48         final BitArray flags = BitArray.valueOf(buffer, FLAGS_F_LENGTH);
49         sb.setLspUpdateCapability(flags.get(U_FLAG_OFFSET));
50     }
51
52     @Override
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         TlvUtil.formatTlv(TYPE, Unpooled.wrappedBuffer(serializeFlags(sct).array()), buffer);
57     }
58
59     protected BitArray serializeFlags(final Stateful sct) {
60         final BitArray flags = new BitArray(FLAGS_F_LENGTH);
61         flags.set(U_FLAG_OFFSET, sct.isLspUpdateCapability());
62         return flags;
63     }
64 }