]>
git.proxmox.com Git - mirror_frr.git/blob - mgmtd/mgmt_ds.h
1 // SPDX-License-Identifier: GPL-2.0-or-later
5 * Copyright (C) 2021 Vmware, Inc.
6 * Pushpasis Sarkar <spushpasis@vmware.com>
9 #ifndef _FRR_MGMTD_DS_H_
10 #define _FRR_MGMTD_DS_H_
12 #include "mgmt_fe_client.h"
13 #include "northbound.h"
15 #include "mgmtd/mgmt_defines.h"
16 #include "mgmtd/mgmt_be_adapter.h"
17 #include "mgmtd/mgmt_fe_adapter.h"
19 #define MGMTD_MAX_NUM_DSNODES_PER_BATCH 128
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"
27 #define FOREACH_MGMTD_DS_ID(id) \
28 for ((id) = MGMTD_DS_NONE; (id) < MGMTD_DS_MAX_ID; (id)++)
30 #define MGMTD_MAX_COMMIT_LIST 10
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"
35 extern struct nb_config
*running_config
;
39 /***************************************************************
40 * Global data exported
41 ***************************************************************/
43 extern const char *mgmt_ds_names
[MGMTD_DS_MAX_ID
+ 1];
46 * Convert datastore ID to datastore name.
54 static inline const char *mgmt_ds_id2name(Mgmtd__DatastoreId id
)
56 if (id
> MGMTD_DS_MAX_ID
)
58 return mgmt_ds_names
[id
];
62 * Convert datastore name to datastore ID.
70 static inline Mgmtd__DatastoreId
mgmt_ds_name2id(const char *name
)
72 Mgmtd__DatastoreId id
;
74 FOREACH_MGMTD_DS_ID (id
) {
75 if (!strncmp(mgmt_ds_names
[id
], name
, MGMTD_DS_NAME_MAX_LEN
))
83 * Convert datastore ID to datastore name.
85 * similar to above funtion.
87 static inline Mgmtd__DatastoreId
mgmt_get_ds_id_by_name(const char *ds_name
)
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
;
99 * Appends trail wildcard '/' '*' to a given xpath.
107 static inline void mgmt_xpath_append_trail_wildcard(char *xpath
,
110 if (!xpath
|| !xpath_len
)
114 *xpath_len
= strlen(xpath
);
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;
121 } else if (xpath
[*xpath_len
- 1] != '*') {
122 xpath
[*xpath_len
] = '/';
123 xpath
[*xpath_len
+ 1] = '*';
124 xpath
[*xpath_len
+ 2] = 0;
131 * Removes trail wildcard '/' '*' from a given xpath.
139 static inline void mgmt_xpath_remove_trail_wildcard(char *xpath
,
142 if (!xpath
|| !xpath_len
)
146 *xpath_len
= strlen(xpath
);
148 if (*xpath_len
> 2 && xpath
[*xpath_len
- 2] == '/'
149 && xpath
[*xpath_len
- 1] == '*') {
150 xpath
[*xpath_len
- 2] = 0;
155 /* Initialise datastore */
156 extern int mgmt_ds_init(struct mgmt_master
*cm
);
158 /* Destroy datastore */
159 extern void mgmt_ds_destroy(void);
162 * Get datastore handler by ID
165 * Management master structure.
171 * Datastore context (Holds info about ID, lock, root node etc).
173 extern struct mgmt_ds_ctx
*mgmt_ds_get_ctx_by_id(struct mgmt_master
*mm
,
174 Mgmtd__DatastoreId ds_id
);
177 * Check if a given datastore is config ds
179 extern bool mgmt_ds_is_config(struct mgmt_ds_ctx
*ds_ctx
);
182 * Acquire read lock to a ds given a ds_handle
184 extern int mgmt_ds_read_lock(struct mgmt_ds_ctx
*ds_ctx
);
187 * Acquire write lock to a ds given a ds_handle
189 extern int mgmt_ds_write_lock(struct mgmt_ds_ctx
*ds_ctx
);
192 * Remove a lock from ds given a ds_handle
194 extern int mgmt_ds_unlock(struct mgmt_ds_ctx
*ds_ctx
);
197 * Copy from source to destination datastore.
200 * Source datastore handle (ds to be copied from).
203 * Destination datastore handle (ds to be copied to).
206 * TRUE if need to update commit record, FALSE otherwise.
209 * 0 on success, -1 on failure.
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
);
216 * Fetch northbound configuration for a given datastore context.
218 extern struct nb_config
*mgmt_ds_get_nb_config(struct mgmt_ds_ctx
*ds_ctx
);
221 * Lookup YANG data nodes.
230 * Out param - array of YANG data xpaths.
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
240 * TRUE if child nodes needs to be fetched as well, FALSE otherwise.
243 * TRUE if the caller is interested in getting a copy of the xpath.
246 * 0 on success, -1 on failure.
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
,
254 * Find YANG data node given a datastore handle YANG xpath.
256 extern struct lyd_node
*
257 mgmt_ds_find_data_node_by_xpath(struct mgmt_ds_ctx
*ds_ctx
,
261 * Delete YANG data node given a datastore handle and YANG xpath.
263 extern int mgmt_ds_delete_data_nodes(struct mgmt_ds_ctx
*ds_ctx
,
267 * Iterate over datastore data.
273 * Base YANG xpath from where needs to be iterated.
276 * function that will be called during each iteration.
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
285 * TRUE if the caller is interested in getting a copy of the xpath.
288 * 0 on success, -1 on failure.
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
);
298 * Load config to datastore from a file.
304 * File path of the configuration file.
307 * TRUE if you want to merge with existing config,
308 * FALSE if you want to replace with existing config
311 * 0 on success, -1 on failure.
313 extern int mgmt_ds_load_config_from_file(struct mgmt_ds_ctx
*ds_ctx
,
314 const char *file_path
, bool merge
);
317 * Dump the data tree to a file with JSON/XML format.
326 * Base YANG xpath from where data needs to be dumped.
329 * File pointer to where data to be dumped.
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
);
338 * Dump the complete data tree to a file with JSON format.
341 * File path to where data to be dumped.
347 * 0 on success, -1 on failure.
349 extern int mgmt_ds_dump_ds_to_file(char *file_name
,
350 struct mgmt_ds_ctx
*ds_ctx
);
353 * Dump information about specific datastore.
355 extern void mgmt_ds_status_write_one(struct vty
*vty
,
356 struct mgmt_ds_ctx
*ds_ctx
);
359 * Dump information about all the datastores.
361 extern void mgmt_ds_status_write(struct vty
*vty
);
365 * Reset the candidate DS to empty state
367 void mgmt_ds_reset_candidate(void);
369 #endif /* _FRR_MGMTD_DS_H_ */