void add_nacm_rule_list(test_nacm_cfg_t *nacm_config, const char *name, ... ) { struct lyd_node *node = NULL; char xpath[PATH_MAX] = { 0, }; va_list va; const char *group = NULL; snprintf(xpath, PATH_MAX, "/ietf-netconf-acm:nacm/rule-list[name='%s']/name", name); node = lyd_new_path(nacm_config->root, nacm_config->ly_ctx, xpath, (void *)name, 0, 0); assert_non_null(node); if (NULL == nacm_config->root) { nacm_config->root = node; } snprintf(xpath, PATH_MAX, "/ietf-netconf-acm:nacm/rule-list[name='%s']/group", name); va_start(va, name); do { group = va_arg(va, const char *); if (NULL != group) { node = lyd_new_path(nacm_config->root, nacm_config->ly_ctx, xpath, (void *)group, 0, 0); assert_non_null(node); } } while (group); va_end(va); }
static void test_df2(void **state) { struct state *st = (*state); struct lyd_node *node; const char *xml = "<df xmlns=\"urn:libyang:tests:defaults\">" "<list><name>a</name><value>42</value></list>" "<list><name>b</name><value>1</value></list>" "<foo>42</foo><b1_1>42</b1_1>" "</df><hidden xmlns=\"urn:libyang:tests:defaults\">" "<foo>42</foo><baz>42</baz></hidden>"; st->dt = lyd_new(NULL, st->mod, "df"); assert_ptr_not_equal(st->dt, NULL); /* lists */ assert_ptr_not_equal(lyd_new_path(st->dt, NULL, "/defaults:df/defaults:list[name='a']", NULL, 0), NULL); assert_ptr_not_equal((node = lyd_new_path(st->dt, NULL, "/defaults:df/defaults:list[name='b']", NULL, 0)), NULL); assert_ptr_not_equal(lyd_new_leaf(node, NULL, "value", "1"), NULL); assert_int_equal(lyd_validate(&(st->dt), LYD_OPT_CONFIG), 0); assert_int_equal(lyd_wd_add(NULL, &(st->dt), LYD_OPT_CONFIG | LYD_WD_ALL), 0); assert_int_equal(lyd_print_mem(&(st->xml), st->dt, LYD_XML, LYP_WITHSIBLINGS), 0); assert_ptr_not_equal(st->xml, NULL); assert_string_equal(st->xml, xml); }
static void test_dependency_circular(void **state) { struct state *st = (struct state *)*state; struct lyd_node *node; /* schema */ st->mod = lys_parse_path(st->ctx, TESTS_DIR"/data/files/when-circdepend.yin", LYS_IN_YIN); assert_ptr_not_equal(st->mod, NULL); st->dt = lyd_new_path(NULL, st->ctx, "/when-circdepend:top/a", "val_a", 0, 0); assert_ptr_not_equal(st->dt, NULL); node = lyd_new_path(st->dt, st->ctx, "/when-circdepend:top/b", "val_b", 0, 0); assert_ptr_not_equal(node, NULL); node = lyd_new_path(st->dt, st->ctx, "/when-circdepend:top/d", "1", 0, 0); assert_ptr_not_equal(node, NULL); node = lyd_new_path(st->dt, st->ctx, "/when-circdepend:top/d", "2", 0, 0); assert_ptr_not_equal(node, NULL); node = lyd_new_path(st->dt, st->ctx, "/when-circdepend:top/e", "val_e", 0, 0); assert_ptr_not_equal(node, NULL); assert_int_equal(lyd_validate(&(st->dt), LYD_OPT_CONFIG, NULL), 1); assert_int_equal(ly_errno, LY_EVALID); assert_int_equal(ly_vecode, LYVE_INWHEN); }
void add_nacm_rule(test_nacm_cfg_t *nacm_config, const char *rule_list, const char *name, const char *module, nacm_rule_type_t type, const char *data, const char *access, const char *action, const char *comment) { struct lyd_node *node = NULL; char xpath[PATH_MAX] = { 0, }; snprintf(xpath, PATH_MAX, "/ietf-netconf-acm:nacm/rule-list[name='%s']/rule[name='%s']/name", rule_list, name); node = lyd_new_path(nacm_config->root, nacm_config->ly_ctx, xpath, (void *)name, 0, 0); assert_non_null(node); if (NULL == nacm_config->root) { nacm_config->root = node; } if (NULL != module) { snprintf(xpath, PATH_MAX, "/ietf-netconf-acm:nacm/rule-list[name='%s']/rule[name='%s']/module-name", rule_list, name); node = lyd_new_path(nacm_config->root, nacm_config->ly_ctx, xpath, (void *)module, 0, 0); assert_non_null(node); } if (NULL != data) { switch (type) { case NACM_RULE_RPC: snprintf(xpath, PATH_MAX, "/ietf-netconf-acm:nacm/rule-list[name='%s']/rule[name='%s']/rpc-name", rule_list, name); node = lyd_new_path(nacm_config->root, nacm_config->ly_ctx, xpath, (void *)data, 0, 0); assert_non_null(node); break; case NACM_RULE_NOTIF: snprintf(xpath, PATH_MAX, "/ietf-netconf-acm:nacm/rule-list[name='%s']/rule[name='%s']/notification-name", rule_list, name); node = lyd_new_path(nacm_config->root, nacm_config->ly_ctx, xpath, (void *)data, 0, 0); assert_non_null(node); break; case NACM_RULE_DATA: snprintf(xpath, PATH_MAX, "/ietf-netconf-acm:nacm/rule-list[name='%s']/rule[name='%s']/path", rule_list, name); node = lyd_new_path(nacm_config->root, nacm_config->ly_ctx, xpath, (void *)data, 0, 0); assert_non_null(node); break; default: break; } } if (NULL != access) { snprintf(xpath, PATH_MAX, "/ietf-netconf-acm:nacm/rule-list[name='%s']/rule[name='%s']/access-operations", rule_list, name); node = lyd_new_path(nacm_config->root, nacm_config->ly_ctx, xpath, (void *)access, 0, 0); assert_non_null(node); } if (NULL != action) { snprintf(xpath, PATH_MAX, "/ietf-netconf-acm:nacm/rule-list[name='%s']/rule[name='%s']/action", rule_list, name); node = lyd_new_path(nacm_config->root, nacm_config->ly_ctx, xpath, (void *)action, 0, 0); assert_non_null(node); } if (NULL != comment) { snprintf(xpath, PATH_MAX, "/ietf-netconf-acm:nacm/rule-list[name='%s']/rule[name='%s']/comment", rule_list, name); node = lyd_new_path(nacm_config->root, nacm_config->ly_ctx, xpath, (void *)comment, 0, 0); assert_non_null(node); } }
void add_nacm_user(test_nacm_cfg_t *nacm_config, const char *user, const char *group) { struct lyd_node *node = NULL; char xpath[PATH_MAX] = { 0, }; if (NULL == user) { snprintf(xpath, PATH_MAX, "/ietf-netconf-acm:nacm/groups/group[name='%s']/name", group); node = lyd_new_path(nacm_config->root, nacm_config->ly_ctx, xpath, (void *)group, 0, 0); assert_non_null(node); } else { snprintf(xpath, PATH_MAX, "/ietf-netconf-acm:nacm/groups/group[name='%s']/user-name", group); node = lyd_new_path(nacm_config->root, nacm_config->ly_ctx, xpath, (void *)user, 0, 0); assert_non_null(node); } if (NULL == nacm_config->root) { nacm_config->root = node; } }
static void test_unlink_augment(void **state) { struct state *st = (struct state *)*state; struct lyd_node *node; /* schema */ st->mod = lys_parse_path(st->ctx, TESTS_DIR"/data/files/when-unlink.yin", LYS_IN_YIN); assert_ptr_not_equal(st->mod, NULL); st->dt = lyd_new_path(NULL, st->ctx, "/when-unlink:top/d", "1", 0, 0); assert_ptr_not_equal(st->dt, NULL); node = lyd_new_path(st->dt, st->ctx, "/when-unlink:top/d", "2", 0, 0); assert_ptr_not_equal(node, NULL); assert_int_equal(lyd_validate(&(st->dt), LYD_OPT_CONFIG, NULL), 0); lyd_print_mem(&(st->xml), st->dt, LYD_XML, LYP_WITHSIBLINGS); assert_string_equal(st->xml, "<top xmlns=\"urn:libyang:tests:when-unlink\"><d>1</d><d>2</d></top>"); }
void set_nacm_write_dflt(test_nacm_cfg_t *nacm_config, const char *action) { struct lyd_node *node = NULL; node = lyd_new_path(nacm_config->root, nacm_config->ly_ctx, "/ietf-netconf-acm:nacm/write-default", (void *)action, 0, LYD_PATH_OPT_UPDATE); assert_non_null(node); if (NULL == nacm_config->root) { nacm_config->root = node; } }
void enable_nacm_config(test_nacm_cfg_t* nacm_config, bool enable) { struct lyd_node *node = NULL; node = lyd_new_path(nacm_config->root, nacm_config->ly_ctx, "/ietf-netconf-acm:nacm/enable-nacm", enable ? "true" : "false", 0, LYD_PATH_OPT_UPDATE); assert_non_null(node); if (NULL == nacm_config->root) { nacm_config->root = node; } }
int __wrap_sr_set_item(sr_session_ctx_t *session, const char *xpath, const sr_val_t *value, const sr_edit_options_t opts) { (void)session; char buf[128]; int opt = 0; if (opts & SR_EDIT_NON_RECURSIVE) { opt |= LYD_PATH_OPT_NOPARENT; } if (!(opts & SR_EDIT_STRICT)) { opt |= LYD_PATH_OPT_UPDATE; } switch (value->type) { case SR_LIST_T: case SR_CONTAINER_T: case SR_CONTAINER_PRESENCE_T: case SR_LEAF_EMPTY_T: ly_errno = LY_SUCCESS; lyd_new_path(data, np2srv.ly_ctx, xpath, NULL, 0, opt); if ((ly_errno == LY_EVALID) && (ly_vecode == LYVE_PATH_EXISTS)) { return SR_ERR_DATA_EXISTS; } assert_int_equal(ly_errno, LY_SUCCESS); break; default: ly_errno = LY_SUCCESS; lyd_new_path(data, np2srv.ly_ctx, xpath, op_get_srval(np2srv.ly_ctx, (sr_val_t *)value, buf), 0, opt); if ((ly_errno == LY_EVALID) && (ly_vecode == LYVE_PATH_EXISTS)) { return SR_ERR_DATA_EXISTS; } assert_int_equal(ly_errno, LY_SUCCESS); break; } return SR_ERR_OK; }
static void test_dummy(void **state) { struct state *st = (struct state *)*state; /* schema */ st->mod = lys_parse_path(st->ctx, TESTS_DIR"/data/files/when-dummy.yin", LYS_IN_YIN); assert_ptr_not_equal(st->mod, NULL); st->dt = lyd_new_path(NULL, st->ctx, "/when-dummy:c", "value", 0, 0); assert_ptr_not_equal(st->dt, NULL); assert_int_equal(lyd_validate(&(st->dt), LYD_OPT_CONFIG, NULL), 1); assert_int_equal(ly_errno, LY_EVALID); assert_int_equal(ly_vecode, LYVE_XPATH_DUMMY); }
static void test_unlink_all(void **state) { struct state *st = (struct state *)*state; /* schema */ st->mod = lys_parse_path(st->ctx, TESTS_DIR"/data/files/when-unlinkall.yin", LYS_IN_YIN); assert_ptr_not_equal(st->mod, NULL); st->dt = lyd_new_path(NULL, st->ctx, "/when-unlinkall:a", "val_a", 0, 0); assert_ptr_not_equal(st->dt, NULL); assert_int_equal(lyd_validate(&(st->dt), LYD_OPT_CONFIG, NULL), 0); lyd_print_mem(&(st->xml), st->dt, LYD_XML, LYP_WITHSIBLINGS); assert_string_equal(st->xml, "<a xmlns=\"urn:libyang:tests:when-unlinkall\">val_a</a>"); }
void createDataTreeExampleModule() { struct ly_ctx *ctx = NULL; struct lyd_node *root = NULL; ctx = ly_ctx_new(TEST_SCHEMA_SEARCH_DIR); assert_non_null(ctx); const struct lys_module *module = ly_ctx_load_module(ctx, "example-module", NULL); assert_non_null(module); #define XPATH "/example-module:container/list[key1='key1'][key2='key2']/leaf" root = lyd_new_path(NULL, ctx, XPATH, "Leaf value", 0); assert_int_equal(0, lyd_validate(&root, LYD_OPT_STRICT | LYD_OPT_CONFIG)); assert_int_equal(SR_ERR_OK, sr_save_data_tree_file(EXAMPLE_MODULE_DATA_FILE_NAME, root)); lyd_free_withsiblings(root); ly_ctx_destroy(ctx, NULL); }
/** * @brief Get complete libyang data tree of a specified module from sysrepo. */ static int srcfg_get_module_data(struct ly_ctx *ly_ctx, const char *module_name, struct lyd_node **data_tree) { int rc = SR_ERR_OK, ret = 0; sr_val_t *value = NULL; sr_val_iter_t *iter = NULL; struct lyd_node *node = NULL; const struct lys_node *schema = NULL; char query[PATH_MAX] = { 0, }; char *string_val = NULL; snprintf(query, PATH_MAX, "/%s:*//.", module_name); rc = sr_get_items_iter(srcfg_session, query, &iter); if (SR_ERR_OK != rc) { SR_LOG_ERR("Error by sr_get_items_iter: %s", sr_strerror(rc)); goto cleanup; } *data_tree = NULL; ly_errno = LY_SUCCESS; ly_diminish_errors = true; while (SR_ERR_OK == (rc = sr_get_item_next(srcfg_session, iter, &value))) { ly_diminish_errors = false; if (NULL == value) { goto next; } /* get node schema */ schema = ly_ctx_get_node2(ly_ctx, NULL, value->xpath, 0); if (!schema) { SR_LOG_ERR("Error by ly_ctx_get_node2: %s", ly_errmsg()); goto fail; } /* skip default values */ if (schema->nodetype == LYS_LEAF && value->dflt) { goto next; } /* skip non-presence containers */ if (value->type == SR_CONTAINER_T) { goto next; } /* convert value to string */ rc = sr_val_to_str(value, schema, &string_val); if (SR_ERR_OK != rc) { SR_LOG_ERR("Error by sr_val_to_str: %s", sr_strerror(rc)); goto fail; } /* add node to data tree */ ly_errno = LY_SUCCESS; node = lyd_new_path(*data_tree, ly_ctx, value->xpath, string_val, LYD_PATH_OPT_UPDATE); if (!node && LY_SUCCESS != ly_errno) { SR_LOG_ERR("Error by lyd_new_path: %s", ly_errmsg()); goto fail; } if (NULL == *data_tree) { *data_tree = node; } next: /* cleanup before next iteration */ if (NULL != string_val) { free(string_val); string_val = NULL; } if (NULL != value) { sr_free_val(value); value = NULL; } ly_diminish_errors = true; } ly_diminish_errors = false; if (SR_ERR_NOT_FOUND == rc) { rc = SR_ERR_OK; } if (SR_ERR_OK == rc) { if (NULL != *data_tree) { /* validate returned data, but most importantly resolve leafrefs */ ret = lyd_validate(data_tree, LYD_OPT_STRICT | LYD_OPT_CONFIG | LYD_WD_IMPL_TAG); CHECK_ZERO_LOG_GOTO(ret, rc, SR_ERR_INTERNAL, fail, "Received data tree from sysrepo is not valid: %s", ly_errmsg()); /* remove default nodes added by validation */ lyd_wd_cleanup(data_tree, 0); } goto cleanup; } fail: rc = SR_ERR_INTERNAL; if (NULL != *data_tree) { lyd_free_withsiblings(*data_tree); *data_tree = NULL; } cleanup: if (NULL != string_val) { free(string_val); } if (NULL != value) { sr_free_val(value); } if (NULL != iter) { sr_free_val_iter(iter); } return rc; }
ydk::path::DataNode& ydk::path::RootDataImpl::create_datanode(const std::string& path, const std::string& value) { populate_new_schemas_from_path(path); populate_new_schemas_from_path(value); if(path.empty()) { YLOG_ERROR("Path is empty"); throw(YInvalidArgumentError{"Path is empty"}); } //path should not start with / if(path.at(0) == '/') { YLOG_ERROR("Path '{}' should not start with /", path); throw(YInvalidArgumentError{"Path should not start with /"}); } std::vector<std::string> segments = segmentalize(path); if(segments.size()<=0) { YLOG_ERROR("Could not segmentalize path"); throw(YInvalidArgumentError{"Could not segmentalize path"}); } std::string start_seg = m_path + segments[0]; YLOG_DEBUG("Creating root data node with path '{}'", start_seg); struct lyd_node* dnode = lyd_new_path(m_node, m_ctx, start_seg.c_str(), segments.size() == 1 ? (void*)value.c_str():nullptr, LYD_ANYDATA_SXML, 0); if( dnode == nullptr) { YLOG_ERROR("Path '{}' is invalid", path); throw(YInvalidArgumentError{"Path is invalid: " + path}); } DataNodeImpl* dn = nullptr; if(m_node == nullptr) { m_node = dnode; child_map.insert(std::make_pair(m_node, std::make_shared<DataNodeImpl>(this, m_node, m_priv_repo))); dn = dynamic_cast<DataNodeImpl*>(child_map[m_node].get()); } else { //dnode is one of the siblings of m_node auto iter = child_map.find(dnode); if(iter != child_map.end()) { dn = dynamic_cast<DataNodeImpl*>(iter->second.get()); } else { child_map.insert(std::make_pair(m_node, std::make_shared<DataNodeImpl>(this, m_node, m_priv_repo))); dn = dynamic_cast<DataNodeImpl*>(child_map[m_node].get()); } } DataNode* rdn = dn; //at this stage we have dn so for the remaining segments use dn as the parent if(segments.size() > 1) { std::string remaining_path; for(size_t i =1; i< segments.size(); i++) { if(i!=1) { remaining_path+="/"; } remaining_path+=segments[i]; } rdn = &(rdn->create_datanode(remaining_path)); } return *rdn; }