Revert "BUG-47 : unfinished PCEP migration to generated DTOs."
[bgpcep.git] / pcep / impl / src / main / java / org / opendaylight / protocol / pcep / impl / tlv / NoPathVectorTlvParser.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.NoPathVectorTlv;
14 import org.opendaylight.protocol.util.ByteArray;
15
16 /**
17  * Parser for {@link org.opendaylight.protocol.pcep.tlv.NoPathVectorTlv NoPathVectorTlv}
18  */
19 public class NoPathVectorTlvParser {
20
21         public static final int FLAGS_F_LENGTH = 4;
22
23         /*
24          * flags offsets inside flags field in bits
25          */
26         public static final int PCE_UNAVAILABLE = 31;
27         public static final int UNKNOWN_DEST = 30;
28         public static final int UNKNOWN_SRC = 29;
29
30         /*
31          * flags offsets of flags added by GCO extension
32          */
33         public static final int NO_GCO_SOLUTION = 25;
34         public static final int NO_GCO_MIGRATION_PATH = 26;
35
36         /*
37          * flags offsets of flags added by RFC 6006
38          */
39         public static final int REACHABLITY_PROBLEM = 24;
40
41         public static NoPathVectorTlv parse(byte[] valueBytes) throws PCEPDeserializerException {
42                 if (valueBytes == null || valueBytes.length == 0)
43                         throw new IllegalArgumentException("Array of bytes is mandatory. Can't be null or empty.");
44
45                 if (valueBytes.length != FLAGS_F_LENGTH)
46                         throw new PCEPDeserializerException("Wrong length of array of bytes. Passed: " + valueBytes.length + "; Expected: >=" + FLAGS_F_LENGTH + ".");
47
48                 final BitSet flags = ByteArray.bytesToBitSet(valueBytes);
49                 return new NoPathVectorTlv(flags.get(PCE_UNAVAILABLE), flags.get(UNKNOWN_DEST), flags.get(UNKNOWN_SRC), flags.get(NO_GCO_SOLUTION),
50                                 flags.get(NO_GCO_MIGRATION_PATH), flags.get(REACHABLITY_PROBLEM));
51         }
52
53         public static byte[] put(NoPathVectorTlv obj) {
54                 if (obj == null)
55                         throw new IllegalArgumentException("NoPathVectorTlv is mandatory.");
56
57                 final BitSet flags = new BitSet(FLAGS_F_LENGTH * Byte.SIZE);
58
59                 flags.set(PCE_UNAVAILABLE, obj.isPceUnavailable());
60                 flags.set(UNKNOWN_DEST, obj.isUnknownDest());
61                 flags.set(UNKNOWN_SRC, obj.isUnknownSrc());
62                 flags.set(NO_GCO_SOLUTION, obj.isNoGCOSolution());
63                 flags.set(NO_GCO_MIGRATION_PATH, obj.isNoGCOMigrationPath());
64                 flags.set(REACHABLITY_PROBLEM, obj.isReachablityProblem());
65
66                 return ByteArray.bitSetToBytes(flags, FLAGS_F_LENGTH);
67         }
68 }