]> git.proxmox.com Git - mirror_frr.git/blob - lib/northbound.c
fd71579ff02d4e7cb26ba9d3afbaaa8a185d65cf
[mirror_frr.git] / lib / northbound.c
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 #include <zebra.h>
21
22 #include "libfrr.h"
23 #include "log.h"
24 #include "lib_errors.h"
25 #include "command.h"
26 #include "db.h"
27 #include "northbound.h"
28 #include "northbound_cli.h"
29 #include "northbound_db.h"
30
31 DEFINE_MTYPE_STATIC(LIB, NB_NODE, "Northbound Node")
32 DEFINE_MTYPE_STATIC(LIB, NB_CONFIG, "Northbound Configuration")
33
34 /* Running configuration - shouldn't be modified directly. */
35 struct nb_config *running_config;
36
37 /*
38 * Global lock used to prevent multiple configuration transactions from
39 * happening concurrently.
40 */
41 static bool transaction_in_progress;
42
43 static int nb_configuration_callback(const enum nb_event event,
44 struct nb_config_change *change);
45 static struct nb_transaction *nb_transaction_new(struct nb_config *config,
46 struct nb_config_cbs *changes,
47 enum nb_client client,
48 const char *comment);
49 static void nb_transaction_free(struct nb_transaction *transaction);
50 static int nb_transaction_process(enum nb_event event,
51 struct nb_transaction *transaction);
52 static void nb_transaction_apply_finish(struct nb_transaction *transaction);
53 static int nb_oper_data_iter_node(const struct lys_node *snode,
54 const char *xpath, const void *list_entry,
55 const struct yang_list_keys *list_keys,
56 struct yang_translator *translator,
57 bool first, uint32_t flags,
58 nb_oper_data_cb cb, void *arg);
59
60 static int nb_node_check_config_only(const struct lys_node *snode, void *arg)
61 {
62 bool *config_only = arg;
63
64 if (CHECK_FLAG(snode->flags, LYS_CONFIG_R)) {
65 *config_only = false;
66 return YANG_ITER_STOP;
67 }
68
69 return YANG_ITER_CONTINUE;
70 }
71
72 static int nb_node_new_cb(const struct lys_node *snode, void *arg)
73 {
74 struct nb_node *nb_node;
75 struct lys_node *sparent, *sparent_list;
76
77 nb_node = XCALLOC(MTYPE_NB_NODE, sizeof(*nb_node));
78 yang_snode_get_path(snode, YANG_PATH_DATA, nb_node->xpath,
79 sizeof(nb_node->xpath));
80 nb_node->priority = NB_DFLT_PRIORITY;
81 sparent = yang_snode_real_parent(snode);
82 if (sparent)
83 nb_node->parent = sparent->priv;
84 sparent_list = yang_snode_parent_list(snode);
85 if (sparent_list)
86 nb_node->parent_list = sparent_list->priv;
87
88 /* Set flags. */
89 if (CHECK_FLAG(snode->nodetype, LYS_CONTAINER | LYS_LIST)) {
90 bool config_only = true;
91
92 yang_snodes_iterate_subtree(snode, nb_node_check_config_only,
93 YANG_ITER_ALLOW_AUGMENTATIONS,
94 &config_only);
95 if (config_only)
96 SET_FLAG(nb_node->flags, F_NB_NODE_CONFIG_ONLY);
97 }
98 if (CHECK_FLAG(snode->nodetype, LYS_LIST)) {
99 struct lys_node_list *slist;
100
101 slist = (struct lys_node_list *)snode;
102 if (slist->keys_size == 0)
103 SET_FLAG(nb_node->flags, F_NB_NODE_KEYLESS_LIST);
104 }
105
106 /*
107 * Link the northbound node and the libyang schema node with one
108 * another.
109 */
110 nb_node->snode = snode;
111 lys_set_private(snode, nb_node);
112
113 return YANG_ITER_CONTINUE;
114 }
115
116 static int nb_node_del_cb(const struct lys_node *snode, void *arg)
117 {
118 struct nb_node *nb_node;
119
120 nb_node = snode->priv;
121 lys_set_private(snode, NULL);
122 XFREE(MTYPE_NB_NODE, nb_node);
123
124 return YANG_ITER_CONTINUE;
125 }
126
127 void nb_nodes_create(void)
128 {
129 yang_snodes_iterate_all(nb_node_new_cb, 0, NULL);
130 }
131
132 void nb_nodes_delete(void)
133 {
134 yang_snodes_iterate_all(nb_node_del_cb, 0, NULL);
135 }
136
137 struct nb_node *nb_node_find(const char *xpath)
138 {
139 const struct lys_node *snode;
140
141 /*
142 * Use libyang to find the schema node associated to the xpath and get
143 * the northbound node from there (snode private pointer).
144 */
145 snode = ly_ctx_get_node(ly_native_ctx, NULL, xpath, 0);
146 if (!snode)
147 return NULL;
148
149 return snode->priv;
150 }
151
152 static int nb_node_validate_cb(const struct nb_node *nb_node,
153 enum nb_operation operation,
154 int callback_implemented, bool optional)
155 {
156 bool valid;
157
158 valid = nb_operation_is_valid(operation, nb_node->snode);
159
160 if (!valid && callback_implemented)
161 flog_warn(EC_LIB_NB_CB_UNNEEDED,
162 "unneeded '%s' callback for '%s'",
163 nb_operation_name(operation), nb_node->xpath);
164
165 if (!optional && valid && !callback_implemented) {
166 flog_err(EC_LIB_NB_CB_MISSING, "missing '%s' callback for '%s'",
167 nb_operation_name(operation), nb_node->xpath);
168 return 1;
169 }
170
171 return 0;
172 }
173
174 /*
175 * Check if the required callbacks were implemented for the given northbound
176 * node.
177 */
178 static unsigned int nb_node_validate_cbs(const struct nb_node *nb_node)
179
180 {
181 unsigned int error = 0;
182
183 error += nb_node_validate_cb(nb_node, NB_OP_CREATE,
184 !!nb_node->cbs.create, false);
185 error += nb_node_validate_cb(nb_node, NB_OP_MODIFY,
186 !!nb_node->cbs.modify, false);
187 error += nb_node_validate_cb(nb_node, NB_OP_DESTROY,
188 !!nb_node->cbs.destroy, false);
189 error += nb_node_validate_cb(nb_node, NB_OP_MOVE, !!nb_node->cbs.move,
190 false);
191 error += nb_node_validate_cb(nb_node, NB_OP_APPLY_FINISH,
192 !!nb_node->cbs.apply_finish, true);
193 error += nb_node_validate_cb(nb_node, NB_OP_GET_ELEM,
194 !!nb_node->cbs.get_elem, false);
195 error += nb_node_validate_cb(nb_node, NB_OP_GET_NEXT,
196 !!nb_node->cbs.get_next, false);
197 error += nb_node_validate_cb(nb_node, NB_OP_GET_KEYS,
198 !!nb_node->cbs.get_keys, false);
199 error += nb_node_validate_cb(nb_node, NB_OP_LOOKUP_ENTRY,
200 !!nb_node->cbs.lookup_entry, false);
201 error += nb_node_validate_cb(nb_node, NB_OP_RPC, !!nb_node->cbs.rpc,
202 false);
203
204 return error;
205 }
206
207 static unsigned int nb_node_validate_priority(const struct nb_node *nb_node)
208 {
209 /* Top-level nodes can have any priority. */
210 if (!nb_node->parent)
211 return 0;
212
213 if (nb_node->priority < nb_node->parent->priority) {
214 flog_err(EC_LIB_NB_CB_INVALID_PRIO,
215 "node has higher priority than its parent [xpath %s]",
216 nb_node->xpath);
217 return 1;
218 }
219
220 return 0;
221 }
222
223 static int nb_node_validate(const struct lys_node *snode, void *arg)
224 {
225 struct nb_node *nb_node = snode->priv;
226 unsigned int *errors = arg;
227
228 /* Validate callbacks and priority. */
229 *errors += nb_node_validate_cbs(nb_node);
230 *errors += nb_node_validate_priority(nb_node);
231
232 return YANG_ITER_CONTINUE;
233 }
234
235 struct nb_config *nb_config_new(struct lyd_node *dnode)
236 {
237 struct nb_config *config;
238
239 config = XCALLOC(MTYPE_NB_CONFIG, sizeof(*config));
240 if (dnode)
241 config->dnode = dnode;
242 else
243 config->dnode = yang_dnode_new(ly_native_ctx, true);
244 config->version = 0;
245
246 return config;
247 }
248
249 void nb_config_free(struct nb_config *config)
250 {
251 if (config->dnode)
252 yang_dnode_free(config->dnode);
253 XFREE(MTYPE_NB_CONFIG, config);
254 }
255
256 struct nb_config *nb_config_dup(const struct nb_config *config)
257 {
258 struct nb_config *dup;
259
260 dup = XCALLOC(MTYPE_NB_CONFIG, sizeof(*dup));
261 dup->dnode = yang_dnode_dup(config->dnode);
262 dup->version = config->version;
263
264 return dup;
265 }
266
267 int nb_config_merge(struct nb_config *config_dst, struct nb_config *config_src,
268 bool preserve_source)
269 {
270 int ret;
271
272 ret = lyd_merge(config_dst->dnode, config_src->dnode, LYD_OPT_EXPLICIT);
273 if (ret != 0)
274 flog_warn(EC_LIB_LIBYANG, "%s: lyd_merge() failed", __func__);
275
276 if (!preserve_source)
277 nb_config_free(config_src);
278
279 return (ret == 0) ? NB_OK : NB_ERR;
280 }
281
282 void nb_config_replace(struct nb_config *config_dst,
283 struct nb_config *config_src, bool preserve_source)
284 {
285 /* Update version. */
286 if (config_src->version != 0)
287 config_dst->version = config_src->version;
288
289 /* Update dnode. */
290 if (config_dst->dnode)
291 yang_dnode_free(config_dst->dnode);
292 if (preserve_source) {
293 config_dst->dnode = yang_dnode_dup(config_src->dnode);
294 } else {
295 config_dst->dnode = config_src->dnode;
296 config_src->dnode = NULL;
297 nb_config_free(config_src);
298 }
299 }
300
301 /* Generate the nb_config_cbs tree. */
302 static inline int nb_config_cb_compare(const struct nb_config_cb *a,
303 const struct nb_config_cb *b)
304 {
305 /* Sort by priority first. */
306 if (a->nb_node->priority < b->nb_node->priority)
307 return -1;
308 if (a->nb_node->priority > b->nb_node->priority)
309 return 1;
310
311 /*
312 * Use XPath as a tie-breaker. This will naturally sort parent nodes
313 * before their children.
314 */
315 return strcmp(a->xpath, b->xpath);
316 }
317 RB_GENERATE(nb_config_cbs, nb_config_cb, entry, nb_config_cb_compare);
318
319 static void nb_config_diff_add_change(struct nb_config_cbs *changes,
320 enum nb_operation operation,
321 const struct lyd_node *dnode)
322 {
323 struct nb_config_change *change;
324
325 change = XCALLOC(MTYPE_TMP, sizeof(*change));
326 change->cb.operation = operation;
327 change->cb.nb_node = dnode->schema->priv;
328 yang_dnode_get_path(dnode, change->cb.xpath, sizeof(change->cb.xpath));
329 change->cb.dnode = dnode;
330
331 RB_INSERT(nb_config_cbs, changes, &change->cb);
332 }
333
334 static void nb_config_diff_del_changes(struct nb_config_cbs *changes)
335 {
336 while (!RB_EMPTY(nb_config_cbs, changes)) {
337 struct nb_config_change *change;
338
339 change = (struct nb_config_change *)RB_ROOT(nb_config_cbs,
340 changes);
341 RB_REMOVE(nb_config_cbs, changes, &change->cb);
342 XFREE(MTYPE_TMP, change);
343 }
344 }
345
346 /*
347 * Helper function used when calculating the delta between two different
348 * configurations. Given a new subtree, calculate all new YANG data nodes,
349 * excluding default leafs and leaf-lists. This is a recursive function.
350 */
351 static void nb_config_diff_created(const struct lyd_node *dnode,
352 struct nb_config_cbs *changes)
353 {
354 enum nb_operation operation;
355 struct lyd_node *child;
356
357 switch (dnode->schema->nodetype) {
358 case LYS_LEAF:
359 case LYS_LEAFLIST:
360 if (lyd_wd_default((struct lyd_node_leaf_list *)dnode))
361 break;
362
363 if (nb_operation_is_valid(NB_OP_CREATE, dnode->schema))
364 operation = NB_OP_CREATE;
365 else if (nb_operation_is_valid(NB_OP_MODIFY, dnode->schema))
366 operation = NB_OP_MODIFY;
367 else
368 return;
369
370 nb_config_diff_add_change(changes, operation, dnode);
371 break;
372 case LYS_CONTAINER:
373 case LYS_LIST:
374 if (nb_operation_is_valid(NB_OP_CREATE, dnode->schema))
375 nb_config_diff_add_change(changes, NB_OP_CREATE, dnode);
376
377 /* Process child nodes recursively. */
378 LY_TREE_FOR (dnode->child, child) {
379 nb_config_diff_created(child, changes);
380 }
381 break;
382 default:
383 break;
384 }
385 }
386
387 /* Calculate the delta between two different configurations. */
388 static void nb_config_diff(const struct nb_config *config1,
389 const struct nb_config *config2,
390 struct nb_config_cbs *changes)
391 {
392 struct lyd_difflist *diff;
393
394 diff = lyd_diff(config1->dnode, config2->dnode,
395 LYD_DIFFOPT_WITHDEFAULTS);
396 assert(diff);
397
398 for (int i = 0; diff->type[i] != LYD_DIFF_END; i++) {
399 LYD_DIFFTYPE type;
400 struct lyd_node *dnode;
401
402 type = diff->type[i];
403
404 switch (type) {
405 case LYD_DIFF_CREATED:
406 dnode = diff->second[i];
407 nb_config_diff_created(dnode, changes);
408 break;
409 case LYD_DIFF_DELETED:
410 dnode = diff->first[i];
411 nb_config_diff_add_change(changes, NB_OP_DESTROY,
412 dnode);
413 break;
414 case LYD_DIFF_CHANGED:
415 dnode = diff->second[i];
416 nb_config_diff_add_change(changes, NB_OP_MODIFY, dnode);
417 break;
418 case LYD_DIFF_MOVEDAFTER1:
419 case LYD_DIFF_MOVEDAFTER2:
420 default:
421 continue;
422 }
423 }
424
425 lyd_free_diff(diff);
426 }
427
428 int nb_candidate_edit(struct nb_config *candidate,
429 const struct nb_node *nb_node,
430 enum nb_operation operation, const char *xpath,
431 const struct yang_data *previous,
432 const struct yang_data *data)
433 {
434 struct lyd_node *dnode;
435 char xpath_edit[XPATH_MAXLEN];
436
437 if (!nb_operation_is_valid(operation, nb_node->snode)) {
438 flog_warn(EC_LIB_NB_CANDIDATE_EDIT_ERROR,
439 "%s: %s operation not valid for %s", __func__,
440 nb_operation_name(operation), xpath);
441 return NB_ERR;
442 }
443
444 /* Use special notation for leaf-lists (RFC 6020, section 9.13.5). */
445 if (nb_node->snode->nodetype == LYS_LEAFLIST)
446 snprintf(xpath_edit, sizeof(xpath_edit), "%s[.='%s']", xpath,
447 data->value);
448 else
449 strlcpy(xpath_edit, xpath, sizeof(xpath_edit));
450
451 switch (operation) {
452 case NB_OP_CREATE:
453 case NB_OP_MODIFY:
454 ly_errno = 0;
455 dnode = lyd_new_path(candidate->dnode, ly_native_ctx,
456 xpath_edit, (void *)data->value, 0,
457 LYD_PATH_OPT_UPDATE);
458 if (!dnode && ly_errno) {
459 flog_warn(EC_LIB_LIBYANG, "%s: lyd_new_path() failed",
460 __func__);
461 return NB_ERR;
462 }
463
464 /*
465 * If a new node was created, call lyd_validate() only to create
466 * default child nodes.
467 */
468 if (dnode) {
469 lyd_schema_sort(dnode, 0);
470 lyd_validate(&dnode, LYD_OPT_CONFIG, ly_native_ctx);
471 }
472 break;
473 case NB_OP_DESTROY:
474 dnode = yang_dnode_get(candidate->dnode, xpath_edit);
475 if (!dnode)
476 /*
477 * Return a special error code so the caller can choose
478 * whether to ignore it or not.
479 */
480 return NB_ERR_NOT_FOUND;
481 lyd_free(dnode);
482 break;
483 case NB_OP_MOVE:
484 /* TODO: update configuration. */
485 break;
486 default:
487 flog_warn(EC_LIB_DEVELOPMENT,
488 "%s: unknown operation (%u) [xpath %s]", __func__,
489 operation, xpath_edit);
490 return NB_ERR;
491 }
492
493 return NB_OK;
494 }
495
496 bool nb_candidate_needs_update(const struct nb_config *candidate)
497 {
498 if (candidate->version < running_config->version)
499 return true;
500
501 return false;
502 }
503
504 int nb_candidate_update(struct nb_config *candidate)
505 {
506 struct nb_config *updated_config;
507
508 updated_config = nb_config_dup(running_config);
509 if (nb_config_merge(updated_config, candidate, true) != NB_OK)
510 return NB_ERR;
511
512 nb_config_replace(candidate, updated_config, false);
513
514 return NB_OK;
515 }
516
517 /*
518 * The northbound configuration callbacks use the 'priv' pointer present in the
519 * libyang lyd_node structure to store pointers to FRR internal variables
520 * associated to YANG lists and presence containers. Before commiting a
521 * candidate configuration, we must restore the 'priv' pointers stored in the
522 * running configuration since they might be lost while editing the candidate.
523 */
524 static void nb_candidate_restore_priv_pointers(struct nb_config *candidate)
525 {
526 struct lyd_node *root, *next, *dnode_iter;
527
528 LY_TREE_FOR (running_config->dnode, root) {
529 LY_TREE_DFS_BEGIN (root, next, dnode_iter) {
530 struct lyd_node *dnode_candidate;
531 char xpath[XPATH_MAXLEN];
532
533 if (!dnode_iter->priv)
534 goto next;
535
536 yang_dnode_get_path(dnode_iter, xpath, sizeof(xpath));
537 dnode_candidate =
538 yang_dnode_get(candidate->dnode, xpath);
539 if (dnode_candidate)
540 yang_dnode_set_entry(dnode_candidate,
541 dnode_iter->priv);
542
543 next:
544 LY_TREE_DFS_END(root, next, dnode_iter);
545 }
546 }
547 }
548
549 /*
550 * Perform YANG syntactic and semantic validation.
551 *
552 * WARNING: lyd_validate() can change the configuration as part of the
553 * validation process.
554 */
555 static int nb_candidate_validate_yang(struct nb_config *candidate)
556 {
557 if (lyd_validate(&candidate->dnode, LYD_OPT_STRICT | LYD_OPT_CONFIG,
558 ly_native_ctx)
559 != 0)
560 return NB_ERR_VALIDATION;
561
562 return NB_OK;
563 }
564
565 /* Perform code-level validation using the northbound callbacks. */
566 static int nb_candidate_validate_changes(struct nb_config *candidate,
567 struct nb_config_cbs *changes)
568 {
569 struct nb_config_cb *cb;
570
571 nb_candidate_restore_priv_pointers(candidate);
572 RB_FOREACH (cb, nb_config_cbs, changes) {
573 struct nb_config_change *change = (struct nb_config_change *)cb;
574 int ret;
575
576 ret = nb_configuration_callback(NB_EV_VALIDATE, change);
577 if (ret != NB_OK)
578 return NB_ERR_VALIDATION;
579 }
580
581 return NB_OK;
582 }
583
584 int nb_candidate_validate(struct nb_config *candidate)
585 {
586 struct nb_config_cbs changes;
587 int ret;
588
589 if (nb_candidate_validate_yang(candidate) != NB_OK)
590 return NB_ERR_VALIDATION;
591
592 RB_INIT(nb_config_cbs, &changes);
593 nb_config_diff(running_config, candidate, &changes);
594 ret = nb_candidate_validate_changes(candidate, &changes);
595 nb_config_diff_del_changes(&changes);
596
597 return ret;
598 }
599
600 int nb_candidate_commit_prepare(struct nb_config *candidate,
601 enum nb_client client, const char *comment,
602 struct nb_transaction **transaction)
603 {
604 struct nb_config_cbs changes;
605
606 if (nb_candidate_validate_yang(candidate) != NB_OK) {
607 flog_warn(EC_LIB_NB_CANDIDATE_INVALID,
608 "%s: failed to validate candidate configuration",
609 __func__);
610 return NB_ERR_VALIDATION;
611 }
612
613 RB_INIT(nb_config_cbs, &changes);
614 nb_config_diff(running_config, candidate, &changes);
615 if (RB_EMPTY(nb_config_cbs, &changes))
616 return NB_ERR_NO_CHANGES;
617
618 if (nb_candidate_validate_changes(candidate, &changes) != NB_OK) {
619 flog_warn(EC_LIB_NB_CANDIDATE_INVALID,
620 "%s: failed to validate candidate configuration",
621 __func__);
622 nb_config_diff_del_changes(&changes);
623 return NB_ERR_VALIDATION;
624 }
625
626 *transaction = nb_transaction_new(candidate, &changes, client, comment);
627 if (*transaction == NULL) {
628 flog_warn(EC_LIB_NB_TRANSACTION_CREATION_FAILED,
629 "%s: failed to create transaction", __func__);
630 nb_config_diff_del_changes(&changes);
631 return NB_ERR_LOCKED;
632 }
633
634 return nb_transaction_process(NB_EV_PREPARE, *transaction);
635 }
636
637 void nb_candidate_commit_abort(struct nb_transaction *transaction)
638 {
639 (void)nb_transaction_process(NB_EV_ABORT, transaction);
640 nb_transaction_free(transaction);
641 }
642
643 void nb_candidate_commit_apply(struct nb_transaction *transaction,
644 bool save_transaction, uint32_t *transaction_id)
645 {
646 (void)nb_transaction_process(NB_EV_APPLY, transaction);
647 nb_transaction_apply_finish(transaction);
648
649 /* Replace running by candidate. */
650 transaction->config->version++;
651 nb_config_replace(running_config, transaction->config, true);
652
653 /* Record transaction. */
654 if (save_transaction
655 && nb_db_transaction_save(transaction, transaction_id) != NB_OK)
656 flog_warn(EC_LIB_NB_TRANSACTION_RECORD_FAILED,
657 "%s: failed to record transaction", __func__);
658
659 nb_transaction_free(transaction);
660 }
661
662 int nb_candidate_commit(struct nb_config *candidate, enum nb_client client,
663 bool save_transaction, const char *comment,
664 uint32_t *transaction_id)
665 {
666 struct nb_transaction *transaction = NULL;
667 int ret;
668
669 ret = nb_candidate_commit_prepare(candidate, client, comment,
670 &transaction);
671 /*
672 * Apply the changes if the preparation phase succeeded. Otherwise abort
673 * the transaction.
674 */
675 if (ret == NB_OK)
676 nb_candidate_commit_apply(transaction, save_transaction,
677 transaction_id);
678 else if (transaction != NULL)
679 nb_candidate_commit_abort(transaction);
680
681 return ret;
682 }
683
684 static void nb_log_callback(const enum nb_event event,
685 enum nb_operation operation, const char *xpath,
686 const char *value)
687 {
688 zlog_debug(
689 "northbound callback: event [%s] op [%s] xpath [%s] value [%s]",
690 nb_event_name(event), nb_operation_name(operation), xpath,
691 value);
692 }
693
694 /*
695 * Call the northbound configuration callback associated to a given
696 * configuration change.
697 */
698 static int nb_configuration_callback(const enum nb_event event,
699 struct nb_config_change *change)
700 {
701 enum nb_operation operation = change->cb.operation;
702 const char *xpath = change->cb.xpath;
703 const struct nb_node *nb_node = change->cb.nb_node;
704 const struct lyd_node *dnode = change->cb.dnode;
705 union nb_resource *resource;
706 int ret = NB_ERR;
707
708 if (debug_northbound) {
709 const char *value = "(none)";
710
711 if (dnode && !yang_snode_is_typeless_data(dnode->schema))
712 value = yang_dnode_get_string(dnode, NULL);
713
714 nb_log_callback(event, operation, xpath, value);
715 }
716
717 if (event == NB_EV_VALIDATE)
718 resource = NULL;
719 else
720 resource = &change->resource;
721
722 switch (operation) {
723 case NB_OP_CREATE:
724 ret = (*nb_node->cbs.create)(event, dnode, resource);
725 break;
726 case NB_OP_MODIFY:
727 ret = (*nb_node->cbs.modify)(event, dnode, resource);
728 break;
729 case NB_OP_DESTROY:
730 ret = (*nb_node->cbs.destroy)(event, dnode);
731 break;
732 case NB_OP_MOVE:
733 ret = (*nb_node->cbs.move)(event, dnode);
734 break;
735 default:
736 break;
737 }
738
739 if (ret != NB_OK) {
740 enum lib_log_refs ref = 0;
741
742 switch (event) {
743 case NB_EV_VALIDATE:
744 ref = EC_LIB_NB_CB_CONFIG_VALIDATE;
745 break;
746 case NB_EV_PREPARE:
747 ref = EC_LIB_NB_CB_CONFIG_PREPARE;
748 break;
749 case NB_EV_ABORT:
750 ref = EC_LIB_NB_CB_CONFIG_ABORT;
751 break;
752 case NB_EV_APPLY:
753 ref = EC_LIB_NB_CB_CONFIG_APPLY;
754 break;
755 }
756 if (event == NB_EV_VALIDATE || event == NB_EV_PREPARE)
757 flog_warn(
758 ref,
759 "%s: error processing configuration change: error [%s] event [%s] operation [%s] xpath [%s]",
760 __func__, nb_err_name(ret),
761 nb_event_name(event),
762 nb_operation_name(operation), xpath);
763 else
764 flog_err(
765 ref,
766 "%s: error processing configuration change: error [%s] event [%s] operation [%s] xpath [%s]",
767 __func__, nb_err_name(ret),
768 nb_event_name(event),
769 nb_operation_name(operation), xpath);
770 }
771
772 return ret;
773 }
774
775 static struct nb_transaction *nb_transaction_new(struct nb_config *config,
776 struct nb_config_cbs *changes,
777 enum nb_client client,
778 const char *comment)
779 {
780 struct nb_transaction *transaction;
781
782 if (transaction_in_progress) {
783 flog_warn(
784 EC_LIB_NB_TRANSACTION_CREATION_FAILED,
785 "%s: error - there's already another transaction in progress",
786 __func__);
787 return NULL;
788 }
789 transaction_in_progress = true;
790
791 transaction = XCALLOC(MTYPE_TMP, sizeof(*transaction));
792 transaction->client = client;
793 if (comment)
794 strlcpy(transaction->comment, comment,
795 sizeof(transaction->comment));
796 transaction->config = config;
797 transaction->changes = *changes;
798
799 return transaction;
800 }
801
802 static void nb_transaction_free(struct nb_transaction *transaction)
803 {
804 nb_config_diff_del_changes(&transaction->changes);
805 XFREE(MTYPE_TMP, transaction);
806 transaction_in_progress = false;
807 }
808
809 /* Process all configuration changes associated to a transaction. */
810 static int nb_transaction_process(enum nb_event event,
811 struct nb_transaction *transaction)
812 {
813 struct nb_config_cb *cb;
814
815 RB_FOREACH (cb, nb_config_cbs, &transaction->changes) {
816 struct nb_config_change *change = (struct nb_config_change *)cb;
817 int ret;
818
819 /*
820 * Only try to release resources that were allocated
821 * successfully.
822 */
823 if (event == NB_EV_ABORT && change->prepare_ok == false)
824 break;
825
826 /* Call the appropriate callback. */
827 ret = nb_configuration_callback(event, change);
828 switch (event) {
829 case NB_EV_PREPARE:
830 if (ret != NB_OK)
831 return ret;
832 change->prepare_ok = true;
833 break;
834 case NB_EV_ABORT:
835 case NB_EV_APPLY:
836 /*
837 * At this point it's not possible to reject the
838 * transaction anymore, so any failure here can lead to
839 * inconsistencies and should be treated as a bug.
840 * Operations prone to errors, like validations and
841 * resource allocations, should be performed during the
842 * 'prepare' phase.
843 */
844 break;
845 default:
846 break;
847 }
848 }
849
850 return NB_OK;
851 }
852
853 static struct nb_config_cb *
854 nb_apply_finish_cb_new(struct nb_config_cbs *cbs, const char *xpath,
855 const struct nb_node *nb_node,
856 const struct lyd_node *dnode)
857 {
858 struct nb_config_cb *cb;
859
860 cb = XCALLOC(MTYPE_TMP, sizeof(*cb));
861 strlcpy(cb->xpath, xpath, sizeof(cb->xpath));
862 cb->nb_node = nb_node;
863 cb->dnode = dnode;
864 RB_INSERT(nb_config_cbs, cbs, cb);
865
866 return cb;
867 }
868
869 static struct nb_config_cb *
870 nb_apply_finish_cb_find(struct nb_config_cbs *cbs, const char *xpath,
871 const struct nb_node *nb_node)
872 {
873 struct nb_config_cb s;
874
875 strlcpy(s.xpath, xpath, sizeof(s.xpath));
876 s.nb_node = nb_node;
877 return RB_FIND(nb_config_cbs, cbs, &s);
878 }
879
880 /* Call the 'apply_finish' callbacks. */
881 static void nb_transaction_apply_finish(struct nb_transaction *transaction)
882 {
883 struct nb_config_cbs cbs;
884 struct nb_config_cb *cb;
885
886 /* Initialize tree of 'apply_finish' callbacks. */
887 RB_INIT(nb_config_cbs, &cbs);
888
889 /* Identify the 'apply_finish' callbacks that need to be called. */
890 RB_FOREACH (cb, nb_config_cbs, &transaction->changes) {
891 struct nb_config_change *change = (struct nb_config_change *)cb;
892 const struct lyd_node *dnode = change->cb.dnode;
893
894 /*
895 * Iterate up to the root of the data tree. When a node is being
896 * deleted, skip its 'apply_finish' callback if one is defined
897 * (the 'apply_finish' callbacks from the node ancestors should
898 * be called though).
899 */
900 if (change->cb.operation == NB_OP_DESTROY) {
901 char xpath[XPATH_MAXLEN];
902
903 dnode = dnode->parent;
904 if (!dnode)
905 break;
906
907 /*
908 * The dnode from 'delete' callbacks point to elements
909 * from the running configuration. Use yang_dnode_get()
910 * to get the corresponding dnode from the candidate
911 * configuration that is being committed.
912 */
913 yang_dnode_get_path(dnode, xpath, sizeof(xpath));
914 dnode = yang_dnode_get(transaction->config->dnode,
915 xpath);
916 }
917 while (dnode) {
918 char xpath[XPATH_MAXLEN];
919 struct nb_node *nb_node;
920
921 nb_node = dnode->schema->priv;
922 if (!nb_node->cbs.apply_finish)
923 goto next;
924
925 /*
926 * Don't call the callback more than once for the same
927 * data node.
928 */
929 yang_dnode_get_path(dnode, xpath, sizeof(xpath));
930 if (nb_apply_finish_cb_find(&cbs, xpath, nb_node))
931 goto next;
932
933 nb_apply_finish_cb_new(&cbs, xpath, nb_node, dnode);
934
935 next:
936 dnode = dnode->parent;
937 }
938 }
939
940 /* Call the 'apply_finish' callbacks, sorted by their priorities. */
941 RB_FOREACH (cb, nb_config_cbs, &cbs) {
942 if (debug_northbound)
943 nb_log_callback(NB_EV_APPLY, NB_OP_APPLY_FINISH,
944 cb->xpath, NULL);
945
946 (*cb->nb_node->cbs.apply_finish)(cb->dnode);
947 }
948
949 /* Release memory. */
950 while (!RB_EMPTY(nb_config_cbs, &cbs)) {
951 cb = RB_ROOT(nb_config_cbs, &cbs);
952 RB_REMOVE(nb_config_cbs, &cbs, cb);
953 XFREE(MTYPE_TMP, cb);
954 }
955 }
956
957 static int nb_oper_data_iter_children(const struct lys_node *snode,
958 const char *xpath, const void *list_entry,
959 const struct yang_list_keys *list_keys,
960 struct yang_translator *translator,
961 bool first, uint32_t flags,
962 nb_oper_data_cb cb, void *arg)
963 {
964 struct lys_node *child;
965
966 LY_TREE_FOR (snode->child, child) {
967 int ret;
968
969 ret = nb_oper_data_iter_node(child, xpath, list_entry,
970 list_keys, translator, false,
971 flags, cb, arg);
972 if (ret != NB_OK)
973 return ret;
974 }
975
976 return NB_OK;
977 }
978
979 static int nb_oper_data_iter_leaf(const struct nb_node *nb_node,
980 const char *xpath, const void *list_entry,
981 const struct yang_list_keys *list_keys,
982 struct yang_translator *translator,
983 uint32_t flags, nb_oper_data_cb cb, void *arg)
984 {
985 struct yang_data *data;
986
987 if (CHECK_FLAG(nb_node->snode->flags, LYS_CONFIG_W))
988 return NB_OK;
989
990 /* Ignore list keys. */
991 if (lys_is_key((struct lys_node_leaf *)nb_node->snode, NULL))
992 return NB_OK;
993
994 data = nb_node->cbs.get_elem(xpath, list_entry);
995 if (data == NULL)
996 /* Leaf of type "empty" is not present. */
997 return NB_OK;
998
999 return (*cb)(nb_node->snode, translator, data, arg);
1000 }
1001
1002 static int nb_oper_data_iter_container(const struct nb_node *nb_node,
1003 const char *xpath,
1004 const void *list_entry,
1005 const struct yang_list_keys *list_keys,
1006 struct yang_translator *translator,
1007 uint32_t flags, nb_oper_data_cb cb,
1008 void *arg)
1009 {
1010 if (CHECK_FLAG(nb_node->flags, F_NB_NODE_CONFIG_ONLY))
1011 return NB_OK;
1012
1013 /* Presence containers. */
1014 if (nb_node->cbs.get_elem) {
1015 struct yang_data *data;
1016 int ret;
1017
1018 data = nb_node->cbs.get_elem(xpath, list_entry);
1019 if (data == NULL)
1020 /* Presence container is not present. */
1021 return NB_OK;
1022
1023 ret = (*cb)(nb_node->snode, translator, data, arg);
1024 if (ret != NB_OK)
1025 return ret;
1026 }
1027
1028 /* Iterate over the child nodes. */
1029 return nb_oper_data_iter_children(nb_node->snode, xpath, list_entry,
1030 list_keys, translator, false, flags,
1031 cb, arg);
1032 }
1033
1034 static int
1035 nb_oper_data_iter_leaflist(const struct nb_node *nb_node, const char *xpath,
1036 const void *parent_list_entry,
1037 const struct yang_list_keys *parent_list_keys,
1038 struct yang_translator *translator, uint32_t flags,
1039 nb_oper_data_cb cb, void *arg)
1040 {
1041 const void *list_entry = NULL;
1042
1043 if (CHECK_FLAG(nb_node->snode->flags, LYS_CONFIG_W))
1044 return NB_OK;
1045
1046 do {
1047 struct yang_data *data;
1048 int ret;
1049
1050 list_entry =
1051 nb_node->cbs.get_next(parent_list_entry, list_entry);
1052 if (!list_entry)
1053 /* End of the list. */
1054 break;
1055
1056 data = nb_node->cbs.get_elem(xpath, list_entry);
1057 if (data == NULL)
1058 continue;
1059
1060 ret = (*cb)(nb_node->snode, translator, data, arg);
1061 if (ret != NB_OK)
1062 return ret;
1063 } while (list_entry);
1064
1065 return NB_OK;
1066 }
1067
1068 static int nb_oper_data_iter_list(const struct nb_node *nb_node,
1069 const char *xpath_list,
1070 const void *parent_list_entry,
1071 const struct yang_list_keys *parent_list_keys,
1072 struct yang_translator *translator,
1073 uint32_t flags, nb_oper_data_cb cb, void *arg)
1074 {
1075 struct lys_node_list *slist = (struct lys_node_list *)nb_node->snode;
1076 const void *list_entry = NULL;
1077 uint32_t position = 1;
1078
1079 if (CHECK_FLAG(nb_node->flags, F_NB_NODE_CONFIG_ONLY))
1080 return NB_OK;
1081
1082 /* Iterate over all list entries. */
1083 do {
1084 struct yang_list_keys list_keys;
1085 char xpath[XPATH_MAXLEN * 2];
1086 int ret;
1087
1088 /* Obtain list entry. */
1089 list_entry =
1090 nb_node->cbs.get_next(parent_list_entry, list_entry);
1091 if (!list_entry)
1092 /* End of the list. */
1093 break;
1094
1095 if (!CHECK_FLAG(nb_node->flags, F_NB_NODE_KEYLESS_LIST)) {
1096 /* Obtain the list entry keys. */
1097 if (nb_node->cbs.get_keys(list_entry, &list_keys)
1098 != NB_OK) {
1099 flog_warn(EC_LIB_NB_CB_STATE,
1100 "%s: failed to get list keys",
1101 __func__);
1102 return NB_ERR;
1103 }
1104
1105 /* Build XPath of the list entry. */
1106 strlcpy(xpath, xpath_list, sizeof(xpath));
1107 for (unsigned int i = 0; i < list_keys.num; i++) {
1108 snprintf(xpath + strlen(xpath),
1109 sizeof(xpath) - strlen(xpath),
1110 "[%s='%s']", slist->keys[i]->name,
1111 list_keys.key[i]);
1112 }
1113 } else {
1114 /*
1115 * Keyless list - build XPath using a positional index.
1116 */
1117 snprintf(xpath, sizeof(xpath), "%s[%u]", xpath_list,
1118 position);
1119 position++;
1120 }
1121
1122 /* Iterate over the child nodes. */
1123 ret = nb_oper_data_iter_children(
1124 nb_node->snode, xpath, list_entry, &list_keys,
1125 translator, false, flags, cb, arg);
1126 if (ret != NB_OK)
1127 return ret;
1128 } while (list_entry);
1129
1130 return NB_OK;
1131 }
1132
1133 static int nb_oper_data_iter_node(const struct lys_node *snode,
1134 const char *xpath_parent,
1135 const void *list_entry,
1136 const struct yang_list_keys *list_keys,
1137 struct yang_translator *translator,
1138 bool first, uint32_t flags,
1139 nb_oper_data_cb cb, void *arg)
1140 {
1141 struct nb_node *nb_node;
1142 char xpath[XPATH_MAXLEN];
1143 int ret = NB_OK;
1144
1145 if (!first && CHECK_FLAG(flags, NB_OPER_DATA_ITER_NORECURSE)
1146 && CHECK_FLAG(snode->nodetype, LYS_CONTAINER | LYS_LIST))
1147 return NB_OK;
1148
1149 /* Update XPath. */
1150 strlcpy(xpath, xpath_parent, sizeof(xpath));
1151 if (!first && snode->nodetype != LYS_USES)
1152 snprintf(xpath + strlen(xpath), sizeof(xpath) - strlen(xpath),
1153 "/%s", snode->name);
1154
1155 nb_node = snode->priv;
1156 switch (snode->nodetype) {
1157 case LYS_CONTAINER:
1158 ret = nb_oper_data_iter_container(nb_node, xpath, list_entry,
1159 list_keys, translator, flags,
1160 cb, arg);
1161 break;
1162 case LYS_LEAF:
1163 ret = nb_oper_data_iter_leaf(nb_node, xpath, list_entry,
1164 list_keys, translator, flags, cb,
1165 arg);
1166 break;
1167 case LYS_LEAFLIST:
1168 ret = nb_oper_data_iter_leaflist(nb_node, xpath, list_entry,
1169 list_keys, translator, flags,
1170 cb, arg);
1171 break;
1172 case LYS_LIST:
1173 ret = nb_oper_data_iter_list(nb_node, xpath, list_entry,
1174 list_keys, translator, flags, cb,
1175 arg);
1176 break;
1177 case LYS_USES:
1178 ret = nb_oper_data_iter_children(snode, xpath, list_entry,
1179 list_keys, translator, false,
1180 flags, cb, arg);
1181 break;
1182 default:
1183 break;
1184 }
1185
1186 return ret;
1187 }
1188
1189 int nb_oper_data_iterate(const char *xpath, struct yang_translator *translator,
1190 uint32_t flags, nb_oper_data_cb cb, void *arg)
1191 {
1192 struct nb_node *nb_node;
1193 const void *list_entry = NULL;
1194 struct yang_list_keys list_keys;
1195 struct list *list_dnodes;
1196 struct lyd_node *dnode, *dn;
1197 struct listnode *ln;
1198 int ret;
1199
1200 nb_node = nb_node_find(xpath);
1201 if (!nb_node) {
1202 flog_warn(EC_LIB_YANG_UNKNOWN_DATA_PATH,
1203 "%s: unknown data path: %s", __func__, xpath);
1204 return NB_ERR;
1205 }
1206
1207 /* For now this function works only with containers and lists. */
1208 if (!CHECK_FLAG(nb_node->snode->nodetype, LYS_CONTAINER | LYS_LIST)) {
1209 flog_warn(
1210 EC_LIB_NB_OPERATIONAL_DATA,
1211 "%s: can't iterate over YANG leaf or leaf-list [xpath %s]",
1212 __func__, xpath);
1213 return NB_ERR;
1214 }
1215
1216 /*
1217 * Create a data tree from the XPath so that we can parse the keys of
1218 * all YANG lists (if any).
1219 */
1220 ly_errno = 0;
1221 dnode = lyd_new_path(NULL, ly_native_ctx, xpath, NULL, 0,
1222 LYD_PATH_OPT_UPDATE);
1223 if (!dnode && ly_errno) {
1224 flog_warn(EC_LIB_LIBYANG, "%s: lyd_new_path() failed",
1225 __func__);
1226 return NB_ERR;
1227 }
1228 /*
1229 * We can remove the following two lines once we depend on
1230 * libyang-v0.16-r2, which has the LYD_PATH_OPT_NOPARENTRET flag for
1231 * lyd_new_path().
1232 */
1233 dnode = yang_dnode_get(dnode, xpath);
1234 assert(dnode);
1235
1236 /*
1237 * Create a linked list to sort the data nodes starting from the root.
1238 */
1239 list_dnodes = list_new();
1240 for (dn = dnode; dn; dn = dn->parent) {
1241 if (dn->schema->nodetype != LYS_LIST || !dn->child)
1242 continue;
1243 listnode_add_head(list_dnodes, dn);
1244 }
1245 /*
1246 * Use the northbound callbacks to find list entry pointer corresponding
1247 * to the given XPath.
1248 */
1249 for (ALL_LIST_ELEMENTS_RO(list_dnodes, ln, dn)) {
1250 struct lyd_node *child;
1251 struct nb_node *nn;
1252 unsigned int n = 0;
1253
1254 /* Obtain the list entry keys. */
1255 memset(&list_keys, 0, sizeof(list_keys));
1256 LY_TREE_FOR (dn->child, child) {
1257 if (!lys_is_key((struct lys_node_leaf *)child->schema,
1258 NULL))
1259 continue;
1260 strlcpy(list_keys.key[n],
1261 yang_dnode_get_string(child, NULL),
1262 sizeof(list_keys.key[n]));
1263 n++;
1264 }
1265 list_keys.num = n;
1266 if (list_keys.num
1267 != ((struct lys_node_list *)dn->schema)->keys_size) {
1268 list_delete(&list_dnodes);
1269 yang_dnode_free(dnode);
1270 return NB_ERR_NOT_FOUND;
1271 }
1272
1273 /* Find the list entry pointer. */
1274 nn = dn->schema->priv;
1275 list_entry = nn->cbs.lookup_entry(list_entry, &list_keys);
1276 if (list_entry == NULL) {
1277 list_delete(&list_dnodes);
1278 yang_dnode_free(dnode);
1279 return NB_ERR_NOT_FOUND;
1280 }
1281 }
1282
1283 /* If a list entry was given, iterate over that list entry only. */
1284 if (dnode->schema->nodetype == LYS_LIST && dnode->child)
1285 ret = nb_oper_data_iter_children(
1286 nb_node->snode, xpath, list_entry, &list_keys,
1287 translator, true, flags, cb, arg);
1288 else
1289 ret = nb_oper_data_iter_node(nb_node->snode, xpath, list_entry,
1290 &list_keys, translator, true,
1291 flags, cb, arg);
1292
1293 list_delete(&list_dnodes);
1294 yang_dnode_free(dnode);
1295
1296 return ret;
1297 }
1298
1299 bool nb_operation_is_valid(enum nb_operation operation,
1300 const struct lys_node *snode)
1301 {
1302 struct nb_node *nb_node = snode->priv;
1303 struct lys_node_container *scontainer;
1304 struct lys_node_leaf *sleaf;
1305
1306 switch (operation) {
1307 case NB_OP_CREATE:
1308 if (!CHECK_FLAG(snode->flags, LYS_CONFIG_W))
1309 return false;
1310
1311 switch (snode->nodetype) {
1312 case LYS_LEAF:
1313 sleaf = (struct lys_node_leaf *)snode;
1314 if (sleaf->type.base != LY_TYPE_EMPTY)
1315 return false;
1316 break;
1317 case LYS_CONTAINER:
1318 scontainer = (struct lys_node_container *)snode;
1319 if (!scontainer->presence)
1320 return false;
1321 break;
1322 case LYS_LIST:
1323 case LYS_LEAFLIST:
1324 break;
1325 default:
1326 return false;
1327 }
1328 return true;
1329 case NB_OP_MODIFY:
1330 if (!CHECK_FLAG(snode->flags, LYS_CONFIG_W))
1331 return false;
1332
1333 switch (snode->nodetype) {
1334 case LYS_LEAF:
1335 sleaf = (struct lys_node_leaf *)snode;
1336 if (sleaf->type.base == LY_TYPE_EMPTY)
1337 return false;
1338
1339 /* List keys can't be modified. */
1340 if (lys_is_key(sleaf, NULL))
1341 return false;
1342 break;
1343 default:
1344 return false;
1345 }
1346 return true;
1347 case NB_OP_DESTROY:
1348 if (!CHECK_FLAG(snode->flags, LYS_CONFIG_W))
1349 return false;
1350
1351 switch (snode->nodetype) {
1352 case LYS_LEAF:
1353 sleaf = (struct lys_node_leaf *)snode;
1354
1355 /* List keys can't be deleted. */
1356 if (lys_is_key(sleaf, NULL))
1357 return false;
1358
1359 /*
1360 * Only optional leafs can be deleted, or leafs whose
1361 * parent is a case statement.
1362 */
1363 if (snode->parent->nodetype == LYS_CASE)
1364 return true;
1365 if (sleaf->when)
1366 return true;
1367 if (CHECK_FLAG(sleaf->flags, LYS_MAND_TRUE)
1368 || sleaf->dflt)
1369 return false;
1370 break;
1371 case LYS_CONTAINER:
1372 scontainer = (struct lys_node_container *)snode;
1373 if (!scontainer->presence)
1374 return false;
1375 break;
1376 case LYS_LIST:
1377 case LYS_LEAFLIST:
1378 break;
1379 default:
1380 return false;
1381 }
1382 return true;
1383 case NB_OP_MOVE:
1384 if (!CHECK_FLAG(snode->flags, LYS_CONFIG_W))
1385 return false;
1386
1387 switch (snode->nodetype) {
1388 case LYS_LIST:
1389 case LYS_LEAFLIST:
1390 if (!CHECK_FLAG(snode->flags, LYS_USERORDERED))
1391 return false;
1392 break;
1393 default:
1394 return false;
1395 }
1396 return true;
1397 case NB_OP_APPLY_FINISH:
1398 if (!CHECK_FLAG(snode->flags, LYS_CONFIG_W))
1399 return false;
1400 return true;
1401 case NB_OP_GET_ELEM:
1402 if (!CHECK_FLAG(snode->flags, LYS_CONFIG_R))
1403 return false;
1404
1405 switch (snode->nodetype) {
1406 case LYS_LEAF:
1407 case LYS_LEAFLIST:
1408 break;
1409 case LYS_CONTAINER:
1410 scontainer = (struct lys_node_container *)snode;
1411 if (!scontainer->presence)
1412 return false;
1413 break;
1414 default:
1415 return false;
1416 }
1417 return true;
1418 case NB_OP_GET_NEXT:
1419 switch (snode->nodetype) {
1420 case LYS_LIST:
1421 if (CHECK_FLAG(nb_node->flags, F_NB_NODE_CONFIG_ONLY))
1422 return false;
1423 break;
1424 case LYS_LEAFLIST:
1425 if (CHECK_FLAG(snode->flags, LYS_CONFIG_W))
1426 return false;
1427 break;
1428 default:
1429 return false;
1430 }
1431 return true;
1432 case NB_OP_GET_KEYS:
1433 case NB_OP_LOOKUP_ENTRY:
1434 switch (snode->nodetype) {
1435 case LYS_LIST:
1436 if (CHECK_FLAG(nb_node->flags, F_NB_NODE_CONFIG_ONLY))
1437 return false;
1438 if (CHECK_FLAG(nb_node->flags, F_NB_NODE_KEYLESS_LIST))
1439 return false;
1440 break;
1441 default:
1442 return false;
1443 }
1444 return true;
1445 case NB_OP_RPC:
1446 if (CHECK_FLAG(snode->flags, LYS_CONFIG_W | LYS_CONFIG_R))
1447 return false;
1448
1449 switch (snode->nodetype) {
1450 case LYS_RPC:
1451 case LYS_ACTION:
1452 break;
1453 default:
1454 return false;
1455 }
1456 return true;
1457 default:
1458 return false;
1459 }
1460 }
1461
1462 DEFINE_HOOK(nb_notification_send, (const char *xpath, struct list *arguments),
1463 (xpath, arguments));
1464
1465 int nb_notification_send(const char *xpath, struct list *arguments)
1466 {
1467 int ret;
1468
1469 ret = hook_call(nb_notification_send, xpath, arguments);
1470 if (arguments)
1471 list_delete(&arguments);
1472
1473 return ret;
1474 }
1475
1476 const char *nb_event_name(enum nb_event event)
1477 {
1478 switch (event) {
1479 case NB_EV_VALIDATE:
1480 return "validate";
1481 case NB_EV_PREPARE:
1482 return "prepare";
1483 case NB_EV_ABORT:
1484 return "abort";
1485 case NB_EV_APPLY:
1486 return "apply";
1487 default:
1488 return "unknown";
1489 }
1490 }
1491
1492 const char *nb_operation_name(enum nb_operation operation)
1493 {
1494 switch (operation) {
1495 case NB_OP_CREATE:
1496 return "create";
1497 case NB_OP_MODIFY:
1498 return "modify";
1499 case NB_OP_DESTROY:
1500 return "destroy";
1501 case NB_OP_MOVE:
1502 return "move";
1503 case NB_OP_APPLY_FINISH:
1504 return "apply_finish";
1505 case NB_OP_GET_ELEM:
1506 return "get_elem";
1507 case NB_OP_GET_NEXT:
1508 return "get_next";
1509 case NB_OP_GET_KEYS:
1510 return "get_keys";
1511 case NB_OP_LOOKUP_ENTRY:
1512 return "lookup_entry";
1513 case NB_OP_RPC:
1514 return "rpc";
1515 default:
1516 return "unknown";
1517 }
1518 }
1519
1520 const char *nb_err_name(enum nb_error error)
1521 {
1522 switch (error) {
1523 case NB_OK:
1524 return "ok";
1525 case NB_ERR:
1526 return "generic error";
1527 case NB_ERR_NO_CHANGES:
1528 return "no changes";
1529 case NB_ERR_NOT_FOUND:
1530 return "element not found";
1531 case NB_ERR_LOCKED:
1532 return "resource is locked";
1533 case NB_ERR_VALIDATION:
1534 return "validation error";
1535 case NB_ERR_RESOURCE:
1536 return "failed to allocate resource";
1537 case NB_ERR_INCONSISTENCY:
1538 return "internal inconsistency";
1539 default:
1540 return "unknown";
1541 }
1542 }
1543
1544 const char *nb_client_name(enum nb_client client)
1545 {
1546 switch (client) {
1547 case NB_CLIENT_CLI:
1548 return "CLI";
1549 case NB_CLIENT_CONFD:
1550 return "ConfD";
1551 case NB_CLIENT_SYSREPO:
1552 return "Sysrepo";
1553 default:
1554 return "unknown";
1555 }
1556 }
1557
1558 static void nb_load_callbacks(const struct frr_yang_module_info *module)
1559 {
1560 for (size_t i = 0; module->nodes[i].xpath; i++) {
1561 struct nb_node *nb_node;
1562 uint32_t priority;
1563
1564 nb_node = nb_node_find(module->nodes[i].xpath);
1565 if (!nb_node) {
1566 flog_warn(EC_LIB_YANG_UNKNOWN_DATA_PATH,
1567 "%s: unknown data path: %s", __func__,
1568 module->nodes[i].xpath);
1569 continue;
1570 }
1571
1572 nb_node->cbs = module->nodes[i].cbs;
1573 priority = module->nodes[i].priority;
1574 if (priority != 0)
1575 nb_node->priority = priority;
1576 }
1577 }
1578
1579 void nb_init(struct thread_master *tm,
1580 const struct frr_yang_module_info *modules[], size_t nmodules)
1581 {
1582 unsigned int errors = 0;
1583
1584 /* Load YANG modules. */
1585 for (size_t i = 0; i < nmodules; i++)
1586 yang_module_load(modules[i]->name);
1587
1588 /* Create a nb_node for all YANG schema nodes. */
1589 nb_nodes_create();
1590
1591 /* Load northbound callbacks. */
1592 for (size_t i = 0; i < nmodules; i++)
1593 nb_load_callbacks(modules[i]);
1594
1595 /* Validate northbound callbacks. */
1596 yang_snodes_iterate_all(nb_node_validate, 0, &errors);
1597 if (errors > 0) {
1598 flog_err(
1599 EC_LIB_NB_CBS_VALIDATION,
1600 "%s: failed to validate northbound callbacks: %u error(s)",
1601 __func__, errors);
1602 exit(1);
1603 }
1604
1605 /* Initialize the northbound database (used for the rollback log). */
1606 if (nb_db_init() != NB_OK)
1607 flog_warn(EC_LIB_NB_DATABASE,
1608 "%s: failed to initialize northbound database",
1609 __func__);
1610
1611 /* Create an empty running configuration. */
1612 running_config = nb_config_new(NULL);
1613
1614 /* Initialize the northbound CLI. */
1615 nb_cli_init(tm);
1616 }
1617
1618 void nb_terminate(void)
1619 {
1620 /* Terminate the northbound CLI. */
1621 nb_cli_terminate();
1622
1623 /* Delete all nb_node's from all YANG modules. */
1624 nb_nodes_delete();
1625
1626 /* Delete the running configuration. */
1627 nb_config_free(running_config);
1628 }