Skip to content

Commit

Permalink
lib, mgmtd: add ability to request the exact node in get-data request
Browse files Browse the repository at this point in the history
RESTCONF expects to receive the exact node as a result, not the whole
data tree.

Signed-off-by: Igor Ryzhov <[email protected]>
  • Loading branch information
idryzhov committed Jan 13, 2024
1 parent a5e30a4 commit 202cb53
Show file tree
Hide file tree
Showing 9 changed files with 29 additions and 12 deletions.
3 changes: 2 additions & 1 deletion lib/mgmt_fe_client.c
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,7 @@ int mgmt_fe_send_regnotify_req(struct mgmt_fe_client *client,
int mgmt_fe_send_get_data_req(struct mgmt_fe_client *client, uint64_t session_id,
uint64_t req_id, LYD_FORMAT result_type,
enum mgmt_msg_get_data_content content,
const char *xpath)
bool exact, const char *xpath)
{
struct mgmt_msg_get_data *msg;
size_t xplen = strlen(xpath);
Expand All @@ -324,6 +324,7 @@ int mgmt_fe_send_get_data_req(struct mgmt_fe_client *client, uint64_t session_id
msg->code = MGMT_MSG_CODE_GET_DATA;
msg->result_type = result_type;
msg->content = content;
msg->exact = exact;
strlcpy(msg->xpath, xpath, xplen + 1);

MGMTD_FE_CLIENT_DBG("Sending GET_DATA_REQ session-id %" PRIu64
Expand Down
5 changes: 4 additions & 1 deletion lib/mgmt_fe_client.h
Original file line number Diff line number Diff line change
Expand Up @@ -385,6 +385,9 @@ extern int mgmt_fe_send_regnotify_req(struct mgmt_fe_client *client,
* content
* The content type of the result.
*
* exact
* Return the exact node requested instead of the full tree.
*
* xpath
* the xpath to get.
*
Expand All @@ -395,7 +398,7 @@ extern int mgmt_fe_send_get_data_req(struct mgmt_fe_client *client,
uint64_t session_id, uint64_t req_id,
LYD_FORMAT result_type,
enum mgmt_msg_get_data_content content,
const char *xpath);
bool exact, const char *xpath);

/*
* Destroy library and cleanup everything.
Expand Down
4 changes: 3 additions & 1 deletion lib/mgmt_msg_native.h
Original file line number Diff line number Diff line change
Expand Up @@ -250,13 +250,15 @@ enum mgmt_msg_get_data_content {
*
* @result_type: ``LYD_FORMAT`` for the returned result.
* @content: ``enum mgmt_msg_get_data_content``.
* @exact: return the exact node requested instead of the full tree.
* @xpath: the query for the data to return.
*/
struct mgmt_msg_get_data {
struct mgmt_msg_header;
uint8_t result_type;
uint8_t content;
uint8_t resv2[6];
uint8_t exact;
uint8_t resv2[5];

alignas(8) char xpath[];
};
Expand Down
4 changes: 2 additions & 2 deletions lib/vty.c
Original file line number Diff line number Diff line change
Expand Up @@ -4107,15 +4107,15 @@ int vty_mgmt_send_get_req(struct vty *vty, bool is_config,

int vty_mgmt_send_get_data_req(struct vty *vty, LYD_FORMAT result_type,
enum mgmt_msg_get_data_content content,
const char *xpath)
bool exact, const char *xpath)
{
LYD_FORMAT intern_format = result_type;

vty->mgmt_req_id++;

if (mgmt_fe_send_get_data_req(mgmt_fe_client, vty->mgmt_session_id,
vty->mgmt_req_id, intern_format, content,
xpath)) {
exact, xpath)) {
zlog_err("Failed to send GET-DATA to MGMTD session-id: %" PRIu64
" req-id %" PRIu64 ".",
vty->mgmt_session_id, vty->mgmt_req_id);
Expand Down
2 changes: 1 addition & 1 deletion lib/vty.h
Original file line number Diff line number Diff line change
Expand Up @@ -422,7 +422,7 @@ extern int vty_mgmt_send_get_req(struct vty *vty, bool is_config,
const char **xpath_list, int num_req);
extern int vty_mgmt_send_get_data_req(struct vty *vty, LYD_FORMAT result_type,
enum mgmt_msg_get_data_content content,
const char *xpath);
bool exact, const char *xpath);
extern int vty_mgmt_send_lockds_req(struct vty *vty, Mgmtd__DatastoreId ds_id,
bool lock, bool scok);
extern void vty_mgmt_resume_response(struct vty *vty, int ret);
Expand Down
2 changes: 1 addition & 1 deletion mgmtd/mgmt_fe_adapter.c
Original file line number Diff line number Diff line change
Expand Up @@ -1208,7 +1208,7 @@ static void fe_adapter_handle_get_data(struct mgmt_fe_session_ctx *session,
/* Create a GET-TREE request under the transaction */
ret = mgmt_txn_send_get_tree_oper(session->txn_id, req_id, clients,
msg->result_type, msg->content,
simple_xpath, msg->xpath);
msg->exact, simple_xpath, msg->xpath);
if (ret) {
/* destroy the just created txn */
mgmt_destroy_txn(&session->txn_id);
Expand Down
12 changes: 10 additions & 2 deletions mgmtd/mgmt_txn.c
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@ struct txn_req_get_tree {
uint64_t recv_clients; /* Bitmask of clients recv reply from */
int32_t partial_error; /* an error while gather results */
uint8_t result_type; /* LYD_FORMAT for results */
uint8_t exact; /* if exact node is requested */
uint8_t simple_xpath; /* if xpath is simple */
struct lyd_node *client_results; /* result tree from clients */
};
Expand Down Expand Up @@ -1258,6 +1259,7 @@ static int txn_get_tree_data_done(struct mgmt_txn_ctx *txn,
{
struct txn_req_get_tree *get_tree = txn_req->req.get_tree;
uint64_t req_id = txn_req->req_id;
struct lyd_node *result;
int ret = NB_OK;

/* cancel timer and send reply onward */
Expand All @@ -1272,12 +1274,17 @@ static int txn_get_tree_data_done(struct mgmt_txn_ctx *txn,
ret = NB_ERR;
}

result = get_tree->client_results;

if (ret == NB_OK && result && get_tree->exact)
result = yang_dnode_get(result, get_tree->xpath);

if (ret == NB_OK)
ret = mgmt_fe_adapter_send_tree_data(txn->session_id,
txn->txn_id,
txn_req->req_id,
get_tree->result_type,
get_tree->client_results,
result,
get_tree->partial_error,
false);

Expand Down Expand Up @@ -2365,7 +2372,7 @@ int mgmt_txn_send_get_req(uint64_t txn_id, uint64_t req_id,
int mgmt_txn_send_get_tree_oper(uint64_t txn_id, uint64_t req_id,
uint64_t clients, LYD_FORMAT result_type,
enum mgmt_msg_get_data_content content,
bool simple_xpath, const char *xpath)
bool exact, bool simple_xpath, const char *xpath)
{
struct mgmt_msg_get_tree *msg;
struct mgmt_txn_ctx *txn;
Expand All @@ -2383,6 +2390,7 @@ int mgmt_txn_send_get_tree_oper(uint64_t txn_id, uint64_t req_id,
txn_req = mgmt_txn_req_alloc(txn, req_id, MGMTD_TXN_PROC_GETTREE);
get_tree = txn_req->req.get_tree;
get_tree->result_type = result_type;
get_tree->exact = exact;
get_tree->simple_xpath = simple_xpath;
get_tree->xpath = XSTRDUP(MTYPE_MGMTD_XPATH, xpath);

Expand Down
4 changes: 3 additions & 1 deletion mgmtd/mgmt_txn.h
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,7 @@ extern int mgmt_txn_send_get_req(uint64_t txn_id, uint64_t req_id,
* clients: Bitmask of clients to send get-tree to.
* result_type: LYD_FORMAT result format.
* content: The content type to get.
* exact: request the exact node instead of the full tree.
* simple_xpath: true if xpath is simple (only key predicates).
* xpath: The xpath to get the tree from.
*
Expand All @@ -213,7 +214,8 @@ extern int mgmt_txn_send_get_req(uint64_t txn_id, uint64_t req_id,
extern int mgmt_txn_send_get_tree_oper(uint64_t txn_id, uint64_t req_id,
uint64_t clients, LYD_FORMAT result_type,
enum mgmt_msg_get_data_content content,
bool simple_xpath, const char *xpath);
bool exact, bool simple_xpath,
const char *xpath);

/*
* Notifiy backend adapter on connection.
Expand Down
5 changes: 3 additions & 2 deletions mgmtd/mgmt_vty.c
Original file line number Diff line number Diff line change
Expand Up @@ -251,13 +251,14 @@ DEFPY(show_mgmt_get_config, show_mgmt_get_config_cmd,
}

DEFPY(show_mgmt_get_data, show_mgmt_get_data_cmd,
"show mgmt get-data WORD$path [with-config|only-config]$cont [json|xml]$fmt",
"show mgmt get-data WORD$path [with-config|only-config]$cont [exact]$exact [json|xml]$fmt",
SHOW_STR
MGMTD_STR
"Get a data from the operational datastore\n"
"XPath expression specifying the YANG data root\n"
"Include \"config true\" data\n"
"Get only \"config true\" data\n"
"Get exact node instead of the whole data tree\n"
"JSON output format\n"
"XML output format\n")
{
Expand All @@ -282,7 +283,7 @@ DEFPY(show_mgmt_get_data, show_mgmt_get_data_cmd,
path = xpath;
}

vty_mgmt_send_get_data_req(vty, format, content, path);
vty_mgmt_send_get_data_req(vty, format, content, exact ? true : false, path);

if (xpath)
XFREE(MTYPE_TMP, xpath);
Expand Down

0 comments on commit 202cb53

Please sign in to comment.