]> git.puffer.fish Git - matthieu/frr.git/commitdiff
ospf6d: Json support added for command "show ipv6 ospf6 interface traffic [json]"
authorgithub login name <ranjany@vmware.com>
Fri, 9 Oct 2020 07:53:39 +0000 (00:53 -0700)
committerYash Ranjan <ranjany@vmware.com>
Mon, 16 Nov 2020 05:16:10 +0000 (21:16 -0800)
Modify code to add JSON format output in show command
"show ipv6 ospf6 interface traffic" with proper formating

Signed-off-by: Yash Ranjan <ranjany@vmware.com>
doc/user/ospf6d.rst
ospf6d/ospf6_interface.c

index dd53d8f8b4b3604e3d1f1504494c15bd158b404f..e2a81c79988b53b0acfa8d26200f70ff2c572c49 100644 (file)
@@ -185,6 +185,13 @@ Showing OSPF6 information
    Shows state and chosen (Backup) DR of neighbor. JSON output can be
    obtained by appending 'json' at the end.
 
+.. index:: show ipv6 ospf6 interface traffic [json]
+.. clicmd:: show ipv6 ospf6 interface traffic [json]
+
+   Shows counts of different packets that have been recieved and transmitted
+   by the interfaces. JSON output can be obtained by appending "json" at the
+   end.
+
 .. index:: show ipv6 ospf6 request-list A.B.C.D
 .. clicmd:: show ipv6 ospf6 request-list A.B.C.D
 
index 2d1b5e7b5a5682f86e66d2f3d973eb80ff4bfd7b..68615ce672591f28eea5cd66b5598c86d4c5b9c3 100644 (file)
@@ -43,6 +43,7 @@
 #include "ospf6d.h"
 #include "ospf6_bfd.h"
 #include "ospf6_zebra.h"
+#include "lib/json.h"
 
 DEFINE_MTYPE_STATIC(OSPF6D, CFG_PLIST_NAME, "configured prefix list names")
 DEFINE_QOBJ_TYPE(ospf6_interface)
