Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 1 | /** |
| 2 | * @file context.c |
| 3 | * @author Radek Krejci <rkrejci@cesnet.cz> |
| 4 | * @brief context implementation for libyang |
| 5 | * |
| 6 | * Copyright (c) 2015 CESNET, z.s.p.o. |
| 7 | * |
Radek Krejci | 54f6fb3 | 2016-02-24 12:56:39 +0100 | [diff] [blame] | 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 |
Michal Vasko | 8de098c | 2016-02-26 10:00:25 +0100 | [diff] [blame] | 11 | * |
Radek Krejci | 54f6fb3 | 2016-02-24 12:56:39 +0100 | [diff] [blame] | 12 | * https://opensource.org/licenses/BSD-3-Clause |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 13 | */ |
| 14 | |
| 15 | #define _GNU_SOURCE |
Radek Krejci | fd4e6e3 | 2015-08-10 15:00:51 +0200 | [diff] [blame] | 16 | #include <stddef.h> |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 17 | #include <stdlib.h> |
| 18 | #include <string.h> |
| 19 | #include <sys/types.h> |
| 20 | #include <sys/stat.h> |
| 21 | #include <unistd.h> |
| 22 | #include <errno.h> |
| 23 | #include <fcntl.h> |
| 24 | |
| 25 | #include "common.h" |
| 26 | #include "context.h" |
Radek Krejci | 41912fe | 2015-10-22 10:22:12 +0200 | [diff] [blame] | 27 | #include "dict_private.h" |
Michal Vasko | 209a622 | 2015-10-16 09:51:07 +0200 | [diff] [blame] | 28 | #include "parser.h" |
Radek Krejci | bc9cf93 | 2015-07-30 11:09:39 +0200 | [diff] [blame] | 29 | #include "tree_internal.h" |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 30 | |
Radek Krejci | d9f7ec5 | 2016-02-11 16:27:01 +0100 | [diff] [blame] | 31 | #define YANG_FAKEMODULE_PATH "../models/yang@2016-02-11.h" |
Michal Vasko | 8d054e4 | 2015-08-03 12:42:06 +0200 | [diff] [blame] | 32 | #define IETF_INET_TYPES_PATH "../models/ietf-inet-types@2013-07-15.h" |
Michal Vasko | 21181c4 | 2015-08-03 13:46:45 +0200 | [diff] [blame] | 33 | #define IETF_YANG_TYPES_PATH "../models/ietf-yang-types@2013-07-15.h" |
Radek Krejci | bd9e8d2 | 2016-02-03 14:11:48 +0100 | [diff] [blame] | 34 | #define IETF_YANG_LIB_PATH "../models/ietf-yang-library@2016-02-01.h" |
| 35 | #define IETF_YANG_LIB_REV "2016-02-01" |
Michal Vasko | 8d054e4 | 2015-08-03 12:42:06 +0200 | [diff] [blame] | 36 | |
Radek Krejci | d9f7ec5 | 2016-02-11 16:27:01 +0100 | [diff] [blame] | 37 | #include YANG_FAKEMODULE_PATH |
Michal Vasko | 8d054e4 | 2015-08-03 12:42:06 +0200 | [diff] [blame] | 38 | #include IETF_INET_TYPES_PATH |
Michal Vasko | 21181c4 | 2015-08-03 13:46:45 +0200 | [diff] [blame] | 39 | #include IETF_YANG_TYPES_PATH |
Michal Vasko | 8d054e4 | 2015-08-03 12:42:06 +0200 | [diff] [blame] | 40 | #include IETF_YANG_LIB_PATH |
| 41 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 42 | API struct ly_ctx * |
| 43 | ly_ctx_new(const char *search_dir) |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 44 | { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 45 | struct ly_ctx *ctx; |
Michal Vasko | 70b6d69 | 2015-08-03 14:05:59 +0200 | [diff] [blame] | 46 | char *cwd; |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 47 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 48 | ctx = calloc(1, sizeof *ctx); |
| 49 | if (!ctx) { |
| 50 | LOGMEM; |
| 51 | return NULL; |
| 52 | } |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 53 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 54 | /* dictionary */ |
| 55 | lydict_init(&ctx->dict); |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 56 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 57 | /* models list */ |
| 58 | ctx->models.list = calloc(16, sizeof *ctx->models.list); |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 59 | if (!ctx->models.list) { |
| 60 | LOGMEM; |
| 61 | free(ctx); |
| 62 | return NULL; |
| 63 | } |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 64 | ctx->models.used = 0; |
| 65 | ctx->models.size = 16; |
| 66 | if (search_dir) { |
| 67 | cwd = get_current_dir_name(); |
| 68 | if (chdir(search_dir)) { |
| 69 | LOGERR(LY_ESYS, "Unable to use search directory \"%s\" (%s)", |
| 70 | search_dir, strerror(errno)); |
| 71 | free(cwd); |
Radek Krejci | fa0b5e0 | 2016-02-04 13:57:03 +0100 | [diff] [blame] | 72 | ly_ctx_destroy(ctx, NULL); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 73 | return NULL; |
| 74 | } |
| 75 | ctx->models.search_path = get_current_dir_name(); |
Radek Krejci | 15412ca | 2016-03-03 11:16:52 +0100 | [diff] [blame] | 76 | if (chdir(cwd)) { |
| 77 | LOGWRN("Unable to return back to working directory \"%s\" (%s)", |
| 78 | cwd, strerror(errno)); |
| 79 | } |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 80 | free(cwd); |
| 81 | } |
Michal Vasko | 14719b2 | 2015-08-03 12:47:55 +0200 | [diff] [blame] | 82 | ctx->models.module_set_id = 1; |
| 83 | |
Radek Krejci | d9f7ec5 | 2016-02-11 16:27:01 +0100 | [diff] [blame] | 84 | /* load (fake) YANG module */ |
| 85 | if (!lys_parse_mem(ctx, (char *)yang_2016_02_11_yin, LYS_IN_YIN)) { |
| 86 | ly_ctx_destroy(ctx, NULL); |
| 87 | return NULL; |
| 88 | } |
| 89 | |
Michal Vasko | 8d054e4 | 2015-08-03 12:42:06 +0200 | [diff] [blame] | 90 | /* load ietf-inet-types */ |
Radek Krejci | d9f7ec5 | 2016-02-11 16:27:01 +0100 | [diff] [blame] | 91 | if (!lys_parse_mem(ctx, (char *)ietf_inet_types_2013_07_15_yin, LYS_IN_YIN)) { |
Radek Krejci | fa0b5e0 | 2016-02-04 13:57:03 +0100 | [diff] [blame] | 92 | ly_ctx_destroy(ctx, NULL); |
Michal Vasko | 8d054e4 | 2015-08-03 12:42:06 +0200 | [diff] [blame] | 93 | return NULL; |
| 94 | } |
| 95 | |
| 96 | /* load ietf-yang-types */ |
Radek Krejci | d9f7ec5 | 2016-02-11 16:27:01 +0100 | [diff] [blame] | 97 | if (!lys_parse_mem(ctx, (char *)ietf_yang_types_2013_07_15_yin, LYS_IN_YIN)) { |
Radek Krejci | fa0b5e0 | 2016-02-04 13:57:03 +0100 | [diff] [blame] | 98 | ly_ctx_destroy(ctx, NULL); |
Michal Vasko | 8d054e4 | 2015-08-03 12:42:06 +0200 | [diff] [blame] | 99 | return NULL; |
| 100 | } |
| 101 | |
| 102 | /* load ietf-yang-library */ |
Radek Krejci | d9f7ec5 | 2016-02-11 16:27:01 +0100 | [diff] [blame] | 103 | if (!lys_parse_mem(ctx, (char *)ietf_yang_library_2016_02_01_yin, LYS_IN_YIN)) { |
Radek Krejci | fa0b5e0 | 2016-02-04 13:57:03 +0100 | [diff] [blame] | 104 | ly_ctx_destroy(ctx, NULL); |
Michal Vasko | 8d054e4 | 2015-08-03 12:42:06 +0200 | [diff] [blame] | 105 | return NULL; |
| 106 | } |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 107 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 108 | return ctx; |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 109 | } |
| 110 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 111 | API void |
Michal Vasko | 60ba9a6 | 2015-07-03 14:42:31 +0200 | [diff] [blame] | 112 | ly_ctx_set_searchdir(struct ly_ctx *ctx, const char *search_dir) |
| 113 | { |
| 114 | char *cwd; |
| 115 | |
| 116 | if (!ctx) { |
| 117 | return; |
| 118 | } |
| 119 | |
| 120 | if (search_dir) { |
| 121 | cwd = get_current_dir_name(); |
| 122 | if (chdir(search_dir)) { |
| 123 | LOGERR(LY_ESYS, "Unable to use search directory \"%s\" (%s)", |
| 124 | search_dir, strerror(errno)); |
| 125 | free(cwd); |
| 126 | return; |
| 127 | } |
Michal Vasko | 3eff932 | 2015-11-10 11:02:30 +0100 | [diff] [blame] | 128 | free(ctx->models.search_path); |
Michal Vasko | 60ba9a6 | 2015-07-03 14:42:31 +0200 | [diff] [blame] | 129 | ctx->models.search_path = get_current_dir_name(); |
Michal Vasko | 3eff932 | 2015-11-10 11:02:30 +0100 | [diff] [blame] | 130 | |
Radek Krejci | 15412ca | 2016-03-03 11:16:52 +0100 | [diff] [blame] | 131 | if (chdir(cwd)) { |
| 132 | LOGWRN("Unable to return back to working directory \"%s\" (%s)", |
| 133 | cwd, strerror(errno)); |
| 134 | } |
Michal Vasko | 60ba9a6 | 2015-07-03 14:42:31 +0200 | [diff] [blame] | 135 | free(cwd); |
| 136 | } else { |
| 137 | free(ctx->models.search_path); |
| 138 | ctx->models.search_path = NULL; |
| 139 | } |
| 140 | } |
| 141 | |
Radek Krejci | b081d8d | 2015-10-21 16:29:07 +0200 | [diff] [blame] | 142 | API const char * |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 143 | ly_ctx_get_searchdir(const struct ly_ctx *ctx) |
Radek Krejci | 5a79757 | 2015-10-21 15:45:45 +0200 | [diff] [blame] | 144 | { |
| 145 | return ctx->models.search_path; |
| 146 | } |
| 147 | |
Michal Vasko | 60ba9a6 | 2015-07-03 14:42:31 +0200 | [diff] [blame] | 148 | API void |
Radek Krejci | fa0b5e0 | 2016-02-04 13:57:03 +0100 | [diff] [blame] | 149 | ly_ctx_destroy(struct ly_ctx *ctx, void (*private_destructor)(const struct lys_node *node, void *priv)) |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 150 | { |
Michal Vasko | 627975a | 2016-02-11 11:39:03 +0100 | [diff] [blame] | 151 | int i; |
| 152 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 153 | if (!ctx) { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 154 | return; |
| 155 | } |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 156 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 157 | /* models list */ |
Michal Vasko | 627975a | 2016-02-11 11:39:03 +0100 | [diff] [blame] | 158 | for (i = 0; i < ctx->models.used; ++i) { |
| 159 | lys_free(ctx->models.list[i], private_destructor, 0); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 160 | } |
| 161 | free(ctx->models.search_path); |
| 162 | free(ctx->models.list); |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 163 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 164 | /* dictionary */ |
| 165 | lydict_clean(&ctx->dict); |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 166 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 167 | free(ctx); |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 168 | } |
| 169 | |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 170 | API const struct lys_submodule * |
Radek Krejci | 62f0da7 | 2016-03-07 11:35:43 +0100 | [diff] [blame] | 171 | ly_ctx_get_submodule2(const struct lys_module *main_module, const char *submodule) |
| 172 | { |
| 173 | struct lys_submodule *result; |
| 174 | int i; |
| 175 | |
| 176 | if (!main_module || !submodule) { |
| 177 | ly_errno = LY_EINVAL; |
| 178 | return NULL; |
| 179 | } |
| 180 | |
| 181 | /* search in submodules list */ |
| 182 | for (i = 0; i < main_module->inc_size; i++) { |
| 183 | result = main_module->inc[i].submodule; |
| 184 | if (result && ly_strequal(submodule, result->name, 0)) { |
| 185 | return result; |
| 186 | } |
| 187 | } |
| 188 | |
| 189 | return NULL; |
| 190 | } |
| 191 | |
| 192 | API const struct lys_submodule * |
Radek Krejci | a7533f2 | 2016-03-07 07:37:45 +0100 | [diff] [blame] | 193 | ly_ctx_get_submodule(const struct ly_ctx *ctx, const char *module, const char *revision, const char *submodule) |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 194 | { |
Radek Krejci | e797355 | 2016-03-07 08:12:01 +0100 | [diff] [blame] | 195 | const struct lys_module *mainmod; |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 196 | |
Radek Krejci | a7533f2 | 2016-03-07 07:37:45 +0100 | [diff] [blame] | 197 | if (!module || !submodule) { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 198 | ly_errno = LY_EINVAL; |
| 199 | return NULL; |
| 200 | } |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 201 | |
Radek Krejci | e797355 | 2016-03-07 08:12:01 +0100 | [diff] [blame] | 202 | mainmod = ly_ctx_get_module(ctx, module, revision); |
| 203 | if (!mainmod) { |
Radek Krejci | a7533f2 | 2016-03-07 07:37:45 +0100 | [diff] [blame] | 204 | ly_errno = LY_EINVAL; |
| 205 | return NULL; |
| 206 | } |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 207 | |
Radek Krejci | 62f0da7 | 2016-03-07 11:35:43 +0100 | [diff] [blame] | 208 | return ly_ctx_get_submodule2(mainmod, submodule); |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 209 | } |
| 210 | |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 211 | static const struct lys_module * |
| 212 | ly_ctx_get_module_by(const struct ly_ctx *ctx, const char *key, int offset, const char *revision) |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 213 | { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 214 | int i; |
Radek Krejci | b804869 | 2015-08-05 13:36:34 +0200 | [diff] [blame] | 215 | struct lys_module *result = NULL; |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 216 | |
Radek Krejci | fd4e6e3 | 2015-08-10 15:00:51 +0200 | [diff] [blame] | 217 | if (!ctx || !key) { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 218 | ly_errno = LY_EINVAL; |
| 219 | return NULL; |
| 220 | } |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 221 | |
Radek Krejci | dce5145 | 2015-06-16 15:20:08 +0200 | [diff] [blame] | 222 | for (i = 0; i < ctx->models.used; i++) { |
Radek Krejci | fd4e6e3 | 2015-08-10 15:00:51 +0200 | [diff] [blame] | 223 | /* use offset to get address of the pointer to string (char**), remember that offset is in |
| 224 | * bytes, so we have to cast the pointer to the module to (char*), finally, we want to have |
| 225 | * string not the pointer to string |
| 226 | */ |
| 227 | if (!ctx->models.list[i] || strcmp(key, *(char**)(((char*)ctx->models.list[i]) + offset))) { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 228 | continue; |
| 229 | } |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 230 | |
Radek Krejci | f647e61 | 2015-07-30 11:36:07 +0200 | [diff] [blame] | 231 | if (!revision) { |
| 232 | /* compare revisons and remember the newest one */ |
| 233 | if (result) { |
| 234 | if (!ctx->models.list[i]->rev_size) { |
| 235 | /* the current have no revision, keep the previous with some revision */ |
| 236 | continue; |
| 237 | } |
| 238 | if (result->rev_size && strcmp(ctx->models.list[i]->rev[0].date, result->rev[0].date) < 0) { |
| 239 | /* the previous found matching module has a newer revision */ |
| 240 | continue; |
| 241 | } |
| 242 | } |
| 243 | |
| 244 | /* remember the current match and search for newer version */ |
| 245 | result = ctx->models.list[i]; |
| 246 | } else { |
| 247 | if (ctx->models.list[i]->rev_size && !strcmp(revision, ctx->models.list[i]->rev[0].date)) { |
| 248 | /* matching revision */ |
Michal Vasko | 9758650 | 2015-08-12 14:32:18 +0200 | [diff] [blame] | 249 | result = ctx->models.list[i]; |
| 250 | break; |
Radek Krejci | f647e61 | 2015-07-30 11:36:07 +0200 | [diff] [blame] | 251 | } |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 252 | } |
| 253 | } |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 254 | |
Radek Krejci | f647e61 | 2015-07-30 11:36:07 +0200 | [diff] [blame] | 255 | return result; |
Radek Krejci | fd4e6e3 | 2015-08-10 15:00:51 +0200 | [diff] [blame] | 256 | |
| 257 | } |
| 258 | |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 259 | API const struct lys_module * |
| 260 | ly_ctx_get_module_by_ns(const struct ly_ctx *ctx, const char *ns, const char *revision) |
Radek Krejci | fd4e6e3 | 2015-08-10 15:00:51 +0200 | [diff] [blame] | 261 | { |
| 262 | return ly_ctx_get_module_by(ctx, ns, offsetof(struct lys_module, ns), revision); |
| 263 | } |
| 264 | |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 265 | API const struct lys_module * |
| 266 | ly_ctx_get_module(const struct ly_ctx *ctx, const char *name, const char *revision) |
Radek Krejci | fd4e6e3 | 2015-08-10 15:00:51 +0200 | [diff] [blame] | 267 | { |
| 268 | return ly_ctx_get_module_by(ctx, name, offsetof(struct lys_module, name), revision); |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 269 | } |
Michal Vasko | 3ec07dc | 2015-06-30 15:51:30 +0200 | [diff] [blame] | 270 | |
Radek Krejci | 21601a3 | 2016-03-07 11:39:27 +0100 | [diff] [blame^] | 271 | API const struct lys_module * |
| 272 | ly_ctx_get_module_older(const struct ly_ctx *ctx, const struct lys_module *module) |
| 273 | { |
| 274 | int i; |
| 275 | const struct lys_module *result = NULL, *iter; |
| 276 | |
| 277 | if (!ctx || !module || !module->rev_size) { |
| 278 | ly_errno = LY_EINVAL; |
| 279 | return NULL; |
| 280 | } |
| 281 | |
| 282 | |
| 283 | for (i = 0; i < ctx->models.used; i++) { |
| 284 | iter = ctx->models.list[i]; |
| 285 | if (iter == module || !iter->rev_size) { |
| 286 | /* iter is the module itself or iter has no revision */ |
| 287 | continue; |
| 288 | } |
| 289 | if (!ly_strequal(module->name, iter->name, 0)) { |
| 290 | /* different module */ |
| 291 | continue; |
| 292 | } |
| 293 | if (strcmp(iter->rev[0].date, module->rev[0].date) < 0) { |
| 294 | /* iter is older than module */ |
| 295 | if (result) { |
| 296 | if (strcmp(iter->rev[0].date, result->rev[0].date) > 0) { |
| 297 | /* iter is newer than current result */ |
| 298 | result = iter; |
| 299 | } |
| 300 | } else { |
| 301 | result = iter; |
| 302 | } |
| 303 | } |
| 304 | } |
| 305 | |
| 306 | return result; |
| 307 | } |
| 308 | |
Michal Vasko | 99b0aad | 2015-12-01 12:28:51 +0100 | [diff] [blame] | 309 | API void |
| 310 | ly_ctx_set_module_clb(struct ly_ctx *ctx, ly_module_clb clb, void *user_data) |
Michal Vasko | 8246596 | 2015-11-10 11:03:11 +0100 | [diff] [blame] | 311 | { |
Michal Vasko | 99b0aad | 2015-12-01 12:28:51 +0100 | [diff] [blame] | 312 | ctx->module_clb = clb; |
| 313 | ctx->module_clb_data = user_data; |
| 314 | } |
| 315 | |
| 316 | API ly_module_clb |
| 317 | ly_ctx_get_module_clb(const struct ly_ctx *ctx, void **user_data) |
| 318 | { |
| 319 | if (user_data) { |
| 320 | *user_data = ctx->module_clb_data; |
| 321 | } |
| 322 | return ctx->module_clb; |
| 323 | } |
| 324 | |
| 325 | API const struct lys_module * |
| 326 | ly_ctx_load_module(struct ly_ctx *ctx, const char *name, const char *revision) |
| 327 | { |
Michal Vasko | 5a721fd | 2016-02-16 12:16:48 +0100 | [diff] [blame] | 328 | const struct lys_module *module; |
Michal Vasko | 99b0aad | 2015-12-01 12:28:51 +0100 | [diff] [blame] | 329 | char *module_data; |
Michal Vasko | d3e975b | 2016-03-03 15:40:21 +0100 | [diff] [blame] | 330 | void (*module_data_free)(void *module_data) = NULL; |
Michal Vasko | 99b0aad | 2015-12-01 12:28:51 +0100 | [diff] [blame] | 331 | LYS_INFORMAT format = LYS_IN_UNKNOWN; |
Michal Vasko | 8246596 | 2015-11-10 11:03:11 +0100 | [diff] [blame] | 332 | |
| 333 | if (!ctx || !name) { |
| 334 | ly_errno = LY_EINVAL; |
| 335 | return NULL; |
| 336 | } |
| 337 | |
Michal Vasko | 99b0aad | 2015-12-01 12:28:51 +0100 | [diff] [blame] | 338 | if (ctx->module_clb) { |
| 339 | module_data = ctx->module_clb(name, revision, ctx->module_clb_data, &format, &module_data_free); |
| 340 | if (!module_data) { |
| 341 | LOGERR(LY_EVALID, "User module retrieval callback failed!"); |
| 342 | return NULL; |
Michal Vasko | 8246596 | 2015-11-10 11:03:11 +0100 | [diff] [blame] | 343 | } |
Michal Vasko | 5a721fd | 2016-02-16 12:16:48 +0100 | [diff] [blame] | 344 | module = lys_parse_mem(ctx, module_data, format); |
Michal Vasko | 99b0aad | 2015-12-01 12:28:51 +0100 | [diff] [blame] | 345 | if (module_data_free) { |
| 346 | module_data_free(module_data); |
Michal Vasko | 8246596 | 2015-11-10 11:03:11 +0100 | [diff] [blame] | 347 | } |
Michal Vasko | 99b0aad | 2015-12-01 12:28:51 +0100 | [diff] [blame] | 348 | } else { |
Michal Vasko | 5a721fd | 2016-02-16 12:16:48 +0100 | [diff] [blame] | 349 | module = lyp_search_file(ctx, NULL, name, revision, NULL); |
Michal Vasko | 8246596 | 2015-11-10 11:03:11 +0100 | [diff] [blame] | 350 | } |
| 351 | |
Michal Vasko | 5a721fd | 2016-02-16 12:16:48 +0100 | [diff] [blame] | 352 | return module; |
Michal Vasko | 8246596 | 2015-11-10 11:03:11 +0100 | [diff] [blame] | 353 | } |
| 354 | |
Radek Krejci | 96a10da | 2015-07-30 11:00:14 +0200 | [diff] [blame] | 355 | API const char ** |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 356 | ly_ctx_get_module_names(const struct ly_ctx *ctx) |
Michal Vasko | 3ec07dc | 2015-06-30 15:51:30 +0200 | [diff] [blame] | 357 | { |
Radek Krejci | c215afb | 2016-03-07 11:32:55 +0100 | [diff] [blame] | 358 | int i, j, k; |
Radek Krejci | 96a10da | 2015-07-30 11:00:14 +0200 | [diff] [blame] | 359 | const char **result = NULL; |
Michal Vasko | 3ec07dc | 2015-06-30 15:51:30 +0200 | [diff] [blame] | 360 | |
| 361 | if (!ctx) { |
| 362 | ly_errno = LY_EINVAL; |
| 363 | return NULL; |
| 364 | } |
| 365 | |
Michal Vasko | e2ea44b | 2015-07-07 11:31:48 +0200 | [diff] [blame] | 366 | result = malloc((ctx->models.used+1) * sizeof *result); |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 367 | if (!result) { |
| 368 | LOGMEM; |
| 369 | return NULL; |
| 370 | } |
Michal Vasko | e2ea44b | 2015-07-07 11:31:48 +0200 | [diff] [blame] | 371 | |
Radek Krejci | c215afb | 2016-03-07 11:32:55 +0100 | [diff] [blame] | 372 | for (i = j = 0; i < ctx->models.used; i++) { |
| 373 | /* avoid duplicities when multiple revisions of the same module are present */ |
| 374 | for (k = j - 1; k >= 0; k--) { |
| 375 | if (ly_strequal(result[k], ctx->models.list[i]->name, 1)) { |
| 376 | break; |
| 377 | } |
| 378 | } |
| 379 | if (k < 0) { |
| 380 | /* no duplication found */ |
| 381 | result[j] = ctx->models.list[i]->name; |
| 382 | j++; |
| 383 | } |
Michal Vasko | 3ec07dc | 2015-06-30 15:51:30 +0200 | [diff] [blame] | 384 | } |
Radek Krejci | c215afb | 2016-03-07 11:32:55 +0100 | [diff] [blame] | 385 | result[j] = NULL; |
Michal Vasko | 3ec07dc | 2015-06-30 15:51:30 +0200 | [diff] [blame] | 386 | |
| 387 | return result; |
Michal Vasko | fa8c828 | 2015-07-03 15:14:59 +0200 | [diff] [blame] | 388 | } |
| 389 | |
Radek Krejci | 96a10da | 2015-07-30 11:00:14 +0200 | [diff] [blame] | 390 | API const char ** |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 391 | ly_ctx_get_submodule_names(const struct ly_ctx *ctx, const char *module_name) |
Michal Vasko | fa8c828 | 2015-07-03 15:14:59 +0200 | [diff] [blame] | 392 | { |
| 393 | int i; |
Radek Krejci | 96a10da | 2015-07-30 11:00:14 +0200 | [diff] [blame] | 394 | const char **result = NULL; |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 395 | const struct lys_module *mod; |
Michal Vasko | fa8c828 | 2015-07-03 15:14:59 +0200 | [diff] [blame] | 396 | |
| 397 | if (!ctx) { |
| 398 | ly_errno = LY_EINVAL; |
| 399 | return NULL; |
| 400 | } |
| 401 | |
Radek Krejci | 96a10da | 2015-07-30 11:00:14 +0200 | [diff] [blame] | 402 | mod = ly_ctx_get_module(ctx, module_name, NULL); |
Michal Vasko | fa8c828 | 2015-07-03 15:14:59 +0200 | [diff] [blame] | 403 | if (!mod) { |
Radek Krejci | 96a10da | 2015-07-30 11:00:14 +0200 | [diff] [blame] | 404 | LOGERR(LY_EVALID, "Data model \"%s\" not loaded", module_name); |
Michal Vasko | fa8c828 | 2015-07-03 15:14:59 +0200 | [diff] [blame] | 405 | return NULL; |
| 406 | } |
| 407 | |
| 408 | result = malloc((mod->inc_size+1) * sizeof *result); |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 409 | if (!result) { |
| 410 | LOGMEM; |
| 411 | return NULL; |
| 412 | } |
Michal Vasko | fa8c828 | 2015-07-03 15:14:59 +0200 | [diff] [blame] | 413 | |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 414 | for (i = 0; i < mod->inc_size && mod->inc[i].submodule; i++) { |
Radek Krejci | 96a10da | 2015-07-30 11:00:14 +0200 | [diff] [blame] | 415 | result[i] = mod->inc[i].submodule->name; |
Michal Vasko | fa8c828 | 2015-07-03 15:14:59 +0200 | [diff] [blame] | 416 | } |
| 417 | result[i] = NULL; |
| 418 | |
| 419 | return result; |
| 420 | } |
Michal Vasko | 209a622 | 2015-10-16 09:51:07 +0200 | [diff] [blame] | 421 | |
| 422 | static int |
| 423 | ylib_feature(struct lyd_node *parent, struct lys_module *cur_mod) |
| 424 | { |
| 425 | int i, j; |
| 426 | |
| 427 | /* module features */ |
| 428 | for (i = 0; i < cur_mod->features_size; ++i) { |
| 429 | if (!(cur_mod->features[i].flags & LYS_FENABLED)) { |
| 430 | continue; |
| 431 | } |
| 432 | |
Michal Vasko | 3e671b5 | 2015-10-23 16:23:15 +0200 | [diff] [blame] | 433 | if (!lyd_new_leaf(parent, NULL, "feature", cur_mod->features[i].name)) { |
Michal Vasko | 209a622 | 2015-10-16 09:51:07 +0200 | [diff] [blame] | 434 | return EXIT_FAILURE; |
| 435 | } |
| 436 | } |
| 437 | |
| 438 | /* submodule features */ |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 439 | for (i = 0; i < cur_mod->inc_size && cur_mod->inc[i].submodule; ++i) { |
Michal Vasko | 209a622 | 2015-10-16 09:51:07 +0200 | [diff] [blame] | 440 | for (j = 0; j < cur_mod->inc[i].submodule->features_size; ++j) { |
| 441 | if (!(cur_mod->inc[i].submodule->features[j].flags & LYS_FENABLED)) { |
| 442 | continue; |
| 443 | } |
| 444 | |
Michal Vasko | 3e671b5 | 2015-10-23 16:23:15 +0200 | [diff] [blame] | 445 | if (!lyd_new_leaf(parent, NULL, "feature", cur_mod->inc[i].submodule->features[j].name)) { |
Michal Vasko | 209a622 | 2015-10-16 09:51:07 +0200 | [diff] [blame] | 446 | return EXIT_FAILURE; |
| 447 | } |
| 448 | } |
| 449 | } |
| 450 | |
| 451 | return EXIT_SUCCESS; |
| 452 | } |
| 453 | |
| 454 | static int |
Michal Vasko | ff006c1 | 2016-02-17 11:15:19 +0100 | [diff] [blame] | 455 | ylib_deviation(struct lyd_node *parent, struct lys_module *cur_mod) |
Michal Vasko | 97d8c6d | 2016-02-12 11:05:48 +0100 | [diff] [blame] | 456 | { |
Michal Vasko | ff006c1 | 2016-02-17 11:15:19 +0100 | [diff] [blame] | 457 | int i; |
Michal Vasko | 97d8c6d | 2016-02-12 11:05:48 +0100 | [diff] [blame] | 458 | const char *revision; |
Michal Vasko | 209a622 | 2015-10-16 09:51:07 +0200 | [diff] [blame] | 459 | struct lyd_node *cont; |
| 460 | |
Michal Vasko | ff006c1 | 2016-02-17 11:15:19 +0100 | [diff] [blame] | 461 | for (i = 0; i < cur_mod->imp_size; ++i) { |
| 462 | /* marks a deviating module */ |
| 463 | if (cur_mod->imp[i].external == 2) { |
| 464 | revision = (cur_mod->imp[i].module->rev_size ? cur_mod->imp[i].module->rev[0].date : ""); |
Michal Vasko | 209a622 | 2015-10-16 09:51:07 +0200 | [diff] [blame] | 465 | |
Michal Vasko | ff006c1 | 2016-02-17 11:15:19 +0100 | [diff] [blame] | 466 | cont = lyd_new(parent, NULL, "deviation"); |
| 467 | if (!cont) { |
| 468 | return EXIT_FAILURE; |
Michal Vasko | 209a622 | 2015-10-16 09:51:07 +0200 | [diff] [blame] | 469 | } |
Michal Vasko | 209a622 | 2015-10-16 09:51:07 +0200 | [diff] [blame] | 470 | |
Michal Vasko | ff006c1 | 2016-02-17 11:15:19 +0100 | [diff] [blame] | 471 | if (!lyd_new_leaf(cont, NULL, "name", cur_mod->imp[i].module->name)) { |
| 472 | return EXIT_FAILURE; |
| 473 | } |
| 474 | if (!lyd_new_leaf(cont, NULL, "revision", revision)) { |
| 475 | return EXIT_FAILURE; |
Michal Vasko | 209a622 | 2015-10-16 09:51:07 +0200 | [diff] [blame] | 476 | } |
| 477 | } |
| 478 | } |
| 479 | |
| 480 | return EXIT_SUCCESS; |
| 481 | } |
| 482 | |
| 483 | static int |
| 484 | ylib_submodules(struct lyd_node *parent, struct lys_module *cur_mod) |
| 485 | { |
| 486 | int i; |
Radek Krejci | a77904e | 2016-02-25 16:23:45 +0100 | [diff] [blame] | 487 | char *str; |
Radek Krejci | 6e05cea | 2015-12-10 16:34:37 +0100 | [diff] [blame] | 488 | struct lyd_node *cont, *item; |
| 489 | |
| 490 | if (cur_mod->inc_size) { |
| 491 | cont = lyd_new(parent, NULL, "submodules"); |
| 492 | } |
Michal Vasko | 209a622 | 2015-10-16 09:51:07 +0200 | [diff] [blame] | 493 | |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 494 | for (i = 0; i < cur_mod->inc_size && cur_mod->inc[i].submodule; ++i) { |
Radek Krejci | 6e05cea | 2015-12-10 16:34:37 +0100 | [diff] [blame] | 495 | item = lyd_new(cont, NULL, "submodule"); |
| 496 | if (!item) { |
Michal Vasko | 209a622 | 2015-10-16 09:51:07 +0200 | [diff] [blame] | 497 | return EXIT_FAILURE; |
| 498 | } |
| 499 | |
Radek Krejci | 6e05cea | 2015-12-10 16:34:37 +0100 | [diff] [blame] | 500 | if (!lyd_new_leaf(item, NULL, "name", cur_mod->inc[i].submodule->name)) { |
Michal Vasko | 209a622 | 2015-10-16 09:51:07 +0200 | [diff] [blame] | 501 | return EXIT_FAILURE; |
| 502 | } |
Radek Krejci | 6e05cea | 2015-12-10 16:34:37 +0100 | [diff] [blame] | 503 | if (!lyd_new_leaf(item, NULL, "revision", (cur_mod->inc[i].submodule->rev_size ? |
Michal Vasko | 3e671b5 | 2015-10-23 16:23:15 +0200 | [diff] [blame] | 504 | cur_mod->inc[i].submodule->rev[0].date : ""))) { |
Michal Vasko | 209a622 | 2015-10-16 09:51:07 +0200 | [diff] [blame] | 505 | return EXIT_FAILURE; |
| 506 | } |
Radek Krejci | a77904e | 2016-02-25 16:23:45 +0100 | [diff] [blame] | 507 | if (cur_mod->inc[i].submodule->filepath) { |
Radek Krejci | 95aa201 | 2016-03-03 11:21:09 +0100 | [diff] [blame] | 508 | if (asprintf(&str, "file://%s", cur_mod->inc[i].submodule->filepath) == -1) { |
Radek Krejci | a77904e | 2016-02-25 16:23:45 +0100 | [diff] [blame] | 509 | LOGMEM; |
| 510 | return EXIT_FAILURE; |
| 511 | } else if (!lyd_new_leaf(cont, NULL, "schema", str)) { |
Radek Krejci | 19f9ede | 2016-02-25 16:29:21 +0100 | [diff] [blame] | 512 | free(str); |
Radek Krejci | a77904e | 2016-02-25 16:23:45 +0100 | [diff] [blame] | 513 | return EXIT_FAILURE; |
| 514 | } |
Radek Krejci | 19f9ede | 2016-02-25 16:29:21 +0100 | [diff] [blame] | 515 | free(str); |
Michal Vasko | 209a622 | 2015-10-16 09:51:07 +0200 | [diff] [blame] | 516 | } |
| 517 | } |
| 518 | |
| 519 | return EXIT_SUCCESS; |
| 520 | } |
| 521 | |
| 522 | API struct lyd_node * |
| 523 | ly_ctx_info(struct ly_ctx *ctx) |
| 524 | { |
| 525 | int i; |
| 526 | char id[8]; |
Radek Krejci | a77904e | 2016-02-25 16:23:45 +0100 | [diff] [blame] | 527 | char *str; |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 528 | const struct lys_module *mod; |
Michal Vasko | 209a622 | 2015-10-16 09:51:07 +0200 | [diff] [blame] | 529 | struct lyd_node *root, *cont; |
| 530 | |
Radek Krejci | bd9e8d2 | 2016-02-03 14:11:48 +0100 | [diff] [blame] | 531 | mod = ly_ctx_get_module(ctx, "ietf-yang-library", IETF_YANG_LIB_REV); |
| 532 | if (!mod || !mod->data) { |
| 533 | LOGINT; |
Michal Vasko | 209a622 | 2015-10-16 09:51:07 +0200 | [diff] [blame] | 534 | return NULL; |
| 535 | } |
| 536 | |
Radek Krejci | bd9e8d2 | 2016-02-03 14:11:48 +0100 | [diff] [blame] | 537 | root = lyd_new(NULL, mod, "modules-state"); |
Michal Vasko | 209a622 | 2015-10-16 09:51:07 +0200 | [diff] [blame] | 538 | if (!root) { |
| 539 | return NULL; |
| 540 | } |
| 541 | |
| 542 | for (i = 0; i < ctx->models.used; ++i) { |
| 543 | cont = lyd_new(root, NULL, "module"); |
| 544 | if (!cont) { |
| 545 | lyd_free(root); |
| 546 | return NULL; |
| 547 | } |
| 548 | |
Michal Vasko | 3e671b5 | 2015-10-23 16:23:15 +0200 | [diff] [blame] | 549 | if (!lyd_new_leaf(cont, NULL, "name", ctx->models.list[i]->name)) { |
Michal Vasko | 209a622 | 2015-10-16 09:51:07 +0200 | [diff] [blame] | 550 | lyd_free(root); |
| 551 | return NULL; |
| 552 | } |
Michal Vasko | 3e671b5 | 2015-10-23 16:23:15 +0200 | [diff] [blame] | 553 | if (!lyd_new_leaf(cont, NULL, "revision", (ctx->models.list[i]->rev_size ? |
Michal Vasko | 209a622 | 2015-10-16 09:51:07 +0200 | [diff] [blame] | 554 | ctx->models.list[i]->rev[0].date : ""))) { |
| 555 | lyd_free(root); |
| 556 | return NULL; |
| 557 | } |
Radek Krejci | a77904e | 2016-02-25 16:23:45 +0100 | [diff] [blame] | 558 | if (ctx->models.list[i]->filepath) { |
Radek Krejci | 15412ca | 2016-03-03 11:16:52 +0100 | [diff] [blame] | 559 | if (asprintf(&str, "file://%s", ctx->models.list[i]->filepath) == -1) { |
Radek Krejci | a77904e | 2016-02-25 16:23:45 +0100 | [diff] [blame] | 560 | LOGMEM; |
| 561 | lyd_free(root); |
| 562 | return NULL; |
| 563 | } else if (!lyd_new_leaf(cont, NULL, "schema", str)) { |
Radek Krejci | 19f9ede | 2016-02-25 16:29:21 +0100 | [diff] [blame] | 564 | free(str); |
Radek Krejci | a77904e | 2016-02-25 16:23:45 +0100 | [diff] [blame] | 565 | lyd_free(root); |
| 566 | return NULL; |
| 567 | } |
Radek Krejci | 19f9ede | 2016-02-25 16:29:21 +0100 | [diff] [blame] | 568 | free(str); |
Michal Vasko | 209a622 | 2015-10-16 09:51:07 +0200 | [diff] [blame] | 569 | } |
Michal Vasko | 3e671b5 | 2015-10-23 16:23:15 +0200 | [diff] [blame] | 570 | if (!lyd_new_leaf(cont, NULL, "namespace", ctx->models.list[i]->ns)) { |
Michal Vasko | 209a622 | 2015-10-16 09:51:07 +0200 | [diff] [blame] | 571 | lyd_free(root); |
| 572 | return NULL; |
| 573 | } |
| 574 | if (ylib_feature(cont, ctx->models.list[i])) { |
| 575 | lyd_free(root); |
| 576 | return NULL; |
| 577 | } |
Michal Vasko | ff006c1 | 2016-02-17 11:15:19 +0100 | [diff] [blame] | 578 | if (ylib_deviation(cont, ctx->models.list[i])) { |
Michal Vasko | 209a622 | 2015-10-16 09:51:07 +0200 | [diff] [blame] | 579 | lyd_free(root); |
| 580 | return NULL; |
| 581 | } |
| 582 | if (ctx->models.list[i]->implemented |
Radek Krejci | bd9e8d2 | 2016-02-03 14:11:48 +0100 | [diff] [blame] | 583 | && !lyd_new_leaf(cont, NULL, "conformance-type", "implement")) { |
Michal Vasko | 209a622 | 2015-10-16 09:51:07 +0200 | [diff] [blame] | 584 | lyd_free(root); |
| 585 | return NULL; |
| 586 | } |
| 587 | if (!ctx->models.list[i]->implemented |
Radek Krejci | bd9e8d2 | 2016-02-03 14:11:48 +0100 | [diff] [blame] | 588 | && !lyd_new_leaf(cont, NULL, "conformance-type", "import")) { |
Michal Vasko | 209a622 | 2015-10-16 09:51:07 +0200 | [diff] [blame] | 589 | lyd_free(root); |
| 590 | return NULL; |
| 591 | } |
| 592 | if (ylib_submodules(cont, ctx->models.list[i])) { |
| 593 | lyd_free(root); |
| 594 | return NULL; |
| 595 | } |
| 596 | } |
| 597 | |
| 598 | sprintf(id, "%u", ctx->models.module_set_id); |
Michal Vasko | 3e671b5 | 2015-10-23 16:23:15 +0200 | [diff] [blame] | 599 | if (!lyd_new_leaf(root, mod, "module-set-id", id)) { |
Michal Vasko | 209a622 | 2015-10-16 09:51:07 +0200 | [diff] [blame] | 600 | lyd_free(root); |
| 601 | return NULL; |
| 602 | } |
| 603 | |
| 604 | if (lyd_validate(root, 0)) { |
| 605 | lyd_free(root); |
| 606 | return NULL; |
| 607 | } |
| 608 | |
| 609 | return root; |
| 610 | } |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 611 | |
| 612 | API const struct lys_node * |
| 613 | ly_ctx_get_node(struct ly_ctx *ctx, const char *nodeid) |
| 614 | { |
| 615 | const struct lys_node *ret; |
| 616 | |
| 617 | if (!ctx || !nodeid) { |
| 618 | ly_errno = LY_EINVAL; |
| 619 | return NULL; |
| 620 | } |
| 621 | |
| 622 | if (resolve_json_absolute_schema_nodeid(nodeid, ctx, &ret)) { |
| 623 | ly_errno = LY_EINVAL; |
| 624 | return NULL; |
| 625 | } |
| 626 | |
| 627 | return ret; |
| 628 | } |