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