]> git.proxmox.com Git - mirror_frr.git/blame - lib/northbound.c
Merge pull request #5625 from qlyoung/fix-zapi-ipset-name-nullterm
[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 383 change->cb.nb_node = dnode->schema->priv;
1c2facd1
RW
384 change->cb.dnode = dnode;
385
386 RB_INSERT(nb_config_cbs, changes, &change->cb);
387}
388
389static void nb_config_diff_del_changes(struct nb_config_cbs *changes)
390{
391 while (!RB_EMPTY(nb_config_cbs, changes)) {
392 struct nb_config_change *change;
393
394 change = (struct nb_config_change *)RB_ROOT(nb_config_cbs,
395 changes);
396 RB_REMOVE(nb_config_cbs, changes, &change->cb);
397 XFREE(MTYPE_TMP, change);
398 }
399}
400
401/*
402 * Helper function used when calculating the delta between two different
403 * configurations. Given a new subtree, calculate all new YANG data nodes,
404 * excluding default leafs and leaf-lists. This is a recursive function.
405 */
6b5d6e2d 406static void nb_config_diff_created(const struct lyd_node *dnode, uint32_t *seq,
cacbffaf 407 struct nb_config_cbs *changes)
1c2facd1 408{
cacbffaf 409 enum nb_operation operation;
1c2facd1
RW
410 struct lyd_node *child;
411
cacbffaf
RW
412 switch (dnode->schema->nodetype) {
413 case LYS_LEAF:
414 case LYS_LEAFLIST:
415 if (lyd_wd_default((struct lyd_node_leaf_list *)dnode))
416 break;
1c2facd1 417
cacbffaf
RW
418 if (nb_operation_is_valid(NB_OP_CREATE, dnode->schema))
419 operation = NB_OP_CREATE;
420 else if (nb_operation_is_valid(NB_OP_MODIFY, dnode->schema))
421 operation = NB_OP_MODIFY;
422 else
423 return;
1c2facd1 424
6b5d6e2d 425 nb_config_diff_add_change(changes, operation, seq, dnode);
cacbffaf
RW
426 break;
427 case LYS_CONTAINER:
428 case LYS_LIST:
429 if (nb_operation_is_valid(NB_OP_CREATE, dnode->schema))
6b5d6e2d
RW
430 nb_config_diff_add_change(changes, NB_OP_CREATE, seq,
431 dnode);
cacbffaf
RW
432
433 /* Process child nodes recursively. */
434 LY_TREE_FOR (dnode->child, child) {
6b5d6e2d 435 nb_config_diff_created(child, seq, changes);
1c2facd1 436 }
cacbffaf
RW
437 break;
438 default:
439 break;
1c2facd1
RW
440 }
441}
442
6b5d6e2d 443static void nb_config_diff_deleted(const struct lyd_node *dnode, uint32_t *seq,
1912caa2
RW
444 struct nb_config_cbs *changes)
445{
446 if (nb_operation_is_valid(NB_OP_DESTROY, dnode->schema))
6b5d6e2d 447 nb_config_diff_add_change(changes, NB_OP_DESTROY, seq, dnode);
1912caa2
RW
448 else if (CHECK_FLAG(dnode->schema->nodetype, LYS_CONTAINER)) {
449 struct lyd_node *child;
450
451 /*
452 * Non-presence containers need special handling since they
453 * don't have "destroy" callbacks. In this case, what we need to
454 * do is to call the "destroy" callbacks of their child nodes
455 * when applicable (i.e. optional nodes).
456 */
457 LY_TREE_FOR (dnode->child, child) {
6b5d6e2d 458 nb_config_diff_deleted(child, seq, changes);
1912caa2
RW
459 }
460 }
461}
462
1c2facd1
RW
463/* Calculate the delta between two different configurations. */
464static void nb_config_diff(const struct nb_config *config1,
465 const struct nb_config *config2,
466 struct nb_config_cbs *changes)
467{
468 struct lyd_difflist *diff;
6b5d6e2d 469 uint32_t seq = 0;
1c2facd1
RW
470
471 diff = lyd_diff(config1->dnode, config2->dnode,
472 LYD_DIFFOPT_WITHDEFAULTS);
473 assert(diff);
474
475 for (int i = 0; diff->type[i] != LYD_DIFF_END; i++) {
476 LYD_DIFFTYPE type;
477 struct lyd_node *dnode;
1c2facd1
RW
478
479 type = diff->type[i];
480
481 switch (type) {
482 case LYD_DIFF_CREATED:
483 dnode = diff->second[i];
6b5d6e2d 484 nb_config_diff_created(dnode, &seq, changes);
1c2facd1
RW
485 break;
486 case LYD_DIFF_DELETED:
487 dnode = diff->first[i];
6b5d6e2d 488 nb_config_diff_deleted(dnode, &seq, changes);
1c2facd1
RW
489 break;
490 case LYD_DIFF_CHANGED:
491 dnode = diff->second[i];
6b5d6e2d
RW
492 nb_config_diff_add_change(changes, NB_OP_MODIFY, &seq,
493 dnode);
1c2facd1
RW
494 break;
495 case LYD_DIFF_MOVEDAFTER1:
496 case LYD_DIFF_MOVEDAFTER2:
497 default:
498 continue;
499 }
1c2facd1
RW
500 }
501
502 lyd_free_diff(diff);
503}
504
505int nb_candidate_edit(struct nb_config *candidate,
506 const struct nb_node *nb_node,
507 enum nb_operation operation, const char *xpath,
508 const struct yang_data *previous,
509 const struct yang_data *data)
510{
511 struct lyd_node *dnode;
512 char xpath_edit[XPATH_MAXLEN];
513
1c2facd1
RW
514 /* Use special notation for leaf-lists (RFC 6020, section 9.13.5). */
515 if (nb_node->snode->nodetype == LYS_LEAFLIST)
516 snprintf(xpath_edit, sizeof(xpath_edit), "%s[.='%s']", xpath,
517 data->value);
518 else
519 strlcpy(xpath_edit, xpath, sizeof(xpath_edit));
520
521 switch (operation) {
522 case NB_OP_CREATE:
523 case NB_OP_MODIFY:
524 ly_errno = 0;
525 dnode = lyd_new_path(candidate->dnode, ly_native_ctx,
526 xpath_edit, (void *)data->value, 0,
527 LYD_PATH_OPT_UPDATE);
528 if (!dnode && ly_errno) {
529 flog_warn(EC_LIB_LIBYANG, "%s: lyd_new_path() failed",
530 __func__);
531 return NB_ERR;
532 }
1c2facd1 533 break;
95ce849b 534 case NB_OP_DESTROY:
1c2facd1
RW
535 dnode = yang_dnode_get(candidate->dnode, xpath_edit);
536 if (!dnode)
537 /*
538 * Return a special error code so the caller can choose
539 * whether to ignore it or not.
540 */
541 return NB_ERR_NOT_FOUND;
542 lyd_free(dnode);
543 break;
544 case NB_OP_MOVE:
545 /* TODO: update configuration. */
546 break;
547 default:
548 flog_warn(EC_LIB_DEVELOPMENT,
549 "%s: unknown operation (%u) [xpath %s]", __func__,
550 operation, xpath_edit);
551 return NB_ERR;
552 }
553
554 return NB_OK;
555}
556
557bool nb_candidate_needs_update(const struct nb_config *candidate)
558{
8685be73
RW
559 if (candidate->version < running_config->version)
560 return true;
1c2facd1 561
8685be73 562 return false;
1c2facd1
RW
563}
564
565int nb_candidate_update(struct nb_config *candidate)
566{
567 struct nb_config *updated_config;
568
8685be73 569 updated_config = nb_config_dup(running_config);
1c2facd1
RW
570 if (nb_config_merge(updated_config, candidate, true) != NB_OK)
571 return NB_ERR;
572
573 nb_config_replace(candidate, updated_config, false);
574
575 return NB_OK;
576}
577
1c2facd1
RW
578/*
579 * Perform YANG syntactic and semantic validation.
580 *
581 * WARNING: lyd_validate() can change the configuration as part of the
582 * validation process.
583 */
584static int nb_candidate_validate_yang(struct nb_config *candidate)
585{
cd327983
RW
586 if (lyd_validate(&candidate->dnode,
587 LYD_OPT_STRICT | LYD_OPT_CONFIG | LYD_OPT_WHENAUTODEL,
1c2facd1
RW
588 ly_native_ctx)
589 != 0)
590 return NB_ERR_VALIDATION;
591
592 return NB_OK;
593}
594
595/* Perform code-level validation using the northbound callbacks. */
34224f0c
RW
596static int nb_candidate_validate_code(struct nb_config *candidate,
597 struct nb_config_cbs *changes)
1c2facd1
RW
598{
599 struct nb_config_cb *cb;
34224f0c
RW
600 struct lyd_node *root, *next, *child;
601 int ret;
602
603 /* First validate the candidate as a whole. */
604 LY_TREE_FOR (candidate->dnode, root) {
605 LY_TREE_DFS_BEGIN (root, next, child) {
606 struct nb_node *nb_node;
607
608 nb_node = child->schema->priv;
609 if (!nb_node->cbs.pre_validate)
610 goto next;
611
612 if (DEBUG_MODE_CHECK(&nb_dbg_cbs_config,
613 DEBUG_MODE_ALL)) {
614 char xpath[XPATH_MAXLEN];
1c2facd1 615
34224f0c
RW
616 yang_dnode_get_path(child, xpath,
617 sizeof(xpath));
618 nb_log_callback(NB_EV_VALIDATE,
619 NB_OP_PRE_VALIDATE, xpath,
620 NULL);
621 }
622
623 ret = (*nb_node->cbs.pre_validate)(child);
624 if (ret != NB_OK)
625 return NB_ERR_VALIDATION;
626
627 next:
628 LY_TREE_DFS_END(root, next, child);
629 }
630 }
631
632 /* Now validate the configuration changes. */
1c2facd1
RW
633 RB_FOREACH (cb, nb_config_cbs, changes) {
634 struct nb_config_change *change = (struct nb_config_change *)cb;
1c2facd1 635
9eb2c0a1 636 ret = nb_callback_configuration(NB_EV_VALIDATE, change);
1c2facd1
RW
637 if (ret != NB_OK)
638 return NB_ERR_VALIDATION;
639 }
640
641 return NB_OK;
642}
643
644int nb_candidate_validate(struct nb_config *candidate)
645{
646 struct nb_config_cbs changes;
647 int ret;
648
649 if (nb_candidate_validate_yang(candidate) != NB_OK)
650 return NB_ERR_VALIDATION;
651
652 RB_INIT(nb_config_cbs, &changes);
8685be73
RW
653 nb_config_diff(running_config, candidate, &changes);
654 ret = nb_candidate_validate_code(candidate, &changes);
655 nb_config_diff_del_changes(&changes);
1c2facd1
RW
656
657 return ret;
658}
659
660int nb_candidate_commit_prepare(struct nb_config *candidate,
364ad673
RW
661 enum nb_client client, const void *user,
662 const char *comment,
1c2facd1
RW
663 struct nb_transaction **transaction)
664{
665 struct nb_config_cbs changes;
666
667 if (nb_candidate_validate_yang(candidate) != NB_OK) {
668 flog_warn(EC_LIB_NB_CANDIDATE_INVALID,
669 "%s: failed to validate candidate configuration",
670 __func__);
671 return NB_ERR_VALIDATION;
672 }
673
674 RB_INIT(nb_config_cbs, &changes);
8685be73
RW
675 nb_config_diff(running_config, candidate, &changes);
676 if (RB_EMPTY(nb_config_cbs, &changes))
677 return NB_ERR_NO_CHANGES;
1c2facd1 678
8685be73
RW
679 if (nb_candidate_validate_code(candidate, &changes) != NB_OK) {
680 flog_warn(EC_LIB_NB_CANDIDATE_INVALID,
681 "%s: failed to validate candidate configuration",
682 __func__);
683 nb_config_diff_del_changes(&changes);
684 return NB_ERR_VALIDATION;
685 }
1c2facd1 686
8685be73
RW
687 *transaction =
688 nb_transaction_new(candidate, &changes, client, user, comment);
689 if (*transaction == NULL) {
690 flog_warn(EC_LIB_NB_TRANSACTION_CREATION_FAILED,
691 "%s: failed to create transaction", __func__);
692 nb_config_diff_del_changes(&changes);
693 return NB_ERR_LOCKED;
1c2facd1
RW
694 }
695
696 return nb_transaction_process(NB_EV_PREPARE, *transaction);
697}
698
699void nb_candidate_commit_abort(struct nb_transaction *transaction)
700{
701 (void)nb_transaction_process(NB_EV_ABORT, transaction);
702 nb_transaction_free(transaction);
703}
704
705void nb_candidate_commit_apply(struct nb_transaction *transaction,
706 bool save_transaction, uint32_t *transaction_id)
707{
708 (void)nb_transaction_process(NB_EV_APPLY, transaction);
709 nb_transaction_apply_finish(transaction);
710
711 /* Replace running by candidate. */
712 transaction->config->version++;
8685be73 713 nb_config_replace(running_config, transaction->config, true);
1c2facd1
RW
714
715 /* Record transaction. */
716 if (save_transaction
717 && nb_db_transaction_save(transaction, transaction_id) != NB_OK)
718 flog_warn(EC_LIB_NB_TRANSACTION_RECORD_FAILED,
719 "%s: failed to record transaction", __func__);
720
721 nb_transaction_free(transaction);
722}
723
724int nb_candidate_commit(struct nb_config *candidate, enum nb_client client,
364ad673
RW
725 const void *user, bool save_transaction,
726 const char *comment, uint32_t *transaction_id)
1c2facd1
RW
727{
728 struct nb_transaction *transaction = NULL;
729 int ret;
730
364ad673 731 ret = nb_candidate_commit_prepare(candidate, client, user, comment,
1c2facd1
RW
732 &transaction);
733 /*
734 * Apply the changes if the preparation phase succeeded. Otherwise abort
735 * the transaction.
736 */
737 if (ret == NB_OK)
738 nb_candidate_commit_apply(transaction, save_transaction,
739 transaction_id);
740 else if (transaction != NULL)
741 nb_candidate_commit_abort(transaction);
742
743 return ret;
744}
745
364ad673
RW
746int nb_running_lock(enum nb_client client, const void *user)
747{
748 int ret = -1;
749
00dffa8c 750 frr_with_mutex(&running_config_mgmt_lock.mtx) {
364ad673
RW
751 if (!running_config_mgmt_lock.locked) {
752 running_config_mgmt_lock.locked = true;
753 running_config_mgmt_lock.owner_client = client;
754 running_config_mgmt_lock.owner_user = user;
755 ret = 0;
756 }
757 }
364ad673
RW
758
759 return ret;
760}
761
762int nb_running_unlock(enum nb_client client, const void *user)
763{
764 int ret = -1;
765
00dffa8c 766 frr_with_mutex(&running_config_mgmt_lock.mtx) {
364ad673
RW
767 if (running_config_mgmt_lock.locked
768 && running_config_mgmt_lock.owner_client == client
769 && running_config_mgmt_lock.owner_user == user) {
770 running_config_mgmt_lock.locked = false;
771 running_config_mgmt_lock.owner_client = NB_CLIENT_NONE;
772 running_config_mgmt_lock.owner_user = NULL;
773 ret = 0;
774 }
775 }
364ad673
RW
776
777 return ret;
778}
779
780int nb_running_lock_check(enum nb_client client, const void *user)
781{
782 int ret = -1;
783
00dffa8c 784 frr_with_mutex(&running_config_mgmt_lock.mtx) {
364ad673
RW
785 if (!running_config_mgmt_lock.locked
786 || (running_config_mgmt_lock.owner_client == client
787 && running_config_mgmt_lock.owner_user == user))
788 ret = 0;
789 }
364ad673
RW
790
791 return ret;
792}
793
1c2facd1
RW
794static void nb_log_callback(const enum nb_event event,
795 enum nb_operation operation, const char *xpath,
796 const char *value)
797{
798 zlog_debug(
799 "northbound callback: event [%s] op [%s] xpath [%s] value [%s]",
800 nb_event_name(event), nb_operation_name(operation), xpath,
1b3f6ff1 801 value ? value : "(NULL)");
1c2facd1
RW
802}
803
804/*
805 * Call the northbound configuration callback associated to a given
806 * configuration change.
807 */
9eb2c0a1 808static int nb_callback_configuration(const enum nb_event event,
1c2facd1
RW
809 struct nb_config_change *change)
810{
811 enum nb_operation operation = change->cb.operation;
0de19c0e 812 char xpath[XPATH_MAXLEN];
1c2facd1
RW
813 const struct nb_node *nb_node = change->cb.nb_node;
814 const struct lyd_node *dnode = change->cb.dnode;
815 union nb_resource *resource;
816 int ret = NB_ERR;
817
9eb2c0a1 818 if (DEBUG_MODE_CHECK(&nb_dbg_cbs_config, DEBUG_MODE_ALL)) {
1c2facd1
RW
819 const char *value = "(none)";
820
821 if (dnode && !yang_snode_is_typeless_data(dnode->schema))
822 value = yang_dnode_get_string(dnode, NULL);
823
0de19c0e 824 yang_dnode_get_path(dnode, xpath, sizeof(xpath));
1c2facd1
RW
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:
0de19c0e 847 yang_dnode_get_path(dnode, xpath, sizeof(xpath));
c650e48c
RW
848 flog_err(EC_LIB_DEVELOPMENT,
849 "%s: unknown operation (%u) [xpath %s]", __func__,
850 operation, xpath);
851 exit(1);
1c2facd1
RW
852 }
853
625b70e3 854 if (ret != NB_OK) {
c650e48c
RW
855 int priority;
856 enum lib_log_refs ref;
ec348d43 857
0de19c0e
RW
858 yang_dnode_get_path(dnode, xpath, sizeof(xpath));
859
625b70e3
EDP
860 switch (event) {
861 case NB_EV_VALIDATE:
c650e48c 862 priority = LOG_WARNING;
625b70e3
EDP
863 ref = EC_LIB_NB_CB_CONFIG_VALIDATE;
864 break;
865 case NB_EV_PREPARE:
c650e48c 866 priority = LOG_WARNING;
625b70e3
EDP
867 ref = EC_LIB_NB_CB_CONFIG_PREPARE;
868 break;
869 case NB_EV_ABORT:
c650e48c 870 priority = LOG_WARNING;
625b70e3
EDP
871 ref = EC_LIB_NB_CB_CONFIG_ABORT;
872 break;
873 case NB_EV_APPLY:
c650e48c 874 priority = LOG_ERR;
625b70e3
EDP
875 ref = EC_LIB_NB_CB_CONFIG_APPLY;
876 break;
c650e48c
RW
877 default:
878 flog_err(EC_LIB_DEVELOPMENT,
879 "%s: unknown event (%u) [xpath %s]",
880 __func__, event, xpath);
881 exit(1);
625b70e3 882 }
c650e48c
RW
883
884 flog(priority, ref,
885 "%s: error processing configuration change: error [%s] event [%s] operation [%s] xpath [%s]",
886 __func__, nb_err_name(ret), nb_event_name(event),
887 nb_operation_name(operation), xpath);
625b70e3 888 }
1c2facd1
RW
889
890 return ret;
891}
892
9eb2c0a1
RW
893struct yang_data *nb_callback_get_elem(const struct nb_node *nb_node,
894 const char *xpath,
895 const void *list_entry)
896{
897 DEBUGD(&nb_dbg_cbs_state,
898 "northbound callback (get_elem): xpath [%s] list_entry [%p]",
899 xpath, list_entry);
900
901 return nb_node->cbs.get_elem(xpath, list_entry);
902}
903
904const void *nb_callback_get_next(const struct nb_node *nb_node,
905 const void *parent_list_entry,
906 const void *list_entry)
907{
908 DEBUGD(&nb_dbg_cbs_state,
909 "northbound callback (get_next): node [%s] parent_list_entry [%p] list_entry [%p]",
910 nb_node->xpath, parent_list_entry, list_entry);
911
912 return nb_node->cbs.get_next(parent_list_entry, list_entry);
913}
914
915int nb_callback_get_keys(const struct nb_node *nb_node, const void *list_entry,
916 struct yang_list_keys *keys)
917{
918 DEBUGD(&nb_dbg_cbs_state,
919 "northbound callback (get_keys): node [%s] list_entry [%p]",
920 nb_node->xpath, list_entry);
921
922 return nb_node->cbs.get_keys(list_entry, keys);
923}
924
925const void *nb_callback_lookup_entry(const struct nb_node *nb_node,
926 const void *parent_list_entry,
927 const struct yang_list_keys *keys)
928{
929 DEBUGD(&nb_dbg_cbs_state,
930 "northbound callback (lookup_entry): node [%s] parent_list_entry [%p]",
931 nb_node->xpath, parent_list_entry);
932
933 return nb_node->cbs.lookup_entry(parent_list_entry, keys);
934}
935
936int nb_callback_rpc(const struct nb_node *nb_node, const char *xpath,
937 const struct list *input, struct list *output)
938{
939 DEBUGD(&nb_dbg_cbs_rpc, "northbound RPC: %s", xpath);
940
941 return nb_node->cbs.rpc(xpath, input, output);
942}
943
364ad673
RW
944static struct nb_transaction *
945nb_transaction_new(struct nb_config *config, struct nb_config_cbs *changes,
946 enum nb_client client, const void *user, const char *comment)
1c2facd1
RW
947{
948 struct nb_transaction *transaction;
949
364ad673
RW
950 if (nb_running_lock_check(client, user)) {
951 flog_warn(
952 EC_LIB_NB_TRANSACTION_CREATION_FAILED,
953 "%s: running configuration is locked by another client",
954 __func__);
955 return NULL;
956 }
957
1c2facd1
RW
958 if (transaction_in_progress) {
959 flog_warn(
960 EC_LIB_NB_TRANSACTION_CREATION_FAILED,
961 "%s: error - there's already another transaction in progress",
962 __func__);
963 return NULL;
964 }
965 transaction_in_progress = true;
966
967 transaction = XCALLOC(MTYPE_TMP, sizeof(*transaction));
968 transaction->client = client;
969 if (comment)
970 strlcpy(transaction->comment, comment,
971 sizeof(transaction->comment));
972 transaction->config = config;
973 transaction->changes = *changes;
974
975 return transaction;
976}
977
978static void nb_transaction_free(struct nb_transaction *transaction)
979{
980 nb_config_diff_del_changes(&transaction->changes);
981 XFREE(MTYPE_TMP, transaction);
982 transaction_in_progress = false;
983}
984
985/* Process all configuration changes associated to a transaction. */
986static int nb_transaction_process(enum nb_event event,
987 struct nb_transaction *transaction)
988{
989 struct nb_config_cb *cb;
990
8685be73
RW
991 RB_FOREACH (cb, nb_config_cbs, &transaction->changes) {
992 struct nb_config_change *change = (struct nb_config_change *)cb;
993 int ret;
1c2facd1 994
8685be73
RW
995 /*
996 * Only try to release resources that were allocated
997 * successfully.
998 */
999 if (event == NB_EV_ABORT && change->prepare_ok == false)
1000 break;
1001
1002 /* Call the appropriate callback. */
1003 ret = nb_callback_configuration(event, change);
1004 switch (event) {
1005 case NB_EV_PREPARE:
1006 if (ret != NB_OK)
1007 return ret;
1008 change->prepare_ok = true;
1009 break;
1010 case NB_EV_ABORT:
1011 case NB_EV_APPLY:
1c2facd1 1012 /*
8685be73
RW
1013 * At this point it's not possible to reject the
1014 * transaction anymore, so any failure here can lead to
1015 * inconsistencies and should be treated as a bug.
1016 * Operations prone to errors, like validations and
1017 * resource allocations, should be performed during the
1018 * 'prepare' phase.
1c2facd1 1019 */
8685be73
RW
1020 break;
1021 default:
1022 break;
1c2facd1
RW
1023 }
1024 }
1025
1026 return NB_OK;
1027}
1028
1029static struct nb_config_cb *
0de19c0e 1030nb_apply_finish_cb_new(struct nb_config_cbs *cbs, const struct nb_node *nb_node,
1c2facd1
RW
1031 const struct lyd_node *dnode)
1032{
1033 struct nb_config_cb *cb;
1034
1035 cb = XCALLOC(MTYPE_TMP, sizeof(*cb));
1c2facd1
RW
1036 cb->nb_node = nb_node;
1037 cb->dnode = dnode;
1038 RB_INSERT(nb_config_cbs, cbs, cb);
1039
1040 return cb;
1041}
1042
1043static struct nb_config_cb *
fe3f2c61
RW
1044nb_apply_finish_cb_find(struct nb_config_cbs *cbs,
1045 const struct nb_node *nb_node,
1046 const struct lyd_node *dnode)
1c2facd1
RW
1047{
1048 struct nb_config_cb s;
1049
fe3f2c61 1050 s.seq = 0;
1c2facd1 1051 s.nb_node = nb_node;
fe3f2c61 1052 s.dnode = dnode;
1c2facd1
RW
1053 return RB_FIND(nb_config_cbs, cbs, &s);
1054}
1055
1056/* Call the 'apply_finish' callbacks. */
1057static void nb_transaction_apply_finish(struct nb_transaction *transaction)
1058{
1059 struct nb_config_cbs cbs;
1060 struct nb_config_cb *cb;
0de19c0e 1061 char xpath[XPATH_MAXLEN];
1c2facd1
RW
1062
1063 /* Initialize tree of 'apply_finish' callbacks. */
1064 RB_INIT(nb_config_cbs, &cbs);
1065
1066 /* Identify the 'apply_finish' callbacks that need to be called. */
1067 RB_FOREACH (cb, nb_config_cbs, &transaction->changes) {
1068 struct nb_config_change *change = (struct nb_config_change *)cb;
1069 const struct lyd_node *dnode = change->cb.dnode;
1070
1071 /*
1072 * Iterate up to the root of the data tree. When a node is being
1073 * deleted, skip its 'apply_finish' callback if one is defined
1074 * (the 'apply_finish' callbacks from the node ancestors should
1075 * be called though).
1076 */
95ce849b 1077 if (change->cb.operation == NB_OP_DESTROY) {
1c2facd1
RW
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) {
1c2facd1
RW
1093 struct nb_node *nb_node;
1094
1095 nb_node = dnode->schema->priv;
1096 if (!nb_node->cbs.apply_finish)
1097 goto next;
1098
1099 /*
1100 * Don't call the callback more than once for the same
1101 * data node.
1102 */
fe3f2c61 1103 if (nb_apply_finish_cb_find(&cbs, nb_node, dnode))
1c2facd1
RW
1104 goto next;
1105
0de19c0e 1106 nb_apply_finish_cb_new(&cbs, nb_node, dnode);
1c2facd1
RW
1107
1108 next:
1109 dnode = dnode->parent;
1110 }
1111 }
1112
1113 /* Call the 'apply_finish' callbacks, sorted by their priorities. */
1114 RB_FOREACH (cb, nb_config_cbs, &cbs) {
0de19c0e
RW
1115 if (DEBUG_MODE_CHECK(&nb_dbg_cbs_config, DEBUG_MODE_ALL)) {
1116 yang_dnode_get_path(cb->dnode, xpath, sizeof(xpath));
1117 nb_log_callback(NB_EV_APPLY, NB_OP_APPLY_FINISH, xpath,
1118 NULL);
1119 }
1c2facd1
RW
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 1884void nb_init(struct thread_master *tm,
0d8c7a26
DL
1885 const struct frr_yang_module_info *const modules[],
1886 size_t nmodules)
1c2facd1
RW
1887{
1888 unsigned int errors = 0;
1889
1890 /* Load YANG modules. */
1891 for (size_t i = 0; i < nmodules; i++)
1892 yang_module_load(modules[i]->name);
1893
1894 /* Create a nb_node for all YANG schema nodes. */
544ca69a 1895 nb_nodes_create();
1c2facd1
RW
1896
1897 /* Load northbound callbacks. */
1898 for (size_t i = 0; i < nmodules; i++)
1899 nb_load_callbacks(modules[i]);
1900
1901 /* Validate northbound callbacks. */
e0ccfad2 1902 yang_snodes_iterate_all(nb_node_validate, 0, &errors);
1c2facd1
RW
1903 if (errors > 0) {
1904 flog_err(
1905 EC_LIB_NB_CBS_VALIDATION,
1906 "%s: failed to validate northbound callbacks: %u error(s)",
1907 __func__, errors);
1908 exit(1);
1909 }
1910
1c2facd1
RW
1911 /* Create an empty running configuration. */
1912 running_config = nb_config_new(NULL);
ccd43ada
RW
1913 running_config_entries = hash_create(running_config_entry_key_make,
1914 running_config_entry_cmp,
1915 "Running Configuration Entries");
364ad673 1916 pthread_mutex_init(&running_config_mgmt_lock.mtx, NULL);
1c2facd1
RW
1917
1918 /* Initialize the northbound CLI. */
fbdc1c0a 1919 nb_cli_init(tm);
1c2facd1
RW
1920}
1921
1922void nb_terminate(void)
1923{
1924 /* Terminate the northbound CLI. */
1925 nb_cli_terminate();
1926
1927 /* Delete all nb_node's from all YANG modules. */
544ca69a 1928 nb_nodes_delete();
1c2facd1
RW
1929
1930 /* Delete the running configuration. */
ccd43ada
RW
1931 hash_clean(running_config_entries, running_config_entry_free);
1932 hash_free(running_config_entries);
1c2facd1 1933 nb_config_free(running_config);
364ad673 1934 pthread_mutex_destroy(&running_config_mgmt_lock.mtx);
1c2facd1 1935}