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