BUG-612 : switched PCEP tlv serializers to ByteBuf
[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 com.google.common.base.Preconditions;
11
12 import io.netty.buffer.ByteBuf;
13 import io.netty.buffer.Unpooled;
14
15 import java.util.BitSet;
16
17 import org.opendaylight.protocol.pcep.ietf.stateful07.Stateful07StatefulCapabilityTlvParser;
18 import org.opendaylight.protocol.pcep.spi.PCEPDeserializerException;
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.initiated.rev131126.Stateful1;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.crabbe.initiated.rev131126.Stateful1Builder;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.stateful.capability.tlv.Stateful;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.stateful.capability.tlv.StatefulBuilder;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Tlv;
26
27 /**
28  * Parser for {@link Stateful}
29  */
30 public final class CInitiated00StatefulCapabilityTlvParser extends Stateful07StatefulCapabilityTlvParser {
31
32     private static final int I_FLAG_OFFSET = 29;
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
45         final StatefulBuilder sb = new StatefulBuilder();
46         sb.setLspUpdateCapability(flags.get(U_FLAG_OFFSET));
47
48         if (flags.get(I_FLAG_OFFSET)) {
49             sb.addAugmentation(Stateful1.class, new Stateful1Builder().setInitiation(Boolean.TRUE).build());
50         }
51         return sb.build();
52     }
53
54     @Override
55     public void serializeTlv(final Tlv tlv, final ByteBuf buffer) {
56         Preconditions.checkArgument(tlv != null, "StatefulCapabilityTlv is mandatory.");
57         final Stateful sct = (Stateful) tlv;
58         final ByteBuf body = Unpooled.buffer();
59         final BitSet flags = new BitSet(FLAGS_F_LENGTH * Byte.SIZE);
60         final Stateful1 sfi = sct.getAugmentation(Stateful1.class);
61         if (sfi != null) {
62             flags.set(I_FLAG_OFFSET, sfi.isInitiation());
63         }
64         if (sct.isLspUpdateCapability() != null) {
65             flags.set(U_FLAG_OFFSET, sct.isLspUpdateCapability());
66         }
67         body.writeBytes(ByteArray.bitSetToBytes(flags, FLAGS_F_LENGTH));
68         TlvUtil.formatTlv(TYPE, body, buffer);
69     }
70 }