diff --git a/zebra/zebra_routemap.c b/zebra/zebra_routemap.c index 066ef8a8d0..4f43cea493 100644 --- a/zebra/zebra_routemap.c +++ b/zebra/zebra_routemap.c @@ -143,26 +143,48 @@ static void show_vrf_proto_rm(struct vty *vty, struct zebra_vrf *zvrf, } static void show_vrf_nht_rm(struct vty *vty, struct zebra_vrf *zvrf, - int af_type) + int af_type, json_object *json) { int i; - vty_out(vty, "Protocol : route-map\n"); - vty_out(vty, "-------------------------------------\n"); - - for (i = 0; i < ZEBRA_ROUTE_MAX; i++) { - if (NHT_RM_NAME(zvrf, af_type, i)) - vty_out(vty, "%-24s : %-10s\n", zebra_route_string(i), - NHT_RM_NAME(zvrf, af_type, i)); - else - vty_out(vty, "%-24s : none\n", zebra_route_string(i)); + if (!json) { + vty_out(vty, "Protocol : route-map\n"); + vty_out(vty, "-------------------------------------\n"); } - if (NHT_RM_NAME(zvrf, af_type, i)) - vty_out(vty, "%-24s : %-10s\n", "any", - NHT_RM_NAME(zvrf, af_type, i)); - else - vty_out(vty, "%-24s : none\n", "any"); + for (i = 0; i < ZEBRA_ROUTE_MAX; i++) { + if (json) { + if (NHT_RM_NAME(zvrf, af_type, i)) + json_object_string_add( + json, zebra_route_string(i), + NHT_RM_NAME(zvrf, af_type, i)); + else + json_object_string_add( + json, zebra_route_string(i), "none"); + } else { + if (NHT_RM_NAME(zvrf, af_type, i)) + vty_out(vty, "%-24s : %-10s\n", + zebra_route_string(i), + NHT_RM_NAME(zvrf, af_type, i)); + else + vty_out(vty, "%-24s : none\n", + zebra_route_string(i)); + } + } + + if (json) { + if (NHT_RM_NAME(zvrf, af_type, i)) + json_object_string_add(json, "any", + NHT_RM_NAME(zvrf, af_type, i)); + else + json_object_string_add(json, "any", "none"); + } else { + if (NHT_RM_NAME(zvrf, af_type, i)) + vty_out(vty, "%-24s : %-10s\n", "any", + NHT_RM_NAME(zvrf, af_type, i)); + else + vty_out(vty, "%-24s : none\n", "any"); + } } static int show_proto_rm(struct vty *vty, int af_type, const char *vrf_all, @@ -198,35 +220,78 @@ static int show_proto_rm(struct vty *vty, int af_type, const char *vrf_all, } static int show_nht_rm(struct vty *vty, int af_type, const char *vrf_all, - const char *vrf_name) + const char *vrf_name, bool use_json) { struct zebra_vrf *zvrf; + json_object *json = NULL; + json_object *json_vrfs = NULL; + + if (use_json) { + json = json_object_new_object(); + json_vrfs = json_object_new_object(); + json_object_string_add(json, "afi", + (af_type == AFI_IP) ? "ipv4" : "ipv6"); + } if (vrf_all) { struct vrf *vrf; + if (use_json) + json_object_object_add(json, "vrfs", json_vrfs); + RB_FOREACH (vrf, vrf_name_head, &vrfs_by_name) { zvrf = (struct zebra_vrf *)vrf->info; if (zvrf == NULL) continue; - vty_out(vty, "VRF: %s\n", zvrf->vrf->name); - show_vrf_nht_rm(vty, zvrf, af_type); + if (use_json) { + json_object *json_proto = NULL; + json_object *json_vrf = NULL; + json_vrf = json_object_new_object(); + json_object_object_add( + json_vrfs, zvrf->vrf->name, json_vrf); + json_proto = json_object_new_object(); + json_object_object_add(json_vrf, "protocols", + json_proto); + show_vrf_nht_rm(vty, zvrf, af_type, json_proto); + } else { + vty_out(vty, "VRF: %s\n", zvrf->vrf->name); + show_vrf_nht_rm(vty, zvrf, af_type, NULL); + } } } else { + json_object *json_proto = NULL; + json_object *json_vrf = NULL; vrf_id_t vrf_id = VRF_DEFAULT; if (vrf_name) VRF_GET_ID(vrf_id, vrf_name, false); zvrf = zebra_vrf_lookup_by_id(vrf_id); - if (!zvrf) + if (!zvrf) { + json_object_free(json); + json_object_free(json_vrfs); return CMD_SUCCESS; + } - vty_out(vty, "VRF: %s\n", zvrf->vrf->name); - show_vrf_nht_rm(vty, zvrf, af_type); + if (use_json) { + json_object_object_add(json, "vrfs", json_vrfs); + json_vrf = json_object_new_object(); + json_object_object_add(json_vrfs, zvrf->vrf->name, + json_vrf); + json_proto = json_object_new_object(); + json_object_object_add(json_vrf, "protocols", + json_proto); + show_vrf_nht_rm(vty, zvrf, af_type, json_proto); + } else { + vty_out(vty, "VRF: %s\n", zvrf->vrf->name); + show_vrf_nht_rm(vty, zvrf, af_type, NULL); + } } + if (use_json) + vty_json(vty, json); + return CMD_SUCCESS; } @@ -854,14 +919,19 @@ DEFPY_YANG (no_ip_protocol_nht_rmap, DEFPY_YANG (show_ip_protocol_nht, show_ip_protocol_nht_cmd, - "show ip nht route-map [vrf ]", + "show ip nht route-map [vrf ] [json]", SHOW_STR IP_STR - "IP nexthop tracking table\n" - "IP Next Hop tracking filtering status\n" - VRF_FULL_CMD_HELP_STR) + "IPv4 nexthop tracking table\n" + "IPv4 Next Hop tracking filtering status\n" + VRF_CMD_HELP_STR + "All VRFs\n" + JSON_STR) { - int ret = show_nht_rm(vty, AFI_IP, vrf_all, vrf_name); + int ret; + bool uj = use_json(argc, argv); + + ret = show_nht_rm(vty, AFI_IP, vrf_all, vrf_name, uj); return ret; } @@ -936,14 +1006,19 @@ DEFPY_YANG (no_ipv6_protocol_nht_rmap, DEFPY_YANG (show_ipv6_protocol_nht, show_ipv6_protocol_nht_cmd, - "show ipv6 nht route-map [vrf ]", + "show ipv6 nht route-map [vrf ] [json]", SHOW_STR IP6_STR - "Next Hop filtering status\n" - "Route-map\n" - VRF_FULL_CMD_HELP_STR) + "IPv6 nexthop tracking table\n" + "IPv6 Next Hop tracking filtering status\n" + VRF_CMD_HELP_STR + "All VRFs\n" + JSON_STR) { - int ret = show_nht_rm(vty, AFI_IP6, vrf_all, vrf_name); + int ret; + bool uj = use_json(argc, argv); + + ret = show_nht_rm(vty, AFI_IP6, vrf_all, vrf_name, uj); return ret; }