BUG-64 : reformat TlvRegistry, to skip using getType method.
[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 java.util.BitSet;
11
12 import org.opendaylight.protocol.pcep.ietf.stateful07.Stateful07StatefulCapabilityTlvParser;
13 import org.opendaylight.protocol.pcep.impl.tlv.TlvUtil;
14 import org.opendaylight.protocol.pcep.spi.PCEPDeserializerException;
15 import org.opendaylight.protocol.util.ByteArray;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.crabbe.initiated.rev131126.Stateful1;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.crabbe.initiated.rev131126.Stateful1Builder;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.stateful.capability.tlv.Stateful;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.stateful.capability.tlv.StatefulBuilder;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Tlv;
21
22 /**
23  * Parser for {@link Stateful}
24  */
25 public final class CInitiated00StatefulCapabilityTlvParser extends Stateful07StatefulCapabilityTlvParser {
26
27         private static final int I_FLAG_OFFSET = 29;
28
29         @Override
30         public Stateful parseTlv(final byte[] buffer) throws PCEPDeserializerException {
31                 if (buffer == null || buffer.length == 0) {
32                         throw new IllegalArgumentException("Value bytes array is mandatory. Can't be null or empty.");
33                 }
34                 if (buffer.length < FLAGS_F_LENGTH) {
35                         throw new PCEPDeserializerException("Wrong length of array of bytes. Passed: " + buffer.length + "; Expected: >= "
36                                         + FLAGS_F_LENGTH + ".");
37                 }
38                 final BitSet flags = ByteArray.bytesToBitSet(ByteArray.subByte(buffer, 0, FLAGS_F_LENGTH));
39
40                 final StatefulBuilder sb = new StatefulBuilder();
41                 sb.setLspUpdateCapability(flags.get(U_FLAG_OFFSET));
42
43                 if (flags.get(I_FLAG_OFFSET)) {
44                         sb.addAugmentation(Stateful1.class, new Stateful1Builder().setInitiation(Boolean.TRUE).build());
45                 }
46                 return sb.build();
47         }
48
49         @Override
50         public byte[] serializeTlv(final Tlv tlv) {
51                 if (tlv == null) {
52                         throw new IllegalArgumentException("StatefulCapabilityTlv is mandatory.");
53                 }
54                 final Stateful sct = (Stateful) tlv;
55
56                 final BitSet flags = new BitSet(FLAGS_F_LENGTH * Byte.SIZE);
57
58                 final Stateful1 sfi = sct.getAugmentation(Stateful1.class);
59                 if (sfi != null) {
60                         flags.set(I_FLAG_OFFSET, sfi.isInitiation());
61                 }
62                 flags.set(U_FLAG_OFFSET, sct.isLspUpdateCapability());
63                 return TlvUtil.formatTlv(TYPE, ByteArray.bitSetToBytes(flags, FLAGS_F_LENGTH));
64         }
65 }