Revert "BUG-47 : unfinished PCEP migration to generated DTOs."
[bgpcep.git] / pcep / impl / src / main / java / org / opendaylight / protocol / pcep / impl / tlv / PCEStatefulCapabilityTlvParser.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.impl.tlv;
9
10 import java.util.BitSet;
11
12 import org.opendaylight.protocol.pcep.PCEPDeserializerException;
13 import org.opendaylight.protocol.pcep.tlv.PCEStatefulCapabilityTlv;
14 import org.opendaylight.protocol.util.ByteArray;
15
16 /**
17  * Parser for {@link org.opendaylight.protocol.pcep.tlv.PCEStatefulCapabilityTlv
18  * PCEStatefulCapabilityTlv}
19  * 
20  * @see <a
21  *      href="http://www.ietf.org/id/draft-crabbe-pce-pce-initiated-lsp-00.txt#section-4.1">
22  *      Stateful PCE Capability TLV</a>
23  */
24 public final class PCEStatefulCapabilityTlvParser {
25     /*
26      * Flags field length in Bytes
27      */
28     public static final int FLAGS_F_LENGTH = 4;
29
30     /*
31      * Offsets inside flags field in bits;
32      */
33     public static final int I_FLAG_OFFSET = 29;
34     public static final int S_FLAG_OFFSET = 30;
35     public static final int U_FLAG_OFFSET = 31;
36
37     public static PCEStatefulCapabilityTlv deserializeValueField(byte[] valueBytes) throws PCEPDeserializerException {
38         if (valueBytes == null || valueBytes.length == 0)
39             throw new IllegalArgumentException("Value bytes array is mandatory. Can't be null or empty.");
40         if (valueBytes.length < FLAGS_F_LENGTH)
41             throw new PCEPDeserializerException("Wrong length of array of bytes. Passed: " + valueBytes.length + "; Expected: >= " + FLAGS_F_LENGTH + ".");
42
43         final BitSet flags = ByteArray.bytesToBitSet(ByteArray.subByte(valueBytes, 0, FLAGS_F_LENGTH));
44         return new PCEStatefulCapabilityTlv(flags.get(I_FLAG_OFFSET), flags.get(U_FLAG_OFFSET), flags.get(S_FLAG_OFFSET));
45     }
46
47     public static byte[] serializeValueField(PCEStatefulCapabilityTlv objToSerialize) {
48         if (objToSerialize == null)
49             throw new IllegalArgumentException("PCEStatefulCapabilityTlv is mandatory.");
50
51         final BitSet flags = new BitSet(FLAGS_F_LENGTH * Byte.SIZE);
52         flags.set(I_FLAG_OFFSET, objToSerialize.isInstantiated());
53         flags.set(U_FLAG_OFFSET, objToSerialize.isUpdate());
54         flags.set(S_FLAG_OFFSET, objToSerialize.isVersioned());
55
56         return ByteArray.bitSetToBytes(flags, FLAGS_F_LENGTH);
57     }
58 }