]>
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 "northbound.h"
14 #include "mgmtd/mgmt_defines.h"
16 #define MGMTD_MAX_NUM_DSNODES_PER_BATCH 128
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"
24 #define MGMTD_STARTUP_DS_FILE_PATH DAEMON_DB_DIR "/frr_startup.json"
26 #define FOREACH_MGMTD_DS_ID(id) \
27 for ((id) = MGMTD_DS_NONE; (id) < MGMTD_DS_MAX_ID; (id)++)
29 #define MGMTD_MAX_COMMIT_LIST 10
30 #define MGMTD_MD5_HASH_LEN 16
31 #define MGMTD_MD5_HASH_STR_HEX_LEN 33
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
39 extern struct nb_config
*running_config
;
44 * Datastore-Id: For now defined here. Eventually will be
45 * defined as part of MGMTD Front-End interface.
47 enum mgmt_datastore_id
{
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
);
59 /***************************************************************
60 * Global data exported
61 ***************************************************************/
63 extern const char *mgmt_ds_names
[MGMTD_DS_MAX_ID
+ 1];
66 * Convert datastore ID to datastore name.
74 static inline const char *mgmt_ds_id2name(enum mgmt_datastore_id id
)
76 if (id
> MGMTD_DS_MAX_ID
)
78 return mgmt_ds_names
[id
];
82 * Convert datastore name to datastore ID.
90 static inline enum mgmt_datastore_id
mgmt_ds_name2id(const char *name
)
92 enum mgmt_datastore_id id
;
94 FOREACH_MGMTD_DS_ID (id
) {
95 if (!strncmp(mgmt_ds_names
[id
], name
, MGMTD_DS_NAME_MAX_LEN
))
103 * Convert datastore ID to datastore name.
105 * similar to above funtion.
107 static inline enum mgmt_datastore_id
mgmt_get_ds_id_by_name(const char *ds_name
)
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
;
119 * Appends trail wildcard '/' '*' to a given xpath.
127 static inline void mgmt_xpath_append_trail_wildcard(char *xpath
,
130 if (!xpath
|| !xpath_len
)
134 *xpath_len
= strlen(xpath
);
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;
141 } else if (xpath
[*xpath_len
- 1] != '*') {
142 xpath
[*xpath_len
] = '/';
143 xpath
[*xpath_len
+ 1] = '*';
144 xpath
[*xpath_len
+ 2] = 0;
151 * Removes trail wildcard '/' '*' from a given xpath.
159 static inline void mgmt_xpath_remove_trail_wildcard(char *xpath
,
162 if (!xpath
|| !xpath_len
)
166 *xpath_len
= strlen(xpath
);
168 if (*xpath_len
> 2 && xpath
[*xpath_len
- 2] == '/'
169 && xpath
[*xpath_len
- 1] == '*') {
170 xpath
[*xpath_len
- 2] = 0;
175 /* Initialise datastore */
176 extern int mgmt_ds_init(struct mgmt_master
*cm
);
178 /* Destroy datastore */
179 extern void mgmt_ds_destroy(void);
182 * Get datastore handler by ID
185 * Management master structure.
191 * Datastore context (Holds info about ID, lock, root node etc).
193 extern struct mgmt_ds_ctx
*mgmt_ds_get_ctx_by_id(struct mgmt_master
*mm
,
194 enum mgmt_datastore_id ds_id
);
197 * Check if a given datastore is config ds
199 extern bool mgmt_ds_is_config(struct mgmt_ds_ctx
*ds_ctx
);
202 * Acquire read lock to a ds given a ds_handle
204 extern int mgmt_ds_read_lock(struct mgmt_ds_ctx
*ds_ctx
);
207 * Acquire write lock to a ds given a ds_handle
209 extern int mgmt_ds_write_lock(struct mgmt_ds_ctx
*ds_ctx
);
212 * Remove a lock from ds given a ds_handle
214 extern int mgmt_ds_unlock(struct mgmt_ds_ctx
*ds_ctx
);
217 * Merge two datastores.
220 * Source datastore handle.
223 * Destination datastore handle.
226 * TRUE if need to update commit record, FALSE otherwise.
229 * 0 on success, -1 on failure.
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
);
236 * Copy from source to destination datastore.
239 * Source datastore handle (ds to be copied from).
242 * Destination datastore handle (ds to be copied to).
245 * TRUE if need to update commit record, FALSE otherwise.
248 * 0 on success, -1 on failure.
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
);
255 * Fetch northbound configuration for a given datastore context.
257 extern struct nb_config
*mgmt_ds_get_nb_config(struct mgmt_ds_ctx
*ds_ctx
);
260 * Lookup YANG data nodes.
269 * Out param - array of YANG data xpaths.
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
279 * TRUE if child nodes needs to be fetched as well, FALSE otherwise.
282 * TRUE if the caller is interested in getting a copy of the xpath.
285 * 0 on success, -1 on failure.
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
,
293 * Find YANG data node given a datastore handle YANG xpath.
295 extern struct lyd_node
*
296 mgmt_ds_find_data_node_by_xpath(struct mgmt_ds_ctx
*ds_ctx
,
300 * Delete YANG data node given a datastore handle and YANG xpath.
302 extern int mgmt_ds_delete_data_nodes(struct mgmt_ds_ctx
*ds_ctx
,
306 * Iterate over datastore data.
312 * Base YANG xpath from where needs to be iterated.
315 * function that will be called during each iteration.
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
324 * TRUE if the caller is interested in getting a copy of the xpath.
327 * 0 on success, -1 on failure.
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
);
337 * Load config to datastore from a file.
343 * File path of the configuration file.
346 * TRUE if you want to merge with existing config,
347 * FALSE if you want to replace with existing config
350 * 0 on success, -1 on failure.
352 extern int mgmt_ds_load_config_from_file(struct mgmt_ds_ctx
*ds_ctx
,
353 const char *file_path
, bool merge
);
356 * Dump the data tree to a file with JSON/XML format.
365 * Base YANG xpath from where data needs to be dumped.
368 * File pointer to where data to be dumped.
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
);
377 * Dump the complete data tree to a file with JSON format.
380 * File path to where data to be dumped.
386 * 0 on success, -1 on failure.
388 extern int mgmt_ds_dump_ds_to_file(char *file_name
,
389 struct mgmt_ds_ctx
*ds_ctx
);
392 * Dump information about specific datastore.
394 extern void mgmt_ds_status_write_one(struct vty
*vty
,
395 struct mgmt_ds_ctx
*ds_ctx
);
398 * Dump information about all the datastores.
400 extern void mgmt_ds_status_write(struct vty
*vty
);
402 #endif /* _FRR_MGMTD_DS_H_ */