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 lys_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
;
139 /* Load module translator (JSON file). */
140 dnode
= lyd_parse_path(ly_translator_ctx
, path
, LYD_JSON
,
143 flog_warn(EC_LIB_YANG_TRANSLATOR_LOAD
,
144 "%s: lyd_parse_path() failed", __func__
);
147 dnode
= yang_dnode_get(dnode
,
148 "/frr-module-translator:frr-module-translator");
150 * libyang guarantees the "frr-module-translator" top-level container is
151 * always present since it contains mandatory child nodes.
155 family
= yang_dnode_get_string(dnode
, "./family");
156 translator
= yang_translator_find(family
);
157 if (translator
!= NULL
) {
158 flog_warn(EC_LIB_YANG_TRANSLATOR_LOAD
,
159 "%s: module translator \"%s\" is loaded already",
164 translator
= XCALLOC(MTYPE_YANG_TRANSLATOR
, sizeof(*translator
));
165 strlcpy(translator
->family
, family
, sizeof(translator
->family
));
166 translator
->modules
= list_new();
167 for (size_t i
= 0; i
< YANG_TRANSLATE_MAX
; i
++)
168 translator
->mappings
[i
] = hash_create(yang_mapping_hash_key
,
169 yang_mapping_hash_cmp
,
170 "YANG translation table");
171 RB_INSERT(yang_translators
, &yang_translators
, translator
);
173 /* Initialize the translator libyang context. */
174 translator
->ly_ctx
= yang_ctx_new_setup();
175 if (!translator
->ly_ctx
) {
176 flog_warn(EC_LIB_LIBYANG
, "%s: ly_ctx_new() failed", __func__
);
180 /* Load modules and deviations. */
181 set
= lyd_find_path(dnode
, "./module");
183 for (size_t i
= 0; i
< set
->number
; i
++) {
184 const char *module_name
;
187 XCALLOC(MTYPE_YANG_TRANSLATOR_MODULE
, sizeof(*tmodule
));
189 module_name
= yang_dnode_get_string(set
->set
.d
[i
], "./name");
190 tmodule
->module
= ly_ctx_load_module(translator
->ly_ctx
,
192 if (!tmodule
->module
) {
193 flog_warn(EC_LIB_YANG_TRANSLATOR_LOAD
,
194 "%s: failed to load module: %s", __func__
,
201 yang_dnode_get_string(set
->set
.d
[i
], "./deviations");
202 tmodule
->deviations
= ly_ctx_load_module(translator
->ly_ctx
,
204 if (!tmodule
->deviations
) {
205 flog_warn(EC_LIB_YANG_TRANSLATOR_LOAD
,
206 "%s: failed to load module: %s", __func__
,
211 lys_set_disabled(tmodule
->deviations
);
213 listnode_add(translator
->modules
, tmodule
);
217 /* Calculate the coverage. */
218 for (ALL_LIST_ELEMENTS_RO(translator
->modules
, ln
, tmodule
)) {
219 tmodule
->nodes_before_deviations
=
220 yang_module_nodes_count(tmodule
->module
);
222 lys_set_enabled(tmodule
->deviations
);
224 tmodule
->nodes_after_deviations
=
225 yang_module_nodes_count(tmodule
->module
);
226 tmodule
->coverage
= ((double)tmodule
->nodes_after_deviations
227 / (double)tmodule
->nodes_before_deviations
)
232 set
= lyd_find_path(dnode
, "./module/mappings");
234 for (size_t i
= 0; i
< set
->number
; i
++) {
235 const char *xpath_custom
, *xpath_native
;
236 const struct lys_node
*snode_custom
, *snode_native
;
238 xpath_custom
= yang_dnode_get_string(set
->set
.d
[i
], "./custom");
239 snode_custom
= ly_ctx_get_node(translator
->ly_ctx
, NULL
,
242 flog_warn(EC_LIB_YANG_TRANSLATOR_LOAD
,
243 "%s: unknown data path: %s", __func__
,
249 xpath_native
= yang_dnode_get_string(set
->set
.d
[i
], "./native");
251 ly_ctx_get_node(ly_native_ctx
, NULL
, xpath_native
, 0);
253 flog_warn(EC_LIB_YANG_TRANSLATOR_LOAD
,
254 "%s: unknown data path: %s", __func__
,
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
);
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
);
282 static void yang_tmodule_delete(struct yang_tmodule
*tmodule
)
284 XFREE(MTYPE_YANG_TRANSLATOR_MODULE
, tmodule
);
287 void yang_translator_unload(struct yang_translator
*translator
)
289 for (size_t i
= 0; i
< YANG_TRANSLATE_MAX
; i
++)
290 hash_clean(translator
->mappings
[i
], yang_mapping_hash_free
);
291 translator
->modules
->del
= (void (*)(void *))yang_tmodule_delete
;
292 list_delete(&translator
->modules
);
293 ly_ctx_destroy(translator
->ly_ctx
, NULL
);
294 RB_REMOVE(yang_translators
, &yang_translators
, translator
);
295 XFREE(MTYPE_YANG_TRANSLATOR
, translator
);
298 struct yang_translator
*yang_translator_find(const char *family
)
300 struct yang_translator s
;
302 strlcpy(s
.family
, family
, sizeof(s
.family
));
303 return RB_FIND(yang_translators
, &yang_translators
, &s
);
306 enum yang_translate_result
307 yang_translate_xpath(const struct yang_translator
*translator
, int dir
,
308 char *xpath
, size_t xpath_len
)
310 struct ly_ctx
*ly_ctx
;
311 const struct lys_node
*snode
;
312 struct yang_mapping_node
*mapping
;
313 char xpath_canonical
[XPATH_MAXLEN
];
314 char keys
[4][LIST_MAXKEYLEN
];
317 if (dir
== YANG_TRANSLATE_TO_NATIVE
)
318 ly_ctx
= translator
->ly_ctx
;
320 ly_ctx
= ly_native_ctx
;
322 snode
= ly_ctx_get_node(ly_ctx
, NULL
, xpath
, 0);
324 flog_warn(EC_LIB_YANG_TRANSLATION_ERROR
,
325 "%s: unknown data path: %s", __func__
, xpath
);
326 return YANG_TRANSLATE_FAILURE
;
329 yang_snode_get_path(snode
, YANG_PATH_DATA
, xpath_canonical
,
330 sizeof(xpath_canonical
));
331 mapping
= yang_mapping_lookup(translator
, dir
, xpath_canonical
);
333 return YANG_TRANSLATE_NOTFOUND
;
335 n
= sscanf(xpath
, mapping
->xpath_from_fmt
, keys
[0], keys
[1], keys
[2],
338 flog_warn(EC_LIB_YANG_TRANSLATION_ERROR
,
339 "%s: sscanf() failed: %s", __func__
,
340 safe_strerror(errno
));
341 return YANG_TRANSLATE_FAILURE
;
344 snprintf(xpath
, xpath_len
, mapping
->xpath_to_fmt
, keys
[0], keys
[1],
347 return YANG_TRANSLATE_SUCCESS
;
350 int yang_translate_dnode(const struct yang_translator
*translator
, int dir
,
351 struct lyd_node
**dnode
)
353 struct ly_ctx
*ly_ctx
;
354 struct lyd_node
*new;
355 struct lyd_node
*root
, *next
, *dnode_iter
;
357 /* Create new libyang data node to hold the translated data. */
358 if (dir
== YANG_TRANSLATE_TO_NATIVE
)
359 ly_ctx
= ly_native_ctx
;
361 ly_ctx
= translator
->ly_ctx
;
362 new = yang_dnode_new(ly_ctx
, false);
364 /* Iterate over all nodes from the data tree. */
365 LY_TREE_FOR (*dnode
, root
) {
366 LY_TREE_DFS_BEGIN (root
, next
, dnode_iter
) {
367 char xpath
[XPATH_MAXLEN
];
368 enum yang_translate_result ret
;
370 yang_dnode_get_path(dnode_iter
, xpath
, sizeof(xpath
));
371 ret
= yang_translate_xpath(translator
, dir
, xpath
,
374 case YANG_TRANSLATE_SUCCESS
:
376 case YANG_TRANSLATE_NOTFOUND
:
378 case YANG_TRANSLATE_FAILURE
:
382 /* Create new node in the tree of translated data. */
384 if (!lyd_new_path(new, ly_ctx
, xpath
,
385 (void *)yang_dnode_get_string(
387 0, LYD_PATH_OPT_UPDATE
)
389 flog_err(EC_LIB_LIBYANG
,
390 "%s: lyd_new_path() failed", __func__
);
395 LY_TREE_DFS_END(root
, next
, dnode_iter
);
399 /* Replace dnode by the new translated dnode. */
400 yang_dnode_free(*dnode
);
403 return YANG_TRANSLATE_SUCCESS
;
406 yang_dnode_free(new);
408 return YANG_TRANSLATE_FAILURE
;
411 struct translator_validate_args
{
412 struct yang_translator
*translator
;
416 static int yang_translator_validate_cb(const struct lys_node
*snode_custom
,
419 struct translator_validate_args
*args
= arg
;
420 struct yang_mapping_node
*mapping
;
421 const struct lys_node
*snode_native
;
422 const struct lys_type
*stype_custom
, *stype_native
;
423 char xpath
[XPATH_MAXLEN
];
425 yang_snode_get_path(snode_custom
, YANG_PATH_DATA
, xpath
, sizeof(xpath
));
426 mapping
= yang_mapping_lookup(args
->translator
,
427 YANG_TRANSLATE_TO_NATIVE
, xpath
);
429 flog_warn(EC_LIB_YANG_TRANSLATOR_LOAD
,
430 "%s: missing mapping for \"%s\"", __func__
, xpath
);
432 return YANG_ITER_CONTINUE
;
436 ly_ctx_get_node(ly_native_ctx
, NULL
, mapping
->xpath_to_fmt
, 0);
437 assert(snode_native
);
439 /* Check if the YANG types are compatible. */
440 stype_custom
= yang_snode_get_type(snode_custom
);
441 stype_native
= yang_snode_get_type(snode_native
);
442 if (stype_custom
&& stype_native
) {
443 if (stype_custom
->base
!= stype_native
->base
) {
445 EC_LIB_YANG_TRANSLATOR_LOAD
,
446 "%s: YANG types are incompatible (xpath: \"%s\")",
449 return YANG_ITER_CONTINUE
;
452 /* TODO: check if the value spaces are identical. */
455 return YANG_ITER_CONTINUE
;
459 * Check if the modules from the translator have a mapping for all of their
460 * schema nodes (after loading the deviations).
462 static unsigned int yang_translator_validate(struct yang_translator
*translator
)
464 struct yang_tmodule
*tmodule
;
466 struct translator_validate_args args
;
468 args
.translator
= translator
;
471 for (ALL_LIST_ELEMENTS_RO(translator
->modules
, ln
, tmodule
)) {
472 yang_snodes_iterate_module(
473 tmodule
->module
, yang_translator_validate_cb
,
474 YANG_ITER_FILTER_NPCONTAINERS
475 | YANG_ITER_FILTER_LIST_KEYS
476 | YANG_ITER_FILTER_INPUT_OUTPUT
,
482 EC_LIB_YANG_TRANSLATOR_LOAD
,
483 "%s: failed to validate \"%s\" module translator: %u error(s)",
484 __func__
, translator
->family
, args
.errors
);
489 static int yang_module_nodes_count_cb(const struct lys_node
*snode
, void *arg
)
491 unsigned int *total
= arg
;
495 return YANG_ITER_CONTINUE
;
498 /* Calculate the number of nodes for the given module. */
499 static unsigned int yang_module_nodes_count(const struct lys_module
*module
)
501 unsigned int total
= 0;
503 yang_snodes_iterate_module(module
, yang_module_nodes_count_cb
,
504 YANG_ITER_FILTER_NPCONTAINERS
505 | YANG_ITER_FILTER_LIST_KEYS
506 | YANG_ITER_FILTER_INPUT_OUTPUT
,
512 void yang_translator_init(void)
514 ly_translator_ctx
= yang_ctx_new_setup();
515 if (!ly_translator_ctx
) {
516 flog_err(EC_LIB_LIBYANG
, "%s: ly_ctx_new() failed", __func__
);
520 if (!ly_ctx_load_module(ly_translator_ctx
, "frr-module-translator",
523 EC_LIB_YANG_MODULE_LOAD
,
524 "%s: failed to load the \"frr-module-translator\" module",
530 void yang_translator_terminate(void)
532 while (!RB_EMPTY(yang_translators
, &yang_translators
)) {
533 struct yang_translator
*translator
;
535 translator
= RB_ROOT(yang_translators
, &yang_translators
);
536 yang_translator_unload(translator
);
539 ly_ctx_destroy(ly_translator_ctx
, NULL
);