safi_t safi, enum bgp_show_type type);
static int
-bgp_show_table (struct vty *vty, struct bgp_table *table, struct in_addr *router_id,
- enum bgp_show_type type, void *output_arg, u_char use_json)
+bgp_show_table (struct vty *vty, struct bgp_table *table,
+ struct in_addr *router_id, enum bgp_show_type type,
+ void *output_arg, u_char use_json, json_object *json)
{
struct bgp_info *ri;
struct bgp_node *rn;
struct prefix *p;
char buf[BUFSIZ];
char buf2[BUFSIZ];
- json_object *json = NULL;
json_object *json_paths = NULL;
json_object *json_routes = NULL;
if (use_json)
{
- json = json_object_new_object();
+ if (json == NULL)
+ json = json_object_new_object();
+
json_object_int_add(json, "tableVersion", table->version);
json_object_string_add(json, "routerId", inet_ntoa (*router_id));
json_routes = json_object_new_object();
json_object_object_add(json_routes, buf2, json_paths);
}
}
- }
+ }
if (use_json)
{
table = bgp->rib[afi][safi];
- return bgp_show_table (vty, table, &bgp->router_id, type, output_arg, use_json);
+ return bgp_show_table (vty, table, &bgp->router_id, type, output_arg,
+ use_json, NULL);
}
static void
struct listnode *node, *nnode;
struct bgp *bgp;
struct bgp_table *table;
+ json_object *json = NULL;
+ int is_first = 1;
+
+ if (use_json)
+ vty_out (vty, "{%s", VTY_NEWLINE);
for (ALL_LIST_ELEMENTS (bm->bgp, node, nnode, bgp))
{
- vty_out (vty, "%sInstance %s:%s",
- VTY_NEWLINE,
- (bgp->inst_type == BGP_INSTANCE_TYPE_DEFAULT) ? "Default" : bgp->name,
- VTY_NEWLINE);
+ if (use_json)
+ {
+ if (!(json = json_object_new_object()))
+ {
+ zlog_err("Unable to allocate memory for JSON object");
+ vty_out (vty,
+ "{\"error\": {\"message:\": \"Unable to allocate memory for JSON object\"}}}%s",
+ VTY_NEWLINE);
+ return;
+ }
+ json_object_int_add(json, "vrfId",
+ (bgp->vrf_id == VRF_UNKNOWN)
+ ? -1 : bgp->vrf_id);
+ json_object_string_add(json, "vrfName",
+ (bgp->inst_type == BGP_INSTANCE_TYPE_DEFAULT)
+ ? "Default" : bgp->name);
+ if (! is_first)
+ vty_out (vty, ",%s", VTY_NEWLINE);
+ else
+ is_first = 0;
+
+ vty_out(vty, "\"%s\":", (bgp->inst_type == BGP_INSTANCE_TYPE_DEFAULT)
+ ? "Default" : bgp->name);
+ }
+ else
+ {
+ vty_out (vty, "%sInstance %s:%s",
+ VTY_NEWLINE,
+ (bgp->inst_type == BGP_INSTANCE_TYPE_DEFAULT)
+ ? "Default" : bgp->name,
+ VTY_NEWLINE);
+ }
table = bgp->rib[afi][safi];
bgp_show_table (vty, table, &bgp->router_id,
- bgp_show_type_normal, NULL, use_json);
+ bgp_show_type_normal, NULL, use_json, json);
+
}
+
+ if (use_json)
+ vty_out (vty, "}%s", VTY_NEWLINE);
}
/* Header of detailed BGP route information */
/* Show BGP peer's summary information. */
static int
bgp_show_summary (struct vty *vty, struct bgp *bgp, int afi, int safi,
- u_char use_json)
+ u_char use_json, json_object *json)
{
struct peer *peer;
struct listnode *node, *nnode;
unsigned int count = 0, dn_count = 0;
char timebuf[BGP_UPTIME_LEN], dn_flag[2];
int len;
- json_object *json = NULL;
json_object *json_peer = NULL;
json_object *json_peers = NULL;
if (use_json)
{
- json = json_object_new_object();
+ if (json == NULL)
+ json = json_object_new_object();
+
json_peers = json_object_new_object();
}
{
json_object_string_add(json, "routerId", inet_ntoa (bgp->router_id));
json_object_int_add(json, "as", bgp->as);
- json_object_int_add(json, "vrf-id", vrf_id_ui);
-
+ json_object_int_add(json, "vrfId", vrf_id_ui);
+ json_object_string_add(json, "vrfName",
+ (bgp->inst_type == BGP_INSTANCE_TYPE_DEFAULT)
+ ? "Default" : bgp->name);
}
else
{
vty_out (vty, "%sTotal number of neighbors %d%s", VTY_NEWLINE,
count, VTY_NEWLINE);
else
- vty_out (vty, "No %s neighbor is configured%s",
- afi == AFI_IP ? "IPv4" : "IPv6", VTY_NEWLINE);
+ {
+ if (use_json)
+ vty_out(vty, "{\"error\": {\"message\": \"No %s neighbor configured\"}}%s",
+ afi == AFI_IP ? "IPv4" : "IPv6", VTY_NEWLINE);
+ else
+ vty_out (vty, "No %s neighbor is configured%s",
+ afi == AFI_IP ? "IPv4" : "IPv6", VTY_NEWLINE);
+ }
- if (dn_count)
+ if (dn_count && ! use_json)
{
vty_out(vty, "* - dynamic neighbor%s", VTY_NEWLINE);
vty_out(vty,
return CMD_WARNING;
}
- bgp_show_summary (vty, bgp, afi, safi, use_json);
+ bgp_show_summary (vty, bgp, afi, safi, use_json, NULL);
return CMD_SUCCESS;
}
bgp = bgp_get_default ();
if (bgp)
- bgp_show_summary (vty, bgp, afi, safi, use_json);
+ bgp_show_summary (vty, bgp, afi, safi, use_json, NULL);
return CMD_SUCCESS;
}
{
struct listnode *node, *nnode;
struct bgp *bgp;
+ json_object *json = NULL;
+ int is_first = 1;
+
+ if (use_json)
+ vty_out (vty, "{%s", VTY_NEWLINE);
for (ALL_LIST_ELEMENTS (bm->bgp, node, nnode, bgp))
{
- vty_out (vty, "%sInstance %s:%s",
- VTY_NEWLINE,
- (bgp->inst_type == BGP_INSTANCE_TYPE_DEFAULT) ? "Default" : bgp->name,
- VTY_NEWLINE);
- bgp_show_summary (vty, bgp, afi, safi, use_json);
+ if (use_json)
+ {
+ if (!(json = json_object_new_object()))
+ {
+ zlog_err("Unable to allocate memory for JSON object");
+ vty_out (vty,
+ "{\"error\": {\"message:\": \"Unable to allocate memory for JSON object\"}}}%s",
+ VTY_NEWLINE);
+ return;
+ }
+
+ if (! is_first)
+ vty_out (vty, ",%s", VTY_NEWLINE);
+ else
+ is_first = 0;
+
+ vty_out(vty, "\"%s\":", (bgp->inst_type == BGP_INSTANCE_TYPE_DEFAULT)
+ ? "Default" : bgp->name);
+ }
+ else
+ {
+ vty_out (vty, "%sInstance %s:%s",
+ VTY_NEWLINE,
+ (bgp->inst_type == BGP_INSTANCE_TYPE_DEFAULT)
+ ? "Default" : bgp->name, VTY_NEWLINE);
+ }
+ bgp_show_summary (vty, bgp, afi, safi, use_json, json);
}
+
+ if (use_json)
+ vty_out (vty, "}%s", VTY_NEWLINE);
+
}
/* `show ip bgp summary' commands. */
static int
bgp_show_neighbor_vty (struct vty *vty, const char *name,
- enum show_type type, const char *ip_str, u_char use_json)
+ enum show_type type, const char *ip_str, u_char use_json,
+ json_object *json)
{
int ret;
struct bgp *bgp;
union sockunion su;
- json_object *json = NULL;
- if (use_json)
+ if (use_json && (json == NULL))
json = json_object_new_object();
if (name)
struct listnode *node, *nnode;
struct bgp *bgp;
json_object *json = NULL;
+ int is_first = 1;
+
+ if (use_json)
+ vty_out (vty, "{%s", VTY_NEWLINE);
for (ALL_LIST_ELEMENTS (bm->bgp, node, nnode, bgp))
{
- vty_out (vty, "%sInstance %s:%s",
- VTY_NEWLINE,
- (bgp->inst_type == BGP_INSTANCE_TYPE_DEFAULT) ? "Default" : bgp->name,
- VTY_NEWLINE);
if (use_json)
- json = json_object_new_object();
+ {
+ if (!(json = json_object_new_object()))
+ {
+ zlog_err("Unable to allocate memory for JSON object");
+ vty_out (vty,
+ "{\"error\": {\"message:\": \"Unable to allocate memory for JSON object\"}}}%s",
+ VTY_NEWLINE);
+ return;
+ }
+
+ json_object_int_add(json, "vrfId",
+ (bgp->vrf_id == VRF_UNKNOWN)
+ ? -1 : bgp->vrf_id);
+ json_object_string_add(json, "vrfName",
+ (bgp->inst_type == BGP_INSTANCE_TYPE_DEFAULT)
+ ? "Default" : bgp->name);
+
+ if (! is_first)
+ vty_out (vty, ",%s", VTY_NEWLINE);
+ else
+ is_first = 0;
+
+ vty_out(vty, "\"%s\":", (bgp->inst_type == BGP_INSTANCE_TYPE_DEFAULT)
+ ? "Default" : bgp->name);
+ }
+ else
+ {
+ vty_out (vty, "%sInstance %s:%s",
+ VTY_NEWLINE,
+ (bgp->inst_type == BGP_INSTANCE_TYPE_DEFAULT)
+ ? "Default" : bgp->name,
+ VTY_NEWLINE);
+ }
bgp_show_neighbor (vty, bgp, show_all, NULL, NULL, use_json, json);
}
+
+ if (use_json)
+ vty_out (vty, "}%s", VTY_NEWLINE);
}
/* "show ip bgp neighbors" commands. */
{
u_char uj = use_json(argc, argv);
- return bgp_show_neighbor_vty (vty, NULL, show_all, NULL, uj);
+ return bgp_show_neighbor_vty (vty, NULL, show_all, NULL, uj, NULL);
}
ALIAS (show_ip_bgp_neighbors,
{
u_char uj = use_json(argc, argv);
- return bgp_show_neighbor_vty (vty, NULL, show_peer, argv[argc - 2], uj);
+ return bgp_show_neighbor_vty (vty, NULL, show_peer, argv[argc - 2], uj, NULL);
}
ALIAS (show_ip_bgp_neighbors_peer,
{
u_char uj = use_json(argc, argv);
- return bgp_show_neighbor_vty (vty, argv[1], show_all, NULL, uj);
+ return bgp_show_neighbor_vty (vty, argv[1], show_all, NULL, uj, NULL);
}
DEFUN (show_ip_bgp_instance_all_neighbors,
{
u_char uj = use_json(argc, argv);
- return bgp_show_neighbor_vty (vty, argv[1], show_peer, argv[2], uj);
+ return bgp_show_neighbor_vty (vty, argv[1], show_peer, argv[2], uj, NULL);
}
ALIAS (show_ip_bgp_instance_neighbors_peer,