2 * Copyright (C) 2018 NetDEF, Inc.
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)
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
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
23 #include "lib_errors.h"
26 #include "yang_translator.h"
29 DEFINE_MTYPE_STATIC(LIB
, YANG_TRANSLATOR
, "YANG Translator");
30 DEFINE_MTYPE_STATIC(LIB
, YANG_TRANSLATOR_MODULE
, "YANG Translator Module");
31 DEFINE_MTYPE_STATIC(LIB
, YANG_TRANSLATOR_MAPPING
, "YANG Translator Mapping");
33 /* Generate the yang_translators tree. */
34 static inline int yang_translator_compare(const struct yang_translator
*a
,
35 const struct yang_translator
*b
)
37 return strcmp(a
->family
, b
->family
);
39 RB_GENERATE(yang_translators
, yang_translator
, entry
, yang_translator_compare
)
41 struct yang_translators yang_translators
= RB_INITIALIZER(&yang_translators
);
43 /* Separate libyang context for the translator module. */
44 static struct ly_ctx
*ly_translator_ctx
;
47 yang_translator_validate(struct yang_translator
*translator
);
48 static unsigned int yang_module_nodes_count(const struct lys_module
*module
);
50 struct yang_mapping_node
{
51 char xpath_from_canonical
[XPATH_MAXLEN
];
52 char xpath_from_fmt
[XPATH_MAXLEN
];
53 char xpath_to_fmt
[XPATH_MAXLEN
];
56 static bool yang_mapping_hash_cmp(const void *value1
, const void *value2
)
58 const struct yang_mapping_node
*c1
= value1
;
59 const struct yang_mapping_node
*c2
= value2
;
61 return strmatch(c1
->xpath_from_canonical
, c2
->xpath_from_canonical
);
64 static unsigned int yang_mapping_hash_key(const void *value
)
66 return string_hash_make(value
);
69 static void *yang_mapping_hash_alloc(void *p
)
71 struct yang_mapping_node
*new, *key
= p
;
73 new = XCALLOC(MTYPE_YANG_TRANSLATOR_MAPPING
, sizeof(*new));
74 strlcpy(new->xpath_from_canonical
, key
->xpath_from_canonical
,
75 sizeof(new->xpath_from_canonical
));
80 static void yang_mapping_hash_free(void *arg
)
82 XFREE(MTYPE_YANG_TRANSLATOR_MAPPING
, arg
);
85 static struct yang_mapping_node
*
86 yang_mapping_lookup(const struct yang_translator
*translator
, int dir
,
89 struct yang_mapping_node s
;
91 strlcpy(s
.xpath_from_canonical
, xpath
, sizeof(s
.xpath_from_canonical
));
92 return hash_lookup(translator
->mappings
[dir
], &s
);
95 static void yang_mapping_add(struct yang_translator
*translator
, int dir
,
96 const struct lysc_node
*snode
,
97 const char *xpath_from_fmt
,
98 const char *xpath_to_fmt
)
100 struct yang_mapping_node
*mapping
, s
;
102 yang_snode_get_path(snode
, YANG_PATH_DATA
, s
.xpath_from_canonical
,
103 sizeof(s
.xpath_from_canonical
));
104 mapping
= hash_get(translator
->mappings
[dir
], &s
,
105 yang_mapping_hash_alloc
);
106 strlcpy(mapping
->xpath_from_fmt
, xpath_from_fmt
,
107 sizeof(mapping
->xpath_from_fmt
));
108 strlcpy(mapping
->xpath_to_fmt
, xpath_to_fmt
,
109 sizeof(mapping
->xpath_to_fmt
));
111 const char *keys
[] = {"KEY1", "KEY2", "KEY3", "KEY4"};
114 for (unsigned int i
= 0; i
< array_size(keys
); i
++) {
115 xpfmt
= frrstr_replace(mapping
->xpath_from_fmt
, keys
[i
],
117 strlcpy(mapping
->xpath_from_fmt
, xpfmt
,
118 sizeof(mapping
->xpath_from_fmt
));
119 XFREE(MTYPE_TMP
, xpfmt
);
122 for (unsigned int i
= 0; i
< array_size(keys
); i
++) {
123 xpfmt
= frrstr_replace(mapping
->xpath_to_fmt
, keys
[i
], "%s");
124 strlcpy(mapping
->xpath_to_fmt
, xpfmt
,
125 sizeof(mapping
->xpath_to_fmt
));
126 XFREE(MTYPE_TMP
, xpfmt
);
130 struct yang_translator
*yang_translator_load(const char *path
)
132 struct yang_translator
*translator
;
133 struct yang_tmodule
*tmodule
;
135 struct lyd_node
*dnode
;
140 /* Load module translator (JSON file). */
141 err
= lyd_parse_data_path(ly_translator_ctx
, path
, LYD_JSON
,
142 LYD_PARSE_NO_STATE
, LYD_VALIDATE_NO_STATE
,
145 flog_warn(EC_LIB_YANG_TRANSLATOR_LOAD
,
146 "%s: lyd_parse_path() failed: %d", __func__
, err
);
149 dnode
= yang_dnode_get(dnode
,
150 "/frr-module-translator:frr-module-translator");
152 * libyang guarantees the "frr-module-translator" top-level container is
153 * always present since it contains mandatory child nodes.
157 family
= yang_dnode_get_string(dnode
, "./family");
158 translator
= yang_translator_find(family
);
159 if (translator
!= NULL
) {
160 flog_warn(EC_LIB_YANG_TRANSLATOR_LOAD
,
161 "%s: module translator \"%s\" is loaded already",
166 translator
= XCALLOC(MTYPE_YANG_TRANSLATOR
, sizeof(*translator
));
167 strlcpy(translator
->family
, family
, sizeof(translator
->family
));
168 translator
->modules
= list_new();
169 for (size_t i
= 0; i
< YANG_TRANSLATE_MAX
; i
++)
170 translator
->mappings
[i
] = hash_create(yang_mapping_hash_key
,
171 yang_mapping_hash_cmp
,
172 "YANG translation table");
173 RB_INSERT(yang_translators
, &yang_translators
, translator
);
175 /* Initialize the translator libyang context. */
176 translator
->ly_ctx
= yang_ctx_new_setup(false, false);
177 if (!translator
->ly_ctx
) {
178 flog_warn(EC_LIB_LIBYANG
, "%s: ly_ctx_new() failed", __func__
);
183 if (lyd_find_xpath(dnode
, "./module", &set
) != LY_SUCCESS
)
184 assert(0); /* XXX libyang2: old ly1 code asserted success */
186 for (size_t i
= 0; i
< set
->count
; i
++) {
187 const char *module_name
;
190 XCALLOC(MTYPE_YANG_TRANSLATOR_MODULE
, sizeof(*tmodule
));
192 module_name
= yang_dnode_get_string(set
->dnodes
[i
], "./name");
193 tmodule
->module
= ly_ctx_load_module(translator
->ly_ctx
,
194 module_name
, NULL
, NULL
);
195 if (!tmodule
->module
) {
196 flog_warn(EC_LIB_YANG_TRANSLATOR_LOAD
,
197 "%s: failed to load module: %s", __func__
,
199 ly_set_free(set
, NULL
);
204 /* Count nodes in modules. */
205 for (ALL_LIST_ELEMENTS_RO(translator
->modules
, ln
, tmodule
)) {
206 tmodule
->nodes_before_deviations
=
207 yang_module_nodes_count(tmodule
->module
);
210 /* Load the deviations and count nodes again */
211 for (ALL_LIST_ELEMENTS_RO(translator
->modules
, ln
, tmodule
)) {
212 const char *module_name
= tmodule
->module
->name
;
213 tmodule
->deviations
= ly_ctx_load_module(
214 translator
->ly_ctx
, module_name
, NULL
, NULL
);
215 if (!tmodule
->deviations
) {
216 flog_warn(EC_LIB_YANG_TRANSLATOR_LOAD
,
217 "%s: failed to load module: %s", __func__
,
219 ly_set_free(set
, NULL
);
223 tmodule
->nodes_after_deviations
=
224 yang_module_nodes_count(tmodule
->module
);
226 ly_set_free(set
, NULL
);
228 /* Calculate the coverage. */
229 for (ALL_LIST_ELEMENTS_RO(translator
->modules
, ln
, tmodule
)) {
230 tmodule
->coverage
= ((double)tmodule
->nodes_after_deviations
231 / (double)tmodule
->nodes_before_deviations
)
236 if (lyd_find_xpath(dnode
, "./module/mappings", &set
) != LY_SUCCESS
)
237 assert(0); /* XXX libyang2: old ly1 code asserted success */
238 for (size_t i
= 0; i
< set
->count
; i
++) {
239 const char *xpath_custom
, *xpath_native
;
240 const struct lysc_node
*snode_custom
, *snode_native
;
243 yang_dnode_get_string(set
->dnodes
[i
], "./custom");
245 snode_custom
= lys_find_path(translator
->ly_ctx
, NULL
,
248 flog_warn(EC_LIB_YANG_TRANSLATOR_LOAD
,
249 "%s: unknown data path: %s", __func__
,
251 ly_set_free(set
, NULL
);
256 yang_dnode_get_string(set
->dnodes
[i
], "./native");
258 lys_find_path(ly_native_ctx
, NULL
, xpath_native
, 0);
260 flog_warn(EC_LIB_YANG_TRANSLATOR_LOAD
,
261 "%s: unknown data path: %s", __func__
,
263 ly_set_free(set
, NULL
);
267 yang_mapping_add(translator
, YANG_TRANSLATE_TO_NATIVE
,
268 snode_custom
, xpath_custom
, xpath_native
);
269 yang_mapping_add(translator
, YANG_TRANSLATE_FROM_NATIVE
,
270 snode_native
, xpath_native
, xpath_custom
);
272 ly_set_free(set
, NULL
);
274 /* Validate mappings. */
275 if (yang_translator_validate(translator
) != 0)
278 yang_dnode_free(dnode
);
283 yang_dnode_free(dnode
);
284 yang_translator_unload(translator
);
289 static void yang_tmodule_delete(struct yang_tmodule
*tmodule
)
291 XFREE(MTYPE_YANG_TRANSLATOR_MODULE
, tmodule
);
294 void yang_translator_unload(struct yang_translator
*translator
)
296 for (size_t i
= 0; i
< YANG_TRANSLATE_MAX
; i
++)
297 hash_clean(translator
->mappings
[i
], yang_mapping_hash_free
);
298 translator
->modules
->del
= (void (*)(void *))yang_tmodule_delete
;
299 list_delete(&translator
->modules
);
300 ly_ctx_destroy(translator
->ly_ctx
);
301 RB_REMOVE(yang_translators
, &yang_translators
, translator
);
302 XFREE(MTYPE_YANG_TRANSLATOR
, translator
);
305 struct yang_translator
*yang_translator_find(const char *family
)
307 struct yang_translator s
;
309 strlcpy(s
.family
, family
, sizeof(s
.family
));
310 return RB_FIND(yang_translators
, &yang_translators
, &s
);
313 enum yang_translate_result
314 yang_translate_xpath(const struct yang_translator
*translator
, int dir
,
315 char *xpath
, size_t xpath_len
)
317 struct ly_ctx
*ly_ctx
;
318 const struct lysc_node
*snode
;
319 struct yang_mapping_node
*mapping
;
320 char xpath_canonical
[XPATH_MAXLEN
];
321 char keys
[4][LIST_MAXKEYLEN
];
324 if (dir
== YANG_TRANSLATE_TO_NATIVE
)
325 ly_ctx
= translator
->ly_ctx
;
327 ly_ctx
= ly_native_ctx
;
329 snode
= lys_find_path(ly_ctx
, NULL
, xpath
, 0);
331 flog_warn(EC_LIB_YANG_TRANSLATION_ERROR
,
332 "%s: unknown data path: %s", __func__
, xpath
);
333 return YANG_TRANSLATE_FAILURE
;
336 yang_snode_get_path(snode
, YANG_PATH_DATA
, xpath_canonical
,
337 sizeof(xpath_canonical
));
338 mapping
= yang_mapping_lookup(translator
, dir
, xpath_canonical
);
340 return YANG_TRANSLATE_NOTFOUND
;
342 #pragma GCC diagnostic push
343 #pragma GCC diagnostic ignored "-Wformat-nonliteral"
344 /* processing format strings from mapping node... */
345 n
= sscanf(xpath
, mapping
->xpath_from_fmt
, keys
[0], keys
[1], keys
[2],
347 #pragma GCC diagnostic pop
349 flog_warn(EC_LIB_YANG_TRANSLATION_ERROR
,
350 "%s: sscanf() failed: %s", __func__
,
351 safe_strerror(errno
));
352 return YANG_TRANSLATE_FAILURE
;
355 #pragma GCC diagnostic push
356 #pragma GCC diagnostic ignored "-Wformat-nonliteral"
357 /* processing format strings from mapping node... */
358 snprintf(xpath
, xpath_len
, mapping
->xpath_to_fmt
, keys
[0], keys
[1],
360 #pragma GCC diagnostic pop
362 return YANG_TRANSLATE_SUCCESS
;
365 int yang_translate_dnode(const struct yang_translator
*translator
, int dir
,
366 struct lyd_node
**dnode
)
368 struct ly_ctx
*ly_ctx
;
369 struct lyd_node
*new;
370 struct lyd_node
*root
, *dnode_iter
;
372 /* Create new libyang data node to hold the translated data. */
373 if (dir
== YANG_TRANSLATE_TO_NATIVE
)
374 ly_ctx
= ly_native_ctx
;
376 ly_ctx
= translator
->ly_ctx
;
377 new = yang_dnode_new(ly_ctx
, false);
379 /* Iterate over all nodes from the data tree. */
380 LY_LIST_FOR (*dnode
, root
) {
381 LYD_TREE_DFS_BEGIN (root
, dnode_iter
) {
382 char xpath
[XPATH_MAXLEN
];
383 enum yang_translate_result ret
;
385 yang_dnode_get_path(dnode_iter
, xpath
, sizeof(xpath
));
386 ret
= yang_translate_xpath(translator
, dir
, xpath
,
389 case YANG_TRANSLATE_SUCCESS
:
391 case YANG_TRANSLATE_NOTFOUND
:
393 case YANG_TRANSLATE_FAILURE
:
397 /* Create new node in the tree of translated data. */
398 if (lyd_new_path(new, ly_ctx
, xpath
,
399 (void *)yang_dnode_get_string(
401 LYD_NEW_PATH_UPDATE
, NULL
)) {
402 flog_err(EC_LIB_LIBYANG
,
403 "%s: lyd_new_path() failed", __func__
);
408 LYD_TREE_DFS_END(root
, dnode_iter
);
412 /* Replace dnode by the new translated dnode. */
413 yang_dnode_free(*dnode
);
416 return YANG_TRANSLATE_SUCCESS
;
419 yang_dnode_free(new);
421 return YANG_TRANSLATE_FAILURE
;
424 struct translator_validate_args
{
425 struct yang_translator
*translator
;
429 static int yang_translator_validate_cb(const struct lysc_node
*snode_custom
,
432 struct translator_validate_args
*args
= arg
;
433 struct yang_mapping_node
*mapping
;
434 const struct lysc_node
*snode_native
;
435 const struct lysc_type
*stype_custom
, *stype_native
;
436 char xpath
[XPATH_MAXLEN
];
438 yang_snode_get_path(snode_custom
, YANG_PATH_DATA
, xpath
, sizeof(xpath
));
439 mapping
= yang_mapping_lookup(args
->translator
,
440 YANG_TRANSLATE_TO_NATIVE
, xpath
);
442 flog_warn(EC_LIB_YANG_TRANSLATOR_LOAD
,
443 "%s: missing mapping for \"%s\"", __func__
, xpath
);
445 return YANG_ITER_CONTINUE
;
449 lys_find_path(ly_native_ctx
, NULL
, mapping
->xpath_to_fmt
, 0);
450 assert(snode_native
);
452 /* Check if the YANG types are compatible. */
453 stype_custom
= yang_snode_get_type(snode_custom
);
454 stype_native
= yang_snode_get_type(snode_native
);
455 if (stype_custom
&& stype_native
) {
456 if (stype_custom
->basetype
!= stype_native
->basetype
) {
458 EC_LIB_YANG_TRANSLATOR_LOAD
,
459 "%s: YANG types are incompatible (xpath: \"%s\")",
462 return YANG_ITER_CONTINUE
;
465 /* TODO: check if the value spaces are identical. */
468 return YANG_ITER_CONTINUE
;
472 * Check if the modules from the translator have a mapping for all of their
473 * schema nodes (after loading the deviations).
475 static unsigned int yang_translator_validate(struct yang_translator
*translator
)
477 struct yang_tmodule
*tmodule
;
479 struct translator_validate_args args
;
481 args
.translator
= translator
;
484 for (ALL_LIST_ELEMENTS_RO(translator
->modules
, ln
, tmodule
)) {
485 yang_snodes_iterate(tmodule
->module
,
486 yang_translator_validate_cb
,
487 YANG_ITER_FILTER_NPCONTAINERS
488 | YANG_ITER_FILTER_LIST_KEYS
489 | YANG_ITER_FILTER_INPUT_OUTPUT
,
495 EC_LIB_YANG_TRANSLATOR_LOAD
,
496 "%s: failed to validate \"%s\" module translator: %u error(s)",
497 __func__
, translator
->family
, args
.errors
);
502 static int yang_module_nodes_count_cb(const struct lysc_node
*snode
, void *arg
)
504 unsigned int *total
= arg
;
508 return YANG_ITER_CONTINUE
;
511 /* Calculate the number of nodes for the given module. */
512 static unsigned int yang_module_nodes_count(const struct lys_module
*module
)
514 unsigned int total
= 0;
516 yang_snodes_iterate(module
, yang_module_nodes_count_cb
,
517 YANG_ITER_FILTER_NPCONTAINERS
518 | YANG_ITER_FILTER_LIST_KEYS
519 | YANG_ITER_FILTER_INPUT_OUTPUT
,
525 void yang_translator_init(void)
527 ly_translator_ctx
= yang_ctx_new_setup(true, false);
528 if (!ly_translator_ctx
) {
529 flog_err(EC_LIB_LIBYANG
, "%s: ly_ctx_new() failed", __func__
);
533 if (!ly_ctx_load_module(ly_translator_ctx
, "frr-module-translator",
536 EC_LIB_YANG_MODULE_LOAD
,
537 "%s: failed to load the \"frr-module-translator\" module",
543 void yang_translator_terminate(void)
545 while (!RB_EMPTY(yang_translators
, &yang_translators
)) {
546 struct yang_translator
*translator
;
548 translator
= RB_ROOT(yang_translators
, &yang_translators
);
549 yang_translator_unload(translator
);
552 ly_ctx_destroy(ly_translator_ctx
);