]> git.puffer.fish Git - mirror/frr.git/commitdiff
tests: add test for NB RPC callback
authorIgor Ryzhov <iryzhov@nfware.com>
Mon, 18 Mar 2024 17:24:40 +0000 (19:24 +0200)
committerIgor Ryzhov <iryzhov@nfware.com>
Mon, 22 Apr 2024 13:36:22 +0000 (16:36 +0300)
Signed-off-by: Igor Ryzhov <iryzhov@nfware.com>
lib/yang.c
lib/yang.h
tests/lib/northbound/test_oper_data.c
tests/lib/northbound/test_oper_data.in
tests/lib/northbound/test_oper_data.refout
yang/frr-test-module.yang

index 013a7628424ace4d6305233ec390ed0889bb56be..f506d8bf2336f39c03aa2421f8d2fc452049376e 100644 (file)
@@ -653,6 +653,16 @@ void yang_dnode_free(struct lyd_node *dnode)
        lyd_free_all(dnode);
 }
 
+void yang_dnode_rpc_output_add(struct lyd_node *output, const char *xpath,
+                              const char *value)
+{
+       LY_ERR err;
+
+       err = lyd_new_path(output, ly_native_ctx, xpath, value,
+                          LYD_NEW_PATH_OUTPUT | LYD_NEW_PATH_UPDATE, NULL);
+       assert(err == LY_SUCCESS);
+}
+
 struct yang_data *yang_data_new(const char *xpath, const char *value)
 {
        struct yang_data *data;
index 25703b18790360a5033c6e8085c91b3c05ba2500..1903079d1c15e2ccf640898233af078f33f37ac8 100644 (file)
@@ -535,6 +535,21 @@ extern struct lyd_node *yang_dnode_dup(const struct lyd_node *dnode);
  */
 extern void yang_dnode_free(struct lyd_node *dnode);
 
+/*
+ * Add a libyang data node to an RPC/action output container.
+ *
+ * output
+ *    RPC/action output container.
+ *
+ * xpath
+ *    XPath of the data node to add, relative to the output container.
+ *
+ * value
+ *    String representing the value of the data node.
+ */
+extern void yang_dnode_rpc_output_add(struct lyd_node *output,
+                                     const char *xpath, const char *value);
+
 /*
  * Create a new yang_data structure.
  *
index 8f7e7c5f8c7170b3a668bc0435a77445839347e9..321f158668e20cd13c0d85c6a2e6f517d255b02f 100644 (file)
@@ -14,6 +14,7 @@
 #include "lib_vty.h"
 #include "log.h"
 #include "northbound.h"
+#include "northbound_cli.h"
 
 static struct event_loop *master;
 
@@ -200,6 +201,19 @@ static struct yang_data *frr_test_module_vrfs_vrf_routes_route_active_get_elem(
        return NULL;
 }
 
+/*
+ * XPath: /frr-test-module:frr-test-module/vrfs/vrf/ping
+ */
+static int frr_test_module_vrfs_vrf_ping(struct nb_cb_rpc_args *args)
+{
+       const char *vrf = yang_dnode_get_string(args->input, "../name");
+       const char *data = yang_dnode_get_string(args->input, "data");
+
+       yang_dnode_rpc_output_add(args->output, "vrf", vrf);
+       yang_dnode_rpc_output_add(args->output, "data-out", data);
+
+       return NB_OK;
+}
 
 /*
  * XPath: /frr-test-module:frr-test-module/c1value
@@ -262,6 +276,10 @@ const struct frr_yang_module_info frr_test_module_info = {
                        .xpath = "/frr-test-module:frr-test-module/vrfs/vrf/routes/route/active",
                        .cbs.get_elem = frr_test_module_vrfs_vrf_routes_route_active_get_elem,
                },
+               {
+                       .xpath = "/frr-test-module:frr-test-module/vrfs/vrf/ping",
+                       .cbs.rpc = frr_test_module_vrfs_vrf_ping,
+               },
                {
                        .xpath = "/frr-test-module:frr-test-module/c1value",
                        .cbs.get_elem = frr_test_module_c1value_get_elem,
@@ -277,6 +295,33 @@ const struct frr_yang_module_info frr_test_module_info = {
 };
 /* clang-format on */
 
+DEFUN(test_rpc, test_rpc_cmd, "test rpc",
+      "Test\n"
+      "RPC\n")
+{
+       struct lyd_node *output = NULL;
+       char xpath[XPATH_MAXLEN];
+       int ret;
+
+       snprintf(xpath, sizeof(xpath),
+                "/frr-test-module:frr-test-module/vrfs/vrf[name='testname']/ping");
+
+       nb_cli_rpc_enqueue(vty, "data", "testdata");
+
+       ret = nb_cli_rpc(vty, xpath, &output);
+       if (ret != CMD_SUCCESS) {
+               vty_out(vty, "RPC failed\n");
+               return ret;
+       }
+
+       vty_out(vty, "vrf %s data %s\n", yang_dnode_get_string(output, "vrf"),
+               yang_dnode_get_string(output, "data-out"));
+
+       yang_dnode_free(output);
+
+       return CMD_SUCCESS;
+}
+
 static const struct frr_yang_module_info *const modules[] = {
        &frr_test_module_info,
 };
@@ -416,6 +461,8 @@ int main(int argc, char **argv)
        lib_cmd_init();
        nb_init(master, modules, array_size(modules), false);
 
+       install_element(ENABLE_NODE, &test_rpc_cmd);
+
        /* Create artificial data. */
        create_data(num_vrfs, num_interfaces, num_routes);
 
index a6c4f874f500d535d319006b79de570a66cfc02b..f7c44cad315458c6230129aac869e782952decd8 100644 (file)
@@ -1 +1,2 @@
 show yang operational-data /frr-test-module:frr-test-module
+test rpc
index aa930fe127bea42c5dd723588acca2d00f4e06fe..7c565641431c8040093f6763ae1c7d2cc9e7672b 100644 (file)
@@ -119,5 +119,7 @@ test# show yang operational-data /frr-test-module:frr-test-module
     }\r
   }\r
 }\r
+test# test rpc
+vrf testname data testdata
 test# 
 end.
index 6cc60e866533fdd7fcbc9c328b2735cfbe116670..dcf204a9565612814ce1d56edece46b953ef4e4f 100644 (file)
@@ -80,6 +80,23 @@ module frr-test-module {
             }
           }
         }
+        action ping {
+          input {
+            leaf data {
+              type string;
+            }
+          }
+          output {
+            leaf vrf {
+              type string;
+            }
+            // can't use the same name in input and output
+            // because of a bug in libyang < 2.1.148
+            leaf data-out {
+              type string;
+            }
+          }
+        }
       }
     }
     choice achoice {