ospfd: fix show ospf neigh json for multile nbrs

Same neighbor learned from multiple ospf interfaces
(all) were not displayed in json, only last was displayed.
Created list within dictionary using neighbor-id as key.
lookup neigbhor-id in json obejct prior to creating new list.

spine-2# show ip ospf neighbor

Neighbor ID   Pri State     Dead Time Address     Interface
0.0.1.16        1 Full/DR   36.754s   8.0.3.15    swp1:8.0.3.16
0.0.1.16        1 Full/DR   30.903s   7.0.3.15    swp2:7.0.3.16

spine-2# show ip ospf neighbor json
{
   "0.0.1.16":[
   {
      "priority":1,
         "state":"Full\/DR",
         "deadTimeMsecs":35114,
         "address":"8.0.3.15",
         "ifaceName":"swp1:8.0.3.16",
         "retransmitCounter":0,
         "requestCounter":0,
         "dbSummaryCounter":0
   },
   {
      "priority":1,
      "state":"Full\/DR",
      "deadTimeMsecs":39264,
      "address":"7.0.3.15",
      "ifaceName":"swp2:7.0.3.16",
      "retransmitCounter":0,
      "requestCounter":0,
      "dbSummaryCounter":0
   }
   ]
}

Signed-off-by: Chirag Shah <chirag@cumulusnetworks.com>
This commit is contained in:
Chirag Shah 2017-10-30 11:56:59 -07:00
parent b1c3ae8ce3
commit cef262c34a

View File

@ -4132,87 +4132,88 @@ static void show_ip_ospf_neighbor_sub(struct vty *vty,
json_object *json, u_char use_json) json_object *json, u_char use_json)
{ {
struct route_node *rn; struct route_node *rn;
struct ospf_neighbor *nbr; struct ospf_neighbor *nbr, *prev_nbr = NULL;
char msgbuf[16]; char msgbuf[16];
char timebuf[OSPF_TIME_DUMP_SIZE]; char timebuf[OSPF_TIME_DUMP_SIZE];
json_object *json_neighbor = NULL; json_object *json_neighbor = NULL, *json_neigh_array = NULL;
for (rn = route_top(oi->nbrs); rn; rn = route_next(rn)) { for (rn = route_top(oi->nbrs); rn; rn = route_next(rn)) {
if ((nbr = rn->info)) { if ((nbr = rn->info)) {
/* Do not show myself. */ /* Do not show myself. */
if (nbr != oi->nbr_self) { if (nbr == oi->nbr_self)
continue;
/* Down state is not shown. */ /* Down state is not shown. */
if (nbr->state != NSM_Down) { if (nbr->state == NSM_Down)
continue;
if (use_json) { if (use_json) {
char neigh_str[INET_ADDRSTRLEN];
if (prev_nbr &&
!IPV4_ADDR_SAME(&prev_nbr->src, &nbr->src)) {
/* Start new neigh list */
json_neigh_array = NULL;
}
if (nbr->state == NSM_Attempt &&
nbr->router_id.s_addr == 0)
strncpy(neigh_str, "neighbor",
INET_ADDRSTRLEN);
else
strncpy(neigh_str,
inet_ntoa(nbr->router_id),
INET_ADDRSTRLEN);
json_object_object_get_ex(json, neigh_str,
&json_neigh_array);
if (!json_neigh_array) {
json_neigh_array = json_object_new_array();
json_object_object_add(json, neigh_str,
json_neigh_array);
}
json_neighbor = json_neighbor =
json_object_new_object(); json_object_new_object();
ospf_nbr_state_message(
nbr, msgbuf, 16); ospf_nbr_state_message(nbr, msgbuf, 16);
long time_store; long time_store;
time_store = time_store = monotime_until(
monotime_until( &nbr->t_inactivity->u.sands,
&nbr->t_inactivity NULL) / 1000LL;
->u
.sands,
NULL)
/ 1000LL;
json_object_int_add( json_object_int_add(json_neighbor,
json_neighbor,
"priority", "priority",
nbr->priority); nbr->priority);
json_object_string_add( json_object_string_add(json_neighbor, "state",
json_neighbor, "state",
msgbuf); msgbuf);
json_object_int_add( json_object_int_add(json_neighbor,
json_neighbor,
"deadTimeMsecs", "deadTimeMsecs",
time_store); time_store);
json_object_string_add( json_object_string_add(json_neighbor,
json_neighbor,
"address", "address",
inet_ntoa(nbr->src)); inet_ntoa(nbr->src));
json_object_string_add( json_object_string_add(json_neighbor,
json_neighbor,
"ifaceName", "ifaceName",
IF_NAME(oi)); IF_NAME(oi));
json_object_int_add( json_object_int_add(json_neighbor,
json_neighbor,
"retransmitCounter", "retransmitCounter",
ospf_ls_retransmit_count( ospf_ls_retransmit_count(nbr));
nbr)); json_object_int_add(json_neighbor,
json_object_int_add(
json_neighbor,
"requestCounter", "requestCounter",
ospf_ls_request_count( ospf_ls_request_count(nbr));
nbr)); json_object_int_add(json_neighbor,
json_object_int_add(
json_neighbor,
"dbSummaryCounter", "dbSummaryCounter",
ospf_db_summary_count( ospf_db_summary_count(nbr));
nbr));
if (nbr->state == NSM_Attempt json_object_array_add(json_neigh_array,
&& nbr->router_id.s_addr
== 0)
json_object_object_add(
json,
"neighbor",
json_neighbor);
else
json_object_object_add(
json,
inet_ntoa(
nbr->router_id),
json_neighbor); json_neighbor);
} else { } else {
ospf_nbr_state_message( ospf_nbr_state_message(nbr, msgbuf, 16);
nbr, msgbuf, 16);
if (nbr->state == NSM_Attempt if (nbr->state == NSM_Attempt &&
&& nbr->router_id.s_addr nbr->router_id.s_addr == 0)
== 0)
vty_out(vty, vty_out(vty,
"%-15s %3d %-15s ", "%-15s %3d %-15s ",
"-", "-",
@ -4221,30 +4222,23 @@ static void show_ip_ospf_neighbor_sub(struct vty *vty,
else else
vty_out(vty, vty_out(vty,
"%-15s %3d %-15s ", "%-15s %3d %-15s ",
inet_ntoa( inet_ntoa(nbr->router_id),
nbr->router_id),
nbr->priority, nbr->priority,
msgbuf); msgbuf);
vty_out(vty, "%9s ", vty_out(vty, "%9s ",
ospf_timer_dump( ospf_timer_dump(nbr->t_inactivity,
nbr->t_inactivity,
timebuf, timebuf,
sizeof(timebuf))); sizeof(timebuf)));
vty_out(vty, "%-15s ", vty_out(vty, "%-15s ", inet_ntoa(nbr->src));
inet_ntoa(nbr->src));
vty_out(vty, vty_out(vty,
"%-20s %5ld %5ld %5d\n", "%-20s %5ld %5ld %5d\n",
IF_NAME(oi), IF_NAME(oi),
ospf_ls_retransmit_count( ospf_ls_retransmit_count(nbr),
nbr), ospf_ls_request_count(nbr),
ospf_ls_request_count( ospf_db_summary_count(nbr));
nbr),
ospf_db_summary_count(
nbr));
}
}
} }
prev_nbr = nbr;
} }
} }
} }