blob: 9de05924d94598a17dc314862705b123d643bff6 [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 Krejcie7b95092019-05-15 11:03:07 +020017#include <assert.h>
Radek Krejci0af5f5d2018-09-07 15:00:30 +020018#include <errno.h>
Radek Krejcie7b95092019-05-15 11:03:07 +020019#include <pthread.h>
20#include <stddef.h>
Radek Krejci7ada9a02018-09-07 15:34:05 +020021#include <stdlib.h>
22#include <string.h>
Radek Krejci0e212402018-09-20 11:29:38 +020023#include <sys/stat.h>
Radek Krejci0af5f5d2018-09-07 15:00:30 +020024#include <unistd.h>
25
Radek Krejci0af5f5d2018-09-07 15:00:30 +020026#include "context.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020027#include "hash_table.h"
28#include "set.h"
29#include "tree.h"
Radek Krejci86d106e2018-10-18 09:53:19 +020030#include "tree_schema_internal.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020031#include "plugins_types.h"
Radek Krejci0af5f5d2018-09-07 15:00:30 +020032
Radek Krejci86d106e2018-10-18 09:53:19 +020033#define LY_INTERNAL_MODS_COUNT 6
Radek Krejci0af5f5d2018-09-07 15:00:30 +020034
Radek Krejcif3f47842018-11-15 11:22:15 +010035#include "../models/ietf-yang-metadata@2016-08-05.h"
36#include "../models/yang@2017-02-20.h"
37#include "../models/ietf-inet-types@2013-07-15.h"
38#include "../models/ietf-yang-types@2013-07-15.h"
39#include "../models/ietf-datastores@2017-08-17.h"
40#include "../models/ietf-yang-library@2018-01-17.h"
Radek Krejci0af5f5d2018-09-07 15:00:30 +020041#define IETF_YANG_LIB_REV "2018-01-17"
42
Radek Krejci0af5f5d2018-09-07 15:00:30 +020043static struct internal_modules_s {
44 const char *name;
45 const char *revision;
46 const char *data;
47 uint8_t implemented;
48 LYS_INFORMAT format;
49} internal_modules[LY_INTERNAL_MODS_COUNT] = {
Radek Krejci86d106e2018-10-18 09:53:19 +020050 {"ietf-yang-metadata", "2016-08-05", (const char*)ietf_yang_metadata_2016_08_05_yang, 0, LYS_IN_YANG},
51 {"yang", "2017-02-20", (const char*)yang_2017_02_20_yang, 1, LYS_IN_YANG},
52 {"ietf-inet-types", "2013-07-15", (const char*)ietf_inet_types_2013_07_15_yang, 0, LYS_IN_YANG},
53 {"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 +020054 /* ietf-datastores and ietf-yang-library must be right here at the end of the list! */
Radek Krejci096235c2019-01-11 11:12:19 +010055 {"ietf-datastores", "2017-08-17", (const char*)ietf_datastores_2017_08_17_yang, 1, LYS_IN_YANG},
Radek Krejci86d106e2018-10-18 09:53:19 +020056 {"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 +020057};
58
59API LY_ERR
60ly_ctx_set_searchdir(struct ly_ctx *ctx, const char *search_dir)
61{
Radek Krejci0e212402018-09-20 11:29:38 +020062 struct stat st;
Radek Krejci0af5f5d2018-09-07 15:00:30 +020063 char *new_dir = NULL;
Radek Krejci0e212402018-09-20 11:29:38 +020064 unsigned int u;
Radek Krejci0af5f5d2018-09-07 15:00:30 +020065
66 LY_CHECK_ARG_RET(ctx, ctx, LY_EINVAL);
67
68 if (search_dir) {
Radek Krejci0af5f5d2018-09-07 15:00:30 +020069 new_dir = realpath(search_dir, NULL);
Radek Krejcia77e0e12018-09-20 12:39:15 +020070 LY_CHECK_ERR_RET(!new_dir,
Radek Krejci3a077b92019-04-09 17:10:35 +020071 LOGERR(ctx, LY_ESYS, "Unable to use search directory \"%s\" (%s).", search_dir, strerror(errno)),
72 LY_EINVAL);
73 if (strcmp(search_dir, new_dir)) {
74 LOGVRB("Canonicalizing search directory string from \"%s\" to \"%s\".", search_dir, new_dir);
75 }
76 LY_CHECK_ERR_RET(access(new_dir, R_OK | X_OK),
77 LOGERR(ctx, LY_ESYS, "Unable to fully access search directory \"%s\" (%s).", new_dir, strerror(errno)); free(new_dir),
78 LY_EINVAL);
79 LY_CHECK_ERR_RET(stat(new_dir, &st),
80 LOGERR(ctx, LY_ESYS, "stat() failed for \"%s\" (%s).", new_dir, strerror(errno)); free(new_dir),
Radek Krejcia77e0e12018-09-20 12:39:15 +020081 LY_ESYS);
Radek Krejci3a077b92019-04-09 17:10:35 +020082 LY_CHECK_ERR_RET(!S_ISDIR(st.st_mode),
83 LOGERR(ctx, LY_ESYS, "Given search directory \"%s\" is not a directory.", new_dir); free(new_dir),
84 LY_EINVAL);
Radek Krejci0e212402018-09-20 11:29:38 +020085 /* avoid path duplication */
86 for (u = 0; u < ctx->search_paths.count; ++u) {
Radek Krejci14946ab2018-09-20 13:42:06 +020087 if (!strcmp(new_dir, ctx->search_paths.objs[u])) {
Radek Krejci0e212402018-09-20 11:29:38 +020088 free(new_dir);
Radek Krejci14946ab2018-09-20 13:42:06 +020089 return LY_EEXIST;
Radek Krejci0e212402018-09-20 11:29:38 +020090 }
91 }
92 if (ly_set_add(&ctx->search_paths, new_dir, LY_SET_OPT_USEASLIST) == -1) {
Radek Krejci0af5f5d2018-09-07 15:00:30 +020093 free(new_dir);
94 return LY_EMEM;
95 }
96
Radek Krejcie9e987e2018-10-31 12:50:27 +010097 /* new searchdir - possibly more latest revision available */
98 ly_ctx_reset_latests(ctx);
99
Radek Krejci0e212402018-09-20 11:29:38 +0200100 return LY_SUCCESS;
Radek Krejci0af5f5d2018-09-07 15:00:30 +0200101 } else {
102 /* consider that no change is not actually an error */
103 return LY_SUCCESS;
104 }
Radek Krejci0af5f5d2018-09-07 15:00:30 +0200105}
106
107API const char * const *
108ly_ctx_get_searchdirs(const struct ly_ctx *ctx)
109{
Radek Krejci05026432018-09-20 12:13:43 +0200110 void **new;
111
Radek Krejci0af5f5d2018-09-07 15:00:30 +0200112 LY_CHECK_ARG_RET(ctx, ctx, NULL);
Radek Krejci05026432018-09-20 12:13:43 +0200113
114 if (ctx->search_paths.count == ctx->search_paths.size) {
115 /* not enough space for terminating NULL byte */
116 new = realloc(((struct ly_ctx *)ctx)->search_paths.objs, (ctx->search_paths.size + 8) * sizeof *ctx->search_paths.objs);
117 LY_CHECK_ERR_RET(!new, LOGMEM(NULL), NULL);
118 ((struct ly_ctx *)ctx)->search_paths.size += 8;
119 ((struct ly_ctx *)ctx)->search_paths.objs = new;
120 }
121 /* set terminating NULL byte to the strings list */
122 ctx->search_paths.objs[ctx->search_paths.count] = NULL;
123
Radek Krejci0af5f5d2018-09-07 15:00:30 +0200124 return (const char * const *)ctx->search_paths.objs;
125}
126
127API LY_ERR
Radek Krejci0759b792018-09-20 13:53:15 +0200128ly_ctx_unset_searchdirs(struct ly_ctx *ctx, const char *value)
Radek Krejci0af5f5d2018-09-07 15:00:30 +0200129{
Radek Krejci0759b792018-09-20 13:53:15 +0200130 unsigned int index;
131
Radek Krejcicd649072018-09-20 12:14:45 +0200132 LY_CHECK_ARG_RET(ctx, ctx, LY_EINVAL);
Radek Krejcicd649072018-09-20 12:14:45 +0200133
Michal Vaskob34480a2018-09-17 10:34:45 +0200134 if (!ctx->search_paths.count) {
Radek Krejci0af5f5d2018-09-07 15:00:30 +0200135 return LY_SUCCESS;
136 }
137
Radek Krejci0759b792018-09-20 13:53:15 +0200138 if (value) {
Radek Krejci0af5f5d2018-09-07 15:00:30 +0200139 /* remove specific search directory */
Radek Krejci0759b792018-09-20 13:53:15 +0200140 for (index = 0; index < ctx->search_paths.count; ++index) {
141 if (!strcmp(value, ctx->search_paths.objs[index])) {
142 break;
143 }
144 }
145 if (index == ctx->search_paths.count) {
146 LOGARG(ctx, value);
147 return LY_EINVAL;
148 } else {
149 return ly_set_rm_index(&ctx->search_paths, index, free);
150 }
Radek Krejci0af5f5d2018-09-07 15:00:30 +0200151 } else {
152 /* remove them all */
Radek Krejcia40f21b2018-09-18 10:42:08 +0200153 ly_set_erase(&ctx->search_paths, free);
Radek Krejci0af5f5d2018-09-07 15:00:30 +0200154 memset(&ctx->search_paths, 0, sizeof ctx->search_paths);
155 }
156
157 return LY_SUCCESS;
158}
159
160API LY_ERR
Radek Krejcied5acc52019-04-25 15:57:04 +0200161ly_ctx_unset_searchdir(struct ly_ctx *ctx, unsigned int index)
162{
163 LY_CHECK_ARG_RET(ctx, ctx, LY_EINVAL);
164
165 if (!ctx->search_paths.count) {
166 return LY_SUCCESS;
167 }
168
169 if (index >= ctx->search_paths.count) {
170 LOGARG(ctx, value);
171 return LY_EINVAL;
172 } else {
173 return ly_set_rm_index(&ctx->search_paths, index, free);
174 }
175
176 return LY_SUCCESS;
177}
178
179API const struct lys_module *
180ly_ctx_load_module(struct ly_ctx *ctx, const char *name, const char *revision)
181{
182 struct lys_module *result = NULL;
183
184 LY_CHECK_ARG_RET(ctx, ctx, name, NULL);
185
186 LY_CHECK_RET(lysp_load_module(ctx, name, revision, 1, 0, &result), NULL);
187 return result;
188}
189
190API LY_ERR
Radek Krejci0af5f5d2018-09-07 15:00:30 +0200191ly_ctx_new(const char *search_dir, int options, struct ly_ctx **new_ctx)
192{
193 struct ly_ctx *ctx = NULL;
194 struct lys_module *module;
195 char *search_dir_list;
196 char *sep, *dir;
Radek Krejci86d106e2018-10-18 09:53:19 +0200197 int i;
Radek Krejci0af5f5d2018-09-07 15:00:30 +0200198 LY_ERR rc = LY_SUCCESS;
Radek Krejci0af5f5d2018-09-07 15:00:30 +0200199
Radek Krejcied5acc52019-04-25 15:57:04 +0200200 LY_CHECK_ARG_RET(NULL, new_ctx, LY_EINVAL);
201
Radek Krejci0af5f5d2018-09-07 15:00:30 +0200202 ctx = calloc(1, sizeof *ctx);
Radek Krejciad573502018-09-07 15:26:55 +0200203 LY_CHECK_ERR_RET(!ctx, LOGMEM(NULL), LY_EMEM);
Radek Krejci0af5f5d2018-09-07 15:00:30 +0200204
205 /* dictionary */
206 lydict_init(&ctx->dict);
207
Radek Krejciad573502018-09-07 15:26:55 +0200208#if 0 /* TODO when plugins implemented */
Radek Krejci0af5f5d2018-09-07 15:00:30 +0200209 /* plugins */
210 ly_load_plugins();
Radek Krejciad573502018-09-07 15:26:55 +0200211#endif
Radek Krejci0af5f5d2018-09-07 15:00:30 +0200212
213 /* initialize thread-specific key */
Radek Krejciad573502018-09-07 15:26:55 +0200214 while ((pthread_key_create(&ctx->errlist_key, ly_err_free)) == EAGAIN);
Radek Krejci0af5f5d2018-09-07 15:00:30 +0200215
216 /* models list */
217 ctx->flags = options;
218 if (search_dir) {
219 search_dir_list = strdup(search_dir);
220 LY_CHECK_ERR_GOTO(!search_dir_list, LOGMEM(NULL); rc = LY_EMEM, error);
221
222 for (dir = search_dir_list; (sep = strchr(dir, ':')) != NULL && rc == LY_SUCCESS; dir = sep + 1) {
223 *sep = 0;
224 rc = ly_ctx_set_searchdir(ctx, dir);
Radek Krejci14946ab2018-09-20 13:42:06 +0200225 if (rc == LY_EEXIST) {
226 /* ignore duplication */
227 rc = LY_SUCCESS;
228 }
Radek Krejci0af5f5d2018-09-07 15:00:30 +0200229 }
230 if (*dir && rc == LY_SUCCESS) {
231 rc = ly_ctx_set_searchdir(ctx, dir);
Radek Krejci14946ab2018-09-20 13:42:06 +0200232 if (rc == LY_EEXIST) {
233 /* ignore duplication */
234 rc = LY_SUCCESS;
235 }
Radek Krejci0af5f5d2018-09-07 15:00:30 +0200236 }
237 free(search_dir_list);
238
239 /* If ly_ctx_set_searchdir() failed, the error is already logged. Just exit */
240 if (rc != LY_SUCCESS) {
241 goto error;
242 }
243 }
244 ctx->module_set_id = 1;
245
246 /* load internal modules */
Radek Krejci86d106e2018-10-18 09:53:19 +0200247 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 +0100248 module = (struct lys_module *)lys_parse_mem_module(ctx, internal_modules[i].data, internal_modules[i].format,
249 internal_modules[i].implemented, NULL, NULL);
Radek Krejci0e75e6f2018-11-08 09:40:02 +0100250 LY_CHECK_ERR_GOTO(!module, rc = ly_errcode(ctx), error);
Radek Krejci096235c2019-01-11 11:12:19 +0100251 LY_CHECK_GOTO((rc = lys_compile(module, 0)), error);
Radek Krejci0af5f5d2018-09-07 15:00:30 +0200252 }
253
254 *new_ctx = ctx;
255 return rc;
256
257error:
258 ly_ctx_destroy(ctx, NULL);
259 return rc;
260}
Radek Krejci0af5f5d2018-09-07 15:00:30 +0200261API int
Radek Krejci3fbe89a2018-09-20 13:54:46 +0200262ly_ctx_get_options(const struct ly_ctx *ctx)
Radek Krejci0af5f5d2018-09-07 15:00:30 +0200263{
Radek Krejci3fbe89a2018-09-20 13:54:46 +0200264 LY_CHECK_ARG_RET(ctx, ctx, 0);
Radek Krejci0af5f5d2018-09-07 15:00:30 +0200265 return ctx->flags;
266}
267
Radek Krejcica828d92018-09-20 14:19:43 +0200268API LY_ERR
269ly_ctx_set_option(struct ly_ctx *ctx, int option)
270{
271 LY_CHECK_ARG_RET(ctx, ctx, LY_EINVAL);
272 LY_CHECK_ERR_RET(option & LY_CTX_NOYANGLIBRARY, LOGARG(ctx, option), LY_EINVAL);
273
274 /* set the option(s) */
275 ctx->flags |= option;
276
277 return LY_SUCCESS;
278}
279
280API LY_ERR
281ly_ctx_unset_option(struct ly_ctx *ctx, int option)
282{
283 LY_CHECK_ARG_RET(ctx, ctx, LY_EINVAL);
284 LY_CHECK_ERR_RET(option & LY_CTX_NOYANGLIBRARY, LOGARG(ctx, option), LY_EINVAL);
285
286 /* unset the option(s) */
287 ctx->flags &= ~option;
288
289 return LY_SUCCESS;
290}
291
Radek Krejci0af5f5d2018-09-07 15:00:30 +0200292API uint16_t
293ly_ctx_get_module_set_id(const struct ly_ctx *ctx)
294{
Radek Krejci3fbe89a2018-09-20 13:54:46 +0200295 LY_CHECK_ARG_RET(ctx, ctx, 0);
Radek Krejci0af5f5d2018-09-07 15:00:30 +0200296 return ctx->module_set_id;
297}
298
Radek Krejcid33273d2018-10-25 14:55:52 +0200299API void
300ly_ctx_set_module_imp_clb(struct ly_ctx *ctx, ly_module_imp_clb clb, void *user_data)
301{
302 LY_CHECK_ARG_RET(ctx, ctx,);
303
304 ctx->imp_clb = clb;
305 ctx->imp_clb_data = user_data;
306}
307
308API ly_module_imp_clb
309ly_ctx_get_module_imp_clb(const struct ly_ctx *ctx, void **user_data)
310{
311 LY_CHECK_ARG_RET(ctx, ctx, NULL);
312
313 if (user_data) {
314 *user_data = ctx->imp_clb_data;
315 }
316 return ctx->imp_clb;
317}
318
Radek Krejcied5acc52019-04-25 15:57:04 +0200319API const struct lys_module *
320ly_ctx_get_module_iter(const struct ly_ctx *ctx, unsigned int *index)
321{
322 LY_CHECK_ARG_RET(ctx, ctx, index, NULL);
323
Radek Krejci2390fb52019-04-30 13:27:43 +0200324 if (*index < (unsigned)ctx->list.count) {
Radek Krejcied5acc52019-04-25 15:57:04 +0200325 return ctx->list.objs[(*index)++];
Radek Krejci2390fb52019-04-30 13:27:43 +0200326 } else {
327 return NULL;
Radek Krejcied5acc52019-04-25 15:57:04 +0200328 }
Radek Krejcied5acc52019-04-25 15:57:04 +0200329}
330
Radek Krejcib7db73a2018-10-24 14:18:40 +0200331/**
332 * @brief Iterate over the modules in the given context. Returned modules must match the given key at the offset of
333 * lysp_module and lysc_module structures (they are supposed to be placed at the same offset in both structures).
334 *
335 * @param[in] ctx Context where to iterate.
336 * @param[in] key Key value to search for.
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100337 * @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 +0200338 * @param[in,out] Iterator to pass between the function calls. On the first call, the variable is supposed to be
339 * initiated to 0. After each call returning a module, the value is greater by 1 than the index of the returned
340 * module in the context.
341 * @return Module matching the given key, NULL if no such module found.
342 */
Radek Krejci0af46292019-01-11 16:02:31 +0100343static struct lys_module *
Radek Krejcib7db73a2018-10-24 14:18:40 +0200344ly_ctx_get_module_by_iter(const struct ly_ctx *ctx, const char *key, size_t key_offset, unsigned int *index)
345{
Radek Krejci0af46292019-01-11 16:02:31 +0100346 struct lys_module *mod;
Radek Krejcib7db73a2018-10-24 14:18:40 +0200347 const char *value;
348
349 for (; *index < ctx->list.count; ++(*index)) {
350 mod = ctx->list.objs[*index];
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100351 value = *(const char**)(((int8_t*)(mod)) + key_offset);
Radek Krejcib7db73a2018-10-24 14:18:40 +0200352 if (!strcmp(key, value)) {
353 /* increment index for the next run */
354 ++(*index);
355 return mod;
356 }
357 }
358 /* done */
359 return NULL;
360}
361
362/**
363 * @brief Unifying function for ly_ctx_get_module() and ly_ctx_get_module_ns()
364 * @param[in] ctx Context where to search.
365 * @param[in] key Name or Namespace as a search key.
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100366 * @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 +0200367 * @param[in] revision Revision date to match. If NULL, the matching module must have no revision. To search for the latest
368 * revision module, use ly_ctx_get_module_latest_by().
369 * @return Matching module if any.
370 */
Radek Krejci0af46292019-01-11 16:02:31 +0100371static struct lys_module *
Radek Krejcib7db73a2018-10-24 14:18:40 +0200372ly_ctx_get_module_by(const struct ly_ctx *ctx, const char *key, size_t key_offset, const char *revision)
373{
Radek Krejci0af46292019-01-11 16:02:31 +0100374 struct lys_module *mod;
Radek Krejcib7db73a2018-10-24 14:18:40 +0200375 unsigned int index = 0;
376
377 while ((mod = ly_ctx_get_module_by_iter(ctx, key, key_offset, &index))) {
378 if (!revision) {
Radek Krejci0af46292019-01-11 16:02:31 +0100379 if (!mod->revision) {
Radek Krejcib7db73a2018-10-24 14:18:40 +0200380 /* found requested module without revision */
381 return mod;
382 }
383 } else {
Radek Krejci0af46292019-01-11 16:02:31 +0100384 if (mod->revision && !strcmp(mod->revision, revision)) {
Radek Krejcib7db73a2018-10-24 14:18:40 +0200385 /* found requested module of the specific revision */
386 return mod;
387 }
388 }
389 }
390
391 return NULL;
392}
393
Radek Krejci0af46292019-01-11 16:02:31 +0100394API struct lys_module *
Radek Krejcib7db73a2018-10-24 14:18:40 +0200395ly_ctx_get_module_ns(const struct ly_ctx *ctx, const char *ns, const char *revision)
396{
397 LY_CHECK_ARG_RET(ctx, ctx, ns, NULL);
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100398 return ly_ctx_get_module_by(ctx, ns, offsetof(struct lys_module, ns), revision);
Radek Krejcib7db73a2018-10-24 14:18:40 +0200399}
400
Radek Krejci0af46292019-01-11 16:02:31 +0100401API struct lys_module *
Radek Krejcib7db73a2018-10-24 14:18:40 +0200402ly_ctx_get_module(const struct ly_ctx *ctx, const char *name, const char *revision)
403{
404 LY_CHECK_ARG_RET(ctx, ctx, name, NULL);
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100405 return ly_ctx_get_module_by(ctx, name, offsetof(struct lys_module, name), revision);
Radek Krejcib7db73a2018-10-24 14:18:40 +0200406}
407
408/**
409 * @brief Unifying function for ly_ctx_get_module_latest() and ly_ctx_get_module_latest_ns()
410 * @param[in] ctx Context where to search.
411 * @param[in] key Name or Namespace as a search key.
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100412 * @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 +0200413 * @return Matching module if any.
414 */
Radek Krejci0af46292019-01-11 16:02:31 +0100415static struct lys_module *
Radek Krejcib7db73a2018-10-24 14:18:40 +0200416ly_ctx_get_module_latest_by(const struct ly_ctx *ctx, const char *key, size_t key_offset)
417{
Radek Krejci0af46292019-01-11 16:02:31 +0100418 struct lys_module *mod;
Radek Krejcib7db73a2018-10-24 14:18:40 +0200419 unsigned int index = 0;
Radek Krejcib7db73a2018-10-24 14:18:40 +0200420
421 while ((mod = ly_ctx_get_module_by_iter(ctx, key, key_offset, &index))) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100422 if (mod->latest_revision) {
Radek Krejci9f5e6fb2018-10-25 09:26:12 +0200423 return mod;
Radek Krejcib7db73a2018-10-24 14:18:40 +0200424 }
425 }
426
Radek Krejci9f5e6fb2018-10-25 09:26:12 +0200427 return NULL;
Radek Krejcib7db73a2018-10-24 14:18:40 +0200428}
429
Radek Krejci0af46292019-01-11 16:02:31 +0100430API struct lys_module *
Radek Krejcib7db73a2018-10-24 14:18:40 +0200431ly_ctx_get_module_latest(const struct ly_ctx *ctx, const char *name)
432{
433 LY_CHECK_ARG_RET(ctx, ctx, name, NULL);
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100434 return ly_ctx_get_module_latest_by(ctx, name, offsetof(struct lys_module, name));
Radek Krejcib7db73a2018-10-24 14:18:40 +0200435}
436
Radek Krejci0af46292019-01-11 16:02:31 +0100437API struct lys_module *
Radek Krejcib7db73a2018-10-24 14:18:40 +0200438ly_ctx_get_module_latest_ns(const struct ly_ctx *ctx, const char *ns)
439{
440 LY_CHECK_ARG_RET(ctx, ctx, ns, NULL);
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100441 return ly_ctx_get_module_latest_by(ctx, ns, offsetof(struct lys_module, ns));
Radek Krejcib7db73a2018-10-24 14:18:40 +0200442}
443
444/**
445 * @brief Unifying function for ly_ctx_get_module_implemented() and ly_ctx_get_module_implemented_ns()
446 * @param[in] ctx Context where to search.
447 * @param[in] key Name or Namespace as a search key.
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100448 * @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 +0200449 * @return Matching module if any.
450 */
Radek Krejci0af46292019-01-11 16:02:31 +0100451static struct lys_module *
Radek Krejcib7db73a2018-10-24 14:18:40 +0200452ly_ctx_get_module_implemented_by(const struct ly_ctx *ctx, const char *key, size_t key_offset)
453{
Radek Krejci0af46292019-01-11 16:02:31 +0100454 struct lys_module *mod;
Radek Krejcib7db73a2018-10-24 14:18:40 +0200455 unsigned int index = 0;
456
457 while ((mod = ly_ctx_get_module_by_iter(ctx, key, key_offset, &index))) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100458 if (mod->implemented) {
Radek Krejcib7db73a2018-10-24 14:18:40 +0200459 return mod;
460 }
461 }
462
463 return NULL;
464}
465
Radek Krejci0af46292019-01-11 16:02:31 +0100466API struct lys_module *
Radek Krejcib7db73a2018-10-24 14:18:40 +0200467ly_ctx_get_module_implemented(const struct ly_ctx *ctx, const char *name)
468{
469 LY_CHECK_ARG_RET(ctx, ctx, name, NULL);
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100470 return ly_ctx_get_module_implemented_by(ctx, name, offsetof(struct lys_module, name));
Radek Krejcib7db73a2018-10-24 14:18:40 +0200471}
472
Radek Krejci0af46292019-01-11 16:02:31 +0100473API struct lys_module *
Radek Krejcib7db73a2018-10-24 14:18:40 +0200474ly_ctx_get_module_implemented_ns(const struct ly_ctx *ctx, const char *ns)
475{
476 LY_CHECK_ARG_RET(ctx, ctx, ns, NULL);
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100477 return ly_ctx_get_module_implemented_by(ctx, ns, offsetof(struct lys_module, ns));
Radek Krejcib7db73a2018-10-24 14:18:40 +0200478}
479
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100480struct lysp_submodule *
Radek Krejcid33273d2018-10-25 14:55:52 +0200481ly_ctx_get_submodule(const struct ly_ctx *ctx, const char *module, const char *submodule, const char *revision)
482{
483 const struct lys_module *mod;
484 struct lysp_include *inc;
Radek Krejcifaa1eac2018-10-30 14:34:55 +0100485 unsigned int v, u;
Radek Krejcid33273d2018-10-25 14:55:52 +0200486
Radek Krejcifaa1eac2018-10-30 14:34:55 +0100487 assert(submodule);
488
489 for (v = 0; v < ctx->list.count; ++v) {
490 mod = ctx->list.objs[v];
Radek Krejcid33273d2018-10-25 14:55:52 +0200491 if (!mod->parsed) {
492 continue;
493 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100494 if (module && strcmp(module, mod->name)) {
Radek Krejcifaa1eac2018-10-30 14:34:55 +0100495 continue;
496 }
Radek Krejcid33273d2018-10-25 14:55:52 +0200497
498 LY_ARRAY_FOR(mod->parsed->includes, u) {
Radek Krejci086c7132018-10-26 15:29:04 +0200499 if (mod->parsed->includes[u].submodule && !strcmp(submodule, mod->parsed->includes[u].submodule->name)) {
Radek Krejcid33273d2018-10-25 14:55:52 +0200500 inc = &mod->parsed->includes[u];
501 if (!revision) {
502 if (inc->submodule->latest_revision) {
503 return inc->submodule;
504 }
505 } else if (inc->submodule->revs && !strcmp(revision, inc->submodule->revs[0].date)) {
506 return inc->submodule;
507 }
508 }
509 }
510 }
511
512 return NULL;
513}
514
Radek Krejci096235c2019-01-11 11:12:19 +0100515/* TODO ly_ctx_load_module() via lysp_load_module() */
516
Radek Krejci0af5f5d2018-09-07 15:00:30 +0200517API void
Radek Krejcie9e987e2018-10-31 12:50:27 +0100518ly_ctx_reset_latests(struct ly_ctx *ctx)
519{
520 unsigned int u,v ;
521 struct lys_module *mod;
522
523 for (u = 0; u < ctx->list.count; ++u) {
524 mod = ctx->list.objs[u];
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100525 if (mod->latest_revision == 2) {
526 mod->latest_revision = 1;
Radek Krejcie9e987e2018-10-31 12:50:27 +0100527 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100528 if (mod->parsed && mod->parsed->includes) {
529 for (v = 0; v < LY_ARRAY_SIZE(mod->parsed->includes); ++v) {
530 if (mod->parsed->includes[v].submodule->latest_revision == 2) {
531 mod->parsed->includes[v].submodule->latest_revision = 1;
Radek Krejcie9e987e2018-10-31 12:50:27 +0100532 }
533 }
534 }
535 }
536}
537
Radek Krejci95710c92019-02-11 15:49:55 +0100538LY_ERR
539ly_ctx_module_implement_internal(struct ly_ctx *ctx, struct lys_module *mod, uint8_t value)
Radek Krejci0af46292019-01-11 16:02:31 +0100540{
541 struct lys_module *m;
542
543 LY_CHECK_ARG_RET(ctx, mod, LY_EINVAL);
544
545 if (mod->implemented) {
546 return LY_SUCCESS;
547 }
548
549 /* we have module from the current context */
550 m = ly_ctx_get_module_implemented(ctx, mod->name);
551 if (m) {
552 if (m != mod) {
553 /* check collision with other implemented revision */
554 LOGERR(ctx, LY_EDENIED, "Module \"%s\" is present in the context in other implemented revision (%s).",
555 mod->name, mod->revision ? mod->revision : "module without revision");
556 return LY_EDENIED;
557 } else {
558 /* mod is already implemented */
559 return LY_SUCCESS;
560 }
561 }
562
563 /* mark the module implemented, check for collision was already done */
Radek Krejci95710c92019-02-11 15:49:55 +0100564 mod->implemented = value;
Radek Krejci0af46292019-01-11 16:02:31 +0100565
566 /* compile the schema */
Radek Krejci95710c92019-02-11 15:49:55 +0100567 LY_CHECK_RET(lys_compile(mod, LYSC_OPT_INTERNAL));
Radek Krejci0af46292019-01-11 16:02:31 +0100568
569 return LY_SUCCESS;
570}
571
Radek Krejci95710c92019-02-11 15:49:55 +0100572API LY_ERR
573ly_ctx_module_implement(struct ly_ctx *ctx, struct lys_module *mod)
574{
575 return ly_ctx_module_implement_internal(ctx, mod, 1);
576}
577
Radek Krejcie9e987e2018-10-31 12:50:27 +0100578API void
Radek Krejciad573502018-09-07 15:26:55 +0200579ly_ctx_destroy(struct ly_ctx *ctx, void (*private_destructor)(const struct lysc_node *node, void *priv))
Radek Krejci0af5f5d2018-09-07 15:00:30 +0200580{
Radek Krejci418823a2018-09-20 16:42:13 +0200581 if (!ctx) {
582 return;
583 }
Radek Krejci0af5f5d2018-09-07 15:00:30 +0200584
585 /* models list */
Michal Vaskob34480a2018-09-17 10:34:45 +0200586 for (; ctx->list.count; ctx->list.count--) {
Radek Krejci0af5f5d2018-09-07 15:00:30 +0200587 /* remove the module */
Radek Krejci86d106e2018-10-18 09:53:19 +0200588 lys_module_free(ctx->list.objs[ctx->list.count - 1], private_destructor);
Radek Krejci0af5f5d2018-09-07 15:00:30 +0200589 }
590 free(ctx->list.objs);
591
592 /* search paths list */
Radek Krejcia40f21b2018-09-18 10:42:08 +0200593 ly_set_erase(&ctx->search_paths, free);
Radek Krejci0af5f5d2018-09-07 15:00:30 +0200594
595 /* clean the error list */
596 ly_err_clean(ctx, 0);
597 pthread_key_delete(ctx->errlist_key);
598
599 /* dictionary */
600 lydict_clean(&ctx->dict);
601
602#if 0 /* TODO when plugins implemented */
603 /* plugins - will be removed only if this is the last context */
604 ly_clean_plugins();
605#endif
606
607 free(ctx);
608}