blob: 10542c0474e02ca645533c4c5b91f0fac0c00928 [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 Krejci0e212402018-09-20 11:29:38 +020065 LY_CHECK_ERR_RET(access(search_dir, R_OK | X_OK),
66 LOGERR(ctx, LY_ESYS, "Unable to use search directory \"%s\" (%s)", search_dir, strerror(errno)),
67 LY_EINVAL);
68 LY_CHECK_ERR_RET(stat(search_dir, &st),
69 LOGERR(ctx, LY_ESYS, "stat() failed for \"%s\" (%s)", search_dir, strerror(errno)),
70 LY_ESYS);
71 LY_CHECK_ERR_RET(!S_ISDIR(st.st_mode),
72 LOGERR(ctx, LY_ESYS, "Given search directory \"%s\" is not a directory.", search_dir),
73 LY_EINVAL);
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,
76 LOGERR(ctx, LY_ESYS, "realpath() call failed for \"%s\" (%s).", search_dir, strerror(errno)),
77 LY_ESYS);
Radek Krejci0e212402018-09-20 11:29:38 +020078 /* avoid path duplication */
79 for (u = 0; u < ctx->search_paths.count; ++u) {
Radek Krejci14946ab2018-09-20 13:42:06 +020080 if (!strcmp(new_dir, ctx->search_paths.objs[u])) {
Radek Krejci0e212402018-09-20 11:29:38 +020081 free(new_dir);
Radek Krejci14946ab2018-09-20 13:42:06 +020082 return LY_EEXIST;
Radek Krejci0e212402018-09-20 11:29:38 +020083 }
84 }
85 if (ly_set_add(&ctx->search_paths, new_dir, LY_SET_OPT_USEASLIST) == -1) {
Radek Krejci0af5f5d2018-09-07 15:00:30 +020086 free(new_dir);
87 return LY_EMEM;
88 }
89
Radek Krejcie9e987e2018-10-31 12:50:27 +010090 /* new searchdir - possibly more latest revision available */
91 ly_ctx_reset_latests(ctx);
92
Radek Krejci0e212402018-09-20 11:29:38 +020093 return LY_SUCCESS;
Radek Krejci0af5f5d2018-09-07 15:00:30 +020094 } else {
95 /* consider that no change is not actually an error */
96 return LY_SUCCESS;
97 }
Radek Krejci0af5f5d2018-09-07 15:00:30 +020098}
99
100API const char * const *
101ly_ctx_get_searchdirs(const struct ly_ctx *ctx)
102{
Radek Krejci05026432018-09-20 12:13:43 +0200103 void **new;
104
Radek Krejci0af5f5d2018-09-07 15:00:30 +0200105 LY_CHECK_ARG_RET(ctx, ctx, NULL);
Radek Krejci05026432018-09-20 12:13:43 +0200106
107 if (ctx->search_paths.count == ctx->search_paths.size) {
108 /* not enough space for terminating NULL byte */
109 new = realloc(((struct ly_ctx *)ctx)->search_paths.objs, (ctx->search_paths.size + 8) * sizeof *ctx->search_paths.objs);
110 LY_CHECK_ERR_RET(!new, LOGMEM(NULL), NULL);
111 ((struct ly_ctx *)ctx)->search_paths.size += 8;
112 ((struct ly_ctx *)ctx)->search_paths.objs = new;
113 }
114 /* set terminating NULL byte to the strings list */
115 ctx->search_paths.objs[ctx->search_paths.count] = NULL;
116
Radek Krejci0af5f5d2018-09-07 15:00:30 +0200117 return (const char * const *)ctx->search_paths.objs;
118}
119
120API LY_ERR
Radek Krejci0759b792018-09-20 13:53:15 +0200121ly_ctx_unset_searchdirs(struct ly_ctx *ctx, const char *value)
Radek Krejci0af5f5d2018-09-07 15:00:30 +0200122{
Radek Krejci0759b792018-09-20 13:53:15 +0200123 unsigned int index;
124
Radek Krejcicd649072018-09-20 12:14:45 +0200125 LY_CHECK_ARG_RET(ctx, ctx, LY_EINVAL);
Radek Krejcicd649072018-09-20 12:14:45 +0200126
Michal Vaskob34480a2018-09-17 10:34:45 +0200127 if (!ctx->search_paths.count) {
Radek Krejci0af5f5d2018-09-07 15:00:30 +0200128 return LY_SUCCESS;
129 }
130
Radek Krejci0759b792018-09-20 13:53:15 +0200131 if (value) {
Radek Krejci0af5f5d2018-09-07 15:00:30 +0200132 /* remove specific search directory */
Radek Krejci0759b792018-09-20 13:53:15 +0200133 for (index = 0; index < ctx->search_paths.count; ++index) {
134 if (!strcmp(value, ctx->search_paths.objs[index])) {
135 break;
136 }
137 }
138 if (index == ctx->search_paths.count) {
139 LOGARG(ctx, value);
140 return LY_EINVAL;
141 } else {
142 return ly_set_rm_index(&ctx->search_paths, index, free);
143 }
Radek Krejci0af5f5d2018-09-07 15:00:30 +0200144 } else {
145 /* remove them all */
Radek Krejcia40f21b2018-09-18 10:42:08 +0200146 ly_set_erase(&ctx->search_paths, free);
Radek Krejci0af5f5d2018-09-07 15:00:30 +0200147 memset(&ctx->search_paths, 0, sizeof ctx->search_paths);
148 }
149
150 return LY_SUCCESS;
151}
152
153API LY_ERR
154ly_ctx_new(const char *search_dir, int options, struct ly_ctx **new_ctx)
155{
156 struct ly_ctx *ctx = NULL;
157 struct lys_module *module;
158 char *search_dir_list;
159 char *sep, *dir;
Radek Krejci86d106e2018-10-18 09:53:19 +0200160 int i;
Radek Krejci0af5f5d2018-09-07 15:00:30 +0200161 LY_ERR rc = LY_SUCCESS;
Radek Krejci0af5f5d2018-09-07 15:00:30 +0200162
163 ctx = calloc(1, sizeof *ctx);
Radek Krejciad573502018-09-07 15:26:55 +0200164 LY_CHECK_ERR_RET(!ctx, LOGMEM(NULL), LY_EMEM);
Radek Krejci0af5f5d2018-09-07 15:00:30 +0200165
166 /* dictionary */
167 lydict_init(&ctx->dict);
168
Radek Krejciad573502018-09-07 15:26:55 +0200169#if 0 /* TODO when plugins implemented */
Radek Krejci0af5f5d2018-09-07 15:00:30 +0200170 /* plugins */
171 ly_load_plugins();
Radek Krejciad573502018-09-07 15:26:55 +0200172#endif
Radek Krejci0af5f5d2018-09-07 15:00:30 +0200173
174 /* initialize thread-specific key */
Radek Krejciad573502018-09-07 15:26:55 +0200175 while ((pthread_key_create(&ctx->errlist_key, ly_err_free)) == EAGAIN);
Radek Krejci0af5f5d2018-09-07 15:00:30 +0200176
177 /* models list */
178 ctx->flags = options;
179 if (search_dir) {
180 search_dir_list = strdup(search_dir);
181 LY_CHECK_ERR_GOTO(!search_dir_list, LOGMEM(NULL); rc = LY_EMEM, error);
182
183 for (dir = search_dir_list; (sep = strchr(dir, ':')) != NULL && rc == LY_SUCCESS; dir = sep + 1) {
184 *sep = 0;
185 rc = ly_ctx_set_searchdir(ctx, dir);
Radek Krejci14946ab2018-09-20 13:42:06 +0200186 if (rc == LY_EEXIST) {
187 /* ignore duplication */
188 rc = LY_SUCCESS;
189 }
Radek Krejci0af5f5d2018-09-07 15:00:30 +0200190 }
191 if (*dir && rc == LY_SUCCESS) {
192 rc = ly_ctx_set_searchdir(ctx, dir);
Radek Krejci14946ab2018-09-20 13:42:06 +0200193 if (rc == LY_EEXIST) {
194 /* ignore duplication */
195 rc = LY_SUCCESS;
196 }
Radek Krejci0af5f5d2018-09-07 15:00:30 +0200197 }
198 free(search_dir_list);
199
200 /* If ly_ctx_set_searchdir() failed, the error is already logged. Just exit */
201 if (rc != LY_SUCCESS) {
202 goto error;
203 }
204 }
205 ctx->module_set_id = 1;
206
207 /* load internal modules */
Radek Krejci86d106e2018-10-18 09:53:19 +0200208 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 +0100209 module = (struct lys_module *)lys_parse_mem_module(ctx, internal_modules[i].data, internal_modules[i].format,
210 internal_modules[i].implemented, NULL, NULL);
Radek Krejci0e75e6f2018-11-08 09:40:02 +0100211 LY_CHECK_ERR_GOTO(!module, rc = ly_errcode(ctx), error);
Radek Krejci096235c2019-01-11 11:12:19 +0100212 LY_CHECK_GOTO((rc = lys_compile(module, 0)), error);
Radek Krejci0af5f5d2018-09-07 15:00:30 +0200213 }
214
215 *new_ctx = ctx;
216 return rc;
217
218error:
219 ly_ctx_destroy(ctx, NULL);
220 return rc;
221}
Radek Krejci0af5f5d2018-09-07 15:00:30 +0200222API int
Radek Krejci3fbe89a2018-09-20 13:54:46 +0200223ly_ctx_get_options(const struct ly_ctx *ctx)
Radek Krejci0af5f5d2018-09-07 15:00:30 +0200224{
Radek Krejci3fbe89a2018-09-20 13:54:46 +0200225 LY_CHECK_ARG_RET(ctx, ctx, 0);
Radek Krejci0af5f5d2018-09-07 15:00:30 +0200226 return ctx->flags;
227}
228
Radek Krejcica828d92018-09-20 14:19:43 +0200229API LY_ERR
230ly_ctx_set_option(struct ly_ctx *ctx, int option)
231{
232 LY_CHECK_ARG_RET(ctx, ctx, LY_EINVAL);
233 LY_CHECK_ERR_RET(option & LY_CTX_NOYANGLIBRARY, LOGARG(ctx, option), LY_EINVAL);
234
235 /* set the option(s) */
236 ctx->flags |= option;
237
238 return LY_SUCCESS;
239}
240
241API LY_ERR
242ly_ctx_unset_option(struct ly_ctx *ctx, int option)
243{
244 LY_CHECK_ARG_RET(ctx, ctx, LY_EINVAL);
245 LY_CHECK_ERR_RET(option & LY_CTX_NOYANGLIBRARY, LOGARG(ctx, option), LY_EINVAL);
246
247 /* unset the option(s) */
248 ctx->flags &= ~option;
249
250 return LY_SUCCESS;
251}
252
Radek Krejci0af5f5d2018-09-07 15:00:30 +0200253API uint16_t
254ly_ctx_get_module_set_id(const struct ly_ctx *ctx)
255{
Radek Krejci3fbe89a2018-09-20 13:54:46 +0200256 LY_CHECK_ARG_RET(ctx, ctx, 0);
Radek Krejci0af5f5d2018-09-07 15:00:30 +0200257 return ctx->module_set_id;
258}
259
Radek Krejcid33273d2018-10-25 14:55:52 +0200260API void
261ly_ctx_set_module_imp_clb(struct ly_ctx *ctx, ly_module_imp_clb clb, void *user_data)
262{
263 LY_CHECK_ARG_RET(ctx, ctx,);
264
265 ctx->imp_clb = clb;
266 ctx->imp_clb_data = user_data;
267}
268
269API ly_module_imp_clb
270ly_ctx_get_module_imp_clb(const struct ly_ctx *ctx, void **user_data)
271{
272 LY_CHECK_ARG_RET(ctx, ctx, NULL);
273
274 if (user_data) {
275 *user_data = ctx->imp_clb_data;
276 }
277 return ctx->imp_clb;
278}
279
Radek Krejcib7db73a2018-10-24 14:18:40 +0200280/**
281 * @brief Iterate over the modules in the given context. Returned modules must match the given key at the offset of
282 * lysp_module and lysc_module structures (they are supposed to be placed at the same offset in both structures).
283 *
284 * @param[in] ctx Context where to iterate.
285 * @param[in] key Key value to search for.
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100286 * @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 +0200287 * @param[in,out] Iterator to pass between the function calls. On the first call, the variable is supposed to be
288 * initiated to 0. After each call returning a module, the value is greater by 1 than the index of the returned
289 * module in the context.
290 * @return Module matching the given key, NULL if no such module found.
291 */
Radek Krejci0af46292019-01-11 16:02:31 +0100292static struct lys_module *
Radek Krejcib7db73a2018-10-24 14:18:40 +0200293ly_ctx_get_module_by_iter(const struct ly_ctx *ctx, const char *key, size_t key_offset, unsigned int *index)
294{
Radek Krejci0af46292019-01-11 16:02:31 +0100295 struct lys_module *mod;
Radek Krejcib7db73a2018-10-24 14:18:40 +0200296 const char *value;
297
298 for (; *index < ctx->list.count; ++(*index)) {
299 mod = ctx->list.objs[*index];
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100300 value = *(const char**)(((int8_t*)(mod)) + key_offset);
Radek Krejcib7db73a2018-10-24 14:18:40 +0200301 if (!strcmp(key, value)) {
302 /* increment index for the next run */
303 ++(*index);
304 return mod;
305 }
306 }
307 /* done */
308 return NULL;
309}
310
311/**
312 * @brief Unifying function for ly_ctx_get_module() and ly_ctx_get_module_ns()
313 * @param[in] ctx Context where to search.
314 * @param[in] key Name or Namespace as a search key.
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100315 * @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 +0200316 * @param[in] revision Revision date to match. If NULL, the matching module must have no revision. To search for the latest
317 * revision module, use ly_ctx_get_module_latest_by().
318 * @return Matching module if any.
319 */
Radek Krejci0af46292019-01-11 16:02:31 +0100320static struct lys_module *
Radek Krejcib7db73a2018-10-24 14:18:40 +0200321ly_ctx_get_module_by(const struct ly_ctx *ctx, const char *key, size_t key_offset, const char *revision)
322{
Radek Krejci0af46292019-01-11 16:02:31 +0100323 struct lys_module *mod;
Radek Krejcib7db73a2018-10-24 14:18:40 +0200324 unsigned int index = 0;
325
326 while ((mod = ly_ctx_get_module_by_iter(ctx, key, key_offset, &index))) {
327 if (!revision) {
Radek Krejci0af46292019-01-11 16:02:31 +0100328 if (!mod->revision) {
Radek Krejcib7db73a2018-10-24 14:18:40 +0200329 /* found requested module without revision */
330 return mod;
331 }
332 } else {
Radek Krejci0af46292019-01-11 16:02:31 +0100333 if (mod->revision && !strcmp(mod->revision, revision)) {
Radek Krejcib7db73a2018-10-24 14:18:40 +0200334 /* found requested module of the specific revision */
335 return mod;
336 }
337 }
338 }
339
340 return NULL;
341}
342
Radek Krejci0af46292019-01-11 16:02:31 +0100343API struct lys_module *
Radek Krejcib7db73a2018-10-24 14:18:40 +0200344ly_ctx_get_module_ns(const struct ly_ctx *ctx, const char *ns, const char *revision)
345{
346 LY_CHECK_ARG_RET(ctx, ctx, ns, NULL);
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100347 return ly_ctx_get_module_by(ctx, ns, offsetof(struct lys_module, ns), revision);
Radek Krejcib7db73a2018-10-24 14:18:40 +0200348}
349
Radek Krejci0af46292019-01-11 16:02:31 +0100350API struct lys_module *
Radek Krejcib7db73a2018-10-24 14:18:40 +0200351ly_ctx_get_module(const struct ly_ctx *ctx, const char *name, const char *revision)
352{
353 LY_CHECK_ARG_RET(ctx, ctx, name, NULL);
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100354 return ly_ctx_get_module_by(ctx, name, offsetof(struct lys_module, name), revision);
Radek Krejcib7db73a2018-10-24 14:18:40 +0200355}
356
357/**
358 * @brief Unifying function for ly_ctx_get_module_latest() and ly_ctx_get_module_latest_ns()
359 * @param[in] ctx Context where to search.
360 * @param[in] key Name or Namespace as a search key.
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100361 * @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 +0200362 * @return Matching module if any.
363 */
Radek Krejci0af46292019-01-11 16:02:31 +0100364static struct lys_module *
Radek Krejcib7db73a2018-10-24 14:18:40 +0200365ly_ctx_get_module_latest_by(const struct ly_ctx *ctx, const char *key, size_t key_offset)
366{
Radek Krejci0af46292019-01-11 16:02:31 +0100367 struct lys_module *mod;
Radek Krejcib7db73a2018-10-24 14:18:40 +0200368 unsigned int index = 0;
Radek Krejcib7db73a2018-10-24 14:18:40 +0200369
370 while ((mod = ly_ctx_get_module_by_iter(ctx, key, key_offset, &index))) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100371 if (mod->latest_revision) {
Radek Krejci9f5e6fb2018-10-25 09:26:12 +0200372 return mod;
Radek Krejcib7db73a2018-10-24 14:18:40 +0200373 }
374 }
375
Radek Krejci9f5e6fb2018-10-25 09:26:12 +0200376 return NULL;
Radek Krejcib7db73a2018-10-24 14:18:40 +0200377}
378
Radek Krejci0af46292019-01-11 16:02:31 +0100379API struct lys_module *
Radek Krejcib7db73a2018-10-24 14:18:40 +0200380ly_ctx_get_module_latest(const struct ly_ctx *ctx, const char *name)
381{
382 LY_CHECK_ARG_RET(ctx, ctx, name, NULL);
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100383 return ly_ctx_get_module_latest_by(ctx, name, offsetof(struct lys_module, name));
Radek Krejcib7db73a2018-10-24 14:18:40 +0200384}
385
Radek Krejci0af46292019-01-11 16:02:31 +0100386API struct lys_module *
Radek Krejcib7db73a2018-10-24 14:18:40 +0200387ly_ctx_get_module_latest_ns(const struct ly_ctx *ctx, const char *ns)
388{
389 LY_CHECK_ARG_RET(ctx, ctx, ns, NULL);
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100390 return ly_ctx_get_module_latest_by(ctx, ns, offsetof(struct lys_module, ns));
Radek Krejcib7db73a2018-10-24 14:18:40 +0200391}
392
393/**
394 * @brief Unifying function for ly_ctx_get_module_implemented() and ly_ctx_get_module_implemented_ns()
395 * @param[in] ctx Context where to search.
396 * @param[in] key Name or Namespace as a search key.
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100397 * @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 +0200398 * @return Matching module if any.
399 */
Radek Krejci0af46292019-01-11 16:02:31 +0100400static struct lys_module *
Radek Krejcib7db73a2018-10-24 14:18:40 +0200401ly_ctx_get_module_implemented_by(const struct ly_ctx *ctx, const char *key, size_t key_offset)
402{
Radek Krejci0af46292019-01-11 16:02:31 +0100403 struct lys_module *mod;
Radek Krejcib7db73a2018-10-24 14:18:40 +0200404 unsigned int index = 0;
405
406 while ((mod = ly_ctx_get_module_by_iter(ctx, key, key_offset, &index))) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100407 if (mod->implemented) {
Radek Krejcib7db73a2018-10-24 14:18:40 +0200408 return mod;
409 }
410 }
411
412 return NULL;
413}
414
Radek Krejci0af46292019-01-11 16:02:31 +0100415API struct lys_module *
Radek Krejcib7db73a2018-10-24 14:18:40 +0200416ly_ctx_get_module_implemented(const struct ly_ctx *ctx, const char *name)
417{
418 LY_CHECK_ARG_RET(ctx, ctx, name, NULL);
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100419 return ly_ctx_get_module_implemented_by(ctx, name, offsetof(struct lys_module, name));
Radek Krejcib7db73a2018-10-24 14:18:40 +0200420}
421
Radek Krejci0af46292019-01-11 16:02:31 +0100422API struct lys_module *
Radek Krejcib7db73a2018-10-24 14:18:40 +0200423ly_ctx_get_module_implemented_ns(const struct ly_ctx *ctx, const char *ns)
424{
425 LY_CHECK_ARG_RET(ctx, ctx, ns, NULL);
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100426 return ly_ctx_get_module_implemented_by(ctx, ns, offsetof(struct lys_module, ns));
Radek Krejcib7db73a2018-10-24 14:18:40 +0200427}
428
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100429struct lysp_submodule *
Radek Krejcid33273d2018-10-25 14:55:52 +0200430ly_ctx_get_submodule(const struct ly_ctx *ctx, const char *module, const char *submodule, const char *revision)
431{
432 const struct lys_module *mod;
433 struct lysp_include *inc;
Radek Krejcifaa1eac2018-10-30 14:34:55 +0100434 unsigned int v, u;
Radek Krejcid33273d2018-10-25 14:55:52 +0200435
Radek Krejcifaa1eac2018-10-30 14:34:55 +0100436 assert(submodule);
437
438 for (v = 0; v < ctx->list.count; ++v) {
439 mod = ctx->list.objs[v];
Radek Krejcid33273d2018-10-25 14:55:52 +0200440 if (!mod->parsed) {
441 continue;
442 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100443 if (module && strcmp(module, mod->name)) {
Radek Krejcifaa1eac2018-10-30 14:34:55 +0100444 continue;
445 }
Radek Krejcid33273d2018-10-25 14:55:52 +0200446
447 LY_ARRAY_FOR(mod->parsed->includes, u) {
Radek Krejci086c7132018-10-26 15:29:04 +0200448 if (mod->parsed->includes[u].submodule && !strcmp(submodule, mod->parsed->includes[u].submodule->name)) {
Radek Krejcid33273d2018-10-25 14:55:52 +0200449 inc = &mod->parsed->includes[u];
450 if (!revision) {
451 if (inc->submodule->latest_revision) {
452 return inc->submodule;
453 }
454 } else if (inc->submodule->revs && !strcmp(revision, inc->submodule->revs[0].date)) {
455 return inc->submodule;
456 }
457 }
458 }
459 }
460
461 return NULL;
462}
463
Radek Krejci096235c2019-01-11 11:12:19 +0100464/* TODO ly_ctx_load_module() via lysp_load_module() */
465
Radek Krejci0af5f5d2018-09-07 15:00:30 +0200466API void
Radek Krejcie9e987e2018-10-31 12:50:27 +0100467ly_ctx_reset_latests(struct ly_ctx *ctx)
468{
469 unsigned int u,v ;
470 struct lys_module *mod;
471
472 for (u = 0; u < ctx->list.count; ++u) {
473 mod = ctx->list.objs[u];
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100474 if (mod->latest_revision == 2) {
475 mod->latest_revision = 1;
Radek Krejcie9e987e2018-10-31 12:50:27 +0100476 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100477 if (mod->parsed && mod->parsed->includes) {
478 for (v = 0; v < LY_ARRAY_SIZE(mod->parsed->includes); ++v) {
479 if (mod->parsed->includes[v].submodule->latest_revision == 2) {
480 mod->parsed->includes[v].submodule->latest_revision = 1;
Radek Krejcie9e987e2018-10-31 12:50:27 +0100481 }
482 }
483 }
484 }
485}
486
Radek Krejci95710c92019-02-11 15:49:55 +0100487LY_ERR
488ly_ctx_module_implement_internal(struct ly_ctx *ctx, struct lys_module *mod, uint8_t value)
Radek Krejci0af46292019-01-11 16:02:31 +0100489{
490 struct lys_module *m;
491
492 LY_CHECK_ARG_RET(ctx, mod, LY_EINVAL);
493
494 if (mod->implemented) {
495 return LY_SUCCESS;
496 }
497
498 /* we have module from the current context */
499 m = ly_ctx_get_module_implemented(ctx, mod->name);
500 if (m) {
501 if (m != mod) {
502 /* check collision with other implemented revision */
503 LOGERR(ctx, LY_EDENIED, "Module \"%s\" is present in the context in other implemented revision (%s).",
504 mod->name, mod->revision ? mod->revision : "module without revision");
505 return LY_EDENIED;
506 } else {
507 /* mod is already implemented */
508 return LY_SUCCESS;
509 }
510 }
511
512 /* mark the module implemented, check for collision was already done */
Radek Krejci95710c92019-02-11 15:49:55 +0100513 mod->implemented = value;
Radek Krejci0af46292019-01-11 16:02:31 +0100514
515 /* compile the schema */
Radek Krejci95710c92019-02-11 15:49:55 +0100516 LY_CHECK_RET(lys_compile(mod, LYSC_OPT_INTERNAL));
Radek Krejci0af46292019-01-11 16:02:31 +0100517
518 return LY_SUCCESS;
519}
520
Radek Krejci95710c92019-02-11 15:49:55 +0100521API LY_ERR
522ly_ctx_module_implement(struct ly_ctx *ctx, struct lys_module *mod)
523{
524 return ly_ctx_module_implement_internal(ctx, mod, 1);
525}
526
Radek Krejcie9e987e2018-10-31 12:50:27 +0100527API void
Radek Krejciad573502018-09-07 15:26:55 +0200528ly_ctx_destroy(struct ly_ctx *ctx, void (*private_destructor)(const struct lysc_node *node, void *priv))
Radek Krejci0af5f5d2018-09-07 15:00:30 +0200529{
Radek Krejci418823a2018-09-20 16:42:13 +0200530 if (!ctx) {
531 return;
532 }
Radek Krejci0af5f5d2018-09-07 15:00:30 +0200533
534 /* models list */
Michal Vaskob34480a2018-09-17 10:34:45 +0200535 for (; ctx->list.count; ctx->list.count--) {
Radek Krejci0af5f5d2018-09-07 15:00:30 +0200536 /* remove the module */
Radek Krejci86d106e2018-10-18 09:53:19 +0200537 lys_module_free(ctx->list.objs[ctx->list.count - 1], private_destructor);
Radek Krejci0af5f5d2018-09-07 15:00:30 +0200538 }
539 free(ctx->list.objs);
540
541 /* search paths list */
Radek Krejcia40f21b2018-09-18 10:42:08 +0200542 ly_set_erase(&ctx->search_paths, free);
Radek Krejci0af5f5d2018-09-07 15:00:30 +0200543
544 /* clean the error list */
545 ly_err_clean(ctx, 0);
546 pthread_key_delete(ctx->errlist_key);
547
548 /* dictionary */
549 lydict_clean(&ctx->dict);
550
551#if 0 /* TODO when plugins implemented */
552 /* plugins - will be removed only if this is the last context */
553 ly_clean_plugins();
554#endif
555
556 free(ctx);
557}