]> git.puffer.fish Git - matthieu/frr.git/commitdiff
bgpd: Add JSON output for `show rpki cache-connection`
authorDonatas Abraitis <donatas@opensourcerouting.org>
Mon, 23 May 2022 16:40:45 +0000 (19:40 +0300)
committerDonatas Abraitis <donatas@opensourcerouting.org>
Mon, 23 May 2022 21:06:07 +0000 (00:06 +0300)
```
spine1-debian-11# sh rpki cache-connection
Connected to group 1
rpki tcp cache 192.168.10.17 8283 pref 1 (connected)
rpki tcp cache 192.168.10.17 8282 pref 2
spine1-debian-11# sh rpki cache-connection json
{
  "connectedGroup":1,
  "connections":[
    {
      "mode":"tcp",
      "host":"192.168.10.17",
      "port":"8283",
      "preference":1,
      "state":"connected"
    },
    {
      "mode":"tcp",
      "host":"192.168.10.17",
      "port":"8282",
      "preference":2,
      "state":"disconnected"
    }
  ]
}
```

Signed-off-by: Donatas Abraitis <donatas@opensourcerouting.org>
bgpd/bgp_rpki.c
doc/user/rpki.rst

index 97e94a97ceb6fb03721697e7dde2b9e7f42f92f7..b6f59d6712716234ed0968a9bbae39ae5972d445 100644 (file)
@@ -1312,28 +1312,51 @@ DEFUN (show_rpki_cache_server,
        return CMD_SUCCESS;
 }
 
-DEFUN (show_rpki_cache_connection,
+DEFPY (show_rpki_cache_connection,
        show_rpki_cache_connection_cmd,
-       "show rpki cache-connection",
+       "show rpki cache-connection [json$uj]",
        SHOW_STR
        RPKI_OUTPUT_STRING
-       "Show to which RPKI Cache Servers we have a connection\n")
+       "Show to which RPKI Cache Servers we have a connection\n"
+       JSON_STR)
 {
+       struct json_object *json = NULL;
+       struct json_object *json_conn = NULL;
+       struct json_object *json_conns = NULL;
+       struct listnode *cache_node;
+       struct cache *cache;
+       struct rtr_mgr_group *group;
+
+       if (uj)
+               json = json_object_new_object();
+
        if (!is_synchronized()) {
-               vty_out(vty, "No connection to RPKI cache server.\n");
+               if (!json)
+                       vty_out(vty, "No connection to RPKI cache server.\n");
+               else
+                       vty_json(vty, json);
 
                return CMD_SUCCESS;
        }
 
-       struct listnode *cache_node;
-       struct cache *cache;
-       struct rtr_mgr_group *group = get_connected_group();
-
+       group = get_connected_group();
        if (!group) {
-               vty_out(vty, "Cannot find a connected group.\n");
+               if (!json)
+                       vty_out(vty, "Cannot find a connected group.\n");
+               else
+                       vty_json(vty, json);
+
                return CMD_SUCCESS;
        }
-       vty_out(vty, "Connected to group %d\n", group->preference);
+
+       if (!json) {
+               vty_out(vty, "Connected to group %d\n", group->preference);
+       } else {
+               json_conns = json_object_new_array();
+               json_object_int_add(json, "connectedGroup", group->preference);
+               json_object_object_add(json, "connections", json_conns);
+       }
+
        for (ALL_LIST_ELEMENTS_RO(cache_list, cache_node, cache)) {
                struct tr_tcp_config *tcp_config;
 #if defined(FOUND_SSH)
@@ -1342,22 +1365,66 @@ DEFUN (show_rpki_cache_connection,
                switch (cache->type) {
                case TCP:
                        tcp_config = cache->tr_config.tcp_config;
-                       vty_out(vty, "rpki tcp cache %s %s pref %hhu%s\n",
-                               tcp_config->host, tcp_config->port,
-                               cache->preference,
-                               cache->rtr_socket->state == RTR_ESTABLISHED
-                                       ? " (connected)"
-                                       : "");
+
+                       if (!json) {
+                               vty_out(vty,
+                                       "rpki tcp cache %s %s pref %hhu%s\n",
+                                       tcp_config->host, tcp_config->port,
+                                       cache->preference,
+                                       cache->rtr_socket->state ==
+                                                       RTR_ESTABLISHED
+                                               ? " (connected)"
+                                               : "");
+                       } else {
+                               json_conn = json_object_new_object();
+                               json_object_string_add(json_conn, "mode",
+                                                      "tcp");
+                               json_object_string_add(json_conn, "host",
+                                                      tcp_config->host);
+                               json_object_string_add(json_conn, "port",
+                                                      tcp_config->port);
+                               json_object_int_add(json_conn, "preference",
+                                                   cache->preference);
+                               json_object_string_add(
+                                       json_conn, "state",
+                                       cache->rtr_socket->state ==
+                                                       RTR_ESTABLISHED
+                                               ? "connected"
+                                               : "disconnected");
+                               json_object_array_add(json_conns, json_conn);
+                       }
                        break;
 #if defined(FOUND_SSH)
                case SSH:
                        ssh_config = cache->tr_config.ssh_config;
-                       vty_out(vty, "rpki ssh cache %s %u pref %hhu%s\n",
-                               ssh_config->host, ssh_config->port,
-                               cache->preference,
-                               cache->rtr_socket->state == RTR_ESTABLISHED
-                                       ? " (connected)"
-                                       : "");
+
+                       if (!json) {
+                               vty_out(vty,
+                                       "rpki ssh cache %s %u pref %hhu%s\n",
+                                       ssh_config->host, ssh_config->port,
+                                       cache->preference,
+                                       cache->rtr_socket->state ==
+                                                       RTR_ESTABLISHED
+                                               ? " (connected)"
+                                               : "");
+                       } else {
+                               json_conn = json_object_new_object();
+                               json_object_string_add(json_conn, "mode",
+                                                      "ssh");
+                               json_object_string_add(json_conn, "host",
+                                                      ssh_config->host);
+                               json_object_string_add(json_conn, "port",
+                                                      ssh_config->port);
+                               json_object_int_add(json_conn, "preference",
+                                                   cache->preference);
+                               json_object_string_add(
+                                       json_conn, "state",
+                                       cache->rtr_socket->state ==
+                                                       RTR_ESTABLISHED
+                                               ? "connected"
+                                               : "disconnected");
+                               json_object_array_add(json_conns, json_conn);
+                       }
                        break;
 #endif
                default:
@@ -1365,6 +1432,9 @@ DEFUN (show_rpki_cache_connection,
                }
        }
 
+       if (json)
+               vty_json(vty, json);
+
        return CMD_SUCCESS;
 }
 
index c2f8ad12680926997d165aa521ef9833374653bf..f03d8233f78ac6e384b205656e8ca9ca0f4ac684 100644 (file)
@@ -216,7 +216,7 @@ Displaying RPKI
    received from the cache servers and stored in the router. Based on this data,
    the router validates BGP Updates.
 
-.. clicmd:: show rpki cache-connection
+.. clicmd:: show rpki cache-connection [json]
 
    Display all configured cache servers, whether active or not.