9badab21cbde656aada9f178423a0fd1b106e82a
[bgpcep.git] / pcep / ietf-stateful02 / src / main / java / org / opendaylight / protocol / pcep / crabbe / initiated00 / PCEStatefulCapabilityTlvParser.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.crabbe.initiated00;
9
10 import java.util.BitSet;
11
12 import org.opendaylight.protocol.pcep.spi.PCEPDeserializerException;
13 import org.opendaylight.protocol.pcep.spi.TlvParser;
14 import org.opendaylight.protocol.pcep.spi.TlvSerializer;
15 import org.opendaylight.protocol.util.ByteArray;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.crabbe.initiated._00.rev140113.Stateful1;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.crabbe.initiated._00.rev140113.Stateful1Builder;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.crabbe.stateful._02.rev140110.stateful.capability.tlv.Stateful;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.crabbe.stateful._02.rev140110.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 PCEStatefulCapabilityTlvParser implements TlvParser, TlvSerializer {
26
27         public static final int TYPE = 16;
28
29         private static final int FLAGS_F_LENGTH = 4;
30
31         private static final int I_FLAG_OFFSET = 29;
32         private static final int S_FLAG_OFFSET = 30;
33         private static final int U_FLAG_OFFSET = 31;
34
35         @Override
36         public Stateful parseTlv(final byte[] buffer) throws PCEPDeserializerException {
37                 if (buffer == null || buffer.length == 0) {
38                         throw new IllegalArgumentException("Value bytes array is mandatory. Can't be null or empty.");
39                 }
40                 if (buffer.length < FLAGS_F_LENGTH) {
41                         throw new PCEPDeserializerException("Wrong length of array of bytes. Passed: " + buffer.length + "; Expected: >= "
42                                         + FLAGS_F_LENGTH + ".");
43                 }
44
45                 final BitSet flags = ByteArray.bytesToBitSet(ByteArray.subByte(buffer, 0, FLAGS_F_LENGTH));
46
47                 final StatefulBuilder sb = new StatefulBuilder();
48                 sb.setIncludeDbVersion(flags.get(S_FLAG_OFFSET));
49                 sb.setLspUpdateCapability(flags.get(U_FLAG_OFFSET));
50
51                 if (flags.get(I_FLAG_OFFSET)) {
52                         sb.addAugmentation(Stateful1.class, new Stateful1Builder().setInitiation(Boolean.TRUE).build());
53                 }
54
55                 return sb.build();
56         }
57
58         @Override
59         public byte[] serializeTlv(final Tlv tlv) {
60                 if (tlv == null) {
61                         throw new IllegalArgumentException("StatefulCapabilityTlv is mandatory.");
62                 }
63                 final Stateful sct = (Stateful) tlv;
64
65                 final BitSet flags = new BitSet(FLAGS_F_LENGTH * Byte.SIZE);
66
67                 final Stateful1 sfi = sct.getAugmentation(Stateful1.class);
68                 if (sfi != null) {
69                         flags.set(I_FLAG_OFFSET, sfi.isInitiation());
70                 }
71                 if (sct.isLspUpdateCapability() != null && sct.isLspUpdateCapability()) {
72                         flags.set(U_FLAG_OFFSET, sct.isLspUpdateCapability());
73                 }
74                 if (sct.isIncludeDbVersion() != null && sct.isIncludeDbVersion()) {
75                         flags.set(S_FLAG_OFFSET, sct.isIncludeDbVersion());
76                 }
77                 return ByteArray.bitSetToBytes(flags, FLAGS_F_LENGTH);
78         }
79
80         @Override
81         public int getType() {
82                 return TYPE;
83         }
84 }