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 | * |
| 8 | * Redistribution and use in source and binary forms, with or without |
| 9 | * modification, are permitted provided that the following conditions |
| 10 | * are met: |
| 11 | * 1. Redistributions of source code must retain the above copyright |
| 12 | * notice, this list of conditions and the following disclaimer. |
| 13 | * 2. Redistributions in binary form must reproduce the above copyright |
| 14 | * notice, this list of conditions and the following disclaimer in |
| 15 | * the documentation and/or other materials provided with the |
| 16 | * distribution. |
| 17 | * 3. Neither the name of the Company nor the names of its contributors |
| 18 | * may be used to endorse or promote products derived from this |
| 19 | * software without specific prior written permission. |
| 20 | */ |
| 21 | |
| 22 | #define _GNU_SOURCE |
Radek Krejci | fd4e6e3 | 2015-08-10 15:00:51 +0200 | [diff] [blame] | 23 | #include <stddef.h> |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 24 | #include <stdlib.h> |
| 25 | #include <string.h> |
| 26 | #include <sys/types.h> |
| 27 | #include <sys/stat.h> |
| 28 | #include <unistd.h> |
| 29 | #include <errno.h> |
| 30 | #include <fcntl.h> |
| 31 | |
| 32 | #include "common.h" |
| 33 | #include "context.h" |
Radek Krejci | 41912fe | 2015-10-22 10:22:12 +0200 | [diff] [blame] | 34 | #include "dict_private.h" |
Michal Vasko | 209a622 | 2015-10-16 09:51:07 +0200 | [diff] [blame] | 35 | #include "parser.h" |
Radek Krejci | bc9cf93 | 2015-07-30 11:09:39 +0200 | [diff] [blame] | 36 | #include "tree_internal.h" |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 37 | |
Michal Vasko | 8d054e4 | 2015-08-03 12:42:06 +0200 | [diff] [blame] | 38 | #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] | 39 | #define IETF_YANG_TYPES_PATH "../models/ietf-yang-types@2013-07-15.h" |
Michal Vasko | 8d054e4 | 2015-08-03 12:42:06 +0200 | [diff] [blame] | 40 | #define IETF_YANG_LIB_PATH "../models/ietf-yang-library@2015-07-03.h" |
| 41 | |
Michal Vasko | 8d054e4 | 2015-08-03 12:42:06 +0200 | [diff] [blame] | 42 | #include IETF_INET_TYPES_PATH |
Michal Vasko | 21181c4 | 2015-08-03 13:46:45 +0200 | [diff] [blame] | 43 | #include IETF_YANG_TYPES_PATH |
Michal Vasko | 8d054e4 | 2015-08-03 12:42:06 +0200 | [diff] [blame] | 44 | #include IETF_YANG_LIB_PATH |
| 45 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 46 | API struct ly_ctx * |
| 47 | ly_ctx_new(const char *search_dir) |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 48 | { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 49 | struct ly_ctx *ctx; |
Michal Vasko | 70b6d69 | 2015-08-03 14:05:59 +0200 | [diff] [blame] | 50 | char *cwd; |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 51 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 52 | ctx = calloc(1, sizeof *ctx); |
| 53 | if (!ctx) { |
| 54 | LOGMEM; |
| 55 | return NULL; |
| 56 | } |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 57 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 58 | /* dictionary */ |
| 59 | lydict_init(&ctx->dict); |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 60 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 61 | /* models list */ |
| 62 | ctx->models.list = calloc(16, sizeof *ctx->models.list); |
| 63 | ctx->models.used = 0; |
| 64 | ctx->models.size = 16; |
| 65 | if (search_dir) { |
| 66 | cwd = get_current_dir_name(); |
| 67 | if (chdir(search_dir)) { |
| 68 | LOGERR(LY_ESYS, "Unable to use search directory \"%s\" (%s)", |
| 69 | search_dir, strerror(errno)); |
| 70 | free(cwd); |
| 71 | ly_ctx_destroy(ctx); |
| 72 | return NULL; |
| 73 | } |
| 74 | ctx->models.search_path = get_current_dir_name(); |
| 75 | chdir(cwd); |
| 76 | free(cwd); |
| 77 | } |
Michal Vasko | 14719b2 | 2015-08-03 12:47:55 +0200 | [diff] [blame] | 78 | ctx->models.module_set_id = 1; |
| 79 | |
Michal Vasko | 8d054e4 | 2015-08-03 12:42:06 +0200 | [diff] [blame] | 80 | /* load ietf-inet-types */ |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame^] | 81 | ctx->models.list[0] = (struct lys_module *)lys_parse(ctx, (char *)ietf_inet_types_2013_07_15_yin, LYS_IN_YIN); |
Michal Vasko | 8d054e4 | 2015-08-03 12:42:06 +0200 | [diff] [blame] | 82 | if (!ctx->models.list[0]) { |
| 83 | ly_ctx_destroy(ctx); |
| 84 | return NULL; |
| 85 | } |
| 86 | |
| 87 | /* load ietf-yang-types */ |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame^] | 88 | ctx->models.list[1] = (struct lys_module *)lys_parse(ctx, (char *)ietf_yang_types_2013_07_15_yin, LYS_IN_YIN); |
Michal Vasko | 8d054e4 | 2015-08-03 12:42:06 +0200 | [diff] [blame] | 89 | if (!ctx->models.list[1]) { |
| 90 | ly_ctx_destroy(ctx); |
| 91 | return NULL; |
| 92 | } |
| 93 | |
| 94 | /* load ietf-yang-library */ |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame^] | 95 | ctx->models.list[2] = (struct lys_module *)lys_parse(ctx, (char *)ietf_yang_library_2015_07_03_yin, LYS_IN_YIN); |
Michal Vasko | 8d054e4 | 2015-08-03 12:42:06 +0200 | [diff] [blame] | 96 | if (!ctx->models.list[2]) { |
| 97 | ly_ctx_destroy(ctx); |
| 98 | return NULL; |
| 99 | } |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 100 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 101 | return ctx; |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 102 | } |
| 103 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 104 | API void |
Michal Vasko | 60ba9a6 | 2015-07-03 14:42:31 +0200 | [diff] [blame] | 105 | ly_ctx_set_searchdir(struct ly_ctx *ctx, const char *search_dir) |
| 106 | { |
| 107 | char *cwd; |
| 108 | |
| 109 | if (!ctx) { |
| 110 | return; |
| 111 | } |
| 112 | |
| 113 | if (search_dir) { |
| 114 | cwd = get_current_dir_name(); |
| 115 | if (chdir(search_dir)) { |
| 116 | LOGERR(LY_ESYS, "Unable to use search directory \"%s\" (%s)", |
| 117 | search_dir, strerror(errno)); |
| 118 | free(cwd); |
| 119 | return; |
| 120 | } |
Michal Vasko | 3eff932 | 2015-11-10 11:02:30 +0100 | [diff] [blame] | 121 | free(ctx->models.search_path); |
Michal Vasko | 60ba9a6 | 2015-07-03 14:42:31 +0200 | [diff] [blame] | 122 | ctx->models.search_path = get_current_dir_name(); |
Michal Vasko | 3eff932 | 2015-11-10 11:02:30 +0100 | [diff] [blame] | 123 | |
Michal Vasko | 60ba9a6 | 2015-07-03 14:42:31 +0200 | [diff] [blame] | 124 | chdir(cwd); |
| 125 | free(cwd); |
| 126 | } else { |
| 127 | free(ctx->models.search_path); |
| 128 | ctx->models.search_path = NULL; |
| 129 | } |
| 130 | } |
| 131 | |
Radek Krejci | b081d8d | 2015-10-21 16:29:07 +0200 | [diff] [blame] | 132 | API const char * |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame^] | 133 | ly_ctx_get_searchdir(const struct ly_ctx *ctx) |
Radek Krejci | 5a79757 | 2015-10-21 15:45:45 +0200 | [diff] [blame] | 134 | { |
| 135 | return ctx->models.search_path; |
| 136 | } |
| 137 | |
Michal Vasko | 60ba9a6 | 2015-07-03 14:42:31 +0200 | [diff] [blame] | 138 | API void |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 139 | ly_ctx_destroy(struct ly_ctx *ctx) |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 140 | { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 141 | if (!ctx) { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 142 | return; |
| 143 | } |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 144 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 145 | /* models list */ |
Radek Krejci | dce5145 | 2015-06-16 15:20:08 +0200 | [diff] [blame] | 146 | while (ctx->models.used) { |
Michal Vasko | 13b1583 | 2015-08-19 11:04:48 +0200 | [diff] [blame] | 147 | lys_free(ctx->models.list[0], 1); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 148 | } |
| 149 | free(ctx->models.search_path); |
| 150 | free(ctx->models.list); |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 151 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 152 | /* dictionary */ |
| 153 | lydict_clean(&ctx->dict); |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 154 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 155 | free(ctx); |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 156 | } |
| 157 | |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame^] | 158 | API const struct lys_submodule * |
| 159 | ly_ctx_get_submodule(const struct lys_module *module, const char *name, const char *revision) |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 160 | { |
Radek Krejci | b804869 | 2015-08-05 13:36:34 +0200 | [diff] [blame] | 161 | struct lys_submodule *result; |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 162 | int i; |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 163 | |
Radek Krejci | 63a91a9 | 2015-07-29 13:31:04 +0200 | [diff] [blame] | 164 | if (!module || !name) { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 165 | ly_errno = LY_EINVAL; |
| 166 | return NULL; |
| 167 | } |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 168 | |
Radek Krejci | e98bb4b | 2015-07-30 14:21:41 +0200 | [diff] [blame] | 169 | /* TODO search also for submodules not directly available from the main module */ |
| 170 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 171 | /* search in modules included by the main module */ |
| 172 | if (module->type) { |
Radek Krejci | b804869 | 2015-08-05 13:36:34 +0200 | [diff] [blame] | 173 | module = ((struct lys_submodule *)module)->belongsto; |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 174 | } |
| 175 | for (i = 0; i < module->inc_size; i++) { |
| 176 | result = module->inc[i].submodule; |
| 177 | if (strcmp(name, result->name)) { |
| 178 | continue; |
| 179 | } |
Radek Krejci | ce7fb78 | 2015-05-29 16:52:34 +0200 | [diff] [blame] | 180 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 181 | if (!revision || (result->rev_size && !strcmp(revision, result->rev[0].date))) { |
| 182 | return result; |
| 183 | } |
| 184 | } |
Radek Krejci | ce7fb78 | 2015-05-29 16:52:34 +0200 | [diff] [blame] | 185 | |
Radek Krejci | 63a91a9 | 2015-07-29 13:31:04 +0200 | [diff] [blame] | 186 | return NULL; |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 187 | } |
| 188 | |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame^] | 189 | static const struct lys_module * |
| 190 | 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] | 191 | { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 192 | int i; |
Radek Krejci | b804869 | 2015-08-05 13:36:34 +0200 | [diff] [blame] | 193 | struct lys_module *result = NULL; |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 194 | |
Radek Krejci | fd4e6e3 | 2015-08-10 15:00:51 +0200 | [diff] [blame] | 195 | if (!ctx || !key) { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 196 | ly_errno = LY_EINVAL; |
| 197 | return NULL; |
| 198 | } |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 199 | |
Radek Krejci | dce5145 | 2015-06-16 15:20:08 +0200 | [diff] [blame] | 200 | for (i = 0; i < ctx->models.used; i++) { |
Radek Krejci | fd4e6e3 | 2015-08-10 15:00:51 +0200 | [diff] [blame] | 201 | /* use offset to get address of the pointer to string (char**), remember that offset is in |
| 202 | * bytes, so we have to cast the pointer to the module to (char*), finally, we want to have |
| 203 | * string not the pointer to string |
| 204 | */ |
| 205 | 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] | 206 | continue; |
| 207 | } |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 208 | |
Radek Krejci | f647e61 | 2015-07-30 11:36:07 +0200 | [diff] [blame] | 209 | if (!revision) { |
| 210 | /* compare revisons and remember the newest one */ |
| 211 | if (result) { |
| 212 | if (!ctx->models.list[i]->rev_size) { |
| 213 | /* the current have no revision, keep the previous with some revision */ |
| 214 | continue; |
| 215 | } |
| 216 | if (result->rev_size && strcmp(ctx->models.list[i]->rev[0].date, result->rev[0].date) < 0) { |
| 217 | /* the previous found matching module has a newer revision */ |
| 218 | continue; |
| 219 | } |
| 220 | } |
| 221 | |
| 222 | /* remember the current match and search for newer version */ |
| 223 | result = ctx->models.list[i]; |
| 224 | } else { |
| 225 | if (ctx->models.list[i]->rev_size && !strcmp(revision, ctx->models.list[i]->rev[0].date)) { |
| 226 | /* matching revision */ |
Michal Vasko | 9758650 | 2015-08-12 14:32:18 +0200 | [diff] [blame] | 227 | result = ctx->models.list[i]; |
| 228 | break; |
Radek Krejci | f647e61 | 2015-07-30 11:36:07 +0200 | [diff] [blame] | 229 | } |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 230 | } |
| 231 | } |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 232 | |
Radek Krejci | f647e61 | 2015-07-30 11:36:07 +0200 | [diff] [blame] | 233 | return result; |
Radek Krejci | fd4e6e3 | 2015-08-10 15:00:51 +0200 | [diff] [blame] | 234 | |
| 235 | } |
| 236 | |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame^] | 237 | API const struct lys_module * |
| 238 | 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] | 239 | { |
| 240 | return ly_ctx_get_module_by(ctx, ns, offsetof(struct lys_module, ns), revision); |
| 241 | } |
| 242 | |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame^] | 243 | API const struct lys_module * |
| 244 | 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] | 245 | { |
| 246 | 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] | 247 | } |
Michal Vasko | 3ec07dc | 2015-06-30 15:51:30 +0200 | [diff] [blame] | 248 | |
Michal Vasko | 8246596 | 2015-11-10 11:03:11 +0100 | [diff] [blame] | 249 | API struct lys_module * |
| 250 | ly_ctx_load_module(struct ly_ctx *ctx, const char *dir, const char *name, const char *revision) |
| 251 | { |
| 252 | struct lys_module *module; |
| 253 | char *cwd = NULL; |
| 254 | |
| 255 | if (!ctx || !name) { |
| 256 | ly_errno = LY_EINVAL; |
| 257 | return NULL; |
| 258 | } |
| 259 | |
| 260 | if (dir) { |
| 261 | cwd = get_current_dir_name(); |
| 262 | if (chdir(dir) == -1) { |
| 263 | LOGWRN("Unable to change working directory to \"%s\" (%s).", dir, strerror(errno)); |
| 264 | free(cwd); |
| 265 | cwd = NULL; |
| 266 | } |
| 267 | } |
| 268 | |
| 269 | module = lyp_search_file(ctx, NULL, name, revision); |
| 270 | |
| 271 | /* did we change the working directory? */ |
| 272 | if (cwd) { |
| 273 | if (chdir(cwd) == -1) { |
| 274 | /* should seriously not happen */ |
| 275 | LOGINT; |
| 276 | } |
| 277 | free(cwd); |
| 278 | } |
| 279 | |
| 280 | return module; |
| 281 | } |
| 282 | |
Radek Krejci | 96a10da | 2015-07-30 11:00:14 +0200 | [diff] [blame] | 283 | API const char ** |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame^] | 284 | ly_ctx_get_module_names(const struct ly_ctx *ctx) |
Michal Vasko | 3ec07dc | 2015-06-30 15:51:30 +0200 | [diff] [blame] | 285 | { |
| 286 | int i; |
Radek Krejci | 96a10da | 2015-07-30 11:00:14 +0200 | [diff] [blame] | 287 | const char **result = NULL; |
Michal Vasko | 3ec07dc | 2015-06-30 15:51:30 +0200 | [diff] [blame] | 288 | |
| 289 | if (!ctx) { |
| 290 | ly_errno = LY_EINVAL; |
| 291 | return NULL; |
| 292 | } |
| 293 | |
Michal Vasko | e2ea44b | 2015-07-07 11:31:48 +0200 | [diff] [blame] | 294 | result = malloc((ctx->models.used+1) * sizeof *result); |
| 295 | |
Michal Vasko | 3ec07dc | 2015-06-30 15:51:30 +0200 | [diff] [blame] | 296 | for (i = 0; i < ctx->models.used; i++) { |
Radek Krejci | 96a10da | 2015-07-30 11:00:14 +0200 | [diff] [blame] | 297 | result[i] = ctx->models.list[i]->name; |
Michal Vasko | 3ec07dc | 2015-06-30 15:51:30 +0200 | [diff] [blame] | 298 | } |
Michal Vasko | e2ea44b | 2015-07-07 11:31:48 +0200 | [diff] [blame] | 299 | result[i] = NULL; |
Michal Vasko | 3ec07dc | 2015-06-30 15:51:30 +0200 | [diff] [blame] | 300 | |
| 301 | return result; |
Michal Vasko | fa8c828 | 2015-07-03 15:14:59 +0200 | [diff] [blame] | 302 | } |
| 303 | |
Radek Krejci | 96a10da | 2015-07-30 11:00:14 +0200 | [diff] [blame] | 304 | API const char ** |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame^] | 305 | 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] | 306 | { |
| 307 | int i; |
Radek Krejci | 96a10da | 2015-07-30 11:00:14 +0200 | [diff] [blame] | 308 | const char **result = NULL; |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame^] | 309 | const struct lys_module *mod; |
Michal Vasko | fa8c828 | 2015-07-03 15:14:59 +0200 | [diff] [blame] | 310 | |
| 311 | if (!ctx) { |
| 312 | ly_errno = LY_EINVAL; |
| 313 | return NULL; |
| 314 | } |
| 315 | |
Radek Krejci | 96a10da | 2015-07-30 11:00:14 +0200 | [diff] [blame] | 316 | mod = ly_ctx_get_module(ctx, module_name, NULL); |
Michal Vasko | fa8c828 | 2015-07-03 15:14:59 +0200 | [diff] [blame] | 317 | if (!mod) { |
Radek Krejci | 96a10da | 2015-07-30 11:00:14 +0200 | [diff] [blame] | 318 | LOGERR(LY_EVALID, "Data model \"%s\" not loaded", module_name); |
Michal Vasko | fa8c828 | 2015-07-03 15:14:59 +0200 | [diff] [blame] | 319 | return NULL; |
| 320 | } |
| 321 | |
| 322 | result = malloc((mod->inc_size+1) * sizeof *result); |
| 323 | |
| 324 | for (i = 0; i < mod->inc_size; i++) { |
Radek Krejci | 96a10da | 2015-07-30 11:00:14 +0200 | [diff] [blame] | 325 | result[i] = mod->inc[i].submodule->name; |
Michal Vasko | fa8c828 | 2015-07-03 15:14:59 +0200 | [diff] [blame] | 326 | } |
| 327 | result[i] = NULL; |
| 328 | |
| 329 | return result; |
| 330 | } |
Michal Vasko | 209a622 | 2015-10-16 09:51:07 +0200 | [diff] [blame] | 331 | |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame^] | 332 | API const struct lys_node * |
| 333 | ly_ctx_get_node(const struct ly_ctx *ctx, const char *nodeid) |
Michal Vasko | f7a03dc | 2015-10-22 16:09:06 +0200 | [diff] [blame] | 334 | { |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame^] | 335 | const struct lys_node *ret; |
| 336 | const struct lys_module *module; |
Michal Vasko | f7a03dc | 2015-10-22 16:09:06 +0200 | [diff] [blame] | 337 | char *mod_name; |
| 338 | int parsed; |
| 339 | |
| 340 | if (!ctx || !nodeid) { |
| 341 | ly_errno = LY_EINVAL; |
| 342 | return NULL; |
| 343 | } |
| 344 | |
| 345 | if ((nodeid[0] != '/') || ((parsed = parse_identifier(nodeid + 1)) < 1)) { |
| 346 | ly_errno = LY_EINVAL; |
| 347 | return NULL; |
| 348 | } |
| 349 | |
| 350 | /* get the correct module */ |
| 351 | mod_name = strndup(nodeid + 1, parsed); |
| 352 | module = ly_ctx_get_module(ctx, mod_name, NULL); |
| 353 | free(mod_name); |
| 354 | if (!module) { |
| 355 | ly_errno = LY_EINVAL; |
| 356 | return NULL; |
| 357 | } |
| 358 | |
| 359 | /* now we can parse the whole schema */ |
| 360 | if (resolve_schema_nodeid(nodeid, NULL, module, LYS_AUGMENT, &ret)) { |
| 361 | ly_errno = LY_EINVAL; |
| 362 | return NULL; |
| 363 | } |
| 364 | |
| 365 | return ret; |
| 366 | } |
| 367 | |
Michal Vasko | 209a622 | 2015-10-16 09:51:07 +0200 | [diff] [blame] | 368 | static int |
| 369 | ylib_feature(struct lyd_node *parent, struct lys_module *cur_mod) |
| 370 | { |
| 371 | int i, j; |
| 372 | |
| 373 | /* module features */ |
| 374 | for (i = 0; i < cur_mod->features_size; ++i) { |
| 375 | if (!(cur_mod->features[i].flags & LYS_FENABLED)) { |
| 376 | continue; |
| 377 | } |
| 378 | |
Michal Vasko | 3e671b5 | 2015-10-23 16:23:15 +0200 | [diff] [blame] | 379 | if (!lyd_new_leaf(parent, NULL, "feature", cur_mod->features[i].name)) { |
Michal Vasko | 209a622 | 2015-10-16 09:51:07 +0200 | [diff] [blame] | 380 | return EXIT_FAILURE; |
| 381 | } |
| 382 | } |
| 383 | |
| 384 | /* submodule features */ |
| 385 | for (i = 0; i < cur_mod->inc_size; ++i) { |
| 386 | for (j = 0; j < cur_mod->inc[i].submodule->features_size; ++j) { |
| 387 | if (!(cur_mod->inc[i].submodule->features[j].flags & LYS_FENABLED)) { |
| 388 | continue; |
| 389 | } |
| 390 | |
Michal Vasko | 3e671b5 | 2015-10-23 16:23:15 +0200 | [diff] [blame] | 391 | 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] | 392 | return EXIT_FAILURE; |
| 393 | } |
| 394 | } |
| 395 | } |
| 396 | |
| 397 | return EXIT_SUCCESS; |
| 398 | } |
| 399 | |
| 400 | static int |
| 401 | ylib_deviation(struct lyd_node *parent, struct lys_module *cur_mod, struct ly_ctx *ctx) |
| 402 | { |
| 403 | int i, j, k; |
| 404 | struct lys_module *target_module, *mod_iter; |
| 405 | struct lyd_node *cont; |
| 406 | |
| 407 | for (i = 0; i < ctx->models.used; ++i) { |
| 408 | mod_iter = ctx->models.list[i]; |
| 409 | for (k = 0; k < mod_iter->deviation_size; ++k) { |
| 410 | if (mod_iter->deviation[k].target->module->type) { |
| 411 | target_module = ((struct lys_submodule *)mod_iter->deviation[k].target->module)->belongsto; |
| 412 | } else { |
| 413 | target_module = mod_iter->deviation[k].target->module; |
| 414 | } |
| 415 | |
| 416 | /* we found a module deviating our module */ |
| 417 | if (target_module == cur_mod) { |
| 418 | cont = lyd_new(parent, NULL, "deviation"); |
| 419 | if (!cont) { |
| 420 | return EXIT_FAILURE; |
| 421 | } |
| 422 | |
Michal Vasko | 3e671b5 | 2015-10-23 16:23:15 +0200 | [diff] [blame] | 423 | if (!lyd_new_leaf(cont, NULL, "name", mod_iter->name)) { |
Michal Vasko | 209a622 | 2015-10-16 09:51:07 +0200 | [diff] [blame] | 424 | return EXIT_FAILURE; |
| 425 | } |
Michal Vasko | 3e671b5 | 2015-10-23 16:23:15 +0200 | [diff] [blame] | 426 | if (!lyd_new_leaf(cont, NULL, "revision", (mod_iter->rev_size ? mod_iter->rev[0].date : ""))) { |
Michal Vasko | 209a622 | 2015-10-16 09:51:07 +0200 | [diff] [blame] | 427 | return EXIT_FAILURE; |
| 428 | } |
| 429 | } |
| 430 | } |
| 431 | |
| 432 | for (j = 0; j < mod_iter->inc_size; ++j) { |
| 433 | for (k = 0; k < mod_iter->inc[j].submodule->deviation_size; ++k) { |
| 434 | if (mod_iter->inc[j].submodule->deviation[k].target->module->type) { |
| 435 | target_module = ((struct lys_submodule *) |
| 436 | mod_iter->inc[j].submodule->deviation[k].target->module)->belongsto; |
| 437 | } else { |
| 438 | target_module = mod_iter->inc[j].submodule->deviation[k].target->module; |
| 439 | } |
| 440 | |
| 441 | /* we found a submodule deviating our module */ |
| 442 | if (target_module == cur_mod) { |
| 443 | cont = lyd_new(parent, NULL, "deviation"); |
| 444 | if (!cont) { |
| 445 | return EXIT_FAILURE; |
| 446 | } |
| 447 | |
Michal Vasko | 3e671b5 | 2015-10-23 16:23:15 +0200 | [diff] [blame] | 448 | if (!lyd_new_leaf(cont, NULL, "name", mod_iter->inc[j].submodule->name)) { |
Michal Vasko | 209a622 | 2015-10-16 09:51:07 +0200 | [diff] [blame] | 449 | return EXIT_FAILURE; |
| 450 | } |
Michal Vasko | 3e671b5 | 2015-10-23 16:23:15 +0200 | [diff] [blame] | 451 | if (!lyd_new_leaf(cont, NULL, "revision", |
Michal Vasko | 209a622 | 2015-10-16 09:51:07 +0200 | [diff] [blame] | 452 | (mod_iter->inc[j].submodule->rev_size ? |
| 453 | mod_iter->inc[j].submodule->rev[0].date : ""))) { |
| 454 | return EXIT_FAILURE; |
| 455 | } |
| 456 | } |
| 457 | } |
| 458 | } |
| 459 | } |
| 460 | |
| 461 | return EXIT_SUCCESS; |
| 462 | } |
| 463 | |
| 464 | static int |
| 465 | ylib_submodules(struct lyd_node *parent, struct lys_module *cur_mod) |
| 466 | { |
| 467 | int i; |
| 468 | struct lyd_node *cont; |
| 469 | |
| 470 | for (i = 0; i < cur_mod->inc_size; ++i) { |
| 471 | cont = lyd_new(parent, NULL, "submodule"); |
| 472 | if (!cont) { |
| 473 | return EXIT_FAILURE; |
| 474 | } |
| 475 | |
Michal Vasko | 3e671b5 | 2015-10-23 16:23:15 +0200 | [diff] [blame] | 476 | if (!lyd_new_leaf(cont, NULL, "name", cur_mod->inc[i].submodule->name)) { |
Michal Vasko | 209a622 | 2015-10-16 09:51:07 +0200 | [diff] [blame] | 477 | return EXIT_FAILURE; |
| 478 | } |
Michal Vasko | 3e671b5 | 2015-10-23 16:23:15 +0200 | [diff] [blame] | 479 | if (!lyd_new_leaf(cont, NULL, "revision", (cur_mod->inc[i].submodule->rev_size ? |
| 480 | cur_mod->inc[i].submodule->rev[0].date : ""))) { |
Michal Vasko | 209a622 | 2015-10-16 09:51:07 +0200 | [diff] [blame] | 481 | return EXIT_FAILURE; |
| 482 | } |
| 483 | if (cur_mod->inc[i].submodule->uri |
Michal Vasko | 3e671b5 | 2015-10-23 16:23:15 +0200 | [diff] [blame] | 484 | && !lyd_new_leaf(cont, NULL, "schema", cur_mod->inc[i].submodule->uri)) { |
Michal Vasko | 209a622 | 2015-10-16 09:51:07 +0200 | [diff] [blame] | 485 | return EXIT_FAILURE; |
| 486 | } |
| 487 | } |
| 488 | |
| 489 | return EXIT_SUCCESS; |
| 490 | } |
| 491 | |
| 492 | API struct lyd_node * |
| 493 | ly_ctx_info(struct ly_ctx *ctx) |
| 494 | { |
| 495 | int i; |
| 496 | char id[8]; |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame^] | 497 | const struct lys_module *mod; |
Michal Vasko | 209a622 | 2015-10-16 09:51:07 +0200 | [diff] [blame] | 498 | struct lyd_node *root, *cont; |
| 499 | |
| 500 | mod = ly_ctx_get_module(ctx, "ietf-yang-library", NULL); |
| 501 | if (!mod) { |
| 502 | mod = lyp_search_file(ctx, NULL, "ietf-yang-library", NULL); |
| 503 | } |
| 504 | if (!mod || !mod->data || strcmp(mod->data->next->name, "modules")) { |
| 505 | return NULL; |
| 506 | } |
| 507 | |
| 508 | root = lyd_new(NULL, mod, "modules"); |
| 509 | if (!root) { |
| 510 | return NULL; |
| 511 | } |
| 512 | |
| 513 | for (i = 0; i < ctx->models.used; ++i) { |
| 514 | cont = lyd_new(root, NULL, "module"); |
| 515 | if (!cont) { |
| 516 | lyd_free(root); |
| 517 | return NULL; |
| 518 | } |
| 519 | |
Michal Vasko | 3e671b5 | 2015-10-23 16:23:15 +0200 | [diff] [blame] | 520 | if (!lyd_new_leaf(cont, NULL, "name", ctx->models.list[i]->name)) { |
Michal Vasko | 209a622 | 2015-10-16 09:51:07 +0200 | [diff] [blame] | 521 | lyd_free(root); |
| 522 | return NULL; |
| 523 | } |
Michal Vasko | 3e671b5 | 2015-10-23 16:23:15 +0200 | [diff] [blame] | 524 | 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] | 525 | ctx->models.list[i]->rev[0].date : ""))) { |
| 526 | lyd_free(root); |
| 527 | return NULL; |
| 528 | } |
| 529 | if (ctx->models.list[i]->uri |
Michal Vasko | 3e671b5 | 2015-10-23 16:23:15 +0200 | [diff] [blame] | 530 | && !lyd_new_leaf(cont, NULL, "schema", ctx->models.list[i]->uri)) { |
Michal Vasko | 209a622 | 2015-10-16 09:51:07 +0200 | [diff] [blame] | 531 | lyd_free(root); |
| 532 | return NULL; |
| 533 | } |
Michal Vasko | 3e671b5 | 2015-10-23 16:23:15 +0200 | [diff] [blame] | 534 | if (!lyd_new_leaf(cont, NULL, "namespace", ctx->models.list[i]->ns)) { |
Michal Vasko | 209a622 | 2015-10-16 09:51:07 +0200 | [diff] [blame] | 535 | lyd_free(root); |
| 536 | return NULL; |
| 537 | } |
| 538 | if (ylib_feature(cont, ctx->models.list[i])) { |
| 539 | lyd_free(root); |
| 540 | return NULL; |
| 541 | } |
| 542 | if (ylib_deviation(cont, ctx->models.list[i], ctx)) { |
| 543 | lyd_free(root); |
| 544 | return NULL; |
| 545 | } |
| 546 | if (ctx->models.list[i]->implemented |
Michal Vasko | 3e671b5 | 2015-10-23 16:23:15 +0200 | [diff] [blame] | 547 | && !lyd_new_leaf(cont, NULL, "conformance", "implement")) { |
Michal Vasko | 209a622 | 2015-10-16 09:51:07 +0200 | [diff] [blame] | 548 | lyd_free(root); |
| 549 | return NULL; |
| 550 | } |
| 551 | if (!ctx->models.list[i]->implemented |
Michal Vasko | 3e671b5 | 2015-10-23 16:23:15 +0200 | [diff] [blame] | 552 | && !lyd_new_leaf(cont, NULL, "conformance", "import")) { |
Michal Vasko | 209a622 | 2015-10-16 09:51:07 +0200 | [diff] [blame] | 553 | lyd_free(root); |
| 554 | return NULL; |
| 555 | } |
| 556 | if (ylib_submodules(cont, ctx->models.list[i])) { |
| 557 | lyd_free(root); |
| 558 | return NULL; |
| 559 | } |
| 560 | } |
| 561 | |
| 562 | sprintf(id, "%u", ctx->models.module_set_id); |
Michal Vasko | 3e671b5 | 2015-10-23 16:23:15 +0200 | [diff] [blame] | 563 | if (!lyd_new_leaf(root, mod, "module-set-id", id)) { |
Michal Vasko | 209a622 | 2015-10-16 09:51:07 +0200 | [diff] [blame] | 564 | lyd_free(root); |
| 565 | return NULL; |
| 566 | } |
| 567 | |
| 568 | if (lyd_validate(root, 0)) { |
| 569 | lyd_free(root); |
| 570 | return NULL; |
| 571 | } |
| 572 | |
| 573 | return root; |
| 574 | } |