blob: d3ec7a3d53ba6775a1552592ffc84eeabb1e535e [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 */
14
Radek Krejcib7db73a2018-10-24 14:18:40 +020015#include "common.h"
16
Radek Krejci0af5f5d2018-09-07 15:00:30 +020017#include <errno.h>
Radek Krejci7ada9a02018-09-07 15:34:05 +020018#include <limits.h>
19#include <stdlib.h>
20#include <string.h>
Radek Krejci0e212402018-09-20 11:29:38 +020021#include <sys/stat.h>
22#include <sys/types.h>
Radek Krejci0af5f5d2018-09-07 15:00:30 +020023#include <unistd.h>
24
Radek Krejci0af5f5d2018-09-07 15:00:30 +020025#include "context.h"
Radek Krejci86d106e2018-10-18 09:53:19 +020026#include "tree_schema_internal.h"
Radek Krejciad573502018-09-07 15:26:55 +020027#include "libyang.h"
Radek Krejci0af5f5d2018-09-07 15:00:30 +020028
Radek Krejci86d106e2018-10-18 09:53:19 +020029#define LY_INTERNAL_MODS_COUNT 6
Radek Krejci0af5f5d2018-09-07 15:00:30 +020030
Radek Krejcif3f47842018-11-15 11:22:15 +010031#include "../models/ietf-yang-metadata@2016-08-05.h"
32#include "../models/yang@2017-02-20.h"
33#include "../models/ietf-inet-types@2013-07-15.h"
34#include "../models/ietf-yang-types@2013-07-15.h"
35#include "../models/ietf-datastores@2017-08-17.h"
36#include "../models/ietf-yang-library@2018-01-17.h"
Radek Krejci0af5f5d2018-09-07 15:00:30 +020037#define IETF_YANG_LIB_REV "2018-01-17"
38
Radek Krejci0af5f5d2018-09-07 15:00:30 +020039static struct internal_modules_s {
40 const char *name;
41 const char *revision;
42 const char *data;
43 uint8_t implemented;
44 LYS_INFORMAT format;
45} internal_modules[LY_INTERNAL_MODS_COUNT] = {
Radek Krejci86d106e2018-10-18 09:53:19 +020046 {"ietf-yang-metadata", "2016-08-05", (const char*)ietf_yang_metadata_2016_08_05_yang, 0, LYS_IN_YANG},
47 {"yang", "2017-02-20", (const char*)yang_2017_02_20_yang, 1, LYS_IN_YANG},
48 {"ietf-inet-types", "2013-07-15", (const char*)ietf_inet_types_2013_07_15_yang, 0, LYS_IN_YANG},
49 {"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 +020050 /* ietf-datastores and ietf-yang-library must be right here at the end of the list! */
Radek Krejci096235c2019-01-11 11:12:19 +010051 {"ietf-datastores", "2017-08-17", (const char*)ietf_datastores_2017_08_17_yang, 1, LYS_IN_YANG},
Radek Krejci86d106e2018-10-18 09:53:19 +020052 {"ietf-yang-library", IETF_YANG_LIB_REV, (const char*)ietf_yang_library_2018_01_17_yang, 1, LYS_IN_YANG}
Radek Krejci0af5f5d2018-09-07 15:00:30 +020053};
54
55API LY_ERR
56ly_ctx_set_searchdir(struct ly_ctx *ctx, const char *search_dir)
57{
Radek Krejci0e212402018-09-20 11:29:38 +020058 struct stat st;
Radek Krejci0af5f5d2018-09-07 15:00:30 +020059 char *new_dir = NULL;
Radek Krejci0e212402018-09-20 11:29:38 +020060 unsigned int u;
Radek Krejci0af5f5d2018-09-07 15:00:30 +020061
62 LY_CHECK_ARG_RET(ctx, ctx, LY_EINVAL);
63
64 if (search_dir) {
Radek Krejci0af5f5d2018-09-07 15:00:30 +020065 new_dir = realpath(search_dir, NULL);
Radek Krejcia77e0e12018-09-20 12:39:15 +020066 LY_CHECK_ERR_RET(!new_dir,
Radek Krejci3a077b92019-04-09 17:10:35 +020067 LOGERR(ctx, LY_ESYS, "Unable to use search directory \"%s\" (%s).", search_dir, strerror(errno)),
68 LY_EINVAL);
69 if (strcmp(search_dir, new_dir)) {
70 LOGVRB("Canonicalizing search directory string from \"%s\" to \"%s\".", search_dir, new_dir);
71 }
72 LY_CHECK_ERR_RET(access(new_dir, R_OK | X_OK),
73 LOGERR(ctx, LY_ESYS, "Unable to fully access search directory \"%s\" (%s).", new_dir, strerror(errno)); free(new_dir),
74 LY_EINVAL);
75 LY_CHECK_ERR_RET(stat(new_dir, &st),
76 LOGERR(ctx, LY_ESYS, "stat() failed for \"%s\" (%s).", new_dir, strerror(errno)); free(new_dir),
Radek Krejcia77e0e12018-09-20 12:39:15 +020077 LY_ESYS);
Radek Krejci3a077b92019-04-09 17:10:35 +020078 LY_CHECK_ERR_RET(!S_ISDIR(st.st_mode),
79 LOGERR(ctx, LY_ESYS, "Given search directory \"%s\" is not a directory.", new_dir); free(new_dir),
80 LY_EINVAL);
Radek Krejci0e212402018-09-20 11:29:38 +020081 /* avoid path duplication */
82 for (u = 0; u < ctx->search_paths.count; ++u) {
Radek Krejci14946ab2018-09-20 13:42:06 +020083 if (!strcmp(new_dir, ctx->search_paths.objs[u])) {
Radek Krejci0e212402018-09-20 11:29:38 +020084 free(new_dir);
Radek Krejci14946ab2018-09-20 13:42:06 +020085 return LY_EEXIST;
Radek Krejci0e212402018-09-20 11:29:38 +020086 }
87 }
88 if (ly_set_add(&ctx->search_paths, new_dir, LY_SET_OPT_USEASLIST) == -1) {
Radek Krejci0af5f5d2018-09-07 15:00:30 +020089 free(new_dir);
90 return LY_EMEM;
91 }
92
Radek Krejcie9e987e2018-10-31 12:50:27 +010093 /* new searchdir - possibly more latest revision available */
94 ly_ctx_reset_latests(ctx);
95
Radek Krejci0e212402018-09-20 11:29:38 +020096 return LY_SUCCESS;
Radek Krejci0af5f5d2018-09-07 15:00:30 +020097 } else {
98 /* consider that no change is not actually an error */
99 return LY_SUCCESS;
100 }
Radek Krejci0af5f5d2018-09-07 15:00:30 +0200101}
102
103API const char * const *
104ly_ctx_get_searchdirs(const struct ly_ctx *ctx)
105{
Radek Krejci05026432018-09-20 12:13:43 +0200106 void **new;
107
Radek Krejci0af5f5d2018-09-07 15:00:30 +0200108 LY_CHECK_ARG_RET(ctx, ctx, NULL);
Radek Krejci05026432018-09-20 12:13:43 +0200109
110 if (ctx->search_paths.count == ctx->search_paths.size) {
111 /* not enough space for terminating NULL byte */
112 new = realloc(((struct ly_ctx *)ctx)->search_paths.objs, (ctx->search_paths.size + 8) * sizeof *ctx->search_paths.objs);
113 LY_CHECK_ERR_RET(!new, LOGMEM(NULL), NULL);
114 ((struct ly_ctx *)ctx)->search_paths.size += 8;
115 ((struct ly_ctx *)ctx)->search_paths.objs = new;
116 }
117 /* set terminating NULL byte to the strings list */
118 ctx->search_paths.objs[ctx->search_paths.count] = NULL;
119
Radek Krejci0af5f5d2018-09-07 15:00:30 +0200120 return (const char * const *)ctx->search_paths.objs;
121}
122
123API LY_ERR
Radek Krejci0759b792018-09-20 13:53:15 +0200124ly_ctx_unset_searchdirs(struct ly_ctx *ctx, const char *value)
Radek Krejci0af5f5d2018-09-07 15:00:30 +0200125{
Radek Krejci0759b792018-09-20 13:53:15 +0200126 unsigned int index;
127
Radek Krejcicd649072018-09-20 12:14:45 +0200128 LY_CHECK_ARG_RET(ctx, ctx, LY_EINVAL);
Radek Krejcicd649072018-09-20 12:14:45 +0200129
Michal Vaskob34480a2018-09-17 10:34:45 +0200130 if (!ctx->search_paths.count) {
Radek Krejci0af5f5d2018-09-07 15:00:30 +0200131 return LY_SUCCESS;
132 }
133
Radek Krejci0759b792018-09-20 13:53:15 +0200134 if (value) {
Radek Krejci0af5f5d2018-09-07 15:00:30 +0200135 /* remove specific search directory */
Radek Krejci0759b792018-09-20 13:53:15 +0200136 for (index = 0; index < ctx->search_paths.count; ++index) {
137 if (!strcmp(value, ctx->search_paths.objs[index])) {
138 break;
139 }
140 }
141 if (index == ctx->search_paths.count) {
142 LOGARG(ctx, value);
143 return LY_EINVAL;
144 } else {
145 return ly_set_rm_index(&ctx->search_paths, index, free);
146 }
Radek Krejci0af5f5d2018-09-07 15:00:30 +0200147 } else {
148 /* remove them all */
Radek Krejcia40f21b2018-09-18 10:42:08 +0200149 ly_set_erase(&ctx->search_paths, free);
Radek Krejci0af5f5d2018-09-07 15:00:30 +0200150 memset(&ctx->search_paths, 0, sizeof ctx->search_paths);
151 }
152
153 return LY_SUCCESS;
154}
155
156API LY_ERR
Radek Krejcied5acc52019-04-25 15:57:04 +0200157ly_ctx_unset_searchdir(struct ly_ctx *ctx, unsigned int index)
158{
159 LY_CHECK_ARG_RET(ctx, ctx, LY_EINVAL);
160
161 if (!ctx->search_paths.count) {
162 return LY_SUCCESS;
163 }
164
165 if (index >= ctx->search_paths.count) {
166 LOGARG(ctx, value);
167 return LY_EINVAL;
168 } else {
169 return ly_set_rm_index(&ctx->search_paths, index, free);
170 }
171
172 return LY_SUCCESS;
173}
174
175API const struct lys_module *
176ly_ctx_load_module(struct ly_ctx *ctx, const char *name, const char *revision)
177{
178 struct lys_module *result = NULL;
179
180 LY_CHECK_ARG_RET(ctx, ctx, name, NULL);
181
182 LY_CHECK_RET(lysp_load_module(ctx, name, revision, 1, 0, &result), NULL);
183 return result;
184}
185
186API LY_ERR
Radek Krejci0af5f5d2018-09-07 15:00:30 +0200187ly_ctx_new(const char *search_dir, int options, struct ly_ctx **new_ctx)
188{
189 struct ly_ctx *ctx = NULL;
190 struct lys_module *module;
191 char *search_dir_list;
192 char *sep, *dir;
Radek Krejci86d106e2018-10-18 09:53:19 +0200193 int i;
Radek Krejci0af5f5d2018-09-07 15:00:30 +0200194 LY_ERR rc = LY_SUCCESS;
Radek Krejci0af5f5d2018-09-07 15:00:30 +0200195
Radek Krejcied5acc52019-04-25 15:57:04 +0200196 LY_CHECK_ARG_RET(NULL, new_ctx, LY_EINVAL);
197
Radek Krejci0af5f5d2018-09-07 15:00:30 +0200198 ctx = calloc(1, sizeof *ctx);
Radek Krejciad573502018-09-07 15:26:55 +0200199 LY_CHECK_ERR_RET(!ctx, LOGMEM(NULL), LY_EMEM);
Radek Krejci0af5f5d2018-09-07 15:00:30 +0200200
201 /* dictionary */
202 lydict_init(&ctx->dict);
203
Radek Krejciad573502018-09-07 15:26:55 +0200204#if 0 /* TODO when plugins implemented */
Radek Krejci0af5f5d2018-09-07 15:00:30 +0200205 /* plugins */
206 ly_load_plugins();
Radek Krejciad573502018-09-07 15:26:55 +0200207#endif
Radek Krejci0af5f5d2018-09-07 15:00:30 +0200208
209 /* initialize thread-specific key */
Radek Krejciad573502018-09-07 15:26:55 +0200210 while ((pthread_key_create(&ctx->errlist_key, ly_err_free)) == EAGAIN);
Radek Krejci0af5f5d2018-09-07 15:00:30 +0200211
212 /* models list */
213 ctx->flags = options;
214 if (search_dir) {
215 search_dir_list = strdup(search_dir);
216 LY_CHECK_ERR_GOTO(!search_dir_list, LOGMEM(NULL); rc = LY_EMEM, error);
217
218 for (dir = search_dir_list; (sep = strchr(dir, ':')) != NULL && rc == LY_SUCCESS; dir = sep + 1) {
219 *sep = 0;
220 rc = ly_ctx_set_searchdir(ctx, dir);
Radek Krejci14946ab2018-09-20 13:42:06 +0200221 if (rc == LY_EEXIST) {
222 /* ignore duplication */
223 rc = LY_SUCCESS;
224 }
Radek Krejci0af5f5d2018-09-07 15:00:30 +0200225 }
226 if (*dir && rc == LY_SUCCESS) {
227 rc = ly_ctx_set_searchdir(ctx, dir);
Radek Krejci14946ab2018-09-20 13:42:06 +0200228 if (rc == LY_EEXIST) {
229 /* ignore duplication */
230 rc = LY_SUCCESS;
231 }
Radek Krejci0af5f5d2018-09-07 15:00:30 +0200232 }
233 free(search_dir_list);
234
235 /* If ly_ctx_set_searchdir() failed, the error is already logged. Just exit */
236 if (rc != LY_SUCCESS) {
237 goto error;
238 }
239 }
240 ctx->module_set_id = 1;
241
242 /* load internal modules */
Radek Krejci86d106e2018-10-18 09:53:19 +0200243 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 +0100244 module = (struct lys_module *)lys_parse_mem_module(ctx, internal_modules[i].data, internal_modules[i].format,
245 internal_modules[i].implemented, NULL, NULL);
Radek Krejci0e75e6f2018-11-08 09:40:02 +0100246 LY_CHECK_ERR_GOTO(!module, rc = ly_errcode(ctx), error);
Radek Krejci096235c2019-01-11 11:12:19 +0100247 LY_CHECK_GOTO((rc = lys_compile(module, 0)), error);
Radek Krejci0af5f5d2018-09-07 15:00:30 +0200248 }
249
250 *new_ctx = ctx;
251 return rc;
252
253error:
254 ly_ctx_destroy(ctx, NULL);
255 return rc;
256}
Radek Krejci0af5f5d2018-09-07 15:00:30 +0200257API int
Radek Krejci3fbe89a2018-09-20 13:54:46 +0200258ly_ctx_get_options(const struct ly_ctx *ctx)
Radek Krejci0af5f5d2018-09-07 15:00:30 +0200259{
Radek Krejci3fbe89a2018-09-20 13:54:46 +0200260 LY_CHECK_ARG_RET(ctx, ctx, 0);
Radek Krejci0af5f5d2018-09-07 15:00:30 +0200261 return ctx->flags;
262}
263
Radek Krejcica828d92018-09-20 14:19:43 +0200264API LY_ERR
265ly_ctx_set_option(struct ly_ctx *ctx, int option)
266{
267 LY_CHECK_ARG_RET(ctx, ctx, LY_EINVAL);
268 LY_CHECK_ERR_RET(option & LY_CTX_NOYANGLIBRARY, LOGARG(ctx, option), LY_EINVAL);
269
270 /* set the option(s) */
271 ctx->flags |= option;
272
273 return LY_SUCCESS;
274}
275
276API LY_ERR
277ly_ctx_unset_option(struct ly_ctx *ctx, int option)
278{
279 LY_CHECK_ARG_RET(ctx, ctx, LY_EINVAL);
280 LY_CHECK_ERR_RET(option & LY_CTX_NOYANGLIBRARY, LOGARG(ctx, option), LY_EINVAL);
281
282 /* unset the option(s) */
283 ctx->flags &= ~option;
284
285 return LY_SUCCESS;
286}
287
Radek Krejci0af5f5d2018-09-07 15:00:30 +0200288API uint16_t
289ly_ctx_get_module_set_id(const struct ly_ctx *ctx)
290{
Radek Krejci3fbe89a2018-09-20 13:54:46 +0200291 LY_CHECK_ARG_RET(ctx, ctx, 0);
Radek Krejci0af5f5d2018-09-07 15:00:30 +0200292 return ctx->module_set_id;
293}
294
Radek Krejcid33273d2018-10-25 14:55:52 +0200295API void
296ly_ctx_set_module_imp_clb(struct ly_ctx *ctx, ly_module_imp_clb clb, void *user_data)
297{
298 LY_CHECK_ARG_RET(ctx, ctx,);
299
300 ctx->imp_clb = clb;
301 ctx->imp_clb_data = user_data;
302}
303
304API ly_module_imp_clb
305ly_ctx_get_module_imp_clb(const struct ly_ctx *ctx, void **user_data)
306{
307 LY_CHECK_ARG_RET(ctx, ctx, NULL);
308
309 if (user_data) {
310 *user_data = ctx->imp_clb_data;
311 }
312 return ctx->imp_clb;
313}
314
Radek Krejcied5acc52019-04-25 15:57:04 +0200315API const struct lys_module *
316ly_ctx_get_module_iter(const struct ly_ctx *ctx, unsigned int *index)
317{
318 LY_CHECK_ARG_RET(ctx, ctx, index, NULL);
319
Radek Krejci2390fb52019-04-30 13:27:43 +0200320 if (*index < (unsigned)ctx->list.count) {
Radek Krejcied5acc52019-04-25 15:57:04 +0200321 return ctx->list.objs[(*index)++];
Radek Krejci2390fb52019-04-30 13:27:43 +0200322 } else {
323 return NULL;
Radek Krejcied5acc52019-04-25 15:57:04 +0200324 }
Radek Krejcied5acc52019-04-25 15:57:04 +0200325}
326
Radek Krejcib7db73a2018-10-24 14:18:40 +0200327/**
328 * @brief Iterate over the modules in the given context. Returned modules must match the given key at the offset of
329 * lysp_module and lysc_module structures (they are supposed to be placed at the same offset in both structures).
330 *
331 * @param[in] ctx Context where to iterate.
332 * @param[in] key Key value to search for.
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100333 * @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 +0200334 * @param[in,out] Iterator to pass between the function calls. On the first call, the variable is supposed to be
335 * initiated to 0. After each call returning a module, the value is greater by 1 than the index of the returned
336 * module in the context.
337 * @return Module matching the given key, NULL if no such module found.
338 */
Radek Krejci0af46292019-01-11 16:02:31 +0100339static struct lys_module *
Radek Krejcib7db73a2018-10-24 14:18:40 +0200340ly_ctx_get_module_by_iter(const struct ly_ctx *ctx, const char *key, size_t key_offset, unsigned int *index)
341{
Radek Krejci0af46292019-01-11 16:02:31 +0100342 struct lys_module *mod;
Radek Krejcib7db73a2018-10-24 14:18:40 +0200343 const char *value;
344
345 for (; *index < ctx->list.count; ++(*index)) {
346 mod = ctx->list.objs[*index];
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100347 value = *(const char**)(((int8_t*)(mod)) + key_offset);
Radek Krejcib7db73a2018-10-24 14:18:40 +0200348 if (!strcmp(key, value)) {
349 /* increment index for the next run */
350 ++(*index);
351 return mod;
352 }
353 }
354 /* done */
355 return NULL;
356}
357
358/**
359 * @brief Unifying function for ly_ctx_get_module() and ly_ctx_get_module_ns()
360 * @param[in] ctx Context where to search.
361 * @param[in] key Name or Namespace as a search key.
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100362 * @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 +0200363 * @param[in] revision Revision date to match. If NULL, the matching module must have no revision. To search for the latest
364 * revision module, use ly_ctx_get_module_latest_by().
365 * @return Matching module if any.
366 */
Radek Krejci0af46292019-01-11 16:02:31 +0100367static struct lys_module *
Radek Krejcib7db73a2018-10-24 14:18:40 +0200368ly_ctx_get_module_by(const struct ly_ctx *ctx, const char *key, size_t key_offset, const char *revision)
369{
Radek Krejci0af46292019-01-11 16:02:31 +0100370 struct lys_module *mod;
Radek Krejcib7db73a2018-10-24 14:18:40 +0200371 unsigned int index = 0;
372
373 while ((mod = ly_ctx_get_module_by_iter(ctx, key, key_offset, &index))) {
374 if (!revision) {
Radek Krejci0af46292019-01-11 16:02:31 +0100375 if (!mod->revision) {
Radek Krejcib7db73a2018-10-24 14:18:40 +0200376 /* found requested module without revision */
377 return mod;
378 }
379 } else {
Radek Krejci0af46292019-01-11 16:02:31 +0100380 if (mod->revision && !strcmp(mod->revision, revision)) {
Radek Krejcib7db73a2018-10-24 14:18:40 +0200381 /* found requested module of the specific revision */
382 return mod;
383 }
384 }
385 }
386
387 return NULL;
388}
389
Radek Krejci0af46292019-01-11 16:02:31 +0100390API struct lys_module *
Radek Krejcib7db73a2018-10-24 14:18:40 +0200391ly_ctx_get_module_ns(const struct ly_ctx *ctx, const char *ns, const char *revision)
392{
393 LY_CHECK_ARG_RET(ctx, ctx, ns, NULL);
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100394 return ly_ctx_get_module_by(ctx, ns, offsetof(struct lys_module, ns), revision);
Radek Krejcib7db73a2018-10-24 14:18:40 +0200395}
396
Radek Krejci0af46292019-01-11 16:02:31 +0100397API struct lys_module *
Radek Krejcib7db73a2018-10-24 14:18:40 +0200398ly_ctx_get_module(const struct ly_ctx *ctx, const char *name, const char *revision)
399{
400 LY_CHECK_ARG_RET(ctx, ctx, name, NULL);
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100401 return ly_ctx_get_module_by(ctx, name, offsetof(struct lys_module, name), revision);
Radek Krejcib7db73a2018-10-24 14:18:40 +0200402}
403
404/**
405 * @brief Unifying function for ly_ctx_get_module_latest() and ly_ctx_get_module_latest_ns()
406 * @param[in] ctx Context where to search.
407 * @param[in] key Name or Namespace as a search key.
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100408 * @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 +0200409 * @return Matching module if any.
410 */
Radek Krejci0af46292019-01-11 16:02:31 +0100411static struct lys_module *
Radek Krejcib7db73a2018-10-24 14:18:40 +0200412ly_ctx_get_module_latest_by(const struct ly_ctx *ctx, const char *key, size_t key_offset)
413{
Radek Krejci0af46292019-01-11 16:02:31 +0100414 struct lys_module *mod;
Radek Krejcib7db73a2018-10-24 14:18:40 +0200415 unsigned int index = 0;
Radek Krejcib7db73a2018-10-24 14:18:40 +0200416
417 while ((mod = ly_ctx_get_module_by_iter(ctx, key, key_offset, &index))) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100418 if (mod->latest_revision) {
Radek Krejci9f5e6fb2018-10-25 09:26:12 +0200419 return mod;
Radek Krejcib7db73a2018-10-24 14:18:40 +0200420 }
421 }
422
Radek Krejci9f5e6fb2018-10-25 09:26:12 +0200423 return NULL;
Radek Krejcib7db73a2018-10-24 14:18:40 +0200424}
425
Radek Krejci0af46292019-01-11 16:02:31 +0100426API struct lys_module *
Radek Krejcib7db73a2018-10-24 14:18:40 +0200427ly_ctx_get_module_latest(const struct ly_ctx *ctx, const char *name)
428{
429 LY_CHECK_ARG_RET(ctx, ctx, name, NULL);
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100430 return ly_ctx_get_module_latest_by(ctx, name, offsetof(struct lys_module, name));
Radek Krejcib7db73a2018-10-24 14:18:40 +0200431}
432
Radek Krejci0af46292019-01-11 16:02:31 +0100433API struct lys_module *
Radek Krejcib7db73a2018-10-24 14:18:40 +0200434ly_ctx_get_module_latest_ns(const struct ly_ctx *ctx, const char *ns)
435{
436 LY_CHECK_ARG_RET(ctx, ctx, ns, NULL);
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100437 return ly_ctx_get_module_latest_by(ctx, ns, offsetof(struct lys_module, ns));
Radek Krejcib7db73a2018-10-24 14:18:40 +0200438}
439
440/**
441 * @brief Unifying function for ly_ctx_get_module_implemented() and ly_ctx_get_module_implemented_ns()
442 * @param[in] ctx Context where to search.
443 * @param[in] key Name or Namespace as a search key.
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100444 * @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 +0200445 * @return Matching module if any.
446 */
Radek Krejci0af46292019-01-11 16:02:31 +0100447static struct lys_module *
Radek Krejcib7db73a2018-10-24 14:18:40 +0200448ly_ctx_get_module_implemented_by(const struct ly_ctx *ctx, const char *key, size_t key_offset)
449{
Radek Krejci0af46292019-01-11 16:02:31 +0100450 struct lys_module *mod;
Radek Krejcib7db73a2018-10-24 14:18:40 +0200451 unsigned int index = 0;
452
453 while ((mod = ly_ctx_get_module_by_iter(ctx, key, key_offset, &index))) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100454 if (mod->implemented) {
Radek Krejcib7db73a2018-10-24 14:18:40 +0200455 return mod;
456 }
457 }
458
459 return NULL;
460}
461
Radek Krejci0af46292019-01-11 16:02:31 +0100462API struct lys_module *
Radek Krejcib7db73a2018-10-24 14:18:40 +0200463ly_ctx_get_module_implemented(const struct ly_ctx *ctx, const char *name)
464{
465 LY_CHECK_ARG_RET(ctx, ctx, name, NULL);
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100466 return ly_ctx_get_module_implemented_by(ctx, name, offsetof(struct lys_module, name));
Radek Krejcib7db73a2018-10-24 14:18:40 +0200467}
468
Radek Krejci0af46292019-01-11 16:02:31 +0100469API struct lys_module *
Radek Krejcib7db73a2018-10-24 14:18:40 +0200470ly_ctx_get_module_implemented_ns(const struct ly_ctx *ctx, const char *ns)
471{
472 LY_CHECK_ARG_RET(ctx, ctx, ns, NULL);
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100473 return ly_ctx_get_module_implemented_by(ctx, ns, offsetof(struct lys_module, ns));
Radek Krejcib7db73a2018-10-24 14:18:40 +0200474}
475
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100476struct lysp_submodule *
Radek Krejcid33273d2018-10-25 14:55:52 +0200477ly_ctx_get_submodule(const struct ly_ctx *ctx, const char *module, const char *submodule, const char *revision)
478{
479 const struct lys_module *mod;
480 struct lysp_include *inc;
Radek Krejcifaa1eac2018-10-30 14:34:55 +0100481 unsigned int v, u;
Radek Krejcid33273d2018-10-25 14:55:52 +0200482
Radek Krejcifaa1eac2018-10-30 14:34:55 +0100483 assert(submodule);
484
485 for (v = 0; v < ctx->list.count; ++v) {
486 mod = ctx->list.objs[v];
Radek Krejcid33273d2018-10-25 14:55:52 +0200487 if (!mod->parsed) {
488 continue;
489 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100490 if (module && strcmp(module, mod->name)) {
Radek Krejcifaa1eac2018-10-30 14:34:55 +0100491 continue;
492 }
Radek Krejcid33273d2018-10-25 14:55:52 +0200493
494 LY_ARRAY_FOR(mod->parsed->includes, u) {
Radek Krejci086c7132018-10-26 15:29:04 +0200495 if (mod->parsed->includes[u].submodule && !strcmp(submodule, mod->parsed->includes[u].submodule->name)) {
Radek Krejcid33273d2018-10-25 14:55:52 +0200496 inc = &mod->parsed->includes[u];
497 if (!revision) {
498 if (inc->submodule->latest_revision) {
499 return inc->submodule;
500 }
501 } else if (inc->submodule->revs && !strcmp(revision, inc->submodule->revs[0].date)) {
502 return inc->submodule;
503 }
504 }
505 }
506 }
507
508 return NULL;
509}
510
Radek Krejci096235c2019-01-11 11:12:19 +0100511/* TODO ly_ctx_load_module() via lysp_load_module() */
512
Radek Krejci0af5f5d2018-09-07 15:00:30 +0200513API void
Radek Krejcie9e987e2018-10-31 12:50:27 +0100514ly_ctx_reset_latests(struct ly_ctx *ctx)
515{
516 unsigned int u,v ;
517 struct lys_module *mod;
518
519 for (u = 0; u < ctx->list.count; ++u) {
520 mod = ctx->list.objs[u];
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100521 if (mod->latest_revision == 2) {
522 mod->latest_revision = 1;
Radek Krejcie9e987e2018-10-31 12:50:27 +0100523 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100524 if (mod->parsed && mod->parsed->includes) {
525 for (v = 0; v < LY_ARRAY_SIZE(mod->parsed->includes); ++v) {
526 if (mod->parsed->includes[v].submodule->latest_revision == 2) {
527 mod->parsed->includes[v].submodule->latest_revision = 1;
Radek Krejcie9e987e2018-10-31 12:50:27 +0100528 }
529 }
530 }
531 }
532}
533
Radek Krejci95710c92019-02-11 15:49:55 +0100534LY_ERR
535ly_ctx_module_implement_internal(struct ly_ctx *ctx, struct lys_module *mod, uint8_t value)
Radek Krejci0af46292019-01-11 16:02:31 +0100536{
537 struct lys_module *m;
538
539 LY_CHECK_ARG_RET(ctx, mod, LY_EINVAL);
540
541 if (mod->implemented) {
542 return LY_SUCCESS;
543 }
544
545 /* we have module from the current context */
546 m = ly_ctx_get_module_implemented(ctx, mod->name);
547 if (m) {
548 if (m != mod) {
549 /* check collision with other implemented revision */
550 LOGERR(ctx, LY_EDENIED, "Module \"%s\" is present in the context in other implemented revision (%s).",
551 mod->name, mod->revision ? mod->revision : "module without revision");
552 return LY_EDENIED;
553 } else {
554 /* mod is already implemented */
555 return LY_SUCCESS;
556 }
557 }
558
559 /* mark the module implemented, check for collision was already done */
Radek Krejci95710c92019-02-11 15:49:55 +0100560 mod->implemented = value;
Radek Krejci0af46292019-01-11 16:02:31 +0100561
562 /* compile the schema */
Radek Krejci95710c92019-02-11 15:49:55 +0100563 LY_CHECK_RET(lys_compile(mod, LYSC_OPT_INTERNAL));
Radek Krejci0af46292019-01-11 16:02:31 +0100564
565 return LY_SUCCESS;
566}
567
Radek Krejci95710c92019-02-11 15:49:55 +0100568API LY_ERR
569ly_ctx_module_implement(struct ly_ctx *ctx, struct lys_module *mod)
570{
571 return ly_ctx_module_implement_internal(ctx, mod, 1);
572}
573
Radek Krejcie9e987e2018-10-31 12:50:27 +0100574API void
Radek Krejciad573502018-09-07 15:26:55 +0200575ly_ctx_destroy(struct ly_ctx *ctx, void (*private_destructor)(const struct lysc_node *node, void *priv))
Radek Krejci0af5f5d2018-09-07 15:00:30 +0200576{
Radek Krejci418823a2018-09-20 16:42:13 +0200577 if (!ctx) {
578 return;
579 }
Radek Krejci0af5f5d2018-09-07 15:00:30 +0200580
581 /* models list */
Michal Vaskob34480a2018-09-17 10:34:45 +0200582 for (; ctx->list.count; ctx->list.count--) {
Radek Krejci0af5f5d2018-09-07 15:00:30 +0200583 /* remove the module */
Radek Krejci86d106e2018-10-18 09:53:19 +0200584 lys_module_free(ctx->list.objs[ctx->list.count - 1], private_destructor);
Radek Krejci0af5f5d2018-09-07 15:00:30 +0200585 }
586 free(ctx->list.objs);
587
588 /* search paths list */
Radek Krejcia40f21b2018-09-18 10:42:08 +0200589 ly_set_erase(&ctx->search_paths, free);
Radek Krejci0af5f5d2018-09-07 15:00:30 +0200590
591 /* clean the error list */
592 ly_err_clean(ctx, 0);
593 pthread_key_delete(ctx->errlist_key);
594
595 /* dictionary */
596 lydict_clean(&ctx->dict);
597
598#if 0 /* TODO when plugins implemented */
599 /* plugins - will be removed only if this is the last context */
600 ly_clean_plugins();
601#endif
602
603 free(ctx);
604}