summaryrefslogtreecommitdiff
path: root/ospf6d/ospf6_top.c
diff options
context:
space:
mode:
authorgithub login name <ranjany@vmware.com>2020-10-13 05:44:52 -0700
committerYash Ranjan <ranjany@vmware.com>2020-11-16 19:37:26 -0800
commit35a45deadaa57d2dd671d6b179d77b8d2cee57c1 (patch)
treeedb55d0a8f19398a42ffb1ad583eff86f4902330 /ospf6d/ospf6_top.c
parent0b75cd0bf3fa4093d0b5ac93f30bf8f656fb5edb (diff)
ospf6d: Json support added for command "show ipv6 ospf6 [json]"
Modify code to add JSON format output in show command "show ipv6 ospf6" with proper formating. Signed-off-by: Yash Ranjan <ranjany@vmware.com>
Diffstat (limited to 'ospf6d/ospf6_top.c')
-rw-r--r--ospf6d/ospf6_top.c252
1 files changed, 185 insertions, 67 deletions
diff --git a/ospf6d/ospf6_top.c b/ospf6d/ospf6_top.c
index 95c72290d0..cbfa8ba3d5 100644
--- a/ospf6d/ospf6_top.c
+++ b/ospf6d/ospf6_top.c
@@ -29,6 +29,7 @@
#include "thread.h"
#include "command.h"
#include "defaults.h"
+#include "lib/json.h"
#include "lib_errors.h"
#include "ospf6_proto.h"
@@ -954,90 +955,207 @@ DEFUN (no_ospf6_stub_router_shutdown,
}
#endif
-static void ospf6_show(struct vty *vty, struct ospf6 *o)
+
+static void ospf6_show(struct vty *vty, struct ospf6 *o, json_object *json,
+ bool use_json)
{
struct listnode *n;
struct ospf6_area *oa;
char router_id[16], duration[32];
struct timeval now, running, result;
char buf[32], rbuf[32];
+ json_object *json_areas = NULL;
+ const char *adjacency;
+
+ if (use_json) {
+ json_areas = json_object_new_object();
+
+ /* process id, router id */
+ inet_ntop(AF_INET, &o->router_id, router_id, sizeof(router_id));
+ json_object_string_add(json, "routerId", router_id);
+
+ /* running time */
+ monotime(&now);
+ timersub(&now, &o->starttime, &running);
+ timerstring(&running, duration, sizeof(duration));
+ json_object_string_add(json, "running", duration);
+
+ /* Redistribute configuration */
+ /* XXX */
+ json_object_int_add(json, "lsaMinimumArrivalMsecs",
+ o->lsa_minarrival);
+
+ /* Show SPF parameters */
+ json_object_int_add(json, "spfScheduleDelayMsecs",
+ o->spf_delay);
+ json_object_int_add(json, "holdTimeMinMsecs", o->spf_holdtime);
+ json_object_int_add(json, "holdTimeMaxMsecs",
+ o->spf_max_holdtime);
+ json_object_int_add(json, "holdTimeMultiplier",
+ o->spf_hold_multiplier);
+
+
+ if (o->ts_spf.tv_sec || o->ts_spf.tv_usec) {
+ timersub(&now, &o->ts_spf, &result);
+ timerstring(&result, buf, sizeof(buf));
+ ospf6_spf_reason_string(o->last_spf_reason, rbuf,
+ sizeof(rbuf));
+ json_object_boolean_true_add(json, "spfHasRun");
+ json_object_string_add(json, "spfLastExecutedMsecs",
+ buf);
+ json_object_string_add(json, "spfLastExecutedReason",
+ rbuf);
+
+ json_object_int_add(
+ json, "spfLastDurationSecs",
+ (long long)o->ts_spf_duration.tv_sec);
+
+ json_object_int_add(
+ json, "spfLastDurationMsecs",
+ (long long)o->ts_spf_duration.tv_usec);
+ } else
+ json_object_boolean_false_add(json, "spfHasRun");
+
+
+ threadtimer_string(now, o->t_spf_calc, buf, sizeof(buf));
+ if (o->t_spf_calc) {
+ long time_store;
+
+ json_object_boolean_true_add(json, "spfTimerActive");
+ time_store =
+ monotime_until(&o->t_spf_calc->u.sands, NULL)
+ / 1000LL;
+ json_object_int_add(json, "spfTimerDueInMsecs",
+ time_store);
+ } else
+ json_object_boolean_false_add(json, "spfTimerActive");
+
+ json_object_boolean_add(json, "routerIsStubRouter",
+ CHECK_FLAG(o->flag, OSPF6_STUB_ROUTER));
+
+ /* LSAs */
+ json_object_int_add(json, "numberOfAsScopedLsa",
+ o->lsdb->count);
+ /* Areas */
+ json_object_int_add(json, "numberOfAreaInRouter",
+ listcount(o->area_list));
+
+ if (CHECK_FLAG(o->config_flags, OSPF6_LOG_ADJACENCY_CHANGES)) {
+ if (CHECK_FLAG(o->config_flags,
+ OSPF6_LOG_ADJACENCY_DETAIL))
+ adjacency = "LoggedAll";
+ else
+ adjacency = "Logged";
+ } else
+ adjacency = "NotLogged";
+ json_object_string_add(json, "adjacencyChanges", adjacency);
+
+ for (ALL_LIST_ELEMENTS_RO(o->area_list, n, oa))
+ ospf6_area_show(vty, oa, json_areas, use_json);
+
+ json_object_object_add(json, "areas", json_areas);
+
+ vty_out(vty, "%s\n",
+ json_object_to_json_string_ext(
+ json, JSON_C_TO_STRING_PRETTY));
+
+ } else {
+ /* process id, router id */
+ inet_ntop(AF_INET, &o->router_id, router_id, sizeof(router_id));
+ vty_out(vty, " OSPFv3 Routing Process (0) with Router-ID %s\n",
+ router_id);
+
+ /* running time */
+ monotime(&now);
+ timersub(&now, &o->starttime, &running);
+ timerstring(&running, duration, sizeof(duration));
+ vty_out(vty, " Running %s\n", duration);
+
+ /* Redistribute configuration */
+ /* XXX */
+ vty_out(vty, " LSA minimum arrival %d msecs\n",
+ o->lsa_minarrival);
+
+
+ /* Show SPF parameters */
+ vty_out(vty,
+ " Initial SPF scheduling delay %d millisec(s)\n"
+ " Minimum hold time between consecutive SPFs %d millsecond(s)\n"
+ " Maximum hold time between consecutive SPFs %d millsecond(s)\n"
+ " Hold time multiplier is currently %d\n",
+ o->spf_delay, o->spf_holdtime, o->spf_max_holdtime,
+ o->spf_hold_multiplier);
+
+
+ vty_out(vty, " SPF algorithm ");
+ if (o->ts_spf.tv_sec || o->ts_spf.tv_usec) {
+ timersub(&now, &o->ts_spf, &result);
+ timerstring(&result, buf, sizeof(buf));
+ ospf6_spf_reason_string(o->last_spf_reason, rbuf,
+ sizeof(rbuf));
+ vty_out(vty, "last executed %s ago, reason %s\n", buf,
+ rbuf);
+ vty_out(vty, " Last SPF duration %lld sec %lld usec\n",
+ (long long)o->ts_spf_duration.tv_sec,
+ (long long)o->ts_spf_duration.tv_usec);
+ } else
+ vty_out(vty, "has not been run\n");
+
+ threadtimer_string(now, o->t_spf_calc, buf, sizeof(buf));
+ vty_out(vty, " SPF timer %s%s\n",
+ (o->t_spf_calc ? "due in " : "is "), buf);
+
+ if (CHECK_FLAG(o->flag, OSPF6_STUB_ROUTER))
+ vty_out(vty, " Router Is Stub Router\n");
+
+ /* LSAs */
+ vty_out(vty, " Number of AS scoped LSAs is %u\n",
+ o->lsdb->count);
+
+ /* Areas */
+ vty_out(vty, " Number of areas in this router is %u\n",
+ listcount(o->area_list));
+
+ if (CHECK_FLAG(o->config_flags, OSPF6_LOG_ADJACENCY_CHANGES)) {
+ if (CHECK_FLAG(o->config_flags,
+ OSPF6_LOG_ADJACENCY_DETAIL))
+ vty_out(vty,
+ " All adjacency changes are logged\n");
+ else
+ vty_out(vty, " Adjacency changes are logged\n");
+ }
- /* process id, router id */
- inet_ntop(AF_INET, &o->router_id, router_id, sizeof(router_id));
- vty_out(vty, " OSPFv3 Routing Process (0) with Router-ID %s\n",
- router_id);
-
- /* running time */
- monotime(&now);
- timersub(&now, &o->starttime, &running);
- timerstring(&running, duration, sizeof(duration));
- vty_out(vty, " Running %s\n", duration);
-
- /* Redistribute configuration */
- /* XXX */
-
- vty_out(vty, " LSA minimum arrival %d msecs\n", o->lsa_minarrival);
-
- /* Show SPF parameters */
- vty_out(vty,
- " Initial SPF scheduling delay %d millisec(s)\n"
- " Minimum hold time between consecutive SPFs %d millsecond(s)\n"
- " Maximum hold time between consecutive SPFs %d millsecond(s)\n"
- " Hold time multiplier is currently %d\n",
- o->spf_delay, o->spf_holdtime, o->spf_max_holdtime,
- o->spf_hold_multiplier);
-
- vty_out(vty, " SPF algorithm ");
- if (o->ts_spf.tv_sec || o->ts_spf.tv_usec) {
- timersub(&now, &o->ts_spf, &result);
- timerstring(&result, buf, sizeof(buf));
- ospf6_spf_reason_string(o->last_spf_reason, rbuf, sizeof(rbuf));
- vty_out(vty, "last executed %s ago, reason %s\n", buf, rbuf);
- vty_out(vty, " Last SPF duration %lld sec %lld usec\n",
- (long long)o->ts_spf_duration.tv_sec,
- (long long)o->ts_spf_duration.tv_usec);
- } else
- vty_out(vty, "has not been run\n");
- threadtimer_string(now, o->t_spf_calc, buf, sizeof(buf));
- vty_out(vty, " SPF timer %s%s\n", (o->t_spf_calc ? "due in " : "is "),
- buf);
-
- if (CHECK_FLAG(o->flag, OSPF6_STUB_ROUTER))
- vty_out(vty, " Router Is Stub Router\n");
-
- /* LSAs */
- vty_out(vty, " Number of AS scoped LSAs is %u\n", o->lsdb->count);
-
- /* Areas */
- vty_out(vty, " Number of areas in this router is %u\n",
- listcount(o->area_list));
-
- if (CHECK_FLAG(o->config_flags, OSPF6_LOG_ADJACENCY_CHANGES)) {
- if (CHECK_FLAG(o->config_flags, OSPF6_LOG_ADJACENCY_DETAIL))
- vty_out(vty, " All adjacency changes are logged\n");
- else
- vty_out(vty, " Adjacency changes are logged\n");
- }
- vty_out(vty, "\n");
+ vty_out(vty, "\n");
- for (ALL_LIST_ELEMENTS_RO(o->area_list, n, oa))
- ospf6_area_show(vty, oa);
+ for (ALL_LIST_ELEMENTS_RO(o->area_list, n, oa))
+ ospf6_area_show(vty, oa, json_areas, use_json);
+ }
}
/* show top level structures */
-DEFUN (show_ipv6_ospf6,
- show_ipv6_ospf6_cmd,
- "show ipv6 ospf6",
- SHOW_STR
- IP6_STR
- OSPF6_STR)
+DEFUN(show_ipv6_ospf6,
+ show_ipv6_ospf6_cmd,
+ "show ipv6 ospf6 [json]",
+ SHOW_STR
+ IP6_STR
+ OSPF6_STR
+ JSON_STR)
{
struct ospf6 *ospf6;
+ bool uj = use_json(argc, argv);
+ json_object *json = NULL;
ospf6 = ospf6_lookup_by_vrf_name(VRF_DEFAULT_NAME);
OSPF6_CMD_CHECK_RUNNING(ospf6);
- ospf6_show(vty, ospf6);
+
+ if (uj)
+ json = json_object_new_object();
+
+ ospf6_show(vty, ospf6, json, uj);
+
+ if (uj)
+ json_object_free(json);
return CMD_SUCCESS;
}