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