Bump versions to 0.21.6-SNAPSHOT
[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.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev200120.OspfRouteType;
13 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev200120.linkstate.object.type.prefix._case.PrefixDescriptors;
14 import org.opendaylight.yangtools.yang.common.QName;
15 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
16 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
17
18 public final class OspfRouteTlvParser implements LinkstateTlvParser<OspfRouteType>,
19         LinkstateTlvParser.LinkstateTlvSerializer<OspfRouteType> {
20     public static final QName OSPF_ROUTE_TYPE_QNAME = QName.create(PrefixDescriptors.QNAME, "ospf-route-type").intern();
21     public static final NodeIdentifier OSPF_ROUTE_NID = NodeIdentifier.create(OspfRouteTlvParser.OSPF_ROUTE_TYPE_QNAME);
22
23     private static final int OSPF_ROUTE_TYPE = 264;
24
25     @Override
26     public void serializeTlvBody(final OspfRouteType tlv, final ByteBuf body) {
27         body.writeByte(tlv.getIntValue());
28     }
29
30     @Override
31     public int getType() {
32         return OSPF_ROUTE_TYPE;
33     }
34
35     @Override
36     public OspfRouteType parseTlvBody(final ByteBuf value) {
37         return OspfRouteType.forValue(value.readUnsignedByte());
38     }
39
40     @Override
41     public QName getTlvQName() {
42         return OSPF_ROUTE_TYPE_QNAME;
43     }
44
45     public static OspfRouteType serializeModel(final ContainerNode prefixDesc) {
46         final var ospfRoute = prefixDesc.childByArg(OSPF_ROUTE_NID);
47         return ospfRoute == null ? null : OspfRouteType.forValue(domOspfRouteTypeValue((String) ospfRoute.body()));
48     }
49
50     // FIXME : use codec
51     private static int domOspfRouteTypeValue(final String ospfRouteType) {
52         return switch (ospfRouteType) {
53             case "intra-area" -> OspfRouteType.IntraArea.getIntValue();
54             case "inter-area" -> OspfRouteType.InterArea.getIntValue();
55             case "external1" -> OspfRouteType.External1.getIntValue();
56             case "external2" -> OspfRouteType.External2.getIntValue();
57             case "nssa1" -> OspfRouteType.Nssa1.getIntValue();
58             case "nssa2" -> OspfRouteType.Nssa2.getIntValue();
59             default -> 0;
60         };
61     }
62 }