]> git.proxmox.com Git - mirror_frr.git/blob - mgmtd/mgmt_ds.h
Merge pull request #13649 from donaldsharp/unlock_the_node_or_else
[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 * Find YANG data node given a datastore handle YANG xpath.
222 */
223 extern struct lyd_node *
224 mgmt_ds_find_data_node_by_xpath(struct mgmt_ds_ctx *ds_ctx,
225 const char *xpath);
226
227 /*
228 * Delete YANG data node given a datastore handle and YANG xpath.
229 */
230 extern int mgmt_ds_delete_data_nodes(struct mgmt_ds_ctx *ds_ctx,
231 const char *xpath);
232
233 /*
234 * Iterate over datastore data.
235 *
236 * ds_ctx
237 * Datastore context.
238 *
239 * base_xpath
240 * Base YANG xpath from where needs to be iterated.
241 *
242 * iter_fn
243 * function that will be called during each iteration.
244 *
245 * ctx
246 * User defined opaque value normally used to pass
247 * reference to some user private context that will
248 * be passed to the iterator function provided in
249 * 'iter_fn'.
250 *
251 * Returns:
252 * 0 on success, -1 on failure.
253 */
254 extern int mgmt_ds_iter_data(
255 struct mgmt_ds_ctx *ds_ctx, const char *base_xpath,
256 void (*mgmt_ds_node_iter_fn)(struct mgmt_ds_ctx *ds_ctx,
257 const char *xpath, struct lyd_node *node,
258 struct nb_node *nb_node, void *ctx),
259 void *ctx);
260
261 /*
262 * Load config to datastore from a file.
263 *
264 * ds_ctx
265 * Datastore context.
266 *
267 * file_path
268 * File path of the configuration file.
269 *
270 * merge
271 * TRUE if you want to merge with existing config,
272 * FALSE if you want to replace with existing config
273 *
274 * Returns:
275 * 0 on success, -1 on failure.
276 */
277 extern int mgmt_ds_load_config_from_file(struct mgmt_ds_ctx *ds_ctx,
278 const char *file_path, bool merge);
279
280 /*
281 * Dump the data tree to a file with JSON/XML format.
282 *
283 * vty
284 * VTY context.
285 *
286 * ds_ctx
287 * Datastore context.
288 *
289 * xpath
290 * Base YANG xpath from where data needs to be dumped.
291 *
292 * f
293 * File pointer to where data to be dumped.
294 *
295 * format
296 * JSON/XML
297 */
298 extern void mgmt_ds_dump_tree(struct vty *vty, struct mgmt_ds_ctx *ds_ctx,
299 const char *xpath, FILE *f, LYD_FORMAT format);
300
301 /*
302 * Dump the complete data tree to a file with JSON format.
303 *
304 * file_name
305 * File path to where data to be dumped.
306 *
307 * ds
308 * Datastore context.
309 *
310 * Returns:
311 * 0 on success, -1 on failure.
312 */
313 extern int mgmt_ds_dump_ds_to_file(char *file_name,
314 struct mgmt_ds_ctx *ds_ctx);
315
316 /*
317 * Dump information about specific datastore.
318 */
319 extern void mgmt_ds_status_write_one(struct vty *vty,
320 struct mgmt_ds_ctx *ds_ctx);
321
322 /*
323 * Dump information about all the datastores.
324 */
325 extern void mgmt_ds_status_write(struct vty *vty);
326
327
328 /*
329 * Reset the candidate DS to empty state
330 */
331 void mgmt_ds_reset_candidate(void);
332
333 #endif /* _FRR_MGMTD_DS_H_ */