|
9 | 9 | #if (LY_VERSION_MAJOR != 3)
|
10 | 10 | #error "This version of libyang bindings only works with libyang 3.x"
|
11 | 11 | #endif
|
| 12 | + |
| 13 | +typedef struct { |
| 14 | + char **results; |
| 15 | + size_t nresults; |
| 16 | + size_t alloc_results; |
| 17 | +} pyly_string_list_t; |
| 18 | + |
| 19 | +/*! Takes append an entry to a dynamic array of strings |
| 20 | + * \param[in] l Pointer to pyly_string_list_t object (must be initialized to zero) |
| 21 | + * \param[in] str String, the pointer will be owned by the list |
| 22 | + */ |
| 23 | +static void pyly_strlist_append(pyly_string_list_t *l, char *str /* Takes ownership */) |
| 24 | +{ |
| 25 | + if (l == NULL || str == NULL) { |
| 26 | + return; |
| 27 | + } |
| 28 | + |
| 29 | + if (l->nresults + 1 > l->alloc_results) { |
| 30 | + if (l->alloc_results == 0) { |
| 31 | + l->alloc_results = 1; |
| 32 | + } else { |
| 33 | + l->alloc_results <<= 1; |
| 34 | + } |
| 35 | + l->results = realloc(l->results, l->alloc_results * sizeof(*l->results)); |
| 36 | + } |
| 37 | + l->results[l->nresults++] = str; |
| 38 | +} |
| 39 | + |
| 40 | +void pyly_cstr_array_free(char **list, size_t nlist) |
| 41 | +{ |
| 42 | + size_t i; |
| 43 | + |
| 44 | + if (list == NULL) |
| 45 | + return; |
| 46 | + |
| 47 | + for (i=0; i<nlist; i++) { |
| 48 | + free(list[i]); |
| 49 | + } |
| 50 | + free(list); |
| 51 | +} |
| 52 | + |
| 53 | +typedef struct { |
| 54 | + const struct ly_ctx *ctx; |
| 55 | + const char *base_path; |
| 56 | + int include_children; |
| 57 | + pyly_string_list_t *res; |
| 58 | +} pyly_dfs_data_t; |
| 59 | + |
| 60 | +static char *pyly_lref_to_xpath(const struct lysc_node *node, const struct lysc_type_leafref *lref) |
| 61 | +{ |
| 62 | + struct ly_set *set = NULL; |
| 63 | + LY_ERR err; |
| 64 | + char *path = NULL; |
| 65 | + |
| 66 | + err = lys_find_expr_atoms(node, node->module, lref->path, lref->prefixes, 0, &set); |
| 67 | + if (err != LY_SUCCESS) { |
| 68 | + return NULL; |
| 69 | + } |
| 70 | + |
| 71 | + if (set->count != 0) { |
| 72 | + path = lysc_path(set->snodes[set->count - 1], LYSC_PATH_DATA, NULL, 0); |
| 73 | + } |
| 74 | + |
| 75 | + ly_set_free(set, NULL); |
| 76 | + return path; |
| 77 | +} |
| 78 | + |
| 79 | +static size_t pyly_snode_fetch_leafrefs(const struct lysc_node *node, char ***out) |
| 80 | +{ |
| 81 | + pyly_string_list_t res; |
| 82 | + const struct lysc_node_leaf *leaf; |
| 83 | + |
| 84 | + if (node == NULL || out == NULL) { |
| 85 | + return 0; |
| 86 | + } |
| 87 | + |
| 88 | + memset(&res, 0, sizeof(res)); |
| 89 | + *out = NULL; |
| 90 | + |
| 91 | + /* Not a node type we are interested in */ |
| 92 | + if (node->nodetype != LYS_LEAF && node->nodetype != LYS_LEAFLIST) { |
| 93 | + return 0; |
| 94 | + } |
| 95 | + |
| 96 | + leaf = (const struct lysc_node_leaf *)node; |
| 97 | + if (leaf->type->basetype == LY_TYPE_UNION) { |
| 98 | + /* Unions are a bit of a pain as they aren't represented by nodes, |
| 99 | + * so we need to iterate across them to see if they contain any |
| 100 | + * leafrefs */ |
| 101 | + const struct lysc_type_union *un = (const struct lysc_type_union *)leaf->type; |
| 102 | + size_t i; |
| 103 | + |
| 104 | + for (i=0; i<LY_ARRAY_COUNT(un->types); i++) { |
| 105 | + const struct lysc_type *utype = un->types[i]; |
| 106 | + |
| 107 | + if (utype->basetype != LY_TYPE_LEAFREF) { |
| 108 | + continue; |
| 109 | + } |
| 110 | + |
| 111 | + pyly_strlist_append(&res, pyly_lref_to_xpath(node, (const struct lysc_type_leafref *)utype)); |
| 112 | + } |
| 113 | + } else if (leaf->type->basetype == LY_TYPE_LEAFREF) { |
| 114 | + const struct lysc_node *base_node = lysc_node_lref_target(node); |
| 115 | + |
| 116 | + if (base_node == NULL) { |
| 117 | + return 0; |
| 118 | + } |
| 119 | + |
| 120 | + pyly_strlist_append(&res, lysc_path(base_node, LYSC_PATH_DATA, NULL, 0)); |
| 121 | + } else { |
| 122 | + /* Not a node type we're interested in */ |
| 123 | + return 0; |
| 124 | + } |
| 125 | + |
| 126 | + *out = res.results; |
| 127 | + return res.nresults; |
| 128 | +} |
| 129 | + |
| 130 | +/*! For the given xpath, return the xpaths for the nodes they reference. |
| 131 | + * |
| 132 | + * \param[in] ctx Initialized context with loaded schema |
| 133 | + * \param[in] xpath Xpath |
| 134 | + * \param[out] out Pointer passed by reference that will hold a C array |
| 135 | + * of c strings representing the paths for any leaf |
| 136 | + * references. |
| 137 | + * \return number of results, or 0 if none. |
| 138 | + */ |
| 139 | +size_t pyly_backlinks_xpath_leafrefs(const struct ly_ctx *ctx, const char *xpath, char ***out) |
| 140 | +{ |
| 141 | + LY_ERR err; |
| 142 | + struct ly_set *set = NULL; |
| 143 | + size_t i; |
| 144 | + pyly_string_list_t res; |
| 145 | + |
| 146 | + if (ctx == NULL || xpath == NULL || out == NULL) { |
| 147 | + return 0; |
| 148 | + } |
| 149 | + |
| 150 | + memset(&res, 0, sizeof(res)); |
| 151 | + |
| 152 | + *out = NULL; |
| 153 | + |
| 154 | + err = lys_find_xpath(ctx, NULL, xpath, 0, &set); |
| 155 | + if (err != LY_SUCCESS) { |
| 156 | + return 0; |
| 157 | + } |
| 158 | + |
| 159 | + for (i=0; i<set->count; i++) { |
| 160 | + size_t cnt; |
| 161 | + size_t j; |
| 162 | + char **refs = NULL; |
| 163 | + cnt = pyly_snode_fetch_leafrefs(set->snodes[i], &refs); |
| 164 | + for (j=0; j<cnt; j++) { |
| 165 | + pyly_strlist_append(&res, strdup(refs[j])); |
| 166 | + } |
| 167 | + pyly_cstr_array_free(refs, cnt); |
| 168 | + } |
| 169 | + |
| 170 | + ly_set_free(set, NULL); |
| 171 | + |
| 172 | + *out = res.results; |
| 173 | + return res.nresults; |
| 174 | +} |
| 175 | + |
| 176 | +static LY_ERR pyly_backlinks_find_leafref_nodes_clb(struct lysc_node *node, void *data, ly_bool *dfs_continue) |
| 177 | +{ |
| 178 | + pyly_dfs_data_t *dctx = data; |
| 179 | + char **leafrefs = NULL; |
| 180 | + size_t nleafrefs; |
| 181 | + size_t i; |
| 182 | + int found = 0; |
| 183 | + |
| 184 | + /* Not a node type we are interested in */ |
| 185 | + if (node->nodetype != LYS_LEAF && node->nodetype != LYS_LEAFLIST) { |
| 186 | + return LY_SUCCESS; |
| 187 | + } |
| 188 | + |
| 189 | + /* Fetch leafrefs for comparison against our base path. Even if we are |
| 190 | + * going to throw them away, we still need a count to know if this has |
| 191 | + * leafrefs */ |
| 192 | + nleafrefs = pyly_snode_fetch_leafrefs(node, &leafrefs); |
| 193 | + if (nleafrefs == 0) { |
| 194 | + return LY_SUCCESS; |
| 195 | + } |
| 196 | + |
| 197 | + for (i=0; i<nleafrefs && !found; i++) { |
| 198 | + if (dctx->base_path != NULL) { |
| 199 | + if (dctx->include_children) { |
| 200 | + if (strncmp(leafrefs[i], dctx->base_path, strlen(dctx->base_path)) != 0) { |
| 201 | + continue; |
| 202 | + } |
| 203 | + } else { |
| 204 | + if (strcmp(leafrefs[i], dctx->base_path) != 0) { |
| 205 | + continue; |
| 206 | + } |
| 207 | + } |
| 208 | + } |
| 209 | + found = 1; |
| 210 | + } |
| 211 | + pyly_cstr_array_free(leafrefs, nleafrefs); |
| 212 | + |
| 213 | + if (found) { |
| 214 | + pyly_strlist_append(dctx->res, lysc_path(node, LYSC_PATH_DATA, NULL, 0)); |
| 215 | + } |
| 216 | + |
| 217 | + return LY_SUCCESS; |
| 218 | +} |
| 219 | + |
| 220 | +/*! Search the entire loaded schema for any nodes that contain a leafref and |
| 221 | + * record the path. If a base_path is specified, only leafrefs that point to |
| 222 | + * the specified path will be recorded, if include_children is 1, then children |
| 223 | + * of the specified path are also included. |
| 224 | + * |
| 225 | + * This function is used in replacement for the concept of backlink references |
| 226 | + * that were part of libyang v1 but were subsequently removed. This is |
| 227 | + * implemented in C code due to the overhead involved with needing to produce |
| 228 | + * Python nodes for results for evaluation which has a high overhead. |
| 229 | + * |
| 230 | + * If building a data cache to more accurately replicate the prior backlinks |
| 231 | + * concept, pass NULL to base_path which will then return any paths that |
| 232 | + * reference another. It is then the caller's responsibility to look up where |
| 233 | + * the leafref is pointing as part of building the cache. It is expected most |
| 234 | + * users will not need the cache and will simply pass in the base_path as needed. |
| 235 | + * |
| 236 | + * \param[in] ctx Initialized context with loaded schema |
| 237 | + * \param[in] base_path Optional base node path to restrict output. |
| 238 | + * \param[in] include_children Whether or not to include children of the |
| 239 | + * specified base path or if the path is an |
| 240 | + * explicit reference. |
| 241 | + * \param[out] out Pointer passed by reference that will hold a C |
| 242 | + * array of c strings representing the paths for |
| 243 | + * any leaf references. |
| 244 | + * \return number of results, or 0 if none. |
| 245 | + */ |
| 246 | +size_t pyly_backlinks_find_leafref_nodes(const struct ly_ctx *ctx, const char *base_path, int include_children, char ***out) |
| 247 | +{ |
| 248 | + pyly_string_list_t res; |
| 249 | + uint32_t module_idx = 0; |
| 250 | + const struct lys_module *module; |
| 251 | + |
| 252 | + memset(&res, 0, sizeof(res)); |
| 253 | + |
| 254 | + if (ctx == NULL || out == NULL) { |
| 255 | + return 0; |
| 256 | + } |
| 257 | + |
| 258 | + /* Iterate across all loaded modules */ |
| 259 | + for (module_idx = 0; (module = ly_ctx_get_module_iter(ctx, &module_idx)) != NULL; ) { |
| 260 | + pyly_dfs_data_t data = { ctx, base_path, include_children, &res }; |
| 261 | + |
| 262 | + lysc_module_dfs_full(module, pyly_backlinks_find_leafref_nodes_clb, &data); |
| 263 | + /* Ignore error */ |
| 264 | + } |
| 265 | + |
| 266 | + *out = res.results; |
| 267 | + return res.nresults; |
| 268 | +} |
0 commit comments