]>
Commit | Line | Data |
---|---|---|
1c2facd1 RW |
1 | /* |
2 | * Copyright (C) 2018 NetDEF, Inc. | |
3 | * Renato Westphal | |
4 | * | |
5 | * This program is free software; you can redistribute it and/or modify it | |
6 | * under the terms of the GNU General Public License as published by the Free | |
7 | * Software Foundation; either version 2 of the License, or (at your option) | |
8 | * any later version. | |
9 | * | |
10 | * This program is distributed in the hope that it will be useful, but WITHOUT | |
11 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
12 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for | |
13 | * more details. | |
14 | * | |
15 | * You should have received a copy of the GNU General Public License along | |
16 | * with this program; see the file COPYING; if not, write to the Free Software | |
17 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA | |
18 | */ | |
19 | ||
20 | #ifndef _FRR_NORTHBOUND_H_ | |
21 | #define _FRR_NORTHBOUND_H_ | |
22 | ||
fbdc1c0a | 23 | #include "thread.h" |
1c2facd1 | 24 | #include "hook.h" |
1c2facd1 RW |
25 | #include "linklist.h" |
26 | #include "openbsd-tree.h" | |
1a4bc045 RW |
27 | #include "yang.h" |
28 | #include "yang_translator.h" | |
1c2facd1 | 29 | |
5e244469 RW |
30 | #ifdef __cplusplus |
31 | extern "C" { | |
32 | #endif | |
33 | ||
1c2facd1 RW |
34 | /* Forward declaration(s). */ |
35 | struct vty; | |
9eb2c0a1 | 36 | struct debug; |
1c2facd1 RW |
37 | |
38 | /* Northbound events. */ | |
39 | enum nb_event { | |
40 | /* | |
41 | * The configuration callback is supposed to verify that the changes are | |
42 | * valid and can be applied. | |
43 | */ | |
44 | NB_EV_VALIDATE, | |
45 | ||
46 | /* | |
47 | * The configuration callback is supposed to prepare all resources | |
48 | * required to apply the changes. | |
49 | */ | |
50 | NB_EV_PREPARE, | |
51 | ||
52 | /* | |
53 | * Transaction has failed, the configuration callback needs to release | |
54 | * all resources previously allocated. | |
55 | */ | |
56 | NB_EV_ABORT, | |
57 | ||
58 | /* | |
59 | * The configuration changes need to be applied. The changes can't be | |
60 | * rejected at this point (errors are logged and ignored). | |
61 | */ | |
62 | NB_EV_APPLY, | |
63 | }; | |
64 | ||
65 | /* | |
66 | * Northbound operations. | |
67 | * | |
68 | * Refer to the documentation comments of nb_callbacks for more details. | |
69 | */ | |
70 | enum nb_operation { | |
71 | NB_OP_CREATE, | |
72 | NB_OP_MODIFY, | |
95ce849b | 73 | NB_OP_DESTROY, |
1c2facd1 RW |
74 | NB_OP_MOVE, |
75 | NB_OP_APPLY_FINISH, | |
76 | NB_OP_GET_ELEM, | |
77 | NB_OP_GET_NEXT, | |
78 | NB_OP_GET_KEYS, | |
79 | NB_OP_LOOKUP_ENTRY, | |
80 | NB_OP_RPC, | |
81 | }; | |
82 | ||
83 | union nb_resource { | |
84 | int fd; | |
85 | void *ptr; | |
86 | }; | |
87 | ||
88 | struct nb_callbacks { | |
89 | /* | |
90 | * Configuration callback. | |
91 | * | |
92 | * A presence container, list entry, leaf-list entry or leaf of type | |
93 | * empty has been created. | |
94 | * | |
95 | * For presence-containers and list entries, the callback is supposed to | |
96 | * initialize the default values of its children (if any) from the YANG | |
97 | * models. | |
98 | * | |
99 | * event | |
100 | * The transaction phase. Refer to the documentation comments of | |
101 | * nb_event for more details. | |
102 | * | |
103 | * dnode | |
104 | * libyang data node that is being created. | |
105 | * | |
106 | * resource | |
107 | * Pointer to store resource(s) allocated during the NB_EV_PREPARE | |
108 | * phase. The same pointer can be used during the NB_EV_ABORT and | |
109 | * NB_EV_APPLY phases to either release or make use of the allocated | |
110 | * resource(s). It's set to NULL when the event is NB_EV_VALIDATE. | |
111 | * | |
112 | * Returns: | |
113 | * - NB_OK on success. | |
114 | * - NB_ERR_VALIDATION when a validation error occurred. | |
115 | * - NB_ERR_RESOURCE when the callback failed to allocate a resource. | |
116 | * - NB_ERR_INCONSISTENCY when an inconsistency was detected. | |
117 | * - NB_ERR for other errors. | |
118 | */ | |
119 | int (*create)(enum nb_event event, const struct lyd_node *dnode, | |
120 | union nb_resource *resource); | |
121 | ||
122 | /* | |
123 | * Configuration callback. | |
124 | * | |
125 | * The value of a leaf has been modified. | |
126 | * | |
127 | * List keys don't need to implement this callback. When a list key is | |
128 | * modified, the northbound treats this as if the list was deleted and a | |
129 | * new one created with the updated key value. | |
130 | * | |
131 | * event | |
132 | * The transaction phase. Refer to the documentation comments of | |
133 | * nb_event for more details. | |
134 | * | |
135 | * dnode | |
136 | * libyang data node that is being modified | |
137 | * | |
138 | * resource | |
139 | * Pointer to store resource(s) allocated during the NB_EV_PREPARE | |
140 | * phase. The same pointer can be used during the NB_EV_ABORT and | |
141 | * NB_EV_APPLY phases to either release or make use of the allocated | |
142 | * resource(s). It's set to NULL when the event is NB_EV_VALIDATE. | |
143 | * | |
144 | * Returns: | |
145 | * - NB_OK on success. | |
146 | * - NB_ERR_VALIDATION when a validation error occurred. | |
147 | * - NB_ERR_RESOURCE when the callback failed to allocate a resource. | |
148 | * - NB_ERR_INCONSISTENCY when an inconsistency was detected. | |
149 | * - NB_ERR for other errors. | |
150 | */ | |
151 | int (*modify)(enum nb_event event, const struct lyd_node *dnode, | |
152 | union nb_resource *resource); | |
153 | ||
154 | /* | |
155 | * Configuration callback. | |
156 | * | |
157 | * A presence container, list entry, leaf-list entry or optional leaf | |
158 | * has been deleted. | |
159 | * | |
160 | * The callback is supposed to delete the entire configuration object, | |
161 | * including its children when they exist. | |
162 | * | |
163 | * event | |
164 | * The transaction phase. Refer to the documentation comments of | |
165 | * nb_event for more details. | |
166 | * | |
167 | * dnode | |
168 | * libyang data node that is being deleted. | |
169 | * | |
170 | * Returns: | |
171 | * - NB_OK on success. | |
172 | * - NB_ERR_VALIDATION when a validation error occurred. | |
173 | * - NB_ERR_INCONSISTENCY when an inconsistency was detected. | |
174 | * - NB_ERR for other errors. | |
175 | */ | |
d01b92fd | 176 | int (*destroy)(enum nb_event event, const struct lyd_node *dnode); |
1c2facd1 RW |
177 | |
178 | /* | |
179 | * Configuration callback. | |
180 | * | |
181 | * A list entry or leaf-list entry has been moved. Only applicable when | |
182 | * the "ordered-by user" statement is present. | |
183 | * | |
184 | * event | |
185 | * The transaction phase. Refer to the documentation comments of | |
186 | * nb_event for more details. | |
187 | * | |
188 | * dnode | |
189 | * libyang data node that is being moved. | |
190 | * | |
191 | * Returns: | |
192 | * - NB_OK on success. | |
193 | * - NB_ERR_VALIDATION when a validation error occurred. | |
194 | * - NB_ERR_INCONSISTENCY when an inconsistency was detected. | |
195 | * - NB_ERR for other errors. | |
196 | */ | |
197 | int (*move)(enum nb_event event, const struct lyd_node *dnode); | |
198 | ||
199 | /* | |
200 | * Optional configuration callback. | |
201 | * | |
202 | * The 'apply_finish' callbacks are called after all other callbacks | |
203 | * during the apply phase (NB_EV_APPLY). These callbacks are called only | |
204 | * under one of the following two cases: | |
205 | * - The data node has been created or modified (but not deleted); | |
206 | * - Any change was made within the descendants of the data node (e.g. a | |
207 | * child leaf was modified, created or deleted). | |
208 | * | |
209 | * In the second case above, the 'apply_finish' callback is called only | |
210 | * once even if multiple changes occurred within the descendants of the | |
211 | * data node. | |
212 | * | |
213 | * dnode | |
214 | * libyang data node associated with the 'apply_finish' callback. | |
215 | */ | |
216 | void (*apply_finish)(const struct lyd_node *dnode); | |
217 | ||
218 | /* | |
219 | * Operational data callback. | |
220 | * | |
1a4bc045 RW |
221 | * The callback function should return the value of a specific leaf, |
222 | * leaf-list entry or inform if a typeless value (presence containers or | |
223 | * leafs of type empty) exists or not. | |
1c2facd1 RW |
224 | * |
225 | * xpath | |
226 | * YANG data path of the data we want to get. | |
227 | * | |
228 | * list_entry | |
1a4bc045 | 229 | * Pointer to list entry (might be NULL). |
1c2facd1 RW |
230 | * |
231 | * Returns: | |
232 | * Pointer to newly created yang_data structure, or NULL to indicate | |
233 | * the absence of data. | |
234 | */ | |
235 | struct yang_data *(*get_elem)(const char *xpath, | |
236 | const void *list_entry); | |
237 | ||
238 | /* | |
1a4bc045 | 239 | * Operational data callback for YANG lists and leaf-lists. |
1c2facd1 | 240 | * |
1a4bc045 RW |
241 | * The callback function should return the next entry in the list or |
242 | * leaf-list. The 'list_entry' parameter will be NULL on the first | |
243 | * invocation. | |
1c2facd1 | 244 | * |
1a4bc045 RW |
245 | * parent_list_entry |
246 | * Pointer to parent list entry. | |
1c2facd1 RW |
247 | * |
248 | * list_entry | |
1a4bc045 | 249 | * Pointer to (leaf-)list entry. |
1c2facd1 RW |
250 | * |
251 | * Returns: | |
1a4bc045 RW |
252 | * Pointer to the next entry in the (leaf-)list, or NULL to signal |
253 | * that the end of the (leaf-)list was reached. | |
1c2facd1 | 254 | */ |
1a4bc045 RW |
255 | const void *(*get_next)(const void *parent_list_entry, |
256 | const void *list_entry); | |
1c2facd1 RW |
257 | |
258 | /* | |
259 | * Operational data callback for YANG lists. | |
260 | * | |
261 | * The callback function should fill the 'keys' parameter based on the | |
99fb518f RW |
262 | * given list_entry. Keyless lists don't need to implement this |
263 | * callback. | |
1c2facd1 RW |
264 | * |
265 | * list_entry | |
266 | * Pointer to list entry. | |
267 | * | |
268 | * keys | |
269 | * Structure to be filled based on the attributes of the provided | |
270 | * list entry. | |
271 | * | |
272 | * Returns: | |
273 | * NB_OK on success, NB_ERR otherwise. | |
274 | */ | |
275 | int (*get_keys)(const void *list_entry, struct yang_list_keys *keys); | |
276 | ||
277 | /* | |
278 | * Operational data callback for YANG lists. | |
279 | * | |
280 | * The callback function should return a list entry based on the list | |
99fb518f RW |
281 | * keys given as a parameter. Keyless lists don't need to implement this |
282 | * callback. | |
1c2facd1 | 283 | * |
1a4bc045 RW |
284 | * parent_list_entry |
285 | * Pointer to parent list entry. | |
286 | * | |
1c2facd1 RW |
287 | * keys |
288 | * Structure containing the keys of the list entry. | |
289 | * | |
290 | * Returns: | |
291 | * Pointer to the list entry if found, or NULL if not found. | |
292 | */ | |
1a4bc045 RW |
293 | const void *(*lookup_entry)(const void *parent_list_entry, |
294 | const struct yang_list_keys *keys); | |
1c2facd1 RW |
295 | |
296 | /* | |
297 | * RPC and action callback. | |
298 | * | |
299 | * Both 'input' and 'output' are lists of 'yang_data' structures. The | |
300 | * callback should fetch all the input parameters from the 'input' list, | |
301 | * and add output parameters to the 'output' list if necessary. | |
302 | * | |
303 | * xpath | |
304 | * XPath of the YANG RPC or action. | |
305 | * | |
306 | * input | |
307 | * Read-only list of input parameters. | |
308 | * | |
309 | * output | |
310 | * List of output parameters to be populated by the callback. | |
311 | * | |
312 | * Returns: | |
313 | * NB_OK on success, NB_ERR otherwise. | |
314 | */ | |
315 | int (*rpc)(const char *xpath, const struct list *input, | |
316 | struct list *output); | |
317 | ||
318 | /* | |
319 | * Optional callback to show the CLI command associated to the given | |
320 | * YANG data node. | |
321 | * | |
322 | * vty | |
323 | * The vty terminal to dump the configuration to. | |
324 | * | |
325 | * dnode | |
326 | * libyang data node that should be shown in the form of a CLI | |
327 | * command. | |
328 | * | |
329 | * show_defaults | |
330 | * Specify whether to display default configuration values or not. | |
331 | * This parameter can be ignored most of the time since the | |
332 | * northbound doesn't call this callback for default leaves or | |
333 | * non-presence containers that contain only default child nodes. | |
334 | * The exception are commands associated to multiple configuration | |
335 | * nodes, in which case it might be desirable to hide one or more | |
336 | * parts of the command when this parameter is set to false. | |
337 | */ | |
338 | void (*cli_show)(struct vty *vty, struct lyd_node *dnode, | |
339 | bool show_defaults); | |
340 | }; | |
341 | ||
342 | /* | |
343 | * Northbound-specific data that is allocated for each schema node of the native | |
344 | * YANG modules. | |
345 | */ | |
346 | struct nb_node { | |
347 | /* Back pointer to the libyang schema node. */ | |
348 | const struct lys_node *snode; | |
349 | ||
350 | /* Data path of this YANG node. */ | |
351 | char xpath[XPATH_MAXLEN]; | |
352 | ||
353 | /* Priority - lower priorities are processed first. */ | |
354 | uint32_t priority; | |
355 | ||
356 | /* Callbacks implemented for this node. */ | |
357 | struct nb_callbacks cbs; | |
358 | ||
359 | /* | |
360 | * Pointer to the parent node (disconsidering non-presence containers). | |
361 | */ | |
362 | struct nb_node *parent; | |
363 | ||
364 | /* Pointer to the nearest parent list, if any. */ | |
365 | struct nb_node *parent_list; | |
366 | ||
544ca69a RW |
367 | /* Flags. */ |
368 | uint8_t flags; | |
369 | ||
1c2facd1 RW |
370 | #ifdef HAVE_CONFD |
371 | /* ConfD hash value corresponding to this YANG path. */ | |
372 | int confd_hash; | |
373 | #endif | |
374 | }; | |
544ca69a RW |
375 | /* The YANG container or list contains only config data. */ |
376 | #define F_NB_NODE_CONFIG_ONLY 0x01 | |
99fb518f RW |
377 | /* The YANG list doesn't contain key leafs. */ |
378 | #define F_NB_NODE_KEYLESS_LIST 0x02 | |
1c2facd1 RW |
379 | |
380 | struct frr_yang_module_info { | |
381 | /* YANG module name. */ | |
382 | const char *name; | |
383 | ||
384 | /* Northbound callbacks. */ | |
385 | const struct { | |
386 | /* Data path of this YANG node. */ | |
387 | const char *xpath; | |
388 | ||
389 | /* Callbacks implemented for this node. */ | |
390 | struct nb_callbacks cbs; | |
391 | ||
392 | /* Priority - lower priorities are processed first. */ | |
393 | uint32_t priority; | |
394 | } nodes[]; | |
395 | }; | |
396 | ||
397 | /* Northbound error codes. */ | |
398 | enum nb_error { | |
399 | NB_OK = 0, | |
400 | NB_ERR, | |
401 | NB_ERR_NO_CHANGES, | |
402 | NB_ERR_NOT_FOUND, | |
403 | NB_ERR_LOCKED, | |
404 | NB_ERR_VALIDATION, | |
405 | NB_ERR_RESOURCE, | |
406 | NB_ERR_INCONSISTENCY, | |
407 | }; | |
408 | ||
409 | /* Default priority. */ | |
410 | #define NB_DFLT_PRIORITY (UINT32_MAX / 2) | |
411 | ||
412 | /* Default maximum of configuration rollbacks to store. */ | |
413 | #define NB_DLFT_MAX_CONFIG_ROLLBACKS 20 | |
414 | ||
415 | /* Northbound clients. */ | |
416 | enum nb_client { | |
364ad673 RW |
417 | NB_CLIENT_NONE = 0, |
418 | NB_CLIENT_CLI, | |
5bce33b3 | 419 | NB_CLIENT_CONFD, |
a7ca2199 | 420 | NB_CLIENT_SYSREPO, |
ec2ac5f2 | 421 | NB_CLIENT_GRPC, |
1c2facd1 RW |
422 | }; |
423 | ||
424 | /* Northbound configuration. */ | |
425 | struct nb_config { | |
83981138 | 426 | /* Configuration data. */ |
1c2facd1 | 427 | struct lyd_node *dnode; |
83981138 RW |
428 | |
429 | /* Configuration version. */ | |
1c2facd1 | 430 | uint32_t version; |
83981138 RW |
431 | |
432 | /* | |
433 | * Lock protecting this structure. The use of this lock is always | |
434 | * necessary when reading or modifying the global running configuration. | |
435 | * For candidate configurations, use of this lock is optional depending | |
436 | * on the threading scheme of the northbound plugin. | |
437 | */ | |
438 | pthread_rwlock_t lock; | |
1c2facd1 RW |
439 | }; |
440 | ||
441 | /* Northbound configuration callback. */ | |
442 | struct nb_config_cb { | |
443 | RB_ENTRY(nb_config_cb) entry; | |
444 | enum nb_operation operation; | |
445 | char xpath[XPATH_MAXLEN]; | |
446 | const struct nb_node *nb_node; | |
447 | const struct lyd_node *dnode; | |
448 | }; | |
449 | RB_HEAD(nb_config_cbs, nb_config_cb); | |
450 | RB_PROTOTYPE(nb_config_cbs, nb_config_cb, entry, nb_config_cb_compare); | |
451 | ||
452 | /* Northbound configuration change. */ | |
453 | struct nb_config_change { | |
454 | struct nb_config_cb cb; | |
455 | union nb_resource resource; | |
456 | bool prepare_ok; | |
457 | }; | |
458 | ||
459 | /* Northbound configuration transaction. */ | |
460 | struct nb_transaction { | |
461 | enum nb_client client; | |
462 | char comment[80]; | |
463 | struct nb_config *config; | |
464 | struct nb_config_cbs changes; | |
465 | }; | |
466 | ||
1a4bc045 RW |
467 | /* Callback function used by nb_oper_data_iterate(). */ |
468 | typedef int (*nb_oper_data_cb)(const struct lys_node *snode, | |
469 | struct yang_translator *translator, | |
470 | struct yang_data *data, void *arg); | |
471 | ||
472 | /* Iterate over direct child nodes only. */ | |
473 | #define NB_OPER_DATA_ITER_NORECURSE 0x0001 | |
474 | ||
9eb2c0a1 | 475 | /* Hooks. */ |
1c2facd1 RW |
476 | DECLARE_HOOK(nb_notification_send, (const char *xpath, struct list *arguments), |
477 | (xpath, arguments)) | |
9eb2c0a1 RW |
478 | DECLARE_HOOK(nb_client_debug_config_write, (struct vty *vty), (vty)) |
479 | DECLARE_HOOK(nb_client_debug_set_all, (uint32_t flags, bool set), (flags, set)) | |
1c2facd1 | 480 | |
9eb2c0a1 RW |
481 | /* Northbound debugging records */ |
482 | extern struct debug nb_dbg_cbs_config; | |
483 | extern struct debug nb_dbg_cbs_state; | |
484 | extern struct debug nb_dbg_cbs_rpc; | |
485 | extern struct debug nb_dbg_notif; | |
486 | extern struct debug nb_dbg_events; | |
487 | ||
488 | /* Global running configuration. */ | |
1c2facd1 RW |
489 | extern struct nb_config *running_config; |
490 | ||
9eb2c0a1 RW |
491 | /* Wrappers for the northbound callbacks. */ |
492 | extern struct yang_data *nb_callback_get_elem(const struct nb_node *nb_node, | |
493 | const char *xpath, | |
494 | const void *list_entry); | |
495 | extern const void *nb_callback_get_next(const struct nb_node *nb_node, | |
496 | const void *parent_list_entry, | |
497 | const void *list_entry); | |
498 | extern int nb_callback_get_keys(const struct nb_node *nb_node, | |
499 | const void *list_entry, | |
500 | struct yang_list_keys *keys); | |
501 | extern const void *nb_callback_lookup_entry(const struct nb_node *nb_node, | |
502 | const void *parent_list_entry, | |
503 | const struct yang_list_keys *keys); | |
504 | extern int nb_callback_rpc(const struct nb_node *nb_node, const char *xpath, | |
505 | const struct list *input, struct list *output); | |
506 | ||
544ca69a RW |
507 | /* |
508 | * Create a northbound node for all YANG schema nodes. | |
509 | */ | |
510 | void nb_nodes_create(void); | |
511 | ||
512 | /* | |
513 | * Delete all northbound nodes from all YANG schema nodes. | |
514 | */ | |
515 | void nb_nodes_delete(void); | |
516 | ||
1c2facd1 RW |
517 | /* |
518 | * Find the northbound node corresponding to a YANG data path. | |
519 | * | |
520 | * xpath | |
521 | * XPath to search for (with or without predicates). | |
522 | * | |
523 | * Returns: | |
524 | * Pointer to northbound node if found, NULL otherwise. | |
525 | */ | |
526 | extern struct nb_node *nb_node_find(const char *xpath); | |
527 | ||
528 | /* | |
529 | * Create a new northbound configuration. | |
530 | * | |
531 | * dnode | |
532 | * Pointer to a libyang data node containing the configuration data. If NULL | |
533 | * is given, an empty configuration will be created. | |
534 | * | |
535 | * Returns: | |
536 | * Pointer to newly created northbound configuration. | |
537 | */ | |
538 | extern struct nb_config *nb_config_new(struct lyd_node *dnode); | |
539 | ||
540 | /* | |
541 | * Delete a northbound configuration. | |
542 | * | |
543 | * config | |
544 | * Pointer to the config that is going to be deleted. | |
545 | */ | |
546 | extern void nb_config_free(struct nb_config *config); | |
547 | ||
548 | /* | |
549 | * Duplicate a northbound configuration. | |
550 | * | |
551 | * config | |
552 | * Northbound configuration to duplicate. | |
553 | * | |
554 | * Returns: | |
555 | * Pointer to duplicated configuration. | |
556 | */ | |
557 | extern struct nb_config *nb_config_dup(const struct nb_config *config); | |
558 | ||
559 | /* | |
560 | * Merge one configuration into another. | |
561 | * | |
562 | * config_dst | |
563 | * Configuration to merge to. | |
564 | * | |
565 | * config_src | |
566 | * Configuration to merge config_dst with. | |
567 | * | |
568 | * preserve_source | |
569 | * Specify whether config_src should be deleted or not after the merge | |
570 | * operation. | |
571 | * | |
572 | * Returns: | |
573 | * NB_OK on success, NB_ERR otherwise. | |
574 | */ | |
575 | extern int nb_config_merge(struct nb_config *config_dst, | |
576 | struct nb_config *config_src, bool preserve_source); | |
577 | ||
578 | /* | |
579 | * Replace one configuration by another. | |
580 | * | |
581 | * config_dst | |
582 | * Configuration to be replaced. | |
583 | * | |
584 | * config_src | |
585 | * Configuration to replace config_dst. | |
586 | * | |
587 | * preserve_source | |
588 | * Specify whether config_src should be deleted or not after the replace | |
589 | * operation. | |
590 | */ | |
591 | extern void nb_config_replace(struct nb_config *config_dst, | |
592 | struct nb_config *config_src, | |
593 | bool preserve_source); | |
594 | ||
595 | /* | |
596 | * Edit a candidate configuration. | |
597 | * | |
598 | * candidate | |
599 | * Candidate configuration to edit. | |
600 | * | |
601 | * nb_node | |
602 | * Northbound node associated to the configuration being edited. | |
603 | * | |
604 | * operation | |
605 | * Operation to apply. | |
606 | * | |
607 | * xpath | |
608 | * XPath of the configuration node being edited. | |
609 | * | |
610 | * previous | |
611 | * Previous value of the configuration node. Should be used only when the | |
612 | * operation is NB_OP_MOVE, otherwise this parameter is ignored. | |
613 | * | |
614 | * data | |
615 | * New value of the configuration node. | |
616 | * | |
617 | * Returns: | |
618 | * - NB_OK on success. | |
619 | * - NB_ERR_NOT_FOUND when the element to be deleted was not found. | |
620 | * - NB_ERR for other errors. | |
621 | */ | |
622 | extern int nb_candidate_edit(struct nb_config *candidate, | |
623 | const struct nb_node *nb_node, | |
624 | enum nb_operation operation, const char *xpath, | |
625 | const struct yang_data *previous, | |
626 | const struct yang_data *data); | |
627 | ||
628 | /* | |
629 | * Check if a candidate configuration is outdated and needs to be updated. | |
630 | * | |
631 | * candidate | |
632 | * Candidate configuration to check. | |
633 | * | |
634 | * Returns: | |
635 | * true if the candidate is outdated, false otherwise. | |
636 | */ | |
637 | extern bool nb_candidate_needs_update(const struct nb_config *candidate); | |
638 | ||
639 | /* | |
640 | * Update a candidate configuration by rebasing the changes on top of the latest | |
641 | * running configuration. Resolve conflicts automatically by giving preference | |
642 | * to the changes done in the candidate configuration. | |
643 | * | |
644 | * candidate | |
645 | * Candidate configuration to update. | |
646 | * | |
647 | * Returns: | |
648 | * NB_OK on success, NB_ERR otherwise. | |
649 | */ | |
650 | extern int nb_candidate_update(struct nb_config *candidate); | |
651 | ||
652 | /* | |
653 | * Validate a candidate configuration. Perform both YANG syntactic/semantic | |
654 | * validation and code-level validation using the northbound callbacks. | |
655 | * | |
656 | * WARNING: the candidate can be modified as part of the validation process | |
657 | * (e.g. add default nodes). | |
658 | * | |
659 | * candidate | |
660 | * Candidate configuration to validate. | |
661 | * | |
662 | * Returns: | |
663 | * NB_OK on success, NB_ERR_VALIDATION otherwise. | |
664 | */ | |
665 | extern int nb_candidate_validate(struct nb_config *candidate); | |
666 | ||
667 | /* | |
668 | * Create a new configuration transaction but do not commit it yet. Only | |
669 | * validate the candidate and prepare all resources required to apply the | |
670 | * configuration changes. | |
671 | * | |
672 | * candidate | |
673 | * Candidate configuration to commit. | |
674 | * | |
675 | * client | |
676 | * Northbound client performing the commit. | |
677 | * | |
364ad673 RW |
678 | * user |
679 | * Northbound user performing the commit (can be NULL). | |
680 | * | |
1c2facd1 RW |
681 | * comment |
682 | * Optional comment describing the commit. | |
683 | * | |
684 | * transaction | |
685 | * Output parameter providing the created transaction when one is created | |
686 | * successfully. In this case, it must be either aborted using | |
687 | * nb_candidate_commit_abort() or committed using | |
688 | * nb_candidate_commit_apply(). | |
689 | * | |
690 | * Returns: | |
691 | * - NB_OK on success. | |
692 | * - NB_ERR_NO_CHANGES when the candidate is identical to the running | |
693 | * configuration. | |
694 | * - NB_ERR_LOCKED when there's already another transaction in progress. | |
695 | * - NB_ERR_VALIDATION when the candidate fails the validation checks. | |
696 | * - NB_ERR_RESOURCE when the system fails to allocate resources to apply | |
697 | * the candidate configuration. | |
698 | * - NB_ERR for other errors. | |
699 | */ | |
700 | extern int nb_candidate_commit_prepare(struct nb_config *candidate, | |
364ad673 | 701 | enum nb_client client, const void *user, |
1c2facd1 RW |
702 | const char *comment, |
703 | struct nb_transaction **transaction); | |
704 | ||
705 | /* | |
706 | * Abort a previously created configuration transaction, releasing all resources | |
707 | * allocated during the preparation phase. | |
708 | * | |
709 | * transaction | |
710 | * Candidate configuration to abort. It's consumed by this function. | |
711 | */ | |
712 | extern void nb_candidate_commit_abort(struct nb_transaction *transaction); | |
713 | ||
714 | /* | |
715 | * Commit a previously created configuration transaction. | |
716 | * | |
717 | * transaction | |
718 | * Configuration transaction to commit. It's consumed by this function. | |
719 | * | |
720 | * save_transaction | |
721 | * Specify whether the transaction should be recorded in the transactions log | |
722 | * or not. | |
723 | * | |
724 | * transaction_id | |
725 | * Optional output parameter providing the ID of the committed transaction. | |
726 | */ | |
727 | extern void nb_candidate_commit_apply(struct nb_transaction *transaction, | |
728 | bool save_transaction, | |
729 | uint32_t *transaction_id); | |
730 | ||
731 | /* | |
732 | * Create a new transaction to commit a candidate configuration. This is a | |
733 | * convenience function that performs the two-phase commit protocol | |
734 | * transparently to the user. The cost is reduced flexibility, since | |
735 | * network-wide and multi-daemon transactions require the network manager to | |
736 | * take into account the results of the preparation phase of multiple managed | |
737 | * entities. | |
738 | * | |
739 | * candidate | |
740 | * Candidate configuration to commit. It's preserved regardless if the commit | |
741 | * operation fails or not. | |
742 | * | |
743 | * client | |
744 | * Northbound client performing the commit. | |
745 | * | |
364ad673 RW |
746 | * user |
747 | * Northbound user performing the commit (can be NULL). | |
748 | * | |
1c2facd1 RW |
749 | * save_transaction |
750 | * Specify whether the transaction should be recorded in the transactions log | |
751 | * or not. | |
752 | * | |
753 | * comment | |
754 | * Optional comment describing the commit. | |
755 | * | |
756 | * transaction_id | |
757 | * Optional output parameter providing the ID of the committed transaction. | |
758 | * | |
759 | * Returns: | |
760 | * - NB_OK on success. | |
761 | * - NB_ERR_NO_CHANGES when the candidate is identical to the running | |
762 | * configuration. | |
763 | * - NB_ERR_LOCKED when there's already another transaction in progress. | |
764 | * - NB_ERR_VALIDATION when the candidate fails the validation checks. | |
765 | * - NB_ERR_RESOURCE when the system fails to allocate resources to apply | |
766 | * the candidate configuration. | |
767 | * - NB_ERR for other errors. | |
768 | */ | |
769 | extern int nb_candidate_commit(struct nb_config *candidate, | |
364ad673 RW |
770 | enum nb_client client, const void *user, |
771 | bool save_transaction, const char *comment, | |
772 | uint32_t *transaction_id); | |
773 | ||
774 | /* | |
775 | * Lock the running configuration. | |
776 | * | |
777 | * client | |
778 | * Northbound client. | |
779 | * | |
780 | * user | |
781 | * Northbound user (can be NULL). | |
782 | * | |
783 | * Returns: | |
784 | * 0 on success, -1 when the running configuration is already locked. | |
785 | */ | |
786 | extern int nb_running_lock(enum nb_client client, const void *user); | |
787 | ||
788 | /* | |
789 | * Unlock the running configuration. | |
790 | * | |
791 | * client | |
792 | * Northbound client. | |
793 | * | |
794 | * user | |
795 | * Northbound user (can be NULL). | |
796 | * | |
797 | * Returns: | |
798 | * 0 on success, -1 when the running configuration is already unlocked or | |
799 | * locked by another client/user. | |
800 | */ | |
801 | extern int nb_running_unlock(enum nb_client client, const void *user); | |
802 | ||
803 | /* | |
804 | * Check if the running configuration is locked or not for the given | |
805 | * client/user. | |
806 | * | |
807 | * client | |
808 | * Northbound client. | |
809 | * | |
810 | * user | |
811 | * Northbound user (can be NULL). | |
812 | * | |
813 | * Returns: | |
814 | * 0 if the running configuration is unlocked or if the client/user owns the | |
815 | * lock, -1 otherwise. | |
816 | */ | |
817 | extern int nb_running_lock_check(enum nb_client client, const void *user); | |
1c2facd1 | 818 | |
1a4bc045 | 819 | /* |
364ad673 | 820 | * Iterate over operational data. |
1a4bc045 RW |
821 | * |
822 | * xpath | |
823 | * Data path of the YANG data we want to iterate over. | |
824 | * | |
825 | * translator | |
826 | * YANG module translator (might be NULL). | |
827 | * | |
828 | * flags | |
829 | * NB_OPER_DATA_ITER_ flags to control how the iteration is performed. | |
830 | * | |
831 | * cb | |
832 | * Function to call with each data node. | |
833 | * | |
834 | * arg | |
835 | * Arbitrary argument passed as the fourth parameter in each call to 'cb'. | |
836 | * | |
837 | * Returns: | |
838 | * NB_OK on success, NB_ERR otherwise. | |
839 | */ | |
840 | extern int nb_oper_data_iterate(const char *xpath, | |
841 | struct yang_translator *translator, | |
842 | uint32_t flags, nb_oper_data_cb cb, void *arg); | |
843 | ||
1c2facd1 RW |
844 | /* |
845 | * Validate if the northbound operation is valid for the given node. | |
846 | * | |
847 | * operation | |
848 | * Operation we want to check. | |
849 | * | |
850 | * snode | |
851 | * libyang schema node we want to check. | |
852 | * | |
853 | * Returns: | |
854 | * true if the operation is valid, false otherwise. | |
855 | */ | |
856 | extern bool nb_operation_is_valid(enum nb_operation operation, | |
857 | const struct lys_node *snode); | |
858 | ||
859 | /* | |
860 | * Send a YANG notification. This is a no-op unless the 'nb_notification_send' | |
861 | * hook was registered by a northbound plugin. | |
862 | * | |
863 | * xpath | |
864 | * XPath of the YANG notification. | |
865 | * | |
866 | * arguments | |
867 | * Linked list containing the arguments that should be sent. This list is | |
868 | * deleted after being used. | |
869 | * | |
870 | * Returns: | |
871 | * NB_OK on success, NB_ERR otherwise. | |
872 | */ | |
873 | extern int nb_notification_send(const char *xpath, struct list *arguments); | |
874 | ||
ccd43ada RW |
875 | /* |
876 | * Associate a user pointer to a configuration node. | |
877 | * | |
878 | * This should be called by northbound 'create' callbacks in the NB_EV_APPLY | |
879 | * phase only. | |
880 | * | |
881 | * dnode | |
882 | * libyang data node - only its XPath is used. | |
883 | * | |
884 | * entry | |
885 | * Arbitrary user-specified pointer. | |
886 | */ | |
887 | extern void nb_running_set_entry(const struct lyd_node *dnode, void *entry); | |
888 | ||
889 | /* | |
890 | * Unset the user pointer associated to a configuration node. | |
891 | * | |
892 | * This should be called by northbound 'destroy' callbacks in the NB_EV_APPLY | |
893 | * phase only. | |
894 | * | |
895 | * dnode | |
896 | * libyang data node - only its XPath is used. | |
897 | * | |
898 | * Returns: | |
899 | * The user pointer that was unset. | |
900 | */ | |
901 | extern void *nb_running_unset_entry(const struct lyd_node *dnode); | |
902 | ||
903 | /* | |
904 | * Find the user pointer (if any) associated to a configuration node. | |
905 | * | |
906 | * The XPath associated to the configuration node can be provided directly or | |
907 | * indirectly through a libyang data node. | |
908 | * | |
909 | * If an user point is not found, this function follows the parent nodes in the | |
910 | * running configuration until an user pointer is found or until the root node | |
911 | * is reached. | |
912 | * | |
913 | * dnode | |
914 | * libyang data node - only its XPath is used (can be NULL if 'xpath' is | |
915 | * provided). | |
916 | * | |
917 | * xpath | |
918 | * XPath of the configuration node (can be NULL if 'dnode' is provided). | |
919 | * | |
920 | * abort_if_not_found | |
921 | * When set to true, abort the program if no user pointer is found. | |
922 | * | |
923 | * As a rule of thumb, this parameter should be set to true in the following | |
924 | * scenarios: | |
925 | * - Calling this function from any northbound configuration callback during | |
926 | * the NB_EV_APPLY phase. | |
927 | * - Calling this function from a 'delete' northbound configuration callback | |
928 | * during any phase. | |
929 | * | |
930 | * In both the above cases, the given configuration node should contain an | |
931 | * user pointer except when there's a bug in the code, in which case it's | |
932 | * better to abort the program right away and eliminate the need for | |
933 | * unnecessary NULL checks. | |
934 | * | |
935 | * In all other cases, this parameter should be set to false and the caller | |
936 | * should check if the function returned NULL or not. | |
937 | * | |
938 | * Returns: | |
939 | * User pointer if found, NULL otherwise. | |
940 | */ | |
941 | extern void *nb_running_get_entry(const struct lyd_node *dnode, const char *xpath, | |
942 | bool abort_if_not_found); | |
943 | ||
1c2facd1 RW |
944 | /* |
945 | * Return a human-readable string representing a northbound event. | |
946 | * | |
947 | * event | |
948 | * Northbound event. | |
949 | * | |
950 | * Returns: | |
951 | * String representation of the given northbound event. | |
952 | */ | |
953 | extern const char *nb_event_name(enum nb_event event); | |
954 | ||
955 | /* | |
956 | * Return a human-readable string representing a northbound operation. | |
957 | * | |
958 | * operation | |
959 | * Northbound operation. | |
960 | * | |
961 | * Returns: | |
962 | * String representation of the given northbound operation. | |
963 | */ | |
964 | extern const char *nb_operation_name(enum nb_operation operation); | |
965 | ||
966 | /* | |
967 | * Return a human-readable string representing a northbound error. | |
968 | * | |
969 | * error | |
970 | * Northbound error. | |
971 | * | |
972 | * Returns: | |
973 | * String representation of the given northbound error. | |
974 | */ | |
975 | extern const char *nb_err_name(enum nb_error error); | |
976 | ||
977 | /* | |
978 | * Return a human-readable string representing a northbound client. | |
979 | * | |
980 | * client | |
981 | * Northbound client. | |
982 | * | |
983 | * Returns: | |
984 | * String representation of the given northbound client. | |
985 | */ | |
986 | extern const char *nb_client_name(enum nb_client client); | |
987 | ||
988 | /* | |
989 | * Initialize the northbound layer. Should be called only once during the | |
990 | * daemon initialization process. | |
991 | * | |
992 | * modules | |
993 | * Array of YANG modules to parse and initialize. | |
994 | * | |
995 | * nmodules | |
996 | * Size of the modules array. | |
997 | */ | |
fbdc1c0a | 998 | extern void nb_init(struct thread_master *tm, const struct frr_yang_module_info *modules[], |
1c2facd1 RW |
999 | size_t nmodules); |
1000 | ||
1001 | /* | |
1002 | * Finish the northbound layer gracefully. Should be called only when the daemon | |
1003 | * is exiting. | |
1004 | */ | |
1005 | extern void nb_terminate(void); | |
1006 | ||
5e244469 RW |
1007 | #ifdef __cplusplus |
1008 | } | |
1009 | #endif | |
1010 | ||
1c2facd1 | 1011 | #endif /* _FRR_NORTHBOUND_H_ */ |