Merge "Bug-731: pcep Tlv serializers - fixed sonar issues"
[bgpcep.git] / pcep / ietf-stateful07 / src / main / java / org / opendaylight / protocol / pcep / ietf / initiated00 / CInitiated00StatefulCapabilityTlvParser.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.initiated00;
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.ietf.stateful07.Stateful07StatefulCapabilityTlvParser;
17 import org.opendaylight.protocol.pcep.spi.PCEPDeserializerException;
18 import org.opendaylight.protocol.pcep.spi.TlvUtil;
19 import org.opendaylight.protocol.util.ByteArray;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.crabbe.initiated.rev131126.Stateful1;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.crabbe.initiated.rev131126.Stateful1Builder;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.stateful.capability.tlv.Stateful;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.stateful.capability.tlv.StatefulBuilder;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Tlv;
25
26 /**
27  * Parser for {@link Stateful}
28  */
29 public final class CInitiated00StatefulCapabilityTlvParser extends Stateful07StatefulCapabilityTlvParser {
30
31     private static final int I_FLAG_OFFSET = 29;
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) {
39             throw new PCEPDeserializerException("Wrong length of array of bytes. Passed: " + buffer.readableBytes() + "; Expected: >= "
40                     + FLAGS_F_LENGTH + ".");
41         }
42         final BitSet flags = ByteArray.bytesToBitSet(ByteArray.readBytes(buffer, FLAGS_F_LENGTH));
43
44         final StatefulBuilder sb = new StatefulBuilder();
45         sb.setLspUpdateCapability(flags.get(U_FLAG_OFFSET));
46
47         if (flags.get(I_FLAG_OFFSET)) {
48             sb.addAugmentation(Stateful1.class, new Stateful1Builder().setInitiation(Boolean.TRUE).build());
49         }
50         return sb.build();
51     }
52
53     @Override
54     public void serializeTlv(final Tlv tlv, final ByteBuf buffer) {
55         Preconditions.checkArgument(tlv instanceof Stateful, "StatefulCapabilityTlv is mandatory.");
56         final Stateful sct = (Stateful) tlv;
57         final ByteBuf body = Unpooled.buffer();
58         final BitSet flags = new BitSet(FLAGS_F_LENGTH * Byte.SIZE);
59         final Stateful1 sfi = sct.getAugmentation(Stateful1.class);
60         if (sfi != null) {
61             flags.set(I_FLAG_OFFSET, sfi.isInitiation());
62         }
63         if (sct.isLspUpdateCapability() != null) {
64             flags.set(U_FLAG_OFFSET, sct.isLspUpdateCapability());
65         }
66         writeBitSet(flags, FLAGS_F_LENGTH, body);
67         TlvUtil.formatTlv(TYPE, body, buffer);
68     }
69 }