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