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