2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.protocol.bgp.linkstate.impl.tlvs;
10 import io.netty.buffer.ByteBuf;
11 import java.util.Optional;
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.rev180329.OspfRouteType;
15 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev180329.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;
22 public final class OspfRouteTlvParser implements LinkstateTlvParser<OspfRouteType>,
23 LinkstateTlvParser.LinkstateTlvSerializer<OspfRouteType> {
25 public static final QName OSPF_ROUTE_TYPE_QNAME =
26 QName.create(PrefixDescriptors.QNAME.getModule(), "ospf-route-type");
27 public static final NodeIdentifier OSPF_ROUTE_NID = new NodeIdentifier(OspfRouteTlvParser.OSPF_ROUTE_TYPE_QNAME);
28 private static final int OSPF_ROUTE_TYPE = 264;
31 public void serializeTlvBody(final OspfRouteType tlv, final ByteBuf body) {
32 ByteBufWriteUtil.writeUnsignedByte((short) tlv.getIntValue(), body);
36 public int getType() {
37 return OSPF_ROUTE_TYPE;
41 public OspfRouteType parseTlvBody(final ByteBuf value) {
42 return OspfRouteType.forValue(value.readUnsignedByte());
46 public QName getTlvQName() {
47 return OSPF_ROUTE_TYPE_QNAME;
50 public static OspfRouteType serializeModel(final ContainerNode prefixDesc) {
51 final Optional<DataContainerChild<? extends YangInstanceIdentifier.PathArgument, ?>> ospfRoute
52 = prefixDesc.getChild(OSPF_ROUTE_NID);
53 return ospfRoute.map(dataContainerChild
54 -> OspfRouteType.forValue(domOspfRouteTypeValue((String) dataContainerChild.getValue())))
60 private static int domOspfRouteTypeValue(final String ospfRouteType) {
61 switch (ospfRouteType) {
63 return OspfRouteType.IntraArea.getIntValue();
65 return OspfRouteType.InterArea.getIntValue();
67 return OspfRouteType.External1.getIntValue();
69 return OspfRouteType.External2.getIntValue();
71 return OspfRouteType.Nssa1.getIntValue();
73 return OspfRouteType.Nssa2.getIntValue();