ospfd: fix show ip ospf neigh json

Same ospf neigbor can be learnt via multiple
interfaces, ospf detail json only displayed
last instance only.

Fix json output format to contain "neighbors"
keyword, under which to display all neighbors
for a given vrf.

Fix
show ip ospf neighbor detail json
show ip ospf neighbor detail all json
show ip ospf neighbor <intf name> detail json

Ticket:CM-25528
Reviewed By:
Testing Done:

Run the output with JSON formatter and the output
has passed.

switch1# show ip ospf vrf all neighbor detail json
{
  "default":{
    "vrfName":"default",
    "vrfId":0,
    "neighbors":{
      "0.0.0.2":[
        {
          "ifaceAddress":"14.0.0.22",
          "areaId":"0.0.0.0",
          "ifaceName":"Bridge1.510",
          "nbrPriority":1,
          "nbrState":"Full",
          "stateChangeCounter":6,
          "lastPrgrsvChangeMsec":82668,
          "routerDesignatedId":"14.0.0.22",
          "routerDesignatedBackupId":"14.0.0.21",
          "optionsCounter":2,
          "optionsList":"*|-|-|-|-|-|E|-",
          "routerDeadIntervalTimerDueMsec":36195,
          "databaseSummaryListCounter":0,
          "linkStateRequestListCounter":0,
          "linkStateRetransmissionListCounter":0,
          "threadInactivityTimer":"on",
          "threadLinkStateRequestRetransmission":"on",
          "threadLinkStateUpdateRetransmission":"on",
          "peerBfdInfo":{
            "type":"single hop",
            "detectMultiplier":4,
            "rxMinInterval":600,
            "txMinInterval":800,
            "status":"Down",
            "lastUpdate":"0:00:00:29"
          }
        },
        {
          "ifaceAddress":"14.0.0.26",
          "areaId":"0.0.0.0",
          "ifaceName":"Bridge1.511",
          "nbrPriority":1,
          "nbrState":"Full",
          "stateChangeCounter":6,
          "lastPrgrsvChangeMsec":82658,
          "routerDesignatedId":"14.0.0.26",
          "routerDesignatedBackupId":"14.0.0.25",
          "optionsCounter":2,
          "optionsList":"*|-|-|-|-|-|E|-",
          "routerDeadIntervalTimerDueMsec":36196,
          "databaseSummaryListCounter":0,
          "linkStateRequestListCounter":0,
          "linkStateRetransmissionListCounter":0,
          "threadInactivityTimer":"on",
          "threadLinkStateRequestRetransmission":"on",
          "threadLinkStateUpdateRetransmission":"on"
        },
     ]
    }
  }
}

Signed-off-by: Chirag Shah <chirag@cumulusnetworks.com>
This commit is contained in:
Chirag Shah 2019-07-05 15:45:56 -07:00
parent f85d5e3d97
commit cb0b2ac6a5

View File

