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