blob: 9f185109d2db9bb1184fac3d879e85b362d9f65a [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"
Radek Krejcica376bd2020-06-11 16:04:06 +020030#include "plugins_types.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020031#include "set.h"
32#include "tree.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020033#include "tree_data.h"
Radek Krejcica376bd2020-06-11 16:04:06 +020034#include "tree_schema.h"
Radek Krejci86d106e2018-10-18 09:53:19 +020035#include "tree_schema_internal.h"
Radek Krejci0af5f5d2018-09-07 15:00:30 +020036
Radek Krejci86d106e2018-10-18 09:53:19 +020037#define LY_INTERNAL_MODS_COUNT 6
Radek Krejci0af5f5d2018-09-07 15:00:30 +020038
Radek Krejcif3f47842018-11-15 11:22:15 +010039#include "../models/ietf-yang-metadata@2016-08-05.h"
40#include "../models/yang@2017-02-20.h"
41#include "../models/ietf-inet-types@2013-07-15.h"
42#include "../models/ietf-yang-types@2013-07-15.h"
Michal Vaskocf296c82020-05-27 11:16:45 +020043#include "../models/ietf-datastores@2018-02-14.h"
44#include "../models/ietf-yang-library@2019-01-04.h"
45#define IETF_YANG_LIB_REV "2019-01-04"
Radek Krejci0af5f5d2018-09-07 15:00:30 +020046
Radek Krejci0af5f5d2018-09-07 15:00:30 +020047static struct internal_modules_s {
48 const char *name;
49 const char *revision;
50 const char *data;
51 uint8_t implemented;
52 LYS_INFORMAT format;
53} internal_modules[LY_INTERNAL_MODS_COUNT] = {
Radek Krejci335332a2019-09-05 13:03:35 +020054 {"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 +020055 {"yang", "2017-02-20", (const char*)yang_2017_02_20_yang, 1, LYS_IN_YANG},
56 {"ietf-inet-types", "2013-07-15", (const char*)ietf_inet_types_2013_07_15_yang, 0, LYS_IN_YANG},
57 {"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 +020058 /* ietf-datastores and ietf-yang-library must be right here at the end of the list! */
Michal Vaskocf296c82020-05-27 11:16:45 +020059 {"ietf-datastores", "2018-02-14", (const char*)ietf_datastores_2018_02_14_yang, 1, LYS_IN_YANG},
60 {"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 +020061};
62
63API LY_ERR
64ly_ctx_set_searchdir(struct ly_ctx *ctx, const char *search_dir)
65{
Radek Krejci0e212402018-09-20 11:29:38 +020066 struct stat st;
Radek Krejci0af5f5d2018-09-07 15:00:30 +020067 char *new_dir = NULL;
Radek Krejci0e212402018-09-20 11:29:38 +020068 unsigned int u;
Radek Krejci0af5f5d2018-09-07 15:00:30 +020069
70 LY_CHECK_ARG_RET(ctx, ctx, LY_EINVAL);
71
72 if (search_dir) {
Radek Krejci0af5f5d2018-09-07 15:00:30 +020073 new_dir = realpath(search_dir, NULL);
Radek Krejcia77e0e12018-09-20 12:39:15 +020074 LY_CHECK_ERR_RET(!new_dir,
Radek Krejci3a077b92019-04-09 17:10:35 +020075 LOGERR(ctx, LY_ESYS, "Unable to use search directory \"%s\" (%s).", search_dir, strerror(errno)),
76 LY_EINVAL);
77 if (strcmp(search_dir, new_dir)) {
78 LOGVRB("Canonicalizing search directory string from \"%s\" to \"%s\".", search_dir, new_dir);
79 }
80 LY_CHECK_ERR_RET(access(new_dir, R_OK | X_OK),
81 LOGERR(ctx, LY_ESYS, "Unable to fully access search directory \"%s\" (%s).", new_dir, strerror(errno)); free(new_dir),
82 LY_EINVAL);
83 LY_CHECK_ERR_RET(stat(new_dir, &st),
84 LOGERR(ctx, LY_ESYS, "stat() failed for \"%s\" (%s).", new_dir, strerror(errno)); free(new_dir),
Radek Krejcia77e0e12018-09-20 12:39:15 +020085 LY_ESYS);
Radek Krejci3a077b92019-04-09 17:10:35 +020086 LY_CHECK_ERR_RET(!S_ISDIR(st.st_mode),
87 LOGERR(ctx, LY_ESYS, "Given search directory \"%s\" is not a directory.", new_dir); free(new_dir),
88 LY_EINVAL);
Radek Krejci0e212402018-09-20 11:29:38 +020089 /* avoid path duplication */
90 for (u = 0; u < ctx->search_paths.count; ++u) {
Radek Krejci14946ab2018-09-20 13:42:06 +020091 if (!strcmp(new_dir, ctx->search_paths.objs[u])) {
Radek Krejci0e212402018-09-20 11:29:38 +020092 free(new_dir);
Radek Krejci14946ab2018-09-20 13:42:06 +020093 return LY_EEXIST;
Radek Krejci0e212402018-09-20 11:29:38 +020094 }
95 }
96 if (ly_set_add(&ctx->search_paths, new_dir, LY_SET_OPT_USEASLIST) == -1) {
Radek Krejci0af5f5d2018-09-07 15:00:30 +020097 free(new_dir);
98 return LY_EMEM;
99 }
100
Radek Krejcie9e987e2018-10-31 12:50:27 +0100101 /* new searchdir - possibly more latest revision available */
102 ly_ctx_reset_latests(ctx);
103
Radek Krejci0e212402018-09-20 11:29:38 +0200104 return LY_SUCCESS;
Radek Krejci0af5f5d2018-09-07 15:00:30 +0200105 } else {
106 /* consider that no change is not actually an error */
107 return LY_SUCCESS;
108 }
Radek Krejci0af5f5d2018-09-07 15:00:30 +0200109}
110
111API const char * const *
112ly_ctx_get_searchdirs(const struct ly_ctx *ctx)
113{
Radek Krejci05026432018-09-20 12:13:43 +0200114 void **new;
115
Radek Krejci0af5f5d2018-09-07 15:00:30 +0200116 LY_CHECK_ARG_RET(ctx, ctx, NULL);
Radek Krejci05026432018-09-20 12:13:43 +0200117
118 if (ctx->search_paths.count == ctx->search_paths.size) {
119 /* not enough space for terminating NULL byte */
120 new = realloc(((struct ly_ctx *)ctx)->search_paths.objs, (ctx->search_paths.size + 8) * sizeof *ctx->search_paths.objs);
121 LY_CHECK_ERR_RET(!new, LOGMEM(NULL), NULL);
122 ((struct ly_ctx *)ctx)->search_paths.size += 8;
123 ((struct ly_ctx *)ctx)->search_paths.objs = new;
124 }
125 /* set terminating NULL byte to the strings list */
126 ctx->search_paths.objs[ctx->search_paths.count] = NULL;
127
Radek Krejci0af5f5d2018-09-07 15:00:30 +0200128 return (const char * const *)ctx->search_paths.objs;
129}
130
131API LY_ERR
Radek Krejci0759b792018-09-20 13:53:15 +0200132ly_ctx_unset_searchdirs(struct ly_ctx *ctx, const char *value)
Radek Krejci0af5f5d2018-09-07 15:00:30 +0200133{
Radek Krejci0759b792018-09-20 13:53:15 +0200134 unsigned int index;
135
Radek Krejcicd649072018-09-20 12:14:45 +0200136 LY_CHECK_ARG_RET(ctx, ctx, LY_EINVAL);
Radek Krejcicd649072018-09-20 12:14:45 +0200137
Michal Vaskob34480a2018-09-17 10:34:45 +0200138 if (!ctx->search_paths.count) {
Radek Krejci0af5f5d2018-09-07 15:00:30 +0200139 return LY_SUCCESS;
140 }
141
Radek Krejci0759b792018-09-20 13:53:15 +0200142 if (value) {
Radek Krejci0af5f5d2018-09-07 15:00:30 +0200143 /* remove specific search directory */
Radek Krejci0759b792018-09-20 13:53:15 +0200144 for (index = 0; index < ctx->search_paths.count; ++index) {
145 if (!strcmp(value, ctx->search_paths.objs[index])) {
146 break;
147 }
148 }
149 if (index == ctx->search_paths.count) {
150 LOGARG(ctx, value);
151 return LY_EINVAL;
152 } else {
153 return ly_set_rm_index(&ctx->search_paths, index, free);
154 }
Radek Krejci0af5f5d2018-09-07 15:00:30 +0200155 } else {
156 /* remove them all */
Radek Krejcia40f21b2018-09-18 10:42:08 +0200157 ly_set_erase(&ctx->search_paths, free);
Radek Krejci0af5f5d2018-09-07 15:00:30 +0200158 memset(&ctx->search_paths, 0, sizeof ctx->search_paths);
159 }
160
161 return LY_SUCCESS;
162}
163
164API LY_ERR
Radek Krejcied5acc52019-04-25 15:57:04 +0200165ly_ctx_unset_searchdir(struct ly_ctx *ctx, unsigned int index)
166{
167 LY_CHECK_ARG_RET(ctx, ctx, LY_EINVAL);
168
169 if (!ctx->search_paths.count) {
170 return LY_SUCCESS;
171 }
172
173 if (index >= ctx->search_paths.count) {
174 LOGARG(ctx, value);
175 return LY_EINVAL;
176 } else {
177 return ly_set_rm_index(&ctx->search_paths, index, free);
178 }
179
180 return LY_SUCCESS;
181}
182
183API const struct lys_module *
184ly_ctx_load_module(struct ly_ctx *ctx, const char *name, const char *revision)
185{
186 struct lys_module *result = NULL;
187
188 LY_CHECK_ARG_RET(ctx, ctx, name, NULL);
189
190 LY_CHECK_RET(lysp_load_module(ctx, name, revision, 1, 0, &result), NULL);
191 return result;
192}
193
194API LY_ERR
Radek Krejci0af5f5d2018-09-07 15:00:30 +0200195ly_ctx_new(const char *search_dir, int options, struct ly_ctx **new_ctx)
196{
197 struct ly_ctx *ctx = NULL;
198 struct lys_module *module;
199 char *search_dir_list;
200 char *sep, *dir;
Radek Krejci86d106e2018-10-18 09:53:19 +0200201 int i;
Radek Krejci0af5f5d2018-09-07 15:00:30 +0200202 LY_ERR rc = LY_SUCCESS;
Radek Krejci0af5f5d2018-09-07 15:00:30 +0200203
Radek Krejcied5acc52019-04-25 15:57:04 +0200204 LY_CHECK_ARG_RET(NULL, new_ctx, LY_EINVAL);
205
Radek Krejci0af5f5d2018-09-07 15:00:30 +0200206 ctx = calloc(1, sizeof *ctx);
Radek Krejciad573502018-09-07 15:26:55 +0200207 LY_CHECK_ERR_RET(!ctx, LOGMEM(NULL), LY_EMEM);
Radek Krejci0af5f5d2018-09-07 15:00:30 +0200208
209 /* dictionary */
210 lydict_init(&ctx->dict);
211
Radek Krejciad573502018-09-07 15:26:55 +0200212#if 0 /* TODO when plugins implemented */
Radek Krejci0af5f5d2018-09-07 15:00:30 +0200213 /* plugins */
214 ly_load_plugins();
Radek Krejciad573502018-09-07 15:26:55 +0200215#endif
Radek Krejci0af5f5d2018-09-07 15:00:30 +0200216
217 /* initialize thread-specific key */
Radek Krejciad573502018-09-07 15:26:55 +0200218 while ((pthread_key_create(&ctx->errlist_key, ly_err_free)) == EAGAIN);
Radek Krejci0af5f5d2018-09-07 15:00:30 +0200219
220 /* models list */
221 ctx->flags = options;
222 if (search_dir) {
223 search_dir_list = strdup(search_dir);
224 LY_CHECK_ERR_GOTO(!search_dir_list, LOGMEM(NULL); rc = LY_EMEM, error);
225
226 for (dir = search_dir_list; (sep = strchr(dir, ':')) != NULL && rc == LY_SUCCESS; dir = sep + 1) {
227 *sep = 0;
228 rc = ly_ctx_set_searchdir(ctx, dir);
Radek Krejci14946ab2018-09-20 13:42:06 +0200229 if (rc == LY_EEXIST) {
230 /* ignore duplication */
231 rc = LY_SUCCESS;
232 }
Radek Krejci0af5f5d2018-09-07 15:00:30 +0200233 }
234 if (*dir && rc == LY_SUCCESS) {
235 rc = ly_ctx_set_searchdir(ctx, dir);
Radek Krejci14946ab2018-09-20 13:42:06 +0200236 if (rc == LY_EEXIST) {
237 /* ignore duplication */
238 rc = LY_SUCCESS;
239 }
Radek Krejci0af5f5d2018-09-07 15:00:30 +0200240 }
241 free(search_dir_list);
242
243 /* If ly_ctx_set_searchdir() failed, the error is already logged. Just exit */
244 if (rc != LY_SUCCESS) {
245 goto error;
246 }
247 }
248 ctx->module_set_id = 1;
249
250 /* load internal modules */
Radek Krejci86d106e2018-10-18 09:53:19 +0200251 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 +0100252 module = (struct lys_module *)lys_parse_mem_module(ctx, internal_modules[i].data, internal_modules[i].format,
253 internal_modules[i].implemented, NULL, NULL);
Radek Krejci0e75e6f2018-11-08 09:40:02 +0100254 LY_CHECK_ERR_GOTO(!module, rc = ly_errcode(ctx), error);
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200255 LY_CHECK_GOTO((rc = lys_compile(&module, 0)), error);
Radek Krejci0af5f5d2018-09-07 15:00:30 +0200256 }
257
258 *new_ctx = ctx;
259 return rc;
260
261error:
262 ly_ctx_destroy(ctx, NULL);
263 return rc;
264}
Radek Krejci0af5f5d2018-09-07 15:00:30 +0200265API int
Radek Krejci3fbe89a2018-09-20 13:54:46 +0200266ly_ctx_get_options(const struct ly_ctx *ctx)
Radek Krejci0af5f5d2018-09-07 15:00:30 +0200267{
Radek Krejci3fbe89a2018-09-20 13:54:46 +0200268 LY_CHECK_ARG_RET(ctx, ctx, 0);
Radek Krejci0af5f5d2018-09-07 15:00:30 +0200269 return ctx->flags;
270}
271
Radek Krejcica828d92018-09-20 14:19:43 +0200272API LY_ERR
Radek Krejci3fa46b62019-09-11 10:47:30 +0200273ly_ctx_set_options(struct ly_ctx *ctx, int option)
Radek Krejcica828d92018-09-20 14:19:43 +0200274{
275 LY_CHECK_ARG_RET(ctx, ctx, LY_EINVAL);
276 LY_CHECK_ERR_RET(option & LY_CTX_NOYANGLIBRARY, LOGARG(ctx, option), LY_EINVAL);
277
278 /* set the option(s) */
279 ctx->flags |= option;
280
281 return LY_SUCCESS;
282}
283
284API LY_ERR
Radek Krejci3fa46b62019-09-11 10:47:30 +0200285ly_ctx_unset_options(struct ly_ctx *ctx, int option)
Radek Krejcica828d92018-09-20 14:19:43 +0200286{
287 LY_CHECK_ARG_RET(ctx, ctx, LY_EINVAL);
288 LY_CHECK_ERR_RET(option & LY_CTX_NOYANGLIBRARY, LOGARG(ctx, option), LY_EINVAL);
289
290 /* unset the option(s) */
291 ctx->flags &= ~option;
292
293 return LY_SUCCESS;
294}
295
Radek Krejci0af5f5d2018-09-07 15:00:30 +0200296API uint16_t
297ly_ctx_get_module_set_id(const struct ly_ctx *ctx)
298{
Radek Krejci3fbe89a2018-09-20 13:54:46 +0200299 LY_CHECK_ARG_RET(ctx, ctx, 0);
Radek Krejci0af5f5d2018-09-07 15:00:30 +0200300 return ctx->module_set_id;
301}
302
Radek Krejcid33273d2018-10-25 14:55:52 +0200303API void
304ly_ctx_set_module_imp_clb(struct ly_ctx *ctx, ly_module_imp_clb clb, void *user_data)
305{
306 LY_CHECK_ARG_RET(ctx, ctx,);
307
308 ctx->imp_clb = clb;
309 ctx->imp_clb_data = user_data;
310}
311
312API ly_module_imp_clb
313ly_ctx_get_module_imp_clb(const struct ly_ctx *ctx, void **user_data)
314{
315 LY_CHECK_ARG_RET(ctx, ctx, NULL);
316
317 if (user_data) {
318 *user_data = ctx->imp_clb_data;
319 }
320 return ctx->imp_clb;
321}
322
Radek Krejcied5acc52019-04-25 15:57:04 +0200323API const struct lys_module *
324ly_ctx_get_module_iter(const struct ly_ctx *ctx, unsigned int *index)
325{
326 LY_CHECK_ARG_RET(ctx, ctx, index, NULL);
327
Radek Krejci2390fb52019-04-30 13:27:43 +0200328 if (*index < (unsigned)ctx->list.count) {
Radek Krejcied5acc52019-04-25 15:57:04 +0200329 return ctx->list.objs[(*index)++];
Radek Krejci2390fb52019-04-30 13:27:43 +0200330 } else {
331 return NULL;
Radek Krejcied5acc52019-04-25 15:57:04 +0200332 }
Radek Krejcied5acc52019-04-25 15:57:04 +0200333}
334
Radek Krejcib7db73a2018-10-24 14:18:40 +0200335/**
336 * @brief Iterate over the modules in the given context. Returned modules must match the given key at the offset of
337 * lysp_module and lysc_module structures (they are supposed to be placed at the same offset in both structures).
338 *
339 * @param[in] ctx Context where to iterate.
340 * @param[in] key Key value to search for.
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100341 * @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 +0200342 * @param[in,out] Iterator to pass between the function calls. On the first call, the variable is supposed to be
343 * initiated to 0. After each call returning a module, the value is greater by 1 than the index of the returned
344 * module in the context.
345 * @return Module matching the given key, NULL if no such module found.
346 */
Radek Krejci0af46292019-01-11 16:02:31 +0100347static struct lys_module *
Radek Krejcib7db73a2018-10-24 14:18:40 +0200348ly_ctx_get_module_by_iter(const struct ly_ctx *ctx, const char *key, size_t key_offset, unsigned int *index)
349{
Radek Krejci0af46292019-01-11 16:02:31 +0100350 struct lys_module *mod;
Radek Krejcib7db73a2018-10-24 14:18:40 +0200351 const char *value;
352
353 for (; *index < ctx->list.count; ++(*index)) {
354 mod = ctx->list.objs[*index];
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100355 value = *(const char**)(((int8_t*)(mod)) + key_offset);
Radek Krejcib7db73a2018-10-24 14:18:40 +0200356 if (!strcmp(key, value)) {
357 /* increment index for the next run */
358 ++(*index);
359 return mod;
360 }
361 }
362 /* done */
363 return NULL;
364}
365
366/**
367 * @brief Unifying function for ly_ctx_get_module() and ly_ctx_get_module_ns()
368 * @param[in] ctx Context where to search.
369 * @param[in] key Name or Namespace as a search key.
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100370 * @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 +0200371 * @param[in] revision Revision date to match. If NULL, the matching module must have no revision. To search for the latest
372 * revision module, use ly_ctx_get_module_latest_by().
373 * @return Matching module if any.
374 */
Radek Krejci0af46292019-01-11 16:02:31 +0100375static struct lys_module *
Radek Krejcib7db73a2018-10-24 14:18:40 +0200376ly_ctx_get_module_by(const struct ly_ctx *ctx, const char *key, size_t key_offset, const char *revision)
377{
Radek Krejci0af46292019-01-11 16:02:31 +0100378 struct lys_module *mod;
Radek Krejcib7db73a2018-10-24 14:18:40 +0200379 unsigned int index = 0;
380
381 while ((mod = ly_ctx_get_module_by_iter(ctx, key, key_offset, &index))) {
382 if (!revision) {
Radek Krejci0af46292019-01-11 16:02:31 +0100383 if (!mod->revision) {
Radek Krejcib7db73a2018-10-24 14:18:40 +0200384 /* found requested module without revision */
385 return mod;
386 }
387 } else {
Radek Krejci0af46292019-01-11 16:02:31 +0100388 if (mod->revision && !strcmp(mod->revision, revision)) {
Radek Krejcib7db73a2018-10-24 14:18:40 +0200389 /* found requested module of the specific revision */
390 return mod;
391 }
392 }
393 }
394
395 return NULL;
396}
397
Radek Krejci0af46292019-01-11 16:02:31 +0100398API struct lys_module *
Radek Krejcib7db73a2018-10-24 14:18:40 +0200399ly_ctx_get_module_ns(const struct ly_ctx *ctx, const char *ns, const char *revision)
400{
401 LY_CHECK_ARG_RET(ctx, ctx, ns, NULL);
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100402 return ly_ctx_get_module_by(ctx, ns, offsetof(struct lys_module, ns), revision);
Radek Krejcib7db73a2018-10-24 14:18:40 +0200403}
404
Radek Krejci0af46292019-01-11 16:02:31 +0100405API struct lys_module *
Radek Krejcib7db73a2018-10-24 14:18:40 +0200406ly_ctx_get_module(const struct ly_ctx *ctx, const char *name, const char *revision)
407{
408 LY_CHECK_ARG_RET(ctx, ctx, name, NULL);
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100409 return ly_ctx_get_module_by(ctx, name, offsetof(struct lys_module, name), revision);
Radek Krejcib7db73a2018-10-24 14:18:40 +0200410}
411
412/**
413 * @brief Unifying function for ly_ctx_get_module_latest() and ly_ctx_get_module_latest_ns()
414 * @param[in] ctx Context where to search.
415 * @param[in] key Name or Namespace as a search key.
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100416 * @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 +0200417 * @return Matching module if any.
418 */
Radek Krejci0af46292019-01-11 16:02:31 +0100419static struct lys_module *
Radek Krejcib7db73a2018-10-24 14:18:40 +0200420ly_ctx_get_module_latest_by(const struct ly_ctx *ctx, const char *key, size_t key_offset)
421{
Radek Krejci0af46292019-01-11 16:02:31 +0100422 struct lys_module *mod;
Radek Krejcib7db73a2018-10-24 14:18:40 +0200423 unsigned int index = 0;
Radek Krejcib7db73a2018-10-24 14:18:40 +0200424
425 while ((mod = ly_ctx_get_module_by_iter(ctx, key, key_offset, &index))) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100426 if (mod->latest_revision) {
Radek Krejci9f5e6fb2018-10-25 09:26:12 +0200427 return mod;
Radek Krejcib7db73a2018-10-24 14:18:40 +0200428 }
429 }
430
Radek Krejci9f5e6fb2018-10-25 09:26:12 +0200431 return NULL;
Radek Krejcib7db73a2018-10-24 14:18:40 +0200432}
433
Radek Krejci0af46292019-01-11 16:02:31 +0100434API struct lys_module *
Radek Krejcib7db73a2018-10-24 14:18:40 +0200435ly_ctx_get_module_latest(const struct ly_ctx *ctx, const char *name)
436{
437 LY_CHECK_ARG_RET(ctx, ctx, name, NULL);
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100438 return ly_ctx_get_module_latest_by(ctx, name, offsetof(struct lys_module, name));
Radek Krejcib7db73a2018-10-24 14:18:40 +0200439}
440
Radek Krejci0af46292019-01-11 16:02:31 +0100441API struct lys_module *
Radek Krejcib7db73a2018-10-24 14:18:40 +0200442ly_ctx_get_module_latest_ns(const struct ly_ctx *ctx, const char *ns)
443{
444 LY_CHECK_ARG_RET(ctx, ctx, ns, NULL);
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100445 return ly_ctx_get_module_latest_by(ctx, ns, offsetof(struct lys_module, ns));
Radek Krejcib7db73a2018-10-24 14:18:40 +0200446}
447
448/**
449 * @brief Unifying function for ly_ctx_get_module_implemented() and ly_ctx_get_module_implemented_ns()
450 * @param[in] ctx Context where to search.
451 * @param[in] key Name or Namespace as a search key.
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100452 * @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 +0200453 * @return Matching module if any.
454 */
Radek Krejci0af46292019-01-11 16:02:31 +0100455static struct lys_module *
Radek Krejcib7db73a2018-10-24 14:18:40 +0200456ly_ctx_get_module_implemented_by(const struct ly_ctx *ctx, const char *key, size_t key_offset)
457{
Radek Krejci0af46292019-01-11 16:02:31 +0100458 struct lys_module *mod;
Radek Krejcib7db73a2018-10-24 14:18:40 +0200459 unsigned int index = 0;
460
461 while ((mod = ly_ctx_get_module_by_iter(ctx, key, key_offset, &index))) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100462 if (mod->implemented) {
Radek Krejcib7db73a2018-10-24 14:18:40 +0200463 return mod;
464 }
465 }
466
467 return NULL;
468}
469
Radek Krejci0af46292019-01-11 16:02:31 +0100470API struct lys_module *
Radek Krejcib7db73a2018-10-24 14:18:40 +0200471ly_ctx_get_module_implemented(const struct ly_ctx *ctx, const char *name)
472{
473 LY_CHECK_ARG_RET(ctx, ctx, name, NULL);
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100474 return ly_ctx_get_module_implemented_by(ctx, name, offsetof(struct lys_module, name));
Radek Krejcib7db73a2018-10-24 14:18:40 +0200475}
476
Radek Krejci0af46292019-01-11 16:02:31 +0100477API struct lys_module *
Radek Krejcib7db73a2018-10-24 14:18:40 +0200478ly_ctx_get_module_implemented_ns(const struct ly_ctx *ctx, const char *ns)
479{
480 LY_CHECK_ARG_RET(ctx, ctx, ns, NULL);
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100481 return ly_ctx_get_module_implemented_by(ctx, ns, offsetof(struct lys_module, ns));
Radek Krejcib7db73a2018-10-24 14:18:40 +0200482}
483
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100484struct lysp_submodule *
Radek Krejcid33273d2018-10-25 14:55:52 +0200485ly_ctx_get_submodule(const struct ly_ctx *ctx, const char *module, const char *submodule, const char *revision)
486{
487 const struct lys_module *mod;
488 struct lysp_include *inc;
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200489 uint32_t v;
490 LY_ARRAY_SIZE_TYPE u;
Radek Krejcid33273d2018-10-25 14:55:52 +0200491
Radek Krejcifaa1eac2018-10-30 14:34:55 +0100492 assert(submodule);
493
494 for (v = 0; v < ctx->list.count; ++v) {
495 mod = ctx->list.objs[v];
Radek Krejcid33273d2018-10-25 14:55:52 +0200496 if (!mod->parsed) {
497 continue;
498 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100499 if (module && strcmp(module, mod->name)) {
Radek Krejcifaa1eac2018-10-30 14:34:55 +0100500 continue;
501 }
Radek Krejcid33273d2018-10-25 14:55:52 +0200502
503 LY_ARRAY_FOR(mod->parsed->includes, u) {
Radek Krejci086c7132018-10-26 15:29:04 +0200504 if (mod->parsed->includes[u].submodule && !strcmp(submodule, mod->parsed->includes[u].submodule->name)) {
Radek Krejcid33273d2018-10-25 14:55:52 +0200505 inc = &mod->parsed->includes[u];
506 if (!revision) {
507 if (inc->submodule->latest_revision) {
508 return inc->submodule;
509 }
510 } else if (inc->submodule->revs && !strcmp(revision, inc->submodule->revs[0].date)) {
511 return inc->submodule;
512 }
513 }
514 }
515 }
516
517 return NULL;
518}
519
Radek Krejci0af5f5d2018-09-07 15:00:30 +0200520API void
Radek Krejcie9e987e2018-10-31 12:50:27 +0100521ly_ctx_reset_latests(struct ly_ctx *ctx)
522{
Radek Krejcie9e987e2018-10-31 12:50:27 +0100523 struct lys_module *mod;
524
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200525 for (uint32_t v = 0; v < ctx->list.count; ++v) {
526 mod = ctx->list.objs[v];
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100527 if (mod->latest_revision == 2) {
528 mod->latest_revision = 1;
Radek Krejcie9e987e2018-10-31 12:50:27 +0100529 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100530 if (mod->parsed && mod->parsed->includes) {
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200531 for (LY_ARRAY_SIZE_TYPE u = 0; u < LY_ARRAY_SIZE(mod->parsed->includes); ++u) {
532 if (mod->parsed->includes[u].submodule->latest_revision == 2) {
533 mod->parsed->includes[u].submodule->latest_revision = 1;
Radek Krejcie9e987e2018-10-31 12:50:27 +0100534 }
535 }
536 }
537 }
538}
539
Michal Vasko57c10cd2020-05-27 15:57:11 +0200540static LY_ERR
541ylib_feature(struct lyd_node *parent, const struct lys_module *cur_mod)
542{
543 LY_ARRAY_SIZE_TYPE i;
544 struct lyd_node *node;
545
546 if (!cur_mod->implemented) {
547 /* no features can be enabled */
548 return LY_SUCCESS;
549 }
550
551 LY_ARRAY_FOR(cur_mod->compiled->features, i) {
552 if (!(cur_mod->compiled->features[i].flags & LYS_FENABLED)) {
553 continue;
554 }
555
556 node = lyd_new_term(parent, NULL, "feature", cur_mod->compiled->features[i].name);
557 LY_CHECK_RET(!node, LY_EOTHER);
558 }
559
560 return LY_SUCCESS;
561}
562
563static LY_ERR
564ylib_deviation(struct lyd_node *parent, const struct lys_module *cur_mod, int bis)
565{
566 LY_ARRAY_SIZE_TYPE i;
567 struct lyd_node *node;
568 struct lys_module *mod;
569
570 if (!cur_mod->implemented) {
571 /* no deviations of the module for certain */
572 return LY_SUCCESS;
573 }
574
575 LY_ARRAY_FOR(cur_mod->compiled->deviated_by, i) {
576 mod = cur_mod->compiled->deviated_by[i];
577
578 if (bis) {
579 node = lyd_new_term(parent, NULL, "deviation", mod->name);
580 LY_CHECK_RET(!node, LY_EOTHER);
581 } else {
582 node = lyd_new_list(parent, NULL, "deviation", mod->name, (mod->parsed->revs ? mod->parsed->revs[0].date : ""));
583 LY_CHECK_RET(!node, LY_EOTHER);
584 }
585 }
586
587 return LY_SUCCESS;
588}
589
590static LY_ERR
591ylib_submodules(struct lyd_node *parent, const struct lys_module *cur_mod, int bis)
592{
593 LY_ARRAY_SIZE_TYPE i;
594 struct lyd_node *node, *cont;
595 struct lysp_submodule *submod;
596 int ret;
597 char *str;
598
599 LY_ARRAY_FOR(cur_mod->parsed->includes, i) {
600 submod = cur_mod->parsed->includes[i].submodule;
601
602 if (bis) {
603 cont = lyd_new_list(parent, NULL, "submodule", submod->name);
604 LY_CHECK_RET(!cont, LY_EOTHER);
605
606 if (submod->revs) {
607 node = lyd_new_term(cont, NULL, "revision", submod->revs[0].date);
608 LY_CHECK_RET(!node, LY_EOTHER);
609 }
610 } else {
611 cont = lyd_new_list(parent, NULL, "submodule", submod->name, (submod->revs ? submod->revs[0].date : ""));
612 LY_CHECK_RET(!cont, LY_EOTHER);
613 }
614
615 if (submod->filepath) {
616 ret = asprintf(&str, "file://%s", submod->filepath);
617 LY_CHECK_ERR_RET(ret == -1, LOGMEM(cur_mod->ctx), LY_EMEM);
618
619 node = lyd_new_term(cont, NULL, bis ? "location" : "schema", str);
620 LY_CHECK_RET(!node, LY_EOTHER);
621 free(str);
622 }
623 }
624
625 return LY_SUCCESS;
626}
627
628API uint16_t
629ly_ctx_get_yanglib_id(const struct ly_ctx *ctx)
630{
631 return ctx->module_set_id;
632}
633
634API struct lyd_node *
635ly_ctx_get_yanglib_data(const struct ly_ctx *ctx)
636{
637 uint32_t i;
638 int bis = 0, ret;
639 char id[8], *str;
640 const struct lys_module *mod;
641 struct lyd_node *root = NULL, *root_bis = NULL, *cont, *set_bis, *node;
642
643 LY_CHECK_ARG_RET(ctx, ctx, NULL);
644
645 mod = ly_ctx_get_module_implemented(ctx, "ietf-yang-library");
646 LY_CHECK_ERR_RET(!mod, LOGERR(ctx, LY_EINVAL, "Module \"ietf-yang-library\" is not implemented."), NULL);
647
648 if (mod->parsed->revs && !strcmp(mod->parsed->revs[0].date, "2016-06-21")) {
649 bis = 0;
650 } else if (mod->parsed->revs && !strcmp(mod->parsed->revs[0].date, IETF_YANG_LIB_REV)) {
651 bis = 1;
652 } else {
653 LOGERR(ctx, LY_EINVAL, "Incompatible ietf-yang-library version in context.");
654 return NULL;
655 }
656
657 root = lyd_new_inner(NULL, mod, "modules-state");
658 LY_CHECK_GOTO(!root, error);
659
660 if (bis) {
661 root_bis = lyd_new_inner(NULL, mod, "yang-library");
662 LY_CHECK_GOTO(!root_bis, error);
663
664 set_bis = lyd_new_list(root_bis, NULL, "module-set", "complete");
665 LY_CHECK_GOTO(!set_bis, error);
666 }
667
668 for (i = 0; i < ctx->list.count; ++i) {
669 mod = ctx->list.objs[i];
670
671 /*
672 * deprecated legacy
673 */
674 cont = lyd_new_list(root, NULL, "module", mod->name, (mod->parsed->revs ? mod->parsed->revs[0].date : ""));
675 LY_CHECK_GOTO(!cont, error);
676
677 /* schema */
678 if (mod->filepath) {
679 ret = asprintf(&str, "file://%s", mod->filepath);
680 LY_CHECK_ERR_GOTO(ret == -1, LOGMEM(ctx), error);
681
682 node = lyd_new_term(cont, NULL, "schema", str);
683 free(str);
684 LY_CHECK_GOTO(!node, error);
685 }
686
687 /* namespace */
688 node = lyd_new_term(cont, NULL, "namespace", mod->ns);
689 LY_CHECK_GOTO(!node, error);
690
691 /* feature leaf-list */
692 LY_CHECK_GOTO(ylib_feature(cont, mod), error);
693
694 /* deviation list */
695 LY_CHECK_GOTO(ylib_deviation(cont, mod, 0), error);
696
697 /* conformance-type */
698 node = lyd_new_term(cont, NULL, "conformance-type", (mod->implemented ? "implement" : "import"));
699 LY_CHECK_GOTO(!node, error);
700
701 /* submodule list */
702 LY_CHECK_GOTO(ylib_submodules(cont, mod, 0), error);
703
704 /*
705 * current revision
706 */
707 if (bis) {
708 /* name and revision */
709 if (mod->implemented) {
710 cont = lyd_new_list(set_bis, NULL, "module", mod->name);
711 LY_CHECK_GOTO(!cont, error);
712
713 if (mod->parsed->revs) {
714 node = lyd_new_term(cont, NULL, "revision", mod->parsed->revs[0].date);
715 LY_CHECK_GOTO(!node, error);
716 }
717 } else {
718 cont = lyd_new_list(set_bis, NULL, "import-only-module", mod->name,
719 (mod->parsed->revs ? mod->parsed->revs[0].date : ""));
720 LY_CHECK_GOTO(!cont, error);
721 }
722
723 /* namespace */
724 node = lyd_new_term(cont, NULL, "namespace", mod->ns);
725 LY_CHECK_GOTO(!node, error);
726
727 /* location */
728 if (mod->filepath) {
729 ret = asprintf(&str, "file://%s", mod->filepath);
730 LY_CHECK_ERR_GOTO(ret == -1, LOGMEM(ctx), error);
731
732 node = lyd_new_term(cont, NULL, "schema", str);
733 free(str);
734 LY_CHECK_GOTO(!node, error);
735 }
736
737 /* submodule list */
738 LY_CHECK_GOTO(ylib_submodules(cont, mod, 1), error);
739
740 /* feature list */
741 LY_CHECK_GOTO(ylib_feature(cont, mod), error);
742
743 /* deviation */
744 LY_CHECK_GOTO(ylib_deviation(cont, mod, 1), error);
745 }
746 }
747
748 /* IDs */
749 sprintf(id, "%u", ctx->module_set_id);
750 node = lyd_new_term(root, NULL, "module-set-id", id);
751 LY_CHECK_GOTO(!node, error);
752
753 if (bis) {
754 /* create one complete schema */
755 cont = lyd_new_list(root_bis, NULL, "schema", "complete");
756 LY_CHECK_GOTO(!cont, error);
757
758 node = lyd_new_term(cont, NULL, "module-set", "complete");
759 LY_CHECK_GOTO(!node, error);
760
761 /* content-id */
762 node = lyd_new_term(root_bis, NULL, "content-id", id);
763 LY_CHECK_GOTO(!node, error);
764 }
765
766 if (root_bis) {
767 if (lyd_insert_sibling(root_bis, root)) {
768 goto error;
769 }
770 root = root_bis;
771 root_bis = 0;
772 }
773
Michal Vasko57c10cd2020-05-27 15:57:11 +0200774 if (lyd_validate(&root, NULL, LYD_VALOPT_DATA_ONLY)) {
775 goto error;
Michal Vasko00cbf532020-06-15 13:58:47 +0200776 }
Michal Vasko57c10cd2020-05-27 15:57:11 +0200777
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}