summaryrefslogtreecommitdiffstats
path: root/mgmtd/mgmt_ds.h
blob: b8e77e330afeb062cc1c601d20604e369e8af1ec (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
// SPDX-License-Identifier: GPL-2.0-or-later
/*
 * MGMTD Datastores
 *
 * Copyright (C) 2021  Vmware, Inc.
 *		       Pushpasis Sarkar <spushpasis@vmware.com>
 */

#ifndef _FRR_MGMTD_DS_H_
#define _FRR_MGMTD_DS_H_

#include "mgmt_fe_client.h"
#include "northbound.h"
#include "mgmt_defines.h"

#include "mgmtd/mgmt_be_adapter.h"
#include "mgmtd/mgmt_fe_adapter.h"

#define MGMTD_MAX_NUM_DSNODES_PER_BATCH 128

#define MGMTD_DS_NAME_MAX_LEN 32
#define MGMTD_DS_NAME_NONE "none"
#define MGMTD_DS_NAME_RUNNING "running"
#define MGMTD_DS_NAME_CANDIDATE "candidate"
#define MGMTD_DS_NAME_OPERATIONAL "operational"

#define FOREACH_MGMTD_DS_ID(id)                                                \
	for ((id) = MGMTD_DS_NONE; (id) < MGMTD_DS_MAX_ID; (id)++)

#define MGMTD_MAX_COMMIT_LIST 10

#define MGMTD_COMMIT_FILE_PATH(id)   "%s/commit-%s.json", frr_libstatedir, id
#define MGMTD_COMMIT_INDEX_FILE_PATH "%s/commit-index.dat", frr_libstatedir

extern struct nb_config *running_config;

struct mgmt_ds_ctx;

/***************************************************************
 * Global data exported
 ***************************************************************/

extern const char *mgmt_ds_names[MGMTD_DS_MAX_ID + 1];

/*
 * Convert datastore ID to datastore name.
 *
 * id
 *    Datastore ID.
 *
 * Returns:
 *    Datastore name.
 */
static inline const char *mgmt_ds_id2name(Mgmtd__DatastoreId id)
{
	if (id > MGMTD_DS_MAX_ID)
		id = MGMTD_DS_MAX_ID;
	return mgmt_ds_names[id];
}

/*
 * Convert datastore name to datastore ID.
 *
 * id
 *    Datastore name.
 *
 * Returns:
 *    Datastore ID.
 */
static inline Mgmtd__DatastoreId mgmt_ds_name2id(const char *name)
{
	Mgmtd__DatastoreId id;

	FOREACH_MGMTD_DS_ID (id) {
		if (!strncmp(mgmt_ds_names[id], name, MGMTD_DS_NAME_MAX_LEN))
			return id;
	}

	return MGMTD_DS_NONE;
}

/*
 * Convert datastore ID to datastore name.
 *
 * similar to above funtion.
 */
static inline Mgmtd__DatastoreId mgmt_get_ds_id_by_name(const char *ds_name)
{
	if (!strncmp(ds_name, "candidate", sizeof("candidate")))
		return MGMTD_DS_CANDIDATE;
	else if (!strncmp(ds_name, "running", sizeof("running")))
		return MGMTD_DS_RUNNING;
	else if (!strncmp(ds_name, "operational", sizeof("operational")))
		return MGMTD_DS_OPERATIONAL;
	return MGMTD_DS_NONE;
}

/*
 * Appends trail wildcard '/' '*' to a given xpath.
 *
 * xpath
 *     YANG xpath.
 *
 * path_len
 *     xpath length.
 */
static inline void mgmt_xpath_append_trail_wildcard(char *xpath,
						    size_t *xpath_len)
{
	if (!xpath || !xpath_len)
		return;

	if (!*xpath_len)
		*xpath_len = strlen(xpath);

	if (*xpath_len > 2 && *xpath_len < MGMTD_MAX_XPATH_LEN - 2) {
		if (xpath[*xpath_len - 1] == '/') {
			xpath[*xpath_len] = '*';
			xpath[*xpath_len + 1] = 0;
			(*xpath_len)++;
		} else if (xpath[*xpath_len - 1] != '*') {
			xpath[*xpath_len] = '/';
			xpath[*xpath_len + 1] = '*';
			xpath[*xpath_len + 2] = 0;
			(*xpath_len) += 2;
		}
	}
}

/*
 * Removes trail wildcard '/' '*' from a given xpath.
 *
 * xpath
 *     YANG xpath.
 *
 * path_len
 *     xpath length.
 */
static inline void mgmt_xpath_remove_trail_wildcard(char *xpath,
						    size_t *xpath_len)
{
	if (!xpath || !xpath_len)
		return;

	if (!*xpath_len)
		*xpath_len = strlen(xpath);

	if (*xpath_len > 2 && xpath[*xpath_len - 2] == '/'
	    && xpath[*xpath_len - 1] == '*') {
		xpath[*xpath_len - 2] = 0;
		(*xpath_len) -= 2;
	}
}

/* Initialise datastore */
extern int mgmt_ds_init(struct mgmt_master *cm);

/* Destroy datastore */
extern void mgmt_ds_destroy(void);

/*
 * Get datastore handler by ID
 *
 * mm
 *    Management master structure.
 *
 * ds_id
 *    Datastore ID.
 *
 * Returns:
 *    Datastore context (Holds info about ID, lock, root node etc).
 */
extern struct mgmt_ds_ctx *mgmt_ds_get_ctx_by_id(struct mgmt_master *mm,
						   Mgmtd__DatastoreId ds_id);

/*
 * Check if a given datastore is config ds
 */
extern bool mgmt_ds_is_config(struct mgmt_ds_ctx *ds_ctx);

/*
 * Check if a given datastore is locked by a given session
 */
extern bool mgmt_ds_is_locked(struct mgmt_ds_ctx *ds_ctx, uint64_t session_id);

/*
 * Acquire write lock to a ds given a ds_handle
 */
extern int mgmt_ds_lock(struct mgmt_ds_ctx *ds_ctx, uint64_t session_id);

/*
 * Remove a lock from ds given a ds_handle
 */
extern void mgmt_ds_unlock(struct mgmt_ds_ctx *ds_ctx);

/*
 * Copy from source to destination datastore.
 *
 * src_ds
 *    Source datastore handle (ds to be copied from).
 *
 * dst_ds
 *    Destination datastore handle (ds to be copied to).
 *
 * update_cmd_rec
 *    TRUE if need to update commit record, FALSE otherwise.
 *
 * Returns:
 *    0 on success, -1 on failure.
 */
extern int mgmt_ds_copy_dss(struct mgmt_ds_ctx *src_ds_ctx,
			    struct mgmt_ds_ctx *dst_ds_ctx,
			    bool update_cmt_rec);

/*
 * Fetch northbound configuration for a given datastore context.
 */
extern struct nb_config *mgmt_ds_get_nb_config(struct mgmt_ds_ctx *ds_ctx);

/*
 * Find YANG data node given a datastore handle YANG xpath.
 */
extern struct lyd_node *
mgmt_ds_find_data_node_by_xpath(struct mgmt_ds_ctx *ds_ctx,
				const char *xpath);

/*
 * Delete YANG data node given a datastore handle and YANG xpath.
 */
extern int mgmt_ds_delete_data_nodes(struct mgmt_ds_ctx *ds_ctx,
				     const char *xpath);

/*
 * Iterate over datastore data.
 *
 * ds_id
 *    Datastore ID..
 *
 * root
 *    The root of the tree to iterate over.
 *
 * base_xpath
 *    Base YANG xpath from where needs to be iterated.
 *
 * iter_fn
 *    function that will be called during each iteration.
 *
 * ctx
 *    User defined opaque value normally used to pass
 *    reference to some user private context that will
 *    be passed to the iterator function provided in
 *    'iter_fn'.
 *
 * Returns:
 *    0 on success, -1 on failure.
 */
extern int mgmt_ds_iter_data(
	Mgmtd__DatastoreId ds_id, struct nb_config *root,
	const char *base_xpath,
	void (*mgmt_ds_node_iter_fn)(const char *xpath, struct lyd_node *node,
				     struct nb_node *nb_node, void *ctx),
	void *ctx);

/*
 * Load config to datastore from a file.
 *
 * ds_ctx
 *    Datastore context.
 *
 * file_path
 *    File path of the configuration file.
 *
 * merge
 *    TRUE if you want to merge with existing config,
 *    FALSE if you want to replace with existing config
 *
 * Returns:
 *    0 on success, -1 on failure.
 */
extern int mgmt_ds_load_config_from_file(struct mgmt_ds_ctx *ds_ctx,
					 const char *file_path, bool merge);

/*
 * Dump the data tree to a file with JSON/XML format.
 *
 * vty
 *    VTY context.
 *
 * ds_ctx
 *    Datastore context.
 *
 * xpath
 *    Base YANG xpath from where data needs to be dumped.
 *
 * f
 *    File pointer to where data to be dumped.
 *
 * format
 *    JSON/XML
 */
extern void mgmt_ds_dump_tree(struct vty *vty, struct mgmt_ds_ctx *ds_ctx,
			      const char *xpath, FILE *f, LYD_FORMAT format);

/*
 * Dump the complete data tree to a file with JSON format.
 *
 * file_name
 *    File path to where data to be dumped.
 *
 * ds
 *    Datastore context.
 *
 * Returns:
 *    0 on success, -1 on failure.
 */
extern int mgmt_ds_dump_ds_to_file(char *file_name,
				   struct mgmt_ds_ctx *ds_ctx);

/*
 * Dump information about specific datastore.
 */
extern void mgmt_ds_status_write_one(struct vty *vty,
				     struct mgmt_ds_ctx *ds_ctx);

/*
 * Dump information about all the datastores.
 */
extern void mgmt_ds_status_write(struct vty *vty);


/*
 * Reset the candidate DS to empty state
 */
void mgmt_ds_reset_candidate(void);

#endif /* _FRR_MGMTD_DS_H_ */