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"
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")
32 /* Generate the yang_translators tree. */
33 static inline int yang_translator_compare(const struct yang_translator
*a
,
34 const struct yang_translator
*b
)
36 return strcmp(a
->family
, b
->family
);
38 RB_GENERATE(yang_translators
, yang_translator
, entry
, yang_translator_compare
)
40 struct yang_translators yang_translators
= RB_INITIALIZER(&yang_translators
);
42 /* Separate libyang context for the translator module. */
43 static struct ly_ctx
*ly_translator_ctx
;
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
);
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
];
57 static bool yang_mapping_hash_cmp(const void *value1
, const void *value2
)
59 const struct yang_mapping_node
*c1
= value1
;
60 const struct yang_mapping_node
*c2
= value2
;
62 return strmatch(c1
->xpath_from_canonical
, c2
->xpath_from_canonical
);
65 static unsigned int yang_mapping_hash_key(void *value
)
67 return string_hash_make(value
);
70 static void *yang_mapping_hash_alloc(void *p
)
72 struct yang_mapping_node
*new, *key
= p
;
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
));
81 static void yang_mapping_hash_free(void *arg
)
83 XFREE(MTYPE_YANG_TRANSLATOR_MAPPING
, arg
);
86 static struct yang_mapping_node
*
87 yang_mapping_lookup(const struct yang_translator
*translator
, int dir
,
90 struct yang_mapping_node s
;
92 strlcpy(s
.xpath_from_canonical
, xpath
, sizeof(s
.xpath_from_canonical
));
93 return hash_lookup(translator
->mappings
[dir
], &s
);
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
)
101 struct yang_mapping_node
*mapping
, s
;
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");
121 struct yang_translator
*yang_translator_load(const char *path
)
123 struct yang_translator
*translator
;
124 struct yang_tmodule
*tmodule
;
126 struct lyd_node
*dnode
;
130 /* Load module translator (JSON file). */
131 dnode
= lyd_parse_path(ly_translator_ctx
, path
, LYD_JSON
,
134 flog_warn(EC_LIB_YANG_TRANSLATOR_LOAD
,
135 "%s: lyd_parse_path() failed", __func__
);
138 dnode
= yang_dnode_get(dnode
,
139 "/frr-module-translator:frr-module-translator");
141 * libyang guarantees the "frr-module-translator" top-level container is
142 * always present since it contains mandatory child nodes.
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",
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
);
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__
);
171 /* Load modules and deviations. */
172 set
= lyd_find_path(dnode
, "./module");
174 for (size_t i
= 0; i
< set
->number
; i
++) {
175 const char *module_name
;
178 XCALLOC(MTYPE_YANG_TRANSLATOR_MODULE
, sizeof(*tmodule
));
180 module_name
= yang_dnode_get_string(set
->set
.d
[i
], "./name");
181 tmodule
->module
= ly_ctx_load_module(translator
->ly_ctx
,
183 if (!tmodule
->module
) {
184 flog_warn(EC_LIB_YANG_TRANSLATOR_LOAD
,
185 "%s: failed to load module: %s", __func__
,
192 yang_dnode_get_string(set
->set
.d
[i
], "./deviations");
193 tmodule
->deviations
= ly_ctx_load_module(translator
->ly_ctx
,
195 if (!tmodule
->deviations
) {
196 flog_warn(EC_LIB_YANG_TRANSLATOR_LOAD
,
197 "%s: failed to load module: %s", __func__
,
202 lys_set_disabled(tmodule
->deviations
);
204 listnode_add(translator
->modules
, tmodule
);
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
);
213 lys_set_enabled(tmodule
->deviations
);
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
)
223 set
= lyd_find_path(dnode
, "./module/mappings");
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
;
229 xpath_custom
= yang_dnode_get_string(set
->set
.d
[i
], "./custom");
230 snode_custom
= ly_ctx_get_node(translator
->ly_ctx
, NULL
,
233 flog_warn(EC_LIB_YANG_TRANSLATOR_LOAD
,
234 "%s: unknown data path: %s", __func__
,
240 xpath_native
= yang_dnode_get_string(set
->set
.d
[i
], "./native");
242 ly_ctx_get_node(ly_native_ctx
, NULL
, xpath_native
, 0);
244 flog_warn(EC_LIB_YANG_TRANSLATOR_LOAD
,
245 "%s: unknown data path: %s", __func__
,
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
);
258 /* Validate mappings. */
259 if (yang_translator_validate(translator
) != 0)
262 yang_dnode_free(dnode
);
267 yang_dnode_free(dnode
);
268 yang_translator_unload(translator
);
273 static void yang_tmodule_delete(struct yang_tmodule
*tmodule
)
275 XFREE(MTYPE_YANG_TRANSLATOR_MODULE
, tmodule
);
278 void yang_translator_unload(struct yang_translator
*translator
)
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
);
289 struct yang_translator
*yang_translator_find(const char *family
)
291 struct yang_translator s
;
293 strlcpy(s
.family
, family
, sizeof(s
.family
));
294 return RB_FIND(yang_translators
, &yang_translators
, &s
);
297 enum yang_translate_result
298 yang_translate_xpath(const struct yang_translator
*translator
, int dir
,
299 char *xpath
, size_t xpath_len
)
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
];
308 if (dir
== YANG_TRANSLATE_TO_NATIVE
)
309 ly_ctx
= translator
->ly_ctx
;
311 ly_ctx
= ly_native_ctx
;
313 snode
= ly_ctx_get_node(ly_ctx
, NULL
, xpath
, 0);
315 flog_warn(EC_LIB_YANG_TRANSLATION_ERROR
,
316 "%s: unknown data path: %s", __func__
, xpath
);
317 return YANG_TRANSLATE_FAILURE
;
320 yang_snode_get_path(snode
, YANG_PATH_DATA
, xpath_canonical
,
321 sizeof(xpath_canonical
));
322 mapping
= yang_mapping_lookup(translator
, dir
, xpath_canonical
);
324 return YANG_TRANSLATE_NOTFOUND
;
326 n
= sscanf(xpath
, mapping
->xpath_from_fmt
, keys
[0], keys
[1], keys
[2],
329 flog_warn(EC_LIB_YANG_TRANSLATION_ERROR
,
330 "%s: sscanf() failed: %s", __func__
,
331 safe_strerror(errno
));
332 return YANG_TRANSLATE_FAILURE
;
335 snprintf(xpath
, xpath_len
, mapping
->xpath_to_fmt
, keys
[0], keys
[1],
338 return YANG_TRANSLATE_SUCCESS
;
341 int yang_translate_dnode(const struct yang_translator
*translator
, int dir
,
342 struct lyd_node
**dnode
)
344 struct ly_ctx
*ly_ctx
;
345 struct lyd_node
*new;
346 struct lyd_node
*root
, *next
, *dnode_iter
;
348 /* Create new libyang data node to hold the translated data. */
349 if (dir
== YANG_TRANSLATE_TO_NATIVE
)
350 ly_ctx
= ly_native_ctx
;
352 ly_ctx
= translator
->ly_ctx
;
353 new = yang_dnode_new(ly_ctx
, false);
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
;
361 yang_dnode_get_path(dnode_iter
, xpath
, sizeof(xpath
));
362 ret
= yang_translate_xpath(translator
, dir
, xpath
,
365 case YANG_TRANSLATE_SUCCESS
:
367 case YANG_TRANSLATE_NOTFOUND
:
369 case YANG_TRANSLATE_FAILURE
:
373 /* Create new node in the tree of translated data. */
375 if (!lyd_new_path(new, ly_ctx
, xpath
,
376 (void *)yang_dnode_get_string(
378 0, LYD_PATH_OPT_UPDATE
)
380 flog_err(EC_LIB_LIBYANG
,
381 "%s: lyd_new_path() failed", __func__
);
386 LY_TREE_DFS_END(root
, next
, dnode_iter
);
390 /* Replace dnode by the new translated dnode. */
391 yang_dnode_free(*dnode
);
394 return YANG_TRANSLATE_SUCCESS
;
397 yang_dnode_free(new);
399 return YANG_TRANSLATE_FAILURE
;
402 struct translator_validate_args
{
403 struct yang_translator
*translator
;
407 static int yang_translator_validate_cb(const struct lys_node
*snode_custom
,
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
];
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
);
420 flog_warn(EC_LIB_YANG_TRANSLATOR_LOAD
,
421 "%s: missing mapping for \"%s\"", __func__
, xpath
);
423 return YANG_ITER_CONTINUE
;
427 ly_ctx_get_node(ly_native_ctx
, NULL
, mapping
->xpath_to_fmt
, 0);
428 assert(snode_native
);
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
) {
436 EC_LIB_YANG_TRANSLATOR_LOAD
,
437 "%s: YANG types are incompatible (xpath: \"%s\")",
440 return YANG_ITER_CONTINUE
;
443 /* TODO: check if the value spaces are identical. */
446 return YANG_ITER_CONTINUE
;
450 * Check if the modules from the translator have a mapping for all of their
451 * schema nodes (after loading the deviations).
453 static unsigned int yang_translator_validate(struct yang_translator
*translator
)
455 struct yang_tmodule
*tmodule
;
457 struct translator_validate_args args
;
459 args
.translator
= translator
;
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
,
473 EC_LIB_YANG_TRANSLATOR_LOAD
,
474 "%s: failed to validate \"%s\" module translator: %u error(s)",
475 __func__
, translator
->family
, args
.errors
);
480 static int yang_module_nodes_count_cb(const struct lys_node
*snode
, void *arg
)
482 unsigned int *total
= arg
;
486 return YANG_ITER_CONTINUE
;
489 /* Calculate the number of nodes for the given module. */
490 static unsigned int yang_module_nodes_count(const struct lys_module
*module
)
492 unsigned int total
= 0;
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
,
503 /* TODO: rewrite this function. */
504 static void str_replace(char *o_string
, const char *s_string
,
505 const char *r_string
)
510 ch
= strstr(o_string
, s_string
);
514 memcpy(buffer
, o_string
, ch
- o_string
);
515 buffer
[ch
- o_string
] = 0;
517 sprintf(buffer
+ (ch
- o_string
), "%s%s", r_string
,
518 ch
+ strlen(s_string
));
521 strcpy(o_string
, buffer
);
522 return str_replace(o_string
, s_string
, r_string
);
525 void yang_translator_init(void)
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__
);
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
, NULL
);