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.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.types.rev131005.NoPathVectorTlv;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.NoPathVectorTlv.Flags;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Tlv;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcrep.message.pcrep.message.replies.result.failure.no.path.tlvs.NoPathVectorBuilder;
20
21 /**
22  * Parser for {@link NoPathVectorTlv}
23  */
24 public class NoPathVectorTlvParser implements TlvParser, TlvSerializer {
25
26         public static final int TYPE = 1;
27
28         public static final int FLAGS_F_LENGTH = 4;
29
30         /*
31          * flags offsets inside flags field in bits
32          */
33         public static final int PCE_UNAVAILABLE = 31;
34         public static final int UNKNOWN_DEST = 30;
35         public static final int UNKNOWN_SRC = 29;
36
37         /*
38          * flags offsets of flags added by GCO extension
39          */
40         public static final int NO_GCO_SOLUTION = 25;
41         public static final int NO_GCO_MIGRATION_PATH = 26;
42
43         /*
44          * flags offsets of flags added by RFC 6006
45          */
46         public static final int REACHABLITY_PROBLEM = 24;
47
48         @Override
49         public NoPathVectorTlv parseTlv(final byte[] valueBytes) throws PCEPDeserializerException {
50                 if (valueBytes == null || valueBytes.length == 0)
51                         throw new IllegalArgumentException("Array of bytes is mandatory. Can't be null or empty.");
52
53                 if (valueBytes.length != FLAGS_F_LENGTH)
54                         throw new PCEPDeserializerException("Wrong length of array of bytes. Passed: " + valueBytes.length + "; Expected: >="
55                                         + FLAGS_F_LENGTH + ".");
56
57                 final BitSet flags = ByteArray.bytesToBitSet(valueBytes);
58
59                 return new NoPathVectorBuilder().setFlags(
60                                 new Flags(false, flags.get(NO_GCO_MIGRATION_PATH), flags.get(NO_GCO_SOLUTION), flags.get(REACHABLITY_PROBLEM), false, flags.get(PCE_UNAVAILABLE), flags.get(UNKNOWN_DEST), flags.get(UNKNOWN_SRC))).build();
61         }
62
63         @Override
64         public byte[] serializeTlv(final Tlv tlvs) {
65                 if (tlvs == null)
66                         throw new IllegalArgumentException("NoPathVectorTlv is mandatory.");
67                 final NoPathVectorTlv tlv = (NoPathVectorTlv) tlvs;
68
69                 final BitSet flags = new BitSet(FLAGS_F_LENGTH * Byte.SIZE);
70
71                 flags.set(PCE_UNAVAILABLE, tlv.getFlags().isPceUnavailable());
72                 flags.set(UNKNOWN_DEST, tlv.getFlags().isUnknownDestination());
73                 flags.set(UNKNOWN_SRC, tlv.getFlags().isUnknownSource());
74                 flags.set(NO_GCO_SOLUTION, tlv.getFlags().isNoGcoSolution());
75                 flags.set(NO_GCO_MIGRATION_PATH, tlv.getFlags().isNoGcoMigration());
76                 flags.set(REACHABLITY_PROBLEM, tlv.getFlags().isP2mpUnreachable());
77
78                 return ByteArray.bitSetToBytes(flags, FLAGS_F_LENGTH);
79         }
80
81         @Override
82         public int getType() {
83                 return TYPE;
84         }
85 }