Skip to content

Commit

Permalink
netconf monitoring UPDATE own get-schema callback
Browse files Browse the repository at this point in the history
For NACM to be checked.
Refs CESNET#1300
  • Loading branch information
michalvasko committed Nov 15, 2022
1 parent 45e8408 commit f77b18f
Show file tree
Hide file tree
Showing 3 changed files with 120 additions and 7 deletions.
11 changes: 6 additions & 5 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -350,10 +350,8 @@ np2srv_rpc_cb(struct lyd_node *rpc, struct nc_session *ncs)
NC_WD_MODE nc_wd;
int rc;

/* use default lnc2 callbacks when available */
if (!strcmp(LYD_NAME(rpc), "get-schema") && !strcmp(lyd_owner_module(rpc)->name, "ietf-netconf-monitoring")) {
return nc_clb_default_get_schema(rpc, ncs);
} else if (!strcmp(LYD_NAME(rpc), "close-session") && !strcmp(lyd_owner_module(rpc)->name, "ietf-netconf")) {
if (!strcmp(LYD_NAME(rpc), "close-session") && !strcmp(lyd_owner_module(rpc)->name, "ietf-netconf")) {
/* call close-session directly */
return nc_clb_default_close_session(rpc, ncs);
}

Expand Down Expand Up @@ -718,13 +716,16 @@ server_rpc_subscribe(void)
SR_RPC_SUBSCR("/ietf-netconf:lock", np2srv_rpc_un_lock_cb);
SR_RPC_SUBSCR("/ietf-netconf:unlock", np2srv_rpc_un_lock_cb);
SR_RPC_SUBSCR("/ietf-netconf:get", np2srv_rpc_get_cb);
/* keep close-session empty so that internal lnc2 callback is used */
/* close-session called directly */
SR_RPC_SUBSCR("/ietf-netconf:kill-session", np2srv_rpc_kill_cb);
SR_RPC_SUBSCR("/ietf-netconf:commit", np2srv_rpc_commit_cb);
SR_RPC_SUBSCR("/ietf-netconf:cancel-commit", np2srv_rpc_cancel_commit_cb);
SR_RPC_SUBSCR("/ietf-netconf:discard-changes", np2srv_rpc_discard_cb);
SR_RPC_SUBSCR("/ietf-netconf:validate", np2srv_rpc_validate_cb);

/* subscribe to get-schema */
SR_RPC_SUBSCR("/ietf-netconf-monitoring:get-schema", np2srv_rpc_getschema_cb);

/* subscribe to create-subscription */
SR_RPC_SUBSCR("/notifications:create-subscription", np2srv_rpc_subscribe_cb);

Expand Down
109 changes: 109 additions & 0 deletions src/netconf_monitoring.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@

#include "common.h"
#include "compat.h"
#include "err_netconf.h"
#include "log.h"

struct ncm stats;
Expand Down Expand Up @@ -398,3 +399,111 @@ np2srv_ncm_oper_cb(sr_session_ctx_t *session, uint32_t UNUSED(sub_id), const cha
lyd_free_tree(root);
return SR_ERR_INTERNAL;
}

int
np2srv_rpc_getschema_cb(sr_session_ctx_t *session, uint32_t UNUSED(sub_id), const char *UNUSED(op_path),
const struct lyd_node *input, sr_event_t event, uint32_t UNUSED(request_id), struct lyd_node *output,
void *UNUSED(private_data))
{
const char *identifier = NULL, *revision = NULL, *format = NULL;
const struct ly_ctx *ly_ctx = NULL;
int rc = SR_ERR_OK;
char *model_data = NULL;
struct ly_out *out;
const struct lys_module *module = NULL;
const struct lysp_submodule *submodule = NULL;
struct lyd_node *node;
LYS_OUTFORMAT outformat = 0;

if (np_ignore_rpc(session, event, &rc)) {
/* ignore in this case */
return rc;
}

/* identifier */
if (!lyd_find_path(input, "identifier", 0, &node)) {
identifier = lyd_get_value(node);
}

/* revision */
if (!lyd_find_path(input, "version", 0, &node)) {
revision = lyd_get_value(node);
if (!strlen(revision)) {
revision = NULL;
}
}

/* format */
if (!lyd_find_path(input, "format", 0, &node)) {
format = lyd_get_value(node);
}
VRB("Module \"%s@%s\" was requested.", identifier, revision ? revision : "<any>");

/* check revision */
if (revision && (strlen(revision) != 10) && strcmp(revision, "1.0")) {
np_err_invalid_value(session, "The requested version is not supported.", NULL);
rc = SR_ERR_INVAL_ARG;
goto cleanup;
}

ly_ctx = sr_acquire_context(np2srv.sr_conn);
if (revision) {
/* get specific module */
module = ly_ctx_get_module(ly_ctx, identifier, revision);
if (!module) {
submodule = ly_ctx_get_submodule(ly_ctx, identifier, revision);
}
} else {
/* try to get implemented, then latest module */
module = ly_ctx_get_module_implemented(ly_ctx, identifier);
if (!module) {
module = ly_ctx_get_module_latest(ly_ctx, identifier);
}
if (!module) {
submodule = ly_ctx_get_submodule_latest(ly_ctx, identifier);
}
}
if (!module && !submodule) {
np_err_invalid_value(session, "The requested module was not found.", NULL);
rc = SR_ERR_INVAL_ARG;
goto cleanup;
}

/* check format */
if (!format || !strcmp(format, "ietf-netconf-monitoring:yang")) {
outformat = LYS_OUT_YANG;
} else if (!strcmp(format, "ietf-netconf-monitoring:yin")) {
outformat = LYS_OUT_YIN;
} else {
np_err_invalid_value(session, "The requested format is not supported.", NULL);
rc = SR_ERR_INVAL_ARG;
goto cleanup;
}

/* print */
ly_out_new_memory(&model_data, 0, &out);
if (module) {
lys_print_module(out, module, outformat, 0, 0);
} else {
lys_print_submodule(out, submodule, outformat, 0, 0);
}
ly_out_free(out, NULL, 0);
if (!model_data) {
rc = SR_ERR_INTERNAL;
goto cleanup;
}

/* add output */
if (lyd_new_any(output, NULL, "data", model_data, 1, LYD_ANYDATA_STRING, 1, NULL)) {
rc = SR_ERR_LY;
goto cleanup;
}
model_data = NULL;

cleanup:
if (ly_ctx) {
sr_release_context(np2srv.sr_conn);
}
free(model_data);
return rc;
}
7 changes: 5 additions & 2 deletions src/netconf_monitoring.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
* @brief ietf-netconf-monitoring statistics and counters header
*
* @copyright
* Copyright (c) 2019 - 2021 Deutsche Telekom AG.
* Copyright (c) 2017 - 2021 CESNET, z.s.p.o.
* Copyright (c) 2019 - 2022 Deutsche Telekom AG.
* Copyright (c) 2017 - 2022 CESNET, z.s.p.o.
*
* This source code is licensed under BSD 3-Clause License (the "License").
* You may not use this file except in compliance with the License.
Expand Down Expand Up @@ -59,4 +59,7 @@ uint32_t ncm_session_get_notification(struct nc_session *session);
int np2srv_ncm_oper_cb(sr_session_ctx_t *session, uint32_t sub_id, const char *module_name, const char *path,
const char *request_xpath, uint32_t request_id, struct lyd_node **parent, void *private_data);

int np2srv_rpc_getschema_cb(sr_session_ctx_t *session, uint32_t sub_id, const char *op_path, const struct lyd_node *input,
sr_event_t event, uint32_t request_id, struct lyd_node *output, void *private_data);

#endif /* NP2SRV_NETCONF_MONITORING_H_ */

0 comments on commit f77b18f

Please sign in to comment.