@ -4884,14 +4884,36 @@ static void show_ip_ospf_nbr_nbma_detail_sub(struct vty *vty,
static void show_ip_ospf_neighbor_detail_sub(struct vty *vty, static void show_ip_ospf_neighbor_detail_sub(struct vty *vty,
struct ospf_interface *oi, struct ospf_interface *oi,
struct ospf_neighbor *nbr, struct ospf_neighbor *nbr,
struct ospf_neighbor *prev_nbr,
json_object *json, bool use_json) json_object *json, bool use_json)
{ {
char timebuf[OSPF_TIME_DUMP_SIZE]; char timebuf[OSPF_TIME_DUMP_SIZE];
json_object *json_sub = NULL; json_object *json_neigh = NULL, *json_neigh_array = NULL;
char neigh_str[INET_ADDRSTRLEN] = {0};
if (use_json) if (use_json) {
json_sub = json_object_new_object(); if (prev_nbr &&
else { !IPV4_ADDR_SAME(&prev_nbr->src, &nbr->src)) {
json_neigh_array = NULL;
}
if (nbr->state == NSM_Attempt && nbr->router_id.s_addr == 0)
strlcpy(neigh_str, "noNbrId", sizeof(neigh_str));
else
strlcpy(neigh_str, inet_ntoa(nbr->router_id),
sizeof(neigh_str));
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_neigh = json_object_new_object();
} else {
/* Show neighbor ID. */ /* Show neighbor ID. */
if (nbr->state == NSM_Attempt && nbr->router_id.s_addr == 0) if (nbr->state == NSM_Attempt && nbr->router_id.s_addr == 0)
vty_out(vty, " Neighbor %s,", "-"); vty_out(vty, " Neighbor %s,", "-");
@ -4902,7 +4924,7 @@ static void show_ip_ospf_neighbor_detail_sub(struct vty *vty,
/* Show interface address. */ /* Show interface address. */
if (use_json) if (use_json)
json_object_string_add(json_sub, "ifaceAddress", json_object_string_add(json_neigh, "ifaceAddress",
inet_ntoa(nbr->address.u.prefix4)); inet_ntoa(nbr->address.u.prefix4));
else else
vty_out(vty, " interface address %s\n", vty_out(vty, " interface address %s\n",
@ -4910,18 +4932,18 @@ static void show_ip_ospf_neighbor_detail_sub(struct vty *vty,
/* Show Area ID. */ /* Show Area ID. */
if (use_json) { if (use_json) {
json_object_string_add(json_sub, "areaId", json_object_string_add(json_neigh, "areaId",
ospf_area_desc_string(oi->area)); ospf_area_desc_string(oi->area));
json_object_string_add(json_sub, "ifaceName", oi->ifp->name); json_object_string_add(json_neigh, "ifaceName", oi->ifp->name);
} else } else
vty_out(vty, " In the area %s via interface %s\n", vty_out(vty, " In the area %s via interface %s\n",
ospf_area_desc_string(oi->area), oi->ifp->name); ospf_area_desc_string(oi->area), oi->ifp->name);
/* Show neighbor priority and state. */ /* Show neighbor priority and state. */
if (use_json) { if (use_json) {
json_object_int_add(json_sub, "nbrPriority", nbr->priority); json_object_int_add(json_neigh, "nbrPriority", nbr->priority);
json_object_string_add( json_object_string_add(
json_sub, "nbrState", json_neigh, "nbrState",
lookup_msg(ospf_nsm_state_msg, nbr->state, NULL)); lookup_msg(ospf_nsm_state_msg, nbr->state, NULL));
} else } else
vty_out(vty, " Neighbor priority is %d, State is %s,", vty_out(vty, " Neighbor priority is %d, State is %s,",
@ -4930,7 +4952,7 @@ static void show_ip_ospf_neighbor_detail_sub(struct vty *vty,
/* Show state changes. */ /* Show state changes. */
if (use_json) if (use_json)
json_object_int_add(json_sub, "stateChangeCounter", json_object_int_add(json_neigh, "stateChangeCounter",
nbr->state_change); nbr->state_change);
else else
vty_out(vty, " %d state changes\n", nbr->state_change); vty_out(vty, " %d state changes\n", nbr->state_change);
@ -4942,7 +4964,7 @@ static void show_ip_ospf_neighbor_detail_sub(struct vty *vty,
time_store = time_store =
monotime_since(&nbr->ts_last_progress, &res) / 1000LL; monotime_since(&nbr->ts_last_progress, &res) / 1000LL;
if (use_json) { if (use_json) {
json_object_int_add(json_sub, "lastPrgrsvChangeMsec", json_object_int_add(json_neigh, "lastPrgrsvChangeMsec",
time_store); time_store);
} else { } else {
vty_out(vty, vty_out(vty,
@ -4960,12 +4982,13 @@ static void show_ip_ospf_neighbor_detail_sub(struct vty *vty,
time_store = time_store =
monotime_since(&nbr->ts_last_regress, &res) / 1000LL; monotime_since(&nbr->ts_last_regress, &res) / 1000LL;
if (use_json) { if (use_json) {
json_object_int_add(json_sub, json_object_int_add(json_neigh,
"lastRegressiveChangeMsec", "lastRegressiveChangeMsec",
time_store); time_store);
if (nbr->last_regress_str) if (nbr->last_regress_str)
json_object_string_add( json_object_string_add(
json_sub, "lastRegressiveChangeReason", json_neigh,
"lastRegressiveChangeReason",
nbr->last_regress_str); nbr->last_regress_str);
} else { } else {
vty_out(vty, vty_out(vty,
@ -4979,22 +5002,22 @@ static void show_ip_ospf_neighbor_detail_sub(struct vty *vty,
/* Show Designated Rotuer ID. */ /* Show Designated Rotuer ID. */
if (use_json) if (use_json)
json_object_string_add(json_sub, "routerDesignatedId", json_object_string_add(json_neigh, "routerDesignatedId",
inet_ntoa(nbr->d_router)); inet_ntoa(nbr->d_router));
else else
vty_out(vty, " DR is %s,", inet_ntoa(nbr->d_router)); vty_out(vty, " DR is %s,", inet_ntoa(nbr->d_router));
/* Show Backup Designated Rotuer ID. */ /* Show Backup Designated Rotuer ID. */
if (use_json) if (use_json)
json_object_string_add(json_sub, "routerDesignatedBackupId", json_object_string_add(json_neigh, "routerDesignatedBackupId",
inet_ntoa(nbr->bd_router)); inet_ntoa(nbr->bd_router));
else else
vty_out(vty, " BDR is %s\n", inet_ntoa(nbr->bd_router)); vty_out(vty, " BDR is %s\n", inet_ntoa(nbr->bd_router));
/* Show options. */ /* Show options. */
if (use_json) { if (use_json) {
json_object_int_add(json_sub, "optionsCounter", nbr->options); json_object_int_add(json_neigh, "optionsCounter", nbr->options);
json_object_string_add(json_sub, "optionsList", json_object_string_add(json_neigh, "optionsList",
ospf_options_dump(nbr->options)); ospf_options_dump(nbr->options));
} else } else
vty_out(vty, " Options %d %s\n", nbr->options, vty_out(vty, " Options %d %s\n", nbr->options,
@ -5007,12 +5030,13 @@ static void show_ip_ospf_neighbor_detail_sub(struct vty *vty,
time_store = monotime_until(&nbr->t_inactivity->u.sands, time_store = monotime_until(&nbr->t_inactivity->u.sands,
NULL) NULL)
/ 1000LL; / 1000LL;
json_object_int_add(json_sub, json_object_int_add(json_neigh,
"routerDeadIntervalTimerDueMsec", "routerDeadIntervalTimerDueMsec",
time_store); time_store);
} else } else
json_object_int_add( json_object_int_add(
json_sub, "routerDeadIntervalTimerDueMsec", -1); json_neigh,
"routerDeadIntervalTimerDueMsec", -1);
} else } else
vty_out(vty, " Dead timer due in %s\n", vty_out(vty, " Dead timer due in %s\n",
ospf_timer_dump(nbr->t_inactivity, timebuf, ospf_timer_dump(nbr->t_inactivity, timebuf,
@ -5020,7 +5044,7 @@ static void show_ip_ospf_neighbor_detail_sub(struct vty *vty,
/* Show Database Summary list. */ /* Show Database Summary list. */
if (use_json) if (use_json)
json_object_int_add(json_sub, "databaseSummaryListCounter", json_object_int_add(json_neigh, "databaseSummaryListCounter",
ospf_db_summary_count(nbr)); ospf_db_summary_count(nbr));
else else
vty_out(vty, " Database Summary List %d\n", vty_out(vty, " Database Summary List %d\n",
@ -5028,7 +5052,7 @@ static void show_ip_ospf_neighbor_detail_sub(struct vty *vty,
/* Show Link State Request list. */ /* Show Link State Request list. */
if (use_json) if (use_json)
json_object_int_add(json_sub, "linkStateRequestListCounter", json_object_int_add(json_neigh, "linkStateRequestListCounter",
ospf_ls_request_count(nbr)); ospf_ls_request_count(nbr));
else else
vty_out(vty, " Link State Request List %ld\n", vty_out(vty, " Link State Request List %ld\n",
@ -5036,7 +5060,7 @@ static void show_ip_ospf_neighbor_detail_sub(struct vty *vty,
/* Show Link State Retransmission list. */ /* Show Link State Retransmission list. */
if (use_json) if (use_json)
json_object_int_add(json_sub, json_object_int_add(json_neigh,
"linkStateRetransmissionListCounter", "linkStateRetransmissionListCounter",
ospf_ls_retransmit_count(nbr)); ospf_ls_retransmit_count(nbr));
else else
@ -5046,7 +5070,7 @@ static void show_ip_ospf_neighbor_detail_sub(struct vty *vty,
/* Show inactivity timer thread. */ /* Show inactivity timer thread. */
if (use_json) { if (use_json) {
if (nbr->t_inactivity != NULL) if (nbr->t_inactivity != NULL)
json_object_string_add(json_sub, json_object_string_add(json_neigh,
"threadInactivityTimer", "on"); "threadInactivityTimer", "on");
} else } else
vty_out(vty, " Thread Inactivity Timer %s\n", vty_out(vty, " Thread Inactivity Timer %s\n",
@ -5056,7 +5080,7 @@ static void show_ip_ospf_neighbor_detail_sub(struct vty *vty,
if (use_json) { if (use_json) {
if (nbr->t_db_desc != NULL) if (nbr->t_db_desc != NULL)
json_object_string_add( json_object_string_add(
json_sub, json_neigh,
"threadDatabaseDescriptionRetransmission", "threadDatabaseDescriptionRetransmission",
"on"); "on");
} else } else
@ -5068,7 +5092,7 @@ static void show_ip_ospf_neighbor_detail_sub(struct vty *vty,
if (use_json) { if (use_json) {
if (nbr->t_ls_req != NULL) if (nbr->t_ls_req != NULL)
json_object_string_add( json_object_string_add(
json_sub, json_neigh,
"threadLinkStateRequestRetransmission", "on"); "threadLinkStateRequestRetransmission", "on");
} else } else
vty_out(vty, vty_out(vty,
@ -5079,22 +5103,19 @@ static void show_ip_ospf_neighbor_detail_sub(struct vty *vty,
if (use_json) { if (use_json) {
if (nbr->t_ls_upd != NULL) if (nbr->t_ls_upd != NULL)
json_object_string_add( json_object_string_add(
json_sub, "threadLinkStateUpdateRetransmission", json_neigh,
"threadLinkStateUpdateRetransmission",
"on"); "on");
} else } else
vty_out(vty, vty_out(vty,
" Thread Link State Update Retransmission %s\n\n", " Thread Link State Update Retransmission %s\n\n",
nbr->t_ls_upd != NULL ? "on" : "off"); nbr->t_ls_upd != NULL ? "on" : "off");
if (use_json) { ospf_bfd_show_info(vty, nbr->bfd_info, json_neigh, use_json, 0);
if (nbr->state == NSM_Attempt && nbr->router_id.s_addr == 0)
json_object_object_add(json, "noNbrId", json_sub); if (use_json)
else json_object_array_add(json_neigh_array, json_neigh);
json_object_object_add(json, inet_ntoa(nbr->router_id),
json_sub);
}
ospf_bfd_show_info(vty, nbr->bfd_info, json, use_json, 0);
} }
static int show_ip_ospf_neighbor_id_common(struct vty *vty, struct ospf *ospf, static int show_ip_ospf_neighbor_id_common(struct vty *vty, struct ospf *ospf,
@ -5121,8 +5142,8 @@ static int show_ip_ospf_neighbor_id_common(struct vty *vty, struct ospf *ospf,
for (ALL_LIST_ELEMENTS_RO(ospf->oiflist, node, oi)) { for (ALL_LIST_ELEMENTS_RO(ospf->oiflist, node, oi)) {
if ((nbr = ospf_nbr_lookup_by_routerid(oi->nbrs, router_id))) { if ((nbr = ospf_nbr_lookup_by_routerid(oi->nbrs, router_id))) {
show_ip_ospf_neighbor_detail_sub(vty, oi, nbr, json, show_ip_ospf_neighbor_detail_sub(vty, oi, nbr, NULL,
use_json); json, use_json);
} }
} }
@ -5192,16 +5213,20 @@ static int show_ip_ospf_neighbor_detail_common(struct vty *vty,
struct ospf_interface *oi; struct ospf_interface *oi;
struct listnode *node; struct listnode *node;
json_object *json_vrf = NULL; json_object *json_vrf = NULL;
json_object *json_nbr_sub = NULL;
if (use_json) { if (use_json) {
if (use_vrf) if (use_vrf)
json_vrf = json_object_new_object(); json_vrf = json_object_new_object();
else else
json_vrf = json; json_vrf = json;
json_nbr_sub = json_object_new_object();
} }
if (ospf->instance) { if (ospf->instance) {
if (use_json) if (use_json)
json_object_int_add(json_vrf, "ospfInstance", json_object_int_add(json, "ospfInstance",
ospf->instance); ospf->instance);
else else
vty_out(vty, "\nOSPF Instance: %d\n\n", ospf->instance); vty_out(vty, "\nOSPF Instance: %d\n\n", ospf->instance);
@ -5211,22 +5236,25 @@ static int show_ip_ospf_neighbor_detail_common(struct vty *vty,
for (ALL_LIST_ELEMENTS_RO(ospf->oiflist, node, oi)) { for (ALL_LIST_ELEMENTS_RO(ospf->oiflist, node, oi)) {
struct route_node *rn; struct route_node *rn;
struct ospf_neighbor *nbr; struct ospf_neighbor *nbr, *prev_nbr = 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)) {
if (nbr != oi->nbr_self) { if (nbr != oi->nbr_self) {
if (nbr->state != NSM_Down) { if (nbr->state != NSM_Down) {
show_ip_ospf_neighbor_detail_sub( show_ip_ospf_neighbor_detail_sub(
vty, oi, nbr, json_vrf, vty, oi, nbr, prev_nbr,
use_json); json_nbr_sub, use_json);
} }
} }
prev_nbr = nbr;
} }
} }
} }
if (use_json) { if (use_json) {
json_object_object_add(json_vrf, "neighbors",
json_nbr_sub);
if (use_vrf) { if (use_vrf) {
if (ospf->vrf_id == VRF_DEFAULT) if (ospf->vrf_id == VRF_DEFAULT)
json_object_object_add(json, "default", json_object_object_add(json, "default",
@ -5389,16 +5417,20 @@ static int show_ip_ospf_neighbor_detail_all_common(struct vty *vty,
for (ALL_LIST_ELEMENTS_RO(ospf->oiflist, node, oi)) { for (ALL_LIST_ELEMENTS_RO(ospf->oiflist, node, oi)) {
struct route_node *rn; struct route_node *rn;
struct ospf_neighbor *nbr; struct ospf_neighbor *nbr, *prev_nbr = NULL;
struct ospf_nbr_nbma *nbr_nbma; struct ospf_nbr_nbma *nbr_nbma;
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)) {
if (nbr != oi->nbr_self) if (nbr != oi->nbr_self)
if (nbr->state != NSM_Down) if (nbr->state != NSM_Down)
show_ip_ospf_neighbor_detail_sub( show_ip_ospf_neighbor_detail_sub(
vty, oi, rn->info, vty, oi, rn->info,
prev_nbr,
json_vrf, use_json); json_vrf, use_json);
prev_nbr = nbr;
}
}
if (oi->type == OSPF_IFTYPE_NBMA) { if (oi->type == OSPF_IFTYPE_NBMA) {
struct listnode *nd; struct listnode *nd;
@ -5594,6 +5626,7 @@ static int show_ip_ospf_neighbor_int_detail_common(struct vty *vty,
if (nbr->state != NSM_Down) if (nbr->state != NSM_Down)
show_ip_ospf_neighbor_detail_sub( show_ip_ospf_neighbor_detail_sub(
vty, oi, nbr, vty, oi, nbr,
NULL,
json, use_json); json, use_json);
} }
} }
@ -5632,7 +5665,7 @@ DEFUN (show_ip_ospf_neighbor_int_detail,
if (!ospf->oi_running) if (!ospf->oi_running)
continue; continue;
ospf_output = true; ospf_output = true;
ret = show_ip_ospf_neighbor_int_detail_common(vty, ospf, 0, ret = show_ip_ospf_neighbor_int_detail_common(vty, ospf, 4,
argv, uj); argv, uj);
} }