blob: d5bc77c7eccbfe8876fccc0279c3d73972a033f4 [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"
Michal Vasko63f3d842020-07-08 10:10:14 +020030#include "parser.h"
Radek Krejci7931b192020-06-25 17:05:03 +020031#include "parser_data.h"
Radek Krejcica376bd2020-06-11 16:04:06 +020032#include "plugins_types.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020033#include "set.h"
34#include "tree.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020035#include "tree_data.h"
Radek Krejcica376bd2020-06-11 16:04:06 +020036#include "tree_schema.h"
Radek Krejci86d106e2018-10-18 09:53:19 +020037#include "tree_schema_internal.h"
Radek Krejci0af5f5d2018-09-07 15:00:30 +020038
Radek Krejci86d106e2018-10-18 09:53:19 +020039#define LY_INTERNAL_MODS_COUNT 6
Radek Krejci0af5f5d2018-09-07 15:00:30 +020040
Radek Krejcif3f47842018-11-15 11:22:15 +010041#include "../models/ietf-yang-metadata@2016-08-05.h"
Michal Vaskoe893ddd2020-06-23 13:35:20 +020042#include "../models/yang@2020-06-17.h"
Radek Krejcif3f47842018-11-15 11:22:15 +010043#include "../models/ietf-inet-types@2013-07-15.h"
44#include "../models/ietf-yang-types@2013-07-15.h"
Michal Vaskocf296c82020-05-27 11:16:45 +020045#include "../models/ietf-datastores@2018-02-14.h"
46#include "../models/ietf-yang-library@2019-01-04.h"
47#define IETF_YANG_LIB_REV "2019-01-04"
Radek Krejci0af5f5d2018-09-07 15:00:30 +020048
Radek Krejci0af5f5d2018-09-07 15:00:30 +020049static struct internal_modules_s {
50 const char *name;
51 const char *revision;
52 const char *data;
53 uint8_t implemented;
54 LYS_INFORMAT format;
55} internal_modules[LY_INTERNAL_MODS_COUNT] = {
Radek Krejci335332a2019-09-05 13:03:35 +020056 {"ietf-yang-metadata", "2016-08-05", (const char*)ietf_yang_metadata_2016_08_05_yang, 1, LYS_IN_YANG},
Michal Vaskoe893ddd2020-06-23 13:35:20 +020057 {"yang", "2020-06-17", (const char*)yang_2020_06_17_yang, 1, LYS_IN_YANG},
Radek Krejci86d106e2018-10-18 09:53:19 +020058 {"ietf-inet-types", "2013-07-15", (const char*)ietf_inet_types_2013_07_15_yang, 0, LYS_IN_YANG},
59 {"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 +020060 /* ietf-datastores and ietf-yang-library must be right here at the end of the list! */
Michal Vaskocf296c82020-05-27 11:16:45 +020061 {"ietf-datastores", "2018-02-14", (const char*)ietf_datastores_2018_02_14_yang, 1, LYS_IN_YANG},
62 {"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 +020063};
64
65API LY_ERR
66ly_ctx_set_searchdir(struct ly_ctx *ctx, const char *search_dir)
67{
Radek Krejci0e212402018-09-20 11:29:38 +020068 struct stat st;
Radek Krejci0af5f5d2018-09-07 15:00:30 +020069 char *new_dir = NULL;
Radek Krejci0e212402018-09-20 11:29:38 +020070 unsigned int u;
Radek Krejci0af5f5d2018-09-07 15:00:30 +020071
72 LY_CHECK_ARG_RET(ctx, ctx, LY_EINVAL);
73
74 if (search_dir) {
Radek Krejci0af5f5d2018-09-07 15:00:30 +020075 new_dir = realpath(search_dir, NULL);
Radek Krejcia77e0e12018-09-20 12:39:15 +020076 LY_CHECK_ERR_RET(!new_dir,
Radek Krejci3a077b92019-04-09 17:10:35 +020077 LOGERR(ctx, LY_ESYS, "Unable to use search directory \"%s\" (%s).", search_dir, strerror(errno)),
78 LY_EINVAL);
79 if (strcmp(search_dir, new_dir)) {
80 LOGVRB("Canonicalizing search directory string from \"%s\" to \"%s\".", search_dir, new_dir);
81 }
82 LY_CHECK_ERR_RET(access(new_dir, R_OK | X_OK),
83 LOGERR(ctx, LY_ESYS, "Unable to fully access search directory \"%s\" (%s).", new_dir, strerror(errno)); free(new_dir),
84 LY_EINVAL);
85 LY_CHECK_ERR_RET(stat(new_dir, &st),
86 LOGERR(ctx, LY_ESYS, "stat() failed for \"%s\" (%s).", new_dir, strerror(errno)); free(new_dir),
Radek Krejcia77e0e12018-09-20 12:39:15 +020087 LY_ESYS);
Radek Krejci3a077b92019-04-09 17:10:35 +020088 LY_CHECK_ERR_RET(!S_ISDIR(st.st_mode),
89 LOGERR(ctx, LY_ESYS, "Given search directory \"%s\" is not a directory.", new_dir); free(new_dir),
90 LY_EINVAL);
Radek Krejci0e212402018-09-20 11:29:38 +020091 /* avoid path duplication */
92 for (u = 0; u < ctx->search_paths.count; ++u) {
Radek Krejci14946ab2018-09-20 13:42:06 +020093 if (!strcmp(new_dir, ctx->search_paths.objs[u])) {
Radek Krejci0e212402018-09-20 11:29:38 +020094 free(new_dir);
Radek Krejci14946ab2018-09-20 13:42:06 +020095 return LY_EEXIST;
Radek Krejci0e212402018-09-20 11:29:38 +020096 }
97 }
98 if (ly_set_add(&ctx->search_paths, new_dir, LY_SET_OPT_USEASLIST) == -1) {
Radek Krejci0af5f5d2018-09-07 15:00:30 +020099 free(new_dir);
100 return LY_EMEM;
101 }
102
Radek Krejcie9e987e2018-10-31 12:50:27 +0100103 /* new searchdir - possibly more latest revision available */
104 ly_ctx_reset_latests(ctx);
105
Radek Krejci0e212402018-09-20 11:29:38 +0200106 return LY_SUCCESS;
Radek Krejci0af5f5d2018-09-07 15:00:30 +0200107 } else {
108 /* consider that no change is not actually an error */
109 return LY_SUCCESS;
110 }
Radek Krejci0af5f5d2018-09-07 15:00:30 +0200111}
112
113API const char * const *
114ly_ctx_get_searchdirs(const struct ly_ctx *ctx)
115{
Radek Krejci05026432018-09-20 12:13:43 +0200116 void **new;
117
Radek Krejci0af5f5d2018-09-07 15:00:30 +0200118 LY_CHECK_ARG_RET(ctx, ctx, NULL);
Radek Krejci05026432018-09-20 12:13:43 +0200119
120 if (ctx->search_paths.count == ctx->search_paths.size) {
121 /* not enough space for terminating NULL byte */
122 new = realloc(((struct ly_ctx *)ctx)->search_paths.objs, (ctx->search_paths.size + 8) * sizeof *ctx->search_paths.objs);
123 LY_CHECK_ERR_RET(!new, LOGMEM(NULL), NULL);
124 ((struct ly_ctx *)ctx)->search_paths.size += 8;
125 ((struct ly_ctx *)ctx)->search_paths.objs = new;
126 }
127 /* set terminating NULL byte to the strings list */
128 ctx->search_paths.objs[ctx->search_paths.count] = NULL;
129
Radek Krejci0af5f5d2018-09-07 15:00:30 +0200130 return (const char * const *)ctx->search_paths.objs;
131}
132
133API LY_ERR
Radek Krejci0759b792018-09-20 13:53:15 +0200134ly_ctx_unset_searchdirs(struct ly_ctx *ctx, const char *value)
Radek Krejci0af5f5d2018-09-07 15:00:30 +0200135{
Radek Krejci0759b792018-09-20 13:53:15 +0200136 unsigned int index;
137
Radek Krejcicd649072018-09-20 12:14:45 +0200138 LY_CHECK_ARG_RET(ctx, ctx, LY_EINVAL);
Radek Krejcicd649072018-09-20 12:14:45 +0200139
Michal Vaskob34480a2018-09-17 10:34:45 +0200140 if (!ctx->search_paths.count) {
Radek Krejci0af5f5d2018-09-07 15:00:30 +0200141 return LY_SUCCESS;
142 }
143
Radek Krejci0759b792018-09-20 13:53:15 +0200144 if (value) {
Radek Krejci0af5f5d2018-09-07 15:00:30 +0200145 /* remove specific search directory */
Radek Krejci0759b792018-09-20 13:53:15 +0200146 for (index = 0; index < ctx->search_paths.count; ++index) {
147 if (!strcmp(value, ctx->search_paths.objs[index])) {
148 break;
149 }
150 }
151 if (index == ctx->search_paths.count) {
152 LOGARG(ctx, value);
153 return LY_EINVAL;
154 } else {
155 return ly_set_rm_index(&ctx->search_paths, index, free);
156 }
Radek Krejci0af5f5d2018-09-07 15:00:30 +0200157 } else {
158 /* remove them all */
Radek Krejcia40f21b2018-09-18 10:42:08 +0200159 ly_set_erase(&ctx->search_paths, free);
Radek Krejci0af5f5d2018-09-07 15:00:30 +0200160 memset(&ctx->search_paths, 0, sizeof ctx->search_paths);
161 }
162
163 return LY_SUCCESS;
164}
165
166API LY_ERR
Radek Krejcied5acc52019-04-25 15:57:04 +0200167ly_ctx_unset_searchdir(struct ly_ctx *ctx, unsigned int index)
168{
169 LY_CHECK_ARG_RET(ctx, ctx, LY_EINVAL);
170
171 if (!ctx->search_paths.count) {
172 return LY_SUCCESS;
173 }
174
175 if (index >= ctx->search_paths.count) {
176 LOGARG(ctx, value);
177 return LY_EINVAL;
178 } else {
179 return ly_set_rm_index(&ctx->search_paths, index, free);
180 }
181
182 return LY_SUCCESS;
183}
184
185API const struct lys_module *
186ly_ctx_load_module(struct ly_ctx *ctx, const char *name, const char *revision)
187{
188 struct lys_module *result = NULL;
189
190 LY_CHECK_ARG_RET(ctx, ctx, name, NULL);
191
192 LY_CHECK_RET(lysp_load_module(ctx, name, revision, 1, 0, &result), NULL);
193 return result;
194}
195
196API LY_ERR
Radek Krejci0af5f5d2018-09-07 15:00:30 +0200197ly_ctx_new(const char *search_dir, int options, struct ly_ctx **new_ctx)
198{
199 struct ly_ctx *ctx = NULL;
200 struct lys_module *module;
201 char *search_dir_list;
202 char *sep, *dir;
Radek Krejci86d106e2018-10-18 09:53:19 +0200203 int i;
Michal Vasko63f3d842020-07-08 10:10:14 +0200204 struct ly_in *in = NULL;
Radek Krejci0af5f5d2018-09-07 15:00:30 +0200205 LY_ERR rc = LY_SUCCESS;
Radek Krejci0af5f5d2018-09-07 15:00:30 +0200206
Radek Krejcied5acc52019-04-25 15:57:04 +0200207 LY_CHECK_ARG_RET(NULL, new_ctx, LY_EINVAL);
208
Radek Krejci0af5f5d2018-09-07 15:00:30 +0200209 ctx = calloc(1, sizeof *ctx);
Radek Krejciad573502018-09-07 15:26:55 +0200210 LY_CHECK_ERR_RET(!ctx, LOGMEM(NULL), LY_EMEM);
Radek Krejci0af5f5d2018-09-07 15:00:30 +0200211
212 /* dictionary */
213 lydict_init(&ctx->dict);
214
Radek Krejciad573502018-09-07 15:26:55 +0200215#if 0 /* TODO when plugins implemented */
Radek Krejci0af5f5d2018-09-07 15:00:30 +0200216 /* plugins */
217 ly_load_plugins();
Radek Krejciad573502018-09-07 15:26:55 +0200218#endif
Radek Krejci0af5f5d2018-09-07 15:00:30 +0200219
220 /* initialize thread-specific key */
Radek Krejciad573502018-09-07 15:26:55 +0200221 while ((pthread_key_create(&ctx->errlist_key, ly_err_free)) == EAGAIN);
Radek Krejci0af5f5d2018-09-07 15:00:30 +0200222
223 /* models list */
224 ctx->flags = options;
225 if (search_dir) {
226 search_dir_list = strdup(search_dir);
227 LY_CHECK_ERR_GOTO(!search_dir_list, LOGMEM(NULL); rc = LY_EMEM, error);
228
229 for (dir = search_dir_list; (sep = strchr(dir, ':')) != NULL && rc == LY_SUCCESS; dir = sep + 1) {
230 *sep = 0;
231 rc = ly_ctx_set_searchdir(ctx, dir);
Radek Krejci14946ab2018-09-20 13:42:06 +0200232 if (rc == LY_EEXIST) {
233 /* ignore duplication */
234 rc = LY_SUCCESS;
235 }
Radek Krejci0af5f5d2018-09-07 15:00:30 +0200236 }
237 if (*dir && rc == LY_SUCCESS) {
238 rc = ly_ctx_set_searchdir(ctx, dir);
Radek Krejci14946ab2018-09-20 13:42:06 +0200239 if (rc == LY_EEXIST) {
240 /* ignore duplication */
241 rc = LY_SUCCESS;
242 }
Radek Krejci0af5f5d2018-09-07 15:00:30 +0200243 }
244 free(search_dir_list);
245
246 /* If ly_ctx_set_searchdir() failed, the error is already logged. Just exit */
247 if (rc != LY_SUCCESS) {
248 goto error;
249 }
250 }
251 ctx->module_set_id = 1;
252
Michal Vasko63f3d842020-07-08 10:10:14 +0200253 /* create dummy in */
254 rc = ly_in_new_memory(internal_modules[0].data, &in);
255 LY_CHECK_GOTO(rc, error);
256
Radek Krejci0af5f5d2018-09-07 15:00:30 +0200257 /* load internal modules */
Radek Krejci86d106e2018-10-18 09:53:19 +0200258 for (i = 0; i < ((options & LY_CTX_NOYANGLIBRARY) ? (LY_INTERNAL_MODS_COUNT - 2) : LY_INTERNAL_MODS_COUNT); i++) {
Michal Vasko63f3d842020-07-08 10:10:14 +0200259 ly_in_memory(in, internal_modules[i].data);
260 module = (struct lys_module *)lys_parse_mem_module(ctx, in, internal_modules[i].format,
Radek Krejci096235c2019-01-11 11:12:19 +0100261 internal_modules[i].implemented, NULL, NULL);
Radek Krejci0e75e6f2018-11-08 09:40:02 +0100262 LY_CHECK_ERR_GOTO(!module, rc = ly_errcode(ctx), error);
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200263 LY_CHECK_GOTO((rc = lys_compile(&module, 0)), error);
Radek Krejci0af5f5d2018-09-07 15:00:30 +0200264 }
265
Michal Vasko63f3d842020-07-08 10:10:14 +0200266 ly_in_free(in, 0);
Radek Krejci0af5f5d2018-09-07 15:00:30 +0200267 *new_ctx = ctx;
268 return rc;
269
270error:
Michal Vasko63f3d842020-07-08 10:10:14 +0200271 ly_in_free(in, 0);
Radek Krejci0af5f5d2018-09-07 15:00:30 +0200272 ly_ctx_destroy(ctx, NULL);
273 return rc;
274}
Radek Krejci0af5f5d2018-09-07 15:00:30 +0200275API int
Radek Krejci3fbe89a2018-09-20 13:54:46 +0200276ly_ctx_get_options(const struct ly_ctx *ctx)
Radek Krejci0af5f5d2018-09-07 15:00:30 +0200277{
Radek Krejci3fbe89a2018-09-20 13:54:46 +0200278 LY_CHECK_ARG_RET(ctx, ctx, 0);
Radek Krejci0af5f5d2018-09-07 15:00:30 +0200279 return ctx->flags;
280}
281
Radek Krejcica828d92018-09-20 14:19:43 +0200282API LY_ERR
Radek Krejci3fa46b62019-09-11 10:47:30 +0200283ly_ctx_set_options(struct ly_ctx *ctx, int option)
Radek Krejcica828d92018-09-20 14:19:43 +0200284{
285 LY_CHECK_ARG_RET(ctx, ctx, LY_EINVAL);
286 LY_CHECK_ERR_RET(option & LY_CTX_NOYANGLIBRARY, LOGARG(ctx, option), LY_EINVAL);
287
288 /* set the option(s) */
289 ctx->flags |= option;
290
291 return LY_SUCCESS;
292}
293
294API LY_ERR
Radek Krejci3fa46b62019-09-11 10:47:30 +0200295ly_ctx_unset_options(struct ly_ctx *ctx, int option)
Radek Krejcica828d92018-09-20 14:19:43 +0200296{
297 LY_CHECK_ARG_RET(ctx, ctx, LY_EINVAL);
298 LY_CHECK_ERR_RET(option & LY_CTX_NOYANGLIBRARY, LOGARG(ctx, option), LY_EINVAL);
299
300 /* unset the option(s) */
301 ctx->flags &= ~option;
302
303 return LY_SUCCESS;
304}
305
Radek Krejci0af5f5d2018-09-07 15:00:30 +0200306API uint16_t
307ly_ctx_get_module_set_id(const struct ly_ctx *ctx)
308{
Radek Krejci3fbe89a2018-09-20 13:54:46 +0200309 LY_CHECK_ARG_RET(ctx, ctx, 0);
Radek Krejci0af5f5d2018-09-07 15:00:30 +0200310 return ctx->module_set_id;
311}
312
Radek Krejcid33273d2018-10-25 14:55:52 +0200313API void
314ly_ctx_set_module_imp_clb(struct ly_ctx *ctx, ly_module_imp_clb clb, void *user_data)
315{
316 LY_CHECK_ARG_RET(ctx, ctx,);
317
318 ctx->imp_clb = clb;
319 ctx->imp_clb_data = user_data;
320}
321
322API ly_module_imp_clb
323ly_ctx_get_module_imp_clb(const struct ly_ctx *ctx, void **user_data)
324{
325 LY_CHECK_ARG_RET(ctx, ctx, NULL);
326
327 if (user_data) {
328 *user_data = ctx->imp_clb_data;
329 }
330 return ctx->imp_clb;
331}
332
Radek Krejcied5acc52019-04-25 15:57:04 +0200333API const struct lys_module *
334ly_ctx_get_module_iter(const struct ly_ctx *ctx, unsigned int *index)
335{
336 LY_CHECK_ARG_RET(ctx, ctx, index, NULL);
337
Radek Krejci2390fb52019-04-30 13:27:43 +0200338 if (*index < (unsigned)ctx->list.count) {
Radek Krejcied5acc52019-04-25 15:57:04 +0200339 return ctx->list.objs[(*index)++];
Radek Krejci2390fb52019-04-30 13:27:43 +0200340 } else {
341 return NULL;
Radek Krejcied5acc52019-04-25 15:57:04 +0200342 }
Radek Krejcied5acc52019-04-25 15:57:04 +0200343}
344
Radek Krejcib7db73a2018-10-24 14:18:40 +0200345/**
346 * @brief Iterate over the modules in the given context. Returned modules must match the given key at the offset of
347 * lysp_module and lysc_module structures (they are supposed to be placed at the same offset in both structures).
348 *
349 * @param[in] ctx Context where to iterate.
350 * @param[in] key Key value to search for.
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100351 * @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 +0200352 * @param[in,out] Iterator to pass between the function calls. On the first call, the variable is supposed to be
353 * initiated to 0. After each call returning a module, the value is greater by 1 than the index of the returned
354 * module in the context.
355 * @return Module matching the given key, NULL if no such module found.
356 */
Radek Krejci0af46292019-01-11 16:02:31 +0100357static struct lys_module *
Radek Krejcib7db73a2018-10-24 14:18:40 +0200358ly_ctx_get_module_by_iter(const struct ly_ctx *ctx, const char *key, size_t key_offset, unsigned int *index)
359{
Radek Krejci0af46292019-01-11 16:02:31 +0100360 struct lys_module *mod;
Radek Krejcib7db73a2018-10-24 14:18:40 +0200361 const char *value;
362
363 for (; *index < ctx->list.count; ++(*index)) {
364 mod = ctx->list.objs[*index];
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100365 value = *(const char**)(((int8_t*)(mod)) + key_offset);
Radek Krejcib7db73a2018-10-24 14:18:40 +0200366 if (!strcmp(key, value)) {
367 /* increment index for the next run */
368 ++(*index);
369 return mod;
370 }
371 }
372 /* done */
373 return NULL;
374}
375
376/**
377 * @brief Unifying function for ly_ctx_get_module() and ly_ctx_get_module_ns()
378 * @param[in] ctx Context where to search.
379 * @param[in] key Name or Namespace as a search key.
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100380 * @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 +0200381 * @param[in] revision Revision date to match. If NULL, the matching module must have no revision. To search for the latest
382 * revision module, use ly_ctx_get_module_latest_by().
383 * @return Matching module if any.
384 */
Radek Krejci0af46292019-01-11 16:02:31 +0100385static struct lys_module *
Radek Krejcib7db73a2018-10-24 14:18:40 +0200386ly_ctx_get_module_by(const struct ly_ctx *ctx, const char *key, size_t key_offset, const char *revision)
387{
Radek Krejci0af46292019-01-11 16:02:31 +0100388 struct lys_module *mod;
Radek Krejcib7db73a2018-10-24 14:18:40 +0200389 unsigned int index = 0;
390
391 while ((mod = ly_ctx_get_module_by_iter(ctx, key, key_offset, &index))) {
392 if (!revision) {
Radek Krejci0af46292019-01-11 16:02:31 +0100393 if (!mod->revision) {
Radek Krejcib7db73a2018-10-24 14:18:40 +0200394 /* found requested module without revision */
395 return mod;
396 }
397 } else {
Radek Krejci0af46292019-01-11 16:02:31 +0100398 if (mod->revision && !strcmp(mod->revision, revision)) {
Radek Krejcib7db73a2018-10-24 14:18:40 +0200399 /* found requested module of the specific revision */
400 return mod;
401 }
402 }
403 }
404
405 return NULL;
406}
407
Radek Krejci0af46292019-01-11 16:02:31 +0100408API struct lys_module *
Radek Krejcib7db73a2018-10-24 14:18:40 +0200409ly_ctx_get_module_ns(const struct ly_ctx *ctx, const char *ns, const char *revision)
410{
411 LY_CHECK_ARG_RET(ctx, ctx, ns, NULL);
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100412 return ly_ctx_get_module_by(ctx, ns, offsetof(struct lys_module, ns), revision);
Radek Krejcib7db73a2018-10-24 14:18:40 +0200413}
414
Radek Krejci0af46292019-01-11 16:02:31 +0100415API struct lys_module *
Radek Krejcib7db73a2018-10-24 14:18:40 +0200416ly_ctx_get_module(const struct ly_ctx *ctx, const char *name, const char *revision)
417{
418 LY_CHECK_ARG_RET(ctx, ctx, name, NULL);
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100419 return ly_ctx_get_module_by(ctx, name, offsetof(struct lys_module, name), revision);
Radek Krejcib7db73a2018-10-24 14:18:40 +0200420}
421
422/**
423 * @brief Unifying function for ly_ctx_get_module_latest() and ly_ctx_get_module_latest_ns()
424 * @param[in] ctx Context where to search.
425 * @param[in] key Name or Namespace as a search key.
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100426 * @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 +0200427 * @return Matching module if any.
428 */
Radek Krejci0af46292019-01-11 16:02:31 +0100429static struct lys_module *
Radek Krejcib7db73a2018-10-24 14:18:40 +0200430ly_ctx_get_module_latest_by(const struct ly_ctx *ctx, const char *key, size_t key_offset)
431{
Radek Krejci0af46292019-01-11 16:02:31 +0100432 struct lys_module *mod;
Radek Krejcib7db73a2018-10-24 14:18:40 +0200433 unsigned int index = 0;
Radek Krejcib7db73a2018-10-24 14:18:40 +0200434
435 while ((mod = ly_ctx_get_module_by_iter(ctx, key, key_offset, &index))) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100436 if (mod->latest_revision) {
Radek Krejci9f5e6fb2018-10-25 09:26:12 +0200437 return mod;
Radek Krejcib7db73a2018-10-24 14:18:40 +0200438 }
439 }
440
Radek Krejci9f5e6fb2018-10-25 09:26:12 +0200441 return NULL;
Radek Krejcib7db73a2018-10-24 14:18:40 +0200442}
443
Radek Krejci0af46292019-01-11 16:02:31 +0100444API struct lys_module *
Radek Krejcib7db73a2018-10-24 14:18:40 +0200445ly_ctx_get_module_latest(const struct ly_ctx *ctx, const char *name)
446{
447 LY_CHECK_ARG_RET(ctx, ctx, name, NULL);
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100448 return ly_ctx_get_module_latest_by(ctx, name, offsetof(struct lys_module, name));
Radek Krejcib7db73a2018-10-24 14:18:40 +0200449}
450
Radek Krejci0af46292019-01-11 16:02:31 +0100451API struct lys_module *
Radek Krejcib7db73a2018-10-24 14:18:40 +0200452ly_ctx_get_module_latest_ns(const struct ly_ctx *ctx, const char *ns)
453{
454 LY_CHECK_ARG_RET(ctx, ctx, ns, NULL);
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100455 return ly_ctx_get_module_latest_by(ctx, ns, offsetof(struct lys_module, ns));
Radek Krejcib7db73a2018-10-24 14:18:40 +0200456}
457
458/**
459 * @brief Unifying function for ly_ctx_get_module_implemented() and ly_ctx_get_module_implemented_ns()
460 * @param[in] ctx Context where to search.
461 * @param[in] key Name or Namespace as a search key.
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100462 * @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 +0200463 * @return Matching module if any.
464 */
Radek Krejci0af46292019-01-11 16:02:31 +0100465static struct lys_module *
Radek Krejcib7db73a2018-10-24 14:18:40 +0200466ly_ctx_get_module_implemented_by(const struct ly_ctx *ctx, const char *key, size_t key_offset)
467{
Radek Krejci0af46292019-01-11 16:02:31 +0100468 struct lys_module *mod;
Radek Krejcib7db73a2018-10-24 14:18:40 +0200469 unsigned int index = 0;
470
471 while ((mod = ly_ctx_get_module_by_iter(ctx, key, key_offset, &index))) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100472 if (mod->implemented) {
Radek Krejcib7db73a2018-10-24 14:18:40 +0200473 return mod;
474 }
475 }
476
477 return NULL;
478}
479
Radek Krejci0af46292019-01-11 16:02:31 +0100480API struct lys_module *
Radek Krejcib7db73a2018-10-24 14:18:40 +0200481ly_ctx_get_module_implemented(const struct ly_ctx *ctx, const char *name)
482{
483 LY_CHECK_ARG_RET(ctx, ctx, name, NULL);
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100484 return ly_ctx_get_module_implemented_by(ctx, name, offsetof(struct lys_module, name));
Radek Krejcib7db73a2018-10-24 14:18:40 +0200485}
486
Radek Krejci0af46292019-01-11 16:02:31 +0100487API struct lys_module *
Radek Krejcib7db73a2018-10-24 14:18:40 +0200488ly_ctx_get_module_implemented_ns(const struct ly_ctx *ctx, const char *ns)
489{
490 LY_CHECK_ARG_RET(ctx, ctx, ns, NULL);
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100491 return ly_ctx_get_module_implemented_by(ctx, ns, offsetof(struct lys_module, ns));
Radek Krejcib7db73a2018-10-24 14:18:40 +0200492}
493
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100494struct lysp_submodule *
Radek Krejcid33273d2018-10-25 14:55:52 +0200495ly_ctx_get_submodule(const struct ly_ctx *ctx, const char *module, const char *submodule, const char *revision)
496{
497 const struct lys_module *mod;
498 struct lysp_include *inc;
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200499 uint32_t v;
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200500 LY_ARRAY_COUNT_TYPE u;
Radek Krejcid33273d2018-10-25 14:55:52 +0200501
Radek Krejcifaa1eac2018-10-30 14:34:55 +0100502 assert(submodule);
503
504 for (v = 0; v < ctx->list.count; ++v) {
505 mod = ctx->list.objs[v];
Radek Krejcid33273d2018-10-25 14:55:52 +0200506 if (!mod->parsed) {
507 continue;
508 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100509 if (module && strcmp(module, mod->name)) {
Radek Krejcifaa1eac2018-10-30 14:34:55 +0100510 continue;
511 }
Radek Krejcid33273d2018-10-25 14:55:52 +0200512
513 LY_ARRAY_FOR(mod->parsed->includes, u) {
Radek Krejci086c7132018-10-26 15:29:04 +0200514 if (mod->parsed->includes[u].submodule && !strcmp(submodule, mod->parsed->includes[u].submodule->name)) {
Radek Krejcid33273d2018-10-25 14:55:52 +0200515 inc = &mod->parsed->includes[u];
516 if (!revision) {
517 if (inc->submodule->latest_revision) {
518 return inc->submodule;
519 }
520 } else if (inc->submodule->revs && !strcmp(revision, inc->submodule->revs[0].date)) {
521 return inc->submodule;
522 }
523 }
524 }
525 }
526
527 return NULL;
528}
529
Radek Krejci0af5f5d2018-09-07 15:00:30 +0200530API void
Radek Krejcie9e987e2018-10-31 12:50:27 +0100531ly_ctx_reset_latests(struct ly_ctx *ctx)
532{
Radek Krejcie9e987e2018-10-31 12:50:27 +0100533 struct lys_module *mod;
534
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200535 for (uint32_t v = 0; v < ctx->list.count; ++v) {
536 mod = ctx->list.objs[v];
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100537 if (mod->latest_revision == 2) {
538 mod->latest_revision = 1;
Radek Krejcie9e987e2018-10-31 12:50:27 +0100539 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100540 if (mod->parsed && mod->parsed->includes) {
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200541 for (LY_ARRAY_COUNT_TYPE u = 0; u < LY_ARRAY_COUNT(mod->parsed->includes); ++u) {
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200542 if (mod->parsed->includes[u].submodule->latest_revision == 2) {
543 mod->parsed->includes[u].submodule->latest_revision = 1;
Radek Krejcie9e987e2018-10-31 12:50:27 +0100544 }
545 }
546 }
547 }
548}
549
Michal Vasko57c10cd2020-05-27 15:57:11 +0200550static LY_ERR
551ylib_feature(struct lyd_node *parent, const struct lys_module *cur_mod)
552{
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200553 LY_ARRAY_COUNT_TYPE i;
Michal Vasko57c10cd2020-05-27 15:57:11 +0200554 struct lyd_node *node;
555
556 if (!cur_mod->implemented) {
557 /* no features can be enabled */
558 return LY_SUCCESS;
559 }
560
561 LY_ARRAY_FOR(cur_mod->compiled->features, i) {
562 if (!(cur_mod->compiled->features[i].flags & LYS_FENABLED)) {
563 continue;
564 }
565
566 node = lyd_new_term(parent, NULL, "feature", cur_mod->compiled->features[i].name);
567 LY_CHECK_RET(!node, LY_EOTHER);
568 }
569
570 return LY_SUCCESS;
571}
572
573static LY_ERR
574ylib_deviation(struct lyd_node *parent, const struct lys_module *cur_mod, int bis)
575{
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200576 LY_ARRAY_COUNT_TYPE i;
Michal Vasko57c10cd2020-05-27 15:57:11 +0200577 struct lyd_node *node;
578 struct lys_module *mod;
579
580 if (!cur_mod->implemented) {
581 /* no deviations of the module for certain */
582 return LY_SUCCESS;
583 }
584
585 LY_ARRAY_FOR(cur_mod->compiled->deviated_by, i) {
586 mod = cur_mod->compiled->deviated_by[i];
587
588 if (bis) {
589 node = lyd_new_term(parent, NULL, "deviation", mod->name);
590 LY_CHECK_RET(!node, LY_EOTHER);
591 } else {
592 node = lyd_new_list(parent, NULL, "deviation", mod->name, (mod->parsed->revs ? mod->parsed->revs[0].date : ""));
593 LY_CHECK_RET(!node, LY_EOTHER);
594 }
595 }
596
597 return LY_SUCCESS;
598}
599
600static LY_ERR
601ylib_submodules(struct lyd_node *parent, const struct lys_module *cur_mod, int bis)
602{
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200603 LY_ARRAY_COUNT_TYPE i;
Michal Vasko57c10cd2020-05-27 15:57:11 +0200604 struct lyd_node *node, *cont;
605 struct lysp_submodule *submod;
606 int ret;
607 char *str;
608
609 LY_ARRAY_FOR(cur_mod->parsed->includes, i) {
610 submod = cur_mod->parsed->includes[i].submodule;
611
612 if (bis) {
613 cont = lyd_new_list(parent, NULL, "submodule", submod->name);
614 LY_CHECK_RET(!cont, LY_EOTHER);
615
616 if (submod->revs) {
617 node = lyd_new_term(cont, NULL, "revision", submod->revs[0].date);
618 LY_CHECK_RET(!node, LY_EOTHER);
619 }
620 } else {
621 cont = lyd_new_list(parent, NULL, "submodule", submod->name, (submod->revs ? submod->revs[0].date : ""));
622 LY_CHECK_RET(!cont, LY_EOTHER);
623 }
624
625 if (submod->filepath) {
626 ret = asprintf(&str, "file://%s", submod->filepath);
627 LY_CHECK_ERR_RET(ret == -1, LOGMEM(cur_mod->ctx), LY_EMEM);
628
629 node = lyd_new_term(cont, NULL, bis ? "location" : "schema", str);
630 LY_CHECK_RET(!node, LY_EOTHER);
631 free(str);
632 }
633 }
634
635 return LY_SUCCESS;
636}
637
638API uint16_t
639ly_ctx_get_yanglib_id(const struct ly_ctx *ctx)
640{
641 return ctx->module_set_id;
642}
643
644API struct lyd_node *
645ly_ctx_get_yanglib_data(const struct ly_ctx *ctx)
646{
647 uint32_t i;
648 int bis = 0, ret;
649 char id[8], *str;
650 const struct lys_module *mod;
651 struct lyd_node *root = NULL, *root_bis = NULL, *cont, *set_bis, *node;
652
653 LY_CHECK_ARG_RET(ctx, ctx, NULL);
654
655 mod = ly_ctx_get_module_implemented(ctx, "ietf-yang-library");
656 LY_CHECK_ERR_RET(!mod, LOGERR(ctx, LY_EINVAL, "Module \"ietf-yang-library\" is not implemented."), NULL);
657
658 if (mod->parsed->revs && !strcmp(mod->parsed->revs[0].date, "2016-06-21")) {
659 bis = 0;
660 } else if (mod->parsed->revs && !strcmp(mod->parsed->revs[0].date, IETF_YANG_LIB_REV)) {
661 bis = 1;
662 } else {
663 LOGERR(ctx, LY_EINVAL, "Incompatible ietf-yang-library version in context.");
664 return NULL;
665 }
666
667 root = lyd_new_inner(NULL, mod, "modules-state");
668 LY_CHECK_GOTO(!root, error);
669
670 if (bis) {
671 root_bis = lyd_new_inner(NULL, mod, "yang-library");
672 LY_CHECK_GOTO(!root_bis, error);
673
674 set_bis = lyd_new_list(root_bis, NULL, "module-set", "complete");
675 LY_CHECK_GOTO(!set_bis, error);
676 }
677
678 for (i = 0; i < ctx->list.count; ++i) {
679 mod = ctx->list.objs[i];
680
681 /*
682 * deprecated legacy
683 */
684 cont = lyd_new_list(root, NULL, "module", mod->name, (mod->parsed->revs ? mod->parsed->revs[0].date : ""));
685 LY_CHECK_GOTO(!cont, error);
686
687 /* schema */
688 if (mod->filepath) {
689 ret = asprintf(&str, "file://%s", mod->filepath);
690 LY_CHECK_ERR_GOTO(ret == -1, LOGMEM(ctx), error);
691
692 node = lyd_new_term(cont, NULL, "schema", str);
693 free(str);
694 LY_CHECK_GOTO(!node, error);
695 }
696
697 /* namespace */
698 node = lyd_new_term(cont, NULL, "namespace", mod->ns);
699 LY_CHECK_GOTO(!node, error);
700
701 /* feature leaf-list */
702 LY_CHECK_GOTO(ylib_feature(cont, mod), error);
703
704 /* deviation list */
705 LY_CHECK_GOTO(ylib_deviation(cont, mod, 0), error);
706
707 /* conformance-type */
708 node = lyd_new_term(cont, NULL, "conformance-type", (mod->implemented ? "implement" : "import"));
709 LY_CHECK_GOTO(!node, error);
710
711 /* submodule list */
712 LY_CHECK_GOTO(ylib_submodules(cont, mod, 0), error);
713
714 /*
715 * current revision
716 */
717 if (bis) {
718 /* name and revision */
719 if (mod->implemented) {
720 cont = lyd_new_list(set_bis, NULL, "module", mod->name);
721 LY_CHECK_GOTO(!cont, error);
722
723 if (mod->parsed->revs) {
724 node = lyd_new_term(cont, NULL, "revision", mod->parsed->revs[0].date);
725 LY_CHECK_GOTO(!node, error);
726 }
727 } else {
728 cont = lyd_new_list(set_bis, NULL, "import-only-module", mod->name,
729 (mod->parsed->revs ? mod->parsed->revs[0].date : ""));
730 LY_CHECK_GOTO(!cont, error);
731 }
732
733 /* namespace */
734 node = lyd_new_term(cont, NULL, "namespace", mod->ns);
735 LY_CHECK_GOTO(!node, error);
736
737 /* location */
738 if (mod->filepath) {
739 ret = asprintf(&str, "file://%s", mod->filepath);
740 LY_CHECK_ERR_GOTO(ret == -1, LOGMEM(ctx), error);
741
742 node = lyd_new_term(cont, NULL, "schema", str);
743 free(str);
744 LY_CHECK_GOTO(!node, error);
745 }
746
747 /* submodule list */
748 LY_CHECK_GOTO(ylib_submodules(cont, mod, 1), error);
749
750 /* feature list */
751 LY_CHECK_GOTO(ylib_feature(cont, mod), error);
752
753 /* deviation */
754 LY_CHECK_GOTO(ylib_deviation(cont, mod, 1), error);
755 }
756 }
757
758 /* IDs */
759 sprintf(id, "%u", ctx->module_set_id);
760 node = lyd_new_term(root, NULL, "module-set-id", id);
761 LY_CHECK_GOTO(!node, error);
762
763 if (bis) {
764 /* create one complete schema */
765 cont = lyd_new_list(root_bis, NULL, "schema", "complete");
766 LY_CHECK_GOTO(!cont, error);
767
768 node = lyd_new_term(cont, NULL, "module-set", "complete");
769 LY_CHECK_GOTO(!node, error);
770
771 /* content-id */
772 node = lyd_new_term(root_bis, NULL, "content-id", id);
773 LY_CHECK_GOTO(!node, error);
774 }
775
776 if (root_bis) {
777 if (lyd_insert_sibling(root_bis, root)) {
778 goto error;
779 }
780 root = root_bis;
781 root_bis = 0;
782 }
783
Radek Krejci7931b192020-06-25 17:05:03 +0200784 if (lyd_validate(&root, NULL, LYD_VALIDATE_PRESENT)) {
Michal Vasko57c10cd2020-05-27 15:57:11 +0200785 goto error;
Michal Vasko00cbf532020-06-15 13:58:47 +0200786 }
Michal Vasko57c10cd2020-05-27 15:57:11 +0200787
788 return root;
789
790error:
791 lyd_free_all(root);
792 lyd_free_all(root_bis);
793 return NULL;
794}
795
Radek Krejcie9e987e2018-10-31 12:50:27 +0100796API void
Radek Krejciad573502018-09-07 15:26:55 +0200797ly_ctx_destroy(struct ly_ctx *ctx, void (*private_destructor)(const struct lysc_node *node, void *priv))
Radek Krejci0af5f5d2018-09-07 15:00:30 +0200798{
Radek Krejci418823a2018-09-20 16:42:13 +0200799 if (!ctx) {
800 return;
801 }
Radek Krejci0af5f5d2018-09-07 15:00:30 +0200802
803 /* models list */
Michal Vaskob34480a2018-09-17 10:34:45 +0200804 for (; ctx->list.count; ctx->list.count--) {
Radek Krejci0af5f5d2018-09-07 15:00:30 +0200805 /* remove the module */
Radek Krejci86d106e2018-10-18 09:53:19 +0200806 lys_module_free(ctx->list.objs[ctx->list.count - 1], private_destructor);
Radek Krejci0af5f5d2018-09-07 15:00:30 +0200807 }
808 free(ctx->list.objs);
809
810 /* search paths list */
Radek Krejcia40f21b2018-09-18 10:42:08 +0200811 ly_set_erase(&ctx->search_paths, free);
Radek Krejci0af5f5d2018-09-07 15:00:30 +0200812
813 /* clean the error list */
814 ly_err_clean(ctx, 0);
815 pthread_key_delete(ctx->errlist_key);
816
817 /* dictionary */
818 lydict_clean(&ctx->dict);
819
820#if 0 /* TODO when plugins implemented */
821 /* plugins - will be removed only if this is the last context */
822 ly_clean_plugins();
823#endif
824
825 free(ctx);
826}