blob: 7e498ee3694ab0cacfc00b1a3ccbc48927247300 [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 Krejci7931b192020-06-25 17:05:03 +020030#include "parser_data.h"
Radek Krejcica376bd2020-06-11 16:04:06 +020031#include "plugins_types.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020032#include "set.h"
33#include "tree.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020034#include "tree_data.h"
Radek Krejcica376bd2020-06-11 16:04:06 +020035#include "tree_schema.h"
Radek Krejci86d106e2018-10-18 09:53:19 +020036#include "tree_schema_internal.h"
Radek Krejci0af5f5d2018-09-07 15:00:30 +020037
Radek Krejci86d106e2018-10-18 09:53:19 +020038#define LY_INTERNAL_MODS_COUNT 6
Radek Krejci0af5f5d2018-09-07 15:00:30 +020039
Radek Krejcif3f47842018-11-15 11:22:15 +010040#include "../models/ietf-yang-metadata@2016-08-05.h"
Michal Vaskoe893ddd2020-06-23 13:35:20 +020041#include "../models/yang@2020-06-17.h"
Radek Krejcif3f47842018-11-15 11:22:15 +010042#include "../models/ietf-inet-types@2013-07-15.h"
43#include "../models/ietf-yang-types@2013-07-15.h"
Michal Vaskocf296c82020-05-27 11:16:45 +020044#include "../models/ietf-datastores@2018-02-14.h"
45#include "../models/ietf-yang-library@2019-01-04.h"
46#define IETF_YANG_LIB_REV "2019-01-04"
Radek Krejci0af5f5d2018-09-07 15:00:30 +020047
Radek Krejci0af5f5d2018-09-07 15:00:30 +020048static struct internal_modules_s {
49 const char *name;
50 const char *revision;
51 const char *data;
52 uint8_t implemented;
53 LYS_INFORMAT format;
54} internal_modules[LY_INTERNAL_MODS_COUNT] = {
Radek Krejci335332a2019-09-05 13:03:35 +020055 {"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 +020056 {"yang", "2020-06-17", (const char*)yang_2020_06_17_yang, 1, LYS_IN_YANG},
Radek Krejci86d106e2018-10-18 09:53:19 +020057 {"ietf-inet-types", "2013-07-15", (const char*)ietf_inet_types_2013_07_15_yang, 0, LYS_IN_YANG},
58 {"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 +020059 /* ietf-datastores and ietf-yang-library must be right here at the end of the list! */
Michal Vaskocf296c82020-05-27 11:16:45 +020060 {"ietf-datastores", "2018-02-14", (const char*)ietf_datastores_2018_02_14_yang, 1, LYS_IN_YANG},
61 {"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 +020062};
63
64API LY_ERR
65ly_ctx_set_searchdir(struct ly_ctx *ctx, const char *search_dir)
66{
Radek Krejci0e212402018-09-20 11:29:38 +020067 struct stat st;
Radek Krejci0af5f5d2018-09-07 15:00:30 +020068 char *new_dir = NULL;
Radek Krejci0e212402018-09-20 11:29:38 +020069 unsigned int u;
Radek Krejci0af5f5d2018-09-07 15:00:30 +020070
71 LY_CHECK_ARG_RET(ctx, ctx, LY_EINVAL);
72
73 if (search_dir) {
Radek Krejci0af5f5d2018-09-07 15:00:30 +020074 new_dir = realpath(search_dir, NULL);
Radek Krejcia77e0e12018-09-20 12:39:15 +020075 LY_CHECK_ERR_RET(!new_dir,
Radek Krejci3a077b92019-04-09 17:10:35 +020076 LOGERR(ctx, LY_ESYS, "Unable to use search directory \"%s\" (%s).", search_dir, strerror(errno)),
77 LY_EINVAL);
78 if (strcmp(search_dir, new_dir)) {
79 LOGVRB("Canonicalizing search directory string from \"%s\" to \"%s\".", search_dir, new_dir);
80 }
81 LY_CHECK_ERR_RET(access(new_dir, R_OK | X_OK),
82 LOGERR(ctx, LY_ESYS, "Unable to fully access search directory \"%s\" (%s).", new_dir, strerror(errno)); free(new_dir),
83 LY_EINVAL);
84 LY_CHECK_ERR_RET(stat(new_dir, &st),
85 LOGERR(ctx, LY_ESYS, "stat() failed for \"%s\" (%s).", new_dir, strerror(errno)); free(new_dir),
Radek Krejcia77e0e12018-09-20 12:39:15 +020086 LY_ESYS);
Radek Krejci3a077b92019-04-09 17:10:35 +020087 LY_CHECK_ERR_RET(!S_ISDIR(st.st_mode),
88 LOGERR(ctx, LY_ESYS, "Given search directory \"%s\" is not a directory.", new_dir); free(new_dir),
89 LY_EINVAL);
Radek Krejci0e212402018-09-20 11:29:38 +020090 /* avoid path duplication */
91 for (u = 0; u < ctx->search_paths.count; ++u) {
Radek Krejci14946ab2018-09-20 13:42:06 +020092 if (!strcmp(new_dir, ctx->search_paths.objs[u])) {
Radek Krejci0e212402018-09-20 11:29:38 +020093 free(new_dir);
Radek Krejci14946ab2018-09-20 13:42:06 +020094 return LY_EEXIST;
Radek Krejci0e212402018-09-20 11:29:38 +020095 }
96 }
97 if (ly_set_add(&ctx->search_paths, new_dir, LY_SET_OPT_USEASLIST) == -1) {
Radek Krejci0af5f5d2018-09-07 15:00:30 +020098 free(new_dir);
99 return LY_EMEM;
100 }
101
Radek Krejcie9e987e2018-10-31 12:50:27 +0100102 /* new searchdir - possibly more latest revision available */
103 ly_ctx_reset_latests(ctx);
104
Radek Krejci0e212402018-09-20 11:29:38 +0200105 return LY_SUCCESS;
Radek Krejci0af5f5d2018-09-07 15:00:30 +0200106 } else {
107 /* consider that no change is not actually an error */
108 return LY_SUCCESS;
109 }
Radek Krejci0af5f5d2018-09-07 15:00:30 +0200110}
111
112API const char * const *
113ly_ctx_get_searchdirs(const struct ly_ctx *ctx)
114{
Radek Krejci05026432018-09-20 12:13:43 +0200115 void **new;
116
Radek Krejci0af5f5d2018-09-07 15:00:30 +0200117 LY_CHECK_ARG_RET(ctx, ctx, NULL);
Radek Krejci05026432018-09-20 12:13:43 +0200118
119 if (ctx->search_paths.count == ctx->search_paths.size) {
120 /* not enough space for terminating NULL byte */
121 new = realloc(((struct ly_ctx *)ctx)->search_paths.objs, (ctx->search_paths.size + 8) * sizeof *ctx->search_paths.objs);
122 LY_CHECK_ERR_RET(!new, LOGMEM(NULL), NULL);
123 ((struct ly_ctx *)ctx)->search_paths.size += 8;
124 ((struct ly_ctx *)ctx)->search_paths.objs = new;
125 }
126 /* set terminating NULL byte to the strings list */
127 ctx->search_paths.objs[ctx->search_paths.count] = NULL;
128
Radek Krejci0af5f5d2018-09-07 15:00:30 +0200129 return (const char * const *)ctx->search_paths.objs;
130}
131
132API LY_ERR
Radek Krejci0759b792018-09-20 13:53:15 +0200133ly_ctx_unset_searchdirs(struct ly_ctx *ctx, const char *value)
Radek Krejci0af5f5d2018-09-07 15:00:30 +0200134{
Radek Krejci0759b792018-09-20 13:53:15 +0200135 unsigned int index;
136
Radek Krejcicd649072018-09-20 12:14:45 +0200137 LY_CHECK_ARG_RET(ctx, ctx, LY_EINVAL);
Radek Krejcicd649072018-09-20 12:14:45 +0200138
Michal Vaskob34480a2018-09-17 10:34:45 +0200139 if (!ctx->search_paths.count) {
Radek Krejci0af5f5d2018-09-07 15:00:30 +0200140 return LY_SUCCESS;
141 }
142
Radek Krejci0759b792018-09-20 13:53:15 +0200143 if (value) {
Radek Krejci0af5f5d2018-09-07 15:00:30 +0200144 /* remove specific search directory */
Radek Krejci0759b792018-09-20 13:53:15 +0200145 for (index = 0; index < ctx->search_paths.count; ++index) {
146 if (!strcmp(value, ctx->search_paths.objs[index])) {
147 break;
148 }
149 }
150 if (index == ctx->search_paths.count) {
151 LOGARG(ctx, value);
152 return LY_EINVAL;
153 } else {
154 return ly_set_rm_index(&ctx->search_paths, index, free);
155 }
Radek Krejci0af5f5d2018-09-07 15:00:30 +0200156 } else {
157 /* remove them all */
Radek Krejcia40f21b2018-09-18 10:42:08 +0200158 ly_set_erase(&ctx->search_paths, free);
Radek Krejci0af5f5d2018-09-07 15:00:30 +0200159 memset(&ctx->search_paths, 0, sizeof ctx->search_paths);
160 }
161
162 return LY_SUCCESS;
163}
164
165API LY_ERR
Radek Krejcied5acc52019-04-25 15:57:04 +0200166ly_ctx_unset_searchdir(struct ly_ctx *ctx, unsigned int index)
167{
168 LY_CHECK_ARG_RET(ctx, ctx, LY_EINVAL);
169
170 if (!ctx->search_paths.count) {
171 return LY_SUCCESS;
172 }
173
174 if (index >= ctx->search_paths.count) {
175 LOGARG(ctx, value);
176 return LY_EINVAL;
177 } else {
178 return ly_set_rm_index(&ctx->search_paths, index, free);
179 }
180
181 return LY_SUCCESS;
182}
183
184API const struct lys_module *
185ly_ctx_load_module(struct ly_ctx *ctx, const char *name, const char *revision)
186{
187 struct lys_module *result = NULL;
188
189 LY_CHECK_ARG_RET(ctx, ctx, name, NULL);
190
191 LY_CHECK_RET(lysp_load_module(ctx, name, revision, 1, 0, &result), NULL);
192 return result;
193}
194
195API LY_ERR
Radek Krejci0af5f5d2018-09-07 15:00:30 +0200196ly_ctx_new(const char *search_dir, int options, struct ly_ctx **new_ctx)
197{
198 struct ly_ctx *ctx = NULL;
199 struct lys_module *module;
200 char *search_dir_list;
201 char *sep, *dir;
Radek Krejci86d106e2018-10-18 09:53:19 +0200202 int i;
Radek Krejci0af5f5d2018-09-07 15:00:30 +0200203 LY_ERR rc = LY_SUCCESS;
Radek Krejci0af5f5d2018-09-07 15:00:30 +0200204
Radek Krejcied5acc52019-04-25 15:57:04 +0200205 LY_CHECK_ARG_RET(NULL, new_ctx, LY_EINVAL);
206
Radek Krejci0af5f5d2018-09-07 15:00:30 +0200207 ctx = calloc(1, sizeof *ctx);
Radek Krejciad573502018-09-07 15:26:55 +0200208 LY_CHECK_ERR_RET(!ctx, LOGMEM(NULL), LY_EMEM);
Radek Krejci0af5f5d2018-09-07 15:00:30 +0200209
210 /* dictionary */
211 lydict_init(&ctx->dict);
212
Radek Krejciad573502018-09-07 15:26:55 +0200213#if 0 /* TODO when plugins implemented */
Radek Krejci0af5f5d2018-09-07 15:00:30 +0200214 /* plugins */
215 ly_load_plugins();
Radek Krejciad573502018-09-07 15:26:55 +0200216#endif
Radek Krejci0af5f5d2018-09-07 15:00:30 +0200217
218 /* initialize thread-specific key */
Radek Krejciad573502018-09-07 15:26:55 +0200219 while ((pthread_key_create(&ctx->errlist_key, ly_err_free)) == EAGAIN);
Radek Krejci0af5f5d2018-09-07 15:00:30 +0200220
221 /* models list */
222 ctx->flags = options;
223 if (search_dir) {
224 search_dir_list = strdup(search_dir);
225 LY_CHECK_ERR_GOTO(!search_dir_list, LOGMEM(NULL); rc = LY_EMEM, error);
226
227 for (dir = search_dir_list; (sep = strchr(dir, ':')) != NULL && rc == LY_SUCCESS; dir = sep + 1) {
228 *sep = 0;
229 rc = ly_ctx_set_searchdir(ctx, dir);
Radek Krejci14946ab2018-09-20 13:42:06 +0200230 if (rc == LY_EEXIST) {
231 /* ignore duplication */
232 rc = LY_SUCCESS;
233 }
Radek Krejci0af5f5d2018-09-07 15:00:30 +0200234 }
235 if (*dir && rc == LY_SUCCESS) {
236 rc = ly_ctx_set_searchdir(ctx, dir);
Radek Krejci14946ab2018-09-20 13:42:06 +0200237 if (rc == LY_EEXIST) {
238 /* ignore duplication */
239 rc = LY_SUCCESS;
240 }
Radek Krejci0af5f5d2018-09-07 15:00:30 +0200241 }
242 free(search_dir_list);
243
244 /* If ly_ctx_set_searchdir() failed, the error is already logged. Just exit */
245 if (rc != LY_SUCCESS) {
246 goto error;
247 }
248 }
249 ctx->module_set_id = 1;
250
251 /* load internal modules */
Radek Krejci86d106e2018-10-18 09:53:19 +0200252 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 +0100253 module = (struct lys_module *)lys_parse_mem_module(ctx, internal_modules[i].data, internal_modules[i].format,
254 internal_modules[i].implemented, NULL, NULL);
Radek Krejci0e75e6f2018-11-08 09:40:02 +0100255 LY_CHECK_ERR_GOTO(!module, rc = ly_errcode(ctx), error);
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200256 LY_CHECK_GOTO((rc = lys_compile(&module, 0)), error);
Radek Krejci0af5f5d2018-09-07 15:00:30 +0200257 }
258
259 *new_ctx = ctx;
260 return rc;
261
262error:
263 ly_ctx_destroy(ctx, NULL);
264 return rc;
265}
Radek Krejci0af5f5d2018-09-07 15:00:30 +0200266API int
Radek Krejci3fbe89a2018-09-20 13:54:46 +0200267ly_ctx_get_options(const struct ly_ctx *ctx)
Radek Krejci0af5f5d2018-09-07 15:00:30 +0200268{
Radek Krejci3fbe89a2018-09-20 13:54:46 +0200269 LY_CHECK_ARG_RET(ctx, ctx, 0);
Radek Krejci0af5f5d2018-09-07 15:00:30 +0200270 return ctx->flags;
271}
272
Radek Krejcica828d92018-09-20 14:19:43 +0200273API LY_ERR
Radek Krejci3fa46b62019-09-11 10:47:30 +0200274ly_ctx_set_options(struct ly_ctx *ctx, int option)
Radek Krejcica828d92018-09-20 14:19:43 +0200275{
276 LY_CHECK_ARG_RET(ctx, ctx, LY_EINVAL);
277 LY_CHECK_ERR_RET(option & LY_CTX_NOYANGLIBRARY, LOGARG(ctx, option), LY_EINVAL);
278
279 /* set the option(s) */
280 ctx->flags |= option;
281
282 return LY_SUCCESS;
283}
284
285API LY_ERR
Radek Krejci3fa46b62019-09-11 10:47:30 +0200286ly_ctx_unset_options(struct ly_ctx *ctx, int option)
Radek Krejcica828d92018-09-20 14:19:43 +0200287{
288 LY_CHECK_ARG_RET(ctx, ctx, LY_EINVAL);
289 LY_CHECK_ERR_RET(option & LY_CTX_NOYANGLIBRARY, LOGARG(ctx, option), LY_EINVAL);
290
291 /* unset the option(s) */
292 ctx->flags &= ~option;
293
294 return LY_SUCCESS;
295}
296
Radek Krejci0af5f5d2018-09-07 15:00:30 +0200297API uint16_t
298ly_ctx_get_module_set_id(const struct ly_ctx *ctx)
299{
Radek Krejci3fbe89a2018-09-20 13:54:46 +0200300 LY_CHECK_ARG_RET(ctx, ctx, 0);
Radek Krejci0af5f5d2018-09-07 15:00:30 +0200301 return ctx->module_set_id;
302}
303
Radek Krejcid33273d2018-10-25 14:55:52 +0200304API void
305ly_ctx_set_module_imp_clb(struct ly_ctx *ctx, ly_module_imp_clb clb, void *user_data)
306{
307 LY_CHECK_ARG_RET(ctx, ctx,);
308
309 ctx->imp_clb = clb;
310 ctx->imp_clb_data = user_data;
311}
312
313API ly_module_imp_clb
314ly_ctx_get_module_imp_clb(const struct ly_ctx *ctx, void **user_data)
315{
316 LY_CHECK_ARG_RET(ctx, ctx, NULL);
317
318 if (user_data) {
319 *user_data = ctx->imp_clb_data;
320 }
321 return ctx->imp_clb;
322}
323
Radek Krejcied5acc52019-04-25 15:57:04 +0200324API const struct lys_module *
325ly_ctx_get_module_iter(const struct ly_ctx *ctx, unsigned int *index)
326{
327 LY_CHECK_ARG_RET(ctx, ctx, index, NULL);
328
Radek Krejci2390fb52019-04-30 13:27:43 +0200329 if (*index < (unsigned)ctx->list.count) {
Radek Krejcied5acc52019-04-25 15:57:04 +0200330 return ctx->list.objs[(*index)++];
Radek Krejci2390fb52019-04-30 13:27:43 +0200331 } else {
332 return NULL;
Radek Krejcied5acc52019-04-25 15:57:04 +0200333 }
Radek Krejcied5acc52019-04-25 15:57:04 +0200334}
335
Radek Krejcib7db73a2018-10-24 14:18:40 +0200336/**
337 * @brief Iterate over the modules in the given context. Returned modules must match the given key at the offset of
338 * lysp_module and lysc_module structures (they are supposed to be placed at the same offset in both structures).
339 *
340 * @param[in] ctx Context where to iterate.
341 * @param[in] key Key value to search for.
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100342 * @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 +0200343 * @param[in,out] Iterator to pass between the function calls. On the first call, the variable is supposed to be
344 * initiated to 0. After each call returning a module, the value is greater by 1 than the index of the returned
345 * module in the context.
346 * @return Module matching the given key, NULL if no such module found.
347 */
Radek Krejci0af46292019-01-11 16:02:31 +0100348static struct lys_module *
Radek Krejcib7db73a2018-10-24 14:18:40 +0200349ly_ctx_get_module_by_iter(const struct ly_ctx *ctx, const char *key, size_t key_offset, unsigned int *index)
350{
Radek Krejci0af46292019-01-11 16:02:31 +0100351 struct lys_module *mod;
Radek Krejcib7db73a2018-10-24 14:18:40 +0200352 const char *value;
353
354 for (; *index < ctx->list.count; ++(*index)) {
355 mod = ctx->list.objs[*index];
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100356 value = *(const char**)(((int8_t*)(mod)) + key_offset);
Radek Krejcib7db73a2018-10-24 14:18:40 +0200357 if (!strcmp(key, value)) {
358 /* increment index for the next run */
359 ++(*index);
360 return mod;
361 }
362 }
363 /* done */
364 return NULL;
365}
366
367/**
368 * @brief Unifying function for ly_ctx_get_module() and ly_ctx_get_module_ns()
369 * @param[in] ctx Context where to search.
370 * @param[in] key Name or Namespace as a search key.
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100371 * @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 +0200372 * @param[in] revision Revision date to match. If NULL, the matching module must have no revision. To search for the latest
373 * revision module, use ly_ctx_get_module_latest_by().
374 * @return Matching module if any.
375 */
Radek Krejci0af46292019-01-11 16:02:31 +0100376static struct lys_module *
Radek Krejcib7db73a2018-10-24 14:18:40 +0200377ly_ctx_get_module_by(const struct ly_ctx *ctx, const char *key, size_t key_offset, const char *revision)
378{
Radek Krejci0af46292019-01-11 16:02:31 +0100379 struct lys_module *mod;
Radek Krejcib7db73a2018-10-24 14:18:40 +0200380 unsigned int index = 0;
381
382 while ((mod = ly_ctx_get_module_by_iter(ctx, key, key_offset, &index))) {
383 if (!revision) {
Radek Krejci0af46292019-01-11 16:02:31 +0100384 if (!mod->revision) {
Radek Krejcib7db73a2018-10-24 14:18:40 +0200385 /* found requested module without revision */
386 return mod;
387 }
388 } else {
Radek Krejci0af46292019-01-11 16:02:31 +0100389 if (mod->revision && !strcmp(mod->revision, revision)) {
Radek Krejcib7db73a2018-10-24 14:18:40 +0200390 /* found requested module of the specific revision */
391 return mod;
392 }
393 }
394 }
395
396 return NULL;
397}
398
Radek Krejci0af46292019-01-11 16:02:31 +0100399API struct lys_module *
Radek Krejcib7db73a2018-10-24 14:18:40 +0200400ly_ctx_get_module_ns(const struct ly_ctx *ctx, const char *ns, const char *revision)
401{
402 LY_CHECK_ARG_RET(ctx, ctx, ns, NULL);
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100403 return ly_ctx_get_module_by(ctx, ns, offsetof(struct lys_module, ns), revision);
Radek Krejcib7db73a2018-10-24 14:18:40 +0200404}
405
Radek Krejci0af46292019-01-11 16:02:31 +0100406API struct lys_module *
Radek Krejcib7db73a2018-10-24 14:18:40 +0200407ly_ctx_get_module(const struct ly_ctx *ctx, const char *name, const char *revision)
408{
409 LY_CHECK_ARG_RET(ctx, ctx, name, NULL);
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100410 return ly_ctx_get_module_by(ctx, name, offsetof(struct lys_module, name), revision);
Radek Krejcib7db73a2018-10-24 14:18:40 +0200411}
412
413/**
414 * @brief Unifying function for ly_ctx_get_module_latest() and ly_ctx_get_module_latest_ns()
415 * @param[in] ctx Context where to search.
416 * @param[in] key Name or Namespace as a search key.
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100417 * @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 +0200418 * @return Matching module if any.
419 */
Radek Krejci0af46292019-01-11 16:02:31 +0100420static struct lys_module *
Radek Krejcib7db73a2018-10-24 14:18:40 +0200421ly_ctx_get_module_latest_by(const struct ly_ctx *ctx, const char *key, size_t key_offset)
422{
Radek Krejci0af46292019-01-11 16:02:31 +0100423 struct lys_module *mod;
Radek Krejcib7db73a2018-10-24 14:18:40 +0200424 unsigned int index = 0;
Radek Krejcib7db73a2018-10-24 14:18:40 +0200425
426 while ((mod = ly_ctx_get_module_by_iter(ctx, key, key_offset, &index))) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100427 if (mod->latest_revision) {
Radek Krejci9f5e6fb2018-10-25 09:26:12 +0200428 return mod;
Radek Krejcib7db73a2018-10-24 14:18:40 +0200429 }
430 }
431
Radek Krejci9f5e6fb2018-10-25 09:26:12 +0200432 return NULL;
Radek Krejcib7db73a2018-10-24 14:18:40 +0200433}
434
Radek Krejci0af46292019-01-11 16:02:31 +0100435API struct lys_module *
Radek Krejcib7db73a2018-10-24 14:18:40 +0200436ly_ctx_get_module_latest(const struct ly_ctx *ctx, const char *name)
437{
438 LY_CHECK_ARG_RET(ctx, ctx, name, NULL);
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100439 return ly_ctx_get_module_latest_by(ctx, name, offsetof(struct lys_module, name));
Radek Krejcib7db73a2018-10-24 14:18:40 +0200440}
441
Radek Krejci0af46292019-01-11 16:02:31 +0100442API struct lys_module *
Radek Krejcib7db73a2018-10-24 14:18:40 +0200443ly_ctx_get_module_latest_ns(const struct ly_ctx *ctx, const char *ns)
444{
445 LY_CHECK_ARG_RET(ctx, ctx, ns, NULL);
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100446 return ly_ctx_get_module_latest_by(ctx, ns, offsetof(struct lys_module, ns));
Radek Krejcib7db73a2018-10-24 14:18:40 +0200447}
448
449/**
450 * @brief Unifying function for ly_ctx_get_module_implemented() and ly_ctx_get_module_implemented_ns()
451 * @param[in] ctx Context where to search.
452 * @param[in] key Name or Namespace as a search key.
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100453 * @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 +0200454 * @return Matching module if any.
455 */
Radek Krejci0af46292019-01-11 16:02:31 +0100456static struct lys_module *
Radek Krejcib7db73a2018-10-24 14:18:40 +0200457ly_ctx_get_module_implemented_by(const struct ly_ctx *ctx, const char *key, size_t key_offset)
458{
Radek Krejci0af46292019-01-11 16:02:31 +0100459 struct lys_module *mod;
Radek Krejcib7db73a2018-10-24 14:18:40 +0200460 unsigned int index = 0;
461
462 while ((mod = ly_ctx_get_module_by_iter(ctx, key, key_offset, &index))) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100463 if (mod->implemented) {
Radek Krejcib7db73a2018-10-24 14:18:40 +0200464 return mod;
465 }
466 }
467
468 return NULL;
469}
470
Radek Krejci0af46292019-01-11 16:02:31 +0100471API struct lys_module *
Radek Krejcib7db73a2018-10-24 14:18:40 +0200472ly_ctx_get_module_implemented(const struct ly_ctx *ctx, const char *name)
473{
474 LY_CHECK_ARG_RET(ctx, ctx, name, NULL);
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100475 return ly_ctx_get_module_implemented_by(ctx, name, offsetof(struct lys_module, name));
Radek Krejcib7db73a2018-10-24 14:18:40 +0200476}
477
Radek Krejci0af46292019-01-11 16:02:31 +0100478API struct lys_module *
Radek Krejcib7db73a2018-10-24 14:18:40 +0200479ly_ctx_get_module_implemented_ns(const struct ly_ctx *ctx, const char *ns)
480{
481 LY_CHECK_ARG_RET(ctx, ctx, ns, NULL);
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100482 return ly_ctx_get_module_implemented_by(ctx, ns, offsetof(struct lys_module, ns));
Radek Krejcib7db73a2018-10-24 14:18:40 +0200483}
484
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100485struct lysp_submodule *
Radek Krejcid33273d2018-10-25 14:55:52 +0200486ly_ctx_get_submodule(const struct ly_ctx *ctx, const char *module, const char *submodule, const char *revision)
487{
488 const struct lys_module *mod;
489 struct lysp_include *inc;
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200490 uint32_t v;
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200491 LY_ARRAY_COUNT_TYPE u;
Radek Krejcid33273d2018-10-25 14:55:52 +0200492
Radek Krejcifaa1eac2018-10-30 14:34:55 +0100493 assert(submodule);
494
495 for (v = 0; v < ctx->list.count; ++v) {
496 mod = ctx->list.objs[v];
Radek Krejcid33273d2018-10-25 14:55:52 +0200497 if (!mod->parsed) {
498 continue;
499 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100500 if (module && strcmp(module, mod->name)) {
Radek Krejcifaa1eac2018-10-30 14:34:55 +0100501 continue;
502 }
Radek Krejcid33273d2018-10-25 14:55:52 +0200503
504 LY_ARRAY_FOR(mod->parsed->includes, u) {
Radek Krejci086c7132018-10-26 15:29:04 +0200505 if (mod->parsed->includes[u].submodule && !strcmp(submodule, mod->parsed->includes[u].submodule->name)) {
Radek Krejcid33273d2018-10-25 14:55:52 +0200506 inc = &mod->parsed->includes[u];
507 if (!revision) {
508 if (inc->submodule->latest_revision) {
509 return inc->submodule;
510 }
511 } else if (inc->submodule->revs && !strcmp(revision, inc->submodule->revs[0].date)) {
512 return inc->submodule;
513 }
514 }
515 }
516 }
517
518 return NULL;
519}
520
Radek Krejci0af5f5d2018-09-07 15:00:30 +0200521API void
Radek Krejcie9e987e2018-10-31 12:50:27 +0100522ly_ctx_reset_latests(struct ly_ctx *ctx)
523{
Radek Krejcie9e987e2018-10-31 12:50:27 +0100524 struct lys_module *mod;
525
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200526 for (uint32_t v = 0; v < ctx->list.count; ++v) {
527 mod = ctx->list.objs[v];
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100528 if (mod->latest_revision == 2) {
529 mod->latest_revision = 1;
Radek Krejcie9e987e2018-10-31 12:50:27 +0100530 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100531 if (mod->parsed && mod->parsed->includes) {
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200532 for (LY_ARRAY_COUNT_TYPE u = 0; u < LY_ARRAY_COUNT(mod->parsed->includes); ++u) {
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200533 if (mod->parsed->includes[u].submodule->latest_revision == 2) {
534 mod->parsed->includes[u].submodule->latest_revision = 1;
Radek Krejcie9e987e2018-10-31 12:50:27 +0100535 }
536 }
537 }
538 }
539}
540
Michal Vasko57c10cd2020-05-27 15:57:11 +0200541static LY_ERR
542ylib_feature(struct lyd_node *parent, const struct lys_module *cur_mod)
543{
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200544 LY_ARRAY_COUNT_TYPE i;
Michal Vasko57c10cd2020-05-27 15:57:11 +0200545 struct lyd_node *node;
546
547 if (!cur_mod->implemented) {
548 /* no features can be enabled */
549 return LY_SUCCESS;
550 }
551
552 LY_ARRAY_FOR(cur_mod->compiled->features, i) {
553 if (!(cur_mod->compiled->features[i].flags & LYS_FENABLED)) {
554 continue;
555 }
556
557 node = lyd_new_term(parent, NULL, "feature", cur_mod->compiled->features[i].name);
558 LY_CHECK_RET(!node, LY_EOTHER);
559 }
560
561 return LY_SUCCESS;
562}
563
564static LY_ERR
565ylib_deviation(struct lyd_node *parent, const struct lys_module *cur_mod, int bis)
566{
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200567 LY_ARRAY_COUNT_TYPE i;
Michal Vasko57c10cd2020-05-27 15:57:11 +0200568 struct lyd_node *node;
569 struct lys_module *mod;
570
571 if (!cur_mod->implemented) {
572 /* no deviations of the module for certain */
573 return LY_SUCCESS;
574 }
575
576 LY_ARRAY_FOR(cur_mod->compiled->deviated_by, i) {
577 mod = cur_mod->compiled->deviated_by[i];
578
579 if (bis) {
580 node = lyd_new_term(parent, NULL, "deviation", mod->name);
581 LY_CHECK_RET(!node, LY_EOTHER);
582 } else {
583 node = lyd_new_list(parent, NULL, "deviation", mod->name, (mod->parsed->revs ? mod->parsed->revs[0].date : ""));
584 LY_CHECK_RET(!node, LY_EOTHER);
585 }
586 }
587
588 return LY_SUCCESS;
589}
590
591static LY_ERR
592ylib_submodules(struct lyd_node *parent, const struct lys_module *cur_mod, int bis)
593{
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200594 LY_ARRAY_COUNT_TYPE i;
Michal Vasko57c10cd2020-05-27 15:57:11 +0200595 struct lyd_node *node, *cont;
596 struct lysp_submodule *submod;
597 int ret;
598 char *str;
599
600 LY_ARRAY_FOR(cur_mod->parsed->includes, i) {
601 submod = cur_mod->parsed->includes[i].submodule;
602
603 if (bis) {
604 cont = lyd_new_list(parent, NULL, "submodule", submod->name);
605 LY_CHECK_RET(!cont, LY_EOTHER);
606
607 if (submod->revs) {
608 node = lyd_new_term(cont, NULL, "revision", submod->revs[0].date);
609 LY_CHECK_RET(!node, LY_EOTHER);
610 }
611 } else {
612 cont = lyd_new_list(parent, NULL, "submodule", submod->name, (submod->revs ? submod->revs[0].date : ""));
613 LY_CHECK_RET(!cont, LY_EOTHER);
614 }
615
616 if (submod->filepath) {
617 ret = asprintf(&str, "file://%s", submod->filepath);
618 LY_CHECK_ERR_RET(ret == -1, LOGMEM(cur_mod->ctx), LY_EMEM);
619
620 node = lyd_new_term(cont, NULL, bis ? "location" : "schema", str);
621 LY_CHECK_RET(!node, LY_EOTHER);
622 free(str);
623 }
624 }
625
626 return LY_SUCCESS;
627}
628
629API uint16_t
630ly_ctx_get_yanglib_id(const struct ly_ctx *ctx)
631{
632 return ctx->module_set_id;
633}
634
635API struct lyd_node *
636ly_ctx_get_yanglib_data(const struct ly_ctx *ctx)
637{
638 uint32_t i;
639 int bis = 0, ret;
640 char id[8], *str;
641 const struct lys_module *mod;
642 struct lyd_node *root = NULL, *root_bis = NULL, *cont, *set_bis, *node;
643
644 LY_CHECK_ARG_RET(ctx, ctx, NULL);
645
646 mod = ly_ctx_get_module_implemented(ctx, "ietf-yang-library");
647 LY_CHECK_ERR_RET(!mod, LOGERR(ctx, LY_EINVAL, "Module \"ietf-yang-library\" is not implemented."), NULL);
648
649 if (mod->parsed->revs && !strcmp(mod->parsed->revs[0].date, "2016-06-21")) {
650 bis = 0;
651 } else if (mod->parsed->revs && !strcmp(mod->parsed->revs[0].date, IETF_YANG_LIB_REV)) {
652 bis = 1;
653 } else {
654 LOGERR(ctx, LY_EINVAL, "Incompatible ietf-yang-library version in context.");
655 return NULL;
656 }
657
658 root = lyd_new_inner(NULL, mod, "modules-state");
659 LY_CHECK_GOTO(!root, error);
660
661 if (bis) {
662 root_bis = lyd_new_inner(NULL, mod, "yang-library");
663 LY_CHECK_GOTO(!root_bis, error);
664
665 set_bis = lyd_new_list(root_bis, NULL, "module-set", "complete");
666 LY_CHECK_GOTO(!set_bis, error);
667 }
668
669 for (i = 0; i < ctx->list.count; ++i) {
670 mod = ctx->list.objs[i];
671
672 /*
673 * deprecated legacy
674 */
675 cont = lyd_new_list(root, NULL, "module", mod->name, (mod->parsed->revs ? mod->parsed->revs[0].date : ""));
676 LY_CHECK_GOTO(!cont, error);
677
678 /* schema */
679 if (mod->filepath) {
680 ret = asprintf(&str, "file://%s", mod->filepath);
681 LY_CHECK_ERR_GOTO(ret == -1, LOGMEM(ctx), error);
682
683 node = lyd_new_term(cont, NULL, "schema", str);
684 free(str);
685 LY_CHECK_GOTO(!node, error);
686 }
687
688 /* namespace */
689 node = lyd_new_term(cont, NULL, "namespace", mod->ns);
690 LY_CHECK_GOTO(!node, error);
691
692 /* feature leaf-list */
693 LY_CHECK_GOTO(ylib_feature(cont, mod), error);
694
695 /* deviation list */
696 LY_CHECK_GOTO(ylib_deviation(cont, mod, 0), error);
697
698 /* conformance-type */
699 node = lyd_new_term(cont, NULL, "conformance-type", (mod->implemented ? "implement" : "import"));
700 LY_CHECK_GOTO(!node, error);
701
702 /* submodule list */
703 LY_CHECK_GOTO(ylib_submodules(cont, mod, 0), error);
704
705 /*
706 * current revision
707 */
708 if (bis) {
709 /* name and revision */
710 if (mod->implemented) {
711 cont = lyd_new_list(set_bis, NULL, "module", mod->name);
712 LY_CHECK_GOTO(!cont, error);
713
714 if (mod->parsed->revs) {
715 node = lyd_new_term(cont, NULL, "revision", mod->parsed->revs[0].date);
716 LY_CHECK_GOTO(!node, error);
717 }
718 } else {
719 cont = lyd_new_list(set_bis, NULL, "import-only-module", mod->name,
720 (mod->parsed->revs ? mod->parsed->revs[0].date : ""));
721 LY_CHECK_GOTO(!cont, error);
722 }
723
724 /* namespace */
725 node = lyd_new_term(cont, NULL, "namespace", mod->ns);
726 LY_CHECK_GOTO(!node, error);
727
728 /* location */
729 if (mod->filepath) {
730 ret = asprintf(&str, "file://%s", mod->filepath);
731 LY_CHECK_ERR_GOTO(ret == -1, LOGMEM(ctx), error);
732
733 node = lyd_new_term(cont, NULL, "schema", str);
734 free(str);
735 LY_CHECK_GOTO(!node, error);
736 }
737
738 /* submodule list */
739 LY_CHECK_GOTO(ylib_submodules(cont, mod, 1), error);
740
741 /* feature list */
742 LY_CHECK_GOTO(ylib_feature(cont, mod), error);
743
744 /* deviation */
745 LY_CHECK_GOTO(ylib_deviation(cont, mod, 1), error);
746 }
747 }
748
749 /* IDs */
750 sprintf(id, "%u", ctx->module_set_id);
751 node = lyd_new_term(root, NULL, "module-set-id", id);
752 LY_CHECK_GOTO(!node, error);
753
754 if (bis) {
755 /* create one complete schema */
756 cont = lyd_new_list(root_bis, NULL, "schema", "complete");
757 LY_CHECK_GOTO(!cont, error);
758
759 node = lyd_new_term(cont, NULL, "module-set", "complete");
760 LY_CHECK_GOTO(!node, error);
761
762 /* content-id */
763 node = lyd_new_term(root_bis, NULL, "content-id", id);
764 LY_CHECK_GOTO(!node, error);
765 }
766
767 if (root_bis) {
768 if (lyd_insert_sibling(root_bis, root)) {
769 goto error;
770 }
771 root = root_bis;
772 root_bis = 0;
773 }
774
Radek Krejci7931b192020-06-25 17:05:03 +0200775 if (lyd_validate(&root, NULL, LYD_VALIDATE_PRESENT)) {
Michal Vasko57c10cd2020-05-27 15:57:11 +0200776 goto error;
Michal Vasko00cbf532020-06-15 13:58:47 +0200777 }
Michal Vasko57c10cd2020-05-27 15:57:11 +0200778
779 return root;
780
781error:
782 lyd_free_all(root);
783 lyd_free_all(root_bis);
784 return NULL;
785}
786
Radek Krejcie9e987e2018-10-31 12:50:27 +0100787API void
Radek Krejciad573502018-09-07 15:26:55 +0200788ly_ctx_destroy(struct ly_ctx *ctx, void (*private_destructor)(const struct lysc_node *node, void *priv))
Radek Krejci0af5f5d2018-09-07 15:00:30 +0200789{
Radek Krejci418823a2018-09-20 16:42:13 +0200790 if (!ctx) {
791 return;
792 }
Radek Krejci0af5f5d2018-09-07 15:00:30 +0200793
794 /* models list */
Michal Vaskob34480a2018-09-17 10:34:45 +0200795 for (; ctx->list.count; ctx->list.count--) {
Radek Krejci0af5f5d2018-09-07 15:00:30 +0200796 /* remove the module */
Radek Krejci86d106e2018-10-18 09:53:19 +0200797 lys_module_free(ctx->list.objs[ctx->list.count - 1], private_destructor);
Radek Krejci0af5f5d2018-09-07 15:00:30 +0200798 }
799 free(ctx->list.objs);
800
801 /* search paths list */
Radek Krejcia40f21b2018-09-18 10:42:08 +0200802 ly_set_erase(&ctx->search_paths, free);
Radek Krejci0af5f5d2018-09-07 15:00:30 +0200803
804 /* clean the error list */
805 ly_err_clean(ctx, 0);
806 pthread_key_delete(ctx->errlist_key);
807
808 /* dictionary */
809 lydict_clean(&ctx->dict);
810
811#if 0 /* TODO when plugins implemented */
812 /* plugins - will be removed only if this is the last context */
813 ly_clean_plugins();
814#endif
815
816 free(ctx);
817}