From c6462ff4b3134fb986310852d8001d8f9917a621 Mon Sep 17 00:00:00 2001 From: Mitesh Kanjariya Date: Wed, 26 Jul 2017 14:14:07 -0700 Subject: [PATCH] bgpd: Fix json output Ticket: CM-17259 Review: CCR-6512 Testing: Manual Signed-off-by: Mitesh Kanjariya --- bgpd/bgp_route.c | 39 +++++++++++++++++++++++++-------------- 1 file changed, 25 insertions(+), 14 deletions(-) diff --git a/bgpd/bgp_route.c b/bgpd/bgp_route.c index 40d6fd7bfd..3a3da6fc4e 100644 --- a/bgpd/bgp_route.c +++ b/bgpd/bgp_route.c @@ -6235,17 +6235,26 @@ static void route_vty_out_route(struct prefix *p, struct vty *vty, char buf[BUFSIZ]; if (p->family == AF_INET) { - len = vty_out(vty, "%s", - inet_ntop(p->family, &p->u.prefix, buf, BUFSIZ)); - destination = ntohl(p->u.prefix4.s_addr); - - if ((IN_CLASSC(destination) && p->prefixlen == 24) - || (IN_CLASSB(destination) && p->prefixlen == 16) - || (IN_CLASSA(destination) && p->prefixlen == 8) - || p->u.prefix4.s_addr == 0) { - /* When mask is natural, mask is not displayed. */ - } else - len += vty_out(vty, "/%d", p->prefixlen); + if (!json) { + len = vty_out(vty, "%s", + inet_ntop(p->family, &p->u.prefix, buf, + BUFSIZ)); + destination = ntohl(p->u.prefix4.s_addr); + + if ((IN_CLASSC(destination) && p->prefixlen == 24) + || (IN_CLASSB(destination) && p->prefixlen == 16) + || (IN_CLASSA(destination) && p->prefixlen == 8) + || p->u.prefix4.s_addr == 0) { + /* When mask is natural, mask is not displayed. */ + } else + len += vty_out(vty, "/%d", p->prefixlen); + } else { + json_object_string_add(json, "prefix", + inet_ntop(p->family, + &p->u.prefix, buf, + BUFSIZ)); + json_object_int_add(json, "prefixLen", p->prefixlen); + } } else if (p->family == AF_ETHERNET) { prefix2str(p, buf, PREFIX_STRLEN); len = vty_out(vty, "%s", buf); @@ -6262,9 +6271,11 @@ static void route_vty_out_route(struct prefix *p, struct vty *vty, len = vty_out(vty, "%s", buf); #endif } else { - len = vty_out(vty, "%s/%d", - inet_ntop(p->family, &p->u.prefix, buf, BUFSIZ), - p->prefixlen); + if (!json) + len = vty_out(vty, "%s/%d", + inet_ntop(p->family, &p->u.prefix, buf, + BUFSIZ), + p->prefixlen); } if (!json) { -- 2.39.5