blob: 338c8249057cad422945287305afa1c6c5856ace [file] [log] [blame]
Radek Krejci0af5f5d2018-09-07 15:00:30 +02001/**
2 * @file context.c
3 * @author Radek Krejci <rkrejci@cesnet.cz>
4 * @brief Context implementations
5 *
6 * Copyright (c) 2015 - 2018 CESNET, z.s.p.o.
7 *
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
11 *
12 * https://opensource.org/licenses/BSD-3-Clause
13 */
Radek Krejci535ea9f2020-05-29 16:01:05 +020014#define _GNU_SOURCE /* asprintf */
Radek Krejci0af5f5d2018-09-07 15:00:30 +020015
Radek Krejci535ea9f2020-05-29 16:01:05 +020016#include "context.h"
Radek Krejcib7db73a2018-10-24 14:18:40 +020017
Radek Krejcie7b95092019-05-15 11:03:07 +020018#include <assert.h>
Radek Krejci0af5f5d2018-09-07 15:00:30 +020019#include <errno.h>
Radek Krejcie7b95092019-05-15 11:03:07 +020020#include <pthread.h>
21#include <stddef.h>
Radek Krejci535ea9f2020-05-29 16:01:05 +020022#include <stdio.h>
Radek Krejci7ada9a02018-09-07 15:34:05 +020023#include <stdlib.h>
24#include <string.h>
Radek Krejci0e212402018-09-20 11:29:38 +020025#include <sys/stat.h>
Radek Krejci0af5f5d2018-09-07 15:00:30 +020026#include <unistd.h>
27
Radek Krejci535ea9f2020-05-29 16:01:05 +020028#include "common.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020029#include "hash_table.h"
30#include "set.h"
31#include "tree.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020032#include "tree_data.h"
Radek Krejci86d106e2018-10-18 09:53:19 +020033#include "tree_schema_internal.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020034#include "plugins_types.h"
Radek Krejci0af5f5d2018-09-07 15:00:30 +020035
Radek Krejci86d106e2018-10-18 09:53:19 +020036#define LY_INTERNAL_MODS_COUNT 6
Radek Krejci0af5f5d2018-09-07 15:00:30 +020037
Radek Krejcif3f47842018-11-15 11:22:15 +010038#include "../models/ietf-yang-metadata@2016-08-05.h"
39#include "../models/yang@2017-02-20.h"
40#include "../models/ietf-inet-types@2013-07-15.h"
41#include "../models/ietf-yang-types@2013-07-15.h"
Michal Vaskocf296c82020-05-27 11:16:45 +020042#include "../models/ietf-datastores@2018-02-14.h"
43#include "../models/ietf-yang-library@2019-01-04.h"
44#define IETF_YANG_LIB_REV "2019-01-04"
Radek Krejci0af5f5d2018-09-07 15:00:30 +020045
Radek Krejci0af5f5d2018-09-07 15:00:30 +020046static struct internal_modules_s {
47 const char *name;
48 const char *revision;
49 const char *data;
50 uint8_t implemented;
51 LYS_INFORMAT format;
52} internal_modules[LY_INTERNAL_MODS_COUNT] = {
Radek Krejci335332a2019-09-05 13:03:35 +020053 {"ietf-yang-metadata", "2016-08-05", (const char*)ietf_yang_metadata_2016_08_05_yang, 1, LYS_IN_YANG},
Radek Krejci86d106e2018-10-18 09:53:19 +020054 {"yang", "2017-02-20", (const char*)yang_2017_02_20_yang, 1, LYS_IN_YANG},
55 {"ietf-inet-types", "2013-07-15", (const char*)ietf_inet_types_2013_07_15_yang, 0, LYS_IN_YANG},
56 {"ietf-yang-types", "2013-07-15", (const char*)ietf_yang_types_2013_07_15_yang, 0, LYS_IN_YANG},
Radek Krejci0af5f5d2018-09-07 15:00:30 +020057 /* ietf-datastores and ietf-yang-library must be right here at the end of the list! */
Michal Vaskocf296c82020-05-27 11:16:45 +020058 {"ietf-datastores", "2018-02-14", (const char*)ietf_datastores_2018_02_14_yang, 1, LYS_IN_YANG},
59 {"ietf-yang-library", IETF_YANG_LIB_REV, (const char*)ietf_yang_library_2019_01_04_yang, 1, LYS_IN_YANG}
Radek Krejci0af5f5d2018-09-07 15:00:30 +020060};
61
62API LY_ERR
63ly_ctx_set_searchdir(struct ly_ctx *ctx, const char *search_dir)
64{
Radek Krejci0e212402018-09-20 11:29:38 +020065 struct stat st;
Radek Krejci0af5f5d2018-09-07 15:00:30 +020066 char *new_dir = NULL;
Radek Krejci0e212402018-09-20 11:29:38 +020067 unsigned int u;
Radek Krejci0af5f5d2018-09-07 15:00:30 +020068
69 LY_CHECK_ARG_RET(ctx, ctx, LY_EINVAL);
70
71 if (search_dir) {
Radek Krejci0af5f5d2018-09-07 15:00:30 +020072 new_dir = realpath(search_dir, NULL);
Radek Krejcia77e0e12018-09-20 12:39:15 +020073 LY_CHECK_ERR_RET(!new_dir,
Radek Krejci3a077b92019-04-09 17:10:35 +020074 LOGERR(ctx, LY_ESYS, "Unable to use search directory \"%s\" (%s).", search_dir, strerror(errno)),
75 LY_EINVAL);
76 if (strcmp(search_dir, new_dir)) {
77 LOGVRB("Canonicalizing search directory string from \"%s\" to \"%s\".", search_dir, new_dir);
78 }
79 LY_CHECK_ERR_RET(access(new_dir, R_OK | X_OK),
80 LOGERR(ctx, LY_ESYS, "Unable to fully access search directory \"%s\" (%s).", new_dir, strerror(errno)); free(new_dir),
81 LY_EINVAL);
82 LY_CHECK_ERR_RET(stat(new_dir, &st),
83 LOGERR(ctx, LY_ESYS, "stat() failed for \"%s\" (%s).", new_dir, strerror(errno)); free(new_dir),
Radek Krejcia77e0e12018-09-20 12:39:15 +020084 LY_ESYS);
Radek Krejci3a077b92019-04-09 17:10:35 +020085 LY_CHECK_ERR_RET(!S_ISDIR(st.st_mode),
86 LOGERR(ctx, LY_ESYS, "Given search directory \"%s\" is not a directory.", new_dir); free(new_dir),
87 LY_EINVAL);
Radek Krejci0e212402018-09-20 11:29:38 +020088 /* avoid path duplication */
89 for (u = 0; u < ctx->search_paths.count; ++u) {
Radek Krejci14946ab2018-09-20 13:42:06 +020090 if (!strcmp(new_dir, ctx->search_paths.objs[u])) {
Radek Krejci0e212402018-09-20 11:29:38 +020091 free(new_dir);
Radek Krejci14946ab2018-09-20 13:42:06 +020092 return LY_EEXIST;
Radek Krejci0e212402018-09-20 11:29:38 +020093 }
94 }
95 if (ly_set_add(&ctx->search_paths, new_dir, LY_SET_OPT_USEASLIST) == -1) {
Radek Krejci0af5f5d2018-09-07 15:00:30 +020096 free(new_dir);
97 return LY_EMEM;
98 }
99
Radek Krejcie9e987e2018-10-31 12:50:27 +0100100 /* new searchdir - possibly more latest revision available */
101 ly_ctx_reset_latests(ctx);
102
Radek Krejci0e212402018-09-20 11:29:38 +0200103 return LY_SUCCESS;
Radek Krejci0af5f5d2018-09-07 15:00:30 +0200104 } else {
105 /* consider that no change is not actually an error */
106 return LY_SUCCESS;
107 }
Radek Krejci0af5f5d2018-09-07 15:00:30 +0200108}
109
110API const char * const *
111ly_ctx_get_searchdirs(const struct ly_ctx *ctx)
112{
Radek Krejci05026432018-09-20 12:13:43 +0200113 void **new;
114
Radek Krejci0af5f5d2018-09-07 15:00:30 +0200115 LY_CHECK_ARG_RET(ctx, ctx, NULL);
Radek Krejci05026432018-09-20 12:13:43 +0200116
117 if (ctx->search_paths.count == ctx->search_paths.size) {
118 /* not enough space for terminating NULL byte */
119 new = realloc(((struct ly_ctx *)ctx)->search_paths.objs, (ctx->search_paths.size + 8) * sizeof *ctx->search_paths.objs);
120 LY_CHECK_ERR_RET(!new, LOGMEM(NULL), NULL);
121 ((struct ly_ctx *)ctx)->search_paths.size += 8;
122 ((struct ly_ctx *)ctx)->search_paths.objs = new;
123 }
124 /* set terminating NULL byte to the strings list */
125 ctx->search_paths.objs[ctx->search_paths.count] = NULL;
126
Radek Krejci0af5f5d2018-09-07 15:00:30 +0200127 return (const char * const *)ctx->search_paths.objs;
128}
129
130API LY_ERR
Radek Krejci0759b792018-09-20 13:53:15 +0200131ly_ctx_unset_searchdirs(struct ly_ctx *ctx, const char *value)
Radek Krejci0af5f5d2018-09-07 15:00:30 +0200132{
Radek Krejci0759b792018-09-20 13:53:15 +0200133 unsigned int index;
134
Radek Krejcicd649072018-09-20 12:14:45 +0200135 LY_CHECK_ARG_RET(ctx, ctx, LY_EINVAL);
Radek Krejcicd649072018-09-20 12:14:45 +0200136
Michal Vaskob34480a2018-09-17 10:34:45 +0200137 if (!ctx->search_paths.count) {
Radek Krejci0af5f5d2018-09-07 15:00:30 +0200138 return LY_SUCCESS;
139 }
140
Radek Krejci0759b792018-09-20 13:53:15 +0200141 if (value) {
Radek Krejci0af5f5d2018-09-07 15:00:30 +0200142 /* remove specific search directory */
Radek Krejci0759b792018-09-20 13:53:15 +0200143 for (index = 0; index < ctx->search_paths.count; ++index) {
144 if (!strcmp(value, ctx->search_paths.objs[index])) {
145 break;
146 }
147 }
148 if (index == ctx->search_paths.count) {
149 LOGARG(ctx, value);
150 return LY_EINVAL;
151 } else {
152 return ly_set_rm_index(&ctx->search_paths, index, free);
153 }
Radek Krejci0af5f5d2018-09-07 15:00:30 +0200154 } else {
155 /* remove them all */
Radek Krejcia40f21b2018-09-18 10:42:08 +0200156 ly_set_erase(&ctx->search_paths, free);
Radek Krejci0af5f5d2018-09-07 15:00:30 +0200157 memset(&ctx->search_paths, 0, sizeof ctx->search_paths);
158 }
159
160 return LY_SUCCESS;
161}
162
163API LY_ERR
Radek Krejcied5acc52019-04-25 15:57:04 +0200164ly_ctx_unset_searchdir(struct ly_ctx *ctx, unsigned int index)
165{
166 LY_CHECK_ARG_RET(ctx, ctx, LY_EINVAL);
167
168 if (!ctx->search_paths.count) {
169 return LY_SUCCESS;
170 }
171
172 if (index >= ctx->search_paths.count) {
173 LOGARG(ctx, value);
174 return LY_EINVAL;
175 } else {
176 return ly_set_rm_index(&ctx->search_paths, index, free);
177 }
178
179 return LY_SUCCESS;
180}
181
182API const struct lys_module *
183ly_ctx_load_module(struct ly_ctx *ctx, const char *name, const char *revision)
184{
185 struct lys_module *result = NULL;
186
187 LY_CHECK_ARG_RET(ctx, ctx, name, NULL);
188
189 LY_CHECK_RET(lysp_load_module(ctx, name, revision, 1, 0, &result), NULL);
190 return result;
191}
192
193API LY_ERR
Radek Krejci0af5f5d2018-09-07 15:00:30 +0200194ly_ctx_new(const char *search_dir, int options, struct ly_ctx **new_ctx)
195{
196 struct ly_ctx *ctx = NULL;
197 struct lys_module *module;
198 char *search_dir_list;
199 char *sep, *dir;
Radek Krejci86d106e2018-10-18 09:53:19 +0200200 int i;
Radek Krejci0af5f5d2018-09-07 15:00:30 +0200201 LY_ERR rc = LY_SUCCESS;
Radek Krejci0af5f5d2018-09-07 15:00:30 +0200202
Radek Krejcied5acc52019-04-25 15:57:04 +0200203 LY_CHECK_ARG_RET(NULL, new_ctx, LY_EINVAL);
204
Radek Krejci0af5f5d2018-09-07 15:00:30 +0200205 ctx = calloc(1, sizeof *ctx);
Radek Krejciad573502018-09-07 15:26:55 +0200206 LY_CHECK_ERR_RET(!ctx, LOGMEM(NULL), LY_EMEM);
Radek Krejci0af5f5d2018-09-07 15:00:30 +0200207
208 /* dictionary */
209 lydict_init(&ctx->dict);
210
Radek Krejciad573502018-09-07 15:26:55 +0200211#if 0 /* TODO when plugins implemented */
Radek Krejci0af5f5d2018-09-07 15:00:30 +0200212 /* plugins */
213 ly_load_plugins();
Radek Krejciad573502018-09-07 15:26:55 +0200214#endif
Radek Krejci0af5f5d2018-09-07 15:00:30 +0200215
216 /* initialize thread-specific key */
Radek Krejciad573502018-09-07 15:26:55 +0200217 while ((pthread_key_create(&ctx->errlist_key, ly_err_free)) == EAGAIN);
Radek Krejci0af5f5d2018-09-07 15:00:30 +0200218
219 /* models list */
220 ctx->flags = options;
221 if (search_dir) {
222 search_dir_list = strdup(search_dir);
223 LY_CHECK_ERR_GOTO(!search_dir_list, LOGMEM(NULL); rc = LY_EMEM, error);
224
225 for (dir = search_dir_list; (sep = strchr(dir, ':')) != NULL && rc == LY_SUCCESS; dir = sep + 1) {
226 *sep = 0;
227 rc = ly_ctx_set_searchdir(ctx, dir);
Radek Krejci14946ab2018-09-20 13:42:06 +0200228 if (rc == LY_EEXIST) {
229 /* ignore duplication */
230 rc = LY_SUCCESS;
231 }
Radek Krejci0af5f5d2018-09-07 15:00:30 +0200232 }
233 if (*dir && rc == LY_SUCCESS) {
234 rc = ly_ctx_set_searchdir(ctx, dir);
Radek Krejci14946ab2018-09-20 13:42:06 +0200235 if (rc == LY_EEXIST) {
236 /* ignore duplication */
237 rc = LY_SUCCESS;
238 }
Radek Krejci0af5f5d2018-09-07 15:00:30 +0200239 }
240 free(search_dir_list);
241
242 /* If ly_ctx_set_searchdir() failed, the error is already logged. Just exit */
243 if (rc != LY_SUCCESS) {
244 goto error;
245 }
246 }
247 ctx->module_set_id = 1;
248
249 /* load internal modules */
Radek Krejci86d106e2018-10-18 09:53:19 +0200250 for (i = 0; i < ((options & LY_CTX_NOYANGLIBRARY) ? (LY_INTERNAL_MODS_COUNT - 2) : LY_INTERNAL_MODS_COUNT); i++) {
Radek Krejci096235c2019-01-11 11:12:19 +0100251 module = (struct lys_module *)lys_parse_mem_module(ctx, internal_modules[i].data, internal_modules[i].format,
252 internal_modules[i].implemented, NULL, NULL);
Radek Krejci0e75e6f2018-11-08 09:40:02 +0100253 LY_CHECK_ERR_GOTO(!module, rc = ly_errcode(ctx), error);
Radek Krejci096235c2019-01-11 11:12:19 +0100254 LY_CHECK_GOTO((rc = lys_compile(module, 0)), error);
Radek Krejci0af5f5d2018-09-07 15:00:30 +0200255 }
256
257 *new_ctx = ctx;
258 return rc;
259
260error:
261 ly_ctx_destroy(ctx, NULL);
262 return rc;
263}
Radek Krejci0af5f5d2018-09-07 15:00:30 +0200264API int
Radek Krejci3fbe89a2018-09-20 13:54:46 +0200265ly_ctx_get_options(const struct ly_ctx *ctx)
Radek Krejci0af5f5d2018-09-07 15:00:30 +0200266{
Radek Krejci3fbe89a2018-09-20 13:54:46 +0200267 LY_CHECK_ARG_RET(ctx, ctx, 0);
Radek Krejci0af5f5d2018-09-07 15:00:30 +0200268 return ctx->flags;
269}
270
Radek Krejcica828d92018-09-20 14:19:43 +0200271API LY_ERR
Radek Krejci3fa46b62019-09-11 10:47:30 +0200272ly_ctx_set_options(struct ly_ctx *ctx, int option)
Radek Krejcica828d92018-09-20 14:19:43 +0200273{
274 LY_CHECK_ARG_RET(ctx, ctx, LY_EINVAL);
275 LY_CHECK_ERR_RET(option & LY_CTX_NOYANGLIBRARY, LOGARG(ctx, option), LY_EINVAL);
276
277 /* set the option(s) */
278 ctx->flags |= option;
279
280 return LY_SUCCESS;
281}
282
283API LY_ERR
Radek Krejci3fa46b62019-09-11 10:47:30 +0200284ly_ctx_unset_options(struct ly_ctx *ctx, int option)
Radek Krejcica828d92018-09-20 14:19:43 +0200285{
286 LY_CHECK_ARG_RET(ctx, ctx, LY_EINVAL);
287 LY_CHECK_ERR_RET(option & LY_CTX_NOYANGLIBRARY, LOGARG(ctx, option), LY_EINVAL);
288
289 /* unset the option(s) */
290 ctx->flags &= ~option;
291
292 return LY_SUCCESS;
293}
294
Radek Krejci0af5f5d2018-09-07 15:00:30 +0200295API uint16_t
296ly_ctx_get_module_set_id(const struct ly_ctx *ctx)
297{
Radek Krejci3fbe89a2018-09-20 13:54:46 +0200298 LY_CHECK_ARG_RET(ctx, ctx, 0);
Radek Krejci0af5f5d2018-09-07 15:00:30 +0200299 return ctx->module_set_id;
300}
301
Radek Krejcid33273d2018-10-25 14:55:52 +0200302API void
303ly_ctx_set_module_imp_clb(struct ly_ctx *ctx, ly_module_imp_clb clb, void *user_data)
304{
305 LY_CHECK_ARG_RET(ctx, ctx,);
306
307 ctx->imp_clb = clb;
308 ctx->imp_clb_data = user_data;
309}
310
311API ly_module_imp_clb
312ly_ctx_get_module_imp_clb(const struct ly_ctx *ctx, void **user_data)
313{
314 LY_CHECK_ARG_RET(ctx, ctx, NULL);
315
316 if (user_data) {
317 *user_data = ctx->imp_clb_data;
318 }
319 return ctx->imp_clb;
320}
321
Radek Krejcied5acc52019-04-25 15:57:04 +0200322API const struct lys_module *
323ly_ctx_get_module_iter(const struct ly_ctx *ctx, unsigned int *index)
324{
325 LY_CHECK_ARG_RET(ctx, ctx, index, NULL);
326
Radek Krejci2390fb52019-04-30 13:27:43 +0200327 if (*index < (unsigned)ctx->list.count) {
Radek Krejcied5acc52019-04-25 15:57:04 +0200328 return ctx->list.objs[(*index)++];
Radek Krejci2390fb52019-04-30 13:27:43 +0200329 } else {
330 return NULL;
Radek Krejcied5acc52019-04-25 15:57:04 +0200331 }
Radek Krejcied5acc52019-04-25 15:57:04 +0200332}
333
Radek Krejcib7db73a2018-10-24 14:18:40 +0200334/**
335 * @brief Iterate over the modules in the given context. Returned modules must match the given key at the offset of
336 * lysp_module and lysc_module structures (they are supposed to be placed at the same offset in both structures).
337 *
338 * @param[in] ctx Context where to iterate.
339 * @param[in] key Key value to search for.
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100340 * @param[in] key_offset Key's offset in struct lys_module to get value from the context's modules to match with the key.
Radek Krejcib7db73a2018-10-24 14:18:40 +0200341 * @param[in,out] Iterator to pass between the function calls. On the first call, the variable is supposed to be
342 * initiated to 0. After each call returning a module, the value is greater by 1 than the index of the returned
343 * module in the context.
344 * @return Module matching the given key, NULL if no such module found.
345 */
Radek Krejci0af46292019-01-11 16:02:31 +0100346static struct lys_module *
Radek Krejcib7db73a2018-10-24 14:18:40 +0200347ly_ctx_get_module_by_iter(const struct ly_ctx *ctx, const char *key, size_t key_offset, unsigned int *index)
348{
Radek Krejci0af46292019-01-11 16:02:31 +0100349 struct lys_module *mod;
Radek Krejcib7db73a2018-10-24 14:18:40 +0200350 const char *value;
351
352 for (; *index < ctx->list.count; ++(*index)) {
353 mod = ctx->list.objs[*index];
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100354 value = *(const char**)(((int8_t*)(mod)) + key_offset);
Radek Krejcib7db73a2018-10-24 14:18:40 +0200355 if (!strcmp(key, value)) {
356 /* increment index for the next run */
357 ++(*index);
358 return mod;
359 }
360 }
361 /* done */
362 return NULL;
363}
364
365/**
366 * @brief Unifying function for ly_ctx_get_module() and ly_ctx_get_module_ns()
367 * @param[in] ctx Context where to search.
368 * @param[in] key Name or Namespace as a search key.
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100369 * @param[in] key_offset Key's offset in struct lys_module to get value from the context's modules to match with the key.
Radek Krejcib7db73a2018-10-24 14:18:40 +0200370 * @param[in] revision Revision date to match. If NULL, the matching module must have no revision. To search for the latest
371 * revision module, use ly_ctx_get_module_latest_by().
372 * @return Matching module if any.
373 */
Radek Krejci0af46292019-01-11 16:02:31 +0100374static struct lys_module *
Radek Krejcib7db73a2018-10-24 14:18:40 +0200375ly_ctx_get_module_by(const struct ly_ctx *ctx, const char *key, size_t key_offset, const char *revision)
376{
Radek Krejci0af46292019-01-11 16:02:31 +0100377 struct lys_module *mod;
Radek Krejcib7db73a2018-10-24 14:18:40 +0200378 unsigned int index = 0;
379
380 while ((mod = ly_ctx_get_module_by_iter(ctx, key, key_offset, &index))) {
381 if (!revision) {
Radek Krejci0af46292019-01-11 16:02:31 +0100382 if (!mod->revision) {
Radek Krejcib7db73a2018-10-24 14:18:40 +0200383 /* found requested module without revision */
384 return mod;
385 }
386 } else {
Radek Krejci0af46292019-01-11 16:02:31 +0100387 if (mod->revision && !strcmp(mod->revision, revision)) {
Radek Krejcib7db73a2018-10-24 14:18:40 +0200388 /* found requested module of the specific revision */
389 return mod;
390 }
391 }
392 }
393
394 return NULL;
395}
396
Radek Krejci0af46292019-01-11 16:02:31 +0100397API struct lys_module *
Radek Krejcib7db73a2018-10-24 14:18:40 +0200398ly_ctx_get_module_ns(const struct ly_ctx *ctx, const char *ns, const char *revision)
399{
400 LY_CHECK_ARG_RET(ctx, ctx, ns, NULL);
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100401 return ly_ctx_get_module_by(ctx, ns, offsetof(struct lys_module, ns), revision);
Radek Krejcib7db73a2018-10-24 14:18:40 +0200402}
403
Radek Krejci0af46292019-01-11 16:02:31 +0100404API struct lys_module *
Radek Krejcib7db73a2018-10-24 14:18:40 +0200405ly_ctx_get_module(const struct ly_ctx *ctx, const char *name, const char *revision)
406{
407 LY_CHECK_ARG_RET(ctx, ctx, name, NULL);
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100408 return ly_ctx_get_module_by(ctx, name, offsetof(struct lys_module, name), revision);
Radek Krejcib7db73a2018-10-24 14:18:40 +0200409}
410
411/**
412 * @brief Unifying function for ly_ctx_get_module_latest() and ly_ctx_get_module_latest_ns()
413 * @param[in] ctx Context where to search.
414 * @param[in] key Name or Namespace as a search key.
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100415 * @param[in] key_offset Key's offset in struct lys_module to get value from the context's modules to match with the key.
Radek Krejcib7db73a2018-10-24 14:18:40 +0200416 * @return Matching module if any.
417 */
Radek Krejci0af46292019-01-11 16:02:31 +0100418static struct lys_module *
Radek Krejcib7db73a2018-10-24 14:18:40 +0200419ly_ctx_get_module_latest_by(const struct ly_ctx *ctx, const char *key, size_t key_offset)
420{
Radek Krejci0af46292019-01-11 16:02:31 +0100421 struct lys_module *mod;
Radek Krejcib7db73a2018-10-24 14:18:40 +0200422 unsigned int index = 0;
Radek Krejcib7db73a2018-10-24 14:18:40 +0200423
424 while ((mod = ly_ctx_get_module_by_iter(ctx, key, key_offset, &index))) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100425 if (mod->latest_revision) {
Radek Krejci9f5e6fb2018-10-25 09:26:12 +0200426 return mod;
Radek Krejcib7db73a2018-10-24 14:18:40 +0200427 }
428 }
429
Radek Krejci9f5e6fb2018-10-25 09:26:12 +0200430 return NULL;
Radek Krejcib7db73a2018-10-24 14:18:40 +0200431}
432
Radek Krejci0af46292019-01-11 16:02:31 +0100433API struct lys_module *
Radek Krejcib7db73a2018-10-24 14:18:40 +0200434ly_ctx_get_module_latest(const struct ly_ctx *ctx, const char *name)
435{
436 LY_CHECK_ARG_RET(ctx, ctx, name, NULL);
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100437 return ly_ctx_get_module_latest_by(ctx, name, offsetof(struct lys_module, name));
Radek Krejcib7db73a2018-10-24 14:18:40 +0200438}
439
Radek Krejci0af46292019-01-11 16:02:31 +0100440API struct lys_module *
Radek Krejcib7db73a2018-10-24 14:18:40 +0200441ly_ctx_get_module_latest_ns(const struct ly_ctx *ctx, const char *ns)
442{
443 LY_CHECK_ARG_RET(ctx, ctx, ns, NULL);
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100444 return ly_ctx_get_module_latest_by(ctx, ns, offsetof(struct lys_module, ns));
Radek Krejcib7db73a2018-10-24 14:18:40 +0200445}
446
447/**
448 * @brief Unifying function for ly_ctx_get_module_implemented() and ly_ctx_get_module_implemented_ns()
449 * @param[in] ctx Context where to search.
450 * @param[in] key Name or Namespace as a search key.
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100451 * @param[in] key_offset Key's offset in struct lys_module to get value from the context's modules to match with the key.
Radek Krejcib7db73a2018-10-24 14:18:40 +0200452 * @return Matching module if any.
453 */
Radek Krejci0af46292019-01-11 16:02:31 +0100454static struct lys_module *
Radek Krejcib7db73a2018-10-24 14:18:40 +0200455ly_ctx_get_module_implemented_by(const struct ly_ctx *ctx, const char *key, size_t key_offset)
456{
Radek Krejci0af46292019-01-11 16:02:31 +0100457 struct lys_module *mod;
Radek Krejcib7db73a2018-10-24 14:18:40 +0200458 unsigned int index = 0;
459
460 while ((mod = ly_ctx_get_module_by_iter(ctx, key, key_offset, &index))) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100461 if (mod->implemented) {
Radek Krejcib7db73a2018-10-24 14:18:40 +0200462 return mod;
463 }
464 }
465
466 return NULL;
467}
468
Radek Krejci0af46292019-01-11 16:02:31 +0100469API struct lys_module *
Radek Krejcib7db73a2018-10-24 14:18:40 +0200470ly_ctx_get_module_implemented(const struct ly_ctx *ctx, const char *name)
471{
472 LY_CHECK_ARG_RET(ctx, ctx, name, NULL);
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100473 return ly_ctx_get_module_implemented_by(ctx, name, offsetof(struct lys_module, name));
Radek Krejcib7db73a2018-10-24 14:18:40 +0200474}
475
Radek Krejci0af46292019-01-11 16:02:31 +0100476API struct lys_module *
Radek Krejcib7db73a2018-10-24 14:18:40 +0200477ly_ctx_get_module_implemented_ns(const struct ly_ctx *ctx, const char *ns)
478{
479 LY_CHECK_ARG_RET(ctx, ctx, ns, NULL);
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100480 return ly_ctx_get_module_implemented_by(ctx, ns, offsetof(struct lys_module, ns));
Radek Krejcib7db73a2018-10-24 14:18:40 +0200481}
482
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100483struct lysp_submodule *
Radek Krejcid33273d2018-10-25 14:55:52 +0200484ly_ctx_get_submodule(const struct ly_ctx *ctx, const char *module, const char *submodule, const char *revision)
485{
486 const struct lys_module *mod;
487 struct lysp_include *inc;
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200488 uint32_t v;
489 LY_ARRAY_SIZE_TYPE u;
Radek Krejcid33273d2018-10-25 14:55:52 +0200490
Radek Krejcifaa1eac2018-10-30 14:34:55 +0100491 assert(submodule);
492
493 for (v = 0; v < ctx->list.count; ++v) {
494 mod = ctx->list.objs[v];
Radek Krejcid33273d2018-10-25 14:55:52 +0200495 if (!mod->parsed) {
496 continue;
497 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100498 if (module && strcmp(module, mod->name)) {
Radek Krejcifaa1eac2018-10-30 14:34:55 +0100499 continue;
500 }
Radek Krejcid33273d2018-10-25 14:55:52 +0200501
502 LY_ARRAY_FOR(mod->parsed->includes, u) {
Radek Krejci086c7132018-10-26 15:29:04 +0200503 if (mod->parsed->includes[u].submodule && !strcmp(submodule, mod->parsed->includes[u].submodule->name)) {
Radek Krejcid33273d2018-10-25 14:55:52 +0200504 inc = &mod->parsed->includes[u];
505 if (!revision) {
506 if (inc->submodule->latest_revision) {
507 return inc->submodule;
508 }
509 } else if (inc->submodule->revs && !strcmp(revision, inc->submodule->revs[0].date)) {
510 return inc->submodule;
511 }
512 }
513 }
514 }
515
516 return NULL;
517}
518
Radek Krejci0af5f5d2018-09-07 15:00:30 +0200519API void
Radek Krejcie9e987e2018-10-31 12:50:27 +0100520ly_ctx_reset_latests(struct ly_ctx *ctx)
521{
Radek Krejcie9e987e2018-10-31 12:50:27 +0100522 struct lys_module *mod;
523
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200524 for (uint32_t v = 0; v < ctx->list.count; ++v) {
525 mod = ctx->list.objs[v];
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100526 if (mod->latest_revision == 2) {
527 mod->latest_revision = 1;
Radek Krejcie9e987e2018-10-31 12:50:27 +0100528 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100529 if (mod->parsed && mod->parsed->includes) {
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200530 for (LY_ARRAY_SIZE_TYPE u = 0; u < LY_ARRAY_SIZE(mod->parsed->includes); ++u) {
531 if (mod->parsed->includes[u].submodule->latest_revision == 2) {
532 mod->parsed->includes[u].submodule->latest_revision = 1;
Radek Krejcie9e987e2018-10-31 12:50:27 +0100533 }
534 }
535 }
536 }
537}
538
Michal Vasko57c10cd2020-05-27 15:57:11 +0200539static LY_ERR
540ylib_feature(struct lyd_node *parent, const struct lys_module *cur_mod)
541{
542 LY_ARRAY_SIZE_TYPE i;
543 struct lyd_node *node;
544
545 if (!cur_mod->implemented) {
546 /* no features can be enabled */
547 return LY_SUCCESS;
548 }
549
550 LY_ARRAY_FOR(cur_mod->compiled->features, i) {
551 if (!(cur_mod->compiled->features[i].flags & LYS_FENABLED)) {
552 continue;
553 }
554
555 node = lyd_new_term(parent, NULL, "feature", cur_mod->compiled->features[i].name);
556 LY_CHECK_RET(!node, LY_EOTHER);
557 }
558
559 return LY_SUCCESS;
560}
561
562static LY_ERR
563ylib_deviation(struct lyd_node *parent, const struct lys_module *cur_mod, int bis)
564{
565 LY_ARRAY_SIZE_TYPE i;
566 struct lyd_node *node;
567 struct lys_module *mod;
568
569 if (!cur_mod->implemented) {
570 /* no deviations of the module for certain */
571 return LY_SUCCESS;
572 }
573
574 LY_ARRAY_FOR(cur_mod->compiled->deviated_by, i) {
575 mod = cur_mod->compiled->deviated_by[i];
576
577 if (bis) {
578 node = lyd_new_term(parent, NULL, "deviation", mod->name);
579 LY_CHECK_RET(!node, LY_EOTHER);
580 } else {
581 node = lyd_new_list(parent, NULL, "deviation", mod->name, (mod->parsed->revs ? mod->parsed->revs[0].date : ""));
582 LY_CHECK_RET(!node, LY_EOTHER);
583 }
584 }
585
586 return LY_SUCCESS;
587}
588
589static LY_ERR
590ylib_submodules(struct lyd_node *parent, const struct lys_module *cur_mod, int bis)
591{
592 LY_ARRAY_SIZE_TYPE i;
593 struct lyd_node *node, *cont;
594 struct lysp_submodule *submod;
595 int ret;
596 char *str;
597
598 LY_ARRAY_FOR(cur_mod->parsed->includes, i) {
599 submod = cur_mod->parsed->includes[i].submodule;
600
601 if (bis) {
602 cont = lyd_new_list(parent, NULL, "submodule", submod->name);
603 LY_CHECK_RET(!cont, LY_EOTHER);
604
605 if (submod->revs) {
606 node = lyd_new_term(cont, NULL, "revision", submod->revs[0].date);
607 LY_CHECK_RET(!node, LY_EOTHER);
608 }
609 } else {
610 cont = lyd_new_list(parent, NULL, "submodule", submod->name, (submod->revs ? submod->revs[0].date : ""));
611 LY_CHECK_RET(!cont, LY_EOTHER);
612 }
613
614 if (submod->filepath) {
615 ret = asprintf(&str, "file://%s", submod->filepath);
616 LY_CHECK_ERR_RET(ret == -1, LOGMEM(cur_mod->ctx), LY_EMEM);
617
618 node = lyd_new_term(cont, NULL, bis ? "location" : "schema", str);
619 LY_CHECK_RET(!node, LY_EOTHER);
620 free(str);
621 }
622 }
623
624 return LY_SUCCESS;
625}
626
627API uint16_t
628ly_ctx_get_yanglib_id(const struct ly_ctx *ctx)
629{
630 return ctx->module_set_id;
631}
632
633API struct lyd_node *
634ly_ctx_get_yanglib_data(const struct ly_ctx *ctx)
635{
636 uint32_t i;
637 int bis = 0, ret;
638 char id[8], *str;
639 const struct lys_module *mod;
640 struct lyd_node *root = NULL, *root_bis = NULL, *cont, *set_bis, *node;
641
642 LY_CHECK_ARG_RET(ctx, ctx, NULL);
643
644 mod = ly_ctx_get_module_implemented(ctx, "ietf-yang-library");
645 LY_CHECK_ERR_RET(!mod, LOGERR(ctx, LY_EINVAL, "Module \"ietf-yang-library\" is not implemented."), NULL);
646
647 if (mod->parsed->revs && !strcmp(mod->parsed->revs[0].date, "2016-06-21")) {
648 bis = 0;
649 } else if (mod->parsed->revs && !strcmp(mod->parsed->revs[0].date, IETF_YANG_LIB_REV)) {
650 bis = 1;
651 } else {
652 LOGERR(ctx, LY_EINVAL, "Incompatible ietf-yang-library version in context.");
653 return NULL;
654 }
655
656 root = lyd_new_inner(NULL, mod, "modules-state");
657 LY_CHECK_GOTO(!root, error);
658
659 if (bis) {
660 root_bis = lyd_new_inner(NULL, mod, "yang-library");
661 LY_CHECK_GOTO(!root_bis, error);
662
663 set_bis = lyd_new_list(root_bis, NULL, "module-set", "complete");
664 LY_CHECK_GOTO(!set_bis, error);
665 }
666
667 for (i = 0; i < ctx->list.count; ++i) {
668 mod = ctx->list.objs[i];
669
670 /*
671 * deprecated legacy
672 */
673 cont = lyd_new_list(root, NULL, "module", mod->name, (mod->parsed->revs ? mod->parsed->revs[0].date : ""));
674 LY_CHECK_GOTO(!cont, error);
675
676 /* schema */
677 if (mod->filepath) {
678 ret = asprintf(&str, "file://%s", mod->filepath);
679 LY_CHECK_ERR_GOTO(ret == -1, LOGMEM(ctx), error);
680
681 node = lyd_new_term(cont, NULL, "schema", str);
682 free(str);
683 LY_CHECK_GOTO(!node, error);
684 }
685
686 /* namespace */
687 node = lyd_new_term(cont, NULL, "namespace", mod->ns);
688 LY_CHECK_GOTO(!node, error);
689
690 /* feature leaf-list */
691 LY_CHECK_GOTO(ylib_feature(cont, mod), error);
692
693 /* deviation list */
694 LY_CHECK_GOTO(ylib_deviation(cont, mod, 0), error);
695
696 /* conformance-type */
697 node = lyd_new_term(cont, NULL, "conformance-type", (mod->implemented ? "implement" : "import"));
698 LY_CHECK_GOTO(!node, error);
699
700 /* submodule list */
701 LY_CHECK_GOTO(ylib_submodules(cont, mod, 0), error);
702
703 /*
704 * current revision
705 */
706 if (bis) {
707 /* name and revision */
708 if (mod->implemented) {
709 cont = lyd_new_list(set_bis, NULL, "module", mod->name);
710 LY_CHECK_GOTO(!cont, error);
711
712 if (mod->parsed->revs) {
713 node = lyd_new_term(cont, NULL, "revision", mod->parsed->revs[0].date);
714 LY_CHECK_GOTO(!node, error);
715 }
716 } else {
717 cont = lyd_new_list(set_bis, NULL, "import-only-module", mod->name,
718 (mod->parsed->revs ? mod->parsed->revs[0].date : ""));
719 LY_CHECK_GOTO(!cont, error);
720 }
721
722 /* namespace */
723 node = lyd_new_term(cont, NULL, "namespace", mod->ns);
724 LY_CHECK_GOTO(!node, error);
725
726 /* location */
727 if (mod->filepath) {
728 ret = asprintf(&str, "file://%s", mod->filepath);
729 LY_CHECK_ERR_GOTO(ret == -1, LOGMEM(ctx), error);
730
731 node = lyd_new_term(cont, NULL, "schema", str);
732 free(str);
733 LY_CHECK_GOTO(!node, error);
734 }
735
736 /* submodule list */
737 LY_CHECK_GOTO(ylib_submodules(cont, mod, 1), error);
738
739 /* feature list */
740 LY_CHECK_GOTO(ylib_feature(cont, mod), error);
741
742 /* deviation */
743 LY_CHECK_GOTO(ylib_deviation(cont, mod, 1), error);
744 }
745 }
746
747 /* IDs */
748 sprintf(id, "%u", ctx->module_set_id);
749 node = lyd_new_term(root, NULL, "module-set-id", id);
750 LY_CHECK_GOTO(!node, error);
751
752 if (bis) {
753 /* create one complete schema */
754 cont = lyd_new_list(root_bis, NULL, "schema", "complete");
755 LY_CHECK_GOTO(!cont, error);
756
757 node = lyd_new_term(cont, NULL, "module-set", "complete");
758 LY_CHECK_GOTO(!node, error);
759
760 /* content-id */
761 node = lyd_new_term(root_bis, NULL, "content-id", id);
762 LY_CHECK_GOTO(!node, error);
763 }
764
765 if (root_bis) {
766 if (lyd_insert_sibling(root_bis, root)) {
767 goto error;
768 }
769 root = root_bis;
770 root_bis = 0;
771 }
772
773 /* TODO uncomment once lefref validation works
774 if (lyd_validate(&root, NULL, LYD_VALOPT_DATA_ONLY)) {
775 goto error;
776 }*/
777
778 return root;
779
780error:
781 lyd_free_all(root);
782 lyd_free_all(root_bis);
783 return NULL;
784}
785
Radek Krejcie9e987e2018-10-31 12:50:27 +0100786API void
Radek Krejciad573502018-09-07 15:26:55 +0200787ly_ctx_destroy(struct ly_ctx *ctx, void (*private_destructor)(const struct lysc_node *node, void *priv))
Radek Krejci0af5f5d2018-09-07 15:00:30 +0200788{
Radek Krejci418823a2018-09-20 16:42:13 +0200789 if (!ctx) {
790 return;
791 }
Radek Krejci0af5f5d2018-09-07 15:00:30 +0200792
793 /* models list */
Michal Vaskob34480a2018-09-17 10:34:45 +0200794 for (; ctx->list.count; ctx->list.count--) {
Radek Krejci0af5f5d2018-09-07 15:00:30 +0200795 /* remove the module */
Radek Krejci86d106e2018-10-18 09:53:19 +0200796 lys_module_free(ctx->list.objs[ctx->list.count - 1], private_destructor);
Radek Krejci0af5f5d2018-09-07 15:00:30 +0200797 }
798 free(ctx->list.objs);
799
800 /* search paths list */
Radek Krejcia40f21b2018-09-18 10:42:08 +0200801 ly_set_erase(&ctx->search_paths, free);
Radek Krejci0af5f5d2018-09-07 15:00:30 +0200802
803 /* clean the error list */
804 ly_err_clean(ctx, 0);
805 pthread_key_delete(ctx->errlist_key);
806
807 /* dictionary */
808 lydict_clean(&ctx->dict);
809
810#if 0 /* TODO when plugins implemented */
811 /* plugins - will be removed only if this is the last context */
812 ly_clean_plugins();
813#endif
814
815 free(ctx);
816}