]> git.proxmox.com Git - mirror_frr.git/blob - lib/northbound.c
Merge pull request #3949 from qlyoung/remove-zlog-newlines
[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 static void nb_config_diff_deleted(const struct lyd_node *dnode,
388 struct nb_config_cbs *changes)
389 {
390 if (nb_operation_is_valid(NB_OP_DESTROY, dnode->schema))
391 nb_config_diff_add_change(changes, NB_OP_DESTROY, dnode);
392 else if (CHECK_FLAG(dnode->schema->nodetype, LYS_CONTAINER)) {
393 struct lyd_node *child;
394
395 /*
396 * Non-presence containers need special handling since they
397 * don't have "destroy" callbacks. In this case, what we need to
398 * do is to call the "destroy" callbacks of their child nodes
399 * when applicable (i.e. optional nodes).
400 */
401 LY_TREE_FOR (dnode->child, child) {
402 nb_config_diff_deleted(child, changes);
403 }
404 }
405 }
406
407 /* Calculate the delta between two different configurations. */
408 static void nb_config_diff(const struct nb_config *config1,
409 const struct nb_config *config2,
410 struct nb_config_cbs *changes)
411 {
412 struct lyd_difflist *diff;
413
414 diff = lyd_diff(config1->dnode, config2->dnode,
415 LYD_DIFFOPT_WITHDEFAULTS);
416 assert(diff);
417
418 for (int i = 0; diff->type[i] != LYD_DIFF_END; i++) {
419 LYD_DIFFTYPE type;
420 struct lyd_node *dnode;
421
422 type = diff->type[i];
423
424 switch (type) {
425 case LYD_DIFF_CREATED:
426 dnode = diff->second[i];
427 nb_config_diff_created(dnode, changes);
428 break;
429 case LYD_DIFF_DELETED:
430 dnode = diff->first[i];
431 nb_config_diff_deleted(dnode, changes);
432 break;
433 case LYD_DIFF_CHANGED:
434 dnode = diff->second[i];
435 nb_config_diff_add_change(changes, NB_OP_MODIFY, dnode);
436 break;
437 case LYD_DIFF_MOVEDAFTER1:
438 case LYD_DIFF_MOVEDAFTER2:
439 default:
440 continue;
441 }
442 }
443
444 lyd_free_diff(diff);
445 }
446
447 int nb_candidate_edit(struct nb_config *candidate,
448 const struct nb_node *nb_node,
449 enum nb_operation operation, const char *xpath,
450 const struct yang_data *previous,
451 const struct yang_data *data)
452 {
453 struct lyd_node *dnode;
454 char xpath_edit[XPATH_MAXLEN];
455
456 if (!nb_operation_is_valid(operation, nb_node->snode)) {
457 flog_warn(EC_LIB_NB_CANDIDATE_EDIT_ERROR,
458 "%s: %s operation not valid for %s", __func__,
459 nb_operation_name(operation), xpath);
460 return NB_ERR;
461 }
462
463 /* Use special notation for leaf-lists (RFC 6020, section 9.13.5). */
464 if (nb_node->snode->nodetype == LYS_LEAFLIST)
465 snprintf(xpath_edit, sizeof(xpath_edit), "%s[.='%s']", xpath,
466 data->value);
467 else
468 strlcpy(xpath_edit, xpath, sizeof(xpath_edit));
469
470 switch (operation) {
471 case NB_OP_CREATE:
472 case NB_OP_MODIFY:
473 ly_errno = 0;
474 dnode = lyd_new_path(candidate->dnode, ly_native_ctx,
475 xpath_edit, (void *)data->value, 0,
476 LYD_PATH_OPT_UPDATE);
477 if (!dnode && ly_errno) {
478 flog_warn(EC_LIB_LIBYANG, "%s: lyd_new_path() failed",
479 __func__);
480 return NB_ERR;
481 }
482
483 /*
484 * If a new node was created, call lyd_validate() only to create
485 * default child nodes.
486 */
487 if (dnode) {
488 lyd_schema_sort(dnode, 0);
489 lyd_validate(&dnode, LYD_OPT_CONFIG, ly_native_ctx);
490 }
491 break;
492 case NB_OP_DESTROY:
493 dnode = yang_dnode_get(candidate->dnode, xpath_edit);
494 if (!dnode)
495 /*
496 * Return a special error code so the caller can choose
497 * whether to ignore it or not.
498 */
499 return NB_ERR_NOT_FOUND;
500 lyd_free(dnode);
501 break;
502 case NB_OP_MOVE:
503 /* TODO: update configuration. */
504 break;
505 default:
506 flog_warn(EC_LIB_DEVELOPMENT,
507 "%s: unknown operation (%u) [xpath %s]", __func__,
508 operation, xpath_edit);
509 return NB_ERR;
510 }
511
512 return NB_OK;
513 }
514
515 bool nb_candidate_needs_update(const struct nb_config *candidate)
516 {
517 if (candidate->version < running_config->version)
518 return true;
519
520 return false;
521 }
522
523 int nb_candidate_update(struct nb_config *candidate)
524 {
525 struct nb_config *updated_config;
526
527 updated_config = nb_config_dup(running_config);
528 if (nb_config_merge(updated_config, candidate, true) != NB_OK)
529 return NB_ERR;
530
531 nb_config_replace(candidate, updated_config, false);
532
533 return NB_OK;
534 }
535
536 /*
537 * The northbound configuration callbacks use the 'priv' pointer present in the
538 * libyang lyd_node structure to store pointers to FRR internal variables
539 * associated to YANG lists and presence containers. Before commiting a
540 * candidate configuration, we must restore the 'priv' pointers stored in the
541 * running configuration since they might be lost while editing the candidate.
542 */
543 static void nb_candidate_restore_priv_pointers(struct nb_config *candidate)
544 {
545 struct lyd_node *root, *next, *dnode_iter;
546
547 LY_TREE_FOR (running_config->dnode, root) {
548 LY_TREE_DFS_BEGIN (root, next, dnode_iter) {
549 struct lyd_node *dnode_candidate;
550 char xpath[XPATH_MAXLEN];
551
552 if (!dnode_iter->priv)
553 goto next;
554
555 yang_dnode_get_path(dnode_iter, xpath, sizeof(xpath));
556 dnode_candidate =
557 yang_dnode_get(candidate->dnode, xpath);
558 if (dnode_candidate)
559 yang_dnode_set_entry(dnode_candidate,
560 dnode_iter->priv);
561
562 next:
563 LY_TREE_DFS_END(root, next, dnode_iter);
564 }
565 }
566 }
567
568 /*
569 * Perform YANG syntactic and semantic validation.
570 *
571 * WARNING: lyd_validate() can change the configuration as part of the
572 * validation process.
573 */
574 static int nb_candidate_validate_yang(struct nb_config *candidate)
575 {
576 if (lyd_validate(&candidate->dnode, LYD_OPT_STRICT | LYD_OPT_CONFIG,
577 ly_native_ctx)
578 != 0)
579 return NB_ERR_VALIDATION;
580
581 return NB_OK;
582 }
583
584 /* Perform code-level validation using the northbound callbacks. */
585 static int nb_candidate_validate_changes(struct nb_config *candidate,
586 struct nb_config_cbs *changes)
587 {
588 struct nb_config_cb *cb;
589
590 nb_candidate_restore_priv_pointers(candidate);
591 RB_FOREACH (cb, nb_config_cbs, changes) {
592 struct nb_config_change *change = (struct nb_config_change *)cb;
593 int ret;
594
595 ret = nb_configuration_callback(NB_EV_VALIDATE, change);
596 if (ret != NB_OK)
597 return NB_ERR_VALIDATION;
598 }
599
600 return NB_OK;
601 }
602
603 int nb_candidate_validate(struct nb_config *candidate)
604 {
605 struct nb_config_cbs changes;
606 int ret;
607
608 if (nb_candidate_validate_yang(candidate) != NB_OK)
609 return NB_ERR_VALIDATION;
610
611 RB_INIT(nb_config_cbs, &changes);
612 nb_config_diff(running_config, candidate, &changes);
613 ret = nb_candidate_validate_changes(candidate, &changes);
614 nb_config_diff_del_changes(&changes);
615
616 return ret;
617 }
618
619 int nb_candidate_commit_prepare(struct nb_config *candidate,
620 enum nb_client client, const char *comment,
621 struct nb_transaction **transaction)
622 {
623 struct nb_config_cbs changes;
624
625 if (nb_candidate_validate_yang(candidate) != NB_OK) {
626 flog_warn(EC_LIB_NB_CANDIDATE_INVALID,
627 "%s: failed to validate candidate configuration",
628 __func__);
629 return NB_ERR_VALIDATION;
630 }
631
632 RB_INIT(nb_config_cbs, &changes);
633 nb_config_diff(running_config, candidate, &changes);
634 if (RB_EMPTY(nb_config_cbs, &changes))
635 return NB_ERR_NO_CHANGES;
636
637 if (nb_candidate_validate_changes(candidate, &changes) != NB_OK) {
638 flog_warn(EC_LIB_NB_CANDIDATE_INVALID,
639 "%s: failed to validate candidate configuration",
640 __func__);
641 nb_config_diff_del_changes(&changes);
642 return NB_ERR_VALIDATION;
643 }
644
645 *transaction = nb_transaction_new(candidate, &changes, client, comment);
646 if (*transaction == NULL) {
647 flog_warn(EC_LIB_NB_TRANSACTION_CREATION_FAILED,
648 "%s: failed to create transaction", __func__);
649 nb_config_diff_del_changes(&changes);
650 return NB_ERR_LOCKED;
651 }
652
653 return nb_transaction_process(NB_EV_PREPARE, *transaction);
654 }
655
656 void nb_candidate_commit_abort(struct nb_transaction *transaction)
657 {
658 (void)nb_transaction_process(NB_EV_ABORT, transaction);
659 nb_transaction_free(transaction);
660 }
661
662 void nb_candidate_commit_apply(struct nb_transaction *transaction,
663 bool save_transaction, uint32_t *transaction_id)
664 {
665 (void)nb_transaction_process(NB_EV_APPLY, transaction);
666 nb_transaction_apply_finish(transaction);
667
668 /* Replace running by candidate. */
669 transaction->config->version++;
670 nb_config_replace(running_config, transaction->config, true);
671
672 /* Record transaction. */
673 if (save_transaction
674 && nb_db_transaction_save(transaction, transaction_id) != NB_OK)
675 flog_warn(EC_LIB_NB_TRANSACTION_RECORD_FAILED,
676 "%s: failed to record transaction", __func__);
677
678 nb_transaction_free(transaction);
679 }
680
681 int nb_candidate_commit(struct nb_config *candidate, enum nb_client client,
682 bool save_transaction, const char *comment,
683 uint32_t *transaction_id)
684 {
685 struct nb_transaction *transaction = NULL;
686 int ret;
687
688 ret = nb_candidate_commit_prepare(candidate, client, comment,
689 &transaction);
690 /*
691 * Apply the changes if the preparation phase succeeded. Otherwise abort
692 * the transaction.
693 */
694 if (ret == NB_OK)
695 nb_candidate_commit_apply(transaction, save_transaction,
696 transaction_id);
697 else if (transaction != NULL)
698 nb_candidate_commit_abort(transaction);
699
700 return ret;
701 }
702
703 static void nb_log_callback(const enum nb_event event,
704 enum nb_operation operation, const char *xpath,
705 const char *value)
706 {
707 zlog_debug(
708 "northbound callback: event [%s] op [%s] xpath [%s] value [%s]",
709 nb_event_name(event), nb_operation_name(operation), xpath,
710 value);
711 }
712
713 /*
714 * Call the northbound configuration callback associated to a given
715 * configuration change.
716 */
717 static int nb_configuration_callback(const enum nb_event event,
718 struct nb_config_change *change)
719 {
720 enum nb_operation operation = change->cb.operation;
721 const char *xpath = change->cb.xpath;
722 const struct nb_node *nb_node = change->cb.nb_node;
723 const struct lyd_node *dnode = change->cb.dnode;
724 union nb_resource *resource;
725 int ret = NB_ERR;
726
727 if (debug_northbound) {
728 const char *value = "(none)";
729
730 if (dnode && !yang_snode_is_typeless_data(dnode->schema))
731 value = yang_dnode_get_string(dnode, NULL);
732
733 nb_log_callback(event, operation, xpath, value);
734 }
735
736 if (event == NB_EV_VALIDATE)
737 resource = NULL;
738 else
739 resource = &change->resource;
740
741 switch (operation) {
742 case NB_OP_CREATE:
743 ret = (*nb_node->cbs.create)(event, dnode, resource);
744 break;
745 case NB_OP_MODIFY:
746 ret = (*nb_node->cbs.modify)(event, dnode, resource);
747 break;
748 case NB_OP_DESTROY:
749 ret = (*nb_node->cbs.destroy)(event, dnode);
750 break;
751 case NB_OP_MOVE:
752 ret = (*nb_node->cbs.move)(event, dnode);
753 break;
754 default:
755 break;
756 }
757
758 if (ret != NB_OK) {
759 enum lib_log_refs ref = 0;
760
761 switch (event) {
762 case NB_EV_VALIDATE:
763 ref = EC_LIB_NB_CB_CONFIG_VALIDATE;
764 break;
765 case NB_EV_PREPARE:
766 ref = EC_LIB_NB_CB_CONFIG_PREPARE;
767 break;
768 case NB_EV_ABORT:
769 ref = EC_LIB_NB_CB_CONFIG_ABORT;
770 break;
771 case NB_EV_APPLY:
772 ref = EC_LIB_NB_CB_CONFIG_APPLY;
773 break;
774 }
775 if (event == NB_EV_VALIDATE || event == NB_EV_PREPARE)
776 flog_warn(
777 ref,
778 "%s: error processing configuration change: error [%s] event [%s] operation [%s] xpath [%s]",
779 __func__, nb_err_name(ret),
780 nb_event_name(event),
781 nb_operation_name(operation), xpath);
782 else
783 flog_err(
784 ref,
785 "%s: error processing configuration change: error [%s] event [%s] operation [%s] xpath [%s]",
786 __func__, nb_err_name(ret),
787 nb_event_name(event),
788 nb_operation_name(operation), xpath);
789 }
790
791 return ret;
792 }
793
794 static struct nb_transaction *nb_transaction_new(struct nb_config *config,
795 struct nb_config_cbs *changes,
796 enum nb_client client,
797 const char *comment)
798 {
799 struct nb_transaction *transaction;
800
801 if (transaction_in_progress) {
802 flog_warn(
803 EC_LIB_NB_TRANSACTION_CREATION_FAILED,
804 "%s: error - there's already another transaction in progress",
805 __func__);
806 return NULL;
807 }
808 transaction_in_progress = true;
809
810 transaction = XCALLOC(MTYPE_TMP, sizeof(*transaction));
811 transaction->client = client;
812 if (comment)
813 strlcpy(transaction->comment, comment,
814 sizeof(transaction->comment));
815 transaction->config = config;
816 transaction->changes = *changes;
817
818 return transaction;
819 }
820
821 static void nb_transaction_free(struct nb_transaction *transaction)
822 {
823 nb_config_diff_del_changes(&transaction->changes);
824 XFREE(MTYPE_TMP, transaction);
825 transaction_in_progress = false;
826 }
827
828 /* Process all configuration changes associated to a transaction. */
829 static int nb_transaction_process(enum nb_event event,
830 struct nb_transaction *transaction)
831 {
832 struct nb_config_cb *cb;
833
834 RB_FOREACH (cb, nb_config_cbs, &transaction->changes) {
835 struct nb_config_change *change = (struct nb_config_change *)cb;
836 int ret;
837
838 /*
839 * Only try to release resources that were allocated
840 * successfully.
841 */
842 if (event == NB_EV_ABORT && change->prepare_ok == false)
843 break;
844
845 /* Call the appropriate callback. */
846 ret = nb_configuration_callback(event, change);
847 switch (event) {
848 case NB_EV_PREPARE:
849 if (ret != NB_OK)
850 return ret;
851 change->prepare_ok = true;
852 break;
853 case NB_EV_ABORT:
854 case NB_EV_APPLY:
855 /*
856 * At this point it's not possible to reject the
857 * transaction anymore, so any failure here can lead to
858 * inconsistencies and should be treated as a bug.
859 * Operations prone to errors, like validations and
860 * resource allocations, should be performed during the
861 * 'prepare' phase.
862 */
863 break;
864 default:
865 break;
866 }
867 }
868
869 return NB_OK;
870 }
871
872 static struct nb_config_cb *
873 nb_apply_finish_cb_new(struct nb_config_cbs *cbs, const char *xpath,
874 const struct nb_node *nb_node,
875 const struct lyd_node *dnode)
876 {
877 struct nb_config_cb *cb;
878
879 cb = XCALLOC(MTYPE_TMP, sizeof(*cb));
880 strlcpy(cb->xpath, xpath, sizeof(cb->xpath));
881 cb->nb_node = nb_node;
882 cb->dnode = dnode;
883 RB_INSERT(nb_config_cbs, cbs, cb);
884
885 return cb;
886 }
887
888 static struct nb_config_cb *
889 nb_apply_finish_cb_find(struct nb_config_cbs *cbs, const char *xpath,
890 const struct nb_node *nb_node)
891 {
892 struct nb_config_cb s;
893
894 strlcpy(s.xpath, xpath, sizeof(s.xpath));
895 s.nb_node = nb_node;
896 return RB_FIND(nb_config_cbs, cbs, &s);
897 }
898
899 /* Call the 'apply_finish' callbacks. */
900 static void nb_transaction_apply_finish(struct nb_transaction *transaction)
901 {
902 struct nb_config_cbs cbs;
903 struct nb_config_cb *cb;
904
905 /* Initialize tree of 'apply_finish' callbacks. */
906 RB_INIT(nb_config_cbs, &cbs);
907
908 /* Identify the 'apply_finish' callbacks that need to be called. */
909 RB_FOREACH (cb, nb_config_cbs, &transaction->changes) {
910 struct nb_config_change *change = (struct nb_config_change *)cb;
911 const struct lyd_node *dnode = change->cb.dnode;
912
913 /*
914 * Iterate up to the root of the data tree. When a node is being
915 * deleted, skip its 'apply_finish' callback if one is defined
916 * (the 'apply_finish' callbacks from the node ancestors should
917 * be called though).
918 */
919 if (change->cb.operation == NB_OP_DESTROY) {
920 char xpath[XPATH_MAXLEN];
921
922 dnode = dnode->parent;
923 if (!dnode)
924 break;
925
926 /*
927 * The dnode from 'delete' callbacks point to elements
928 * from the running configuration. Use yang_dnode_get()
929 * to get the corresponding dnode from the candidate
930 * configuration that is being committed.
931 */
932 yang_dnode_get_path(dnode, xpath, sizeof(xpath));
933 dnode = yang_dnode_get(transaction->config->dnode,
934 xpath);
935 }
936 while (dnode) {
937 char xpath[XPATH_MAXLEN];
938 struct nb_node *nb_node;
939
940 nb_node = dnode->schema->priv;
941 if (!nb_node->cbs.apply_finish)
942 goto next;
943
944 /*
945 * Don't call the callback more than once for the same
946 * data node.
947 */
948 yang_dnode_get_path(dnode, xpath, sizeof(xpath));
949 if (nb_apply_finish_cb_find(&cbs, xpath, nb_node))
950 goto next;
951
952 nb_apply_finish_cb_new(&cbs, xpath, nb_node, dnode);
953
954 next:
955 dnode = dnode->parent;
956 }
957 }
958
959 /* Call the 'apply_finish' callbacks, sorted by their priorities. */
960 RB_FOREACH (cb, nb_config_cbs, &cbs) {
961 if (debug_northbound)
962 nb_log_callback(NB_EV_APPLY, NB_OP_APPLY_FINISH,
963 cb->xpath, NULL);
964
965 (*cb->nb_node->cbs.apply_finish)(cb->dnode);
966 }
967
968 /* Release memory. */
969 while (!RB_EMPTY(nb_config_cbs, &cbs)) {
970 cb = RB_ROOT(nb_config_cbs, &cbs);
971 RB_REMOVE(nb_config_cbs, &cbs, cb);
972 XFREE(MTYPE_TMP, cb);
973 }
974 }
975
976 static int nb_oper_data_iter_children(const struct lys_node *snode,
977 const char *xpath, const void *list_entry,
978 const struct yang_list_keys *list_keys,
979 struct yang_translator *translator,
980 bool first, uint32_t flags,
981 nb_oper_data_cb cb, void *arg)
982 {
983 struct lys_node *child;
984
985 LY_TREE_FOR (snode->child, child) {
986 int ret;
987
988 ret = nb_oper_data_iter_node(child, xpath, list_entry,
989 list_keys, translator, false,
990 flags, cb, arg);
991 if (ret != NB_OK)
992 return ret;
993 }
994
995 return NB_OK;
996 }
997
998 static int nb_oper_data_iter_leaf(const struct nb_node *nb_node,
999 const char *xpath, const void *list_entry,
1000 const struct yang_list_keys *list_keys,
1001 struct yang_translator *translator,
1002 uint32_t flags, nb_oper_data_cb cb, void *arg)
1003 {
1004 struct yang_data *data;
1005
1006 if (CHECK_FLAG(nb_node->snode->flags, LYS_CONFIG_W))
1007 return NB_OK;
1008
1009 /* Ignore list keys. */
1010 if (lys_is_key((struct lys_node_leaf *)nb_node->snode, NULL))
1011 return NB_OK;
1012
1013 data = nb_node->cbs.get_elem(xpath, list_entry);
1014 if (data == NULL)
1015 /* Leaf of type "empty" is not present. */
1016 return NB_OK;
1017
1018 return (*cb)(nb_node->snode, translator, data, arg);
1019 }
1020
1021 static int nb_oper_data_iter_container(const struct nb_node *nb_node,
1022 const char *xpath,
1023 const void *list_entry,
1024 const struct yang_list_keys *list_keys,
1025 struct yang_translator *translator,
1026 uint32_t flags, nb_oper_data_cb cb,
1027 void *arg)
1028 {
1029 if (CHECK_FLAG(nb_node->flags, F_NB_NODE_CONFIG_ONLY))
1030 return NB_OK;
1031
1032 /* Presence containers. */
1033 if (nb_node->cbs.get_elem) {
1034 struct yang_data *data;
1035 int ret;
1036
1037 data = nb_node->cbs.get_elem(xpath, list_entry);
1038 if (data == NULL)
1039 /* Presence container is not present. */
1040 return NB_OK;
1041
1042 ret = (*cb)(nb_node->snode, translator, data, arg);
1043 if (ret != NB_OK)
1044 return ret;
1045 }
1046
1047 /* Iterate over the child nodes. */
1048 return nb_oper_data_iter_children(nb_node->snode, xpath, list_entry,
1049 list_keys, translator, false, flags,
1050 cb, arg);
1051 }
1052
1053 static int
1054 nb_oper_data_iter_leaflist(const struct nb_node *nb_node, const char *xpath,
1055 const void *parent_list_entry,
1056 const struct yang_list_keys *parent_list_keys,
1057 struct yang_translator *translator, uint32_t flags,
1058 nb_oper_data_cb cb, void *arg)
1059 {
1060 const void *list_entry = NULL;
1061
1062 if (CHECK_FLAG(nb_node->snode->flags, LYS_CONFIG_W))
1063 return NB_OK;
1064
1065 do {
1066 struct yang_data *data;
1067 int ret;
1068
1069 list_entry =
1070 nb_node->cbs.get_next(parent_list_entry, list_entry);
1071 if (!list_entry)
1072 /* End of the list. */
1073 break;
1074
1075 data = nb_node->cbs.get_elem(xpath, list_entry);
1076 if (data == NULL)
1077 continue;
1078
1079 ret = (*cb)(nb_node->snode, translator, data, arg);
1080 if (ret != NB_OK)
1081 return ret;
1082 } while (list_entry);
1083
1084 return NB_OK;
1085 }
1086
1087 static int nb_oper_data_iter_list(const struct nb_node *nb_node,
1088 const char *xpath_list,
1089 const void *parent_list_entry,
1090 const struct yang_list_keys *parent_list_keys,
1091 struct yang_translator *translator,
1092 uint32_t flags, nb_oper_data_cb cb, void *arg)
1093 {
1094 struct lys_node_list *slist = (struct lys_node_list *)nb_node->snode;
1095 const void *list_entry = NULL;
1096 uint32_t position = 1;
1097
1098 if (CHECK_FLAG(nb_node->flags, F_NB_NODE_CONFIG_ONLY))
1099 return NB_OK;
1100
1101 /* Iterate over all list entries. */
1102 do {
1103 struct yang_list_keys list_keys;
1104 char xpath[XPATH_MAXLEN * 2];
1105 int ret;
1106
1107 /* Obtain list entry. */
1108 list_entry =
1109 nb_node->cbs.get_next(parent_list_entry, list_entry);
1110 if (!list_entry)
1111 /* End of the list. */
1112 break;
1113
1114 if (!CHECK_FLAG(nb_node->flags, F_NB_NODE_KEYLESS_LIST)) {
1115 /* Obtain the list entry keys. */
1116 if (nb_node->cbs.get_keys(list_entry, &list_keys)
1117 != NB_OK) {
1118 flog_warn(EC_LIB_NB_CB_STATE,
1119 "%s: failed to get list keys",
1120 __func__);
1121 return NB_ERR;
1122 }
1123
1124 /* Build XPath of the list entry. */
1125 strlcpy(xpath, xpath_list, sizeof(xpath));
1126 for (unsigned int i = 0; i < list_keys.num; i++) {
1127 snprintf(xpath + strlen(xpath),
1128 sizeof(xpath) - strlen(xpath),
1129 "[%s='%s']", slist->keys[i]->name,
1130 list_keys.key[i]);
1131 }
1132 } else {
1133 /*
1134 * Keyless list - build XPath using a positional index.
1135 */
1136 snprintf(xpath, sizeof(xpath), "%s[%u]", xpath_list,
1137 position);
1138 position++;
1139 }
1140
1141 /* Iterate over the child nodes. */
1142 ret = nb_oper_data_iter_children(
1143 nb_node->snode, xpath, list_entry, &list_keys,
1144 translator, false, flags, cb, arg);
1145 if (ret != NB_OK)
1146 return ret;
1147 } while (list_entry);
1148
1149 return NB_OK;
1150 }
1151
1152 static int nb_oper_data_iter_node(const struct lys_node *snode,
1153 const char *xpath_parent,
1154 const void *list_entry,
1155 const struct yang_list_keys *list_keys,
1156 struct yang_translator *translator,
1157 bool first, uint32_t flags,
1158 nb_oper_data_cb cb, void *arg)
1159 {
1160 struct nb_node *nb_node;
1161 char xpath[XPATH_MAXLEN];
1162 int ret = NB_OK;
1163
1164 if (!first && CHECK_FLAG(flags, NB_OPER_DATA_ITER_NORECURSE)
1165 && CHECK_FLAG(snode->nodetype, LYS_CONTAINER | LYS_LIST))
1166 return NB_OK;
1167
1168 /* Update XPath. */
1169 strlcpy(xpath, xpath_parent, sizeof(xpath));
1170 if (!first && snode->nodetype != LYS_USES)
1171 snprintf(xpath + strlen(xpath), sizeof(xpath) - strlen(xpath),
1172 "/%s", snode->name);
1173
1174 nb_node = snode->priv;
1175 switch (snode->nodetype) {
1176 case LYS_CONTAINER:
1177 ret = nb_oper_data_iter_container(nb_node, xpath, list_entry,
1178 list_keys, translator, flags,
1179 cb, arg);
1180 break;
1181 case LYS_LEAF:
1182 ret = nb_oper_data_iter_leaf(nb_node, xpath, list_entry,
1183 list_keys, translator, flags, cb,
1184 arg);
1185 break;
1186 case LYS_LEAFLIST:
1187 ret = nb_oper_data_iter_leaflist(nb_node, xpath, list_entry,
1188 list_keys, translator, flags,
1189 cb, arg);
1190 break;
1191 case LYS_LIST:
1192 ret = nb_oper_data_iter_list(nb_node, xpath, list_entry,
1193 list_keys, translator, flags, cb,
1194 arg);
1195 break;
1196 case LYS_USES:
1197 ret = nb_oper_data_iter_children(snode, xpath, list_entry,
1198 list_keys, translator, false,
1199 flags, cb, arg);
1200 break;
1201 default:
1202 break;
1203 }
1204
1205 return ret;
1206 }
1207
1208 int nb_oper_data_iterate(const char *xpath, struct yang_translator *translator,
1209 uint32_t flags, nb_oper_data_cb cb, void *arg)
1210 {
1211 struct nb_node *nb_node;
1212 const void *list_entry = NULL;
1213 struct yang_list_keys list_keys;
1214 struct list *list_dnodes;
1215 struct lyd_node *dnode, *dn;
1216 struct listnode *ln;
1217 int ret;
1218
1219 nb_node = nb_node_find(xpath);
1220 if (!nb_node) {
1221 flog_warn(EC_LIB_YANG_UNKNOWN_DATA_PATH,
1222 "%s: unknown data path: %s", __func__, xpath);
1223 return NB_ERR;
1224 }
1225
1226 /* For now this function works only with containers and lists. */
1227 if (!CHECK_FLAG(nb_node->snode->nodetype, LYS_CONTAINER | LYS_LIST)) {
1228 flog_warn(
1229 EC_LIB_NB_OPERATIONAL_DATA,
1230 "%s: can't iterate over YANG leaf or leaf-list [xpath %s]",
1231 __func__, xpath);
1232 return NB_ERR;
1233 }
1234
1235 /*
1236 * Create a data tree from the XPath so that we can parse the keys of
1237 * all YANG lists (if any).
1238 */
1239 ly_errno = 0;
1240 dnode = lyd_new_path(NULL, ly_native_ctx, xpath, NULL, 0,
1241 LYD_PATH_OPT_UPDATE);
1242 if (!dnode && ly_errno) {
1243 flog_warn(EC_LIB_LIBYANG, "%s: lyd_new_path() failed",
1244 __func__);
1245 return NB_ERR;
1246 }
1247 /*
1248 * We can remove the following two lines once we depend on
1249 * libyang-v0.16-r2, which has the LYD_PATH_OPT_NOPARENTRET flag for
1250 * lyd_new_path().
1251 */
1252 dnode = yang_dnode_get(dnode, xpath);
1253 assert(dnode);
1254
1255 /*
1256 * Create a linked list to sort the data nodes starting from the root.
1257 */
1258 list_dnodes = list_new();
1259 for (dn = dnode; dn; dn = dn->parent) {
1260 if (dn->schema->nodetype != LYS_LIST || !dn->child)
1261 continue;
1262 listnode_add_head(list_dnodes, dn);
1263 }
1264 /*
1265 * Use the northbound callbacks to find list entry pointer corresponding
1266 * to the given XPath.
1267 */
1268 for (ALL_LIST_ELEMENTS_RO(list_dnodes, ln, dn)) {
1269 struct lyd_node *child;
1270 struct nb_node *nn;
1271 unsigned int n = 0;
1272
1273 /* Obtain the list entry keys. */
1274 memset(&list_keys, 0, sizeof(list_keys));
1275 LY_TREE_FOR (dn->child, child) {
1276 if (!lys_is_key((struct lys_node_leaf *)child->schema,
1277 NULL))
1278 continue;
1279 strlcpy(list_keys.key[n],
1280 yang_dnode_get_string(child, NULL),
1281 sizeof(list_keys.key[n]));
1282 n++;
1283 }
1284 list_keys.num = n;
1285 if (list_keys.num
1286 != ((struct lys_node_list *)dn->schema)->keys_size) {
1287 list_delete(&list_dnodes);
1288 yang_dnode_free(dnode);
1289 return NB_ERR_NOT_FOUND;
1290 }
1291
1292 /* Find the list entry pointer. */
1293 nn = dn->schema->priv;
1294 list_entry = nn->cbs.lookup_entry(list_entry, &list_keys);
1295 if (list_entry == NULL) {
1296 list_delete(&list_dnodes);
1297 yang_dnode_free(dnode);
1298 return NB_ERR_NOT_FOUND;
1299 }
1300 }
1301
1302 /* If a list entry was given, iterate over that list entry only. */
1303 if (dnode->schema->nodetype == LYS_LIST && dnode->child)
1304 ret = nb_oper_data_iter_children(
1305 nb_node->snode, xpath, list_entry, &list_keys,
1306 translator, true, flags, cb, arg);
1307 else
1308 ret = nb_oper_data_iter_node(nb_node->snode, xpath, list_entry,
1309 &list_keys, translator, true,
1310 flags, cb, arg);
1311
1312 list_delete(&list_dnodes);
1313 yang_dnode_free(dnode);
1314
1315 return ret;
1316 }
1317
1318 bool nb_operation_is_valid(enum nb_operation operation,
1319 const struct lys_node *snode)
1320 {
1321 struct nb_node *nb_node = snode->priv;
1322 struct lys_node_container *scontainer;
1323 struct lys_node_leaf *sleaf;
1324
1325 switch (operation) {
1326 case NB_OP_CREATE:
1327 if (!CHECK_FLAG(snode->flags, LYS_CONFIG_W))
1328 return false;
1329
1330 switch (snode->nodetype) {
1331 case LYS_LEAF:
1332 sleaf = (struct lys_node_leaf *)snode;
1333 if (sleaf->type.base != LY_TYPE_EMPTY)
1334 return false;
1335 break;
1336 case LYS_CONTAINER:
1337 scontainer = (struct lys_node_container *)snode;
1338 if (!scontainer->presence)
1339 return false;
1340 break;
1341 case LYS_LIST:
1342 case LYS_LEAFLIST:
1343 break;
1344 default:
1345 return false;
1346 }
1347 return true;
1348 case NB_OP_MODIFY:
1349 if (!CHECK_FLAG(snode->flags, LYS_CONFIG_W))
1350 return false;
1351
1352 switch (snode->nodetype) {
1353 case LYS_LEAF:
1354 sleaf = (struct lys_node_leaf *)snode;
1355 if (sleaf->type.base == LY_TYPE_EMPTY)
1356 return false;
1357
1358 /* List keys can't be modified. */
1359 if (lys_is_key(sleaf, NULL))
1360 return false;
1361 break;
1362 default:
1363 return false;
1364 }
1365 return true;
1366 case NB_OP_DESTROY:
1367 if (!CHECK_FLAG(snode->flags, LYS_CONFIG_W))
1368 return false;
1369
1370 switch (snode->nodetype) {
1371 case LYS_LEAF:
1372 sleaf = (struct lys_node_leaf *)snode;
1373
1374 /* List keys can't be deleted. */
1375 if (lys_is_key(sleaf, NULL))
1376 return false;
1377
1378 /*
1379 * Only optional leafs can be deleted, or leafs whose
1380 * parent is a case statement.
1381 */
1382 if (snode->parent->nodetype == LYS_CASE)
1383 return true;
1384 if (sleaf->when)
1385 return true;
1386 if (CHECK_FLAG(sleaf->flags, LYS_MAND_TRUE)
1387 || sleaf->dflt)
1388 return false;
1389 break;
1390 case LYS_CONTAINER:
1391 scontainer = (struct lys_node_container *)snode;
1392 if (!scontainer->presence)
1393 return false;
1394 break;
1395 case LYS_LIST:
1396 case LYS_LEAFLIST:
1397 break;
1398 default:
1399 return false;
1400 }
1401 return true;
1402 case NB_OP_MOVE:
1403 if (!CHECK_FLAG(snode->flags, LYS_CONFIG_W))
1404 return false;
1405
1406 switch (snode->nodetype) {
1407 case LYS_LIST:
1408 case LYS_LEAFLIST:
1409 if (!CHECK_FLAG(snode->flags, LYS_USERORDERED))
1410 return false;
1411 break;
1412 default:
1413 return false;
1414 }
1415 return true;
1416 case NB_OP_APPLY_FINISH:
1417 if (!CHECK_FLAG(snode->flags, LYS_CONFIG_W))
1418 return false;
1419 return true;
1420 case NB_OP_GET_ELEM:
1421 if (!CHECK_FLAG(snode->flags, LYS_CONFIG_R))
1422 return false;
1423
1424 switch (snode->nodetype) {
1425 case LYS_LEAF:
1426 case LYS_LEAFLIST:
1427 break;
1428 case LYS_CONTAINER:
1429 scontainer = (struct lys_node_container *)snode;
1430 if (!scontainer->presence)
1431 return false;
1432 break;
1433 default:
1434 return false;
1435 }
1436 return true;
1437 case NB_OP_GET_NEXT:
1438 switch (snode->nodetype) {
1439 case LYS_LIST:
1440 if (CHECK_FLAG(nb_node->flags, F_NB_NODE_CONFIG_ONLY))
1441 return false;
1442 break;
1443 case LYS_LEAFLIST:
1444 if (CHECK_FLAG(snode->flags, LYS_CONFIG_W))
1445 return false;
1446 break;
1447 default:
1448 return false;
1449 }
1450 return true;
1451 case NB_OP_GET_KEYS:
1452 case NB_OP_LOOKUP_ENTRY:
1453 switch (snode->nodetype) {
1454 case LYS_LIST:
1455 if (CHECK_FLAG(nb_node->flags, F_NB_NODE_CONFIG_ONLY))
1456 return false;
1457 if (CHECK_FLAG(nb_node->flags, F_NB_NODE_KEYLESS_LIST))
1458 return false;
1459 break;
1460 default:
1461 return false;
1462 }
1463 return true;
1464 case NB_OP_RPC:
1465 if (CHECK_FLAG(snode->flags, LYS_CONFIG_W | LYS_CONFIG_R))
1466 return false;
1467
1468 switch (snode->nodetype) {
1469 case LYS_RPC:
1470 case LYS_ACTION:
1471 break;
1472 default:
1473 return false;
1474 }
1475 return true;
1476 default:
1477 return false;
1478 }
1479 }
1480
1481 DEFINE_HOOK(nb_notification_send, (const char *xpath, struct list *arguments),
1482 (xpath, arguments));
1483
1484 int nb_notification_send(const char *xpath, struct list *arguments)
1485 {
1486 int ret;
1487
1488 ret = hook_call(nb_notification_send, xpath, arguments);
1489 if (arguments)
1490 list_delete(&arguments);
1491
1492 return ret;
1493 }
1494
1495 const char *nb_event_name(enum nb_event event)
1496 {
1497 switch (event) {
1498 case NB_EV_VALIDATE:
1499 return "validate";
1500 case NB_EV_PREPARE:
1501 return "prepare";
1502 case NB_EV_ABORT:
1503 return "abort";
1504 case NB_EV_APPLY:
1505 return "apply";
1506 default:
1507 return "unknown";
1508 }
1509 }
1510
1511 const char *nb_operation_name(enum nb_operation operation)
1512 {
1513 switch (operation) {
1514 case NB_OP_CREATE:
1515 return "create";
1516 case NB_OP_MODIFY:
1517 return "modify";
1518 case NB_OP_DESTROY:
1519 return "destroy";
1520 case NB_OP_MOVE:
1521 return "move";
1522 case NB_OP_APPLY_FINISH:
1523 return "apply_finish";
1524 case NB_OP_GET_ELEM:
1525 return "get_elem";
1526 case NB_OP_GET_NEXT:
1527 return "get_next";
1528 case NB_OP_GET_KEYS:
1529 return "get_keys";
1530 case NB_OP_LOOKUP_ENTRY:
1531 return "lookup_entry";
1532 case NB_OP_RPC:
1533 return "rpc";
1534 default:
1535 return "unknown";
1536 }
1537 }
1538
1539 const char *nb_err_name(enum nb_error error)
1540 {
1541 switch (error) {
1542 case NB_OK:
1543 return "ok";
1544 case NB_ERR:
1545 return "generic error";
1546 case NB_ERR_NO_CHANGES:
1547 return "no changes";
1548 case NB_ERR_NOT_FOUND:
1549 return "element not found";
1550 case NB_ERR_LOCKED:
1551 return "resource is locked";
1552 case NB_ERR_VALIDATION:
1553 return "validation error";
1554 case NB_ERR_RESOURCE:
1555 return "failed to allocate resource";
1556 case NB_ERR_INCONSISTENCY:
1557 return "internal inconsistency";
1558 default:
1559 return "unknown";
1560 }
1561 }
1562
1563 const char *nb_client_name(enum nb_client client)
1564 {
1565 switch (client) {
1566 case NB_CLIENT_CLI:
1567 return "CLI";
1568 case NB_CLIENT_CONFD:
1569 return "ConfD";
1570 case NB_CLIENT_SYSREPO:
1571 return "Sysrepo";
1572 default:
1573 return "unknown";
1574 }
1575 }
1576
1577 static void nb_load_callbacks(const struct frr_yang_module_info *module)
1578 {
1579 for (size_t i = 0; module->nodes[i].xpath; i++) {
1580 struct nb_node *nb_node;
1581 uint32_t priority;
1582
1583 nb_node = nb_node_find(module->nodes[i].xpath);
1584 if (!nb_node) {
1585 flog_warn(EC_LIB_YANG_UNKNOWN_DATA_PATH,
1586 "%s: unknown data path: %s", __func__,
1587 module->nodes[i].xpath);
1588 continue;
1589 }
1590
1591 nb_node->cbs = module->nodes[i].cbs;
1592 priority = module->nodes[i].priority;
1593 if (priority != 0)
1594 nb_node->priority = priority;
1595 }
1596 }
1597
1598 void nb_init(struct thread_master *tm,
1599 const struct frr_yang_module_info *modules[], size_t nmodules)
1600 {
1601 unsigned int errors = 0;
1602
1603 /* Load YANG modules. */
1604 for (size_t i = 0; i < nmodules; i++)
1605 yang_module_load(modules[i]->name);
1606
1607 /* Create a nb_node for all YANG schema nodes. */
1608 nb_nodes_create();
1609
1610 /* Load northbound callbacks. */
1611 for (size_t i = 0; i < nmodules; i++)
1612 nb_load_callbacks(modules[i]);
1613
1614 /* Validate northbound callbacks. */
1615 yang_snodes_iterate_all(nb_node_validate, 0, &errors);
1616 if (errors > 0) {
1617 flog_err(
1618 EC_LIB_NB_CBS_VALIDATION,
1619 "%s: failed to validate northbound callbacks: %u error(s)",
1620 __func__, errors);
1621 exit(1);
1622 }
1623
1624 /* Initialize the northbound database (used for the rollback log). */
1625 if (nb_db_init() != NB_OK)
1626 flog_warn(EC_LIB_NB_DATABASE,
1627 "%s: failed to initialize northbound database",
1628 __func__);
1629
1630 /* Create an empty running configuration. */
1631 running_config = nb_config_new(NULL);
1632
1633 /* Initialize the northbound CLI. */
1634 nb_cli_init(tm);
1635 }
1636
1637 void nb_terminate(void)
1638 {
1639 /* Terminate the northbound CLI. */
1640 nb_cli_terminate();
1641
1642 /* Delete all nb_node's from all YANG modules. */
1643 nb_nodes_delete();
1644
1645 /* Delete the running configuration. */
1646 nb_config_free(running_config);
1647 }