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