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