Removed checkstyle warnings.
[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 io.netty.buffer.ByteBuf;
11
12 import java.util.BitSet;
13
14 import org.opendaylight.protocol.pcep.spi.PCEPDeserializerException;
15 import org.opendaylight.protocol.pcep.spi.TlvParser;
16 import org.opendaylight.protocol.pcep.spi.TlvSerializer;
17 import org.opendaylight.protocol.util.ByteArray;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.NoPathVectorTlv.Flags;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Tlv;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcrep.message.pcrep.message.replies.result.failure._case.no.path.tlvs.NoPathVector;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcrep.message.pcrep.message.replies.result.failure._case.no.path.tlvs.NoPathVectorBuilder;
22
23 /**
24  * Parser for {@link NoPathVector}
25  */
26 public class NoPathVectorTlvParser implements TlvParser, TlvSerializer {
27
28     public static final int TYPE = 1;
29
30     private static final int FLAGS_F_LENGTH = 4;
31
32     private static final int REACHABLITY_PROBLEM = 24;
33     private static final int NO_GCO_SOLUTION = 25;
34     private static final int NO_GCO_MIGRATION_PATH = 26;
35     private static final int PATH_KEY = 27;
36     private static final int CHAIN_UNAVAILABLE = 28;
37     private static final int UNKNOWN_SRC = 29;
38     private static final int UNKNOWN_DEST = 30;
39     private static final int PCE_UNAVAILABLE = 31;
40
41     @Override
42     public NoPathVector parseTlv(final ByteBuf buffer) throws PCEPDeserializerException {
43         if (buffer == null) {
44             return null;
45         }
46         if (buffer.readableBytes() != FLAGS_F_LENGTH) {
47             throw new PCEPDeserializerException("Wrong length of array of bytes. Passed: " + buffer.readableBytes() + "; Expected: >="
48                     + FLAGS_F_LENGTH + ".");
49         }
50         final BitSet flags = ByteArray.bytesToBitSet(ByteArray.readAllBytes(buffer));
51         return new NoPathVectorBuilder().setFlags(
52                 new Flags(flags.get(CHAIN_UNAVAILABLE), flags.get(NO_GCO_MIGRATION_PATH), flags.get(NO_GCO_SOLUTION), flags.get(REACHABLITY_PROBLEM), flags.get(PATH_KEY), flags.get(PCE_UNAVAILABLE), flags.get(UNKNOWN_DEST), flags.get(UNKNOWN_SRC))).build();
53     }
54
55     @Override
56     public byte[] serializeTlv(final Tlv tlvs) {
57         if (tlvs == null) {
58             throw new IllegalArgumentException("NoPathVectorTlv is mandatory.");
59         }
60         final NoPathVector tlv = (NoPathVector) tlvs;
61
62         final BitSet flags = new BitSet(FLAGS_F_LENGTH * Byte.SIZE);
63         flags.set(REACHABLITY_PROBLEM, tlv.getFlags().isP2mpUnreachable());
64         flags.set(NO_GCO_SOLUTION, tlv.getFlags().isNoGcoSolution());
65         flags.set(NO_GCO_MIGRATION_PATH, tlv.getFlags().isNoGcoMigration());
66         flags.set(PATH_KEY, tlv.getFlags().isPathKey());
67         flags.set(CHAIN_UNAVAILABLE, tlv.getFlags().isChainUnavailable());
68         flags.set(UNKNOWN_SRC, tlv.getFlags().isUnknownSource());
69         flags.set(UNKNOWN_DEST, tlv.getFlags().isUnknownDestination());
70         flags.set(PCE_UNAVAILABLE, tlv.getFlags().isPceUnavailable());
71         return TlvUtil.formatTlv(TYPE, ByteArray.bitSetToBytes(flags, FLAGS_F_LENGTH));
72     }
73 }