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 n
= sscanf(xpath
, mapping
->xpath_from_fmt
, keys
[0], keys
[1], keys
[2],
345 flog_warn(EC_LIB_YANG_TRANSLATION_ERROR
,
346 "%s: sscanf() failed: %s", __func__
,
347 safe_strerror(errno
));
348 return YANG_TRANSLATE_FAILURE
;
351 snprintf(xpath
, xpath_len
, mapping
->xpath_to_fmt
, keys
[0], keys
[1],
354 return YANG_TRANSLATE_SUCCESS
;
357 int yang_translate_dnode(const struct yang_translator
*translator
, int dir
,
358 struct lyd_node
**dnode
)
360 struct ly_ctx
*ly_ctx
;
361 struct lyd_node
*new;
362 struct lyd_node
*root
, *dnode_iter
;
364 /* Create new libyang data node to hold the translated data. */
365 if (dir
== YANG_TRANSLATE_TO_NATIVE
)
366 ly_ctx
= ly_native_ctx
;
368 ly_ctx
= translator
->ly_ctx
;
369 new = yang_dnode_new(ly_ctx
, false);
371 /* Iterate over all nodes from the data tree. */
372 LY_LIST_FOR (*dnode
, root
) {
373 LYD_TREE_DFS_BEGIN (root
, dnode_iter
) {
374 char xpath
[XPATH_MAXLEN
];
375 enum yang_translate_result ret
;
377 yang_dnode_get_path(dnode_iter
, xpath
, sizeof(xpath
));
378 ret
= yang_translate_xpath(translator
, dir
, xpath
,
381 case YANG_TRANSLATE_SUCCESS
:
383 case YANG_TRANSLATE_NOTFOUND
:
385 case YANG_TRANSLATE_FAILURE
:
389 /* Create new node in the tree of translated data. */
390 if (lyd_new_path(new, ly_ctx
, xpath
,
391 (void *)yang_dnode_get_string(
393 LYD_NEW_PATH_UPDATE
, NULL
)) {
394 flog_err(EC_LIB_LIBYANG
,
395 "%s: lyd_new_path() failed", __func__
);
400 LYD_TREE_DFS_END(root
, dnode_iter
);
404 /* Replace dnode by the new translated dnode. */
405 yang_dnode_free(*dnode
);
408 return YANG_TRANSLATE_SUCCESS
;
411 yang_dnode_free(new);
413 return YANG_TRANSLATE_FAILURE
;
416 struct translator_validate_args
{
417 struct yang_translator
*translator
;
421 static int yang_translator_validate_cb(const struct lysc_node
*snode_custom
,
424 struct translator_validate_args
*args
= arg
;
425 struct yang_mapping_node
*mapping
;
426 const struct lysc_node
*snode_native
;
427 const struct lysc_type
*stype_custom
, *stype_native
;
428 char xpath
[XPATH_MAXLEN
];
430 yang_snode_get_path(snode_custom
, YANG_PATH_DATA
, xpath
, sizeof(xpath
));
431 mapping
= yang_mapping_lookup(args
->translator
,
432 YANG_TRANSLATE_TO_NATIVE
, xpath
);
434 flog_warn(EC_LIB_YANG_TRANSLATOR_LOAD
,
435 "%s: missing mapping for \"%s\"", __func__
, xpath
);
437 return YANG_ITER_CONTINUE
;
441 lys_find_path(ly_native_ctx
, NULL
, mapping
->xpath_to_fmt
, 0);
442 assert(snode_native
);
444 /* Check if the YANG types are compatible. */
445 stype_custom
= yang_snode_get_type(snode_custom
);
446 stype_native
= yang_snode_get_type(snode_native
);
447 if (stype_custom
&& stype_native
) {
448 if (stype_custom
->basetype
!= stype_native
->basetype
) {
450 EC_LIB_YANG_TRANSLATOR_LOAD
,
451 "%s: YANG types are incompatible (xpath: \"%s\")",
454 return YANG_ITER_CONTINUE
;
457 /* TODO: check if the value spaces are identical. */
460 return YANG_ITER_CONTINUE
;
464 * Check if the modules from the translator have a mapping for all of their
465 * schema nodes (after loading the deviations).
467 static unsigned int yang_translator_validate(struct yang_translator
*translator
)
469 struct yang_tmodule
*tmodule
;
471 struct translator_validate_args args
;
473 args
.translator
= translator
;
476 for (ALL_LIST_ELEMENTS_RO(translator
->modules
, ln
, tmodule
)) {
477 yang_snodes_iterate(tmodule
->module
,
478 yang_translator_validate_cb
,
479 YANG_ITER_FILTER_NPCONTAINERS
480 | YANG_ITER_FILTER_LIST_KEYS
481 | YANG_ITER_FILTER_INPUT_OUTPUT
,
487 EC_LIB_YANG_TRANSLATOR_LOAD
,
488 "%s: failed to validate \"%s\" module translator: %u error(s)",
489 __func__
, translator
->family
, args
.errors
);
494 static int yang_module_nodes_count_cb(const struct lysc_node
*snode
, void *arg
)
496 unsigned int *total
= arg
;
500 return YANG_ITER_CONTINUE
;
503 /* Calculate the number of nodes for the given module. */
504 static unsigned int yang_module_nodes_count(const struct lys_module
*module
)
506 unsigned int total
= 0;
508 yang_snodes_iterate(module
, yang_module_nodes_count_cb
,
509 YANG_ITER_FILTER_NPCONTAINERS
510 | YANG_ITER_FILTER_LIST_KEYS
511 | YANG_ITER_FILTER_INPUT_OUTPUT
,
517 void yang_translator_init(void)
519 ly_translator_ctx
= yang_ctx_new_setup(true, false);
520 if (!ly_translator_ctx
) {
521 flog_err(EC_LIB_LIBYANG
, "%s: ly_ctx_new() failed", __func__
);
525 if (!ly_ctx_load_module(ly_translator_ctx
, "frr-module-translator",
528 EC_LIB_YANG_MODULE_LOAD
,
529 "%s: failed to load the \"frr-module-translator\" module",
535 void yang_translator_terminate(void)
537 while (!RB_EMPTY(yang_translators
, &yang_translators
)) {
538 struct yang_translator
*translator
;
540 translator
= RB_ROOT(yang_translators
, &yang_translators
);
541 yang_translator_unload(translator
);
544 ly_ctx_destroy(ly_translator_ctx
);