Bug-731: pcep Tlv serializers - fixed sonar issues
[bgpcep.git] / pcep / ietf-stateful02 / src / main / java / org / opendaylight / protocol / pcep / ietf / stateful02 / Stateful02StatefulCapabilityTlvParser.java
1 /*
2  * Copyright (c) 2014 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.stateful02;
9
10 import static org.opendaylight.protocol.util.ByteBufWriteUtil.writeBitSet;
11
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.crabbe.stateful._02.rev140110.stateful.capability.tlv.Stateful;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.crabbe.stateful._02.rev140110.stateful.capability.tlv.StatefulBuilder;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Tlv;
24
25 public class Stateful02StatefulCapabilityTlvParser implements TlvParser, TlvSerializer {
26
27     public static final int TYPE = 16;
28
29     protected static final int FLAGS_F_LENGTH = 4;
30
31     protected static final int S_FLAG_OFFSET = 30;
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) {
40             throw new PCEPDeserializerException("Wrong length of array of bytes. Passed: " + buffer.readableBytes() + "; Expected: >= "
41                     + FLAGS_F_LENGTH + ".");
42         }
43         final BitSet flags = ByteArray.bytesToBitSet(ByteArray.readBytes(buffer, FLAGS_F_LENGTH));
44         final StatefulBuilder sb = new StatefulBuilder();
45         sb.setIncludeDbVersion(flags.get(S_FLAG_OFFSET));
46         sb.setLspUpdateCapability(flags.get(U_FLAG_OFFSET));
47         return sb.build();
48     }
49
50     @Override
51     public void serializeTlv(final Tlv tlv, final ByteBuf buffer) {
52         Preconditions.checkArgument(tlv instanceof Stateful, "StatefulCapabilityTlv is mandatory.");
53         final Stateful sct = (Stateful) tlv;
54         final ByteBuf body = Unpooled.buffer();
55         final BitSet flags = new BitSet(FLAGS_F_LENGTH * Byte.SIZE);
56         if (sct.isLspUpdateCapability() != null) {
57             flags.set(U_FLAG_OFFSET, sct.isLspUpdateCapability());
58         }
59         if (sct.isIncludeDbVersion() != null) {
60             flags.set(S_FLAG_OFFSET, sct.isIncludeDbVersion());
61         }
62         writeBitSet(flags, FLAGS_F_LENGTH, body);
63         TlvUtil.formatTlv(TYPE, body, buffer);
64     }
65 }