Line | Count | Source |
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 | 0 | { |
56 | 0 | if (id > MGMTD_DS_MAX_ID) |
57 | 0 | id = MGMTD_DS_MAX_ID; |
58 | 0 | return mgmt_ds_names[id]; |
59 | 0 | } |
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 | 0 | { |
72 | 0 | Mgmtd__DatastoreId id; |
73 | 0 |
|
74 | 0 | FOREACH_MGMTD_DS_ID (id) { |
75 | 0 | if (!strncmp(mgmt_ds_names[id], name, MGMTD_DS_NAME_MAX_LEN)) |
76 | 0 | return id; |
77 | 0 | } |
78 | 0 |
|
79 | 0 | return MGMTD_DS_NONE; |
80 | 0 | } |
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 | 0 | { |
89 | 0 | if (!strncmp(ds_name, "candidate", sizeof("candidate"))) |
90 | 0 | return MGMTD_DS_CANDIDATE; |
91 | 0 | else if (!strncmp(ds_name, "running", sizeof("running"))) |
92 | 0 | return MGMTD_DS_RUNNING; |
93 | 0 | else if (!strncmp(ds_name, "operational", sizeof("operational"))) |
94 | 0 | return MGMTD_DS_OPERATIONAL; |
95 | 0 | return MGMTD_DS_NONE; |
96 | 0 | } |
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 | 0 | { |
110 | 0 | if (!xpath || !xpath_len) |
111 | 0 | return; |
112 | 0 |
|
113 | 0 | if (!*xpath_len) |
114 | 0 | *xpath_len = strlen(xpath); |
115 | 0 |
|
116 | 0 | if (*xpath_len > 2 && *xpath_len < MGMTD_MAX_XPATH_LEN - 2) { |
117 | 0 | if (xpath[*xpath_len - 1] == '/') { |
118 | 0 | xpath[*xpath_len] = '*'; |
119 | 0 | xpath[*xpath_len + 1] = 0; |
120 | 0 | (*xpath_len)++; |
121 | 0 | } else if (xpath[*xpath_len - 1] != '*') { |
122 | 0 | xpath[*xpath_len] = '/'; |
123 | 0 | xpath[*xpath_len + 1] = '*'; |
124 | 0 | xpath[*xpath_len + 2] = 0; |
125 | 0 | (*xpath_len) += 2; |
126 | 0 | } |
127 | 0 | } |
128 | 0 | } |
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 | 0 | { |
142 | 0 | if (!xpath || !xpath_len) |
143 | 0 | return; |
144 | 0 |
|
145 | 0 | if (!*xpath_len) |
146 | 0 | *xpath_len = strlen(xpath); |
147 | 0 |
|
148 | 0 | if (*xpath_len > 2 && xpath[*xpath_len - 2] == '/' |
149 | 0 | && xpath[*xpath_len - 1] == '*') { |
150 | 0 | xpath[*xpath_len - 2] = 0; |
151 | 0 | (*xpath_len) -= 2; |
152 | 0 | } |
153 | 0 | } |
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 | | * Check if a given datastore is locked by a given session |
183 | | */ |
184 | | extern bool mgmt_ds_is_locked(struct mgmt_ds_ctx *ds_ctx, uint64_t session_id); |
185 | | |
186 | | /* |
187 | | * Acquire write lock to a ds given a ds_handle |
188 | | */ |
189 | | extern int mgmt_ds_lock(struct mgmt_ds_ctx *ds_ctx, uint64_t session_id); |
190 | | |
191 | | /* |
192 | | * Remove a lock from ds given a ds_handle |
193 | | */ |
194 | | extern void 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_id |
237 | | * Datastore ID.. |
238 | | * |
239 | | * root |
240 | | * The root of the tree to iterate over. |
241 | | * |
242 | | * base_xpath |
243 | | * Base YANG xpath from where needs to be iterated. |
244 | | * |
245 | | * iter_fn |
246 | | * function that will be called during each iteration. |
247 | | * |
248 | | * ctx |
249 | | * User defined opaque value normally used to pass |
250 | | * reference to some user private context that will |
251 | | * be passed to the iterator function provided in |
252 | | * 'iter_fn'. |
253 | | * |
254 | | * Returns: |
255 | | * 0 on success, -1 on failure. |
256 | | */ |
257 | | extern int mgmt_ds_iter_data( |
258 | | Mgmtd__DatastoreId ds_id, struct nb_config *root, |
259 | | const char *base_xpath, |
260 | | void (*mgmt_ds_node_iter_fn)(const char *xpath, struct lyd_node *node, |
261 | | struct nb_node *nb_node, void *ctx), |
262 | | void *ctx); |
263 | | |
264 | | /* |
265 | | * Load config to datastore from a file. |
266 | | * |
267 | | * ds_ctx |
268 | | * Datastore context. |
269 | | * |
270 | | * file_path |
271 | | * File path of the configuration file. |
272 | | * |
273 | | * merge |
274 | | * TRUE if you want to merge with existing config, |
275 | | * FALSE if you want to replace with existing config |
276 | | * |
277 | | * Returns: |
278 | | * 0 on success, -1 on failure. |
279 | | */ |
280 | | extern int mgmt_ds_load_config_from_file(struct mgmt_ds_ctx *ds_ctx, |
281 | | const char *file_path, bool merge); |
282 | | |
283 | | /* |
284 | | * Dump the data tree to a file with JSON/XML format. |
285 | | * |
286 | | * vty |
287 | | * VTY context. |
288 | | * |
289 | | * ds_ctx |
290 | | * Datastore context. |
291 | | * |
292 | | * xpath |
293 | | * Base YANG xpath from where data needs to be dumped. |
294 | | * |
295 | | * f |
296 | | * File pointer to where data to be dumped. |
297 | | * |
298 | | * format |
299 | | * JSON/XML |
300 | | */ |
301 | | extern void mgmt_ds_dump_tree(struct vty *vty, struct mgmt_ds_ctx *ds_ctx, |
302 | | const char *xpath, FILE *f, LYD_FORMAT format); |
303 | | |
304 | | /* |
305 | | * Dump the complete data tree to a file with JSON format. |
306 | | * |
307 | | * file_name |
308 | | * File path to where data to be dumped. |
309 | | * |
310 | | * ds |
311 | | * Datastore context. |
312 | | * |
313 | | * Returns: |
314 | | * 0 on success, -1 on failure. |
315 | | */ |
316 | | extern int mgmt_ds_dump_ds_to_file(char *file_name, |
317 | | struct mgmt_ds_ctx *ds_ctx); |
318 | | |
319 | | /* |
320 | | * Dump information about specific datastore. |
321 | | */ |
322 | | extern void mgmt_ds_status_write_one(struct vty *vty, |
323 | | struct mgmt_ds_ctx *ds_ctx); |
324 | | |
325 | | /* |
326 | | * Dump information about all the datastores. |
327 | | */ |
328 | | extern void mgmt_ds_status_write(struct vty *vty); |
329 | | |
330 | | |
331 | | /* |
332 | | * Reset the candidate DS to empty state |
333 | | */ |
334 | | void mgmt_ds_reset_candidate(void); |
335 | | |
336 | | #endif /* _FRR_MGMTD_DS_H_ */ |