]> git.proxmox.com Git - mirror_frr.git/blob - lib/yang_translator.c
Merge pull request #12798 from donaldsharp/rib_match_multicast
[mirror_frr.git] / lib / yang_translator.c
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Copyright (C) 2018 NetDEF, Inc.
4 * Renato Westphal
5 */
6
7 #include <zebra.h>
8
9 #include "log.h"
10 #include "lib_errors.h"
11 #include "hash.h"
12 #include "yang.h"
13 #include "yang_translator.h"
14 #include "frrstr.h"
15
16 DEFINE_MTYPE_STATIC(LIB, YANG_TRANSLATOR, "YANG Translator");
17 DEFINE_MTYPE_STATIC(LIB, YANG_TRANSLATOR_MODULE, "YANG Translator Module");
18 DEFINE_MTYPE_STATIC(LIB, YANG_TRANSLATOR_MAPPING, "YANG Translator Mapping");
19
20 /* Generate the yang_translators tree. */
21 static inline int yang_translator_compare(const struct yang_translator *a,
22 const struct yang_translator *b)
23 {
24 return strcmp(a->family, b->family);
25 }
26 RB_GENERATE(yang_translators, yang_translator, entry, yang_translator_compare)
27
28 struct yang_translators yang_translators = RB_INITIALIZER(&yang_translators);
29
30 /* Separate libyang context for the translator module. */
31 static struct ly_ctx *ly_translator_ctx;
32
33 static unsigned int
34 yang_translator_validate(struct yang_translator *translator);
35 static unsigned int yang_module_nodes_count(const struct lys_module *module);
36
37 struct yang_mapping_node {
38 char xpath_from_canonical[XPATH_MAXLEN];
39 char xpath_from_fmt[XPATH_MAXLEN];
40 char xpath_to_fmt[XPATH_MAXLEN];
41 };
42
43 static bool yang_mapping_hash_cmp(const void *value1, const void *value2)
44 {
45 const struct yang_mapping_node *c1 = value1;
46 const struct yang_mapping_node *c2 = value2;
47
48 return strmatch(c1->xpath_from_canonical, c2->xpath_from_canonical);
49 }
50
51 static unsigned int yang_mapping_hash_key(const void *value)
52 {
53 return string_hash_make(value);
54 }
55
56 static void *yang_mapping_hash_alloc(void *p)
57 {
58 struct yang_mapping_node *new, *key = p;
59
60 new = XCALLOC(MTYPE_YANG_TRANSLATOR_MAPPING, sizeof(*new));
61 strlcpy(new->xpath_from_canonical, key->xpath_from_canonical,
62 sizeof(new->xpath_from_canonical));
63
64 return new;
65 }
66
67 static void yang_mapping_hash_free(void *arg)
68 {
69 XFREE(MTYPE_YANG_TRANSLATOR_MAPPING, arg);
70 }
71
72 static struct yang_mapping_node *
73 yang_mapping_lookup(const struct yang_translator *translator, int dir,
74 const char *xpath)
75 {
76 struct yang_mapping_node s;
77
78 strlcpy(s.xpath_from_canonical, xpath, sizeof(s.xpath_from_canonical));
79 return hash_lookup(translator->mappings[dir], &s);
80 }
81
82 static void yang_mapping_add(struct yang_translator *translator, int dir,
83 const struct lysc_node *snode,
84 const char *xpath_from_fmt,
85 const char *xpath_to_fmt)
86 {
87 struct yang_mapping_node *mapping, s;
88
89 yang_snode_get_path(snode, YANG_PATH_DATA, s.xpath_from_canonical,
90 sizeof(s.xpath_from_canonical));
91 mapping = hash_get(translator->mappings[dir], &s,
92 yang_mapping_hash_alloc);
93 strlcpy(mapping->xpath_from_fmt, xpath_from_fmt,
94 sizeof(mapping->xpath_from_fmt));
95 strlcpy(mapping->xpath_to_fmt, xpath_to_fmt,
96 sizeof(mapping->xpath_to_fmt));
97
98 const char *keys[] = {"KEY1", "KEY2", "KEY3", "KEY4"};
99 char *xpfmt;
100
101 for (unsigned int i = 0; i < array_size(keys); i++) {
102 xpfmt = frrstr_replace(mapping->xpath_from_fmt, keys[i],
103 "%[^']");
104 strlcpy(mapping->xpath_from_fmt, xpfmt,
105 sizeof(mapping->xpath_from_fmt));
106 XFREE(MTYPE_TMP, xpfmt);
107 }
108
109 for (unsigned int i = 0; i < array_size(keys); i++) {
110 xpfmt = frrstr_replace(mapping->xpath_to_fmt, keys[i], "%s");
111 strlcpy(mapping->xpath_to_fmt, xpfmt,
112 sizeof(mapping->xpath_to_fmt));
113 XFREE(MTYPE_TMP, xpfmt);
114 }
115 }
116
117 static void yang_tmodule_delete(struct yang_tmodule *tmodule)
118 {
119 XFREE(MTYPE_YANG_TRANSLATOR_MODULE, tmodule);
120 }
121
122 struct yang_translator *yang_translator_load(const char *path)
123 {
124 struct yang_translator *translator;
125 struct yang_tmodule *tmodule = NULL;
126 const char *family;
127 struct lyd_node *dnode;
128 struct ly_set *set;
129 struct listnode *ln;
130 LY_ERR err;
131
132 /* Load module translator (JSON file). */
133 err = lyd_parse_data_path(ly_translator_ctx, path, LYD_JSON,
134 LYD_PARSE_NO_STATE, LYD_VALIDATE_NO_STATE,
135 &dnode);
136 if (err) {
137 flog_warn(EC_LIB_YANG_TRANSLATOR_LOAD,
138 "%s: lyd_parse_path() failed: %d", __func__, err);
139 return NULL;
140 }
141 dnode = yang_dnode_get(dnode,
142 "/frr-module-translator:frr-module-translator");
143 /*
144 * libyang guarantees the "frr-module-translator" top-level container is
145 * always present since it contains mandatory child nodes.
146 */
147 assert(dnode);
148
149 family = yang_dnode_get_string(dnode, "./family");
150 translator = yang_translator_find(family);
151 if (translator != NULL) {
152 flog_warn(EC_LIB_YANG_TRANSLATOR_LOAD,
153 "%s: module translator \"%s\" is loaded already",
154 __func__, family);
155 yang_dnode_free(dnode);
156 return NULL;
157 }
158
159 translator = XCALLOC(MTYPE_YANG_TRANSLATOR, sizeof(*translator));
160 strlcpy(translator->family, family, sizeof(translator->family));
161 translator->modules = list_new();
162 for (size_t i = 0; i < YANG_TRANSLATE_MAX; i++)
163 translator->mappings[i] = hash_create(yang_mapping_hash_key,
164 yang_mapping_hash_cmp,
165 "YANG translation table");
166 RB_INSERT(yang_translators, &yang_translators, translator);
167
168 /* Initialize the translator libyang context. */
169 translator->ly_ctx = yang_ctx_new_setup(false, false);
170 if (!translator->ly_ctx) {
171 flog_warn(EC_LIB_LIBYANG, "%s: ly_ctx_new() failed", __func__);
172 goto error;
173 }
174
175 /* Load modules */
176 if (lyd_find_xpath(dnode, "./module", &set) != LY_SUCCESS)
177 assert(0); /* XXX libyang2: old ly1 code asserted success */
178
179 for (size_t i = 0; i < set->count; i++) {
180 const char *module_name;
181
182 tmodule =
183 XCALLOC(MTYPE_YANG_TRANSLATOR_MODULE, sizeof(*tmodule));
184
185 module_name = yang_dnode_get_string(set->dnodes[i], "./name");
186 tmodule->module = ly_ctx_load_module(translator->ly_ctx,
187 module_name, NULL, NULL);
188 if (!tmodule->module) {
189 flog_warn(EC_LIB_YANG_TRANSLATOR_LOAD,
190 "%s: failed to load module: %s", __func__,
191 module_name);
192 ly_set_free(set, NULL);
193 goto error;
194 }
195 }
196
197 /* Count nodes in modules. */
198 for (ALL_LIST_ELEMENTS_RO(translator->modules, ln, tmodule)) {
199 tmodule->nodes_before_deviations =
200 yang_module_nodes_count(tmodule->module);
201 }
202
203 /* Load the deviations and count nodes again */
204 for (ALL_LIST_ELEMENTS_RO(translator->modules, ln, tmodule)) {
205 const char *module_name = tmodule->module->name;
206 tmodule->deviations = ly_ctx_load_module(
207 translator->ly_ctx, module_name, NULL, NULL);
208 if (!tmodule->deviations) {
209 flog_warn(EC_LIB_YANG_TRANSLATOR_LOAD,
210 "%s: failed to load module: %s", __func__,
211 module_name);
212 ly_set_free(set, NULL);
213 goto error;
214 }
215
216 tmodule->nodes_after_deviations =
217 yang_module_nodes_count(tmodule->module);
218 }
219 ly_set_free(set, NULL);
220
221 /* Calculate the coverage. */
222 for (ALL_LIST_ELEMENTS_RO(translator->modules, ln, tmodule)) {
223 tmodule->coverage = ((double)tmodule->nodes_after_deviations
224 / (double)tmodule->nodes_before_deviations)
225 * 100;
226 }
227
228 /* Load mappings. */
229 if (lyd_find_xpath(dnode, "./module/mappings", &set) != LY_SUCCESS)
230 assert(0); /* XXX libyang2: old ly1 code asserted success */
231 for (size_t i = 0; i < set->count; i++) {
232 const char *xpath_custom, *xpath_native;
233 const struct lysc_node *snode_custom, *snode_native;
234
235 xpath_custom =
236 yang_dnode_get_string(set->dnodes[i], "./custom");
237
238 snode_custom = lys_find_path(translator->ly_ctx, NULL,
239 xpath_custom, 0);
240 if (!snode_custom) {
241 flog_warn(EC_LIB_YANG_TRANSLATOR_LOAD,
242 "%s: unknown data path: %s", __func__,
243 xpath_custom);
244 ly_set_free(set, NULL);
245 goto error;
246 }
247
248 xpath_native =
249 yang_dnode_get_string(set->dnodes[i], "./native");
250 snode_native =
251 lys_find_path(ly_native_ctx, NULL, xpath_native, 0);
252 if (!snode_native) {
253 flog_warn(EC_LIB_YANG_TRANSLATOR_LOAD,
254 "%s: unknown data path: %s", __func__,
255 xpath_native);
256 ly_set_free(set, NULL);
257 goto error;
258 }
259
260 yang_mapping_add(translator, YANG_TRANSLATE_TO_NATIVE,
261 snode_custom, xpath_custom, xpath_native);
262 yang_mapping_add(translator, YANG_TRANSLATE_FROM_NATIVE,
263 snode_native, xpath_native, xpath_custom);
264 }
265 ly_set_free(set, NULL);
266
267 /* Validate mappings. */
268 if (yang_translator_validate(translator) != 0)
269 goto error;
270
271 yang_dnode_free(dnode);
272
273 return translator;
274
275 error:
276 yang_dnode_free(dnode);
277 yang_translator_unload(translator);
278 yang_tmodule_delete(tmodule);
279
280 return NULL;
281 }
282
283 void yang_translator_unload(struct yang_translator *translator)
284 {
285 for (size_t i = 0; i < YANG_TRANSLATE_MAX; i++)
286 hash_clean(translator->mappings[i], yang_mapping_hash_free);
287 translator->modules->del = (void (*)(void *))yang_tmodule_delete;
288 list_delete(&translator->modules);
289 ly_ctx_destroy(translator->ly_ctx);
290 RB_REMOVE(yang_translators, &yang_translators, translator);
291 XFREE(MTYPE_YANG_TRANSLATOR, translator);
292 }
293
294 struct yang_translator *yang_translator_find(const char *family)
295 {
296 struct yang_translator s;
297
298 strlcpy(s.family, family, sizeof(s.family));
299 return RB_FIND(yang_translators, &yang_translators, &s);
300 }
301
302 enum yang_translate_result
303 yang_translate_xpath(const struct yang_translator *translator, int dir,
304 char *xpath, size_t xpath_len)
305 {
306 struct ly_ctx *ly_ctx;
307 const struct lysc_node *snode;
308 struct yang_mapping_node *mapping;
309 char xpath_canonical[XPATH_MAXLEN];
310 char keys[4][LIST_MAXKEYLEN];
311 int n;
312
313 if (dir == YANG_TRANSLATE_TO_NATIVE)
314 ly_ctx = translator->ly_ctx;
315 else
316 ly_ctx = ly_native_ctx;
317
318 snode = lys_find_path(ly_ctx, NULL, xpath, 0);
319 if (!snode) {
320 flog_warn(EC_LIB_YANG_TRANSLATION_ERROR,
321 "%s: unknown data path: %s", __func__, xpath);
322 return YANG_TRANSLATE_FAILURE;
323 }
324
325 yang_snode_get_path(snode, YANG_PATH_DATA, xpath_canonical,
326 sizeof(xpath_canonical));
327 mapping = yang_mapping_lookup(translator, dir, xpath_canonical);
328 if (!mapping)
329 return YANG_TRANSLATE_NOTFOUND;
330
331 #pragma GCC diagnostic push
332 #pragma GCC diagnostic ignored "-Wformat-nonliteral"
333 /* processing format strings from mapping node... */
334 n = sscanf(xpath, mapping->xpath_from_fmt, keys[0], keys[1], keys[2],
335 keys[3]);
336 #pragma GCC diagnostic pop
337 if (n < 0) {
338 flog_warn(EC_LIB_YANG_TRANSLATION_ERROR,
339 "%s: sscanf() failed: %s", __func__,
340 safe_strerror(errno));
341 return YANG_TRANSLATE_FAILURE;
342 }
343
344 #pragma GCC diagnostic push
345 #pragma GCC diagnostic ignored "-Wformat-nonliteral"
346 /* processing format strings from mapping node... */
347 snprintf(xpath, xpath_len, mapping->xpath_to_fmt, keys[0], keys[1],
348 keys[2], keys[3]);
349 #pragma GCC diagnostic pop
350
351 return YANG_TRANSLATE_SUCCESS;
352 }
353
354 int yang_translate_dnode(const struct yang_translator *translator, int dir,
355 struct lyd_node **dnode)
356 {
357 struct ly_ctx *ly_ctx;
358 struct lyd_node *new;
359 struct lyd_node *root, *dnode_iter;
360
361 /* Create new libyang data node to hold the translated data. */
362 if (dir == YANG_TRANSLATE_TO_NATIVE)
363 ly_ctx = ly_native_ctx;
364 else
365 ly_ctx = translator->ly_ctx;
366 new = yang_dnode_new(ly_ctx, false);
367
368 /* Iterate over all nodes from the data tree. */
369 LY_LIST_FOR (*dnode, root) {
370 LYD_TREE_DFS_BEGIN (root, dnode_iter) {
371 char xpath[XPATH_MAXLEN];
372 enum yang_translate_result ret;
373
374 yang_dnode_get_path(dnode_iter, xpath, sizeof(xpath));
375 ret = yang_translate_xpath(translator, dir, xpath,
376 sizeof(xpath));
377 switch (ret) {
378 case YANG_TRANSLATE_SUCCESS:
379 break;
380 case YANG_TRANSLATE_NOTFOUND:
381 goto next;
382 case YANG_TRANSLATE_FAILURE:
383 goto error;
384 }
385
386 /* Create new node in the tree of translated data. */
387 if (lyd_new_path(new, ly_ctx, xpath,
388 (void *)yang_dnode_get_string(
389 dnode_iter, NULL),
390 LYD_NEW_PATH_UPDATE, NULL)) {
391 flog_err(EC_LIB_LIBYANG,
392 "%s: lyd_new_path() failed", __func__);
393 goto error;
394 }
395
396 next:
397 LYD_TREE_DFS_END(root, dnode_iter);
398 }
399 }
400
401 /* Replace dnode by the new translated dnode. */
402 yang_dnode_free(*dnode);
403 *dnode = new;
404
405 return YANG_TRANSLATE_SUCCESS;
406
407 error:
408 yang_dnode_free(new);
409
410 return YANG_TRANSLATE_FAILURE;
411 }
412
413 struct translator_validate_args {
414 struct yang_translator *translator;
415 unsigned int errors;
416 };
417
418 static int yang_translator_validate_cb(const struct lysc_node *snode_custom,
419 void *arg)
420 {
421 struct translator_validate_args *args = arg;
422 struct yang_mapping_node *mapping;
423 const struct lysc_node *snode_native;
424 const struct lysc_type *stype_custom, *stype_native;
425 char xpath[XPATH_MAXLEN];
426
427 yang_snode_get_path(snode_custom, YANG_PATH_DATA, xpath, sizeof(xpath));
428 mapping = yang_mapping_lookup(args->translator,
429 YANG_TRANSLATE_TO_NATIVE, xpath);
430 if (!mapping) {
431 flog_warn(EC_LIB_YANG_TRANSLATOR_LOAD,
432 "%s: missing mapping for \"%s\"", __func__, xpath);
433 args->errors += 1;
434 return YANG_ITER_CONTINUE;
435 }
436
437 snode_native =
438 lys_find_path(ly_native_ctx, NULL, mapping->xpath_to_fmt, 0);
439 assert(snode_native);
440
441 /* Check if the YANG types are compatible. */
442 stype_custom = yang_snode_get_type(snode_custom);
443 stype_native = yang_snode_get_type(snode_native);
444 if (stype_custom && stype_native) {
445 if (stype_custom->basetype != stype_native->basetype) {
446 flog_warn(
447 EC_LIB_YANG_TRANSLATOR_LOAD,
448 "%s: YANG types are incompatible (xpath: \"%s\")",
449 __func__, xpath);
450 args->errors += 1;
451 return YANG_ITER_CONTINUE;
452 }
453
454 /* TODO: check if the value spaces are identical. */
455 }
456
457 return YANG_ITER_CONTINUE;
458 }
459
460 /*
461 * Check if the modules from the translator have a mapping for all of their
462 * schema nodes (after loading the deviations).
463 */
464 static unsigned int yang_translator_validate(struct yang_translator *translator)
465 {
466 struct yang_tmodule *tmodule;
467 struct listnode *ln;
468 struct translator_validate_args args;
469
470 args.translator = translator;
471 args.errors = 0;
472
473 for (ALL_LIST_ELEMENTS_RO(translator->modules, ln, tmodule)) {
474 yang_snodes_iterate(tmodule->module,
475 yang_translator_validate_cb,
476 YANG_ITER_FILTER_NPCONTAINERS
477 | YANG_ITER_FILTER_LIST_KEYS
478 | YANG_ITER_FILTER_INPUT_OUTPUT,
479 &args);
480 }
481
482 if (args.errors)
483 flog_warn(
484 EC_LIB_YANG_TRANSLATOR_LOAD,
485 "%s: failed to validate \"%s\" module translator: %u error(s)",
486 __func__, translator->family, args.errors);
487
488 return args.errors;
489 }
490
491 static int yang_module_nodes_count_cb(const struct lysc_node *snode, void *arg)
492 {
493 unsigned int *total = arg;
494
495 *total += 1;
496
497 return YANG_ITER_CONTINUE;
498 }
499
500 /* Calculate the number of nodes for the given module. */
501 static unsigned int yang_module_nodes_count(const struct lys_module *module)
502 {
503 unsigned int total = 0;
504
505 yang_snodes_iterate(module, yang_module_nodes_count_cb,
506 YANG_ITER_FILTER_NPCONTAINERS
507 | YANG_ITER_FILTER_LIST_KEYS
508 | YANG_ITER_FILTER_INPUT_OUTPUT,
509 &total);
510
511 return total;
512 }
513
514 void yang_translator_init(void)
515 {
516 ly_translator_ctx = yang_ctx_new_setup(true, false);
517 if (!ly_translator_ctx) {
518 flog_err(EC_LIB_LIBYANG, "%s: ly_ctx_new() failed", __func__);
519 exit(1);
520 }
521
522 if (!ly_ctx_load_module(ly_translator_ctx, "frr-module-translator",
523 NULL, NULL)) {
524 flog_err(
525 EC_LIB_YANG_MODULE_LOAD,
526 "%s: failed to load the \"frr-module-translator\" module",
527 __func__);
528 exit(1);
529 }
530 }
531
532 void yang_translator_terminate(void)
533 {
534 while (!RB_EMPTY(yang_translators, &yang_translators)) {
535 struct yang_translator *translator;
536
537 translator = RB_ROOT(yang_translators, &yang_translators);
538 yang_translator_unload(translator);
539 }
540
541 ly_ctx_destroy(ly_translator_ctx);
542 }