Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 1 | /** |
| 2 | * @file context.c |
| 3 | * @author Radek Krejci <rkrejci@cesnet.cz> |
| 4 | * @brief context implementation for libyang |
| 5 | * |
| 6 | * Copyright (c) 2015 CESNET, z.s.p.o. |
| 7 | * |
Radek Krejci | 54f6fb3 | 2016-02-24 12:56:39 +0100 | [diff] [blame] | 8 | * This source code is licensed under BSD 3-Clause License (the "License"). |
| 9 | * You may not use this file except in compliance with the License. |
| 10 | * You may obtain a copy of the License at |
Michal Vasko | 8de098c | 2016-02-26 10:00:25 +0100 | [diff] [blame] | 11 | * |
Radek Krejci | 54f6fb3 | 2016-02-24 12:56:39 +0100 | [diff] [blame] | 12 | * https://opensource.org/licenses/BSD-3-Clause |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 13 | */ |
| 14 | |
| 15 | #define _GNU_SOURCE |
Radek Krejci | fd4e6e3 | 2015-08-10 15:00:51 +0200 | [diff] [blame] | 16 | #include <stddef.h> |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 17 | #include <stdlib.h> |
| 18 | #include <string.h> |
| 19 | #include <sys/types.h> |
| 20 | #include <sys/stat.h> |
| 21 | #include <unistd.h> |
| 22 | #include <errno.h> |
| 23 | #include <fcntl.h> |
| 24 | |
| 25 | #include "common.h" |
| 26 | #include "context.h" |
Radek Krejci | 41912fe | 2015-10-22 10:22:12 +0200 | [diff] [blame] | 27 | #include "dict_private.h" |
Michal Vasko | 209a622 | 2015-10-16 09:51:07 +0200 | [diff] [blame] | 28 | #include "parser.h" |
Radek Krejci | bc9cf93 | 2015-07-30 11:09:39 +0200 | [diff] [blame] | 29 | #include "tree_internal.h" |
Radek Krejci | 83a4bac | 2017-02-07 15:53:04 +0100 | [diff] [blame] | 30 | #include "resolve.h" |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 31 | |
Radek Krejci | 858ad95 | 2017-01-04 11:16:32 +0100 | [diff] [blame] | 32 | /* |
| 33 | * counter for references to the extensions plugins (for the number of contexts) |
| 34 | * located in extensions.c |
| 35 | */ |
| 36 | extern unsigned int ext_plugins_ref; |
| 37 | |
Radek Krejci | 532e5e9 | 2017-02-22 12:59:24 +0100 | [diff] [blame] | 38 | #define IETF_YANG_METADATA_PATH "../models/ietf-yang-metadata@2016-08-05.h" |
| 39 | #define YANG_PATH "../models/yang@2017-02-20.h" |
Michal Vasko | 8d054e4 | 2015-08-03 12:42:06 +0200 | [diff] [blame] | 40 | #define IETF_INET_TYPES_PATH "../models/ietf-inet-types@2013-07-15.h" |
Michal Vasko | 21181c4 | 2015-08-03 13:46:45 +0200 | [diff] [blame] | 41 | #define IETF_YANG_TYPES_PATH "../models/ietf-yang-types@2013-07-15.h" |
Radek Krejci | d972391 | 2016-09-16 17:06:06 +0200 | [diff] [blame] | 42 | #define IETF_YANG_LIB_PATH "../models/ietf-yang-library@2016-06-21.h" |
| 43 | #define IETF_YANG_LIB_REV "2016-06-21" |
Michal Vasko | 8d054e4 | 2015-08-03 12:42:06 +0200 | [diff] [blame] | 44 | |
Radek Krejci | 532e5e9 | 2017-02-22 12:59:24 +0100 | [diff] [blame] | 45 | #include IETF_YANG_METADATA_PATH |
| 46 | #include YANG_PATH |
Michal Vasko | 8d054e4 | 2015-08-03 12:42:06 +0200 | [diff] [blame] | 47 | #include IETF_INET_TYPES_PATH |
Michal Vasko | 21181c4 | 2015-08-03 13:46:45 +0200 | [diff] [blame] | 48 | #include IETF_YANG_TYPES_PATH |
Michal Vasko | 8d054e4 | 2015-08-03 12:42:06 +0200 | [diff] [blame] | 49 | #include IETF_YANG_LIB_PATH |
| 50 | |
Radek Krejci | 0320341 | 2016-06-23 15:20:53 +0200 | [diff] [blame] | 51 | static struct internal_modules_s { |
| 52 | const char *name; |
| 53 | const char *revision; |
| 54 | const char *data; |
| 55 | uint8_t implemented; |
| 56 | LYS_INFORMAT format; |
Michal Vasko | 2d051a1 | 2017-04-21 09:28:57 +0200 | [diff] [blame^] | 57 | } internal_modules[LY_INTERNAL_MODULE_COUNT] = { |
Radek Krejci | 532e5e9 | 2017-02-22 12:59:24 +0100 | [diff] [blame] | 58 | {"ietf-yang-metadata", "2016-08-05", (const char*)ietf_yang_metadata_2016_08_05_yin, 0, LYS_IN_YIN}, |
| 59 | {"yang", "2017-02-20", (const char*)yang_2017_02_20_yin, 1, LYS_IN_YIN}, |
Radek Krejci | 0320341 | 2016-06-23 15:20:53 +0200 | [diff] [blame] | 60 | {"ietf-inet-types", "2013-07-15", (const char*)ietf_inet_types_2013_07_15_yin, 0, LYS_IN_YIN}, |
| 61 | {"ietf-yang-types", "2013-07-15", (const char*)ietf_yang_types_2013_07_15_yin, 0, LYS_IN_YIN}, |
Radek Krejci | d972391 | 2016-09-16 17:06:06 +0200 | [diff] [blame] | 62 | {"ietf-yang-library", "2016-06-21", (const char*)ietf_yang_library_2016_06_21_yin, 1, LYS_IN_YIN} |
Radek Krejci | 0320341 | 2016-06-23 15:20:53 +0200 | [diff] [blame] | 63 | }; |
| 64 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 65 | API struct ly_ctx * |
| 66 | ly_ctx_new(const char *search_dir) |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 67 | { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 68 | struct ly_ctx *ctx; |
Michal Vasko | 7d7de95 | 2016-05-02 17:13:14 +0200 | [diff] [blame] | 69 | struct lys_module *module; |
Michal Vasko | 70b6d69 | 2015-08-03 14:05:59 +0200 | [diff] [blame] | 70 | char *cwd; |
Radek Krejci | 0320341 | 2016-06-23 15:20:53 +0200 | [diff] [blame] | 71 | int i; |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 72 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 73 | ctx = calloc(1, sizeof *ctx); |
| 74 | if (!ctx) { |
| 75 | LOGMEM; |
| 76 | return NULL; |
| 77 | } |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 78 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 79 | /* dictionary */ |
| 80 | lydict_init(&ctx->dict); |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 81 | |
Radek Krejci | 858ad95 | 2017-01-04 11:16:32 +0100 | [diff] [blame] | 82 | /* plugins */ |
Radek Krejci | 858ad95 | 2017-01-04 11:16:32 +0100 | [diff] [blame] | 83 | lyext_load_plugins(); |
| 84 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 85 | /* models list */ |
| 86 | ctx->models.list = calloc(16, sizeof *ctx->models.list); |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 87 | if (!ctx->models.list) { |
| 88 | LOGMEM; |
| 89 | free(ctx); |
| 90 | return NULL; |
| 91 | } |
Radek Krejci | 37d0263 | 2017-02-21 14:14:11 +0100 | [diff] [blame] | 92 | ext_plugins_ref++; |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 93 | ctx->models.used = 0; |
| 94 | ctx->models.size = 16; |
| 95 | if (search_dir) { |
| 96 | cwd = get_current_dir_name(); |
| 97 | if (chdir(search_dir)) { |
| 98 | LOGERR(LY_ESYS, "Unable to use search directory \"%s\" (%s)", |
| 99 | search_dir, strerror(errno)); |
| 100 | free(cwd); |
Radek Krejci | fa0b5e0 | 2016-02-04 13:57:03 +0100 | [diff] [blame] | 101 | ly_ctx_destroy(ctx, NULL); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 102 | return NULL; |
| 103 | } |
Radek Krejci | da9f839 | 2017-03-25 19:40:56 -0500 | [diff] [blame] | 104 | ctx->models.search_paths = malloc(2 * sizeof *ctx->models.search_paths); |
| 105 | ctx->models.search_paths[0] = get_current_dir_name(); |
| 106 | ctx->models.search_paths[1] = NULL; |
Radek Krejci | 15412ca | 2016-03-03 11:16:52 +0100 | [diff] [blame] | 107 | if (chdir(cwd)) { |
| 108 | LOGWRN("Unable to return back to working directory \"%s\" (%s)", |
| 109 | cwd, strerror(errno)); |
| 110 | } |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 111 | free(cwd); |
| 112 | } |
Michal Vasko | 14719b2 | 2015-08-03 12:47:55 +0200 | [diff] [blame] | 113 | ctx->models.module_set_id = 1; |
| 114 | |
Radek Krejci | 0320341 | 2016-06-23 15:20:53 +0200 | [diff] [blame] | 115 | /* load internal modules */ |
Michal Vasko | 2d051a1 | 2017-04-21 09:28:57 +0200 | [diff] [blame^] | 116 | for (i = 0; i < LY_INTERNAL_MODULE_COUNT; i++) { |
Radek Krejci | 0320341 | 2016-06-23 15:20:53 +0200 | [diff] [blame] | 117 | module = (struct lys_module *)lys_parse_mem(ctx, internal_modules[i].data, internal_modules[i].format); |
| 118 | if (!module) { |
| 119 | ly_ctx_destroy(ctx, NULL); |
| 120 | return NULL; |
| 121 | } |
| 122 | module->implemented = internal_modules[i].implemented; |
Michal Vasko | 8d054e4 | 2015-08-03 12:42:06 +0200 | [diff] [blame] | 123 | } |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 124 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 125 | return ctx; |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 126 | } |
| 127 | |
Radek Krejci | 69333c9 | 2017-03-17 16:14:43 +0100 | [diff] [blame] | 128 | static struct ly_ctx * |
| 129 | ly_ctx_new_yl_common(const char *search_dir, const char *input, LYD_FORMAT format, |
| 130 | struct lyd_node* (*parser_func)(struct ly_ctx*, const char*, LYD_FORMAT, int,...)) |
| 131 | { |
| 132 | unsigned int u; |
| 133 | struct lyd_node *module, *node; |
| 134 | const char *name, *revision; |
Radek Krejci | 5cb217d | 2017-03-25 16:07:52 -0500 | [diff] [blame] | 135 | struct ly_set features = {0, 0, {NULL}}; |
Radek Krejci | 69333c9 | 2017-03-17 16:14:43 +0100 | [diff] [blame] | 136 | const struct lys_module *mod; |
| 137 | struct lyd_node *yltree = NULL; |
| 138 | struct ly_ctx *ctx = NULL; |
| 139 | |
| 140 | /* create empty (with internal modules including ietf-yang-library) context */ |
| 141 | ctx = ly_ctx_new(search_dir); |
| 142 | if (!ctx) { |
| 143 | goto error; |
| 144 | } |
| 145 | |
| 146 | /* parse yang library data tree */ |
| 147 | yltree = parser_func(ctx, input, format, LYD_OPT_DATA, NULL); |
| 148 | if (!yltree) { |
| 149 | goto error; |
| 150 | } |
| 151 | |
| 152 | /* process the data tree */ |
| 153 | LY_TREE_FOR(yltree->child, module) { |
| 154 | if (module->schema->nodetype == LYS_LEAF) { |
| 155 | /* module-set-id - ignore it */ |
| 156 | continue; |
| 157 | } |
| 158 | |
| 159 | /* initiate */ |
| 160 | name = NULL; |
| 161 | revision = NULL; |
| 162 | ly_set_clean(&features); |
| 163 | |
| 164 | LY_TREE_FOR(module->child, node) { |
| 165 | if (!strcmp(node->schema->name, "name")) { |
| 166 | name = ((struct lyd_node_leaf_list*)node)->value_str; |
| 167 | } else if (!strcmp(node->schema->name, "revision")) { |
| 168 | revision = ((struct lyd_node_leaf_list*)node)->value_str; |
| 169 | } else if (!strcmp(node->schema->name, "feature")) { |
| 170 | ly_set_add(&features, node, LY_SET_OPT_USEASLIST); |
| 171 | } else if (!strcmp(node->schema->name, "conformance-type") && |
| 172 | ((struct lyd_node_leaf_list*)node)->value.enm->value) { |
| 173 | /* imported module - skip it, it will be loaded as a side effect |
| 174 | * of loading another module */ |
| 175 | goto next_module; |
| 176 | } |
| 177 | } |
| 178 | |
| 179 | /* use the gathered data to load the module */ |
| 180 | mod = ly_ctx_load_module(ctx, name, revision); |
| 181 | if (!mod) { |
| 182 | LOGERR(LY_EINVAL, "Unable to load module specified by yang library data."); |
| 183 | goto error; |
| 184 | } |
| 185 | |
| 186 | /* set features */ |
| 187 | for (u = 0; u < features.number; u++) { |
| 188 | lys_features_enable(mod, ((struct lyd_node_leaf_list*)features.set.d[u])->value_str); |
| 189 | } |
| 190 | |
| 191 | next_module:; |
| 192 | } |
| 193 | |
| 194 | if (0) { |
| 195 | /* skip context destroy in case of success */ |
| 196 | error: |
| 197 | ly_ctx_destroy(ctx, NULL); |
| 198 | ctx = NULL; |
| 199 | } |
| 200 | |
| 201 | /* cleanup */ |
| 202 | if (yltree) { |
| 203 | /* yang library data tree */ |
| 204 | lyd_free_withsiblings(yltree); |
| 205 | } |
| 206 | |
| 207 | return ctx; |
| 208 | } |
| 209 | |
| 210 | API struct ly_ctx * |
| 211 | ly_ctx_new_ylpath(const char *search_dir, const char *path, LYD_FORMAT format) |
| 212 | { |
| 213 | return ly_ctx_new_yl_common(search_dir, path, format, lyd_parse_path); |
| 214 | } |
| 215 | |
| 216 | API struct ly_ctx * |
| 217 | ly_ctx_new_ylmem(const char *search_dir, const char *data, LYD_FORMAT format) |
| 218 | { |
| 219 | return ly_ctx_new_yl_common(search_dir, data, format, lyd_parse_mem); |
| 220 | } |
| 221 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 222 | API void |
Radek Krejci | 819dd4b | 2017-03-07 15:35:48 +0100 | [diff] [blame] | 223 | ly_ctx_set_allimplemented(struct ly_ctx *ctx) |
| 224 | { |
| 225 | if (!ctx) { |
| 226 | return; |
| 227 | } |
| 228 | |
| 229 | ctx->models.flags |= LY_CTX_ALLIMPLEMENTED; |
| 230 | } |
| 231 | |
| 232 | API void |
| 233 | ly_ctx_unset_allimplemented(struct ly_ctx *ctx) |
| 234 | { |
| 235 | if (!ctx) { |
| 236 | return; |
| 237 | } |
| 238 | |
| 239 | ctx->models.flags &= ~LY_CTX_ALLIMPLEMENTED; |
| 240 | } |
| 241 | |
| 242 | API void |
Michal Vasko | 60ba9a6 | 2015-07-03 14:42:31 +0200 | [diff] [blame] | 243 | ly_ctx_set_searchdir(struct ly_ctx *ctx, const char *search_dir) |
| 244 | { |
| 245 | char *cwd; |
Radek Krejci | da9f839 | 2017-03-25 19:40:56 -0500 | [diff] [blame] | 246 | int index = 0; |
| 247 | void *r; |
Michal Vasko | 60ba9a6 | 2015-07-03 14:42:31 +0200 | [diff] [blame] | 248 | |
| 249 | if (!ctx) { |
| 250 | return; |
| 251 | } |
| 252 | |
| 253 | if (search_dir) { |
| 254 | cwd = get_current_dir_name(); |
| 255 | if (chdir(search_dir)) { |
| 256 | LOGERR(LY_ESYS, "Unable to use search directory \"%s\" (%s)", |
| 257 | search_dir, strerror(errno)); |
| 258 | free(cwd); |
| 259 | return; |
| 260 | } |
Radek Krejci | da9f839 | 2017-03-25 19:40:56 -0500 | [diff] [blame] | 261 | |
| 262 | if (!ctx->models.search_paths) { |
| 263 | ctx->models.search_paths = malloc(2 * sizeof *ctx->models.search_paths); |
| 264 | index = 0; |
| 265 | } else { |
| 266 | for (index = 0; ctx->models.search_paths[index]; index++); |
| 267 | r = realloc(ctx->models.search_paths, (index + 2) * sizeof *ctx->models.search_paths); |
| 268 | if (!r) { |
| 269 | LOGMEM; |
Radek Krejci | c10366a | 2017-03-26 12:05:39 -0500 | [diff] [blame] | 270 | free(cwd); |
Radek Krejci | da9f839 | 2017-03-25 19:40:56 -0500 | [diff] [blame] | 271 | return; |
| 272 | } |
| 273 | ctx->models.search_paths = r; |
| 274 | } |
| 275 | ctx->models.search_paths[index] = get_current_dir_name(); |
| 276 | ctx->models.search_paths[index + 1] = NULL; |
Michal Vasko | 3eff932 | 2015-11-10 11:02:30 +0100 | [diff] [blame] | 277 | |
Radek Krejci | 15412ca | 2016-03-03 11:16:52 +0100 | [diff] [blame] | 278 | if (chdir(cwd)) { |
| 279 | LOGWRN("Unable to return back to working directory \"%s\" (%s)", |
| 280 | cwd, strerror(errno)); |
| 281 | } |
Michal Vasko | 60ba9a6 | 2015-07-03 14:42:31 +0200 | [diff] [blame] | 282 | free(cwd); |
Michal Vasko | 60ba9a6 | 2015-07-03 14:42:31 +0200 | [diff] [blame] | 283 | } |
| 284 | } |
| 285 | |
Radek Krejci | b081d8d | 2015-10-21 16:29:07 +0200 | [diff] [blame] | 286 | API const char * |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 287 | ly_ctx_get_searchdir(const struct ly_ctx *ctx) |
Radek Krejci | 5a79757 | 2015-10-21 15:45:45 +0200 | [diff] [blame] | 288 | { |
Radek Krejci | ee55417 | 2016-12-14 10:29:06 +0100 | [diff] [blame] | 289 | if (!ctx) { |
| 290 | ly_errno = LY_EINVAL; |
| 291 | return NULL; |
| 292 | } |
Radek Krejci | da9f839 | 2017-03-25 19:40:56 -0500 | [diff] [blame] | 293 | return ctx->models.search_paths ? ctx->models.search_paths[0] : NULL; |
| 294 | } |
| 295 | |
| 296 | API void |
| 297 | ly_ctx_unset_searchdirs(struct ly_ctx *ctx) |
| 298 | { |
| 299 | int i; |
| 300 | |
| 301 | if (!ctx->models.search_paths) { |
| 302 | return; |
| 303 | } |
| 304 | |
| 305 | for (i = 0; ctx->models.search_paths[i]; i++) { |
| 306 | free(ctx->models.search_paths[i]); |
| 307 | } |
| 308 | free(ctx->models.search_paths); |
| 309 | ctx->models.search_paths = NULL; |
Radek Krejci | 5a79757 | 2015-10-21 15:45:45 +0200 | [diff] [blame] | 310 | } |
| 311 | |
Michal Vasko | 60ba9a6 | 2015-07-03 14:42:31 +0200 | [diff] [blame] | 312 | API void |
Radek Krejci | fa0b5e0 | 2016-02-04 13:57:03 +0100 | [diff] [blame] | 313 | ly_ctx_destroy(struct ly_ctx *ctx, void (*private_destructor)(const struct lys_node *node, void *priv)) |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 314 | { |
Radek Krejci | da9f839 | 2017-03-25 19:40:56 -0500 | [diff] [blame] | 315 | int i; |
| 316 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 317 | if (!ctx) { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 318 | return; |
| 319 | } |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 320 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 321 | /* models list */ |
Radek Krejci | c436a23 | 2017-02-08 14:43:11 +0100 | [diff] [blame] | 322 | for (; ctx->models.used > 0; ctx->models.used--) { |
Radek Krejci | fc8411a | 2017-02-03 10:26:05 +0100 | [diff] [blame] | 323 | /* remove the applied deviations and augments */ |
Radek Krejci | c436a23 | 2017-02-08 14:43:11 +0100 | [diff] [blame] | 324 | lys_sub_module_remove_devs_augs(ctx->models.list[ctx->models.used - 1]); |
Radek Krejci | fc8411a | 2017-02-03 10:26:05 +0100 | [diff] [blame] | 325 | /* remove the module */ |
Radek Krejci | c436a23 | 2017-02-08 14:43:11 +0100 | [diff] [blame] | 326 | lys_free(ctx->models.list[ctx->models.used - 1], private_destructor, 0); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 327 | } |
Radek Krejci | da9f839 | 2017-03-25 19:40:56 -0500 | [diff] [blame] | 328 | if (ctx->models.search_paths) { |
| 329 | for(i = 0; ctx->models.search_paths[i]; i++) { |
| 330 | free(ctx->models.search_paths[i]); |
| 331 | } |
| 332 | free(ctx->models.search_paths); |
| 333 | } |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 334 | free(ctx->models.list); |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 335 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 336 | /* dictionary */ |
| 337 | lydict_clean(&ctx->dict); |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 338 | |
Radek Krejci | 858ad95 | 2017-01-04 11:16:32 +0100 | [diff] [blame] | 339 | /* plugins - will be removed only if this is the last context */ |
| 340 | ext_plugins_ref--; |
| 341 | lyext_clean_plugins(); |
| 342 | |
Radek Krejci | 8b76a4a | 2016-10-26 10:02:32 +0200 | [diff] [blame] | 343 | /* clean the error list */ |
Radek Krejci | 00a0e71 | 2016-10-26 10:24:46 +0200 | [diff] [blame] | 344 | ly_err_clean(0); |
Radek Krejci | 8b76a4a | 2016-10-26 10:02:32 +0200 | [diff] [blame] | 345 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 346 | free(ctx); |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 347 | } |
| 348 | |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 349 | API const struct lys_submodule * |
Radek Krejci | 62f0da7 | 2016-03-07 11:35:43 +0100 | [diff] [blame] | 350 | ly_ctx_get_submodule2(const struct lys_module *main_module, const char *submodule) |
| 351 | { |
Radek Krejci | d4c1d0f | 2017-01-19 16:11:38 +0100 | [diff] [blame] | 352 | const struct lys_submodule *result; |
Radek Krejci | 62f0da7 | 2016-03-07 11:35:43 +0100 | [diff] [blame] | 353 | int i; |
| 354 | |
| 355 | if (!main_module || !submodule) { |
| 356 | ly_errno = LY_EINVAL; |
| 357 | return NULL; |
| 358 | } |
| 359 | |
| 360 | /* search in submodules list */ |
| 361 | for (i = 0; i < main_module->inc_size; i++) { |
| 362 | result = main_module->inc[i].submodule; |
Radek Krejci | d4c1d0f | 2017-01-19 16:11:38 +0100 | [diff] [blame] | 363 | if (ly_strequal(submodule, result->name, 0)) { |
Radek Krejci | 62f0da7 | 2016-03-07 11:35:43 +0100 | [diff] [blame] | 364 | return result; |
| 365 | } |
Radek Krejci | d4c1d0f | 2017-01-19 16:11:38 +0100 | [diff] [blame] | 366 | |
| 367 | /* in YANG 1.1 all the submodules must be included in the main module, so we are done. |
| 368 | * YANG 1.0 allows (is unclear about denying it) to include a submodule only in another submodule |
| 369 | * but when libyang parses such a module it adds the include into the main module so we are also done. |
| 370 | */ |
Radek Krejci | 62f0da7 | 2016-03-07 11:35:43 +0100 | [diff] [blame] | 371 | } |
| 372 | |
Radek Krejci | d4c1d0f | 2017-01-19 16:11:38 +0100 | [diff] [blame] | 373 | |
Radek Krejci | 62f0da7 | 2016-03-07 11:35:43 +0100 | [diff] [blame] | 374 | return NULL; |
| 375 | } |
| 376 | |
| 377 | API const struct lys_submodule * |
Michal Vasko | f6d94c6 | 2016-04-05 11:21:54 +0200 | [diff] [blame] | 378 | ly_ctx_get_submodule(const struct ly_ctx *ctx, const char *module, const char *revision, const char *submodule, |
| 379 | const char *sub_revision) |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 380 | { |
Radek Krejci | e797355 | 2016-03-07 08:12:01 +0100 | [diff] [blame] | 381 | const struct lys_module *mainmod; |
Michal Vasko | f6d94c6 | 2016-04-05 11:21:54 +0200 | [diff] [blame] | 382 | const struct lys_submodule *ret = NULL, *submod; |
| 383 | uint32_t idx = 0; |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 384 | |
Michal Vasko | f6d94c6 | 2016-04-05 11:21:54 +0200 | [diff] [blame] | 385 | if (!ctx || !submodule || (revision && !module)) { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 386 | ly_errno = LY_EINVAL; |
| 387 | return NULL; |
| 388 | } |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 389 | |
Michal Vasko | f6d94c6 | 2016-04-05 11:21:54 +0200 | [diff] [blame] | 390 | while ((mainmod = ly_ctx_get_module_iter(ctx, &idx))) { |
| 391 | if (module && strcmp(mainmod->name, module)) { |
| 392 | /* main module name does not match */ |
| 393 | continue; |
| 394 | } |
| 395 | |
| 396 | if (revision && (!mainmod->rev || strcmp(revision, mainmod->rev[0].date))) { |
| 397 | /* main module revision does not match */ |
| 398 | continue; |
| 399 | } |
| 400 | |
| 401 | submod = ly_ctx_get_submodule2(mainmod, submodule); |
| 402 | if (!submod) { |
| 403 | continue; |
| 404 | } |
| 405 | |
| 406 | if (!sub_revision) { |
| 407 | /* store only if newer */ |
| 408 | if (ret) { |
| 409 | if (submod->rev && (!ret->rev || (strcmp(submod->rev[0].date, ret->rev[0].date) > 0))) { |
| 410 | ret = submod; |
| 411 | } |
| 412 | } else { |
| 413 | ret = submod; |
| 414 | } |
| 415 | } else { |
| 416 | /* store only if revision matches, we are done if it does */ |
| 417 | if (!submod->rev) { |
| 418 | continue; |
| 419 | } else if (!strcmp(sub_revision, submod->rev[0].date)) { |
| 420 | ret = submod; |
| 421 | break; |
| 422 | } |
| 423 | } |
Radek Krejci | a7533f2 | 2016-03-07 07:37:45 +0100 | [diff] [blame] | 424 | } |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 425 | |
Michal Vasko | f6d94c6 | 2016-04-05 11:21:54 +0200 | [diff] [blame] | 426 | return ret; |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 427 | } |
| 428 | |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 429 | static const struct lys_module * |
Radek Krejci | 0ec51da | 2016-12-14 16:42:03 +0100 | [diff] [blame] | 430 | ly_ctx_get_module_by(const struct ly_ctx *ctx, const char *key, int offset, const char *revision, int with_disabled) |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 431 | { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 432 | int i; |
Radek Krejci | b804869 | 2015-08-05 13:36:34 +0200 | [diff] [blame] | 433 | struct lys_module *result = NULL; |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 434 | |
Radek Krejci | fd4e6e3 | 2015-08-10 15:00:51 +0200 | [diff] [blame] | 435 | if (!ctx || !key) { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 436 | ly_errno = LY_EINVAL; |
| 437 | return NULL; |
| 438 | } |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 439 | |
Radek Krejci | dce5145 | 2015-06-16 15:20:08 +0200 | [diff] [blame] | 440 | for (i = 0; i < ctx->models.used; i++) { |
Radek Krejci | 0ec51da | 2016-12-14 16:42:03 +0100 | [diff] [blame] | 441 | if (!with_disabled && ctx->models.list[i]->disabled) { |
| 442 | /* skip the disabled modules */ |
| 443 | continue; |
| 444 | } |
Radek Krejci | fd4e6e3 | 2015-08-10 15:00:51 +0200 | [diff] [blame] | 445 | /* use offset to get address of the pointer to string (char**), remember that offset is in |
| 446 | * bytes, so we have to cast the pointer to the module to (char*), finally, we want to have |
| 447 | * string not the pointer to string |
| 448 | */ |
| 449 | if (!ctx->models.list[i] || strcmp(key, *(char**)(((char*)ctx->models.list[i]) + offset))) { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 450 | continue; |
| 451 | } |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 452 | |
Radek Krejci | f647e61 | 2015-07-30 11:36:07 +0200 | [diff] [blame] | 453 | if (!revision) { |
| 454 | /* compare revisons and remember the newest one */ |
| 455 | if (result) { |
| 456 | if (!ctx->models.list[i]->rev_size) { |
| 457 | /* the current have no revision, keep the previous with some revision */ |
| 458 | continue; |
| 459 | } |
| 460 | if (result->rev_size && strcmp(ctx->models.list[i]->rev[0].date, result->rev[0].date) < 0) { |
| 461 | /* the previous found matching module has a newer revision */ |
| 462 | continue; |
| 463 | } |
| 464 | } |
| 465 | |
| 466 | /* remember the current match and search for newer version */ |
| 467 | result = ctx->models.list[i]; |
| 468 | } else { |
| 469 | if (ctx->models.list[i]->rev_size && !strcmp(revision, ctx->models.list[i]->rev[0].date)) { |
| 470 | /* matching revision */ |
Michal Vasko | 9758650 | 2015-08-12 14:32:18 +0200 | [diff] [blame] | 471 | result = ctx->models.list[i]; |
| 472 | break; |
Radek Krejci | f647e61 | 2015-07-30 11:36:07 +0200 | [diff] [blame] | 473 | } |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 474 | } |
| 475 | } |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 476 | |
Radek Krejci | f647e61 | 2015-07-30 11:36:07 +0200 | [diff] [blame] | 477 | return result; |
Radek Krejci | fd4e6e3 | 2015-08-10 15:00:51 +0200 | [diff] [blame] | 478 | |
| 479 | } |
| 480 | |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 481 | API const struct lys_module * |
| 482 | ly_ctx_get_module_by_ns(const struct ly_ctx *ctx, const char *ns, const char *revision) |
Radek Krejci | fd4e6e3 | 2015-08-10 15:00:51 +0200 | [diff] [blame] | 483 | { |
Radek Krejci | 0ec51da | 2016-12-14 16:42:03 +0100 | [diff] [blame] | 484 | return ly_ctx_get_module_by(ctx, ns, offsetof(struct lys_module, ns), revision, 0); |
Radek Krejci | fd4e6e3 | 2015-08-10 15:00:51 +0200 | [diff] [blame] | 485 | } |
| 486 | |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 487 | API const struct lys_module * |
| 488 | ly_ctx_get_module(const struct ly_ctx *ctx, const char *name, const char *revision) |
Radek Krejci | fd4e6e3 | 2015-08-10 15:00:51 +0200 | [diff] [blame] | 489 | { |
Radek Krejci | 0ec51da | 2016-12-14 16:42:03 +0100 | [diff] [blame] | 490 | return ly_ctx_get_module_by(ctx, name, offsetof(struct lys_module, name), revision, 0); |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 491 | } |
Michal Vasko | 3ec07dc | 2015-06-30 15:51:30 +0200 | [diff] [blame] | 492 | |
Radek Krejci | 21601a3 | 2016-03-07 11:39:27 +0100 | [diff] [blame] | 493 | API const struct lys_module * |
| 494 | ly_ctx_get_module_older(const struct ly_ctx *ctx, const struct lys_module *module) |
| 495 | { |
| 496 | int i; |
| 497 | const struct lys_module *result = NULL, *iter; |
| 498 | |
| 499 | if (!ctx || !module || !module->rev_size) { |
| 500 | ly_errno = LY_EINVAL; |
| 501 | return NULL; |
| 502 | } |
| 503 | |
| 504 | |
| 505 | for (i = 0; i < ctx->models.used; i++) { |
| 506 | iter = ctx->models.list[i]; |
Radek Krejci | 0ec51da | 2016-12-14 16:42:03 +0100 | [diff] [blame] | 507 | if (iter->disabled) { |
| 508 | /* skip the disabled modules */ |
| 509 | continue; |
| 510 | } |
Radek Krejci | 21601a3 | 2016-03-07 11:39:27 +0100 | [diff] [blame] | 511 | if (iter == module || !iter->rev_size) { |
| 512 | /* iter is the module itself or iter has no revision */ |
| 513 | continue; |
| 514 | } |
| 515 | if (!ly_strequal(module->name, iter->name, 0)) { |
| 516 | /* different module */ |
| 517 | continue; |
| 518 | } |
| 519 | if (strcmp(iter->rev[0].date, module->rev[0].date) < 0) { |
| 520 | /* iter is older than module */ |
| 521 | if (result) { |
| 522 | if (strcmp(iter->rev[0].date, result->rev[0].date) > 0) { |
| 523 | /* iter is newer than current result */ |
| 524 | result = iter; |
| 525 | } |
| 526 | } else { |
| 527 | result = iter; |
| 528 | } |
| 529 | } |
| 530 | } |
| 531 | |
| 532 | return result; |
| 533 | } |
| 534 | |
Michal Vasko | 99b0aad | 2015-12-01 12:28:51 +0100 | [diff] [blame] | 535 | API void |
Michal Vasko | ca28aab | 2017-01-13 13:50:12 +0100 | [diff] [blame] | 536 | ly_ctx_set_module_imp_clb(struct ly_ctx *ctx, ly_module_imp_clb clb, void *user_data) |
Michal Vasko | 8246596 | 2015-11-10 11:03:11 +0100 | [diff] [blame] | 537 | { |
Michal Vasko | ad9f92a | 2017-04-06 09:49:07 +0200 | [diff] [blame] | 538 | if (!ctx) { |
| 539 | ly_errno = LY_EINVAL; |
| 540 | return; |
| 541 | } |
| 542 | |
Michal Vasko | f53187d | 2017-01-13 13:23:14 +0100 | [diff] [blame] | 543 | ctx->imp_clb = clb; |
| 544 | ctx->imp_clb_data = user_data; |
Michal Vasko | 99b0aad | 2015-12-01 12:28:51 +0100 | [diff] [blame] | 545 | } |
| 546 | |
Michal Vasko | f53187d | 2017-01-13 13:23:14 +0100 | [diff] [blame] | 547 | API ly_module_imp_clb |
Michal Vasko | ca28aab | 2017-01-13 13:50:12 +0100 | [diff] [blame] | 548 | ly_ctx_get_module_imp_clb(const struct ly_ctx *ctx, void **user_data) |
Michal Vasko | 99b0aad | 2015-12-01 12:28:51 +0100 | [diff] [blame] | 549 | { |
Radek Krejci | ee55417 | 2016-12-14 10:29:06 +0100 | [diff] [blame] | 550 | if (!ctx) { |
| 551 | ly_errno = LY_EINVAL; |
| 552 | return NULL; |
| 553 | } |
| 554 | |
Michal Vasko | 99b0aad | 2015-12-01 12:28:51 +0100 | [diff] [blame] | 555 | if (user_data) { |
Michal Vasko | f53187d | 2017-01-13 13:23:14 +0100 | [diff] [blame] | 556 | *user_data = ctx->imp_clb_data; |
Michal Vasko | 99b0aad | 2015-12-01 12:28:51 +0100 | [diff] [blame] | 557 | } |
Michal Vasko | f53187d | 2017-01-13 13:23:14 +0100 | [diff] [blame] | 558 | return ctx->imp_clb; |
| 559 | } |
| 560 | |
| 561 | API void |
Michal Vasko | ca28aab | 2017-01-13 13:50:12 +0100 | [diff] [blame] | 562 | ly_ctx_set_module_data_clb(struct ly_ctx *ctx, ly_module_data_clb clb, void *user_data) |
Michal Vasko | f53187d | 2017-01-13 13:23:14 +0100 | [diff] [blame] | 563 | { |
Michal Vasko | ad9f92a | 2017-04-06 09:49:07 +0200 | [diff] [blame] | 564 | if (!ctx) { |
| 565 | ly_errno = LY_EINVAL; |
| 566 | return; |
| 567 | } |
| 568 | |
Michal Vasko | f53187d | 2017-01-13 13:23:14 +0100 | [diff] [blame] | 569 | ctx->data_clb = clb; |
| 570 | ctx->data_clb_data = user_data; |
| 571 | } |
| 572 | |
| 573 | API ly_module_data_clb |
Michal Vasko | ca28aab | 2017-01-13 13:50:12 +0100 | [diff] [blame] | 574 | ly_ctx_get_module_data_clb(const struct ly_ctx *ctx, void **user_data) |
Michal Vasko | f53187d | 2017-01-13 13:23:14 +0100 | [diff] [blame] | 575 | { |
Michal Vasko | ad9f92a | 2017-04-06 09:49:07 +0200 | [diff] [blame] | 576 | if (!ctx) { |
| 577 | ly_errno = LY_EINVAL; |
| 578 | return NULL; |
| 579 | } |
| 580 | |
Michal Vasko | f53187d | 2017-01-13 13:23:14 +0100 | [diff] [blame] | 581 | if (user_data) { |
| 582 | *user_data = ctx->data_clb_data; |
| 583 | } |
| 584 | return ctx->data_clb; |
Michal Vasko | 99b0aad | 2015-12-01 12:28:51 +0100 | [diff] [blame] | 585 | } |
| 586 | |
Radek Krejci | bf4e465 | 2016-10-21 15:44:13 +0200 | [diff] [blame] | 587 | const struct lys_module * |
Michal Vasko | 8447515 | 2016-07-25 16:16:25 +0200 | [diff] [blame] | 588 | ly_ctx_load_sub_module(struct ly_ctx *ctx, struct lys_module *module, const char *name, const char *revision, |
Michal Vasko | 58e5f3e | 2016-07-28 11:06:34 +0200 | [diff] [blame] | 589 | int implement, struct unres_schema *unres) |
Michal Vasko | 99b0aad | 2015-12-01 12:28:51 +0100 | [diff] [blame] | 590 | { |
Radek Krejci | bf4e465 | 2016-10-21 15:44:13 +0200 | [diff] [blame] | 591 | const struct lys_module *mod; |
Michal Vasko | 99b0aad | 2015-12-01 12:28:51 +0100 | [diff] [blame] | 592 | char *module_data; |
Radek Krejci | 0320341 | 2016-06-23 15:20:53 +0200 | [diff] [blame] | 593 | int i; |
Michal Vasko | d3e975b | 2016-03-03 15:40:21 +0100 | [diff] [blame] | 594 | void (*module_data_free)(void *module_data) = NULL; |
Michal Vasko | 99b0aad | 2015-12-01 12:28:51 +0100 | [diff] [blame] | 595 | LYS_INFORMAT format = LYS_IN_UNKNOWN; |
Michal Vasko | 8246596 | 2015-11-10 11:03:11 +0100 | [diff] [blame] | 596 | |
Michal Vasko | 8447515 | 2016-07-25 16:16:25 +0200 | [diff] [blame] | 597 | if (!module) { |
Radek Krejci | bf4e465 | 2016-10-21 15:44:13 +0200 | [diff] [blame] | 598 | /* exception for internal modules */ |
Michal Vasko | 2d051a1 | 2017-04-21 09:28:57 +0200 | [diff] [blame^] | 599 | for (i = 0; i < LY_INTERNAL_MODULE_COUNT; i++) { |
Michal Vasko | 8447515 | 2016-07-25 16:16:25 +0200 | [diff] [blame] | 600 | if (ly_strequal(name, internal_modules[i].name, 0)) { |
| 601 | if (!revision || ly_strequal(revision, internal_modules[i].revision, 0)) { |
| 602 | /* return internal module */ |
| 603 | return (struct lys_module *)ly_ctx_get_module(ctx, name, revision); |
| 604 | } |
Radek Krejci | 0320341 | 2016-06-23 15:20:53 +0200 | [diff] [blame] | 605 | } |
| 606 | } |
Radek Krejci | bf4e465 | 2016-10-21 15:44:13 +0200 | [diff] [blame] | 607 | if (revision) { |
Radek Krejci | 0ec51da | 2016-12-14 16:42:03 +0100 | [diff] [blame] | 608 | /* try to get the schema with the specific revision from the context, |
| 609 | * include the disabled modules in the search to avoid their duplication, |
| 610 | * they are enabled by the subsequent call to lys_set_implemented() */ |
Michal Vasko | 2d051a1 | 2017-04-21 09:28:57 +0200 | [diff] [blame^] | 611 | for (i = LY_INTERNAL_MODULE_COUNT, mod = NULL; i < ctx->models.used; i++) { |
Radek Krejci | 0ec51da | 2016-12-14 16:42:03 +0100 | [diff] [blame] | 612 | mod = ctx->models.list[i]; /* shortcut */ |
| 613 | if (ly_strequal(name, mod->name, 0) && mod->rev_size && !strcmp(revision, mod->rev[0].date)) { |
| 614 | break; |
| 615 | } |
| 616 | mod = NULL; |
| 617 | } |
Radek Krejci | bf4e465 | 2016-10-21 15:44:13 +0200 | [diff] [blame] | 618 | if (mod) { |
| 619 | /* we get such a module, make it implemented */ |
| 620 | if (lys_set_implemented(mod)) { |
| 621 | /* the schema cannot be implemented */ |
| 622 | mod = NULL; |
| 623 | } |
| 624 | return mod; |
| 625 | } |
| 626 | } |
Radek Krejci | 0320341 | 2016-06-23 15:20:53 +0200 | [diff] [blame] | 627 | } |
| 628 | |
Michal Vasko | f53187d | 2017-01-13 13:23:14 +0100 | [diff] [blame] | 629 | if (ctx->imp_clb) { |
Michal Vasko | 8447515 | 2016-07-25 16:16:25 +0200 | [diff] [blame] | 630 | if (module) { |
| 631 | mod = lys_main_module(module); |
Michal Vasko | f53187d | 2017-01-13 13:23:14 +0100 | [diff] [blame] | 632 | module_data = ctx->imp_clb(mod->name, (mod->rev_size ? mod->rev[0].date : NULL), name, revision, ctx->imp_clb_data, &format, &module_data_free); |
Michal Vasko | 8447515 | 2016-07-25 16:16:25 +0200 | [diff] [blame] | 633 | } else { |
Michal Vasko | f53187d | 2017-01-13 13:23:14 +0100 | [diff] [blame] | 634 | module_data = ctx->imp_clb(name, revision, NULL, NULL, ctx->imp_clb_data, &format, &module_data_free); |
Michal Vasko | 8447515 | 2016-07-25 16:16:25 +0200 | [diff] [blame] | 635 | } |
Michal Vasko | 99b0aad | 2015-12-01 12:28:51 +0100 | [diff] [blame] | 636 | if (!module_data) { |
Radek Krejci | bf4e465 | 2016-10-21 15:44:13 +0200 | [diff] [blame] | 637 | if (module || revision) { |
| 638 | /* we already know that the specified revision is not present in context, and we have no other |
| 639 | * option in case of submodules */ |
| 640 | LOGERR(LY_ESYS, "User module retrieval callback failed!"); |
| 641 | return NULL; |
| 642 | } else { |
| 643 | /* get the newest revision from the context */ |
Radek Krejci | 0ec51da | 2016-12-14 16:42:03 +0100 | [diff] [blame] | 644 | mod = ly_ctx_get_module_by(ctx, name, offsetof(struct lys_module, name), revision, 1); |
| 645 | if (mod && mod->disabled) { |
| 646 | /* enable the required module */ |
| 647 | lys_set_enabled(mod); |
| 648 | } |
| 649 | return mod; |
Radek Krejci | bf4e465 | 2016-10-21 15:44:13 +0200 | [diff] [blame] | 650 | } |
Michal Vasko | 8246596 | 2015-11-10 11:03:11 +0100 | [diff] [blame] | 651 | } |
Michal Vasko | 8447515 | 2016-07-25 16:16:25 +0200 | [diff] [blame] | 652 | |
| 653 | if (module) { |
Michal Vasko | 5b99871 | 2017-01-26 10:34:06 +0100 | [diff] [blame] | 654 | mod = (struct lys_module *)lys_sub_parse_mem(module, module_data, format, unres); |
Michal Vasko | 8447515 | 2016-07-25 16:16:25 +0200 | [diff] [blame] | 655 | } else { |
Michal Vasko | 2924566 | 2017-04-18 15:56:31 +0200 | [diff] [blame] | 656 | mod = (struct lys_module *)lys_parse_mem_(ctx, module_data, format, 0, implement); |
Michal Vasko | 8447515 | 2016-07-25 16:16:25 +0200 | [diff] [blame] | 657 | } |
| 658 | |
Michal Vasko | 99b0aad | 2015-12-01 12:28:51 +0100 | [diff] [blame] | 659 | if (module_data_free) { |
| 660 | module_data_free(module_data); |
Michal Vasko | 8246596 | 2015-11-10 11:03:11 +0100 | [diff] [blame] | 661 | } |
Michal Vasko | 99b0aad | 2015-12-01 12:28:51 +0100 | [diff] [blame] | 662 | } else { |
Michal Vasko | 58e5f3e | 2016-07-28 11:06:34 +0200 | [diff] [blame] | 663 | mod = lyp_search_file(ctx, module, name, revision, implement, unres); |
Michal Vasko | 8246596 | 2015-11-10 11:03:11 +0100 | [diff] [blame] | 664 | } |
| 665 | |
Michal Vasko | 8447515 | 2016-07-25 16:16:25 +0200 | [diff] [blame] | 666 | return mod; |
| 667 | } |
| 668 | |
| 669 | API const struct lys_module * |
| 670 | ly_ctx_load_module(struct ly_ctx *ctx, const char *name, const char *revision) |
| 671 | { |
| 672 | if (!ctx || !name) { |
| 673 | ly_errno = LY_EINVAL; |
| 674 | return NULL; |
| 675 | } |
| 676 | |
Michal Vasko | 58e5f3e | 2016-07-28 11:06:34 +0200 | [diff] [blame] | 677 | return ly_ctx_load_sub_module(ctx, NULL, name, revision, 1, NULL); |
Michal Vasko | 8246596 | 2015-11-10 11:03:11 +0100 | [diff] [blame] | 678 | } |
| 679 | |
Radek Krejci | 85a54be | 2016-10-20 12:39:56 +0200 | [diff] [blame] | 680 | /* |
| 681 | * mods - set of removed modules, if NULL all modules are supposed to be removed so any backlink is invalid |
| 682 | */ |
Michal Vasko | 4be5fa2 | 2017-02-07 10:12:32 +0100 | [diff] [blame] | 683 | static void |
| 684 | ctx_modules_undo_backlinks(struct ly_ctx *ctx, struct ly_set *mods) |
Radek Krejci | 85a54be | 2016-10-20 12:39:56 +0200 | [diff] [blame] | 685 | { |
| 686 | int o; |
| 687 | uint8_t j; |
| 688 | unsigned int u, v; |
| 689 | struct lys_module *mod; |
| 690 | struct lys_node *elem, *next; |
| 691 | struct lys_node_leaf *leaf; |
| 692 | |
| 693 | /* maintain backlinks (start with internal ietf-yang-library which have leafs as possible targets of leafrefs */ |
Michal Vasko | 2d051a1 | 2017-04-21 09:28:57 +0200 | [diff] [blame^] | 694 | for (o = LY_INTERNAL_MODULE_COUNT - 1; o < ctx->models.used; o++) { |
Radek Krejci | 85a54be | 2016-10-20 12:39:56 +0200 | [diff] [blame] | 695 | mod = ctx->models.list[o]; /* shortcut */ |
| 696 | |
| 697 | /* 1) features */ |
| 698 | for (j = 0; j < mod->features_size; j++) { |
| 699 | if (!mod->features[j].depfeatures) { |
| 700 | continue; |
| 701 | } |
| 702 | for (v = 0; v < mod->features[j].depfeatures->number; v++) { |
| 703 | if (!mods || ly_set_contains(mods, ((struct lys_feature *)mod->features[j].depfeatures->set.g[v])->module) != -1) { |
| 704 | /* depending feature is in module to remove */ |
| 705 | ly_set_rm_index(mod->features[j].depfeatures, v); |
| 706 | v--; |
| 707 | } |
| 708 | } |
| 709 | if (!mod->features[j].depfeatures->number) { |
| 710 | /* all backlinks removed */ |
| 711 | ly_set_free(mod->features[j].depfeatures); |
| 712 | mod->features[j].depfeatures = NULL; |
| 713 | } |
| 714 | } |
Michal Vasko | 4be5fa2 | 2017-02-07 10:12:32 +0100 | [diff] [blame] | 715 | |
| 716 | /* 2) identities */ |
Radek Krejci | 85a54be | 2016-10-20 12:39:56 +0200 | [diff] [blame] | 717 | for (u = 0; u < mod->ident_size; u++) { |
| 718 | if (!mod->ident[u].der) { |
| 719 | continue; |
| 720 | } |
| 721 | for (v = 0; v < mod->ident[u].der->number; v++) { |
| 722 | if (!mods || ly_set_contains(mods, ((struct lys_ident *)mod->ident[u].der->set.g[v])->module) != -1) { |
| 723 | /* derived identity is in module to remove */ |
| 724 | ly_set_rm_index(mod->ident[u].der, v); |
| 725 | v--; |
| 726 | } |
| 727 | } |
| 728 | if (!mod->ident[u].der->number) { |
| 729 | /* all backlinks removed */ |
| 730 | ly_set_free(mod->ident[u].der); |
| 731 | mod->ident[u].der = NULL; |
| 732 | } |
| 733 | } |
| 734 | |
Michal Vasko | 4be5fa2 | 2017-02-07 10:12:32 +0100 | [diff] [blame] | 735 | /* 3) leafrefs */ |
Radek Krejci | 85a54be | 2016-10-20 12:39:56 +0200 | [diff] [blame] | 736 | for (elem = next = mod->data; elem; elem = next) { |
| 737 | if (elem->nodetype & (LYS_LEAF | LYS_LEAFLIST)) { |
| 738 | leaf = (struct lys_node_leaf *)elem; /* shortcut */ |
| 739 | if (leaf->backlinks) { |
| 740 | if (!mods) { |
| 741 | /* remove all backlinks */ |
| 742 | ly_set_free(leaf->backlinks); |
| 743 | leaf->backlinks = NULL; |
| 744 | } else { |
| 745 | for (v = 0; v < leaf->backlinks->number; v++) { |
| 746 | if (ly_set_contains(mods, leaf->backlinks->set.s[v]->module) != -1) { |
| 747 | /* derived identity is in module to remove */ |
| 748 | ly_set_rm_index(leaf->backlinks, v); |
| 749 | v--; |
| 750 | } |
| 751 | } |
| 752 | if (!leaf->backlinks->number) { |
| 753 | /* all backlinks removed */ |
| 754 | ly_set_free(leaf->backlinks); |
| 755 | leaf->backlinks = NULL; |
| 756 | } |
| 757 | } |
| 758 | } |
| 759 | } |
| 760 | |
| 761 | /* select next element to process */ |
| 762 | next = elem->child; |
| 763 | /* child exception for leafs, leaflists, anyxml and groupings */ |
| 764 | if (elem->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_ANYDATA | LYS_GROUPING)) { |
| 765 | next = NULL; |
| 766 | } |
| 767 | if (!next) { |
| 768 | /* no children, try siblings */ |
| 769 | next = elem->next; |
| 770 | } |
| 771 | while (!next) { |
| 772 | /* parent is already processed, go to its sibling */ |
| 773 | elem = lys_parent(elem); |
| 774 | if (!elem) { |
| 775 | /* we are done, no next element to process */ |
| 776 | break; |
| 777 | } |
| 778 | /* no siblings, go back through parents */ |
| 779 | next = elem->next; |
| 780 | } |
| 781 | } |
| 782 | } |
Michal Vasko | 4be5fa2 | 2017-02-07 10:12:32 +0100 | [diff] [blame] | 783 | } |
Radek Krejci | 85a54be | 2016-10-20 12:39:56 +0200 | [diff] [blame] | 784 | |
Michal Vasko | 4be5fa2 | 2017-02-07 10:12:32 +0100 | [diff] [blame] | 785 | static int |
Radek Krejci | 83a4bac | 2017-02-07 15:53:04 +0100 | [diff] [blame] | 786 | ctx_modules_redo_backlinks(struct ly_set *mods) |
Michal Vasko | 4be5fa2 | 2017-02-07 10:12:32 +0100 | [diff] [blame] | 787 | { |
Radek Krejci | 83a4bac | 2017-02-07 15:53:04 +0100 | [diff] [blame] | 788 | unsigned int i, j, k, s; |
| 789 | struct lys_module *mod; |
| 790 | struct lys_node *next, *elem; |
| 791 | struct lys_type *type; |
| 792 | struct lys_feature *feat; |
Michal Vasko | 4be5fa2 | 2017-02-07 10:12:32 +0100 | [diff] [blame] | 793 | |
Michal Vasko | 4be5fa2 | 2017-02-07 10:12:32 +0100 | [diff] [blame] | 794 | for (i = 0; i < mods->number; ++i) { |
Radek Krejci | 83a4bac | 2017-02-07 15:53:04 +0100 | [diff] [blame] | 795 | mod = (struct lys_module *)mods->set.g[i]; /* shortcut */ |
Michal Vasko | 4be5fa2 | 2017-02-07 10:12:32 +0100 | [diff] [blame] | 796 | |
Radek Krejci | 83a4bac | 2017-02-07 15:53:04 +0100 | [diff] [blame] | 797 | /* identities */ |
| 798 | for (j = 0; j < mod->ident_size; j++) { |
| 799 | for (k = 0; k < mod->ident[j].base_size; k++) { |
| 800 | resolve_identity_backlink_update(&mod->ident[j], mod->ident[j].base[k]); |
| 801 | } |
| 802 | } |
Michal Vasko | 4be5fa2 | 2017-02-07 10:12:32 +0100 | [diff] [blame] | 803 | |
Radek Krejci | 83a4bac | 2017-02-07 15:53:04 +0100 | [diff] [blame] | 804 | /* features */ |
| 805 | for (j = 0; j < mod->features_size; j++) { |
| 806 | for (k = 0; k < mod->features[j].iffeature_size; k++) { |
| 807 | resolve_iffeature_getsizes(&mod->features[j].iffeature[k], NULL, &s); |
| 808 | while (s--) { |
| 809 | feat = mod->features[j].iffeature[k].features[s]; /* shortcut */ |
| 810 | if (!feat->depfeatures) { |
| 811 | feat->depfeatures = ly_set_new(); |
| 812 | } |
| 813 | ly_set_add(feat->depfeatures, &mod->features[j], LY_SET_OPT_USEASLIST); |
| 814 | } |
| 815 | } |
| 816 | } |
| 817 | |
| 818 | /* leafrefs */ |
| 819 | LY_TREE_DFS_BEGIN(mod->data, next, elem) { |
Michal Vasko | fa3d2f7 | 2017-04-10 13:30:44 +0200 | [diff] [blame] | 820 | if (elem->nodetype == LYS_GROUPING) { |
| 821 | goto next_sibling; |
| 822 | } |
| 823 | |
Radek Krejci | 83a4bac | 2017-02-07 15:53:04 +0100 | [diff] [blame] | 824 | if (elem->nodetype & (LYS_LEAF | LYS_LEAFLIST)) { |
| 825 | type = &((struct lys_node_leaf *)elem)->type; /* shortcut */ |
| 826 | if (type->base == LY_TYPE_LEAFREF) { |
| 827 | lys_leaf_add_leafref_target(type->info.lref.target, elem); |
| 828 | } |
| 829 | } |
| 830 | |
Michal Vasko | fa3d2f7 | 2017-04-10 13:30:44 +0200 | [diff] [blame] | 831 | /* select element for the next run - children first */ |
| 832 | next = elem->child; |
| 833 | |
| 834 | /* child exception for leafs, leaflists and anyxml without children */ |
| 835 | if (elem->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_ANYDATA)) { |
| 836 | next = NULL; |
| 837 | } |
| 838 | if (!next) { |
| 839 | next_sibling: |
| 840 | /* no children */ |
| 841 | if (elem == mod->data) { |
| 842 | /* we are done, (START) has no children */ |
| 843 | break; |
| 844 | } |
| 845 | /* try siblings */ |
| 846 | next = elem->next; |
| 847 | } |
| 848 | while (!next) { |
| 849 | /* parent is already processed, go to its sibling */ |
| 850 | elem = lys_parent(elem); |
| 851 | |
| 852 | /* no siblings, go back through parents */ |
| 853 | if (lys_parent(elem) == lys_parent(mod->data)) { |
| 854 | /* we are done, no next element to process */ |
| 855 | break; |
| 856 | } |
| 857 | next = elem->next; |
| 858 | } |
Radek Krejci | 83a4bac | 2017-02-07 15:53:04 +0100 | [diff] [blame] | 859 | } |
Michal Vasko | 4be5fa2 | 2017-02-07 10:12:32 +0100 | [diff] [blame] | 860 | } |
| 861 | |
| 862 | return 0; |
Radek Krejci | 85a54be | 2016-10-20 12:39:56 +0200 | [diff] [blame] | 863 | } |
| 864 | |
Radek Krejci | 8c107fe | 2016-10-17 16:00:18 +0200 | [diff] [blame] | 865 | API int |
Radek Krejci | 0ec51da | 2016-12-14 16:42:03 +0100 | [diff] [blame] | 866 | lys_set_disabled(const struct lys_module *module) |
| 867 | { |
| 868 | struct ly_ctx *ctx; /* shortcut */ |
| 869 | struct lys_module *mod; |
| 870 | struct ly_set *mods; |
| 871 | uint8_t j, imported; |
| 872 | int i, o; |
| 873 | unsigned int u; |
| 874 | |
| 875 | if (!module) { |
| 876 | ly_errno = LY_EINVAL; |
| 877 | return EXIT_FAILURE; |
| 878 | } else if (module->disabled) { |
| 879 | /* already disabled module */ |
| 880 | return EXIT_SUCCESS; |
| 881 | } |
| 882 | mod = (struct lys_module *)module; |
| 883 | ctx = mod->ctx; |
| 884 | |
| 885 | /* avoid disabling internal modules */ |
Michal Vasko | 2d051a1 | 2017-04-21 09:28:57 +0200 | [diff] [blame^] | 886 | for (i = 0; i < LY_INTERNAL_MODULE_COUNT; i++) { |
Radek Krejci | 0ec51da | 2016-12-14 16:42:03 +0100 | [diff] [blame] | 887 | if (mod == ctx->models.list[i]) { |
Michal Vasko | 2d051a1 | 2017-04-21 09:28:57 +0200 | [diff] [blame^] | 888 | LOGERR(LY_EINVAL, "Internal module \"%s\" cannot be disabled.", mod->name); |
Radek Krejci | 0ec51da | 2016-12-14 16:42:03 +0100 | [diff] [blame] | 889 | return EXIT_FAILURE; |
| 890 | } |
| 891 | } |
| 892 | |
| 893 | /* disable the module */ |
| 894 | mod->disabled = 1; |
| 895 | |
| 896 | /* get the complete list of modules to disable because of dependencies, |
| 897 | * we are going also to disable all the imported (not implemented) modules |
| 898 | * that are not used in any other module */ |
| 899 | mods = ly_set_new(); |
| 900 | ly_set_add(mods, mod, 0); |
| 901 | checkdependency: |
Michal Vasko | 2d051a1 | 2017-04-21 09:28:57 +0200 | [diff] [blame^] | 902 | for (i = LY_INTERNAL_MODULE_COUNT; i < ctx->models.used; i++) { |
Radek Krejci | 0ec51da | 2016-12-14 16:42:03 +0100 | [diff] [blame] | 903 | mod = ctx->models.list[i]; /* shortcut */ |
| 904 | if (mod->disabled) { |
| 905 | /* skip the already disabled modules */ |
| 906 | continue; |
| 907 | } |
| 908 | |
| 909 | /* check depndency of imported modules */ |
| 910 | for (j = 0; j < mod->imp_size; j++) { |
| 911 | for (u = 0; u < mods->number; u++) { |
| 912 | if (mod->imp[j].module == mods->set.g[u]) { |
| 913 | /* module is importing some module to disable, so it must be also disabled */ |
| 914 | mod->disabled = 1; |
| 915 | ly_set_add(mods, mod, 0); |
| 916 | /* we have to start again because some of the already checked modules can |
| 917 | * depend on the one we have just decided to disable */ |
| 918 | goto checkdependency; |
| 919 | } |
| 920 | } |
| 921 | } |
| 922 | /* check if the imported module is used in any module supposed to be kept */ |
| 923 | if (!mod->implemented) { |
| 924 | imported = 0; |
Michal Vasko | 2d051a1 | 2017-04-21 09:28:57 +0200 | [diff] [blame^] | 925 | for (o = LY_INTERNAL_MODULE_COUNT; o < ctx->models.used; o++) { |
Radek Krejci | 0ec51da | 2016-12-14 16:42:03 +0100 | [diff] [blame] | 926 | if (ctx->models.list[o]->disabled) { |
| 927 | /* skip modules already disabled */ |
| 928 | continue; |
| 929 | } |
| 930 | for (j = 0; j < ctx->models.list[o]->imp_size; j++) { |
| 931 | if (ctx->models.list[o]->imp[j].module == mod) { |
| 932 | /* the module is used in some other module not yet selected to be disabled */ |
| 933 | imported = 1; |
| 934 | goto imported; |
| 935 | } |
| 936 | } |
| 937 | } |
| 938 | imported: |
| 939 | if (!imported) { |
| 940 | /* module is not implemented and neither imported by any other module in context |
| 941 | * which is supposed to be kept enabled after this operation, so we are going to disable also |
| 942 | * this module */ |
| 943 | mod->disabled = 1; |
| 944 | ly_set_add(mods, mod, 0); |
| 945 | /* we have to start again, this time not because other module can depend on this one |
| 946 | * (we know that there is no such module), but because the module can import module |
| 947 | * that could became useless. If there are no imports, we can continue */ |
| 948 | if (mod->imp_size) { |
| 949 | goto checkdependency; |
| 950 | } |
| 951 | } |
| 952 | } |
| 953 | } |
| 954 | |
| 955 | /* before removing applied deviations, augments and updating leafrefs, we have to enable the modules |
| 956 | * to disable to allow all that operations */ |
| 957 | for (u = 0; u < mods->number; u++) { |
| 958 | ((struct lys_module *)mods->set.g[u])->disabled = 0; |
| 959 | } |
| 960 | |
| 961 | /* maintain backlinks (start with internal ietf-yang-library which have leafs as possible targets of leafrefs */ |
Michal Vasko | 4be5fa2 | 2017-02-07 10:12:32 +0100 | [diff] [blame] | 962 | ctx_modules_undo_backlinks(ctx, mods); |
Radek Krejci | 0ec51da | 2016-12-14 16:42:03 +0100 | [diff] [blame] | 963 | |
| 964 | /* remove the applied deviations and augments */ |
| 965 | for (u = 0; u < mods->number; u++) { |
| 966 | lys_sub_module_remove_devs_augs((struct lys_module *)mods->set.g[u]); |
| 967 | } |
| 968 | |
| 969 | /* now again disable the modules to disable */ |
| 970 | for (u = 0; u < mods->number; u++) { |
| 971 | ((struct lys_module *)mods->set.g[u])->disabled = 1; |
| 972 | } |
| 973 | |
| 974 | /* free the set */ |
| 975 | ly_set_free(mods); |
| 976 | |
| 977 | /* update the module-set-id */ |
| 978 | ctx->models.module_set_id++; |
| 979 | |
| 980 | return EXIT_SUCCESS; |
| 981 | } |
| 982 | |
| 983 | static void |
| 984 | lys_set_enabled_(struct ly_set *mods, struct lys_module *mod) |
| 985 | { |
| 986 | unsigned int i; |
| 987 | |
| 988 | ly_set_add(mods, mod, 0); |
| 989 | mod->disabled = 0; |
| 990 | |
| 991 | /* go recursively */ |
| 992 | for (i = 0; i < mod->imp_size; i++) { |
| 993 | if (!mod->imp[i].module->disabled) { |
| 994 | continue; |
| 995 | } |
| 996 | |
| 997 | lys_set_enabled_(mods, mod->imp[i].module); |
| 998 | } |
| 999 | } |
| 1000 | |
| 1001 | API int |
| 1002 | lys_set_enabled(const struct lys_module *module) |
| 1003 | { |
| 1004 | struct ly_ctx *ctx; /* shortcut */ |
| 1005 | struct lys_module *mod; |
| 1006 | struct ly_set *mods, *disabled; |
| 1007 | int i; |
| 1008 | unsigned int u, v; |
| 1009 | |
| 1010 | if (!module) { |
| 1011 | ly_errno = LY_EINVAL; |
| 1012 | return EXIT_FAILURE; |
| 1013 | } else if (!module->disabled) { |
| 1014 | /* already enabled module */ |
| 1015 | return EXIT_SUCCESS; |
| 1016 | } |
| 1017 | mod = (struct lys_module *)module; |
| 1018 | ctx = mod->ctx; |
| 1019 | |
| 1020 | /* avoid disabling internal modules */ |
Michal Vasko | 2d051a1 | 2017-04-21 09:28:57 +0200 | [diff] [blame^] | 1021 | for (i = 0; i < LY_INTERNAL_MODULE_COUNT; i++) { |
Radek Krejci | 0ec51da | 2016-12-14 16:42:03 +0100 | [diff] [blame] | 1022 | if (mod == ctx->models.list[i]) { |
| 1023 | LOGERR(LY_EINVAL, "Internal module \"%s\" cannot be removed.", mod->name); |
| 1024 | return EXIT_FAILURE; |
| 1025 | } |
| 1026 | } |
| 1027 | |
| 1028 | mods = ly_set_new(); |
| 1029 | disabled = ly_set_new(); |
| 1030 | |
| 1031 | /* enable the module, including its dependencies */ |
| 1032 | lys_set_enabled_(mods, mod); |
| 1033 | |
| 1034 | /* we will go through the all disabled modules in the context, if the module has no dependency (import) |
| 1035 | * that is still disabled AND at least one of its imported module is from the set we are enabling now, |
| 1036 | * it is going to be also enabled. This way we try to revert everething that was possibly done by |
| 1037 | * lys_set_disabled(). */ |
| 1038 | checkdependency: |
Michal Vasko | 2d051a1 | 2017-04-21 09:28:57 +0200 | [diff] [blame^] | 1039 | for (i = LY_INTERNAL_MODULE_COUNT; i < ctx->models.used; i++) { |
Radek Krejci | 0ec51da | 2016-12-14 16:42:03 +0100 | [diff] [blame] | 1040 | mod = ctx->models.list[i]; /* shortcut */ |
| 1041 | if (!mod->disabled || ly_set_contains(disabled, mod) != -1) { |
| 1042 | /* skip the enabled modules */ |
| 1043 | continue; |
| 1044 | } |
| 1045 | |
| 1046 | /* check imported modules */ |
| 1047 | for (u = 0; u < mod->imp_size; u++) { |
| 1048 | if (mod->imp[u].module->disabled) { |
| 1049 | /* it has disabled dependency so it must stay disabled */ |
| 1050 | break; |
| 1051 | } |
| 1052 | } |
| 1053 | if (u < mod->imp_size) { |
| 1054 | /* it has disabled dependency, continue with the next module in the context */ |
| 1055 | continue; |
| 1056 | } |
| 1057 | |
| 1058 | /* get know if at least one of the imported modules is being enabled this time */ |
| 1059 | for (u = 0; u < mod->imp_size; u++) { |
| 1060 | for (v = 0; v < mods->number; v++) { |
| 1061 | if (mod->imp[u].module == mods->set.g[v]) { |
| 1062 | /* yes, it is, so they are connected and we are going to enable it as well, |
| 1063 | * it is not necessary to call recursive lys_set_enable_() because we already |
| 1064 | * know that there is no disabled import to enable */ |
| 1065 | mod->disabled = 0; |
| 1066 | ly_set_add(mods, mod, 0); |
| 1067 | /* we have to start again because some of the already checked modules can |
| 1068 | * depend on the one we have just decided to enable */ |
| 1069 | goto checkdependency; |
| 1070 | } |
| 1071 | } |
| 1072 | } |
| 1073 | |
| 1074 | /* this module is disabled, but it does not depend on any other disabled module and none |
| 1075 | * of its imports was not enabled in this call. No future enabling of the disabled module |
| 1076 | * will change this so we can remember the module and skip it next time we will have to go |
| 1077 | * through the all context because of the checkdependency goto. |
| 1078 | */ |
| 1079 | ly_set_add(disabled, mod, 0); |
| 1080 | } |
| 1081 | |
| 1082 | /* maintain backlinks (start with internal ietf-yang-library which have leafs as possible targets of leafrefs */ |
Radek Krejci | 83a4bac | 2017-02-07 15:53:04 +0100 | [diff] [blame] | 1083 | ctx_modules_redo_backlinks(mods); |
Radek Krejci | 0ec51da | 2016-12-14 16:42:03 +0100 | [diff] [blame] | 1084 | |
| 1085 | /* re-apply the deviations and augments */ |
| 1086 | for (v = 0; v < mods->number; v++) { |
| 1087 | lys_sub_module_apply_devs_augs((struct lys_module *)mods->set.g[v]); |
| 1088 | } |
| 1089 | |
| 1090 | /* free the sets */ |
| 1091 | ly_set_free(mods); |
| 1092 | ly_set_free(disabled); |
| 1093 | |
| 1094 | /* update the module-set-id */ |
| 1095 | ctx->models.module_set_id++; |
| 1096 | |
| 1097 | return EXIT_SUCCESS; |
| 1098 | } |
| 1099 | |
| 1100 | API int |
| 1101 | ly_ctx_remove_module(const struct lys_module *module, |
Radek Krejci | 8c107fe | 2016-10-17 16:00:18 +0200 | [diff] [blame] | 1102 | void (*private_destructor)(const struct lys_node *node, void *priv)) |
| 1103 | { |
Radek Krejci | 0ec51da | 2016-12-14 16:42:03 +0100 | [diff] [blame] | 1104 | struct ly_ctx *ctx; /* shortcut */ |
Radek Krejci | 8c107fe | 2016-10-17 16:00:18 +0200 | [diff] [blame] | 1105 | struct lys_module *mod = NULL; |
| 1106 | struct ly_set *mods; |
| 1107 | uint8_t j, imported; |
| 1108 | int i, o; |
| 1109 | unsigned int u; |
| 1110 | |
Radek Krejci | 0ec51da | 2016-12-14 16:42:03 +0100 | [diff] [blame] | 1111 | if (!module) { |
Radek Krejci | 8c107fe | 2016-10-17 16:00:18 +0200 | [diff] [blame] | 1112 | ly_errno = LY_EINVAL; |
| 1113 | return EXIT_FAILURE; |
| 1114 | } |
| 1115 | |
Radek Krejci | 0ec51da | 2016-12-14 16:42:03 +0100 | [diff] [blame] | 1116 | mod = (struct lys_module *)module; |
| 1117 | ctx = mod->ctx; |
| 1118 | |
Radek Krejci | 8c107fe | 2016-10-17 16:00:18 +0200 | [diff] [blame] | 1119 | /* avoid removing internal modules ... */ |
Michal Vasko | 2d051a1 | 2017-04-21 09:28:57 +0200 | [diff] [blame^] | 1120 | for (i = 0; i < LY_INTERNAL_MODULE_COUNT; i++) { |
Radek Krejci | 8c107fe | 2016-10-17 16:00:18 +0200 | [diff] [blame] | 1121 | if (mod == ctx->models.list[i]) { |
Radek Krejci | 0ec51da | 2016-12-14 16:42:03 +0100 | [diff] [blame] | 1122 | LOGERR(LY_EINVAL, "Internal module \"%s\" cannot be removed.", mod->name); |
Radek Krejci | 8c107fe | 2016-10-17 16:00:18 +0200 | [diff] [blame] | 1123 | return EXIT_FAILURE; |
| 1124 | } |
| 1125 | } |
| 1126 | /* ... and hide the module from the further processing of the context modules list */ |
Michal Vasko | 2d051a1 | 2017-04-21 09:28:57 +0200 | [diff] [blame^] | 1127 | for (i = LY_INTERNAL_MODULE_COUNT; i < ctx->models.used; i++) { |
Radek Krejci | 8c107fe | 2016-10-17 16:00:18 +0200 | [diff] [blame] | 1128 | if (mod == ctx->models.list[i]) { |
| 1129 | ctx->models.list[i] = NULL; |
| 1130 | break; |
| 1131 | } |
| 1132 | } |
| 1133 | |
| 1134 | /* get the complete list of modules to remove because of dependencies, |
| 1135 | * we are going also to remove all the imported (not implemented) modules |
| 1136 | * that are not used in any other module */ |
| 1137 | mods = ly_set_new(); |
| 1138 | ly_set_add(mods, mod, 0); |
| 1139 | checkdependency: |
Michal Vasko | 2d051a1 | 2017-04-21 09:28:57 +0200 | [diff] [blame^] | 1140 | for (i = LY_INTERNAL_MODULE_COUNT; i < ctx->models.used; i++) { |
Radek Krejci | 8c107fe | 2016-10-17 16:00:18 +0200 | [diff] [blame] | 1141 | mod = ctx->models.list[i]; /* shortcut */ |
| 1142 | if (!mod) { |
| 1143 | /* skip modules already selected for removing */ |
| 1144 | continue; |
| 1145 | } |
| 1146 | |
| 1147 | /* check depndency of imported modules */ |
| 1148 | for (j = 0; j < mod->imp_size; j++) { |
| 1149 | for (u = 0; u < mods->number; u++) { |
| 1150 | if (mod->imp[j].module == mods->set.g[u]) { |
| 1151 | /* module is importing some module to remove, so it must be also removed */ |
| 1152 | ly_set_add(mods, mod, 0); |
| 1153 | ctx->models.list[i] = NULL; |
| 1154 | /* we have to start again because some of the already checked modules can |
| 1155 | * depend on the one we have just decided to remove */ |
| 1156 | goto checkdependency; |
| 1157 | } |
| 1158 | } |
| 1159 | } |
| 1160 | /* check if the imported module is used in any module supposed to be kept */ |
| 1161 | if (!mod->implemented) { |
| 1162 | imported = 0; |
Michal Vasko | 2d051a1 | 2017-04-21 09:28:57 +0200 | [diff] [blame^] | 1163 | for (o = LY_INTERNAL_MODULE_COUNT; o < ctx->models.used; o++) { |
Radek Krejci | 8c107fe | 2016-10-17 16:00:18 +0200 | [diff] [blame] | 1164 | if (!ctx->models.list[o]) { |
| 1165 | /* skip modules already selected for removing */ |
| 1166 | continue; |
| 1167 | } |
| 1168 | for (j = 0; j < ctx->models.list[o]->imp_size; j++) { |
| 1169 | if (ctx->models.list[o]->imp[j].module == mod) { |
| 1170 | /* the module is used in some other module not yet selected to be deleted */ |
| 1171 | imported = 1; |
| 1172 | goto imported; |
| 1173 | } |
| 1174 | } |
| 1175 | } |
| 1176 | imported: |
| 1177 | if (!imported) { |
| 1178 | /* module is not implemented and neither imported by any other module in context |
| 1179 | * which is supposed to be kept after this operation, so we are going to remove also |
| 1180 | * this useless module */ |
| 1181 | ly_set_add(mods, mod, 0); |
| 1182 | ctx->models.list[i] = NULL; |
| 1183 | /* we have to start again, this time not because other module can depend on this one |
Radek Krejci | 0ec51da | 2016-12-14 16:42:03 +0100 | [diff] [blame] | 1184 | * (we know that there is no such module), but because the module can import module |
Radek Krejci | 8c107fe | 2016-10-17 16:00:18 +0200 | [diff] [blame] | 1185 | * that could became useless. If there are no imports, we can continue */ |
| 1186 | if (mod->imp_size) { |
| 1187 | goto checkdependency; |
| 1188 | } |
| 1189 | } |
| 1190 | } |
| 1191 | } |
| 1192 | |
Radek Krejci | 8c107fe | 2016-10-17 16:00:18 +0200 | [diff] [blame] | 1193 | |
| 1194 | /* consolidate the modules list */ |
Michal Vasko | 2d051a1 | 2017-04-21 09:28:57 +0200 | [diff] [blame^] | 1195 | for (i = o = LY_INTERNAL_MODULE_COUNT; i < ctx->models.used; i++) { |
Radek Krejci | 8c107fe | 2016-10-17 16:00:18 +0200 | [diff] [blame] | 1196 | if (ctx->models.list[o]) { |
| 1197 | /* used cell */ |
| 1198 | o++; |
| 1199 | } else { |
| 1200 | /* the current output cell is empty, move here an input cell */ |
| 1201 | ctx->models.list[o] = ctx->models.list[i]; |
| 1202 | ctx->models.list[i] = NULL; |
| 1203 | } |
| 1204 | } |
| 1205 | /* get the last used cell to get know the number of used */ |
| 1206 | while (!ctx->models.list[o]) { |
| 1207 | o--; |
| 1208 | } |
| 1209 | ctx->models.used = o + 1; |
| 1210 | ctx->models.module_set_id++; |
| 1211 | |
Radek Krejci | 85a54be | 2016-10-20 12:39:56 +0200 | [diff] [blame] | 1212 | /* maintain backlinks (start with internal ietf-yang-library which have leafs as possible targets of leafrefs */ |
Michal Vasko | 4be5fa2 | 2017-02-07 10:12:32 +0100 | [diff] [blame] | 1213 | ctx_modules_undo_backlinks(ctx, mods); |
Radek Krejci | 85a54be | 2016-10-20 12:39:56 +0200 | [diff] [blame] | 1214 | |
| 1215 | /* free the modules */ |
| 1216 | for (u = 0; u < mods->number; u++) { |
Radek Krejci | b2541a3 | 2016-12-12 16:45:57 +0100 | [diff] [blame] | 1217 | /* remove the applied deviations and augments */ |
| 1218 | lys_sub_module_remove_devs_augs((struct lys_module *)mods->set.g[u]); |
Radek Krejci | 85a54be | 2016-10-20 12:39:56 +0200 | [diff] [blame] | 1219 | /* remove the module */ |
| 1220 | lys_free((struct lys_module *)mods->set.g[u], private_destructor, 0); |
| 1221 | } |
| 1222 | ly_set_free(mods); |
| 1223 | |
Radek Krejci | 8c107fe | 2016-10-17 16:00:18 +0200 | [diff] [blame] | 1224 | return EXIT_SUCCESS; |
| 1225 | } |
| 1226 | |
Radek Krejci | 85a54be | 2016-10-20 12:39:56 +0200 | [diff] [blame] | 1227 | API void |
| 1228 | ly_ctx_clean(struct ly_ctx *ctx, void (*private_destructor)(const struct lys_node *node, void *priv)) |
| 1229 | { |
Radek Krejci | 85a54be | 2016-10-20 12:39:56 +0200 | [diff] [blame] | 1230 | if (!ctx) { |
| 1231 | return; |
| 1232 | } |
| 1233 | |
| 1234 | /* models list */ |
Michal Vasko | 2d051a1 | 2017-04-21 09:28:57 +0200 | [diff] [blame^] | 1235 | for (; ctx->models.used > LY_INTERNAL_MODULE_COUNT; ctx->models.used--) { |
Radek Krejci | fc8411a | 2017-02-03 10:26:05 +0100 | [diff] [blame] | 1236 | /* remove the applied deviations and augments */ |
Radek Krejci | c436a23 | 2017-02-08 14:43:11 +0100 | [diff] [blame] | 1237 | lys_sub_module_remove_devs_augs(ctx->models.list[ctx->models.used - 1]); |
Radek Krejci | fc8411a | 2017-02-03 10:26:05 +0100 | [diff] [blame] | 1238 | /* remove the module */ |
Radek Krejci | c436a23 | 2017-02-08 14:43:11 +0100 | [diff] [blame] | 1239 | lys_free(ctx->models.list[ctx->models.used - 1], private_destructor, 0); |
Radek Krejci | fc8411a | 2017-02-03 10:26:05 +0100 | [diff] [blame] | 1240 | /* clean it for safer future use */ |
Radek Krejci | c436a23 | 2017-02-08 14:43:11 +0100 | [diff] [blame] | 1241 | ctx->models.list[ctx->models.used - 1] = NULL; |
Radek Krejci | 85a54be | 2016-10-20 12:39:56 +0200 | [diff] [blame] | 1242 | } |
Radek Krejci | 85a54be | 2016-10-20 12:39:56 +0200 | [diff] [blame] | 1243 | ctx->models.module_set_id++; |
| 1244 | |
Radek Krejci | 83a4bac | 2017-02-07 15:53:04 +0100 | [diff] [blame] | 1245 | /* maintain backlinks (actually done only with ietf-yang-library since its leafs can be target of leafref) */ |
Michal Vasko | 4be5fa2 | 2017-02-07 10:12:32 +0100 | [diff] [blame] | 1246 | ctx_modules_undo_backlinks(ctx, NULL); |
Radek Krejci | 85a54be | 2016-10-20 12:39:56 +0200 | [diff] [blame] | 1247 | } |
| 1248 | |
Michal Vasko | d7957c0 | 2016-04-01 10:27:26 +0200 | [diff] [blame] | 1249 | API const struct lys_module * |
| 1250 | ly_ctx_get_module_iter(const struct ly_ctx *ctx, uint32_t *idx) |
| 1251 | { |
| 1252 | if (!ctx || !idx) { |
| 1253 | ly_errno = LY_EINVAL; |
| 1254 | return NULL; |
| 1255 | } |
| 1256 | |
Radek Krejci | 0ec51da | 2016-12-14 16:42:03 +0100 | [diff] [blame] | 1257 | for ( ; *idx < (unsigned)ctx->models.used; (*idx)++) { |
| 1258 | if (!ctx->models.list[(*idx)]->disabled) { |
| 1259 | return ctx->models.list[(*idx)++]; |
| 1260 | } |
| 1261 | } |
| 1262 | |
| 1263 | return NULL; |
| 1264 | } |
| 1265 | |
| 1266 | API const struct lys_module * |
| 1267 | ly_ctx_get_disabled_module_iter(const struct ly_ctx *ctx, uint32_t *idx) |
| 1268 | { |
| 1269 | if (!ctx || !idx) { |
| 1270 | ly_errno = LY_EINVAL; |
Michal Vasko | d7957c0 | 2016-04-01 10:27:26 +0200 | [diff] [blame] | 1271 | return NULL; |
| 1272 | } |
| 1273 | |
Radek Krejci | 0ec51da | 2016-12-14 16:42:03 +0100 | [diff] [blame] | 1274 | for ( ; *idx < (unsigned)ctx->models.used; (*idx)++) { |
| 1275 | if (ctx->models.list[(*idx)]->disabled) { |
| 1276 | return ctx->models.list[(*idx)++]; |
| 1277 | } |
| 1278 | } |
| 1279 | |
| 1280 | return NULL; |
Michal Vasko | d7957c0 | 2016-04-01 10:27:26 +0200 | [diff] [blame] | 1281 | } |
| 1282 | |
Michal Vasko | 209a622 | 2015-10-16 09:51:07 +0200 | [diff] [blame] | 1283 | static int |
| 1284 | ylib_feature(struct lyd_node *parent, struct lys_module *cur_mod) |
| 1285 | { |
| 1286 | int i, j; |
| 1287 | |
| 1288 | /* module features */ |
| 1289 | for (i = 0; i < cur_mod->features_size; ++i) { |
| 1290 | if (!(cur_mod->features[i].flags & LYS_FENABLED)) { |
| 1291 | continue; |
| 1292 | } |
| 1293 | |
Michal Vasko | 3e671b5 | 2015-10-23 16:23:15 +0200 | [diff] [blame] | 1294 | if (!lyd_new_leaf(parent, NULL, "feature", cur_mod->features[i].name)) { |
Michal Vasko | 209a622 | 2015-10-16 09:51:07 +0200 | [diff] [blame] | 1295 | return EXIT_FAILURE; |
| 1296 | } |
| 1297 | } |
| 1298 | |
| 1299 | /* submodule features */ |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 1300 | for (i = 0; i < cur_mod->inc_size && cur_mod->inc[i].submodule; ++i) { |
Michal Vasko | 209a622 | 2015-10-16 09:51:07 +0200 | [diff] [blame] | 1301 | for (j = 0; j < cur_mod->inc[i].submodule->features_size; ++j) { |
| 1302 | if (!(cur_mod->inc[i].submodule->features[j].flags & LYS_FENABLED)) { |
| 1303 | continue; |
| 1304 | } |
| 1305 | |
Michal Vasko | 3e671b5 | 2015-10-23 16:23:15 +0200 | [diff] [blame] | 1306 | if (!lyd_new_leaf(parent, NULL, "feature", cur_mod->inc[i].submodule->features[j].name)) { |
Michal Vasko | 209a622 | 2015-10-16 09:51:07 +0200 | [diff] [blame] | 1307 | return EXIT_FAILURE; |
| 1308 | } |
| 1309 | } |
| 1310 | } |
| 1311 | |
| 1312 | return EXIT_SUCCESS; |
| 1313 | } |
| 1314 | |
| 1315 | static int |
Michal Vasko | ff006c1 | 2016-02-17 11:15:19 +0100 | [diff] [blame] | 1316 | ylib_deviation(struct lyd_node *parent, struct lys_module *cur_mod) |
Michal Vasko | 97d8c6d | 2016-02-12 11:05:48 +0100 | [diff] [blame] | 1317 | { |
Michal Vasko | 89563fc | 2016-07-28 16:19:35 +0200 | [diff] [blame] | 1318 | uint32_t i = 0, j; |
| 1319 | const struct lys_module *mod; |
Michal Vasko | 209a622 | 2015-10-16 09:51:07 +0200 | [diff] [blame] | 1320 | struct lyd_node *cont; |
Michal Vasko | 89563fc | 2016-07-28 16:19:35 +0200 | [diff] [blame] | 1321 | const char *ptr; |
Michal Vasko | 209a622 | 2015-10-16 09:51:07 +0200 | [diff] [blame] | 1322 | |
Michal Vasko | 89563fc | 2016-07-28 16:19:35 +0200 | [diff] [blame] | 1323 | if (cur_mod->deviated) { |
| 1324 | while ((mod = ly_ctx_get_module_iter(cur_mod->ctx, &i))) { |
| 1325 | if (mod == cur_mod) { |
| 1326 | continue; |
Michal Vasko | 209a622 | 2015-10-16 09:51:07 +0200 | [diff] [blame] | 1327 | } |
Michal Vasko | 209a622 | 2015-10-16 09:51:07 +0200 | [diff] [blame] | 1328 | |
Michal Vasko | 89563fc | 2016-07-28 16:19:35 +0200 | [diff] [blame] | 1329 | for (j = 0; j < mod->deviation_size; ++j) { |
| 1330 | ptr = strstr(mod->deviation[j].target_name, cur_mod->name); |
| 1331 | if (ptr && ptr[strlen(cur_mod->name)] == ':') { |
| 1332 | cont = lyd_new(parent, NULL, "deviation"); |
| 1333 | if (!cont) { |
| 1334 | return EXIT_FAILURE; |
| 1335 | } |
| 1336 | |
| 1337 | if (!lyd_new_leaf(cont, NULL, "name", mod->name)) { |
| 1338 | return EXIT_FAILURE; |
| 1339 | } |
| 1340 | if (!lyd_new_leaf(cont, NULL, "revision", (mod->rev_size ? mod->rev[0].date : ""))) { |
| 1341 | return EXIT_FAILURE; |
| 1342 | } |
Michal Vasko | 0c2215b | 2016-08-25 14:18:43 +0200 | [diff] [blame] | 1343 | |
| 1344 | break; |
Michal Vasko | 89563fc | 2016-07-28 16:19:35 +0200 | [diff] [blame] | 1345 | } |
Michal Vasko | 209a622 | 2015-10-16 09:51:07 +0200 | [diff] [blame] | 1346 | } |
| 1347 | } |
| 1348 | } |
| 1349 | |
| 1350 | return EXIT_SUCCESS; |
| 1351 | } |
| 1352 | |
| 1353 | static int |
| 1354 | ylib_submodules(struct lyd_node *parent, struct lys_module *cur_mod) |
| 1355 | { |
| 1356 | int i; |
Radek Krejci | a77904e | 2016-02-25 16:23:45 +0100 | [diff] [blame] | 1357 | char *str; |
Radek Krejci | d972391 | 2016-09-16 17:06:06 +0200 | [diff] [blame] | 1358 | struct lyd_node *item; |
Michal Vasko | 209a622 | 2015-10-16 09:51:07 +0200 | [diff] [blame] | 1359 | |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 1360 | for (i = 0; i < cur_mod->inc_size && cur_mod->inc[i].submodule; ++i) { |
Radek Krejci | d972391 | 2016-09-16 17:06:06 +0200 | [diff] [blame] | 1361 | item = lyd_new(parent, NULL, "submodule"); |
Radek Krejci | 6e05cea | 2015-12-10 16:34:37 +0100 | [diff] [blame] | 1362 | if (!item) { |
Michal Vasko | 209a622 | 2015-10-16 09:51:07 +0200 | [diff] [blame] | 1363 | return EXIT_FAILURE; |
| 1364 | } |
| 1365 | |
Radek Krejci | 6e05cea | 2015-12-10 16:34:37 +0100 | [diff] [blame] | 1366 | if (!lyd_new_leaf(item, NULL, "name", cur_mod->inc[i].submodule->name)) { |
Michal Vasko | 209a622 | 2015-10-16 09:51:07 +0200 | [diff] [blame] | 1367 | return EXIT_FAILURE; |
| 1368 | } |
Radek Krejci | 6e05cea | 2015-12-10 16:34:37 +0100 | [diff] [blame] | 1369 | if (!lyd_new_leaf(item, NULL, "revision", (cur_mod->inc[i].submodule->rev_size ? |
Michal Vasko | 3e671b5 | 2015-10-23 16:23:15 +0200 | [diff] [blame] | 1370 | cur_mod->inc[i].submodule->rev[0].date : ""))) { |
Michal Vasko | 209a622 | 2015-10-16 09:51:07 +0200 | [diff] [blame] | 1371 | return EXIT_FAILURE; |
| 1372 | } |
Radek Krejci | a77904e | 2016-02-25 16:23:45 +0100 | [diff] [blame] | 1373 | if (cur_mod->inc[i].submodule->filepath) { |
Radek Krejci | 95aa201 | 2016-03-03 11:21:09 +0100 | [diff] [blame] | 1374 | if (asprintf(&str, "file://%s", cur_mod->inc[i].submodule->filepath) == -1) { |
Radek Krejci | a77904e | 2016-02-25 16:23:45 +0100 | [diff] [blame] | 1375 | LOGMEM; |
| 1376 | return EXIT_FAILURE; |
Radek Krejci | 6e65af9 | 2016-03-07 12:48:19 +0100 | [diff] [blame] | 1377 | } else if (!lyd_new_leaf(item, NULL, "schema", str)) { |
Radek Krejci | 19f9ede | 2016-02-25 16:29:21 +0100 | [diff] [blame] | 1378 | free(str); |
Radek Krejci | a77904e | 2016-02-25 16:23:45 +0100 | [diff] [blame] | 1379 | return EXIT_FAILURE; |
| 1380 | } |
Radek Krejci | 19f9ede | 2016-02-25 16:29:21 +0100 | [diff] [blame] | 1381 | free(str); |
Michal Vasko | 209a622 | 2015-10-16 09:51:07 +0200 | [diff] [blame] | 1382 | } |
| 1383 | } |
| 1384 | |
| 1385 | return EXIT_SUCCESS; |
| 1386 | } |
| 1387 | |
| 1388 | API struct lyd_node * |
| 1389 | ly_ctx_info(struct ly_ctx *ctx) |
| 1390 | { |
| 1391 | int i; |
| 1392 | char id[8]; |
Radek Krejci | a77904e | 2016-02-25 16:23:45 +0100 | [diff] [blame] | 1393 | char *str; |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 1394 | const struct lys_module *mod; |
Michal Vasko | 209a622 | 2015-10-16 09:51:07 +0200 | [diff] [blame] | 1395 | struct lyd_node *root, *cont; |
| 1396 | |
Michal Vasko | 7eccc6c | 2016-03-24 14:56:33 +0100 | [diff] [blame] | 1397 | if (!ctx) { |
| 1398 | ly_errno = LY_EINVAL; |
| 1399 | return NULL; |
| 1400 | } |
| 1401 | |
Radek Krejci | bd9e8d2 | 2016-02-03 14:11:48 +0100 | [diff] [blame] | 1402 | mod = ly_ctx_get_module(ctx, "ietf-yang-library", IETF_YANG_LIB_REV); |
| 1403 | if (!mod || !mod->data) { |
| 1404 | LOGINT; |
Michal Vasko | 209a622 | 2015-10-16 09:51:07 +0200 | [diff] [blame] | 1405 | return NULL; |
| 1406 | } |
| 1407 | |
Radek Krejci | bd9e8d2 | 2016-02-03 14:11:48 +0100 | [diff] [blame] | 1408 | root = lyd_new(NULL, mod, "modules-state"); |
Michal Vasko | 209a622 | 2015-10-16 09:51:07 +0200 | [diff] [blame] | 1409 | if (!root) { |
| 1410 | return NULL; |
| 1411 | } |
| 1412 | |
| 1413 | for (i = 0; i < ctx->models.used; ++i) { |
Radek Krejci | 0ec51da | 2016-12-14 16:42:03 +0100 | [diff] [blame] | 1414 | if (ctx->models.list[i]->disabled) { |
| 1415 | /* skip the disabled modules */ |
| 1416 | continue; |
| 1417 | } |
| 1418 | |
Michal Vasko | 209a622 | 2015-10-16 09:51:07 +0200 | [diff] [blame] | 1419 | cont = lyd_new(root, NULL, "module"); |
| 1420 | if (!cont) { |
| 1421 | lyd_free(root); |
| 1422 | return NULL; |
| 1423 | } |
| 1424 | |
Michal Vasko | 3e671b5 | 2015-10-23 16:23:15 +0200 | [diff] [blame] | 1425 | if (!lyd_new_leaf(cont, NULL, "name", ctx->models.list[i]->name)) { |
Michal Vasko | 209a622 | 2015-10-16 09:51:07 +0200 | [diff] [blame] | 1426 | lyd_free(root); |
| 1427 | return NULL; |
| 1428 | } |
Michal Vasko | 3e671b5 | 2015-10-23 16:23:15 +0200 | [diff] [blame] | 1429 | if (!lyd_new_leaf(cont, NULL, "revision", (ctx->models.list[i]->rev_size ? |
Michal Vasko | 209a622 | 2015-10-16 09:51:07 +0200 | [diff] [blame] | 1430 | ctx->models.list[i]->rev[0].date : ""))) { |
| 1431 | lyd_free(root); |
| 1432 | return NULL; |
| 1433 | } |
Radek Krejci | a77904e | 2016-02-25 16:23:45 +0100 | [diff] [blame] | 1434 | if (ctx->models.list[i]->filepath) { |
Radek Krejci | 15412ca | 2016-03-03 11:16:52 +0100 | [diff] [blame] | 1435 | if (asprintf(&str, "file://%s", ctx->models.list[i]->filepath) == -1) { |
Radek Krejci | a77904e | 2016-02-25 16:23:45 +0100 | [diff] [blame] | 1436 | LOGMEM; |
| 1437 | lyd_free(root); |
| 1438 | return NULL; |
| 1439 | } else if (!lyd_new_leaf(cont, NULL, "schema", str)) { |
Radek Krejci | 19f9ede | 2016-02-25 16:29:21 +0100 | [diff] [blame] | 1440 | free(str); |
Radek Krejci | a77904e | 2016-02-25 16:23:45 +0100 | [diff] [blame] | 1441 | lyd_free(root); |
| 1442 | return NULL; |
| 1443 | } |
Radek Krejci | 19f9ede | 2016-02-25 16:29:21 +0100 | [diff] [blame] | 1444 | free(str); |
Michal Vasko | 209a622 | 2015-10-16 09:51:07 +0200 | [diff] [blame] | 1445 | } |
Michal Vasko | 3e671b5 | 2015-10-23 16:23:15 +0200 | [diff] [blame] | 1446 | if (!lyd_new_leaf(cont, NULL, "namespace", ctx->models.list[i]->ns)) { |
Michal Vasko | 209a622 | 2015-10-16 09:51:07 +0200 | [diff] [blame] | 1447 | lyd_free(root); |
| 1448 | return NULL; |
| 1449 | } |
| 1450 | if (ylib_feature(cont, ctx->models.list[i])) { |
| 1451 | lyd_free(root); |
| 1452 | return NULL; |
| 1453 | } |
Michal Vasko | ff006c1 | 2016-02-17 11:15:19 +0100 | [diff] [blame] | 1454 | if (ylib_deviation(cont, ctx->models.list[i])) { |
Michal Vasko | 209a622 | 2015-10-16 09:51:07 +0200 | [diff] [blame] | 1455 | lyd_free(root); |
| 1456 | return NULL; |
| 1457 | } |
| 1458 | if (ctx->models.list[i]->implemented |
Radek Krejci | bd9e8d2 | 2016-02-03 14:11:48 +0100 | [diff] [blame] | 1459 | && !lyd_new_leaf(cont, NULL, "conformance-type", "implement")) { |
Michal Vasko | 209a622 | 2015-10-16 09:51:07 +0200 | [diff] [blame] | 1460 | lyd_free(root); |
| 1461 | return NULL; |
| 1462 | } |
| 1463 | if (!ctx->models.list[i]->implemented |
Radek Krejci | bd9e8d2 | 2016-02-03 14:11:48 +0100 | [diff] [blame] | 1464 | && !lyd_new_leaf(cont, NULL, "conformance-type", "import")) { |
Michal Vasko | 209a622 | 2015-10-16 09:51:07 +0200 | [diff] [blame] | 1465 | lyd_free(root); |
| 1466 | return NULL; |
| 1467 | } |
| 1468 | if (ylib_submodules(cont, ctx->models.list[i])) { |
| 1469 | lyd_free(root); |
| 1470 | return NULL; |
| 1471 | } |
| 1472 | } |
| 1473 | |
| 1474 | sprintf(id, "%u", ctx->models.module_set_id); |
Michal Vasko | 3e671b5 | 2015-10-23 16:23:15 +0200 | [diff] [blame] | 1475 | if (!lyd_new_leaf(root, mod, "module-set-id", id)) { |
Michal Vasko | 209a622 | 2015-10-16 09:51:07 +0200 | [diff] [blame] | 1476 | lyd_free(root); |
| 1477 | return NULL; |
| 1478 | } |
| 1479 | |
Michal Vasko | cdb9017 | 2016-09-13 09:34:36 +0200 | [diff] [blame] | 1480 | if (lyd_validate(&root, LYD_OPT_NOSIBLINGS, NULL)) { |
Michal Vasko | 209a622 | 2015-10-16 09:51:07 +0200 | [diff] [blame] | 1481 | lyd_free(root); |
| 1482 | return NULL; |
| 1483 | } |
| 1484 | |
| 1485 | return root; |
| 1486 | } |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1487 | |
| 1488 | API const struct lys_node * |
Michal Vasko | 3547c53 | 2016-03-14 09:40:50 +0100 | [diff] [blame] | 1489 | ly_ctx_get_node(struct ly_ctx *ctx, const struct lys_node *start, const char *nodeid) |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1490 | { |
Michal Vasko | e733d68 | 2016-03-14 09:08:27 +0100 | [diff] [blame] | 1491 | const struct lys_node *node; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1492 | |
Michal Vasko | 3547c53 | 2016-03-14 09:40:50 +0100 | [diff] [blame] | 1493 | if (!ctx || !nodeid || ((nodeid[0] != '/') && !start)) { |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1494 | ly_errno = LY_EINVAL; |
| 1495 | return NULL; |
| 1496 | } |
| 1497 | |
Michal Vasko | e733d68 | 2016-03-14 09:08:27 +0100 | [diff] [blame] | 1498 | /* sets error and everything */ |
Michal Vasko | 8d26e5c | 2016-09-08 10:03:49 +0200 | [diff] [blame] | 1499 | node = resolve_json_nodeid(nodeid, ctx, start); |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1500 | |
Michal Vasko | e733d68 | 2016-03-14 09:08:27 +0100 | [diff] [blame] | 1501 | return node; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1502 | } |