Enforce checkstyle in bgp-linkstate
[bgpcep.git] / bgp / extensions / linkstate / src / main / java / org / opendaylight / protocol / bgp / linkstate / impl / tlvs / OspfRouteTlvParser.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.bgp.linkstate.impl.tlvs;
9
10 import io.netty.buffer.ByteBuf;
11 import org.opendaylight.protocol.bgp.linkstate.spi.LinkstateTlvParser;
12 import org.opendaylight.protocol.util.ByteBufWriteUtil;
13 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev180329.OspfRouteType;
14 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev180329.linkstate.object.type.prefix._case.PrefixDescriptors;
15 import org.opendaylight.yangtools.yang.common.QName;
16 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
17 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
18
19 public final class OspfRouteTlvParser implements LinkstateTlvParser<OspfRouteType>,
20         LinkstateTlvParser.LinkstateTlvSerializer<OspfRouteType> {
21
22     public static final QName OSPF_ROUTE_TYPE_QNAME =
23             QName.create(PrefixDescriptors.QNAME.getModule(), "ospf-route-type");
24     public static final NodeIdentifier OSPF_ROUTE_NID = new NodeIdentifier(OspfRouteTlvParser.OSPF_ROUTE_TYPE_QNAME);
25     private static final int OSPF_ROUTE_TYPE = 264;
26
27     @Override
28     public void serializeTlvBody(final OspfRouteType tlv, final ByteBuf body) {
29         ByteBufWriteUtil.writeUnsignedByte((short) tlv.getIntValue(), body);
30     }
31
32     @Override
33     public int getType() {
34         return OSPF_ROUTE_TYPE;
35     }
36
37     @Override
38     public OspfRouteType parseTlvBody(final ByteBuf value) {
39         return OspfRouteType.forValue(value.readUnsignedByte());
40     }
41
42     @Override
43     public QName getTlvQName() {
44         return OSPF_ROUTE_TYPE_QNAME;
45     }
46
47     public static OspfRouteType serializeModel(final ContainerNode prefixDesc) {
48         return prefixDesc.getChild(OSPF_ROUTE_NID).map(
49             dataContainerChild -> OspfRouteType.forValue(domOspfRouteTypeValue((String) dataContainerChild.getValue())))
50             .orElse(null);
51     }
52
53     // FIXME : use codec
54     private static int domOspfRouteTypeValue(final String ospfRouteType) {
55         switch (ospfRouteType) {
56             case "intra-area":
57                 return OspfRouteType.IntraArea.getIntValue();
58             case "inter-area":
59                 return OspfRouteType.InterArea.getIntValue();
60             case "external1":
61                 return OspfRouteType.External1.getIntValue();
62             case "external2":
63                 return OspfRouteType.External2.getIntValue();
64             case "nssa1":
65                 return OspfRouteType.Nssa1.getIntValue();
66             case "nssa2":
67                 return OspfRouteType.Nssa2.getIntValue();
68             default:
69                 return 0;
70         }
71     }
72 }