2 * Copyright (c) 2016 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
9 package org.opendaylight.protocol.bgp.linkstate.impl.tlvs;
11 import com.google.common.annotations.VisibleForTesting;
12 import io.netty.buffer.ByteBuf;
13 import io.netty.buffer.Unpooled;
14 import org.opendaylight.protocol.bgp.linkstate.spi.LinkstateTlvParser;
15 import org.opendaylight.protocol.util.ByteBufWriteUtil;
16 import org.opendaylight.protocol.util.Ipv4Util;
17 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpPrefix;
18 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Prefix;
19 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv6Prefix;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev180329.linkstate.object.type.prefix._case.PrefixDescriptors;
21 import org.opendaylight.yangtools.yang.common.QName;
22 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
23 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
27 public final class ReachTlvParser implements LinkstateTlvParser.LinkstateTlvSerializer<IpPrefix>, LinkstateTlvParser<IpPrefix> {
28 public static final QName IP_REACHABILITY_QNAME = QName.create(PrefixDescriptors.QNAME, "ip-reachability-information").intern();
30 public static final YangInstanceIdentifier.NodeIdentifier IP_REACH_NID = new YangInstanceIdentifier.NodeIdentifier(IP_REACHABILITY_QNAME);
31 private static final Logger LOG = LoggerFactory.getLogger(ReachTlvParser.class);
32 private static final int IP_REACHABILITY = 265;
35 public IpPrefix parseTlvBody(final ByteBuf value) {
36 return new IpPrefix(Ipv4Util.prefixForByteBuf(value));
40 public QName getTlvQName() {
41 return IP_REACHABILITY_QNAME;
45 public void serializeTlvBody(final IpPrefix tlv, final ByteBuf body) {
46 if (tlv.getIpv4Prefix() != null) {
47 ByteBufWriteUtil.writeMinimalPrefix(tlv.getIpv4Prefix(), body);
48 } else if (tlv.getIpv6Prefix() != null) {
49 ByteBufWriteUtil.writeMinimalPrefix(tlv.getIpv6Prefix(), body);
54 public int getType() {
55 return IP_REACHABILITY;
58 public static IpPrefix serializeModel(final ContainerNode prefixDesc) {
59 if (prefixDesc.getChild(IP_REACH_NID).isPresent()) {
60 final String prefix = (String) prefixDesc.getChild(IP_REACH_NID).get().getValue();
62 final ByteBuf buffer = Unpooled.buffer(5);
63 ByteBufWriteUtil.writeMinimalPrefix(new Ipv4Prefix(prefix), buffer);
64 return new IpPrefix(new Ipv4Prefix(prefix));
65 } catch (final IllegalArgumentException e) {
66 LOG.debug("Creating Ipv6 prefix because", e);
67 return new IpPrefix(new Ipv6Prefix(prefix));