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" |
| 34 | #include "dict.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 */ |
| 81 | ctx->models.list[0] = lys_parse(ctx, (char *)ietf_inet_types_2013_07_15_yin, LYS_IN_YIN); |
| 82 | if (!ctx->models.list[0]) { |
| 83 | ly_ctx_destroy(ctx); |
| 84 | return NULL; |
| 85 | } |
| 86 | |
| 87 | /* load ietf-yang-types */ |
| 88 | ctx->models.list[1] = lys_parse(ctx, (char *)ietf_yang_types_2013_07_15_yin, LYS_IN_YIN); |
| 89 | if (!ctx->models.list[1]) { |
| 90 | ly_ctx_destroy(ctx); |
| 91 | return NULL; |
| 92 | } |
| 93 | |
| 94 | /* load ietf-yang-library */ |
| 95 | ctx->models.list[2] = lys_parse(ctx, (char *)ietf_yang_library_2015_07_03_yin, LYS_IN_YIN); |
| 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 | } |
| 121 | ctx->models.search_path = get_current_dir_name(); |
| 122 | chdir(cwd); |
| 123 | free(cwd); |
| 124 | } else { |
| 125 | free(ctx->models.search_path); |
| 126 | ctx->models.search_path = NULL; |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | API void |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 131 | ly_ctx_destroy(struct ly_ctx *ctx) |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 132 | { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 133 | if (!ctx) { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 134 | return; |
| 135 | } |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 136 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 137 | /* models list */ |
Radek Krejci | dce5145 | 2015-06-16 15:20:08 +0200 | [diff] [blame] | 138 | while (ctx->models.used) { |
Michal Vasko | 13b1583 | 2015-08-19 11:04:48 +0200 | [diff] [blame] | 139 | lys_free(ctx->models.list[0], 1); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 140 | } |
| 141 | free(ctx->models.search_path); |
| 142 | free(ctx->models.list); |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 143 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 144 | /* dictionary */ |
| 145 | lydict_clean(&ctx->dict); |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 146 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 147 | free(ctx); |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 148 | } |
| 149 | |
Radek Krejci | b804869 | 2015-08-05 13:36:34 +0200 | [diff] [blame] | 150 | API struct lys_submodule * |
| 151 | ly_ctx_get_submodule(struct lys_module *module, const char *name, const char *revision) |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 152 | { |
Radek Krejci | b804869 | 2015-08-05 13:36:34 +0200 | [diff] [blame] | 153 | struct lys_submodule *result; |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 154 | int i; |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 155 | |
Radek Krejci | 63a91a9 | 2015-07-29 13:31:04 +0200 | [diff] [blame] | 156 | if (!module || !name) { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 157 | ly_errno = LY_EINVAL; |
| 158 | return NULL; |
| 159 | } |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 160 | |
Radek Krejci | e98bb4b | 2015-07-30 14:21:41 +0200 | [diff] [blame] | 161 | /* TODO search also for submodules not directly available from the main module */ |
| 162 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 163 | /* search in modules included by the main module */ |
| 164 | if (module->type) { |
Radek Krejci | b804869 | 2015-08-05 13:36:34 +0200 | [diff] [blame] | 165 | module = ((struct lys_submodule *)module)->belongsto; |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 166 | } |
| 167 | for (i = 0; i < module->inc_size; i++) { |
| 168 | result = module->inc[i].submodule; |
| 169 | if (strcmp(name, result->name)) { |
| 170 | continue; |
| 171 | } |
Radek Krejci | ce7fb78 | 2015-05-29 16:52:34 +0200 | [diff] [blame] | 172 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 173 | if (!revision || (result->rev_size && !strcmp(revision, result->rev[0].date))) { |
| 174 | return result; |
| 175 | } |
| 176 | } |
Radek Krejci | ce7fb78 | 2015-05-29 16:52:34 +0200 | [diff] [blame] | 177 | |
Radek Krejci | 63a91a9 | 2015-07-29 13:31:04 +0200 | [diff] [blame] | 178 | return NULL; |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 179 | } |
| 180 | |
Radek Krejci | fd4e6e3 | 2015-08-10 15:00:51 +0200 | [diff] [blame] | 181 | static struct lys_module * |
| 182 | ly_ctx_get_module_by(struct ly_ctx *ctx, const char *key, int offset, const char *revision) |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 183 | { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 184 | int i; |
Radek Krejci | b804869 | 2015-08-05 13:36:34 +0200 | [diff] [blame] | 185 | struct lys_module *result = NULL; |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 186 | |
Radek Krejci | fd4e6e3 | 2015-08-10 15:00:51 +0200 | [diff] [blame] | 187 | if (!ctx || !key) { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 188 | ly_errno = LY_EINVAL; |
| 189 | return NULL; |
| 190 | } |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 191 | |
Radek Krejci | dce5145 | 2015-06-16 15:20:08 +0200 | [diff] [blame] | 192 | for (i = 0; i < ctx->models.used; i++) { |
Radek Krejci | fd4e6e3 | 2015-08-10 15:00:51 +0200 | [diff] [blame] | 193 | /* use offset to get address of the pointer to string (char**), remember that offset is in |
| 194 | * bytes, so we have to cast the pointer to the module to (char*), finally, we want to have |
| 195 | * string not the pointer to string |
| 196 | */ |
| 197 | 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] | 198 | continue; |
| 199 | } |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 200 | |
Radek Krejci | f647e61 | 2015-07-30 11:36:07 +0200 | [diff] [blame] | 201 | if (!revision) { |
| 202 | /* compare revisons and remember the newest one */ |
| 203 | if (result) { |
| 204 | if (!ctx->models.list[i]->rev_size) { |
| 205 | /* the current have no revision, keep the previous with some revision */ |
| 206 | continue; |
| 207 | } |
| 208 | if (result->rev_size && strcmp(ctx->models.list[i]->rev[0].date, result->rev[0].date) < 0) { |
| 209 | /* the previous found matching module has a newer revision */ |
| 210 | continue; |
| 211 | } |
| 212 | } |
| 213 | |
| 214 | /* remember the current match and search for newer version */ |
| 215 | result = ctx->models.list[i]; |
| 216 | } else { |
| 217 | if (ctx->models.list[i]->rev_size && !strcmp(revision, ctx->models.list[i]->rev[0].date)) { |
| 218 | /* matching revision */ |
Michal Vasko | 9758650 | 2015-08-12 14:32:18 +0200 | [diff] [blame] | 219 | result = ctx->models.list[i]; |
| 220 | break; |
Radek Krejci | f647e61 | 2015-07-30 11:36:07 +0200 | [diff] [blame] | 221 | } |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 222 | } |
| 223 | } |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 224 | |
Radek Krejci | f647e61 | 2015-07-30 11:36:07 +0200 | [diff] [blame] | 225 | return result; |
Radek Krejci | fd4e6e3 | 2015-08-10 15:00:51 +0200 | [diff] [blame] | 226 | |
| 227 | } |
| 228 | |
| 229 | API struct lys_module * |
| 230 | ly_ctx_get_module_by_ns(struct ly_ctx *ctx, const char *ns, const char *revision) |
| 231 | { |
| 232 | return ly_ctx_get_module_by(ctx, ns, offsetof(struct lys_module, ns), revision); |
| 233 | } |
| 234 | |
| 235 | API struct lys_module * |
| 236 | ly_ctx_get_module(struct ly_ctx *ctx, const char *name, const char *revision) |
| 237 | { |
| 238 | 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] | 239 | } |
Michal Vasko | 3ec07dc | 2015-06-30 15:51:30 +0200 | [diff] [blame] | 240 | |
Radek Krejci | 96a10da | 2015-07-30 11:00:14 +0200 | [diff] [blame] | 241 | API const char ** |
Michal Vasko | 3ec07dc | 2015-06-30 15:51:30 +0200 | [diff] [blame] | 242 | ly_ctx_get_module_names(struct ly_ctx *ctx) |
| 243 | { |
| 244 | int i; |
Radek Krejci | 96a10da | 2015-07-30 11:00:14 +0200 | [diff] [blame] | 245 | const char **result = NULL; |
Michal Vasko | 3ec07dc | 2015-06-30 15:51:30 +0200 | [diff] [blame] | 246 | |
| 247 | if (!ctx) { |
| 248 | ly_errno = LY_EINVAL; |
| 249 | return NULL; |
| 250 | } |
| 251 | |
Michal Vasko | e2ea44b | 2015-07-07 11:31:48 +0200 | [diff] [blame] | 252 | result = malloc((ctx->models.used+1) * sizeof *result); |
| 253 | |
Michal Vasko | 3ec07dc | 2015-06-30 15:51:30 +0200 | [diff] [blame] | 254 | for (i = 0; i < ctx->models.used; i++) { |
Radek Krejci | 96a10da | 2015-07-30 11:00:14 +0200 | [diff] [blame] | 255 | result[i] = ctx->models.list[i]->name; |
Michal Vasko | 3ec07dc | 2015-06-30 15:51:30 +0200 | [diff] [blame] | 256 | } |
Michal Vasko | e2ea44b | 2015-07-07 11:31:48 +0200 | [diff] [blame] | 257 | result[i] = NULL; |
Michal Vasko | 3ec07dc | 2015-06-30 15:51:30 +0200 | [diff] [blame] | 258 | |
| 259 | return result; |
Michal Vasko | fa8c828 | 2015-07-03 15:14:59 +0200 | [diff] [blame] | 260 | } |
| 261 | |
Radek Krejci | 96a10da | 2015-07-30 11:00:14 +0200 | [diff] [blame] | 262 | API const char ** |
| 263 | ly_ctx_get_submodule_names(struct ly_ctx *ctx, const char *module_name) |
Michal Vasko | fa8c828 | 2015-07-03 15:14:59 +0200 | [diff] [blame] | 264 | { |
| 265 | int i; |
Radek Krejci | 96a10da | 2015-07-30 11:00:14 +0200 | [diff] [blame] | 266 | const char **result = NULL; |
Radek Krejci | b804869 | 2015-08-05 13:36:34 +0200 | [diff] [blame] | 267 | struct lys_module *mod; |
Michal Vasko | fa8c828 | 2015-07-03 15:14:59 +0200 | [diff] [blame] | 268 | |
| 269 | if (!ctx) { |
| 270 | ly_errno = LY_EINVAL; |
| 271 | return NULL; |
| 272 | } |
| 273 | |
Radek Krejci | 96a10da | 2015-07-30 11:00:14 +0200 | [diff] [blame] | 274 | mod = ly_ctx_get_module(ctx, module_name, NULL); |
Michal Vasko | fa8c828 | 2015-07-03 15:14:59 +0200 | [diff] [blame] | 275 | if (!mod) { |
Radek Krejci | 96a10da | 2015-07-30 11:00:14 +0200 | [diff] [blame] | 276 | LOGERR(LY_EVALID, "Data model \"%s\" not loaded", module_name); |
Michal Vasko | fa8c828 | 2015-07-03 15:14:59 +0200 | [diff] [blame] | 277 | return NULL; |
| 278 | } |
| 279 | |
| 280 | result = malloc((mod->inc_size+1) * sizeof *result); |
| 281 | |
| 282 | for (i = 0; i < mod->inc_size; i++) { |
Radek Krejci | 96a10da | 2015-07-30 11:00:14 +0200 | [diff] [blame] | 283 | result[i] = mod->inc[i].submodule->name; |
Michal Vasko | fa8c828 | 2015-07-03 15:14:59 +0200 | [diff] [blame] | 284 | } |
| 285 | result[i] = NULL; |
| 286 | |
| 287 | return result; |
| 288 | } |
Michal Vasko | 209a622 | 2015-10-16 09:51:07 +0200 | [diff] [blame^] | 289 | |
| 290 | static int |
| 291 | ylib_feature(struct lyd_node *parent, struct lys_module *cur_mod) |
| 292 | { |
| 293 | int i, j; |
| 294 | |
| 295 | /* module features */ |
| 296 | for (i = 0; i < cur_mod->features_size; ++i) { |
| 297 | if (!(cur_mod->features[i].flags & LYS_FENABLED)) { |
| 298 | continue; |
| 299 | } |
| 300 | |
| 301 | if (!lyd_new_leaf_str(parent, NULL, "feature", LY_TYPE_STRING, cur_mod->features[i].name)) { |
| 302 | return EXIT_FAILURE; |
| 303 | } |
| 304 | } |
| 305 | |
| 306 | /* submodule features */ |
| 307 | for (i = 0; i < cur_mod->inc_size; ++i) { |
| 308 | for (j = 0; j < cur_mod->inc[i].submodule->features_size; ++j) { |
| 309 | if (!(cur_mod->inc[i].submodule->features[j].flags & LYS_FENABLED)) { |
| 310 | continue; |
| 311 | } |
| 312 | |
| 313 | if (!lyd_new_leaf_str(parent, NULL, "feature", LY_TYPE_STRING, cur_mod->inc[i].submodule->features[j].name)) { |
| 314 | return EXIT_FAILURE; |
| 315 | } |
| 316 | } |
| 317 | } |
| 318 | |
| 319 | return EXIT_SUCCESS; |
| 320 | } |
| 321 | |
| 322 | static int |
| 323 | ylib_deviation(struct lyd_node *parent, struct lys_module *cur_mod, struct ly_ctx *ctx) |
| 324 | { |
| 325 | int i, j, k; |
| 326 | struct lys_module *target_module, *mod_iter; |
| 327 | struct lyd_node *cont; |
| 328 | |
| 329 | for (i = 0; i < ctx->models.used; ++i) { |
| 330 | mod_iter = ctx->models.list[i]; |
| 331 | for (k = 0; k < mod_iter->deviation_size; ++k) { |
| 332 | if (mod_iter->deviation[k].target->module->type) { |
| 333 | target_module = ((struct lys_submodule *)mod_iter->deviation[k].target->module)->belongsto; |
| 334 | } else { |
| 335 | target_module = mod_iter->deviation[k].target->module; |
| 336 | } |
| 337 | |
| 338 | /* we found a module deviating our module */ |
| 339 | if (target_module == cur_mod) { |
| 340 | cont = lyd_new(parent, NULL, "deviation"); |
| 341 | if (!cont) { |
| 342 | return EXIT_FAILURE; |
| 343 | } |
| 344 | |
| 345 | if (!lyd_new_leaf_str(cont, NULL, "name", LY_TYPE_STRING, mod_iter->name)) { |
| 346 | return EXIT_FAILURE; |
| 347 | } |
| 348 | if (!lyd_new_leaf_str(cont, NULL, "revision", LY_TYPE_STRING, |
| 349 | (mod_iter->rev_size ? mod_iter->rev[0].date : ""))) { |
| 350 | return EXIT_FAILURE; |
| 351 | } |
| 352 | } |
| 353 | } |
| 354 | |
| 355 | for (j = 0; j < mod_iter->inc_size; ++j) { |
| 356 | for (k = 0; k < mod_iter->inc[j].submodule->deviation_size; ++k) { |
| 357 | if (mod_iter->inc[j].submodule->deviation[k].target->module->type) { |
| 358 | target_module = ((struct lys_submodule *) |
| 359 | mod_iter->inc[j].submodule->deviation[k].target->module)->belongsto; |
| 360 | } else { |
| 361 | target_module = mod_iter->inc[j].submodule->deviation[k].target->module; |
| 362 | } |
| 363 | |
| 364 | /* we found a submodule deviating our module */ |
| 365 | if (target_module == cur_mod) { |
| 366 | cont = lyd_new(parent, NULL, "deviation"); |
| 367 | if (!cont) { |
| 368 | return EXIT_FAILURE; |
| 369 | } |
| 370 | |
| 371 | if (!lyd_new_leaf_str(cont, NULL, "name", LY_TYPE_STRING, mod_iter->inc[j].submodule->name)) { |
| 372 | return EXIT_FAILURE; |
| 373 | } |
| 374 | if (!lyd_new_leaf_str(cont, NULL, "revision", LY_TYPE_STRING, |
| 375 | (mod_iter->inc[j].submodule->rev_size ? |
| 376 | mod_iter->inc[j].submodule->rev[0].date : ""))) { |
| 377 | return EXIT_FAILURE; |
| 378 | } |
| 379 | } |
| 380 | } |
| 381 | } |
| 382 | } |
| 383 | |
| 384 | return EXIT_SUCCESS; |
| 385 | } |
| 386 | |
| 387 | static int |
| 388 | ylib_submodules(struct lyd_node *parent, struct lys_module *cur_mod) |
| 389 | { |
| 390 | int i; |
| 391 | struct lyd_node *cont; |
| 392 | |
| 393 | for (i = 0; i < cur_mod->inc_size; ++i) { |
| 394 | cont = lyd_new(parent, NULL, "submodule"); |
| 395 | if (!cont) { |
| 396 | return EXIT_FAILURE; |
| 397 | } |
| 398 | |
| 399 | if (!lyd_new_leaf_str(cont, NULL, "name", LY_TYPE_STRING, cur_mod->inc[i].submodule->name)) { |
| 400 | return EXIT_FAILURE; |
| 401 | } |
| 402 | if (!lyd_new_leaf_str(cont, NULL, "revision", LY_TYPE_STRING, (cur_mod->inc[i].submodule->rev_size ? |
| 403 | cur_mod->inc[i].submodule->rev[0].date : ""))) { |
| 404 | return EXIT_FAILURE; |
| 405 | } |
| 406 | if (cur_mod->inc[i].submodule->uri |
| 407 | && !lyd_new_leaf_str(cont, NULL, "schema", LY_TYPE_STRING, cur_mod->inc[i].submodule->uri)) { |
| 408 | return EXIT_FAILURE; |
| 409 | } |
| 410 | } |
| 411 | |
| 412 | return EXIT_SUCCESS; |
| 413 | } |
| 414 | |
| 415 | API struct lyd_node * |
| 416 | ly_ctx_info(struct ly_ctx *ctx) |
| 417 | { |
| 418 | int i; |
| 419 | char id[8]; |
| 420 | struct lys_module *mod; |
| 421 | struct lyd_node *root, *cont; |
| 422 | |
| 423 | mod = ly_ctx_get_module(ctx, "ietf-yang-library", NULL); |
| 424 | if (!mod) { |
| 425 | mod = lyp_search_file(ctx, NULL, "ietf-yang-library", NULL); |
| 426 | } |
| 427 | if (!mod || !mod->data || strcmp(mod->data->next->name, "modules")) { |
| 428 | return NULL; |
| 429 | } |
| 430 | |
| 431 | root = lyd_new(NULL, mod, "modules"); |
| 432 | if (!root) { |
| 433 | return NULL; |
| 434 | } |
| 435 | |
| 436 | for (i = 0; i < ctx->models.used; ++i) { |
| 437 | cont = lyd_new(root, NULL, "module"); |
| 438 | if (!cont) { |
| 439 | lyd_free(root); |
| 440 | return NULL; |
| 441 | } |
| 442 | |
| 443 | if (!lyd_new_leaf_str(cont, NULL, "name", LY_TYPE_STRING, ctx->models.list[i]->name)) { |
| 444 | lyd_free(root); |
| 445 | return NULL; |
| 446 | } |
| 447 | if (!lyd_new_leaf_str(cont, NULL, "revision", LY_TYPE_STRING, (ctx->models.list[i]->rev_size ? |
| 448 | ctx->models.list[i]->rev[0].date : ""))) { |
| 449 | lyd_free(root); |
| 450 | return NULL; |
| 451 | } |
| 452 | if (ctx->models.list[i]->uri |
| 453 | && !lyd_new_leaf_str(cont, NULL, "schema", LY_TYPE_STRING, ctx->models.list[i]->uri)) { |
| 454 | lyd_free(root); |
| 455 | return NULL; |
| 456 | } |
| 457 | if (!lyd_new_leaf_str(cont, NULL, "namespace", LY_TYPE_STRING, ctx->models.list[i]->ns)) { |
| 458 | lyd_free(root); |
| 459 | return NULL; |
| 460 | } |
| 461 | if (ylib_feature(cont, ctx->models.list[i])) { |
| 462 | lyd_free(root); |
| 463 | return NULL; |
| 464 | } |
| 465 | if (ylib_deviation(cont, ctx->models.list[i], ctx)) { |
| 466 | lyd_free(root); |
| 467 | return NULL; |
| 468 | } |
| 469 | if (ctx->models.list[i]->implemented |
| 470 | && !lyd_new_leaf_str(cont, NULL, "conformance", LY_TYPE_ENUM, "implement")) { |
| 471 | lyd_free(root); |
| 472 | return NULL; |
| 473 | } |
| 474 | if (!ctx->models.list[i]->implemented |
| 475 | && !lyd_new_leaf_str(cont, NULL, "conformance", LY_TYPE_ENUM, "import")) { |
| 476 | lyd_free(root); |
| 477 | return NULL; |
| 478 | } |
| 479 | if (ylib_submodules(cont, ctx->models.list[i])) { |
| 480 | lyd_free(root); |
| 481 | return NULL; |
| 482 | } |
| 483 | } |
| 484 | |
| 485 | sprintf(id, "%u", ctx->models.module_set_id); |
| 486 | if (!lyd_new_leaf_str(root, mod, "module-set-id", LY_TYPE_STRING, id)) { |
| 487 | lyd_free(root); |
| 488 | return NULL; |
| 489 | } |
| 490 | |
| 491 | if (lyd_validate(root, 0)) { |
| 492 | lyd_free(root); |
| 493 | return NULL; |
| 494 | } |
| 495 | |
| 496 | return root; |
| 497 | } |