@@ -1032,18 +1033,20 @@ DEFUN (show_ipv6_ospf6_interface,
 
 static int ospf6_interface_show_traffic(struct vty *vty,
                                        struct interface *intf_ifp,
-                                       int display_once)
+                                       int display_once, json_object *json,
+                                       bool use_json)
 {
        struct interface *ifp;
        struct vrf *vrf = NULL;
        struct ospf6_interface *oi = NULL;
+       json_object *json_interface;
 
        if (intf_ifp)
                vrf = vrf_lookup_by_id(intf_ifp->vrf_id);
        else
                vrf = vrf_lookup_by_id(VRF_DEFAULT);
 
-       if (!display_once) {
+       if (!display_once && !use_json) {
                vty_out(vty, "\n");
                vty_out(vty, "%-12s%-17s%-17s%-17s%-17s%-17s\n", "Interface",
                        "    HELLO", "    DB-Desc", "   LS-Req", "   LS-Update",
@@ -1062,61 +1065,158 @@ static int ospf6_interface_show_traffic(struct vty *vty,
                        else
                                continue;
 
-                       vty_out(vty,
-                               "%-10s %8u/%-8u %7u/%-7u %7u/%-7u %7u/%-7u %7u/%-7u\n",
-                               oi->interface->name, oi->hello_in,
-                               oi->hello_out, oi->db_desc_in, oi->db_desc_out,
-                               oi->ls_req_in, oi->ls_req_out, oi->ls_upd_in,
-                               oi->ls_upd_out, oi->ls_ack_in, oi->ls_ack_out);
+                       if (use_json) {
+                               json_interface = json_object_new_object();
+                               json_object_int_add(json_interface, "helloRx",
+                                                   oi->hello_in);
+                               json_object_int_add(json_interface, "helloTx",
+                                                   oi->hello_out);
+                               json_object_int_add(json_interface, "dbDescRx",
+                                                   oi->db_desc_in);
+                               json_object_int_add(json_interface, "dbDescTx",
+                                                   oi->db_desc_out);
+                               json_object_int_add(json_interface, "lsReqRx",
+                                                   oi->ls_req_in);
+                               json_object_int_add(json_interface, "lsReqTx",
+                                                   oi->ls_req_out);
+                               json_object_int_add(json_interface,
+                                                   "lsUpdateRx",
+                                                   oi->ls_upd_in);
+                               json_object_int_add(json_interface,
+                                                   "lsUpdateTx",
+                                                   oi->ls_upd_out);
+                               json_object_int_add(json_interface, "lsAckRx",
+                                                   oi->ls_ack_in);
+                               json_object_int_add(json_interface, "lsAckTx",
+                                                   oi->ls_ack_out);
+
+                               json_object_object_add(json,
+                                                      oi->interface->name,
+                                                      json_interface);
+                       } else
+                               vty_out(vty,
+                                       "%-10s %8u/%-8u %7u/%-7u %7u/%-7u %7u/%-7u %7u/%-7u\n",
+                                       oi->interface->name, oi->hello_in,
+                                       oi->hello_out, oi->db_desc_in,
+                                       oi->db_desc_out, oi->ls_req_in,
+                                       oi->ls_req_out, oi->ls_upd_in,
+                                       oi->ls_upd_out, oi->ls_ack_in,
+                                       oi->ls_ack_out);
                }
        } else {
                oi = intf_ifp->info;
                if (oi == NULL)
                        return CMD_WARNING;
 
-               vty_out(vty,
-                       "%-10s %8u/%-8u %7u/%-7u %7u/%-7u %7u/%-7u %7u/%-7u\n",
-                       oi->interface->name, oi->hello_in, oi->hello_out,
-                       oi->db_desc_in, oi->db_desc_out, oi->ls_req_in,
-                       oi->ls_req_out, oi->ls_upd_in, oi->ls_upd_out,
-                       oi->ls_ack_in, oi->ls_ack_out);
+               if (use_json) {
+                       json_interface = json_object_new_object();
+                       json_object_int_add(json_interface, "helloRx",
+                                           oi->hello_in);
+                       json_object_int_add(json_interface, "helloTx",
+                                           oi->hello_out);
+                       json_object_int_add(json_interface, "dbDescRx",
+                                           oi->db_desc_in);
+                       json_object_int_add(json_interface, "dbDescTx",
+                                           oi->db_desc_out);
+                       json_object_int_add(json_interface, "lsReqRx",
+                                           oi->ls_req_in);
+                       json_object_int_add(json_interface, "lsReqTx",
+                                           oi->ls_req_out);
+                       json_object_int_add(json_interface, "lsUpdateRx",
+                                           oi->ls_upd_in);
+                       json_object_int_add(json_interface, "lsUpdateTx",
+                                           oi->ls_upd_out);
+                       json_object_int_add(json_interface, "lsAckRx",
+                                           oi->ls_ack_in);
+                       json_object_int_add(json_interface, "lsAckTx",
+                                           oi->ls_ack_out);
+
+                       json_object_object_add(json, oi->interface->name,
+                                              json_interface);
+               } else
+                       vty_out(vty,
+                               "%-10s %8u/%-8u %7u/%-7u %7u/%-7u %7u/%-7u %7u/%-7u\n",
+                               oi->interface->name, oi->hello_in,
+                               oi->hello_out, oi->db_desc_in, oi->db_desc_out,
+                               oi->ls_req_in, oi->ls_req_out, oi->ls_upd_in,
+                               oi->ls_upd_out, oi->ls_ack_in, oi->ls_ack_out);
        }
 
        return CMD_SUCCESS;
 }
 
 /* show interface */
-DEFUN (show_ipv6_ospf6_interface_traffic,
-       show_ipv6_ospf6_interface_traffic_cmd,
-       "show ipv6 ospf6 interface traffic [IFNAME]",
-       SHOW_STR
-       IP6_STR
-       OSPF6_STR
-       INTERFACE_STR
-       "Protocol Packet counters\n"
-       IFNAME_STR)
+DEFUN(show_ipv6_ospf6_interface_traffic,
+      show_ipv6_ospf6_interface_traffic_cmd,
+      "show ipv6 ospf6 interface traffic [IFNAME] [json]",
+      SHOW_STR
+      IP6_STR
+      OSPF6_STR
+      INTERFACE_STR
+      "Protocol Packet counters\n"
+      IFNAME_STR
+      JSON_STR)
 {
        int idx_ifname = 0;
        int display_once = 0;
        char *intf_name = NULL;
        struct interface *ifp = NULL;
+       json_object *json = NULL;
+       bool uj = use_json(argc, argv);
+
+       if (uj)
+               json = json_object_new_object();
 
        if (argv_find(argv, argc, "IFNAME", &idx_ifname)) {
                intf_name = argv[idx_ifname]->arg;
                ifp = if_lookup_by_name(intf_name, VRF_DEFAULT);
-               if (ifp == NULL) {
-                       vty_out(vty, "No such Interface: %s\n", intf_name);
-                       return CMD_WARNING;
-               }
-               if (ifp->info == NULL) {
-                       vty_out(vty,
-                               "   OSPF not enabled on this interface %s\n",
-                               intf_name);
-                       return 0;
+               if (uj) {
+                       if (ifp == NULL) {
+                               json_object_string_add(json, "status",
+                                                      "No Such Interface");
+                               json_object_string_add(json, "interface",
+                                                      intf_name);
+                               vty_out(vty, "%s\n",
+                                       json_object_to_json_string_ext(
+                                               json, JSON_C_TO_STRING_PRETTY));
+                               json_object_free(json);
+                               return CMD_WARNING;
+                       }
+                       if (ifp->info == NULL) {
+                               json_object_string_add(
+                                       json, "status",
+                                       "OSPF not enabled on this interface");
+                               json_object_string_add(json, "interface",
+                                                      intf_name);
+                               vty_out(vty, "%s\n",
+                                       json_object_to_json_string_ext(
+                                               json, JSON_C_TO_STRING_PRETTY));
+                               json_object_free(json);
+                               return 0;
+                       }
+               } else {
+                       if (ifp == NULL) {
+                               vty_out(vty, "No such Interface: %s\n",
+                                       intf_name);
+                               return CMD_WARNING;
+                       }
+                       if (ifp->info == NULL) {
+                               vty_out(vty,
+                                       "   OSPF not enabled on this interface %s\n",
+                                       intf_name);
+                               return 0;
+                       }
                }
        }
 
-       ospf6_interface_show_traffic(vty, ifp, display_once);
+       ospf6_interface_show_traffic(vty, ifp, display_once, json, uj);
+
+       if (uj) {
+               vty_out(vty, "%s\n",
+                       json_object_to_json_string_ext(
+                               json, JSON_C_TO_STRING_PRETTY));
+               json_object_free(json);
+       }
 
 
        return CMD_SUCCESS;