]> git.proxmox.com Git - mirror_frr.git/blob - lib/northbound.c
mgmtd: Add MGMT Transaction Framework
[mirror_frr.git] / lib / northbound.c
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Copyright (C) 2018 NetDEF, Inc.
4 * Renato Westphal
5 */
6
7 #include <zebra.h>
8
9 #include "libfrr.h"
10 #include "log.h"
11 #include "lib_errors.h"
12 #include "hash.h"
13 #include "command.h"
14 #include "debug.h"
15 #include "db.h"
16 #include "frr_pthread.h"
17 #include "northbound.h"
18 #include "northbound_cli.h"
19 #include "northbound_db.h"
20 #include "frrstr.h"
21
22 DEFINE_MTYPE_STATIC(LIB, NB_NODE, "Northbound Node");
23 DEFINE_MTYPE_STATIC(LIB, NB_CONFIG, "Northbound Configuration");
24 DEFINE_MTYPE_STATIC(LIB, NB_CONFIG_ENTRY, "Northbound Configuration Entry");
25
26 /* Running configuration - shouldn't be modified directly. */
27 struct nb_config *running_config;
28
29 /* Hash table of user pointers associated with configuration entries. */
30 static struct hash *running_config_entries;
31
32 /* Management lock for the running configuration. */
33 static struct {
34 /* Mutex protecting this structure. */
35 pthread_mutex_t mtx;
36
37 /* Actual lock. */
38 bool locked;
39
40 /* Northbound client who owns this lock. */
41 enum nb_client owner_client;
42
43 /* Northbound user who owns this lock. */
44 const void *owner_user;
45 } running_config_mgmt_lock;
46
47 /* Knob to record config transaction */
48 static bool nb_db_enabled;
49 /*
50 * Global lock used to prevent multiple configuration transactions from
51 * happening concurrently.
52 */
53 static bool transaction_in_progress;
54
55 static int nb_callback_pre_validate(struct nb_context *context,
56 const struct nb_node *nb_node,
57 const struct lyd_node *dnode, char *errmsg,
58 size_t errmsg_len);
59 static int nb_callback_configuration(struct nb_context *context,
60 const enum nb_event event,
61 struct nb_config_change *change,
62 char *errmsg, size_t errmsg_len);
63 static struct nb_transaction *
64 nb_transaction_new(struct nb_context context, struct nb_config *config,
65 struct nb_config_cbs *changes, const char *comment,
66 char *errmsg, size_t errmsg_len);
67 static void nb_transaction_free(struct nb_transaction *transaction);
68 static int nb_transaction_process(enum nb_event event,
69 struct nb_transaction *transaction,
70 char *errmsg, size_t errmsg_len);
71 static void nb_transaction_apply_finish(struct nb_transaction *transaction,
72 char *errmsg, size_t errmsg_len);
73 static int nb_oper_data_iter_node(const struct lysc_node *snode,
74 const char *xpath, const void *list_entry,
75 const struct yang_list_keys *list_keys,
76 struct yang_translator *translator,
77 bool first, uint32_t flags,
78 nb_oper_data_cb cb, void *arg);
79
80 static int nb_node_check_config_only(const struct lysc_node *snode, void *arg)
81 {
82 bool *config_only = arg;
83
84 if (CHECK_FLAG(snode->flags, LYS_CONFIG_R)) {
85 *config_only = false;
86 return YANG_ITER_STOP;
87 }
88
89 return YANG_ITER_CONTINUE;
90 }
91
92 static int nb_node_new_cb(const struct lysc_node *snode, void *arg)
93 {
94 struct nb_node *nb_node;
95 struct lysc_node *sparent, *sparent_list;
96 struct frr_yang_module_info *module;
97
98 module = (struct frr_yang_module_info *)arg;
99 nb_node = XCALLOC(MTYPE_NB_NODE, sizeof(*nb_node));
100 yang_snode_get_path(snode, YANG_PATH_DATA, nb_node->xpath,
101 sizeof(nb_node->xpath));
102 nb_node->priority = NB_DFLT_PRIORITY;
103 sparent = yang_snode_real_parent(snode);
104 if (sparent)
105 nb_node->parent = sparent->priv;
106 sparent_list = yang_snode_parent_list(snode);
107 if (sparent_list)
108 nb_node->parent_list = sparent_list->priv;
109
110 /* Set flags. */
111 if (CHECK_FLAG(snode->nodetype, LYS_CONTAINER | LYS_LIST)) {
112 bool config_only = true;
113
114 (void)yang_snodes_iterate_subtree(snode, NULL,
115 nb_node_check_config_only, 0,
116 &config_only);
117 if (config_only)
118 SET_FLAG(nb_node->flags, F_NB_NODE_CONFIG_ONLY);
119 }
120 if (CHECK_FLAG(snode->nodetype, LYS_LIST)) {
121 if (yang_snode_num_keys(snode) == 0)
122 SET_FLAG(nb_node->flags, F_NB_NODE_KEYLESS_LIST);
123 }
124
125 /*
126 * Link the northbound node and the libyang schema node with one
127 * another.
128 */
129 nb_node->snode = snode;
130 assert(snode->priv == NULL);
131 ((struct lysc_node *)snode)->priv = nb_node;
132
133 if (module && module->ignore_cbs)
134 SET_FLAG(nb_node->flags, F_NB_NODE_IGNORE_CBS);
135
136 return YANG_ITER_CONTINUE;
137 }
138
139 static int nb_node_del_cb(const struct lysc_node *snode, void *arg)
140 {
141 struct nb_node *nb_node;
142
143 nb_node = snode->priv;
144 if (nb_node) {
145 ((struct lysc_node *)snode)->priv = NULL;
146 XFREE(MTYPE_NB_NODE, nb_node);
147 }
148
149 return YANG_ITER_CONTINUE;
150 }
151
152 void nb_nodes_create(void)
153 {
154 yang_snodes_iterate(NULL, nb_node_new_cb, 0, NULL);
155 }
156
157 void nb_nodes_delete(void)
158 {
159 yang_snodes_iterate(NULL, nb_node_del_cb, 0, NULL);
160 }
161
162 struct nb_node *nb_node_find(const char *path)
163 {
164 const struct lysc_node *snode;
165
166 /*
167 * Use libyang to find the schema node associated to the path and get
168 * the northbound node from there (snode private pointer).
169 */
170 snode = lys_find_path(ly_native_ctx, NULL, path, 0);
171 if (!snode)
172 return NULL;
173
174 return snode->priv;
175 }
176
177 void nb_node_set_dependency_cbs(const char *dependency_xpath,
178 const char *dependant_xpath,
179 struct nb_dependency_callbacks *cbs)
180 {
181 struct nb_node *dependency = nb_node_find(dependency_xpath);
182 struct nb_node *dependant = nb_node_find(dependant_xpath);
183
184 if (!dependency || !dependant)
185 return;
186
187 dependency->dep_cbs.get_dependant_xpath = cbs->get_dependant_xpath;
188 dependant->dep_cbs.get_dependency_xpath = cbs->get_dependency_xpath;
189 }
190
191 bool nb_node_has_dependency(struct nb_node *node)
192 {
193 return node->dep_cbs.get_dependency_xpath != NULL;
194 }
195
196 static int nb_node_validate_cb(const struct nb_node *nb_node,
197 enum nb_operation operation,
198 int callback_implemented, bool optional)
199 {
200 bool valid;
201
202 valid = nb_operation_is_valid(operation, nb_node->snode);
203
204 /*
205 * Add an exception for operational data callbacks. A rw list usually
206 * doesn't need any associated operational data callbacks. But if this
207 * rw list is augmented by another module which adds state nodes under
208 * it, then this list will need to have the 'get_next()', 'get_keys()'
209 * and 'lookup_entry()' callbacks. As such, never log a warning when
210 * these callbacks are implemented when they are not needed, since this
211 * depends on context (e.g. some daemons might augment "frr-interface"
212 * while others don't).
213 */
214 if (!valid && callback_implemented && operation != NB_OP_GET_NEXT
215 && operation != NB_OP_GET_KEYS && operation != NB_OP_LOOKUP_ENTRY)
216 flog_warn(EC_LIB_NB_CB_UNNEEDED,
217 "unneeded '%s' callback for '%s'",
218 nb_operation_name(operation), nb_node->xpath);
219
220 if (!optional && valid && !callback_implemented) {
221 flog_err(EC_LIB_NB_CB_MISSING, "missing '%s' callback for '%s'",
222 nb_operation_name(operation), nb_node->xpath);
223 return 1;
224 }
225
226 return 0;
227 }
228
229 /*
230 * Check if the required callbacks were implemented for the given northbound
231 * node.
232 */
233 static unsigned int nb_node_validate_cbs(const struct nb_node *nb_node)
234
235 {
236 unsigned int error = 0;
237
238 if (CHECK_FLAG(nb_node->flags, F_NB_NODE_IGNORE_CBS))
239 return error;
240
241 error += nb_node_validate_cb(nb_node, NB_OP_CREATE,
242 !!nb_node->cbs.create, false);
243 error += nb_node_validate_cb(nb_node, NB_OP_MODIFY,
244 !!nb_node->cbs.modify, false);
245 error += nb_node_validate_cb(nb_node, NB_OP_DESTROY,
246 !!nb_node->cbs.destroy, false);
247 error += nb_node_validate_cb(nb_node, NB_OP_MOVE, !!nb_node->cbs.move,
248 false);
249 error += nb_node_validate_cb(nb_node, NB_OP_PRE_VALIDATE,
250 !!nb_node->cbs.pre_validate, true);
251 error += nb_node_validate_cb(nb_node, NB_OP_APPLY_FINISH,
252 !!nb_node->cbs.apply_finish, true);
253 error += nb_node_validate_cb(nb_node, NB_OP_GET_ELEM,
254 !!nb_node->cbs.get_elem, false);
255 error += nb_node_validate_cb(nb_node, NB_OP_GET_NEXT,
256 !!nb_node->cbs.get_next, false);
257 error += nb_node_validate_cb(nb_node, NB_OP_GET_KEYS,
258 !!nb_node->cbs.get_keys, false);
259 error += nb_node_validate_cb(nb_node, NB_OP_LOOKUP_ENTRY,
260 !!nb_node->cbs.lookup_entry, false);
261 error += nb_node_validate_cb(nb_node, NB_OP_RPC, !!nb_node->cbs.rpc,
262 false);
263
264 return error;
265 }
266
267 static unsigned int nb_node_validate_priority(const struct nb_node *nb_node)
268 {
269 /* Top-level nodes can have any priority. */
270 if (!nb_node->parent)
271 return 0;
272
273 if (nb_node->priority < nb_node->parent->priority) {
274 flog_err(EC_LIB_NB_CB_INVALID_PRIO,
275 "node has higher priority than its parent [xpath %s]",
276 nb_node->xpath);
277 return 1;
278 }
279
280 return 0;
281 }
282
283 static int nb_node_validate(const struct lysc_node *snode, void *arg)
284 {
285 struct nb_node *nb_node = snode->priv;
286 unsigned int *errors = arg;
287
288 /* Validate callbacks and priority. */
289 if (nb_node) {
290 *errors += nb_node_validate_cbs(nb_node);
291 *errors += nb_node_validate_priority(nb_node);
292 }
293
294 return YANG_ITER_CONTINUE;
295 }
296
297 struct nb_config *nb_config_new(struct lyd_node *dnode)
298 {
299 struct nb_config *config;
300
301 config = XCALLOC(MTYPE_NB_CONFIG, sizeof(*config));
302 if (dnode)
303 config->dnode = dnode;
304 else
305 config->dnode = yang_dnode_new(ly_native_ctx, true);
306 config->version = 0;
307
308 RB_INIT(nb_config_cbs, &config->cfg_chgs);
309
310 return config;
311 }
312
313 void nb_config_free(struct nb_config *config)
314 {
315 if (config->dnode)
316 yang_dnode_free(config->dnode);
317 nb_config_diff_del_changes(&config->cfg_chgs);
318 XFREE(MTYPE_NB_CONFIG, config);
319 }
320
321 struct nb_config *nb_config_dup(const struct nb_config *config)
322 {
323 struct nb_config *dup;
324
325 dup = XCALLOC(MTYPE_NB_CONFIG, sizeof(*dup));
326 dup->dnode = yang_dnode_dup(config->dnode);
327 dup->version = config->version;
328
329 RB_INIT(nb_config_cbs, &dup->cfg_chgs);
330
331 return dup;
332 }
333
334 int nb_config_merge(struct nb_config *config_dst, struct nb_config *config_src,
335 bool preserve_source)
336 {
337 int ret;
338
339 ret = lyd_merge_siblings(&config_dst->dnode, config_src->dnode, 0);
340 if (ret != 0)
341 flog_warn(EC_LIB_LIBYANG, "%s: lyd_merge() failed", __func__);
342
343 if (!preserve_source)
344 nb_config_free(config_src);
345
346 return (ret == 0) ? NB_OK : NB_ERR;
347 }
348
349 void nb_config_replace(struct nb_config *config_dst,
350 struct nb_config *config_src, bool preserve_source)
351 {
352 /* Update version. */
353 if (config_src->version != 0)
354 config_dst->version = config_src->version;
355
356 /* Update dnode. */
357 if (config_dst->dnode)
358 yang_dnode_free(config_dst->dnode);
359 if (preserve_source) {
360 config_dst->dnode = yang_dnode_dup(config_src->dnode);
361 } else {
362 config_dst->dnode = config_src->dnode;
363 config_src->dnode = NULL;
364 nb_config_free(config_src);
365 }
366 }
367
368 /* Generate the nb_config_cbs tree. */
369 static inline int nb_config_cb_compare(const struct nb_config_cb *a,
370 const struct nb_config_cb *b)
371 {
372 /* Sort by priority first. */
373 if (a->nb_node->priority < b->nb_node->priority)
374 return -1;
375 if (a->nb_node->priority > b->nb_node->priority)
376 return 1;
377
378 /*
379 * Preserve the order of the configuration changes as told by libyang.
380 */
381 if (a->seq < b->seq)
382 return -1;
383 if (a->seq > b->seq)
384 return 1;
385
386 /*
387 * All 'apply_finish' callbacks have their sequence number set to zero.
388 * In this case, compare them using their dnode pointers (the order
389 * doesn't matter for callbacks that have the same priority).
390 */
391 if (a->dnode < b->dnode)
392 return -1;
393 if (a->dnode > b->dnode)
394 return 1;
395
396 return 0;
397 }
398 RB_GENERATE(nb_config_cbs, nb_config_cb, entry, nb_config_cb_compare);
399
400 static void nb_config_diff_add_change(struct nb_config_cbs *changes,
401 enum nb_operation operation,
402 uint32_t *seq,
403 const struct lyd_node *dnode)
404 {
405 struct nb_config_change *change;
406
407 /* Ignore unimplemented nodes. */
408 if (!dnode->schema->priv)
409 return;
410
411 change = XCALLOC(MTYPE_TMP, sizeof(*change));
412 change->cb.operation = operation;
413 change->cb.seq = *seq;
414 *seq = *seq + 1;
415 change->cb.nb_node = dnode->schema->priv;
416 change->cb.dnode = dnode;
417
418 RB_INSERT(nb_config_cbs, changes, &change->cb);
419 }
420
421 void nb_config_diff_del_changes(struct nb_config_cbs *changes)
422 {
423 while (!RB_EMPTY(nb_config_cbs, changes)) {
424 struct nb_config_change *change;
425
426 change = (struct nb_config_change *)RB_ROOT(nb_config_cbs,
427 changes);
428 RB_REMOVE(nb_config_cbs, changes, &change->cb);
429 XFREE(MTYPE_TMP, change);
430 }
431 }
432
433 /*
434 * Helper function used when calculating the delta between two different
435 * configurations. Given a new subtree, calculate all new YANG data nodes,
436 * excluding default leafs and leaf-lists. This is a recursive function.
437 */
438 void nb_config_diff_created(const struct lyd_node *dnode, uint32_t *seq,
439 struct nb_config_cbs *changes)
440 {
441 enum nb_operation operation;
442 struct lyd_node *child;
443
444 /* Ignore unimplemented nodes. */
445 if (!dnode->schema->priv)
446 return;
447
448 switch (dnode->schema->nodetype) {
449 case LYS_LEAF:
450 case LYS_LEAFLIST:
451 if (lyd_is_default(dnode))
452 break;
453
454 if (nb_operation_is_valid(NB_OP_CREATE, dnode->schema))
455 operation = NB_OP_CREATE;
456 else if (nb_operation_is_valid(NB_OP_MODIFY, dnode->schema))
457 operation = NB_OP_MODIFY;
458 else
459 return;
460
461 nb_config_diff_add_change(changes, operation, seq, dnode);
462 break;
463 case LYS_CONTAINER:
464 case LYS_LIST:
465 if (nb_operation_is_valid(NB_OP_CREATE, dnode->schema))
466 nb_config_diff_add_change(changes, NB_OP_CREATE, seq,
467 dnode);
468
469 /* Process child nodes recursively. */
470 LY_LIST_FOR (lyd_child(dnode), child) {
471 nb_config_diff_created(child, seq, changes);
472 }
473 break;
474 default:
475 break;
476 }
477 }
478
479 static void nb_config_diff_deleted(const struct lyd_node *dnode, uint32_t *seq,
480 struct nb_config_cbs *changes)
481 {
482 /* Ignore unimplemented nodes. */
483 if (!dnode->schema->priv)
484 return;
485
486 if (nb_operation_is_valid(NB_OP_DESTROY, dnode->schema))
487 nb_config_diff_add_change(changes, NB_OP_DESTROY, seq, dnode);
488 else if (CHECK_FLAG(dnode->schema->nodetype, LYS_CONTAINER)) {
489 struct lyd_node *child;
490
491 /*
492 * Non-presence containers need special handling since they
493 * don't have "destroy" callbacks. In this case, what we need to
494 * do is to call the "destroy" callbacks of their child nodes
495 * when applicable (i.e. optional nodes).
496 */
497 LY_LIST_FOR (lyd_child(dnode), child) {
498 nb_config_diff_deleted(child, seq, changes);
499 }
500 }
501 }
502
503 static int nb_lyd_diff_get_op(const struct lyd_node *dnode)
504 {
505 const struct lyd_meta *meta;
506 LY_LIST_FOR (dnode->meta, meta) {
507 if (strcmp(meta->name, "operation")
508 || strcmp(meta->annotation->module->name, "yang"))
509 continue;
510 return lyd_get_meta_value(meta)[0];
511 }
512 return 'n';
513 }
514
515 #if 0 /* Used below in nb_config_diff inside normally disabled code */
516 static inline void nb_config_diff_dnode_log_path(const char *context,
517 const char *path,
518 const struct lyd_node *dnode)
519 {
520 if (dnode->schema->nodetype & LYD_NODE_TERM)
521 zlog_debug("nb_config_diff: %s: %s: %s", context, path,
522 lyd_get_value(dnode));
523 else
524 zlog_debug("nb_config_diff: %s: %s", context, path);
525 }
526
527 static inline void nb_config_diff_dnode_log(const char *context,
528 const struct lyd_node *dnode)
529 {
530 if (!dnode) {
531 zlog_debug("nb_config_diff: %s: NULL", context);
532 return;
533 }
534
535 char *path = lyd_path(dnode, LYD_PATH_STD, NULL, 0);
536 nb_config_diff_dnode_log_path(context, path, dnode);
537 free(path);
538 }
539 #endif
540
541 /*
542 * Calculate the delta between two different configurations.
543 *
544 * NOTE: 'config1' is the reference DB, while 'config2' is
545 * the DB being compared against 'config1'. Typically 'config1'
546 * should be the Running DB and 'config2' is the Candidate DB.
547 */
548 void nb_config_diff(const struct nb_config *config1,
549 const struct nb_config *config2,
550 struct nb_config_cbs *changes)
551 {
552 struct lyd_node *diff = NULL;
553 const struct lyd_node *root, *dnode;
554 struct lyd_node *target;
555 int op;
556 LY_ERR err;
557 char *path;
558
559 #if 0 /* Useful (noisy) when debugging diff code, and for improving later */
560 if (DEBUG_MODE_CHECK(&nb_dbg_cbs_config, DEBUG_MODE_ALL)) {
561 LY_LIST_FOR(config1->dnode, root) {
562 LYD_TREE_DFS_BEGIN(root, dnode) {
563 nb_config_diff_dnode_log("from", dnode);
564 LYD_TREE_DFS_END(root, dnode);
565 }
566 }
567 LY_LIST_FOR(config2->dnode, root) {
568 LYD_TREE_DFS_BEGIN(root, dnode) {
569 nb_config_diff_dnode_log("to", dnode);
570 LYD_TREE_DFS_END(root, dnode);
571 }
572 }
573 }
574 #endif
575
576 err = lyd_diff_siblings(config1->dnode, config2->dnode,
577 LYD_DIFF_DEFAULTS, &diff);
578 assert(!err);
579
580 if (diff && DEBUG_MODE_CHECK(&nb_dbg_cbs_config, DEBUG_MODE_ALL)) {
581 char *s;
582
583 if (!lyd_print_mem(&s, diff, LYD_JSON,
584 LYD_PRINT_WITHSIBLINGS | LYD_PRINT_WD_ALL)) {
585 zlog_debug("%s: %s", __func__, s);
586 free(s);
587 }
588 }
589
590 uint32_t seq = 0;
591
592 LY_LIST_FOR (diff, root) {
593 LYD_TREE_DFS_BEGIN (root, dnode) {
594 op = nb_lyd_diff_get_op(dnode);
595
596 path = lyd_path(dnode, LYD_PATH_STD, NULL, 0);
597
598 #if 0 /* Useful (noisy) when debugging diff code, and for improving later */
599 if (DEBUG_MODE_CHECK(&nb_dbg_cbs_config, DEBUG_MODE_ALL)) {
600 char context[80];
601 snprintf(context, sizeof(context),
602 "iterating diff: oper: %c seq: %u", op, seq);
603 nb_config_diff_dnode_log_path(context, path, dnode);
604 }
605 #endif
606 switch (op) {
607 case 'c': /* create */
608 /*
609 * This is rather inefficient, but when we use
610 * dnode from the diff instead of the
611 * candidate config node we get failures when
612 * looking up default values, etc, based on
613 * the diff tree.
614 */
615 target = yang_dnode_get(config2->dnode, path);
616 assert(target);
617 nb_config_diff_created(target, &seq, changes);
618
619 /* Skip rest of sub-tree, move to next sibling
620 */
621 LYD_TREE_DFS_continue = 1;
622 break;
623 case 'd': /* delete */
624 target = yang_dnode_get(config1->dnode, path);
625 assert(target);
626 nb_config_diff_deleted(target, &seq, changes);
627
628 /* Skip rest of sub-tree, move to next sibling
629 */
630 LYD_TREE_DFS_continue = 1;
631 break;
632 case 'r': /* replace */
633 /* either moving an entry or changing a value */
634 target = yang_dnode_get(config2->dnode, path);
635 assert(target);
636 nb_config_diff_add_change(changes, NB_OP_MODIFY,
637 &seq, target);
638 break;
639 case 'n': /* none */
640 default:
641 break;
642 }
643 free(path);
644 LYD_TREE_DFS_END(root, dnode);
645 }
646 }
647
648 lyd_free_all(diff);
649 }
650
651 int nb_candidate_edit(struct nb_config *candidate,
652 const struct nb_node *nb_node,
653 enum nb_operation operation, const char *xpath,
654 const struct yang_data *previous,
655 const struct yang_data *data)
656 {
657 struct lyd_node *dnode, *dep_dnode;
658 char xpath_edit[XPATH_MAXLEN];
659 char dep_xpath[XPATH_MAXLEN];
660 LY_ERR err;
661
662 /* Use special notation for leaf-lists (RFC 6020, section 9.13.5). */
663 if (nb_node->snode->nodetype == LYS_LEAFLIST)
664 snprintf(xpath_edit, sizeof(xpath_edit), "%s[.='%s']", xpath,
665 data->value);
666 else
667 strlcpy(xpath_edit, xpath, sizeof(xpath_edit));
668
669 switch (operation) {
670 case NB_OP_CREATE:
671 case NB_OP_MODIFY:
672 err = lyd_new_path(candidate->dnode, ly_native_ctx, xpath_edit,
673 (void *)data->value, LYD_NEW_PATH_UPDATE,
674 &dnode);
675 if (err) {
676 flog_warn(EC_LIB_LIBYANG,
677 "%s: lyd_new_path(%s) failed: %d", __func__,
678 xpath_edit, err);
679 return NB_ERR;
680 } else if (dnode) {
681 /* Create default nodes */
682 LY_ERR err = lyd_new_implicit_tree(
683 dnode, LYD_IMPLICIT_NO_STATE, NULL);
684 if (err) {
685 flog_warn(EC_LIB_LIBYANG,
686 "%s: lyd_new_implicit_all failed: %d",
687 __func__, err);
688 }
689 /*
690 * create dependency
691 *
692 * dnode returned by the lyd_new_path may be from a
693 * different schema, so we need to update the nb_node
694 */
695 nb_node = dnode->schema->priv;
696 if (nb_node->dep_cbs.get_dependency_xpath) {
697 nb_node->dep_cbs.get_dependency_xpath(
698 dnode, dep_xpath);
699
700 err = lyd_new_path(candidate->dnode,
701 ly_native_ctx, dep_xpath,
702 NULL, LYD_NEW_PATH_UPDATE,
703 &dep_dnode);
704 /* Create default nodes */
705 if (!err && dep_dnode)
706 err = lyd_new_implicit_tree(
707 dep_dnode,
708 LYD_IMPLICIT_NO_STATE, NULL);
709 if (err) {
710 flog_warn(
711 EC_LIB_LIBYANG,
712 "%s: dependency: lyd_new_path(%s) failed: %d",
713 __func__, dep_xpath, err);
714 return NB_ERR;
715 }
716 }
717 }
718 break;
719 case NB_OP_DESTROY:
720 dnode = yang_dnode_get(candidate->dnode, xpath_edit);
721 if (!dnode)
722 /*
723 * Return a special error code so the caller can choose
724 * whether to ignore it or not.
725 */
726 return NB_ERR_NOT_FOUND;
727 /* destroy dependant */
728 if (nb_node->dep_cbs.get_dependant_xpath) {
729 nb_node->dep_cbs.get_dependant_xpath(dnode, dep_xpath);
730
731 dep_dnode = yang_dnode_get(candidate->dnode, dep_xpath);
732 if (dep_dnode)
733 lyd_free_tree(dep_dnode);
734 }
735 lyd_free_tree(dnode);
736 break;
737 case NB_OP_MOVE:
738 /* TODO: update configuration. */
739 break;
740 case NB_OP_PRE_VALIDATE:
741 case NB_OP_APPLY_FINISH:
742 case NB_OP_GET_ELEM:
743 case NB_OP_GET_NEXT:
744 case NB_OP_GET_KEYS:
745 case NB_OP_LOOKUP_ENTRY:
746 case NB_OP_RPC:
747 flog_warn(EC_LIB_DEVELOPMENT,
748 "%s: unknown operation (%u) [xpath %s]", __func__,
749 operation, xpath_edit);
750 return NB_ERR;
751 }
752
753 return NB_OK;
754 }
755
756 static void nb_update_candidate_changes(struct nb_config *candidate,
757 struct nb_cfg_change *change,
758 uint32_t *seq)
759 {
760 enum nb_operation oper = change->operation;
761 char *xpath = change->xpath;
762 struct lyd_node *root = NULL;
763 struct lyd_node *dnode;
764 struct nb_config_cbs *cfg_chgs = &candidate->cfg_chgs;
765 int op;
766
767 switch (oper) {
768 case NB_OP_CREATE:
769 case NB_OP_MODIFY:
770 root = yang_dnode_get(candidate->dnode, xpath);
771 break;
772 case NB_OP_DESTROY:
773 root = yang_dnode_get(running_config->dnode, xpath);
774 /* code */
775 break;
776 case NB_OP_MOVE:
777 case NB_OP_PRE_VALIDATE:
778 case NB_OP_APPLY_FINISH:
779 case NB_OP_GET_ELEM:
780 case NB_OP_GET_NEXT:
781 case NB_OP_GET_KEYS:
782 case NB_OP_LOOKUP_ENTRY:
783 case NB_OP_RPC:
784 break;
785 default:
786 assert(!"non-enum value, invalid");
787 }
788
789 if (!root)
790 return;
791
792 LYD_TREE_DFS_BEGIN (root, dnode) {
793 op = nb_lyd_diff_get_op(dnode);
794 switch (op) {
795 case 'c':
796 nb_config_diff_created(dnode, seq, cfg_chgs);
797 LYD_TREE_DFS_continue = 1;
798 break;
799 case 'd':
800 nb_config_diff_deleted(dnode, seq, cfg_chgs);
801 LYD_TREE_DFS_continue = 1;
802 break;
803 case 'r':
804 nb_config_diff_add_change(cfg_chgs, NB_OP_MODIFY, seq,
805 dnode);
806 break;
807 default:
808 break;
809 }
810 LYD_TREE_DFS_END(root, dnode);
811 }
812 }
813
814 static bool nb_is_operation_allowed(struct nb_node *nb_node,
815 struct nb_cfg_change *change)
816 {
817 enum nb_operation oper = change->operation;
818
819 if (lysc_is_key(nb_node->snode)) {
820 if (oper == NB_OP_MODIFY || oper == NB_OP_DESTROY)
821 return false;
822 }
823 return true;
824 }
825
826 void nb_candidate_edit_config_changes(
827 struct nb_config *candidate_config, struct nb_cfg_change cfg_changes[],
828 size_t num_cfg_changes, const char *xpath_base, const char *curr_xpath,
829 int xpath_index, char *err_buf, int err_bufsize, bool *error)
830 {
831 uint32_t seq = 0;
832
833 if (error)
834 *error = false;
835
836 if (xpath_base == NULL)
837 xpath_base = "";
838
839 /* Edit candidate configuration. */
840 for (size_t i = 0; i < num_cfg_changes; i++) {
841 struct nb_cfg_change *change = &cfg_changes[i];
842 struct nb_node *nb_node;
843 char xpath[XPATH_MAXLEN];
844 struct yang_data *data;
845 int ret;
846
847 /* Handle relative XPaths. */
848 memset(xpath, 0, sizeof(xpath));
849 if (xpath_index > 0
850 && (xpath_base[0] == '.' || change->xpath[0] == '.'))
851 strlcpy(xpath, curr_xpath, sizeof(xpath));
852 if (xpath_base[0]) {
853 if (xpath_base[0] == '.')
854 strlcat(xpath, xpath_base + 1, sizeof(xpath));
855 else
856 strlcat(xpath, xpath_base, sizeof(xpath));
857 }
858 if (change->xpath[0] == '.')
859 strlcat(xpath, change->xpath + 1, sizeof(xpath));
860 else
861 strlcpy(xpath, change->xpath, sizeof(xpath));
862
863 /* Find the northbound node associated to the data path. */
864 nb_node = nb_node_find(xpath);
865 if (!nb_node) {
866 flog_warn(EC_LIB_YANG_UNKNOWN_DATA_PATH,
867 "%s: unknown data path: %s", __func__, xpath);
868 if (error)
869 *error = true;
870 continue;
871 }
872 /* Find if the node to be edited is not a key node */
873 if (!nb_is_operation_allowed(nb_node, change)) {
874 zlog_err(" Xpath %s points to key node", xpath);
875 if (error)
876 *error = true;
877 break;
878 }
879
880 /* If the value is not set, get the default if it exists. */
881 if (change->value == NULL)
882 change->value = yang_snode_get_default(nb_node->snode);
883 data = yang_data_new(xpath, change->value);
884
885 /*
886 * Ignore "not found" errors when editing the candidate
887 * configuration.
888 */
889 ret = nb_candidate_edit(candidate_config, nb_node,
890 change->operation, xpath, NULL, data);
891 yang_data_free(data);
892 if (ret != NB_OK && ret != NB_ERR_NOT_FOUND) {
893 flog_warn(
894 EC_LIB_NB_CANDIDATE_EDIT_ERROR,
895 "%s: failed to edit candidate configuration: operation [%s] xpath [%s]",
896 __func__, nb_operation_name(change->operation),
897 xpath);
898 if (error)
899 *error = true;
900 continue;
901 }
902 nb_update_candidate_changes(candidate_config, change, &seq);
903 }
904
905 if (error && *error) {
906 char buf[BUFSIZ];
907
908 /*
909 * Failure to edit the candidate configuration should never
910 * happen in practice, unless there's a bug in the code. When
911 * that happens, log the error but otherwise ignore it.
912 */
913 snprintf(err_buf, err_bufsize,
914 "%% Failed to edit configuration.\n\n%s",
915 yang_print_errors(ly_native_ctx, buf, sizeof(buf)));
916 }
917 }
918
919 bool nb_candidate_needs_update(const struct nb_config *candidate)
920 {
921 if (candidate->version < running_config->version)
922 return true;
923
924 return false;
925 }
926
927 int nb_candidate_update(struct nb_config *candidate)
928 {
929 struct nb_config *updated_config;
930
931 updated_config = nb_config_dup(running_config);
932 if (nb_config_merge(updated_config, candidate, true) != NB_OK)
933 return NB_ERR;
934
935 nb_config_replace(candidate, updated_config, false);
936
937 return NB_OK;
938 }
939
940 /*
941 * Perform YANG syntactic and semantic validation.
942 *
943 * WARNING: lyd_validate() can change the configuration as part of the
944 * validation process.
945 */
946 int nb_candidate_validate_yang(struct nb_config *candidate, bool no_state,
947 char *errmsg, size_t errmsg_len)
948 {
949 if (lyd_validate_all(&candidate->dnode, ly_native_ctx,
950 no_state ? LYD_VALIDATE_NO_STATE :
951 LYD_VALIDATE_PRESENT, NULL)
952 != 0) {
953 yang_print_errors(ly_native_ctx, errmsg, errmsg_len);
954 return NB_ERR_VALIDATION;
955 }
956
957 return NB_OK;
958 }
959
960 /* Perform code-level validation using the northbound callbacks. */
961 int nb_candidate_validate_code(struct nb_context *context,
962 struct nb_config *candidate,
963 struct nb_config_cbs *changes, char *errmsg,
964 size_t errmsg_len)
965 {
966 struct nb_config_cb *cb;
967 struct lyd_node *root, *child;
968 int ret;
969
970 /* First validate the candidate as a whole. */
971 LY_LIST_FOR (candidate->dnode, root) {
972 LYD_TREE_DFS_BEGIN (root, child) {
973 struct nb_node *nb_node;
974
975 nb_node = child->schema->priv;
976 if (!nb_node || !nb_node->cbs.pre_validate)
977 goto next;
978
979 ret = nb_callback_pre_validate(context, nb_node, child,
980 errmsg, errmsg_len);
981 if (ret != NB_OK)
982 return NB_ERR_VALIDATION;
983
984 next:
985 LYD_TREE_DFS_END(root, child);
986 }
987 }
988
989 /* Now validate the configuration changes. */
990 RB_FOREACH (cb, nb_config_cbs, changes) {
991 struct nb_config_change *change = (struct nb_config_change *)cb;
992
993 ret = nb_callback_configuration(context, NB_EV_VALIDATE, change,
994 errmsg, errmsg_len);
995 if (ret != NB_OK)
996 return NB_ERR_VALIDATION;
997 }
998
999 return NB_OK;
1000 }
1001
1002 int nb_candidate_diff_and_validate_yang(struct nb_context *context,
1003 struct nb_config *candidate,
1004 struct nb_config_cbs *changes,
1005 char *errmsg, size_t errmsg_len)
1006 {
1007 if (nb_candidate_validate_yang(candidate, true, errmsg,
1008 sizeof(errmsg_len))
1009 != NB_OK)
1010 return NB_ERR_VALIDATION;
1011
1012 RB_INIT(nb_config_cbs, changes);
1013 nb_config_diff(running_config, candidate, changes);
1014
1015 return NB_OK;
1016 }
1017
1018 int nb_candidate_validate(struct nb_context *context,
1019 struct nb_config *candidate, char *errmsg,
1020 size_t errmsg_len)
1021 {
1022 struct nb_config_cbs changes;
1023 int ret;
1024
1025 ret = nb_candidate_diff_and_validate_yang(context, candidate, &changes,
1026 errmsg, errmsg_len);
1027 if (ret != NB_OK)
1028 return ret;
1029
1030 ret = nb_candidate_validate_code(context, candidate, &changes, errmsg,
1031 errmsg_len);
1032 nb_config_diff_del_changes(&changes);
1033
1034 return ret;
1035 }
1036
1037 int nb_candidate_commit_prepare(struct nb_context context,
1038 struct nb_config *candidate,
1039 const char *comment,
1040 struct nb_transaction **transaction,
1041 bool skip_validate, bool ignore_zero_change,
1042 char *errmsg, size_t errmsg_len)
1043 {
1044 struct nb_config_cbs changes;
1045
1046 if (!skip_validate
1047 && nb_candidate_validate_yang(candidate, true, errmsg, errmsg_len)
1048 != NB_OK) {
1049 flog_warn(EC_LIB_NB_CANDIDATE_INVALID,
1050 "%s: failed to validate candidate configuration",
1051 __func__);
1052 return NB_ERR_VALIDATION;
1053 }
1054
1055 RB_INIT(nb_config_cbs, &changes);
1056 nb_config_diff(running_config, candidate, &changes);
1057 if (!ignore_zero_change && RB_EMPTY(nb_config_cbs, &changes)) {
1058 snprintf(
1059 errmsg, errmsg_len,
1060 "No changes to apply were found during preparation phase");
1061 return NB_ERR_NO_CHANGES;
1062 }
1063
1064 if (!skip_validate
1065 && nb_candidate_validate_code(&context, candidate, &changes, errmsg,
1066 errmsg_len)
1067 != NB_OK) {
1068 flog_warn(EC_LIB_NB_CANDIDATE_INVALID,
1069 "%s: failed to validate candidate configuration",
1070 __func__);
1071 nb_config_diff_del_changes(&changes);
1072 return NB_ERR_VALIDATION;
1073 }
1074
1075 /*
1076 * Re-use an existing transaction if provided. Else allocate a new one.
1077 */
1078 if (!*transaction)
1079 *transaction = nb_transaction_new(context, candidate, &changes,
1080 comment, errmsg, errmsg_len);
1081 if (*transaction == NULL) {
1082 flog_warn(EC_LIB_NB_TRANSACTION_CREATION_FAILED,
1083 "%s: failed to create transaction: %s", __func__,
1084 errmsg);
1085 nb_config_diff_del_changes(&changes);
1086 return NB_ERR_LOCKED;
1087 }
1088
1089 return nb_transaction_process(NB_EV_PREPARE, *transaction, errmsg,
1090 errmsg_len);
1091 }
1092
1093 void nb_candidate_commit_abort(struct nb_transaction *transaction, char *errmsg,
1094 size_t errmsg_len)
1095 {
1096 (void)nb_transaction_process(NB_EV_ABORT, transaction, errmsg,
1097 errmsg_len);
1098 nb_transaction_free(transaction);
1099 }
1100
1101 void nb_candidate_commit_apply(struct nb_transaction *transaction,
1102 bool save_transaction, uint32_t *transaction_id,
1103 char *errmsg, size_t errmsg_len)
1104 {
1105 (void)nb_transaction_process(NB_EV_APPLY, transaction, errmsg,
1106 errmsg_len);
1107 nb_transaction_apply_finish(transaction, errmsg, errmsg_len);
1108
1109 /* Replace running by candidate. */
1110 transaction->config->version++;
1111 nb_config_replace(running_config, transaction->config, true);
1112
1113 /* Record transaction. */
1114 if (save_transaction && nb_db_enabled
1115 && nb_db_transaction_save(transaction, transaction_id) != NB_OK)
1116 flog_warn(EC_LIB_NB_TRANSACTION_RECORD_FAILED,
1117 "%s: failed to record transaction", __func__);
1118
1119 nb_transaction_free(transaction);
1120 }
1121
1122 int nb_candidate_commit(struct nb_context context, struct nb_config *candidate,
1123 bool save_transaction, const char *comment,
1124 uint32_t *transaction_id, char *errmsg,
1125 size_t errmsg_len)
1126 {
1127 struct nb_transaction *transaction = NULL;
1128 int ret;
1129
1130 ret = nb_candidate_commit_prepare(context, candidate, comment,
1131 &transaction, false, false, errmsg,
1132 errmsg_len);
1133 /*
1134 * Apply the changes if the preparation phase succeeded. Otherwise abort
1135 * the transaction.
1136 */
1137 if (ret == NB_OK)
1138 nb_candidate_commit_apply(transaction, save_transaction,
1139 transaction_id, errmsg, errmsg_len);
1140 else if (transaction != NULL)
1141 nb_candidate_commit_abort(transaction, errmsg, errmsg_len);
1142
1143 return ret;
1144 }
1145
1146 int nb_running_lock(enum nb_client client, const void *user)
1147 {
1148 int ret = -1;
1149
1150 frr_with_mutex (&running_config_mgmt_lock.mtx) {
1151 if (!running_config_mgmt_lock.locked) {
1152 running_config_mgmt_lock.locked = true;
1153 running_config_mgmt_lock.owner_client = client;
1154 running_config_mgmt_lock.owner_user = user;
1155 ret = 0;
1156 }
1157 }
1158
1159 return ret;
1160 }
1161
1162 int nb_running_unlock(enum nb_client client, const void *user)
1163 {
1164 int ret = -1;
1165
1166 frr_with_mutex (&running_config_mgmt_lock.mtx) {
1167 if (running_config_mgmt_lock.locked
1168 && running_config_mgmt_lock.owner_client == client
1169 && running_config_mgmt_lock.owner_user == user) {
1170 running_config_mgmt_lock.locked = false;
1171 running_config_mgmt_lock.owner_client = NB_CLIENT_NONE;
1172 running_config_mgmt_lock.owner_user = NULL;
1173 ret = 0;
1174 }
1175 }
1176
1177 return ret;
1178 }
1179
1180 int nb_running_lock_check(enum nb_client client, const void *user)
1181 {
1182 int ret = -1;
1183
1184 frr_with_mutex (&running_config_mgmt_lock.mtx) {
1185 if (!running_config_mgmt_lock.locked
1186 || (running_config_mgmt_lock.owner_client == client
1187 && running_config_mgmt_lock.owner_user == user))
1188 ret = 0;
1189 }
1190
1191 return ret;
1192 }
1193
1194 static void nb_log_config_callback(const enum nb_event event,
1195 enum nb_operation operation,
1196 const struct lyd_node *dnode)
1197 {
1198 const char *value;
1199 char xpath[XPATH_MAXLEN];
1200
1201 if (!DEBUG_MODE_CHECK(&nb_dbg_cbs_config, DEBUG_MODE_ALL))
1202 return;
1203
1204 yang_dnode_get_path(dnode, xpath, sizeof(xpath));
1205 if (yang_snode_is_typeless_data(dnode->schema))
1206 value = "(none)";
1207 else
1208 value = yang_dnode_get_string(dnode, NULL);
1209
1210 zlog_debug(
1211 "northbound callback: event [%s] op [%s] xpath [%s] value [%s]",
1212 nb_event_name(event), nb_operation_name(operation), xpath,
1213 value);
1214 }
1215
1216 static int nb_callback_create(struct nb_context *context,
1217 const struct nb_node *nb_node,
1218 enum nb_event event, const struct lyd_node *dnode,
1219 union nb_resource *resource, char *errmsg,
1220 size_t errmsg_len)
1221 {
1222 struct nb_cb_create_args args = {};
1223 bool unexpected_error = false;
1224 int ret;
1225
1226 assert(!CHECK_FLAG(nb_node->flags, F_NB_NODE_IGNORE_CBS));
1227
1228 nb_log_config_callback(event, NB_OP_CREATE, dnode);
1229
1230 args.context = context;
1231 args.event = event;
1232 args.dnode = dnode;
1233 args.resource = resource;
1234 args.errmsg = errmsg;
1235 args.errmsg_len = errmsg_len;
1236 ret = nb_node->cbs.create(&args);
1237
1238 /* Detect and log unexpected errors. */
1239 switch (ret) {
1240 case NB_OK:
1241 case NB_ERR:
1242 break;
1243 case NB_ERR_VALIDATION:
1244 if (event != NB_EV_VALIDATE)
1245 unexpected_error = true;
1246 break;
1247 case NB_ERR_RESOURCE:
1248 if (event != NB_EV_PREPARE)
1249 unexpected_error = true;
1250 break;
1251 case NB_ERR_INCONSISTENCY:
1252 if (event == NB_EV_VALIDATE)
1253 unexpected_error = true;
1254 break;
1255 default:
1256 unexpected_error = true;
1257 break;
1258 }
1259 if (unexpected_error)
1260 DEBUGD(&nb_dbg_cbs_config,
1261 "northbound callback: unexpected return value: %s",
1262 nb_err_name(ret));
1263
1264 return ret;
1265 }
1266
1267 static int nb_callback_modify(struct nb_context *context,
1268 const struct nb_node *nb_node,
1269 enum nb_event event, const struct lyd_node *dnode,
1270 union nb_resource *resource, char *errmsg,
1271 size_t errmsg_len)
1272 {
1273 struct nb_cb_modify_args args = {};
1274 bool unexpected_error = false;
1275 int ret;
1276
1277 assert(!CHECK_FLAG(nb_node->flags, F_NB_NODE_IGNORE_CBS));
1278
1279 nb_log_config_callback(event, NB_OP_MODIFY, dnode);
1280
1281 args.context = context;
1282 args.event = event;
1283 args.dnode = dnode;
1284 args.resource = resource;
1285 args.errmsg = errmsg;
1286 args.errmsg_len = errmsg_len;
1287 ret = nb_node->cbs.modify(&args);
1288
1289 /* Detect and log unexpected errors. */
1290 switch (ret) {
1291 case NB_OK:
1292 case NB_ERR:
1293 break;
1294 case NB_ERR_VALIDATION:
1295 if (event != NB_EV_VALIDATE)
1296 unexpected_error = true;
1297 break;
1298 case NB_ERR_RESOURCE:
1299 if (event != NB_EV_PREPARE)
1300 unexpected_error = true;
1301 break;
1302 case NB_ERR_INCONSISTENCY:
1303 if (event == NB_EV_VALIDATE)
1304 unexpected_error = true;
1305 break;
1306 default:
1307 unexpected_error = true;
1308 break;
1309 }
1310 if (unexpected_error)
1311 DEBUGD(&nb_dbg_cbs_config,
1312 "northbound callback: unexpected return value: %s",
1313 nb_err_name(ret));
1314
1315 return ret;
1316 }
1317
1318 static int nb_callback_destroy(struct nb_context *context,
1319 const struct nb_node *nb_node,
1320 enum nb_event event,
1321 const struct lyd_node *dnode, char *errmsg,
1322 size_t errmsg_len)
1323 {
1324 struct nb_cb_destroy_args args = {};
1325 bool unexpected_error = false;
1326 int ret;
1327
1328 assert(!CHECK_FLAG(nb_node->flags, F_NB_NODE_IGNORE_CBS));
1329
1330 nb_log_config_callback(event, NB_OP_DESTROY, dnode);
1331
1332 args.context = context;
1333 args.event = event;
1334 args.dnode = dnode;
1335 args.errmsg = errmsg;
1336 args.errmsg_len = errmsg_len;
1337 ret = nb_node->cbs.destroy(&args);
1338
1339 /* Detect and log unexpected errors. */
1340 switch (ret) {
1341 case NB_OK:
1342 case NB_ERR:
1343 break;
1344 case NB_ERR_VALIDATION:
1345 if (event != NB_EV_VALIDATE)
1346 unexpected_error = true;
1347 break;
1348 case NB_ERR_INCONSISTENCY:
1349 if (event == NB_EV_VALIDATE)
1350 unexpected_error = true;
1351 break;
1352 default:
1353 unexpected_error = true;
1354 break;
1355 }
1356 if (unexpected_error)
1357 DEBUGD(&nb_dbg_cbs_config,
1358 "northbound callback: unexpected return value: %s",
1359 nb_err_name(ret));
1360
1361 return ret;
1362 }
1363
1364 static int nb_callback_move(struct nb_context *context,
1365 const struct nb_node *nb_node, enum nb_event event,
1366 const struct lyd_node *dnode, char *errmsg,
1367 size_t errmsg_len)
1368 {
1369 struct nb_cb_move_args args = {};
1370 bool unexpected_error = false;
1371 int ret;
1372
1373 assert(!CHECK_FLAG(nb_node->flags, F_NB_NODE_IGNORE_CBS));
1374
1375 nb_log_config_callback(event, NB_OP_MOVE, dnode);
1376
1377 args.context = context;
1378 args.event = event;
1379 args.dnode = dnode;
1380 args.errmsg = errmsg;
1381 args.errmsg_len = errmsg_len;
1382 ret = nb_node->cbs.move(&args);
1383
1384 /* Detect and log unexpected errors. */
1385 switch (ret) {
1386 case NB_OK:
1387 case NB_ERR:
1388 break;
1389 case NB_ERR_VALIDATION:
1390 if (event != NB_EV_VALIDATE)
1391 unexpected_error = true;
1392 break;
1393 case NB_ERR_INCONSISTENCY:
1394 if (event == NB_EV_VALIDATE)
1395 unexpected_error = true;
1396 break;
1397 default:
1398 unexpected_error = true;
1399 break;
1400 }
1401 if (unexpected_error)
1402 DEBUGD(&nb_dbg_cbs_config,
1403 "northbound callback: unexpected return value: %s",
1404 nb_err_name(ret));
1405
1406 return ret;
1407 }
1408
1409 static int nb_callback_pre_validate(struct nb_context *context,
1410 const struct nb_node *nb_node,
1411 const struct lyd_node *dnode, char *errmsg,
1412 size_t errmsg_len)
1413 {
1414 struct nb_cb_pre_validate_args args = {};
1415 bool unexpected_error = false;
1416 int ret;
1417
1418 if (CHECK_FLAG(nb_node->flags, F_NB_NODE_IGNORE_CBS))
1419 return 0;
1420
1421 nb_log_config_callback(NB_EV_VALIDATE, NB_OP_PRE_VALIDATE, dnode);
1422
1423 args.dnode = dnode;
1424 args.errmsg = errmsg;
1425 args.errmsg_len = errmsg_len;
1426 ret = nb_node->cbs.pre_validate(&args);
1427
1428 /* Detect and log unexpected errors. */
1429 switch (ret) {
1430 case NB_OK:
1431 case NB_ERR_VALIDATION:
1432 break;
1433 default:
1434 unexpected_error = true;
1435 break;
1436 }
1437 if (unexpected_error)
1438 DEBUGD(&nb_dbg_cbs_config,
1439 "northbound callback: unexpected return value: %s",
1440 nb_err_name(ret));
1441
1442 return ret;
1443 }
1444
1445 static void nb_callback_apply_finish(struct nb_context *context,
1446 const struct nb_node *nb_node,
1447 const struct lyd_node *dnode, char *errmsg,
1448 size_t errmsg_len)
1449 {
1450 struct nb_cb_apply_finish_args args = {};
1451
1452 if (CHECK_FLAG(nb_node->flags, F_NB_NODE_IGNORE_CBS))
1453 return;
1454
1455 nb_log_config_callback(NB_EV_APPLY, NB_OP_APPLY_FINISH, dnode);
1456
1457 args.context = context;
1458 args.dnode = dnode;
1459 args.errmsg = errmsg;
1460 args.errmsg_len = errmsg_len;
1461 nb_node->cbs.apply_finish(&args);
1462 }
1463
1464 struct yang_data *nb_callback_get_elem(const struct nb_node *nb_node,
1465 const char *xpath,
1466 const void *list_entry)
1467 {
1468 struct nb_cb_get_elem_args args = {};
1469
1470 if (CHECK_FLAG(nb_node->flags, F_NB_NODE_IGNORE_CBS))
1471 return NULL;
1472
1473 DEBUGD(&nb_dbg_cbs_state,
1474 "northbound callback (get_elem): xpath [%s] list_entry [%p]",
1475 xpath, list_entry);
1476
1477 args.xpath = xpath;
1478 args.list_entry = list_entry;
1479 return nb_node->cbs.get_elem(&args);
1480 }
1481
1482 const void *nb_callback_get_next(const struct nb_node *nb_node,
1483 const void *parent_list_entry,
1484 const void *list_entry)
1485 {
1486 struct nb_cb_get_next_args args = {};
1487
1488 if (CHECK_FLAG(nb_node->flags, F_NB_NODE_IGNORE_CBS))
1489 return NULL;
1490
1491 DEBUGD(&nb_dbg_cbs_state,
1492 "northbound callback (get_next): node [%s] parent_list_entry [%p] list_entry [%p]",
1493 nb_node->xpath, parent_list_entry, list_entry);
1494
1495 args.parent_list_entry = parent_list_entry;
1496 args.list_entry = list_entry;
1497 return nb_node->cbs.get_next(&args);
1498 }
1499
1500 int nb_callback_get_keys(const struct nb_node *nb_node, const void *list_entry,
1501 struct yang_list_keys *keys)
1502 {
1503 struct nb_cb_get_keys_args args = {};
1504
1505 if (CHECK_FLAG(nb_node->flags, F_NB_NODE_IGNORE_CBS))
1506 return 0;
1507
1508 DEBUGD(&nb_dbg_cbs_state,
1509 "northbound callback (get_keys): node [%s] list_entry [%p]",
1510 nb_node->xpath, list_entry);
1511
1512 args.list_entry = list_entry;
1513 args.keys = keys;
1514 return nb_node->cbs.get_keys(&args);
1515 }
1516
1517 const void *nb_callback_lookup_entry(const struct nb_node *nb_node,
1518 const void *parent_list_entry,
1519 const struct yang_list_keys *keys)
1520 {
1521 struct nb_cb_lookup_entry_args args = {};
1522
1523 if (CHECK_FLAG(nb_node->flags, F_NB_NODE_IGNORE_CBS))
1524 return NULL;
1525
1526 DEBUGD(&nb_dbg_cbs_state,
1527 "northbound callback (lookup_entry): node [%s] parent_list_entry [%p]",
1528 nb_node->xpath, parent_list_entry);
1529
1530 args.parent_list_entry = parent_list_entry;
1531 args.keys = keys;
1532 return nb_node->cbs.lookup_entry(&args);
1533 }
1534
1535 int nb_callback_rpc(const struct nb_node *nb_node, const char *xpath,
1536 const struct list *input, struct list *output, char *errmsg,
1537 size_t errmsg_len)
1538 {
1539 struct nb_cb_rpc_args args = {};
1540
1541 if (CHECK_FLAG(nb_node->flags, F_NB_NODE_IGNORE_CBS))
1542 return 0;
1543
1544 DEBUGD(&nb_dbg_cbs_rpc, "northbound RPC: %s", xpath);
1545
1546 args.xpath = xpath;
1547 args.input = input;
1548 args.output = output;
1549 args.errmsg = errmsg;
1550 args.errmsg_len = errmsg_len;
1551 return nb_node->cbs.rpc(&args);
1552 }
1553
1554 /*
1555 * Call the northbound configuration callback associated to a given
1556 * configuration change.
1557 */
1558 static int nb_callback_configuration(struct nb_context *context,
1559 const enum nb_event event,
1560 struct nb_config_change *change,
1561 char *errmsg, size_t errmsg_len)
1562 {
1563 enum nb_operation operation = change->cb.operation;
1564 char xpath[XPATH_MAXLEN];
1565 const struct nb_node *nb_node = change->cb.nb_node;
1566 const struct lyd_node *dnode = change->cb.dnode;
1567 union nb_resource *resource;
1568 int ret = NB_ERR;
1569
1570 if (CHECK_FLAG(nb_node->flags, F_NB_NODE_IGNORE_CBS))
1571 return NB_OK;
1572
1573 if (event == NB_EV_VALIDATE)
1574 resource = NULL;
1575 else
1576 resource = &change->resource;
1577
1578 switch (operation) {
1579 case NB_OP_CREATE:
1580 ret = nb_callback_create(context, nb_node, event, dnode,
1581 resource, errmsg, errmsg_len);
1582 break;
1583 case NB_OP_MODIFY:
1584 ret = nb_callback_modify(context, nb_node, event, dnode,
1585 resource, errmsg, errmsg_len);
1586 break;
1587 case NB_OP_DESTROY:
1588 ret = nb_callback_destroy(context, nb_node, event, dnode,
1589 errmsg, errmsg_len);
1590 break;
1591 case NB_OP_MOVE:
1592 ret = nb_callback_move(context, nb_node, event, dnode, errmsg,
1593 errmsg_len);
1594 break;
1595 case NB_OP_PRE_VALIDATE:
1596 case NB_OP_APPLY_FINISH:
1597 case NB_OP_GET_ELEM:
1598 case NB_OP_GET_NEXT:
1599 case NB_OP_GET_KEYS:
1600 case NB_OP_LOOKUP_ENTRY:
1601 case NB_OP_RPC:
1602 yang_dnode_get_path(dnode, xpath, sizeof(xpath));
1603 flog_err(EC_LIB_DEVELOPMENT,
1604 "%s: unknown operation (%u) [xpath %s]", __func__,
1605 operation, xpath);
1606 exit(1);
1607 }
1608
1609 if (ret != NB_OK) {
1610 yang_dnode_get_path(dnode, xpath, sizeof(xpath));
1611
1612 switch (event) {
1613 case NB_EV_VALIDATE:
1614 flog_warn(EC_LIB_NB_CB_CONFIG_VALIDATE,
1615 "error processing configuration change: error [%s] event [%s] operation [%s] xpath [%s]%s%s",
1616 nb_err_name(ret), nb_event_name(event),
1617 nb_operation_name(operation), xpath,
1618 errmsg[0] ? " message: " : "", errmsg);
1619 break;
1620 case NB_EV_PREPARE:
1621 flog_warn(EC_LIB_NB_CB_CONFIG_PREPARE,
1622 "error processing configuration change: error [%s] event [%s] operation [%s] xpath [%s]%s%s",
1623 nb_err_name(ret), nb_event_name(event),
1624 nb_operation_name(operation), xpath,
1625 errmsg[0] ? " message: " : "", errmsg);
1626 break;
1627 case NB_EV_ABORT:
1628 flog_warn(EC_LIB_NB_CB_CONFIG_ABORT,
1629 "error processing configuration change: error [%s] event [%s] operation [%s] xpath [%s]%s%s",
1630 nb_err_name(ret), nb_event_name(event),
1631 nb_operation_name(operation), xpath,
1632 errmsg[0] ? " message: " : "", errmsg);
1633 break;
1634 case NB_EV_APPLY:
1635 flog_err(EC_LIB_NB_CB_CONFIG_APPLY,
1636 "error processing configuration change: error [%s] event [%s] operation [%s] xpath [%s]%s%s",
1637 nb_err_name(ret), nb_event_name(event),
1638 nb_operation_name(operation), xpath,
1639 errmsg[0] ? " message: " : "", errmsg);
1640 break;
1641 default:
1642 flog_err(EC_LIB_DEVELOPMENT,
1643 "%s: unknown event (%u) [xpath %s]", __func__,
1644 event, xpath);
1645 exit(1);
1646 }
1647 }
1648
1649 return ret;
1650 }
1651
1652 static struct nb_transaction *
1653 nb_transaction_new(struct nb_context context, struct nb_config *config,
1654 struct nb_config_cbs *changes, const char *comment,
1655 char *errmsg, size_t errmsg_len)
1656 {
1657 struct nb_transaction *transaction;
1658
1659 if (nb_running_lock_check(context.client, context.user)) {
1660 strlcpy(errmsg,
1661 "running configuration is locked by another client",
1662 errmsg_len);
1663 return NULL;
1664 }
1665
1666 if (transaction_in_progress) {
1667 strlcpy(errmsg,
1668 "there's already another transaction in progress",
1669 errmsg_len);
1670 return NULL;
1671 }
1672 transaction_in_progress = true;
1673
1674 transaction = XCALLOC(MTYPE_TMP, sizeof(*transaction));
1675 transaction->context = context;
1676 if (comment)
1677 strlcpy(transaction->comment, comment,
1678 sizeof(transaction->comment));
1679 transaction->config = config;
1680 transaction->changes = *changes;
1681
1682 return transaction;
1683 }
1684
1685 static void nb_transaction_free(struct nb_transaction *transaction)
1686 {
1687 nb_config_diff_del_changes(&transaction->changes);
1688 XFREE(MTYPE_TMP, transaction);
1689 transaction_in_progress = false;
1690 }
1691
1692 /* Process all configuration changes associated to a transaction. */
1693 static int nb_transaction_process(enum nb_event event,
1694 struct nb_transaction *transaction,
1695 char *errmsg, size_t errmsg_len)
1696 {
1697 struct nb_config_cb *cb;
1698
1699 RB_FOREACH (cb, nb_config_cbs, &transaction->changes) {
1700 struct nb_config_change *change = (struct nb_config_change *)cb;
1701 int ret;
1702
1703 /*
1704 * Only try to release resources that were allocated
1705 * successfully.
1706 */
1707 if (event == NB_EV_ABORT && !change->prepare_ok)
1708 break;
1709
1710 /* Call the appropriate callback. */
1711 ret = nb_callback_configuration(&transaction->context, event,
1712 change, errmsg, errmsg_len);
1713 switch (event) {
1714 case NB_EV_PREPARE:
1715 if (ret != NB_OK)
1716 return ret;
1717 change->prepare_ok = true;
1718 break;
1719 case NB_EV_ABORT:
1720 case NB_EV_APPLY:
1721 /*
1722 * At this point it's not possible to reject the
1723 * transaction anymore, so any failure here can lead to
1724 * inconsistencies and should be treated as a bug.
1725 * Operations prone to errors, like validations and
1726 * resource allocations, should be performed during the
1727 * 'prepare' phase.
1728 */
1729 break;
1730 case NB_EV_VALIDATE:
1731 break;
1732 }
1733 }
1734
1735 return NB_OK;
1736 }
1737
1738 static struct nb_config_cb *
1739 nb_apply_finish_cb_new(struct nb_config_cbs *cbs, const struct nb_node *nb_node,
1740 const struct lyd_node *dnode)
1741 {
1742 struct nb_config_cb *cb;
1743
1744 cb = XCALLOC(MTYPE_TMP, sizeof(*cb));
1745 cb->nb_node = nb_node;
1746 cb->dnode = dnode;
1747 RB_INSERT(nb_config_cbs, cbs, cb);
1748
1749 return cb;
1750 }
1751
1752 static struct nb_config_cb *
1753 nb_apply_finish_cb_find(struct nb_config_cbs *cbs,
1754 const struct nb_node *nb_node,
1755 const struct lyd_node *dnode)
1756 {
1757 struct nb_config_cb s;
1758
1759 s.seq = 0;
1760 s.nb_node = nb_node;
1761 s.dnode = dnode;
1762 return RB_FIND(nb_config_cbs, cbs, &s);
1763 }
1764
1765 /* Call the 'apply_finish' callbacks. */
1766 static void nb_transaction_apply_finish(struct nb_transaction *transaction,
1767 char *errmsg, size_t errmsg_len)
1768 {
1769 struct nb_config_cbs cbs;
1770 struct nb_config_cb *cb;
1771
1772 /* Initialize tree of 'apply_finish' callbacks. */
1773 RB_INIT(nb_config_cbs, &cbs);
1774
1775 /* Identify the 'apply_finish' callbacks that need to be called. */
1776 RB_FOREACH (cb, nb_config_cbs, &transaction->changes) {
1777 struct nb_config_change *change = (struct nb_config_change *)cb;
1778 const struct lyd_node *dnode = change->cb.dnode;
1779
1780 /*
1781 * Iterate up to the root of the data tree. When a node is being
1782 * deleted, skip its 'apply_finish' callback if one is defined
1783 * (the 'apply_finish' callbacks from the node ancestors should
1784 * be called though).
1785 */
1786 if (change->cb.operation == NB_OP_DESTROY) {
1787 char xpath[XPATH_MAXLEN];
1788
1789 dnode = lyd_parent(dnode);
1790 if (!dnode)
1791 break;
1792
1793 /*
1794 * The dnode from 'delete' callbacks point to elements
1795 * from the running configuration. Use yang_dnode_get()
1796 * to get the corresponding dnode from the candidate
1797 * configuration that is being committed.
1798 */
1799 yang_dnode_get_path(dnode, xpath, sizeof(xpath));
1800 dnode = yang_dnode_get(transaction->config->dnode,
1801 xpath);
1802 }
1803 while (dnode) {
1804 struct nb_node *nb_node;
1805
1806 nb_node = dnode->schema->priv;
1807 if (!nb_node || !nb_node->cbs.apply_finish)
1808 goto next;
1809
1810 /*
1811 * Don't call the callback more than once for the same
1812 * data node.
1813 */
1814 if (nb_apply_finish_cb_find(&cbs, nb_node, dnode))
1815 goto next;
1816
1817 nb_apply_finish_cb_new(&cbs, nb_node, dnode);
1818
1819 next:
1820 dnode = lyd_parent(dnode);
1821 }
1822 }
1823
1824 /* Call the 'apply_finish' callbacks, sorted by their priorities. */
1825 RB_FOREACH (cb, nb_config_cbs, &cbs)
1826 nb_callback_apply_finish(&transaction->context, cb->nb_node,
1827 cb->dnode, errmsg, errmsg_len);
1828
1829 /* Release memory. */
1830 while (!RB_EMPTY(nb_config_cbs, &cbs)) {
1831 cb = RB_ROOT(nb_config_cbs, &cbs);
1832 RB_REMOVE(nb_config_cbs, &cbs, cb);
1833 XFREE(MTYPE_TMP, cb);
1834 }
1835 }
1836
1837 static int nb_oper_data_iter_children(const struct lysc_node *snode,
1838 const char *xpath, const void *list_entry,
1839 const struct yang_list_keys *list_keys,
1840 struct yang_translator *translator,
1841 bool first, uint32_t flags,
1842 nb_oper_data_cb cb, void *arg)
1843 {
1844 const struct lysc_node *child;
1845
1846 LY_LIST_FOR (lysc_node_child(snode), child) {
1847 int ret;
1848
1849 ret = nb_oper_data_iter_node(child, xpath, list_entry,
1850 list_keys, translator, false,
1851 flags, cb, arg);
1852 if (ret != NB_OK)
1853 return ret;
1854 }
1855
1856 return NB_OK;
1857 }
1858
1859 static int nb_oper_data_iter_leaf(const struct nb_node *nb_node,
1860 const char *xpath, const void *list_entry,
1861 const struct yang_list_keys *list_keys,
1862 struct yang_translator *translator,
1863 uint32_t flags, nb_oper_data_cb cb, void *arg)
1864 {
1865 struct yang_data *data;
1866
1867 if (CHECK_FLAG(nb_node->snode->flags, LYS_CONFIG_W))
1868 return NB_OK;
1869
1870 /* Ignore list keys. */
1871 if (lysc_is_key(nb_node->snode))
1872 return NB_OK;
1873
1874 data = nb_callback_get_elem(nb_node, xpath, list_entry);
1875 if (data == NULL)
1876 /* Leaf of type "empty" is not present. */
1877 return NB_OK;
1878
1879 return (*cb)(nb_node->snode, translator, data, arg);
1880 }
1881
1882 static int nb_oper_data_iter_container(const struct nb_node *nb_node,
1883 const char *xpath,
1884 const void *list_entry,
1885 const struct yang_list_keys *list_keys,
1886 struct yang_translator *translator,
1887 uint32_t flags, nb_oper_data_cb cb,
1888 void *arg)
1889 {
1890 const struct lysc_node *snode = nb_node->snode;
1891
1892 if (CHECK_FLAG(nb_node->flags, F_NB_NODE_CONFIG_ONLY))
1893 return NB_OK;
1894
1895 /* Read-only presence containers. */
1896 if (nb_node->cbs.get_elem) {
1897 struct yang_data *data;
1898 int ret;
1899
1900 data = nb_callback_get_elem(nb_node, xpath, list_entry);
1901 if (data == NULL)
1902 /* Presence container is not present. */
1903 return NB_OK;
1904
1905 ret = (*cb)(snode, translator, data, arg);
1906 if (ret != NB_OK)
1907 return ret;
1908 }
1909
1910 /* Read-write presence containers. */
1911 if (CHECK_FLAG(snode->flags, LYS_CONFIG_W)) {
1912 struct lysc_node_container *scontainer;
1913
1914 scontainer = (struct lysc_node_container *)snode;
1915 if (CHECK_FLAG(scontainer->flags, LYS_PRESENCE)
1916 && !yang_dnode_get(running_config->dnode, xpath))
1917 return NB_OK;
1918 }
1919
1920 /* Iterate over the child nodes. */
1921 return nb_oper_data_iter_children(snode, xpath, list_entry, list_keys,
1922 translator, false, flags, cb, arg);
1923 }
1924
1925 static int
1926 nb_oper_data_iter_leaflist(const struct nb_node *nb_node, const char *xpath,
1927 const void *parent_list_entry,
1928 const struct yang_list_keys *parent_list_keys,
1929 struct yang_translator *translator, uint32_t flags,
1930 nb_oper_data_cb cb, void *arg)
1931 {
1932 const void *list_entry = NULL;
1933
1934 if (CHECK_FLAG(nb_node->snode->flags, LYS_CONFIG_W))
1935 return NB_OK;
1936
1937 do {
1938 struct yang_data *data;
1939 int ret;
1940
1941 list_entry = nb_callback_get_next(nb_node, parent_list_entry,
1942 list_entry);
1943 if (!list_entry)
1944 /* End of the list. */
1945 break;
1946
1947 data = nb_callback_get_elem(nb_node, xpath, list_entry);
1948 if (data == NULL)
1949 continue;
1950
1951 ret = (*cb)(nb_node->snode, translator, data, arg);
1952 if (ret != NB_OK)
1953 return ret;
1954 } while (list_entry);
1955
1956 return NB_OK;
1957 }
1958
1959 static int nb_oper_data_iter_list(const struct nb_node *nb_node,
1960 const char *xpath_list,
1961 const void *parent_list_entry,
1962 const struct yang_list_keys *parent_list_keys,
1963 struct yang_translator *translator,
1964 uint32_t flags, nb_oper_data_cb cb, void *arg)
1965 {
1966 const struct lysc_node *snode = nb_node->snode;
1967 const void *list_entry = NULL;
1968 uint32_t position = 1;
1969
1970 if (CHECK_FLAG(nb_node->flags, F_NB_NODE_CONFIG_ONLY))
1971 return NB_OK;
1972
1973 /* Iterate over all list entries. */
1974 do {
1975 const struct lysc_node_leaf *skey;
1976 struct yang_list_keys list_keys = {};
1977 char xpath[XPATH_MAXLEN * 2];
1978 int ret;
1979
1980 /* Obtain list entry. */
1981 list_entry = nb_callback_get_next(nb_node, parent_list_entry,
1982 list_entry);
1983 if (!list_entry)
1984 /* End of the list. */
1985 break;
1986
1987 if (!CHECK_FLAG(nb_node->flags, F_NB_NODE_KEYLESS_LIST)) {
1988 /* Obtain the list entry keys. */
1989 if (nb_callback_get_keys(nb_node, list_entry,
1990 &list_keys)
1991 != NB_OK) {
1992 flog_warn(EC_LIB_NB_CB_STATE,
1993 "%s: failed to get list keys",
1994 __func__);
1995 return NB_ERR;
1996 }
1997
1998 /* Build XPath of the list entry. */
1999 strlcpy(xpath, xpath_list, sizeof(xpath));
2000 unsigned int i = 0;
2001 LY_FOR_KEYS (snode, skey) {
2002 assert(i < list_keys.num);
2003 snprintf(xpath + strlen(xpath),
2004 sizeof(xpath) - strlen(xpath),
2005 "[%s='%s']", skey->name,
2006 list_keys.key[i]);
2007 i++;
2008 }
2009 assert(i == list_keys.num);
2010 } else {
2011 /*
2012 * Keyless list - build XPath using a positional index.
2013 */
2014 snprintf(xpath, sizeof(xpath), "%s[%u]", xpath_list,
2015 position);
2016 position++;
2017 }
2018
2019 /* Iterate over the child nodes. */
2020 ret = nb_oper_data_iter_children(
2021 nb_node->snode, xpath, list_entry, &list_keys,
2022 translator, false, flags, cb, arg);
2023 if (ret != NB_OK)
2024 return ret;
2025 } while (list_entry);
2026
2027 return NB_OK;
2028 }
2029
2030 static int nb_oper_data_iter_node(const struct lysc_node *snode,
2031 const char *xpath_parent,
2032 const void *list_entry,
2033 const struct yang_list_keys *list_keys,
2034 struct yang_translator *translator,
2035 bool first, uint32_t flags,
2036 nb_oper_data_cb cb, void *arg)
2037 {
2038 struct nb_node *nb_node;
2039 char xpath[XPATH_MAXLEN];
2040 int ret = NB_OK;
2041
2042 if (!first && CHECK_FLAG(flags, NB_OPER_DATA_ITER_NORECURSE)
2043 && CHECK_FLAG(snode->nodetype, LYS_CONTAINER | LYS_LIST))
2044 return NB_OK;
2045
2046 /* Update XPath. */
2047 strlcpy(xpath, xpath_parent, sizeof(xpath));
2048 if (!first && snode->nodetype != LYS_USES) {
2049 struct lysc_node *parent;
2050
2051 /* Get the real parent. */
2052 parent = snode->parent;
2053
2054 /*
2055 * When necessary, include the namespace of the augmenting
2056 * module.
2057 */
2058 if (parent && parent->module != snode->module)
2059 snprintf(xpath + strlen(xpath),
2060 sizeof(xpath) - strlen(xpath), "/%s:%s",
2061 snode->module->name, snode->name);
2062 else
2063 snprintf(xpath + strlen(xpath),
2064 sizeof(xpath) - strlen(xpath), "/%s",
2065 snode->name);
2066 }
2067
2068 nb_node = snode->priv;
2069 switch (snode->nodetype) {
2070 case LYS_CONTAINER:
2071 ret = nb_oper_data_iter_container(nb_node, xpath, list_entry,
2072 list_keys, translator, flags,
2073 cb, arg);
2074 break;
2075 case LYS_LEAF:
2076 ret = nb_oper_data_iter_leaf(nb_node, xpath, list_entry,
2077 list_keys, translator, flags, cb,
2078 arg);
2079 break;
2080 case LYS_LEAFLIST:
2081 ret = nb_oper_data_iter_leaflist(nb_node, xpath, list_entry,
2082 list_keys, translator, flags,
2083 cb, arg);
2084 break;
2085 case LYS_LIST:
2086 ret = nb_oper_data_iter_list(nb_node, xpath, list_entry,
2087 list_keys, translator, flags, cb,
2088 arg);
2089 break;
2090 case LYS_USES:
2091 ret = nb_oper_data_iter_children(snode, xpath, list_entry,
2092 list_keys, translator, false,
2093 flags, cb, arg);
2094 break;
2095 default:
2096 break;
2097 }
2098
2099 return ret;
2100 }
2101
2102 int nb_oper_data_iterate(const char *xpath, struct yang_translator *translator,
2103 uint32_t flags, nb_oper_data_cb cb, void *arg)
2104 {
2105 struct nb_node *nb_node;
2106 const void *list_entry = NULL;
2107 struct yang_list_keys list_keys;
2108 struct list *list_dnodes;
2109 struct lyd_node *dnode, *dn;
2110 struct listnode *ln;
2111 int ret;
2112
2113 nb_node = nb_node_find(xpath);
2114 if (!nb_node) {
2115 flog_warn(EC_LIB_YANG_UNKNOWN_DATA_PATH,
2116 "%s: unknown data path: %s", __func__, xpath);
2117 return NB_ERR;
2118 }
2119
2120 /* For now this function works only with containers and lists. */
2121 if (!CHECK_FLAG(nb_node->snode->nodetype, LYS_CONTAINER | LYS_LIST)) {
2122 flog_warn(
2123 EC_LIB_NB_OPERATIONAL_DATA,
2124 "%s: can't iterate over YANG leaf or leaf-list [xpath %s]",
2125 __func__, xpath);
2126 return NB_ERR;
2127 }
2128
2129 /*
2130 * Create a data tree from the XPath so that we can parse the keys of
2131 * all YANG lists (if any).
2132 */
2133
2134 LY_ERR err = lyd_new_path(NULL, ly_native_ctx, xpath, NULL,
2135 LYD_NEW_PATH_UPDATE, &dnode);
2136 if (err || !dnode) {
2137 const char *errmsg =
2138 err ? ly_errmsg(ly_native_ctx) : "node not found";
2139 flog_warn(EC_LIB_LIBYANG, "%s: lyd_new_path() failed %s",
2140 __func__, errmsg);
2141 return NB_ERR;
2142 }
2143
2144 /*
2145 * Create a linked list to sort the data nodes starting from the root.
2146 */
2147 list_dnodes = list_new();
2148 for (dn = dnode; dn; dn = lyd_parent(dn)) {
2149 if (dn->schema->nodetype != LYS_LIST || !lyd_child(dn))
2150 continue;
2151 listnode_add_head(list_dnodes, dn);
2152 }
2153 /*
2154 * Use the northbound callbacks to find list entry pointer corresponding
2155 * to the given XPath.
2156 */
2157 for (ALL_LIST_ELEMENTS_RO(list_dnodes, ln, dn)) {
2158 struct lyd_node *child;
2159 struct nb_node *nn;
2160 unsigned int n = 0;
2161
2162 /* Obtain the list entry keys. */
2163 memset(&list_keys, 0, sizeof(list_keys));
2164 LY_LIST_FOR (lyd_child(dn), child) {
2165 if (!lysc_is_key(child->schema))
2166 break;
2167 strlcpy(list_keys.key[n],
2168 yang_dnode_get_string(child, NULL),
2169 sizeof(list_keys.key[n]));
2170 n++;
2171 }
2172 list_keys.num = n;
2173 if (list_keys.num != yang_snode_num_keys(dn->schema)) {
2174 list_delete(&list_dnodes);
2175 yang_dnode_free(dnode);
2176 return NB_ERR_NOT_FOUND;
2177 }
2178
2179 /* Find the list entry pointer. */
2180 nn = dn->schema->priv;
2181 if (!nn->cbs.lookup_entry) {
2182 flog_warn(
2183 EC_LIB_NB_OPERATIONAL_DATA,
2184 "%s: data path doesn't support iteration over operational data: %s",
2185 __func__, xpath);
2186 list_delete(&list_dnodes);
2187 yang_dnode_free(dnode);
2188 return NB_ERR;
2189 }
2190
2191 list_entry =
2192 nb_callback_lookup_entry(nn, list_entry, &list_keys);
2193 if (list_entry == NULL) {
2194 list_delete(&list_dnodes);
2195 yang_dnode_free(dnode);
2196 return NB_ERR_NOT_FOUND;
2197 }
2198 }
2199
2200 /* If a list entry was given, iterate over that list entry only. */
2201 if (dnode->schema->nodetype == LYS_LIST && lyd_child(dnode))
2202 ret = nb_oper_data_iter_children(
2203 nb_node->snode, xpath, list_entry, &list_keys,
2204 translator, true, flags, cb, arg);
2205 else
2206 ret = nb_oper_data_iter_node(nb_node->snode, xpath, list_entry,
2207 &list_keys, translator, true,
2208 flags, cb, arg);
2209
2210 list_delete(&list_dnodes);
2211 yang_dnode_free(dnode);
2212
2213 return ret;
2214 }
2215
2216 bool nb_operation_is_valid(enum nb_operation operation,
2217 const struct lysc_node *snode)
2218 {
2219 struct nb_node *nb_node = snode->priv;
2220 struct lysc_node_container *scontainer;
2221 struct lysc_node_leaf *sleaf;
2222
2223 switch (operation) {
2224 case NB_OP_CREATE:
2225 if (!CHECK_FLAG(snode->flags, LYS_CONFIG_W))
2226 return false;
2227
2228 switch (snode->nodetype) {
2229 case LYS_LEAF:
2230 sleaf = (struct lysc_node_leaf *)snode;
2231 if (sleaf->type->basetype != LY_TYPE_EMPTY)
2232 return false;
2233 break;
2234 case LYS_CONTAINER:
2235 scontainer = (struct lysc_node_container *)snode;
2236 if (!CHECK_FLAG(scontainer->flags, LYS_PRESENCE))
2237 return false;
2238 break;
2239 case LYS_LIST:
2240 case LYS_LEAFLIST:
2241 break;
2242 default:
2243 return false;
2244 }
2245 return true;
2246 case NB_OP_MODIFY:
2247 if (!CHECK_FLAG(snode->flags, LYS_CONFIG_W))
2248 return false;
2249
2250 switch (snode->nodetype) {
2251 case LYS_LEAF:
2252 sleaf = (struct lysc_node_leaf *)snode;
2253 if (sleaf->type->basetype == LY_TYPE_EMPTY)
2254 return false;
2255
2256 /* List keys can't be modified. */
2257 if (lysc_is_key(sleaf))
2258 return false;
2259 break;
2260 default:
2261 return false;
2262 }
2263 return true;
2264 case NB_OP_DESTROY:
2265 if (!CHECK_FLAG(snode->flags, LYS_CONFIG_W))
2266 return false;
2267
2268 switch (snode->nodetype) {
2269 case LYS_LEAF:
2270 sleaf = (struct lysc_node_leaf *)snode;
2271
2272 /* List keys can't be deleted. */
2273 if (lysc_is_key(sleaf))
2274 return false;
2275
2276 /*
2277 * Only optional leafs can be deleted, or leafs whose
2278 * parent is a case statement.
2279 */
2280 if (snode->parent->nodetype == LYS_CASE)
2281 return true;
2282 if (sleaf->when)
2283 return true;
2284 if (CHECK_FLAG(sleaf->flags, LYS_MAND_TRUE)
2285 || sleaf->dflt)
2286 return false;
2287 break;
2288 case LYS_CONTAINER:
2289 scontainer = (struct lysc_node_container *)snode;
2290 if (!CHECK_FLAG(scontainer->flags, LYS_PRESENCE))
2291 return false;
2292 break;
2293 case LYS_LIST:
2294 case LYS_LEAFLIST:
2295 break;
2296 default:
2297 return false;
2298 }
2299 return true;
2300 case NB_OP_MOVE:
2301 if (!CHECK_FLAG(snode->flags, LYS_CONFIG_W))
2302 return false;
2303
2304 switch (snode->nodetype) {
2305 case LYS_LIST:
2306 case LYS_LEAFLIST:
2307 if (!CHECK_FLAG(snode->flags, LYS_ORDBY_USER))
2308 return false;
2309 break;
2310 default:
2311 return false;
2312 }
2313 return true;
2314 case NB_OP_PRE_VALIDATE:
2315 case NB_OP_APPLY_FINISH:
2316 if (!CHECK_FLAG(snode->flags, LYS_CONFIG_W))
2317 return false;
2318 return true;
2319 case NB_OP_GET_ELEM:
2320 if (!CHECK_FLAG(snode->flags, LYS_CONFIG_R))
2321 return false;
2322
2323 switch (snode->nodetype) {
2324 case LYS_LEAF:
2325 case LYS_LEAFLIST:
2326 break;
2327 case LYS_CONTAINER:
2328 scontainer = (struct lysc_node_container *)snode;
2329 if (!CHECK_FLAG(scontainer->flags, LYS_PRESENCE))
2330 return false;
2331 break;
2332 default:
2333 return false;
2334 }
2335 return true;
2336 case NB_OP_GET_NEXT:
2337 switch (snode->nodetype) {
2338 case LYS_LIST:
2339 if (CHECK_FLAG(nb_node->flags, F_NB_NODE_CONFIG_ONLY))
2340 return false;
2341 break;
2342 case LYS_LEAFLIST:
2343 if (CHECK_FLAG(snode->flags, LYS_CONFIG_W))
2344 return false;
2345 break;
2346 default:
2347 return false;
2348 }
2349 return true;
2350 case NB_OP_GET_KEYS:
2351 case NB_OP_LOOKUP_ENTRY:
2352 switch (snode->nodetype) {
2353 case LYS_LIST:
2354 if (CHECK_FLAG(nb_node->flags, F_NB_NODE_CONFIG_ONLY))
2355 return false;
2356 if (CHECK_FLAG(nb_node->flags, F_NB_NODE_KEYLESS_LIST))
2357 return false;
2358 break;
2359 default:
2360 return false;
2361 }
2362 return true;
2363 case NB_OP_RPC:
2364 if (CHECK_FLAG(snode->flags, LYS_CONFIG_W | LYS_CONFIG_R))
2365 return false;
2366
2367 switch (snode->nodetype) {
2368 case LYS_RPC:
2369 case LYS_ACTION:
2370 break;
2371 default:
2372 return false;
2373 }
2374 return true;
2375 default:
2376 return false;
2377 }
2378 }
2379
2380 DEFINE_HOOK(nb_notification_send, (const char *xpath, struct list *arguments),
2381 (xpath, arguments));
2382
2383 int nb_notification_send(const char *xpath, struct list *arguments)
2384 {
2385 int ret;
2386
2387 DEBUGD(&nb_dbg_notif, "northbound notification: %s", xpath);
2388
2389 ret = hook_call(nb_notification_send, xpath, arguments);
2390 if (arguments)
2391 list_delete(&arguments);
2392
2393 return ret;
2394 }
2395
2396 /* Running configuration user pointers management. */
2397 struct nb_config_entry {
2398 char xpath[XPATH_MAXLEN];
2399 void *entry;
2400 };
2401
2402 static bool running_config_entry_cmp(const void *value1, const void *value2)
2403 {
2404 const struct nb_config_entry *c1 = value1;
2405 const struct nb_config_entry *c2 = value2;
2406
2407 return strmatch(c1->xpath, c2->xpath);
2408 }
2409
2410 static unsigned int running_config_entry_key_make(const void *value)
2411 {
2412 return string_hash_make(value);
2413 }
2414
2415 static void *running_config_entry_alloc(void *p)
2416 {
2417 struct nb_config_entry *new, *key = p;
2418
2419 new = XCALLOC(MTYPE_NB_CONFIG_ENTRY, sizeof(*new));
2420 strlcpy(new->xpath, key->xpath, sizeof(new->xpath));
2421
2422 return new;
2423 }
2424
2425 static void running_config_entry_free(void *arg)
2426 {
2427 XFREE(MTYPE_NB_CONFIG_ENTRY, arg);
2428 }
2429
2430 void nb_running_set_entry(const struct lyd_node *dnode, void *entry)
2431 {
2432 struct nb_config_entry *config, s;
2433
2434 yang_dnode_get_path(dnode, s.xpath, sizeof(s.xpath));
2435 config = hash_get(running_config_entries, &s,
2436 running_config_entry_alloc);
2437 config->entry = entry;
2438 }
2439
2440 void nb_running_move_tree(const char *xpath_from, const char *xpath_to)
2441 {
2442 struct nb_config_entry *entry;
2443 struct list *entries = hash_to_list(running_config_entries);
2444 struct listnode *ln;
2445
2446 for (ALL_LIST_ELEMENTS_RO(entries, ln, entry)) {
2447 if (!frrstr_startswith(entry->xpath, xpath_from))
2448 continue;
2449
2450 hash_release(running_config_entries, entry);
2451
2452 char *newpath =
2453 frrstr_replace(entry->xpath, xpath_from, xpath_to);
2454 strlcpy(entry->xpath, newpath, sizeof(entry->xpath));
2455 XFREE(MTYPE_TMP, newpath);
2456
2457 (void)hash_get(running_config_entries, entry,
2458 hash_alloc_intern);
2459 }
2460
2461 list_delete(&entries);
2462 }
2463
2464 static void *nb_running_unset_entry_helper(const struct lyd_node *dnode)
2465 {
2466 struct nb_config_entry *config, s;
2467 struct lyd_node *child;
2468 void *entry = NULL;
2469
2470 yang_dnode_get_path(dnode, s.xpath, sizeof(s.xpath));
2471 config = hash_release(running_config_entries, &s);
2472 if (config) {
2473 entry = config->entry;
2474 running_config_entry_free(config);
2475 }
2476
2477 /* Unset user pointers from the child nodes. */
2478 if (CHECK_FLAG(dnode->schema->nodetype, LYS_LIST | LYS_CONTAINER)) {
2479 LY_LIST_FOR (lyd_child(dnode), child) {
2480 (void)nb_running_unset_entry_helper(child);
2481 }
2482 }
2483
2484 return entry;
2485 }
2486
2487 void *nb_running_unset_entry(const struct lyd_node *dnode)
2488 {
2489 void *entry;
2490
2491 entry = nb_running_unset_entry_helper(dnode);
2492 assert(entry);
2493
2494 return entry;
2495 }
2496
2497 static void *nb_running_get_entry_worker(const struct lyd_node *dnode,
2498 const char *xpath,
2499 bool abort_if_not_found,
2500 bool rec_search)
2501 {
2502 const struct lyd_node *orig_dnode = dnode;
2503 char xpath_buf[XPATH_MAXLEN];
2504 bool rec_flag = true;
2505
2506 assert(dnode || xpath);
2507
2508 if (!dnode)
2509 dnode = yang_dnode_get(running_config->dnode, xpath);
2510
2511 while (rec_flag && dnode) {
2512 struct nb_config_entry *config, s;
2513
2514 yang_dnode_get_path(dnode, s.xpath, sizeof(s.xpath));
2515 config = hash_lookup(running_config_entries, &s);
2516 if (config)
2517 return config->entry;
2518
2519 rec_flag = rec_search;
2520
2521 dnode = lyd_parent(dnode);
2522 }
2523
2524 if (!abort_if_not_found)
2525 return NULL;
2526
2527 yang_dnode_get_path(orig_dnode, xpath_buf, sizeof(xpath_buf));
2528 flog_err(EC_LIB_YANG_DNODE_NOT_FOUND,
2529 "%s: failed to find entry [xpath %s]", __func__, xpath_buf);
2530 zlog_backtrace(LOG_ERR);
2531 abort();
2532 }
2533
2534 void *nb_running_get_entry(const struct lyd_node *dnode, const char *xpath,
2535 bool abort_if_not_found)
2536 {
2537 return nb_running_get_entry_worker(dnode, xpath, abort_if_not_found,
2538 true);
2539 }
2540
2541 void *nb_running_get_entry_non_rec(const struct lyd_node *dnode,
2542 const char *xpath, bool abort_if_not_found)
2543 {
2544 return nb_running_get_entry_worker(dnode, xpath, abort_if_not_found,
2545 false);
2546 }
2547
2548 /* Logging functions. */
2549 const char *nb_event_name(enum nb_event event)
2550 {
2551 switch (event) {
2552 case NB_EV_VALIDATE:
2553 return "validate";
2554 case NB_EV_PREPARE:
2555 return "prepare";
2556 case NB_EV_ABORT:
2557 return "abort";
2558 case NB_EV_APPLY:
2559 return "apply";
2560 }
2561
2562 assert(!"Reached end of function we should never hit");
2563 }
2564
2565 const char *nb_operation_name(enum nb_operation operation)
2566 {
2567 switch (operation) {
2568 case NB_OP_CREATE:
2569 return "create";
2570 case NB_OP_MODIFY:
2571 return "modify";
2572 case NB_OP_DESTROY:
2573 return "destroy";
2574 case NB_OP_MOVE:
2575 return "move";
2576 case NB_OP_PRE_VALIDATE:
2577 return "pre_validate";
2578 case NB_OP_APPLY_FINISH:
2579 return "apply_finish";
2580 case NB_OP_GET_ELEM:
2581 return "get_elem";
2582 case NB_OP_GET_NEXT:
2583 return "get_next";
2584 case NB_OP_GET_KEYS:
2585 return "get_keys";
2586 case NB_OP_LOOKUP_ENTRY:
2587 return "lookup_entry";
2588 case NB_OP_RPC:
2589 return "rpc";
2590 }
2591
2592 assert(!"Reached end of function we should never hit");
2593 }
2594
2595 const char *nb_err_name(enum nb_error error)
2596 {
2597 switch (error) {
2598 case NB_OK:
2599 return "ok";
2600 case NB_ERR:
2601 return "generic error";
2602 case NB_ERR_NO_CHANGES:
2603 return "no changes";
2604 case NB_ERR_NOT_FOUND:
2605 return "element not found";
2606 case NB_ERR_LOCKED:
2607 return "resource is locked";
2608 case NB_ERR_VALIDATION:
2609 return "validation";
2610 case NB_ERR_RESOURCE:
2611 return "failed to allocate resource";
2612 case NB_ERR_INCONSISTENCY:
2613 return "internal inconsistency";
2614 }
2615
2616 assert(!"Reached end of function we should never hit");
2617 }
2618
2619 const char *nb_client_name(enum nb_client client)
2620 {
2621 switch (client) {
2622 case NB_CLIENT_CLI:
2623 return "CLI";
2624 case NB_CLIENT_CONFD:
2625 return "ConfD";
2626 case NB_CLIENT_SYSREPO:
2627 return "Sysrepo";
2628 case NB_CLIENT_GRPC:
2629 return "gRPC";
2630 case NB_CLIENT_PCEP:
2631 return "Pcep";
2632 case NB_CLIENT_MGMTD_SERVER:
2633 return "MGMTD Server";
2634 case NB_CLIENT_MGMTD_BE:
2635 return "MGMT Backend";
2636 case NB_CLIENT_NONE:
2637 return "None";
2638 }
2639
2640 assert(!"Reached end of function we should never hit");
2641 }
2642
2643 static void nb_load_callbacks(const struct frr_yang_module_info *module)
2644 {
2645
2646 if (module->ignore_cbs)
2647 return;
2648
2649 for (size_t i = 0; module->nodes[i].xpath; i++) {
2650 struct nb_node *nb_node;
2651 uint32_t priority;
2652
2653 if (i > YANG_MODULE_MAX_NODES) {
2654 zlog_err(
2655 "%s: %s.yang has more than %u nodes. Please increase YANG_MODULE_MAX_NODES to fix this problem.",
2656 __func__, module->name, YANG_MODULE_MAX_NODES);
2657 exit(1);
2658 }
2659
2660 nb_node = nb_node_find(module->nodes[i].xpath);
2661 if (!nb_node) {
2662 flog_warn(EC_LIB_YANG_UNKNOWN_DATA_PATH,
2663 "%s: unknown data path: %s", __func__,
2664 module->nodes[i].xpath);
2665 continue;
2666 }
2667
2668 nb_node->cbs = module->nodes[i].cbs;
2669 priority = module->nodes[i].priority;
2670 if (priority != 0)
2671 nb_node->priority = priority;
2672 }
2673 }
2674
2675 void nb_validate_callbacks(void)
2676 {
2677 unsigned int errors = 0;
2678
2679 yang_snodes_iterate(NULL, nb_node_validate, 0, &errors);
2680 if (errors > 0) {
2681 flog_err(
2682 EC_LIB_NB_CBS_VALIDATION,
2683 "%s: failed to validate northbound callbacks: %u error(s)",
2684 __func__, errors);
2685 exit(1);
2686 }
2687 }
2688
2689
2690 void nb_init(struct thread_master *tm,
2691 const struct frr_yang_module_info *const modules[],
2692 size_t nmodules, bool db_enabled)
2693 {
2694 struct yang_module *loaded[nmodules], **loadedp = loaded;
2695 bool explicit_compile;
2696
2697 /*
2698 * Currently using this explicit compile feature in libyang2 leads to
2699 * incorrect behavior in FRR. The functionality suppresses the compiling
2700 * of modules until they have all been loaded into the context. This
2701 * avoids multiple recompiles of the same modules as they are
2702 * imported/augmented etc.
2703 */
2704 explicit_compile = false;
2705
2706 nb_db_enabled = db_enabled;
2707
2708 yang_init(true, explicit_compile);
2709
2710 /* Load YANG modules and their corresponding northbound callbacks. */
2711 for (size_t i = 0; i < nmodules; i++) {
2712 DEBUGD(&nb_dbg_events, "northbound: loading %s.yang",
2713 modules[i]->name);
2714 *loadedp++ = yang_module_load(modules[i]->name);
2715 }
2716
2717 if (explicit_compile)
2718 yang_init_loading_complete();
2719
2720 /* Initialize the compiled nodes with northbound data */
2721 for (size_t i = 0; i < nmodules; i++) {
2722 yang_snodes_iterate(loaded[i]->info, nb_node_new_cb, 0,
2723 (void *)modules[i]);
2724 nb_load_callbacks(modules[i]);
2725 }
2726
2727 /* Validate northbound callbacks. */
2728 nb_validate_callbacks();
2729
2730 /* Create an empty running configuration. */
2731 running_config = nb_config_new(NULL);
2732 running_config_entries = hash_create(running_config_entry_key_make,
2733 running_config_entry_cmp,
2734 "Running Configuration Entries");
2735 pthread_mutex_init(&running_config_mgmt_lock.mtx, NULL);
2736
2737 /* Initialize the northbound CLI. */
2738 nb_cli_init(tm);
2739 }
2740
2741 void nb_terminate(void)
2742 {
2743 /* Terminate the northbound CLI. */
2744 nb_cli_terminate();
2745
2746 /* Delete all nb_node's from all YANG modules. */
2747 nb_nodes_delete();
2748
2749 /* Delete the running configuration. */
2750 hash_clean(running_config_entries, running_config_entry_free);
2751 hash_free(running_config_entries);
2752 nb_config_free(running_config);
2753 pthread_mutex_destroy(&running_config_mgmt_lock.mtx);
2754 }