diff options
Diffstat (limited to 'pbrd')
-rw-r--r-- | pbrd/pbr_nht.c | 54 | ||||
-rw-r--r-- | pbrd/pbr_nht.h | 2 | ||||
-rw-r--r-- | pbrd/pbr_vty.c | 22 |
3 files changed, 75 insertions, 3 deletions
diff --git a/pbrd/pbr_nht.c b/pbrd/pbr_nht.c index 2f3591ac8..ae7a8017c 100644 --- a/pbrd/pbr_nht.c +++ b/pbrd/pbr_nht.c @@ -1030,8 +1030,22 @@ static void pbr_nht_show_nhg_nexthops(struct hash_bucket *b, void *data) nexthop_group_write_nexthop(vty, pnhc->nexthop); } +static void pbr_nht_json_nhg_nexthops(struct hash_bucket *b, void *data) +{ + struct pbr_nexthop_cache *pnhc = b->data; + json_object *all_hops = data; + json_object *this_hop; + + this_hop = json_object_new_object(); + json_object_boolean_add(this_hop, "valid", pnhc->valid); + nexthop_group_json_nexthop(this_hop, pnhc->nexthop); + + json_object_array_add(all_hops, this_hop); +} + struct pbr_nht_show { struct vty *vty; + json_object *json; const char *name; }; @@ -1051,6 +1065,36 @@ static void pbr_nht_show_nhg(struct hash_bucket *b, void *data) hash_iterate(pnhgc->nhh, pbr_nht_show_nhg_nexthops, vty); } +static void pbr_nht_json_nhg(struct hash_bucket *b, void *data) +{ + struct pbr_nexthop_group_cache *pnhgc = b->data; + struct pbr_nht_show *pns = data; + json_object *j, *this_group, *group_hops; + + if (pns->name && strcmp(pns->name, pnhgc->name) != 0) + return; + + j = pns->json; + this_group = json_object_new_object(); + + if (!j || !this_group) + return; + + json_object_string_add(this_group, "name", pnhgc->name); + json_object_int_add(this_group, "id", pnhgc->table_id); + json_object_boolean_add(this_group, "valid", pnhgc->valid); + json_object_boolean_add(this_group, "installed", pnhgc->installed); + + group_hops = json_object_new_array(); + + if (group_hops) { + hash_iterate(pnhgc->nhh, pbr_nht_json_nhg_nexthops, group_hops); + json_object_object_add(this_group, "nexthops", group_hops); + } + + json_object_object_add(j, pnhgc->name, this_group); +} + void pbr_nht_show_nexthop_group(struct vty *vty, const char *name) { struct pbr_nht_show pns; @@ -1061,6 +1105,16 @@ void pbr_nht_show_nexthop_group(struct vty *vty, const char *name) hash_iterate(pbr_nhg_hash, pbr_nht_show_nhg, &pns); } +void pbr_nht_json_nexthop_group(json_object *j, const char *name) +{ + struct pbr_nht_show pns; + + pns.name = name; + pns.json = j; + + hash_iterate(pbr_nhg_hash, pbr_nht_json_nhg, &pns); +} + void pbr_nht_init(void) { pbr_nhg_hash = hash_create_size( diff --git a/pbrd/pbr_nht.h b/pbrd/pbr_nht.h index 253394254..cbcf71d2f 100644 --- a/pbrd/pbr_nht.h +++ b/pbrd/pbr_nht.h @@ -24,6 +24,7 @@ #include <lib/nexthop_group.h> #include "pbr_map.h" +#include "json.h" #define PBR_NHC_NAMELEN PBR_MAP_NAMELEN + 10 @@ -112,6 +113,7 @@ extern char *pbr_nht_nexthop_make_name(char *name, size_t l, uint32_t seqno, char *buffer); extern void pbr_nht_show_nexthop_group(struct vty *vty, const char *name); +extern void pbr_nht_json_nexthop_group(json_object *j, const char *name); /* * When we get a callback from zebra about a nexthop changing diff --git a/pbrd/pbr_vty.c b/pbrd/pbr_vty.c index 61882eb75..f94a4d43b 100644 --- a/pbrd/pbr_vty.c +++ b/pbrd/pbr_vty.c @@ -733,13 +733,29 @@ DEFPY (show_pbr_map, DEFPY(show_pbr_nexthop_group, show_pbr_nexthop_group_cmd, - "show pbr nexthop-groups [WORD$word]", + "show pbr nexthop-groups [WORD$word] [json$json]", SHOW_STR PBR_STR "Nexthop Groups\n" - "Optional Name of the nexthop group\n") + "Optional Name of the nexthop group\n" + JSON_STR) { - pbr_nht_show_nexthop_group(vty, word); + json_object *j = NULL; + + if (json) + j = json_object_new_object(); + + if (j) { + pbr_nht_json_nexthop_group(j, word); + + vty_out(vty, "%s\n", + json_object_to_json_string_ext( + j, JSON_C_TO_STRING_PRETTY)); + + json_object_free(j); + } else + pbr_nht_show_nexthop_group(vty, word); + return CMD_SUCCESS; } |