1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright (C) 2018 NetDEF, Inc.
10 #include "lib_errors.h"
13 #include "yang_translator.h"
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");
20 /* Generate the yang_translators tree. */
21 static inline int yang_translator_compare(const struct yang_translator
*a
,
22 const struct yang_translator
*b
)
24 return strcmp(a
->family
, b
->family
);
26 RB_GENERATE(yang_translators
, yang_translator
, entry
, yang_translator_compare
)
28 struct yang_translators yang_translators
= RB_INITIALIZER(&yang_translators
);
30 /* Separate libyang context for the translator module. */
31 static struct ly_ctx
*ly_translator_ctx
;
34 yang_translator_validate(struct yang_translator
*translator
);
35 static unsigned int yang_module_nodes_count(const struct lys_module
*module
);
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
];
43 static bool yang_mapping_hash_cmp(const void *value1
, const void *value2
)
45 const struct yang_mapping_node
*c1
= value1
;
46 const struct yang_mapping_node
*c2
= value2
;
48 return strmatch(c1
->xpath_from_canonical
, c2
->xpath_from_canonical
);
51 static unsigned int yang_mapping_hash_key(const void *value
)
53 return string_hash_make(value
);
56 static void *yang_mapping_hash_alloc(void *p
)
58 struct yang_mapping_node
*new, *key
= p
;
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
));
67 static void yang_mapping_hash_free(void *arg
)
69 XFREE(MTYPE_YANG_TRANSLATOR_MAPPING
, arg
);
72 static struct yang_mapping_node
*
73 yang_mapping_lookup(const struct yang_translator
*translator
, int dir
,
76 struct yang_mapping_node s
;
78 strlcpy(s
.xpath_from_canonical
, xpath
, sizeof(s
.xpath_from_canonical
));
79 return hash_lookup(translator
->mappings
[dir
], &s
);
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
)
87 struct yang_mapping_node
*mapping
, s
;
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
));
98 const char *keys
[] = {"KEY1", "KEY2", "KEY3", "KEY4"};
101 for (unsigned int i
= 0; i
< array_size(keys
); i
++) {
102 xpfmt
= frrstr_replace(mapping
->xpath_from_fmt
, keys
[i
],
104 strlcpy(mapping
->xpath_from_fmt
, xpfmt
,
105 sizeof(mapping
->xpath_from_fmt
));
106 XFREE(MTYPE_TMP
, xpfmt
);
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
);
117 static void yang_tmodule_delete(struct yang_tmodule
*tmodule
)
119 XFREE(MTYPE_YANG_TRANSLATOR_MODULE
, tmodule
);
122 struct yang_translator
*yang_translator_load(const char *path
)
124 struct yang_translator
*translator
;
125 struct yang_tmodule
*tmodule
= NULL
;
127 struct lyd_node
*dnode
;
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
,
137 flog_warn(EC_LIB_YANG_TRANSLATOR_LOAD
,
138 "%s: lyd_parse_path() failed: %d", __func__
, err
);
141 dnode
= yang_dnode_get(dnode
,
142 "/frr-module-translator:frr-module-translator");
144 * libyang guarantees the "frr-module-translator" top-level container is
145 * always present since it contains mandatory child nodes.
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",
155 yang_dnode_free(dnode
);
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
);
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__
);
176 if (lyd_find_xpath(dnode
, "./module", &set
) != LY_SUCCESS
)
177 assert(0); /* XXX libyang2: old ly1 code asserted success */
179 for (size_t i
= 0; i
< set
->count
; i
++) {
180 const char *module_name
;
183 XCALLOC(MTYPE_YANG_TRANSLATOR_MODULE
, sizeof(*tmodule
));
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__
,
192 ly_set_free(set
, NULL
);
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
);
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__
,
212 ly_set_free(set
, NULL
);
216 tmodule
->nodes_after_deviations
=
217 yang_module_nodes_count(tmodule
->module
);
219 ly_set_free(set
, NULL
);
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
)
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
;
236 yang_dnode_get_string(set
->dnodes
[i
], "./custom");
238 snode_custom
= lys_find_path(translator
->ly_ctx
, NULL
,
241 flog_warn(EC_LIB_YANG_TRANSLATOR_LOAD
,
242 "%s: unknown data path: %s", __func__
,
244 ly_set_free(set
, NULL
);
249 yang_dnode_get_string(set
->dnodes
[i
], "./native");
251 lys_find_path(ly_native_ctx
, NULL
, xpath_native
, 0);
253 flog_warn(EC_LIB_YANG_TRANSLATOR_LOAD
,
254 "%s: unknown data path: %s", __func__
,
256 ly_set_free(set
, NULL
);
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
);
265 ly_set_free(set
, NULL
);
267 /* Validate mappings. */
268 if (yang_translator_validate(translator
) != 0)
271 yang_dnode_free(dnode
);
276 yang_dnode_free(dnode
);
277 yang_translator_unload(translator
);
278 yang_tmodule_delete(tmodule
);
283 void yang_translator_unload(struct yang_translator
*translator
)
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
);
294 struct yang_translator
*yang_translator_find(const char *family
)
296 struct yang_translator s
;
298 strlcpy(s
.family
, family
, sizeof(s
.family
));
299 return RB_FIND(yang_translators
, &yang_translators
, &s
);
302 enum yang_translate_result
303 yang_translate_xpath(const struct yang_translator
*translator
, int dir
,
304 char *xpath
, size_t xpath_len
)
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
];
313 if (dir
== YANG_TRANSLATE_TO_NATIVE
)
314 ly_ctx
= translator
->ly_ctx
;
316 ly_ctx
= ly_native_ctx
;
318 snode
= lys_find_path(ly_ctx
, NULL
, xpath
, 0);
320 flog_warn(EC_LIB_YANG_TRANSLATION_ERROR
,
321 "%s: unknown data path: %s", __func__
, xpath
);
322 return YANG_TRANSLATE_FAILURE
;
325 yang_snode_get_path(snode
, YANG_PATH_DATA
, xpath_canonical
,
326 sizeof(xpath_canonical
));
327 mapping
= yang_mapping_lookup(translator
, dir
, xpath_canonical
);
329 return YANG_TRANSLATE_NOTFOUND
;
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],
336 #pragma GCC diagnostic pop
338 flog_warn(EC_LIB_YANG_TRANSLATION_ERROR
,
339 "%s: sscanf() failed: %s", __func__
,
340 safe_strerror(errno
));
341 return YANG_TRANSLATE_FAILURE
;
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],
349 #pragma GCC diagnostic pop
351 return YANG_TRANSLATE_SUCCESS
;
354 int yang_translate_dnode(const struct yang_translator
*translator
, int dir
,
355 struct lyd_node
**dnode
)
357 struct ly_ctx
*ly_ctx
;
358 struct lyd_node
*new;
359 struct lyd_node
*root
, *dnode_iter
;
361 /* Create new libyang data node to hold the translated data. */
362 if (dir
== YANG_TRANSLATE_TO_NATIVE
)
363 ly_ctx
= ly_native_ctx
;
365 ly_ctx
= translator
->ly_ctx
;
366 new = yang_dnode_new(ly_ctx
, false);
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
;
374 yang_dnode_get_path(dnode_iter
, xpath
, sizeof(xpath
));
375 ret
= yang_translate_xpath(translator
, dir
, xpath
,
378 case YANG_TRANSLATE_SUCCESS
:
380 case YANG_TRANSLATE_NOTFOUND
:
382 case YANG_TRANSLATE_FAILURE
:
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(
390 LYD_NEW_PATH_UPDATE
, NULL
)) {
391 flog_err(EC_LIB_LIBYANG
,
392 "%s: lyd_new_path() failed", __func__
);
397 LYD_TREE_DFS_END(root
, dnode_iter
);
401 /* Replace dnode by the new translated dnode. */
402 yang_dnode_free(*dnode
);
405 return YANG_TRANSLATE_SUCCESS
;
408 yang_dnode_free(new);
410 return YANG_TRANSLATE_FAILURE
;
413 struct translator_validate_args
{
414 struct yang_translator
*translator
;
418 static int yang_translator_validate_cb(const struct lysc_node
*snode_custom
,
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
];
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
);
431 flog_warn(EC_LIB_YANG_TRANSLATOR_LOAD
,
432 "%s: missing mapping for \"%s\"", __func__
, xpath
);
434 return YANG_ITER_CONTINUE
;
438 lys_find_path(ly_native_ctx
, NULL
, mapping
->xpath_to_fmt
, 0);
439 assert(snode_native
);
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
) {
447 EC_LIB_YANG_TRANSLATOR_LOAD
,
448 "%s: YANG types are incompatible (xpath: \"%s\")",
451 return YANG_ITER_CONTINUE
;
454 /* TODO: check if the value spaces are identical. */
457 return YANG_ITER_CONTINUE
;
461 * Check if the modules from the translator have a mapping for all of their
462 * schema nodes (after loading the deviations).
464 static unsigned int yang_translator_validate(struct yang_translator
*translator
)
466 struct yang_tmodule
*tmodule
;
468 struct translator_validate_args args
;
470 args
.translator
= translator
;
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
,
484 EC_LIB_YANG_TRANSLATOR_LOAD
,
485 "%s: failed to validate \"%s\" module translator: %u error(s)",
486 __func__
, translator
->family
, args
.errors
);
491 static int yang_module_nodes_count_cb(const struct lysc_node
*snode
, void *arg
)
493 unsigned int *total
= arg
;
497 return YANG_ITER_CONTINUE
;
500 /* Calculate the number of nodes for the given module. */
501 static unsigned int yang_module_nodes_count(const struct lys_module
*module
)
503 unsigned int total
= 0;
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
,
514 void yang_translator_init(void)
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__
);
522 if (!ly_ctx_load_module(ly_translator_ctx
, "frr-module-translator",
525 EC_LIB_YANG_MODULE_LOAD
,
526 "%s: failed to load the \"frr-module-translator\" module",
532 void yang_translator_terminate(void)
534 while (!RB_EMPTY(yang_translators
, &yang_translators
)) {
535 struct yang_translator
*translator
;
537 translator
= RB_ROOT(yang_translators
, &yang_translators
);
538 yang_translator_unload(translator
);
541 ly_ctx_destroy(ly_translator_ctx
);