1 /*
2  * Copyright (C) 2018  NetDEF, Inc.
3  *                     Renato Westphal
4  *
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)
8  * any later version.
9  *
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
13  * more details.
14  *
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
18  */
19 
20 #ifndef _FRR_YANG_TRANSLATOR_H_
21 #define _FRR_YANG_TRANSLATOR_H_
22 
23 #ifdef __cplusplus
24 extern "C" {
25 #endif
26 
27 #define YANG_TRANSLATE_TO_NATIVE 0
28 #define YANG_TRANSLATE_FROM_NATIVE 1
29 #define YANG_TRANSLATE_MAX 2
30 
31 struct yang_tmodule {
32 	const struct lys_module *module;
33 	const struct lys_module *deviations;
34 	uint32_t nodes_before_deviations;
35 	uint32_t nodes_after_deviations;
36 	double coverage;
37 };
38 
39 struct yang_translator {
40 	RB_ENTRY(yang_translator) entry;
41 	char family[32];
42 	struct ly_ctx *ly_ctx;
43 	struct list *modules;
44 	struct hash *mappings[YANG_TRANSLATE_MAX];
45 };
46 RB_HEAD(yang_translators, yang_translator);
47 RB_PROTOTYPE(yang_translators, yang_translator, entry, yang_translator_compare);
48 
49 enum yang_translate_result {
50 	YANG_TRANSLATE_SUCCESS,
51 	YANG_TRANSLATE_NOTFOUND,
52 	YANG_TRANSLATE_FAILURE,
53 };
54 
55 /* Tree of all loaded YANG module translators. */
56 extern struct yang_translators yang_translators;
57 
58 /*
59  * Load a YANG module translator from a JSON file.
60  *
61  * path
62  *    Absolute path to the module translator file.
63  *
64  * Returns:
65  *    Pointer to newly created YANG module translator, or NULL in the case of an
66  *    error.
67  */
68 extern struct yang_translator *yang_translator_load(const char *path);
69 
70 /*
71  * Unload a YANG module translator.
72  *
73  * translator
74  *    Pointer to the YANG module translator.
75  */
76 extern void yang_translator_unload(struct yang_translator *translator);
77 
78 /*
79  * Find a YANG module translator by its family name.
80  *
81  * family
82  *    Family of the YANG module translator (e.g. ietf, openconfig).
83  *
84  * Returns:
85  *    Pointer to the YANG module translator if found, NULL otherwise.
86  */
87 extern struct yang_translator *yang_translator_find(const char *family);
88 
89 /*
90  * Translate an XPath expression.
91  *
92  * translator
93  *    Pointer to YANG module translator.
94  *
95  * dir
96  *    Direction of the translation (either YANG_TRANSLATE_TO_NATIVE or
97  *    YANG_TRANSLATE_FROM_NATIVE).
98  *
99  * xpath
100  *    Pointer to previously allocated buffer containing the xpath expression to
101  *    be translated.
102  *
103  * xpath_len
104  *    Size of the xpath buffer.
105  *
106  * Returns:
107  *    - YANG_TRANSLATE_SUCCESS on success.
108  *    - YANG_TRANSLATE_NOTFOUND when there's no available mapping to perform
109  *      the translation.
110  *    - YANG_TRANSLATE_FAILURE when an error occurred during the translation.
111  */
112 extern enum yang_translate_result
113 yang_translate_xpath(const struct yang_translator *translator, int dir,
114 		     char *xpath, size_t xpath_len);
115 
116 /*
117  * Translate an entire libyang data node.
118  *
119  * translator
120  *    Pointer to YANG module translator.
121  *
122  * dir
123  *    Direction of the translation (either YANG_TRANSLATE_TO_NATIVE or
124  *    YANG_TRANSLATE_FROM_NATIVE).
125  *
126  * dnode
127  *    libyang schema node we want to translate.
128  *
129  * Returns:
130  *    - YANG_TRANSLATE_SUCCESS on success.
131  *    - YANG_TRANSLATE_FAILURE when an error occurred during the translation.
132  */
133 extern int yang_translate_dnode(const struct yang_translator *translator,
134 				int dir, struct lyd_node **dnode);
135 
136 /*
137  * Initialize the YANG module translator subsystem. Should be called only once
138  * during the daemon initialization process.
139  */
140 extern void yang_translator_init(void);
141 
142 /*
143  * Finish the YANG module translator subsystem gracefully. Should be called only
144  * when the daemon is exiting.
145  */
146 extern void yang_translator_terminate(void);
147 
148 #ifdef __cplusplus
149 }
150 #endif
151 
152 #endif /* _FRR_YANG_TRANSLATOR_H_ */
153