Skip to content

Commit 994b0d4

Browse files
author
Mark Stapp
committed
zebra: example use of json incremental output
Demonstrate incremental json output with the zebra "show nexthop-group rib" command. Signed-off-by: Mark Stapp <mjs@cisco.com>
1 parent d70ac41 commit 994b0d4

File tree

1 file changed

+43
-14
lines changed

1 file changed

+43
-14
lines changed

zebra/zebra_vty.c

Lines changed: 43 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1459,7 +1459,9 @@ struct nhe_show_context {
14591459
vrf_id_t vrf_id;
14601460
afi_t afi;
14611461
int type;
1462+
int counter;
14621463
json_object *json;
1464+
json_object *json_top;
14631465
};
14641466

14651467
static int nhe_show_walker(struct hash_bucket *bucket, void *arg)
@@ -1480,6 +1482,13 @@ static int nhe_show_walker(struct hash_bucket *bucket, void *arg)
14801482

14811483
show_nexthop_group_out(ctx->vty, nhe, ctx->json);
14821484

1485+
ctx->counter++;
1486+
if (ctx->counter > 5) {
1487+
/* Output and reset counter */
1488+
frr_json_vty_out(ctx->vty, ctx->json_top);
1489+
ctx->counter = 0;
1490+
}
1491+
14831492
done:
14841493
return HASHWALK_CONTINUE;
14851494
}
@@ -1489,14 +1498,31 @@ static void show_nexthop_group_cmd_helper(struct vty *vty,
14891498
int type, json_object *json)
14901499
{
14911500
struct nhe_show_context ctx;
1501+
struct json_object *jvrf = NULL;
1502+
1503+
if (json) {
1504+
jvrf = json_object_new_object();
1505+
1506+
frr_json_set_open(jvrf);
1507+
1508+
json_object_object_add(json, zvrf->vrf->name, jvrf);
1509+
}
14921510

14931511
ctx.vty = vty;
14941512
ctx.afi = afi;
14951513
ctx.vrf_id = zvrf->vrf->vrf_id;
14961514
ctx.type = type;
1497-
ctx.json = json;
1515+
ctx.json = jvrf;
1516+
ctx.json_top = json;
1517+
ctx.counter = 0;
14981518

14991519
hash_walk(zrouter.nhgs_id, nhe_show_walker, &ctx);
1520+
1521+
/* Finish with the json vrf object */
1522+
if (json) {
1523+
frr_json_set_complete(jvrf);
1524+
frr_json_vty_out(vty, json);
1525+
}
15001526
}
15011527

15021528
static void if_nexthop_group_dump_vty(struct vty *vty, struct interface *ifp)
@@ -1575,7 +1601,6 @@ DEFPY(show_nexthop_group,
15751601
uint8_t type = 0;
15761602
bool uj = use_json(argc, argv);
15771603
json_object *json = NULL;
1578-
json_object *json_vrf = NULL;
15791604

15801605
if (uj)
15811606
json = json_object_new_object();
@@ -1608,24 +1633,24 @@ DEFPY(show_nexthop_group,
16081633
if (vrf_all) {
16091634
struct vrf *vrf;
16101635

1636+
if (json)
1637+
frr_json_set_open(json);
1638+
16111639
RB_FOREACH (vrf, vrf_name_head, &vrfs_by_name) {
16121640
zvrf = vrf->info;
16131641
if (!zvrf)
16141642
continue;
1615-
if (uj)
1616-
json_vrf = json_object_new_object();
1617-
else
1643+
if (!uj)
16181644
vty_out(vty, "VRF: %s\n", vrf->name);
16191645

16201646
show_nexthop_group_cmd_helper(vty, zvrf, afi, type,
1621-
json_vrf);
1622-
if (uj)
1623-
json_object_object_add(json, vrf->name,
1624-
json_vrf);
1647+
json);
16251648
}
16261649

1627-
if (uj)
1628-
vty_json(vty, json);
1650+
if (uj) {
1651+
frr_json_set_complete(json);
1652+
frr_json_vty_out(vty, json);
1653+
}
16291654

16301655
return CMD_SUCCESS;
16311656
}
@@ -1644,11 +1669,15 @@ DEFPY(show_nexthop_group,
16441669
return CMD_WARNING;
16451670
}
16461671

1647-
show_nexthop_group_cmd_helper(vty, zvrf, afi, type, json);
1672+
if (json)
1673+
frr_json_set_open(json);
16481674

1649-
if (uj)
1650-
vty_json(vty, json);
1675+
show_nexthop_group_cmd_helper(vty, zvrf, afi, type, json);
16511676

1677+
if (uj) {
1678+
frr_json_set_complete(json);
1679+
frr_json_vty_out(vty, json);
1680+
}
16521681
return CMD_SUCCESS;
16531682
}
16541683

0 commit comments

Comments
 (0)