]> git.proxmox.com Git - mirror_frr.git/blob - mgmtd/mgmt_ds.h
mgmtd: Bringup MGMTD daemon and datastore module support
[mirror_frr.git] / mgmtd / mgmt_ds.h
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * MGMTD Datastores
4 *
5 * Copyright (C) 2021 Vmware, Inc.
6 * Pushpasis Sarkar <spushpasis@vmware.com>
7 */
8
9 #ifndef _FRR_MGMTD_DS_H_
10 #define _FRR_MGMTD_DS_H_
11
12 #include "northbound.h"
13
14 #include "mgmtd/mgmt_defines.h"
15
16 #define MGMTD_MAX_NUM_DSNODES_PER_BATCH 128
17
18 #define MGMTD_DS_NAME_MAX_LEN 32
19 #define MGMTD_DS_NAME_NONE "none"
20 #define MGMTD_DS_NAME_RUNNING "running"
21 #define MGMTD_DS_NAME_CANDIDATE "candidate"
22 #define MGMTD_DS_NAME_OPERATIONAL "operational"
23
24 #define MGMTD_STARTUP_DS_FILE_PATH DAEMON_DB_DIR "/frr_startup.json"
25
26 #define FOREACH_MGMTD_DS_ID(id) \
27 for ((id) = MGMTD_DS_NONE; (id) < MGMTD_DS_MAX_ID; (id)++)
28
29 #define MGMTD_MAX_COMMIT_LIST 10
30 #define MGMTD_MD5_HASH_LEN 16
31 #define MGMTD_MD5_HASH_STR_HEX_LEN 33
32
33 #define MGMTD_COMMIT_FILE_PATH DAEMON_DB_DIR "/commit-%s.json"
34 #define MGMTD_COMMIT_INDEX_FILE_NAME DAEMON_DB_DIR "/commit-index.dat"
35 #define MGMTD_COMMIT_TIME_STR_LEN 100
36
37 struct mgmt_master;
38
39 extern struct nb_config *running_config;
40
41 struct mgmt_ds_ctx;
42
43 /*
44 * Datastore-Id: For now defined here. Eventually will be
45 * defined as part of MGMTD Front-End interface.
46 */
47 enum mgmt_datastore_id {
48 MGMTD_DS_NONE = 0,
49 MGMTD_DS_RUNNING,
50 MGMTD_DS_CANDIDATE,
51 MGMTD_DS_OPERATIONAL,
52 MGMTD_DS_MAX_ID
53 };
54
55 typedef void (*mgmt_ds_node_iter_fn)(uint64_t ds_hndl, char *xpath,
56 struct lyd_node *node,
57 struct nb_node *nb_node, void *ctx);
58
59 /***************************************************************
60 * Global data exported
61 ***************************************************************/
62
63 extern const char *mgmt_ds_names[MGMTD_DS_MAX_ID + 1];
64
65 /*
66 * Convert datastore ID to datastore name.
67 *
68 * id
69 * Datastore ID.
70 *
71 * Returns:
72 * Datastore name.
73 */
74 static inline const char *mgmt_ds_id2name(enum mgmt_datastore_id id)
75 {
76 if (id > MGMTD_DS_MAX_ID)
77 id = MGMTD_DS_MAX_ID;
78 return mgmt_ds_names[id];
79 }
80
81 /*
82 * Convert datastore name to datastore ID.
83 *
84 * id
85 * Datastore name.
86 *
87 * Returns:
88 * Datastore ID.
89 */
90 static inline enum mgmt_datastore_id mgmt_ds_name2id(const char *name)
91 {
92 enum mgmt_datastore_id id;
93
94 FOREACH_MGMTD_DS_ID (id) {
95 if (!strncmp(mgmt_ds_names[id], name, MGMTD_DS_NAME_MAX_LEN))
96 return id;
97 }
98
99 return MGMTD_DS_NONE;
100 }
101
102 /*
103 * Convert datastore ID to datastore name.
104 *
105 * similar to above funtion.
106 */
107 static inline enum mgmt_datastore_id mgmt_get_ds_id_by_name(const char *ds_name)
108 {
109 if (!strncmp(ds_name, "candidate", sizeof("candidate")))
110 return MGMTD_DS_CANDIDATE;
111 else if (!strncmp(ds_name, "running", sizeof("running")))
112 return MGMTD_DS_RUNNING;
113 else if (!strncmp(ds_name, "operational", sizeof("operational")))
114 return MGMTD_DS_OPERATIONAL;
115 return MGMTD_DS_NONE;
116 }
117
118 /*
119 * Appends trail wildcard '/' '*' to a given xpath.
120 *
121 * xpath
122 * YANG xpath.
123 *
124 * path_len
125 * xpath length.
126 */
127 static inline void mgmt_xpath_append_trail_wildcard(char *xpath,
128 size_t *xpath_len)
129 {
130 if (!xpath || !xpath_len)
131 return;
132
133 if (!*xpath_len)
134 *xpath_len = strlen(xpath);
135
136 if (*xpath_len > 2 && *xpath_len < MGMTD_MAX_XPATH_LEN - 2) {
137 if (xpath[*xpath_len - 1] == '/') {
138 xpath[*xpath_len] = '*';
139 xpath[*xpath_len + 1] = 0;
140 (*xpath_len)++;
141 } else if (xpath[*xpath_len - 1] != '*') {
142 xpath[*xpath_len] = '/';
143 xpath[*xpath_len + 1] = '*';
144 xpath[*xpath_len + 2] = 0;
145 (*xpath_len) += 2;
146 }
147 }
148 }
149
150 /*
151 * Removes trail wildcard '/' '*' from a given xpath.
152 *
153 * xpath
154 * YANG xpath.
155 *
156 * path_len
157 * xpath length.
158 */
159 static inline void mgmt_xpath_remove_trail_wildcard(char *xpath,
160 size_t *xpath_len)
161 {
162 if (!xpath || !xpath_len)
163 return;
164
165 if (!*xpath_len)
166 *xpath_len = strlen(xpath);
167
168 if (*xpath_len > 2 && xpath[*xpath_len - 2] == '/'
169 && xpath[*xpath_len - 1] == '*') {
170 xpath[*xpath_len - 2] = 0;
171 (*xpath_len) -= 2;
172 }
173 }
174
175 /* Initialise datastore */
176 extern int mgmt_ds_init(struct mgmt_master *cm);
177
178 /* Destroy datastore */
179 extern void mgmt_ds_destroy(void);
180
181 /*
182 * Get datastore handler by ID
183 *
184 * mm
185 * Management master structure.
186 *
187 * ds_id
188 * Datastore ID.
189 *
190 * Returns:
191 * Datastore context (Holds info about ID, lock, root node etc).
192 */
193 extern struct mgmt_ds_ctx *mgmt_ds_get_ctx_by_id(struct mgmt_master *mm,
194 enum mgmt_datastore_id ds_id);
195
196 /*
197 * Check if a given datastore is config ds
198 */
199 extern bool mgmt_ds_is_config(struct mgmt_ds_ctx *ds_ctx);
200
201 /*
202 * Acquire read lock to a ds given a ds_handle
203 */
204 extern int mgmt_ds_read_lock(struct mgmt_ds_ctx *ds_ctx);
205
206 /*
207 * Acquire write lock to a ds given a ds_handle
208 */
209 extern int mgmt_ds_write_lock(struct mgmt_ds_ctx *ds_ctx);
210
211 /*
212 * Remove a lock from ds given a ds_handle
213 */
214 extern int mgmt_ds_unlock(struct mgmt_ds_ctx *ds_ctx);
215
216 /*
217 * Merge two datastores.
218 *
219 * src_ds
220 * Source datastore handle.
221 *
222 * dst_ds
223 * Destination datastore handle.
224 *
225 * update_cmd_rec
226 * TRUE if need to update commit record, FALSE otherwise.
227 *
228 * Returns:
229 * 0 on success, -1 on failure.
230 */
231 extern int mgmt_ds_merge_dss(struct mgmt_ds_ctx *src_ds_ctx,
232 struct mgmt_ds_ctx *dst_ds_ctx,
233 bool update_cmt_rec);
234
235 /*
236 * Copy from source to destination datastore.
237 *
238 * src_ds
239 * Source datastore handle (ds to be copied from).
240 *
241 * dst_ds
242 * Destination datastore handle (ds to be copied to).
243 *
244 * update_cmd_rec
245 * TRUE if need to update commit record, FALSE otherwise.
246 *
247 * Returns:
248 * 0 on success, -1 on failure.
249 */
250 extern int mgmt_ds_copy_dss(struct mgmt_ds_ctx *src_ds_ctx,
251 struct mgmt_ds_ctx *dst_ds_ctx,
252 bool update_cmt_rec);
253
254 /*
255 * Fetch northbound configuration for a given datastore context.
256 */
257 extern struct nb_config *mgmt_ds_get_nb_config(struct mgmt_ds_ctx *ds_ctx);
258
259 /*
260 * Lookup YANG data nodes.
261 *
262 * ds_ctx
263 * Datastore context.
264 *
265 * xpath
266 * YANG base xpath.
267 *
268 * dxpaths
269 * Out param - array of YANG data xpaths.
270 *
271 * num_nodes
272 * In-out param - number of YANG data xpaths.
273 * Note - Caller should init this to the size of the array
274 * provided in dxpaths.
275 * On return this will have the actual number of xpaths
276 * being returned.
277 *
278 * get_childs_as_well
279 * TRUE if child nodes needs to be fetched as well, FALSE otherwise.
280 *
281 * alloc_xp_copy
282 * TRUE if the caller is interested in getting a copy of the xpath.
283 *
284 * Returns:
285 * 0 on success, -1 on failure.
286 */
287 extern int mgmt_ds_lookup_data_nodes(struct mgmt_ds_ctx *ds_ctx,
288 const char *xpath, char *dxpaths[],
289 int *num_nodes, bool get_childs_as_well,
290 bool alloc_xp_copy);
291
292 /*
293 * Find YANG data node given a datastore handle YANG xpath.
294 */
295 extern struct lyd_node *
296 mgmt_ds_find_data_node_by_xpath(struct mgmt_ds_ctx *ds_ctx,
297 const char *xpath);
298
299 /*
300 * Delete YANG data node given a datastore handle and YANG xpath.
301 */
302 extern int mgmt_ds_delete_data_nodes(struct mgmt_ds_ctx *ds_ctx,
303 const char *xpath);
304
305 /*
306 * Iterate over datastore data.
307 *
308 * ds_ctx
309 * Datastore context.
310 *
311 * base_xpath
312 * Base YANG xpath from where needs to be iterated.
313 *
314 * iter_fn
315 * function that will be called during each iteration.
316 *
317 * ctx
318 * User defined opaque value normally used to pass
319 * reference to some user private context that will
320 * be passed to the iterator function provided in
321 * 'iter_fn'.
322 *
323 * alloc_xp_copy
324 * TRUE if the caller is interested in getting a copy of the xpath.
325 *
326 * Returns:
327 * 0 on success, -1 on failure.
328 */
329 extern int mgmt_ds_iter_data(
330 struct mgmt_ds_ctx *ds_ctx, char *base_xpath,
331 void (*mgmt_ds_node_iter_fn)(struct mgmt_ds_ctx *ds_ctx, char *xpath,
332 struct lyd_node *node,
333 struct nb_node *nb_node, void *ctx),
334 void *ctx, bool alloc_xp_copy);
335
336 /*
337 * Load config to datastore from a file.
338 *
339 * ds_ctx
340 * Datastore context.
341 *
342 * file_path
343 * File path of the configuration file.
344 *
345 * merge
346 * TRUE if you want to merge with existing config,
347 * FALSE if you want to replace with existing config
348 *
349 * Returns:
350 * 0 on success, -1 on failure.
351 */
352 extern int mgmt_ds_load_config_from_file(struct mgmt_ds_ctx *ds_ctx,
353 const char *file_path, bool merge);
354
355 /*
356 * Dump the data tree to a file with JSON/XML format.
357 *
358 * vty
359 * VTY context.
360 *
361 * ds_ctx
362 * Datastore context.
363 *
364 * xpath
365 * Base YANG xpath from where data needs to be dumped.
366 *
367 * f
368 * File pointer to where data to be dumped.
369 *
370 * format
371 * JSON/XML
372 */
373 extern void mgmt_ds_dump_tree(struct vty *vty, struct mgmt_ds_ctx *ds_ctx,
374 const char *xpath, FILE *f, LYD_FORMAT format);
375
376 /*
377 * Dump the complete data tree to a file with JSON format.
378 *
379 * file_name
380 * File path to where data to be dumped.
381 *
382 * ds
383 * Datastore context.
384 *
385 * Returns:
386 * 0 on success, -1 on failure.
387 */
388 extern int mgmt_ds_dump_ds_to_file(char *file_name,
389 struct mgmt_ds_ctx *ds_ctx);
390
391 /*
392 * Dump information about specific datastore.
393 */
394 extern void mgmt_ds_status_write_one(struct vty *vty,
395 struct mgmt_ds_ctx *ds_ctx);
396
397 /*
398 * Dump information about all the datastores.
399 */
400 extern void mgmt_ds_status_write(struct vty *vty);
401
402 #endif /* _FRR_MGMTD_DS_H_ */