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.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;
22 public final class OspfRouteTlvParser implements LinkstateTlvParser<OspfRouteType>, LinkstateTlvParser.LinkstateTlvSerializer<OspfRouteType> {
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;
29 public void serializeTlvBody(final OspfRouteType tlv, final ByteBuf body) {
30 ByteBufWriteUtil.writeUnsignedByte((short) tlv.getIntValue(), body);
34 public int getType() {
35 return OSPF_ROUTE_TYPE;
39 public OspfRouteType parseTlvBody(final ByteBuf value) {
40 return OspfRouteType.forValue(value.readUnsignedByte());
44 public QName getTlvQName() {
45 return OSPF_ROUTE_TYPE_QNAME;
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()));
58 private static int domOspfRouteTypeValue(final String ospfRouteType) {
59 switch (ospfRouteType) {
61 return OspfRouteType.IntraArea.getIntValue();
63 return OspfRouteType.InterArea.getIntValue();
65 return OspfRouteType.External1.getIntValue();
67 return OspfRouteType.External2.getIntValue();
69 return OspfRouteType.Nssa1.getIntValue();
71 return OspfRouteType.Nssa2.getIntValue();