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