]>
Commit | Line | Data |
---|---|---|
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" | |
f7c20aa1 | 33 | #include "frrstr.h" |
1c2facd1 | 34 | |
bf8d3d6a DL |
35 | DEFINE_MTYPE_STATIC(LIB, NB_NODE, "Northbound Node"); |
36 | DEFINE_MTYPE_STATIC(LIB, NB_CONFIG, "Northbound Configuration"); | |
37 | DEFINE_MTYPE_STATIC(LIB, NB_CONFIG_ENTRY, "Northbound Configuration Entry"); | |
1c2facd1 RW |
38 | |
39 | /* Running configuration - shouldn't be modified directly. */ | |
40 | struct nb_config *running_config; | |
41 | ||
ccd43ada RW |
42 | /* Hash table of user pointers associated with configuration entries. */ |
43 | static struct hash *running_config_entries; | |
44 | ||
364ad673 RW |
45 | /* Management lock for the running configuration. */ |
46 | static struct { | |
47 | /* Mutex protecting this structure. */ | |
48 | pthread_mutex_t mtx; | |
49 | ||
50 | /* Actual lock. */ | |
51 | bool locked; | |
52 | ||
53 | /* Northbound client who owns this lock. */ | |
54 | enum nb_client owner_client; | |
55 | ||
56 | /* Northbound user who owns this lock. */ | |
57 | const void *owner_user; | |
58 | } running_config_mgmt_lock; | |
59 | ||
390a8862 CS |
60 | /* Knob to record config transaction */ |
61 | static bool nb_db_enabled; | |
1c2facd1 RW |
62 | /* |
63 | * Global lock used to prevent multiple configuration transactions from | |
64 | * happening concurrently. | |
65 | */ | |
66 | static bool transaction_in_progress; | |
67 | ||
13d6b9c1 RW |
68 | static int nb_callback_pre_validate(struct nb_context *context, |
69 | const struct nb_node *nb_node, | |
df5eda3d RW |
70 | const struct lyd_node *dnode, char *errmsg, |
71 | size_t errmsg_len); | |
13d6b9c1 RW |
72 | static int nb_callback_configuration(struct nb_context *context, |
73 | const enum nb_event event, | |
df5eda3d RW |
74 | struct nb_config_change *change, |
75 | char *errmsg, size_t errmsg_len); | |
76 | static struct nb_transaction * | |
77 | nb_transaction_new(struct nb_context *context, struct nb_config *config, | |
78 | struct nb_config_cbs *changes, const char *comment, | |
79 | char *errmsg, size_t errmsg_len); | |
1c2facd1 RW |
80 | static void nb_transaction_free(struct nb_transaction *transaction); |
81 | static int nb_transaction_process(enum nb_event event, | |
df5eda3d RW |
82 | struct nb_transaction *transaction, |
83 | char *errmsg, size_t errmsg_len); | |
84 | static void nb_transaction_apply_finish(struct nb_transaction *transaction, | |
85 | char *errmsg, size_t errmsg_len); | |
3bb513c3 | 86 | static int nb_oper_data_iter_node(const struct lysc_node *snode, |
1a4bc045 RW |
87 | const char *xpath, const void *list_entry, |
88 | const struct yang_list_keys *list_keys, | |
89 | struct yang_translator *translator, | |
90 | bool first, uint32_t flags, | |
91 | nb_oper_data_cb cb, void *arg); | |
1c2facd1 | 92 | |
3bb513c3 | 93 | static int nb_node_check_config_only(const struct lysc_node *snode, void *arg) |
544ca69a RW |
94 | { |
95 | bool *config_only = arg; | |
96 | ||
97 | if (CHECK_FLAG(snode->flags, LYS_CONFIG_R)) { | |
98 | *config_only = false; | |
99 | return YANG_ITER_STOP; | |
100 | } | |
101 | ||
102 | return YANG_ITER_CONTINUE; | |
103 | } | |
104 | ||
3bb513c3 | 105 | static int nb_node_new_cb(const struct lysc_node *snode, void *arg) |
1c2facd1 RW |
106 | { |
107 | struct nb_node *nb_node; | |
3bb513c3 | 108 | struct lysc_node *sparent, *sparent_list; |
1c2facd1 RW |
109 | |
110 | nb_node = XCALLOC(MTYPE_NB_NODE, sizeof(*nb_node)); | |
111 | yang_snode_get_path(snode, YANG_PATH_DATA, nb_node->xpath, | |
112 | sizeof(nb_node->xpath)); | |
113 | nb_node->priority = NB_DFLT_PRIORITY; | |
114 | sparent = yang_snode_real_parent(snode); | |
115 | if (sparent) | |
116 | nb_node->parent = sparent->priv; | |
117 | sparent_list = yang_snode_parent_list(snode); | |
118 | if (sparent_list) | |
119 | nb_node->parent_list = sparent_list->priv; | |
120 | ||
544ca69a RW |
121 | /* Set flags. */ |
122 | if (CHECK_FLAG(snode->nodetype, LYS_CONTAINER | LYS_LIST)) { | |
123 | bool config_only = true; | |
124 | ||
7895c3bc DS |
125 | (void)yang_snodes_iterate_subtree(snode, NULL, |
126 | nb_node_check_config_only, 0, | |
127 | &config_only); | |
544ca69a RW |
128 | if (config_only) |
129 | SET_FLAG(nb_node->flags, F_NB_NODE_CONFIG_ONLY); | |
130 | } | |
99fb518f | 131 | if (CHECK_FLAG(snode->nodetype, LYS_LIST)) { |
3bb513c3 | 132 | if (yang_snode_num_keys(snode) == 0) |
99fb518f RW |
133 | SET_FLAG(nb_node->flags, F_NB_NODE_KEYLESS_LIST); |
134 | } | |
544ca69a | 135 | |
1c2facd1 RW |
136 | /* |
137 | * Link the northbound node and the libyang schema node with one | |
138 | * another. | |
139 | */ | |
140 | nb_node->snode = snode; | |
8a923b48 | 141 | assert(snode->priv == NULL); |
3bb513c3 | 142 | ((struct lysc_node *)snode)->priv = nb_node; |
e0ccfad2 RW |
143 | |
144 | return YANG_ITER_CONTINUE; | |
1c2facd1 RW |
145 | } |
146 | ||
3bb513c3 | 147 | static int nb_node_del_cb(const struct lysc_node *snode, void *arg) |
1c2facd1 RW |
148 | { |
149 | struct nb_node *nb_node; | |
150 | ||
151 | nb_node = snode->priv; | |
9bde0b25 | 152 | if (nb_node) { |
3bb513c3 | 153 | ((struct lysc_node *)snode)->priv = NULL; |
9bde0b25 RW |
154 | XFREE(MTYPE_NB_NODE, nb_node); |
155 | } | |
e0ccfad2 RW |
156 | |
157 | return YANG_ITER_CONTINUE; | |
1c2facd1 RW |
158 | } |
159 | ||
544ca69a RW |
160 | void nb_nodes_create(void) |
161 | { | |
8d869d37 | 162 | yang_snodes_iterate(NULL, nb_node_new_cb, 0, NULL); |
544ca69a RW |
163 | } |
164 | ||
165 | void nb_nodes_delete(void) | |
166 | { | |
8d869d37 | 167 | yang_snodes_iterate(NULL, nb_node_del_cb, 0, NULL); |
544ca69a RW |
168 | } |
169 | ||
3bb513c3 | 170 | struct nb_node *nb_node_find(const char *path) |
1c2facd1 | 171 | { |
3bb513c3 | 172 | const struct lysc_node *snode; |
1c2facd1 RW |
173 | |
174 | /* | |
3bb513c3 | 175 | * Use libyang to find the schema node associated to the path and get |
1c2facd1 RW |
176 | * the northbound node from there (snode private pointer). |
177 | */ | |
3bb513c3 | 178 | snode = lys_find_path(ly_native_ctx, NULL, path, 0); |
1c2facd1 RW |
179 | if (!snode) |
180 | return NULL; | |
181 | ||
182 | return snode->priv; | |
183 | } | |
184 | ||
f182d8d8 IR |
185 | void nb_node_set_dependency_cbs(const char *dependency_xpath, |
186 | const char *dependant_xpath, | |
187 | struct nb_dependency_callbacks *cbs) | |
188 | { | |
189 | struct nb_node *dependency = nb_node_find(dependency_xpath); | |
190 | struct nb_node *dependant = nb_node_find(dependant_xpath); | |
191 | ||
192 | if (!dependency || !dependant) | |
193 | return; | |
194 | ||
195 | dependency->dep_cbs.get_dependant_xpath = cbs->get_dependant_xpath; | |
196 | dependant->dep_cbs.get_dependency_xpath = cbs->get_dependency_xpath; | |
197 | } | |
198 | ||
199 | bool nb_node_has_dependency(struct nb_node *node) | |
200 | { | |
201 | return node->dep_cbs.get_dependency_xpath != NULL; | |
202 | } | |
203 | ||
1c2facd1 RW |
204 | static int nb_node_validate_cb(const struct nb_node *nb_node, |
205 | enum nb_operation operation, | |
206 | int callback_implemented, bool optional) | |
207 | { | |
208 | bool valid; | |
209 | ||
210 | valid = nb_operation_is_valid(operation, nb_node->snode); | |
211 | ||
6f4e5edd RW |
212 | /* |
213 | * Add an exception for operational data callbacks. A rw list usually | |
214 | * doesn't need any associated operational data callbacks. But if this | |
215 | * rw list is augmented by another module which adds state nodes under | |
216 | * it, then this list will need to have the 'get_next()', 'get_keys()' | |
217 | * and 'lookup_entry()' callbacks. As such, never log a warning when | |
218 | * these callbacks are implemented when they are not needed, since this | |
219 | * depends on context (e.g. some daemons might augment "frr-interface" | |
220 | * while others don't). | |
221 | */ | |
222 | if (!valid && callback_implemented && operation != NB_OP_GET_NEXT | |
223 | && operation != NB_OP_GET_KEYS && operation != NB_OP_LOOKUP_ENTRY) | |
1c2facd1 RW |
224 | flog_warn(EC_LIB_NB_CB_UNNEEDED, |
225 | "unneeded '%s' callback for '%s'", | |
226 | nb_operation_name(operation), nb_node->xpath); | |
227 | ||
228 | if (!optional && valid && !callback_implemented) { | |
229 | flog_err(EC_LIB_NB_CB_MISSING, "missing '%s' callback for '%s'", | |
230 | nb_operation_name(operation), nb_node->xpath); | |
231 | return 1; | |
232 | } | |
233 | ||
234 | return 0; | |
235 | } | |
236 | ||
237 | /* | |
238 | * Check if the required callbacks were implemented for the given northbound | |
239 | * node. | |
240 | */ | |
241 | static unsigned int nb_node_validate_cbs(const struct nb_node *nb_node) | |
242 | ||
243 | { | |
244 | unsigned int error = 0; | |
245 | ||
246 | error += nb_node_validate_cb(nb_node, NB_OP_CREATE, | |
247 | !!nb_node->cbs.create, false); | |
248 | error += nb_node_validate_cb(nb_node, NB_OP_MODIFY, | |
249 | !!nb_node->cbs.modify, false); | |
95ce849b | 250 | error += nb_node_validate_cb(nb_node, NB_OP_DESTROY, |
d01b92fd | 251 | !!nb_node->cbs.destroy, false); |
1c2facd1 RW |
252 | error += nb_node_validate_cb(nb_node, NB_OP_MOVE, !!nb_node->cbs.move, |
253 | false); | |
34224f0c RW |
254 | error += nb_node_validate_cb(nb_node, NB_OP_PRE_VALIDATE, |
255 | !!nb_node->cbs.pre_validate, true); | |
1c2facd1 RW |
256 | error += nb_node_validate_cb(nb_node, NB_OP_APPLY_FINISH, |
257 | !!nb_node->cbs.apply_finish, true); | |
258 | error += nb_node_validate_cb(nb_node, NB_OP_GET_ELEM, | |
259 | !!nb_node->cbs.get_elem, false); | |
260 | error += nb_node_validate_cb(nb_node, NB_OP_GET_NEXT, | |
261 | !!nb_node->cbs.get_next, false); | |
262 | error += nb_node_validate_cb(nb_node, NB_OP_GET_KEYS, | |
263 | !!nb_node->cbs.get_keys, false); | |
264 | error += nb_node_validate_cb(nb_node, NB_OP_LOOKUP_ENTRY, | |
265 | !!nb_node->cbs.lookup_entry, false); | |
266 | error += nb_node_validate_cb(nb_node, NB_OP_RPC, !!nb_node->cbs.rpc, | |
267 | false); | |
268 | ||
269 | return error; | |
270 | } | |
271 | ||
272 | static unsigned int nb_node_validate_priority(const struct nb_node *nb_node) | |
273 | { | |
274 | /* Top-level nodes can have any priority. */ | |
275 | if (!nb_node->parent) | |
276 | return 0; | |
277 | ||
278 | if (nb_node->priority < nb_node->parent->priority) { | |
279 | flog_err(EC_LIB_NB_CB_INVALID_PRIO, | |
280 | "node has higher priority than its parent [xpath %s]", | |
281 | nb_node->xpath); | |
282 | return 1; | |
283 | } | |
284 | ||
285 | return 0; | |
286 | } | |
287 | ||
3bb513c3 | 288 | static int nb_node_validate(const struct lysc_node *snode, void *arg) |
1c2facd1 RW |
289 | { |
290 | struct nb_node *nb_node = snode->priv; | |
e0ccfad2 | 291 | unsigned int *errors = arg; |
1c2facd1 RW |
292 | |
293 | /* Validate callbacks and priority. */ | |
9bde0b25 RW |
294 | if (nb_node) { |
295 | *errors += nb_node_validate_cbs(nb_node); | |
296 | *errors += nb_node_validate_priority(nb_node); | |
297 | } | |
e0ccfad2 RW |
298 | |
299 | return YANG_ITER_CONTINUE; | |
1c2facd1 RW |
300 | } |
301 | ||
302 | struct nb_config *nb_config_new(struct lyd_node *dnode) | |
303 | { | |
304 | struct nb_config *config; | |
305 | ||
306 | config = XCALLOC(MTYPE_NB_CONFIG, sizeof(*config)); | |
307 | if (dnode) | |
308 | config->dnode = dnode; | |
309 | else | |
5e02643a | 310 | config->dnode = yang_dnode_new(ly_native_ctx, true); |
1c2facd1 RW |
311 | config->version = 0; |
312 | ||
313 | return config; | |
314 | } | |
315 | ||
316 | void nb_config_free(struct nb_config *config) | |
317 | { | |
318 | if (config->dnode) | |
319 | yang_dnode_free(config->dnode); | |
320 | XFREE(MTYPE_NB_CONFIG, config); | |
321 | } | |
322 | ||
323 | struct nb_config *nb_config_dup(const struct nb_config *config) | |
324 | { | |
325 | struct nb_config *dup; | |
326 | ||
327 | dup = XCALLOC(MTYPE_NB_CONFIG, sizeof(*dup)); | |
328 | dup->dnode = yang_dnode_dup(config->dnode); | |
329 | dup->version = config->version; | |
330 | ||
331 | return dup; | |
332 | } | |
333 | ||
334 | int nb_config_merge(struct nb_config *config_dst, struct nb_config *config_src, | |
335 | bool preserve_source) | |
336 | { | |
337 | int ret; | |
338 | ||
44cd2b2c | 339 | ret = lyd_merge_siblings(&config_dst->dnode, config_src->dnode, 0); |
1c2facd1 RW |
340 | if (ret != 0) |
341 | flog_warn(EC_LIB_LIBYANG, "%s: lyd_merge() failed", __func__); | |
342 | ||
343 | if (!preserve_source) | |
344 | nb_config_free(config_src); | |
345 | ||
346 | return (ret == 0) ? NB_OK : NB_ERR; | |
347 | } | |
348 | ||
349 | void nb_config_replace(struct nb_config *config_dst, | |
350 | struct nb_config *config_src, bool preserve_source) | |
351 | { | |
352 | /* Update version. */ | |
353 | if (config_src->version != 0) | |
354 | config_dst->version = config_src->version; | |
355 | ||
356 | /* Update dnode. */ | |
e5dc8a44 RW |
357 | if (config_dst->dnode) |
358 | yang_dnode_free(config_dst->dnode); | |
1c2facd1 RW |
359 | if (preserve_source) { |
360 | config_dst->dnode = yang_dnode_dup(config_src->dnode); | |
361 | } else { | |
362 | config_dst->dnode = config_src->dnode; | |
363 | config_src->dnode = NULL; | |
364 | nb_config_free(config_src); | |
365 | } | |
366 | } | |
367 | ||
368 | /* Generate the nb_config_cbs tree. */ | |
369 | static inline int nb_config_cb_compare(const struct nb_config_cb *a, | |
370 | const struct nb_config_cb *b) | |
371 | { | |
372 | /* Sort by priority first. */ | |
373 | if (a->nb_node->priority < b->nb_node->priority) | |
374 | return -1; | |
375 | if (a->nb_node->priority > b->nb_node->priority) | |
376 | return 1; | |
377 | ||
378 | /* | |
6b5d6e2d | 379 | * Preserve the order of the configuration changes as told by libyang. |
1c2facd1 | 380 | */ |
fe3f2c61 RW |
381 | if (a->seq < b->seq) |
382 | return -1; | |
383 | if (a->seq > b->seq) | |
384 | return 1; | |
385 | ||
386 | /* | |
387 | * All 'apply_finish' callbacks have their sequence number set to zero. | |
388 | * In this case, compare them using their dnode pointers (the order | |
389 | * doesn't matter for callbacks that have the same priority). | |
390 | */ | |
391 | if (a->dnode < b->dnode) | |
392 | return -1; | |
393 | if (a->dnode > b->dnode) | |
394 | return 1; | |
395 | ||
396 | return 0; | |
1c2facd1 RW |
397 | } |
398 | RB_GENERATE(nb_config_cbs, nb_config_cb, entry, nb_config_cb_compare); | |
399 | ||
400 | static void nb_config_diff_add_change(struct nb_config_cbs *changes, | |
401 | enum nb_operation operation, | |
6b5d6e2d | 402 | uint32_t *seq, |
1c2facd1 RW |
403 | const struct lyd_node *dnode) |
404 | { | |
405 | struct nb_config_change *change; | |
406 | ||
f267201b RW |
407 | /* Ignore unimplemented nodes. */ |
408 | if (!dnode->schema->priv) | |
409 | return; | |
410 | ||
1c2facd1 RW |
411 | change = XCALLOC(MTYPE_TMP, sizeof(*change)); |
412 | change->cb.operation = operation; | |
6b5d6e2d RW |
413 | change->cb.seq = *seq; |
414 | *seq = *seq + 1; | |
1c2facd1 | 415 | change->cb.nb_node = dnode->schema->priv; |
1c2facd1 RW |
416 | change->cb.dnode = dnode; |
417 | ||
418 | RB_INSERT(nb_config_cbs, changes, &change->cb); | |
419 | } | |
420 | ||
421 | static void nb_config_diff_del_changes(struct nb_config_cbs *changes) | |
422 | { | |
423 | while (!RB_EMPTY(nb_config_cbs, changes)) { | |
424 | struct nb_config_change *change; | |
425 | ||
426 | change = (struct nb_config_change *)RB_ROOT(nb_config_cbs, | |
427 | changes); | |
428 | RB_REMOVE(nb_config_cbs, changes, &change->cb); | |
429 | XFREE(MTYPE_TMP, change); | |
430 | } | |
431 | } | |
432 | ||
433 | /* | |
434 | * Helper function used when calculating the delta between two different | |
435 | * configurations. Given a new subtree, calculate all new YANG data nodes, | |
436 | * excluding default leafs and leaf-lists. This is a recursive function. | |
437 | */ | |
6b5d6e2d | 438 | static void nb_config_diff_created(const struct lyd_node *dnode, uint32_t *seq, |
cacbffaf | 439 | struct nb_config_cbs *changes) |
1c2facd1 | 440 | { |
cacbffaf | 441 | enum nb_operation operation; |
1c2facd1 RW |
442 | struct lyd_node *child; |
443 | ||
f267201b RW |
444 | /* Ignore unimplemented nodes. */ |
445 | if (!dnode->schema->priv) | |
446 | return; | |
447 | ||
cacbffaf RW |
448 | switch (dnode->schema->nodetype) { |
449 | case LYS_LEAF: | |
450 | case LYS_LEAFLIST: | |
3bb513c3 | 451 | if (lyd_is_default(dnode)) |
cacbffaf | 452 | break; |
1c2facd1 | 453 | |
cacbffaf RW |
454 | if (nb_operation_is_valid(NB_OP_CREATE, dnode->schema)) |
455 | operation = NB_OP_CREATE; | |
456 | else if (nb_operation_is_valid(NB_OP_MODIFY, dnode->schema)) | |
457 | operation = NB_OP_MODIFY; | |
458 | else | |
459 | return; | |
1c2facd1 | 460 | |
6b5d6e2d | 461 | nb_config_diff_add_change(changes, operation, seq, dnode); |
cacbffaf RW |
462 | break; |
463 | case LYS_CONTAINER: | |
464 | case LYS_LIST: | |
465 | if (nb_operation_is_valid(NB_OP_CREATE, dnode->schema)) | |
6b5d6e2d RW |
466 | nb_config_diff_add_change(changes, NB_OP_CREATE, seq, |
467 | dnode); | |
cacbffaf RW |
468 | |
469 | /* Process child nodes recursively. */ | |
3bb513c3 | 470 | LY_LIST_FOR (lyd_child(dnode), child) { |
6b5d6e2d | 471 | nb_config_diff_created(child, seq, changes); |
1c2facd1 | 472 | } |
cacbffaf RW |
473 | break; |
474 | default: | |
475 | break; | |
1c2facd1 RW |
476 | } |
477 | } | |
478 | ||
6b5d6e2d | 479 | static void nb_config_diff_deleted(const struct lyd_node *dnode, uint32_t *seq, |
1912caa2 RW |
480 | struct nb_config_cbs *changes) |
481 | { | |
f267201b RW |
482 | /* Ignore unimplemented nodes. */ |
483 | if (!dnode->schema->priv) | |
484 | return; | |
485 | ||
1912caa2 | 486 | if (nb_operation_is_valid(NB_OP_DESTROY, dnode->schema)) |
6b5d6e2d | 487 | nb_config_diff_add_change(changes, NB_OP_DESTROY, seq, dnode); |
1912caa2 RW |
488 | else if (CHECK_FLAG(dnode->schema->nodetype, LYS_CONTAINER)) { |
489 | struct lyd_node *child; | |
490 | ||
491 | /* | |
492 | * Non-presence containers need special handling since they | |
493 | * don't have "destroy" callbacks. In this case, what we need to | |
494 | * do is to call the "destroy" callbacks of their child nodes | |
495 | * when applicable (i.e. optional nodes). | |
496 | */ | |
3bb513c3 | 497 | LY_LIST_FOR (lyd_child(dnode), child) { |
6b5d6e2d | 498 | nb_config_diff_deleted(child, seq, changes); |
1912caa2 RW |
499 | } |
500 | } | |
501 | } | |
502 | ||
3bb513c3 CH |
503 | static int nb_lyd_diff_get_op(const struct lyd_node *dnode) |
504 | { | |
505 | const struct lyd_meta *meta; | |
506 | LY_LIST_FOR (dnode->meta, meta) { | |
507 | if (strcmp(meta->name, "operation") | |
508 | || strcmp(meta->annotation->module->name, "yang")) | |
509 | continue; | |
510 | return lyd_get_meta_value(meta)[0]; | |
511 | } | |
512 | return 'n'; | |
513 | } | |
514 | ||
fd396924 | 515 | #if 0 /* Used below in nb_config_diff inside normally disabled code */ |
3bb513c3 CH |
516 | static inline void nb_config_diff_dnode_log_path(const char *context, |
517 | const char *path, | |
518 | const struct lyd_node *dnode) | |
519 | { | |
520 | if (dnode->schema->nodetype & LYD_NODE_TERM) | |
521 | zlog_debug("nb_config_diff: %s: %s: %s", context, path, | |
522 | lyd_get_value(dnode)); | |
523 | else | |
524 | zlog_debug("nb_config_diff: %s: %s", context, path); | |
525 | } | |
526 | ||
527 | static inline void nb_config_diff_dnode_log(const char *context, | |
528 | const struct lyd_node *dnode) | |
529 | { | |
530 | if (!dnode) { | |
531 | zlog_debug("nb_config_diff: %s: NULL", context); | |
532 | return; | |
533 | } | |
534 | ||
535 | char *path = lyd_path(dnode, LYD_PATH_STD, NULL, 0); | |
536 | nb_config_diff_dnode_log_path(context, path, dnode); | |
537 | free(path); | |
538 | } | |
fd396924 | 539 | #endif |
3bb513c3 | 540 | |
1c2facd1 RW |
541 | /* Calculate the delta between two different configurations. */ |
542 | static void nb_config_diff(const struct nb_config *config1, | |
543 | const struct nb_config *config2, | |
544 | struct nb_config_cbs *changes) | |
545 | { | |
3bb513c3 CH |
546 | struct lyd_node *diff = NULL; |
547 | const struct lyd_node *root, *dnode; | |
548 | struct lyd_node *target; | |
549 | int op; | |
550 | LY_ERR err; | |
551 | char *path; | |
552 | ||
fd396924 | 553 | #if 0 /* Useful (noisy) when debugging diff code, and for improving later */ |
3bb513c3 CH |
554 | if (DEBUG_MODE_CHECK(&nb_dbg_cbs_config, DEBUG_MODE_ALL)) { |
555 | LY_LIST_FOR(config1->dnode, root) { | |
556 | LYD_TREE_DFS_BEGIN(root, dnode) { | |
557 | nb_config_diff_dnode_log("from", dnode); | |
558 | LYD_TREE_DFS_END(root, dnode); | |
559 | } | |
560 | } | |
561 | LY_LIST_FOR(config2->dnode, root) { | |
562 | LYD_TREE_DFS_BEGIN(root, dnode) { | |
563 | nb_config_diff_dnode_log("to", dnode); | |
564 | LYD_TREE_DFS_END(root, dnode); | |
565 | } | |
566 | } | |
567 | } | |
568 | #endif | |
1c2facd1 | 569 | |
3bb513c3 CH |
570 | err = lyd_diff_siblings(config1->dnode, config2->dnode, |
571 | LYD_DIFF_DEFAULTS, &diff); | |
572 | assert(!err); | |
1c2facd1 | 573 | |
fd396924 CH |
574 | if (diff && DEBUG_MODE_CHECK(&nb_dbg_cbs_config, DEBUG_MODE_ALL)) { |
575 | char *s; | |
576 | ||
577 | if (!lyd_print_mem(&s, diff, LYD_JSON, | |
578 | LYD_PRINT_WITHSIBLINGS | LYD_PRINT_WD_ALL)) { | |
579 | zlog_debug("%s: %s", __func__, s); | |
580 | free(s); | |
581 | } | |
582 | } | |
1c2facd1 | 583 | |
3bb513c3 | 584 | uint32_t seq = 0; |
fd396924 | 585 | |
3bb513c3 CH |
586 | LY_LIST_FOR (diff, root) { |
587 | LYD_TREE_DFS_BEGIN (root, dnode) { | |
588 | op = nb_lyd_diff_get_op(dnode); | |
589 | ||
590 | path = lyd_path(dnode, LYD_PATH_STD, NULL, 0); | |
591 | ||
fd396924 | 592 | #if 0 /* Useful (noisy) when debugging diff code, and for improving later */ |
3bb513c3 CH |
593 | if (DEBUG_MODE_CHECK(&nb_dbg_cbs_config, DEBUG_MODE_ALL)) { |
594 | char context[80]; | |
595 | snprintf(context, sizeof(context), | |
596 | "iterating diff: oper: %c seq: %u", op, seq); | |
597 | nb_config_diff_dnode_log_path(context, path, dnode); | |
598 | } | |
599 | #endif | |
600 | switch (op) { | |
601 | case 'c': /* create */ | |
602 | /* | |
603 | * This is rather inefficient, but when we use | |
604 | * dnode from the diff instead of the | |
605 | * candidate config node we get failures when | |
606 | * looking up default values, etc, based on | |
607 | * the diff tree. | |
608 | */ | |
609 | target = yang_dnode_get(config2->dnode, path); | |
f96c2b6d | 610 | assert(target); |
3bb513c3 CH |
611 | nb_config_diff_created(target, &seq, changes); |
612 | ||
613 | /* Skip rest of sub-tree, move to next sibling | |
614 | */ | |
615 | LYD_TREE_DFS_continue = 1; | |
616 | break; | |
617 | case 'd': /* delete */ | |
618 | target = yang_dnode_get(config1->dnode, path); | |
f96c2b6d | 619 | assert(target); |
3bb513c3 | 620 | nb_config_diff_deleted(target, &seq, changes); |
1c2facd1 | 621 | |
3bb513c3 CH |
622 | /* Skip rest of sub-tree, move to next sibling |
623 | */ | |
624 | LYD_TREE_DFS_continue = 1; | |
625 | break; | |
626 | case 'r': /* replace */ | |
627 | /* either moving an entry or changing a value */ | |
628 | target = yang_dnode_get(config2->dnode, path); | |
629 | assert(target); | |
630 | nb_config_diff_add_change(changes, NB_OP_MODIFY, | |
631 | &seq, target); | |
632 | break; | |
633 | case 'n': /* none */ | |
634 | default: | |
635 | break; | |
636 | } | |
637 | free(path); | |
638 | LYD_TREE_DFS_END(root, dnode); | |
1c2facd1 | 639 | } |
1c2facd1 RW |
640 | } |
641 | ||
fd396924 | 642 | lyd_free_all(diff); |
1c2facd1 RW |
643 | } |
644 | ||
645 | int nb_candidate_edit(struct nb_config *candidate, | |
646 | const struct nb_node *nb_node, | |
647 | enum nb_operation operation, const char *xpath, | |
648 | const struct yang_data *previous, | |
649 | const struct yang_data *data) | |
650 | { | |
f182d8d8 | 651 | struct lyd_node *dnode, *dep_dnode; |
1c2facd1 | 652 | char xpath_edit[XPATH_MAXLEN]; |
f182d8d8 | 653 | char dep_xpath[XPATH_MAXLEN]; |
3bb513c3 | 654 | LY_ERR err; |
1c2facd1 | 655 | |
1c2facd1 RW |
656 | /* Use special notation for leaf-lists (RFC 6020, section 9.13.5). */ |
657 | if (nb_node->snode->nodetype == LYS_LEAFLIST) | |
658 | snprintf(xpath_edit, sizeof(xpath_edit), "%s[.='%s']", xpath, | |
659 | data->value); | |
660 | else | |
661 | strlcpy(xpath_edit, xpath, sizeof(xpath_edit)); | |
662 | ||
663 | switch (operation) { | |
664 | case NB_OP_CREATE: | |
665 | case NB_OP_MODIFY: | |
3bb513c3 CH |
666 | err = lyd_new_path(candidate->dnode, ly_native_ctx, xpath_edit, |
667 | (void *)data->value, LYD_NEW_PATH_UPDATE, | |
668 | &dnode); | |
669 | if (err) { | |
670 | flog_warn(EC_LIB_LIBYANG, | |
671 | "%s: lyd_new_path(%s) failed: %d", __func__, | |
672 | xpath_edit, err); | |
673 | return NB_ERR; | |
674 | } else if (dnode) { | |
fd396924 CH |
675 | /* Create default nodes */ |
676 | LY_ERR err = lyd_new_implicit_tree( | |
677 | dnode, LYD_IMPLICIT_NO_STATE, NULL); | |
678 | if (err) { | |
679 | flog_warn(EC_LIB_LIBYANG, | |
680 | "%s: lyd_new_implicit_all failed: %d", | |
681 | __func__, err); | |
682 | } | |
f182d8d8 IR |
683 | /* |
684 | * create dependency | |
685 | * | |
686 | * dnode returned by the lyd_new_path may be from a | |
687 | * different schema, so we need to update the nb_node | |
688 | */ | |
689 | nb_node = dnode->schema->priv; | |
690 | if (nb_node->dep_cbs.get_dependency_xpath) { | |
691 | nb_node->dep_cbs.get_dependency_xpath( | |
692 | dnode, dep_xpath); | |
693 | ||
3bb513c3 CH |
694 | err = lyd_new_path(candidate->dnode, |
695 | ly_native_ctx, dep_xpath, | |
696 | NULL, LYD_NEW_PATH_UPDATE, | |
697 | &dep_dnode); | |
fd396924 CH |
698 | /* Create default nodes */ |
699 | if (!err) | |
700 | err = lyd_new_implicit_tree( | |
701 | dep_dnode, | |
702 | LYD_IMPLICIT_NO_STATE, NULL); | |
3bb513c3 CH |
703 | if (err) { |
704 | flog_warn( | |
705 | EC_LIB_LIBYANG, | |
706 | "%s: lyd_new_path(%s) failed: %d", | |
707 | __func__, dep_xpath, err); | |
f182d8d8 IR |
708 | return NB_ERR; |
709 | } | |
710 | } | |
1c2facd1 | 711 | } |
1c2facd1 | 712 | break; |
95ce849b | 713 | case NB_OP_DESTROY: |
1c2facd1 RW |
714 | dnode = yang_dnode_get(candidate->dnode, xpath_edit); |
715 | if (!dnode) | |
716 | /* | |
717 | * Return a special error code so the caller can choose | |
718 | * whether to ignore it or not. | |
719 | */ | |
720 | return NB_ERR_NOT_FOUND; | |
f182d8d8 IR |
721 | /* destroy dependant */ |
722 | if (nb_node->dep_cbs.get_dependant_xpath) { | |
723 | nb_node->dep_cbs.get_dependant_xpath(dnode, dep_xpath); | |
724 | ||
725 | dep_dnode = yang_dnode_get(candidate->dnode, dep_xpath); | |
726 | if (dep_dnode) | |
3bb513c3 | 727 | lyd_free_tree(dep_dnode); |
f182d8d8 | 728 | } |
3bb513c3 | 729 | lyd_free_tree(dnode); |
1c2facd1 RW |
730 | break; |
731 | case NB_OP_MOVE: | |
732 | /* TODO: update configuration. */ | |
733 | break; | |
734 | default: | |
735 | flog_warn(EC_LIB_DEVELOPMENT, | |
736 | "%s: unknown operation (%u) [xpath %s]", __func__, | |
737 | operation, xpath_edit); | |
738 | return NB_ERR; | |
739 | } | |
740 | ||
741 | return NB_OK; | |
742 | } | |
743 | ||
744 | bool nb_candidate_needs_update(const struct nb_config *candidate) | |
745 | { | |
8685be73 RW |
746 | if (candidate->version < running_config->version) |
747 | return true; | |
1c2facd1 | 748 | |
8685be73 | 749 | return false; |
1c2facd1 RW |
750 | } |
751 | ||
752 | int nb_candidate_update(struct nb_config *candidate) | |
753 | { | |
754 | struct nb_config *updated_config; | |
755 | ||
8685be73 | 756 | updated_config = nb_config_dup(running_config); |
1c2facd1 RW |
757 | if (nb_config_merge(updated_config, candidate, true) != NB_OK) |
758 | return NB_ERR; | |
759 | ||
760 | nb_config_replace(candidate, updated_config, false); | |
761 | ||
762 | return NB_OK; | |
763 | } | |
764 | ||
1c2facd1 RW |
765 | /* |
766 | * Perform YANG syntactic and semantic validation. | |
767 | * | |
768 | * WARNING: lyd_validate() can change the configuration as part of the | |
769 | * validation process. | |
770 | */ | |
df5eda3d RW |
771 | static int nb_candidate_validate_yang(struct nb_config *candidate, char *errmsg, |
772 | size_t errmsg_len) | |
1c2facd1 | 773 | { |
3bb513c3 CH |
774 | if (lyd_validate_all(&candidate->dnode, ly_native_ctx, |
775 | LYD_VALIDATE_NO_STATE, NULL) | |
df5eda3d RW |
776 | != 0) { |
777 | yang_print_errors(ly_native_ctx, errmsg, errmsg_len); | |
1c2facd1 | 778 | return NB_ERR_VALIDATION; |
df5eda3d | 779 | } |
1c2facd1 RW |
780 | |
781 | return NB_OK; | |
782 | } | |
783 | ||
784 | /* Perform code-level validation using the northbound callbacks. */ | |
13d6b9c1 RW |
785 | static int nb_candidate_validate_code(struct nb_context *context, |
786 | struct nb_config *candidate, | |
df5eda3d RW |
787 | struct nb_config_cbs *changes, |
788 | char *errmsg, size_t errmsg_len) | |
1c2facd1 RW |
789 | { |
790 | struct nb_config_cb *cb; | |
3bb513c3 | 791 | struct lyd_node *root, *child; |
34224f0c RW |
792 | int ret; |
793 | ||
794 | /* First validate the candidate as a whole. */ | |
3bb513c3 CH |
795 | LY_LIST_FOR (candidate->dnode, root) { |
796 | LYD_TREE_DFS_BEGIN (root, child) { | |
34224f0c RW |
797 | struct nb_node *nb_node; |
798 | ||
799 | nb_node = child->schema->priv; | |
f267201b | 800 | if (!nb_node || !nb_node->cbs.pre_validate) |
34224f0c RW |
801 | goto next; |
802 | ||
df5eda3d RW |
803 | ret = nb_callback_pre_validate(context, nb_node, child, |
804 | errmsg, errmsg_len); | |
34224f0c RW |
805 | if (ret != NB_OK) |
806 | return NB_ERR_VALIDATION; | |
807 | ||
808 | next: | |
3bb513c3 | 809 | LYD_TREE_DFS_END(root, child); |
34224f0c RW |
810 | } |
811 | } | |
812 | ||
813 | /* Now validate the configuration changes. */ | |
1c2facd1 RW |
814 | RB_FOREACH (cb, nb_config_cbs, changes) { |
815 | struct nb_config_change *change = (struct nb_config_change *)cb; | |
1c2facd1 | 816 | |
df5eda3d RW |
817 | ret = nb_callback_configuration(context, NB_EV_VALIDATE, change, |
818 | errmsg, errmsg_len); | |
1c2facd1 RW |
819 | if (ret != NB_OK) |
820 | return NB_ERR_VALIDATION; | |
821 | } | |
822 | ||
823 | return NB_OK; | |
824 | } | |
825 | ||
13d6b9c1 | 826 | int nb_candidate_validate(struct nb_context *context, |
df5eda3d RW |
827 | struct nb_config *candidate, char *errmsg, |
828 | size_t errmsg_len) | |
1c2facd1 RW |
829 | { |
830 | struct nb_config_cbs changes; | |
831 | int ret; | |
832 | ||
df5eda3d RW |
833 | if (nb_candidate_validate_yang(candidate, errmsg, sizeof(errmsg_len)) |
834 | != NB_OK) | |
1c2facd1 RW |
835 | return NB_ERR_VALIDATION; |
836 | ||
837 | RB_INIT(nb_config_cbs, &changes); | |
8685be73 | 838 | nb_config_diff(running_config, candidate, &changes); |
df5eda3d RW |
839 | ret = nb_candidate_validate_code(context, candidate, &changes, errmsg, |
840 | errmsg_len); | |
8685be73 | 841 | nb_config_diff_del_changes(&changes); |
1c2facd1 RW |
842 | |
843 | return ret; | |
844 | } | |
845 | ||
13d6b9c1 RW |
846 | int nb_candidate_commit_prepare(struct nb_context *context, |
847 | struct nb_config *candidate, | |
364ad673 | 848 | const char *comment, |
df5eda3d RW |
849 | struct nb_transaction **transaction, |
850 | char *errmsg, size_t errmsg_len) | |
1c2facd1 RW |
851 | { |
852 | struct nb_config_cbs changes; | |
853 | ||
df5eda3d RW |
854 | if (nb_candidate_validate_yang(candidate, errmsg, errmsg_len) |
855 | != NB_OK) { | |
1c2facd1 RW |
856 | flog_warn(EC_LIB_NB_CANDIDATE_INVALID, |
857 | "%s: failed to validate candidate configuration", | |
858 | __func__); | |
859 | return NB_ERR_VALIDATION; | |
860 | } | |
861 | ||
862 | RB_INIT(nb_config_cbs, &changes); | |
8685be73 | 863 | nb_config_diff(running_config, candidate, &changes); |
5bfb669b QY |
864 | if (RB_EMPTY(nb_config_cbs, &changes)) { |
865 | snprintf( | |
866 | errmsg, errmsg_len, | |
867 | "No changes to apply were found during preparation phase"); | |
8685be73 | 868 | return NB_ERR_NO_CHANGES; |
5bfb669b | 869 | } |
1c2facd1 | 870 | |
df5eda3d RW |
871 | if (nb_candidate_validate_code(context, candidate, &changes, errmsg, |
872 | errmsg_len) | |
873 | != NB_OK) { | |
8685be73 RW |
874 | flog_warn(EC_LIB_NB_CANDIDATE_INVALID, |
875 | "%s: failed to validate candidate configuration", | |
876 | __func__); | |
877 | nb_config_diff_del_changes(&changes); | |
878 | return NB_ERR_VALIDATION; | |
879 | } | |
1c2facd1 | 880 | |
df5eda3d RW |
881 | *transaction = nb_transaction_new(context, candidate, &changes, comment, |
882 | errmsg, errmsg_len); | |
8685be73 RW |
883 | if (*transaction == NULL) { |
884 | flog_warn(EC_LIB_NB_TRANSACTION_CREATION_FAILED, | |
df5eda3d RW |
885 | "%s: failed to create transaction: %s", __func__, |
886 | errmsg); | |
8685be73 RW |
887 | nb_config_diff_del_changes(&changes); |
888 | return NB_ERR_LOCKED; | |
1c2facd1 RW |
889 | } |
890 | ||
df5eda3d RW |
891 | return nb_transaction_process(NB_EV_PREPARE, *transaction, errmsg, |
892 | errmsg_len); | |
1c2facd1 RW |
893 | } |
894 | ||
0fe5b904 RW |
895 | void nb_candidate_commit_abort(struct nb_transaction *transaction, char *errmsg, |
896 | size_t errmsg_len) | |
1c2facd1 | 897 | { |
df5eda3d | 898 | (void)nb_transaction_process(NB_EV_ABORT, transaction, errmsg, |
0fe5b904 | 899 | errmsg_len); |
1c2facd1 RW |
900 | nb_transaction_free(transaction); |
901 | } | |
902 | ||
903 | void nb_candidate_commit_apply(struct nb_transaction *transaction, | |
0fe5b904 RW |
904 | bool save_transaction, uint32_t *transaction_id, |
905 | char *errmsg, size_t errmsg_len) | |
1c2facd1 | 906 | { |
df5eda3d | 907 | (void)nb_transaction_process(NB_EV_APPLY, transaction, errmsg, |
0fe5b904 RW |
908 | errmsg_len); |
909 | nb_transaction_apply_finish(transaction, errmsg, errmsg_len); | |
1c2facd1 RW |
910 | |
911 | /* Replace running by candidate. */ | |
912 | transaction->config->version++; | |
8685be73 | 913 | nb_config_replace(running_config, transaction->config, true); |
1c2facd1 RW |
914 | |
915 | /* Record transaction. */ | |
390a8862 | 916 | if (save_transaction && nb_db_enabled |
1c2facd1 RW |
917 | && nb_db_transaction_save(transaction, transaction_id) != NB_OK) |
918 | flog_warn(EC_LIB_NB_TRANSACTION_RECORD_FAILED, | |
919 | "%s: failed to record transaction", __func__); | |
920 | ||
921 | nb_transaction_free(transaction); | |
922 | } | |
923 | ||
13d6b9c1 RW |
924 | int nb_candidate_commit(struct nb_context *context, struct nb_config *candidate, |
925 | bool save_transaction, const char *comment, | |
df5eda3d RW |
926 | uint32_t *transaction_id, char *errmsg, |
927 | size_t errmsg_len) | |
1c2facd1 RW |
928 | { |
929 | struct nb_transaction *transaction = NULL; | |
930 | int ret; | |
931 | ||
13d6b9c1 | 932 | ret = nb_candidate_commit_prepare(context, candidate, comment, |
df5eda3d | 933 | &transaction, errmsg, errmsg_len); |
1c2facd1 RW |
934 | /* |
935 | * Apply the changes if the preparation phase succeeded. Otherwise abort | |
936 | * the transaction. | |
937 | */ | |
938 | if (ret == NB_OK) | |
939 | nb_candidate_commit_apply(transaction, save_transaction, | |
0fe5b904 | 940 | transaction_id, errmsg, errmsg_len); |
1c2facd1 | 941 | else if (transaction != NULL) |
0fe5b904 | 942 | nb_candidate_commit_abort(transaction, errmsg, errmsg_len); |
1c2facd1 RW |
943 | |
944 | return ret; | |
945 | } | |
946 | ||
364ad673 RW |
947 | int nb_running_lock(enum nb_client client, const void *user) |
948 | { | |
949 | int ret = -1; | |
950 | ||
1be4decb | 951 | frr_with_mutex (&running_config_mgmt_lock.mtx) { |
364ad673 RW |
952 | if (!running_config_mgmt_lock.locked) { |
953 | running_config_mgmt_lock.locked = true; | |
954 | running_config_mgmt_lock.owner_client = client; | |
955 | running_config_mgmt_lock.owner_user = user; | |
956 | ret = 0; | |
957 | } | |
958 | } | |
364ad673 RW |
959 | |
960 | return ret; | |
961 | } | |
962 | ||
963 | int nb_running_unlock(enum nb_client client, const void *user) | |
964 | { | |
965 | int ret = -1; | |
966 | ||
1be4decb | 967 | frr_with_mutex (&running_config_mgmt_lock.mtx) { |
364ad673 RW |
968 | if (running_config_mgmt_lock.locked |
969 | && running_config_mgmt_lock.owner_client == client | |
970 | && running_config_mgmt_lock.owner_user == user) { | |
971 | running_config_mgmt_lock.locked = false; | |
972 | running_config_mgmt_lock.owner_client = NB_CLIENT_NONE; | |
973 | running_config_mgmt_lock.owner_user = NULL; | |
974 | ret = 0; | |
975 | } | |
976 | } | |
364ad673 RW |
977 | |
978 | return ret; | |
979 | } | |
980 | ||
981 | int nb_running_lock_check(enum nb_client client, const void *user) | |
982 | { | |
983 | int ret = -1; | |
984 | ||
1be4decb | 985 | frr_with_mutex (&running_config_mgmt_lock.mtx) { |
364ad673 RW |
986 | if (!running_config_mgmt_lock.locked |
987 | || (running_config_mgmt_lock.owner_client == client | |
988 | && running_config_mgmt_lock.owner_user == user)) | |
989 | ret = 0; | |
990 | } | |
364ad673 RW |
991 | |
992 | return ret; | |
993 | } | |
994 | ||
97cd8493 RW |
995 | static void nb_log_config_callback(const enum nb_event event, |
996 | enum nb_operation operation, | |
997 | const struct lyd_node *dnode) | |
1c2facd1 | 998 | { |
97cd8493 RW |
999 | const char *value; |
1000 | char xpath[XPATH_MAXLEN]; | |
1001 | ||
1002 | if (!DEBUG_MODE_CHECK(&nb_dbg_cbs_config, DEBUG_MODE_ALL)) | |
1003 | return; | |
1004 | ||
1005 | yang_dnode_get_path(dnode, xpath, sizeof(xpath)); | |
1006 | if (yang_snode_is_typeless_data(dnode->schema)) | |
1007 | value = "(none)"; | |
1008 | else | |
1009 | value = yang_dnode_get_string(dnode, NULL); | |
1010 | ||
1c2facd1 RW |
1011 | zlog_debug( |
1012 | "northbound callback: event [%s] op [%s] xpath [%s] value [%s]", | |
1013 | nb_event_name(event), nb_operation_name(operation), xpath, | |
97cd8493 RW |
1014 | value); |
1015 | } | |
1016 | ||
13d6b9c1 RW |
1017 | static int nb_callback_create(struct nb_context *context, |
1018 | const struct nb_node *nb_node, | |
97cd8493 | 1019 | enum nb_event event, const struct lyd_node *dnode, |
df5eda3d RW |
1020 | union nb_resource *resource, char *errmsg, |
1021 | size_t errmsg_len) | |
97cd8493 | 1022 | { |
60ee8be1 | 1023 | struct nb_cb_create_args args = {}; |
1abe6c53 RW |
1024 | bool unexpected_error = false; |
1025 | int ret; | |
60ee8be1 | 1026 | |
97cd8493 RW |
1027 | nb_log_config_callback(event, NB_OP_CREATE, dnode); |
1028 | ||
13d6b9c1 | 1029 | args.context = context; |
60ee8be1 RW |
1030 | args.event = event; |
1031 | args.dnode = dnode; | |
1032 | args.resource = resource; | |
df5eda3d RW |
1033 | args.errmsg = errmsg; |
1034 | args.errmsg_len = errmsg_len; | |
1abe6c53 RW |
1035 | ret = nb_node->cbs.create(&args); |
1036 | ||
1037 | /* Detect and log unexpected errors. */ | |
1038 | switch (ret) { | |
1039 | case NB_OK: | |
1040 | case NB_ERR: | |
1041 | break; | |
1042 | case NB_ERR_VALIDATION: | |
1043 | if (event != NB_EV_VALIDATE) | |
1044 | unexpected_error = true; | |
1045 | break; | |
1046 | case NB_ERR_RESOURCE: | |
1047 | if (event != NB_EV_PREPARE) | |
1048 | unexpected_error = true; | |
1049 | break; | |
1050 | case NB_ERR_INCONSISTENCY: | |
1051 | if (event == NB_EV_VALIDATE) | |
1052 | unexpected_error = true; | |
1053 | break; | |
1054 | default: | |
1055 | unexpected_error = true; | |
1056 | break; | |
1057 | } | |
1058 | if (unexpected_error) | |
1059 | DEBUGD(&nb_dbg_cbs_config, | |
1060 | "northbound callback: unexpected return value: %s", | |
1061 | nb_err_name(ret)); | |
1062 | ||
1063 | return ret; | |
97cd8493 RW |
1064 | } |
1065 | ||
13d6b9c1 RW |
1066 | static int nb_callback_modify(struct nb_context *context, |
1067 | const struct nb_node *nb_node, | |
97cd8493 | 1068 | enum nb_event event, const struct lyd_node *dnode, |
df5eda3d RW |
1069 | union nb_resource *resource, char *errmsg, |
1070 | size_t errmsg_len) | |
97cd8493 | 1071 | { |
60ee8be1 | 1072 | struct nb_cb_modify_args args = {}; |
1abe6c53 RW |
1073 | bool unexpected_error = false; |
1074 | int ret; | |
60ee8be1 | 1075 | |
97cd8493 RW |
1076 | nb_log_config_callback(event, NB_OP_MODIFY, dnode); |
1077 | ||
13d6b9c1 | 1078 | args.context = context; |
60ee8be1 RW |
1079 | args.event = event; |
1080 | args.dnode = dnode; | |
1081 | args.resource = resource; | |
df5eda3d RW |
1082 | args.errmsg = errmsg; |
1083 | args.errmsg_len = errmsg_len; | |
1abe6c53 RW |
1084 | ret = nb_node->cbs.modify(&args); |
1085 | ||
1086 | /* Detect and log unexpected errors. */ | |
1087 | switch (ret) { | |
1088 | case NB_OK: | |
1089 | case NB_ERR: | |
1090 | break; | |
1091 | case NB_ERR_VALIDATION: | |
1092 | if (event != NB_EV_VALIDATE) | |
1093 | unexpected_error = true; | |
1094 | break; | |
1095 | case NB_ERR_RESOURCE: | |
1096 | if (event != NB_EV_PREPARE) | |
1097 | unexpected_error = true; | |
1098 | break; | |
1099 | case NB_ERR_INCONSISTENCY: | |
1100 | if (event == NB_EV_VALIDATE) | |
1101 | unexpected_error = true; | |
1102 | break; | |
1103 | default: | |
1104 | unexpected_error = true; | |
1105 | break; | |
1106 | } | |
1107 | if (unexpected_error) | |
1108 | DEBUGD(&nb_dbg_cbs_config, | |
1109 | "northbound callback: unexpected return value: %s", | |
1110 | nb_err_name(ret)); | |
1111 | ||
1112 | return ret; | |
97cd8493 RW |
1113 | } |
1114 | ||
13d6b9c1 RW |
1115 | static int nb_callback_destroy(struct nb_context *context, |
1116 | const struct nb_node *nb_node, | |
97cd8493 | 1117 | enum nb_event event, |
df5eda3d RW |
1118 | const struct lyd_node *dnode, char *errmsg, |
1119 | size_t errmsg_len) | |
97cd8493 | 1120 | { |
60ee8be1 | 1121 | struct nb_cb_destroy_args args = {}; |
1abe6c53 RW |
1122 | bool unexpected_error = false; |
1123 | int ret; | |
60ee8be1 | 1124 | |
97cd8493 RW |
1125 | nb_log_config_callback(event, NB_OP_DESTROY, dnode); |
1126 | ||
13d6b9c1 | 1127 | args.context = context; |
60ee8be1 RW |
1128 | args.event = event; |
1129 | args.dnode = dnode; | |
df5eda3d RW |
1130 | args.errmsg = errmsg; |
1131 | args.errmsg_len = errmsg_len; | |
1abe6c53 RW |
1132 | ret = nb_node->cbs.destroy(&args); |
1133 | ||
1134 | /* Detect and log unexpected errors. */ | |
1135 | switch (ret) { | |
1136 | case NB_OK: | |
1137 | case NB_ERR: | |
1138 | break; | |
1139 | case NB_ERR_VALIDATION: | |
1140 | if (event != NB_EV_VALIDATE) | |
1141 | unexpected_error = true; | |
1142 | break; | |
1143 | case NB_ERR_INCONSISTENCY: | |
1144 | if (event == NB_EV_VALIDATE) | |
1145 | unexpected_error = true; | |
1146 | break; | |
1147 | default: | |
1148 | unexpected_error = true; | |
1149 | break; | |
1150 | } | |
1151 | if (unexpected_error) | |
1152 | DEBUGD(&nb_dbg_cbs_config, | |
1153 | "northbound callback: unexpected return value: %s", | |
1154 | nb_err_name(ret)); | |
1155 | ||
1156 | return ret; | |
97cd8493 RW |
1157 | } |
1158 | ||
13d6b9c1 RW |
1159 | static int nb_callback_move(struct nb_context *context, |
1160 | const struct nb_node *nb_node, enum nb_event event, | |
df5eda3d RW |
1161 | const struct lyd_node *dnode, char *errmsg, |
1162 | size_t errmsg_len) | |
97cd8493 | 1163 | { |
60ee8be1 | 1164 | struct nb_cb_move_args args = {}; |
1abe6c53 RW |
1165 | bool unexpected_error = false; |
1166 | int ret; | |
60ee8be1 | 1167 | |
97cd8493 RW |
1168 | nb_log_config_callback(event, NB_OP_MOVE, dnode); |
1169 | ||
13d6b9c1 | 1170 | args.context = context; |
60ee8be1 RW |
1171 | args.event = event; |
1172 | args.dnode = dnode; | |
df5eda3d RW |
1173 | args.errmsg = errmsg; |
1174 | args.errmsg_len = errmsg_len; | |
1abe6c53 RW |
1175 | ret = nb_node->cbs.move(&args); |
1176 | ||
1177 | /* Detect and log unexpected errors. */ | |
1178 | switch (ret) { | |
1179 | case NB_OK: | |
1180 | case NB_ERR: | |
1181 | break; | |
1182 | case NB_ERR_VALIDATION: | |
1183 | if (event != NB_EV_VALIDATE) | |
1184 | unexpected_error = true; | |
1185 | break; | |
1186 | case NB_ERR_INCONSISTENCY: | |
1187 | if (event == NB_EV_VALIDATE) | |
1188 | unexpected_error = true; | |
1189 | break; | |
1190 | default: | |
1191 | unexpected_error = true; | |
1192 | break; | |
1193 | } | |
1194 | if (unexpected_error) | |
1195 | DEBUGD(&nb_dbg_cbs_config, | |
1196 | "northbound callback: unexpected return value: %s", | |
1197 | nb_err_name(ret)); | |
1198 | ||
1199 | return ret; | |
97cd8493 RW |
1200 | } |
1201 | ||
13d6b9c1 RW |
1202 | static int nb_callback_pre_validate(struct nb_context *context, |
1203 | const struct nb_node *nb_node, | |
df5eda3d RW |
1204 | const struct lyd_node *dnode, char *errmsg, |
1205 | size_t errmsg_len) | |
97cd8493 | 1206 | { |
60ee8be1 | 1207 | struct nb_cb_pre_validate_args args = {}; |
1abe6c53 RW |
1208 | bool unexpected_error = false; |
1209 | int ret; | |
60ee8be1 | 1210 | |
97cd8493 RW |
1211 | nb_log_config_callback(NB_EV_VALIDATE, NB_OP_PRE_VALIDATE, dnode); |
1212 | ||
60ee8be1 | 1213 | args.dnode = dnode; |
df5eda3d RW |
1214 | args.errmsg = errmsg; |
1215 | args.errmsg_len = errmsg_len; | |
1abe6c53 RW |
1216 | ret = nb_node->cbs.pre_validate(&args); |
1217 | ||
1218 | /* Detect and log unexpected errors. */ | |
1219 | switch (ret) { | |
1220 | case NB_OK: | |
1221 | case NB_ERR_VALIDATION: | |
1222 | break; | |
1223 | default: | |
1224 | unexpected_error = true; | |
1225 | break; | |
1226 | } | |
1227 | if (unexpected_error) | |
1228 | DEBUGD(&nb_dbg_cbs_config, | |
1229 | "northbound callback: unexpected return value: %s", | |
1230 | nb_err_name(ret)); | |
1231 | ||
1232 | return ret; | |
97cd8493 RW |
1233 | } |
1234 | ||
13d6b9c1 RW |
1235 | static void nb_callback_apply_finish(struct nb_context *context, |
1236 | const struct nb_node *nb_node, | |
df5eda3d RW |
1237 | const struct lyd_node *dnode, char *errmsg, |
1238 | size_t errmsg_len) | |
97cd8493 | 1239 | { |
60ee8be1 RW |
1240 | struct nb_cb_apply_finish_args args = {}; |
1241 | ||
97cd8493 RW |
1242 | nb_log_config_callback(NB_EV_APPLY, NB_OP_APPLY_FINISH, dnode); |
1243 | ||
13d6b9c1 | 1244 | args.context = context; |
60ee8be1 | 1245 | args.dnode = dnode; |
df5eda3d RW |
1246 | args.errmsg = errmsg; |
1247 | args.errmsg_len = errmsg_len; | |
60ee8be1 | 1248 | nb_node->cbs.apply_finish(&args); |
97cd8493 RW |
1249 | } |
1250 | ||
1251 | struct yang_data *nb_callback_get_elem(const struct nb_node *nb_node, | |
1252 | const char *xpath, | |
1253 | const void *list_entry) | |
1254 | { | |
60ee8be1 RW |
1255 | struct nb_cb_get_elem_args args = {}; |
1256 | ||
97cd8493 RW |
1257 | DEBUGD(&nb_dbg_cbs_state, |
1258 | "northbound callback (get_elem): xpath [%s] list_entry [%p]", | |
1259 | xpath, list_entry); | |
1260 | ||
60ee8be1 RW |
1261 | args.xpath = xpath; |
1262 | args.list_entry = list_entry; | |
1263 | return nb_node->cbs.get_elem(&args); | |
97cd8493 RW |
1264 | } |
1265 | ||
1266 | const void *nb_callback_get_next(const struct nb_node *nb_node, | |
1267 | const void *parent_list_entry, | |
1268 | const void *list_entry) | |
1269 | { | |
60ee8be1 RW |
1270 | struct nb_cb_get_next_args args = {}; |
1271 | ||
97cd8493 RW |
1272 | DEBUGD(&nb_dbg_cbs_state, |
1273 | "northbound callback (get_next): node [%s] parent_list_entry [%p] list_entry [%p]", | |
1274 | nb_node->xpath, parent_list_entry, list_entry); | |
1275 | ||
60ee8be1 RW |
1276 | args.parent_list_entry = parent_list_entry; |
1277 | args.list_entry = list_entry; | |
1278 | return nb_node->cbs.get_next(&args); | |
97cd8493 RW |
1279 | } |
1280 | ||
1281 | int nb_callback_get_keys(const struct nb_node *nb_node, const void *list_entry, | |
1282 | struct yang_list_keys *keys) | |
1283 | { | |
60ee8be1 RW |
1284 | struct nb_cb_get_keys_args args = {}; |
1285 | ||
97cd8493 RW |
1286 | DEBUGD(&nb_dbg_cbs_state, |
1287 | "northbound callback (get_keys): node [%s] list_entry [%p]", | |
1288 | nb_node->xpath, list_entry); | |
1289 | ||
60ee8be1 RW |
1290 | args.list_entry = list_entry; |
1291 | args.keys = keys; | |
1292 | return nb_node->cbs.get_keys(&args); | |
97cd8493 RW |
1293 | } |
1294 | ||
1295 | const void *nb_callback_lookup_entry(const struct nb_node *nb_node, | |
1296 | const void *parent_list_entry, | |
1297 | const struct yang_list_keys *keys) | |
1298 | { | |
60ee8be1 RW |
1299 | struct nb_cb_lookup_entry_args args = {}; |
1300 | ||
97cd8493 RW |
1301 | DEBUGD(&nb_dbg_cbs_state, |
1302 | "northbound callback (lookup_entry): node [%s] parent_list_entry [%p]", | |
1303 | nb_node->xpath, parent_list_entry); | |
1304 | ||
60ee8be1 RW |
1305 | args.parent_list_entry = parent_list_entry; |
1306 | args.keys = keys; | |
1307 | return nb_node->cbs.lookup_entry(&args); | |
97cd8493 RW |
1308 | } |
1309 | ||
1310 | int nb_callback_rpc(const struct nb_node *nb_node, const char *xpath, | |
f63f5f19 CS |
1311 | const struct list *input, struct list *output, char *errmsg, |
1312 | size_t errmsg_len) | |
97cd8493 | 1313 | { |
60ee8be1 RW |
1314 | struct nb_cb_rpc_args args = {}; |
1315 | ||
97cd8493 RW |
1316 | DEBUGD(&nb_dbg_cbs_rpc, "northbound RPC: %s", xpath); |
1317 | ||
60ee8be1 RW |
1318 | args.xpath = xpath; |
1319 | args.input = input; | |
1320 | args.output = output; | |
f63f5f19 CS |
1321 | args.errmsg = errmsg; |
1322 | args.errmsg_len = errmsg_len; | |
60ee8be1 | 1323 | return nb_node->cbs.rpc(&args); |
1c2facd1 RW |
1324 | } |
1325 | ||
1326 | /* | |
1327 | * Call the northbound configuration callback associated to a given | |
1328 | * configuration change. | |
1329 | */ | |
13d6b9c1 RW |
1330 | static int nb_callback_configuration(struct nb_context *context, |
1331 | const enum nb_event event, | |
df5eda3d RW |
1332 | struct nb_config_change *change, |
1333 | char *errmsg, size_t errmsg_len) | |
1c2facd1 RW |
1334 | { |
1335 | enum nb_operation operation = change->cb.operation; | |
0de19c0e | 1336 | char xpath[XPATH_MAXLEN]; |
1c2facd1 RW |
1337 | const struct nb_node *nb_node = change->cb.nb_node; |
1338 | const struct lyd_node *dnode = change->cb.dnode; | |
1339 | union nb_resource *resource; | |
1340 | int ret = NB_ERR; | |
1341 | ||
1c2facd1 RW |
1342 | if (event == NB_EV_VALIDATE) |
1343 | resource = NULL; | |
1344 | else | |
1345 | resource = &change->resource; | |
1346 | ||
1347 | switch (operation) { | |
1348 | case NB_OP_CREATE: | |
13d6b9c1 | 1349 | ret = nb_callback_create(context, nb_node, event, dnode, |
df5eda3d | 1350 | resource, errmsg, errmsg_len); |
1c2facd1 RW |
1351 | break; |
1352 | case NB_OP_MODIFY: | |
13d6b9c1 | 1353 | ret = nb_callback_modify(context, nb_node, event, dnode, |
df5eda3d | 1354 | resource, errmsg, errmsg_len); |
1c2facd1 | 1355 | break; |
95ce849b | 1356 | case NB_OP_DESTROY: |
df5eda3d RW |
1357 | ret = nb_callback_destroy(context, nb_node, event, dnode, |
1358 | errmsg, errmsg_len); | |
1c2facd1 RW |
1359 | break; |
1360 | case NB_OP_MOVE: | |
df5eda3d RW |
1361 | ret = nb_callback_move(context, nb_node, event, dnode, errmsg, |
1362 | errmsg_len); | |
1c2facd1 RW |
1363 | break; |
1364 | default: | |
0de19c0e | 1365 | yang_dnode_get_path(dnode, xpath, sizeof(xpath)); |
c650e48c RW |
1366 | flog_err(EC_LIB_DEVELOPMENT, |
1367 | "%s: unknown operation (%u) [xpath %s]", __func__, | |
1368 | operation, xpath); | |
1369 | exit(1); | |
1c2facd1 RW |
1370 | } |
1371 | ||
625b70e3 | 1372 | if (ret != NB_OK) { |
0de19c0e RW |
1373 | yang_dnode_get_path(dnode, xpath, sizeof(xpath)); |
1374 | ||
625b70e3 EDP |
1375 | switch (event) { |
1376 | case NB_EV_VALIDATE: | |
1b293b2b DL |
1377 | flog_warn(EC_LIB_NB_CB_CONFIG_VALIDATE, |
1378 | "error processing configuration change: error [%s] event [%s] operation [%s] xpath [%s]%s%s", | |
1379 | nb_err_name(ret), nb_event_name(event), | |
1380 | nb_operation_name(operation), xpath, | |
1381 | errmsg[0] ? " message: " : "", errmsg); | |
625b70e3 EDP |
1382 | break; |
1383 | case NB_EV_PREPARE: | |
1b293b2b DL |
1384 | flog_warn(EC_LIB_NB_CB_CONFIG_PREPARE, |
1385 | "error processing configuration change: error [%s] event [%s] operation [%s] xpath [%s]%s%s", | |
1386 | nb_err_name(ret), nb_event_name(event), | |
1387 | nb_operation_name(operation), xpath, | |
1388 | errmsg[0] ? " message: " : "", errmsg); | |
625b70e3 EDP |
1389 | break; |
1390 | case NB_EV_ABORT: | |
1b293b2b DL |
1391 | flog_warn(EC_LIB_NB_CB_CONFIG_ABORT, |
1392 | "error processing configuration change: error [%s] event [%s] operation [%s] xpath [%s]%s%s", | |
1393 | nb_err_name(ret), nb_event_name(event), | |
1394 | nb_operation_name(operation), xpath, | |
1395 | errmsg[0] ? " message: " : "", errmsg); | |
625b70e3 EDP |
1396 | break; |
1397 | case NB_EV_APPLY: | |
1b293b2b DL |
1398 | flog_err(EC_LIB_NB_CB_CONFIG_APPLY, |
1399 | "error processing configuration change: error [%s] event [%s] operation [%s] xpath [%s]%s%s", | |
1400 | nb_err_name(ret), nb_event_name(event), | |
1401 | nb_operation_name(operation), xpath, | |
1402 | errmsg[0] ? " message: " : "", errmsg); | |
625b70e3 | 1403 | break; |
c650e48c RW |
1404 | default: |
1405 | flog_err(EC_LIB_DEVELOPMENT, | |
1be4decb RW |
1406 | "%s: unknown event (%u) [xpath %s]", __func__, |
1407 | event, xpath); | |
c650e48c | 1408 | exit(1); |
625b70e3 | 1409 | } |
625b70e3 | 1410 | } |
1c2facd1 RW |
1411 | |
1412 | return ret; | |
1413 | } | |
1414 | ||
364ad673 | 1415 | static struct nb_transaction * |
df5eda3d RW |
1416 | nb_transaction_new(struct nb_context *context, struct nb_config *config, |
1417 | struct nb_config_cbs *changes, const char *comment, | |
1418 | char *errmsg, size_t errmsg_len) | |
1c2facd1 RW |
1419 | { |
1420 | struct nb_transaction *transaction; | |
1421 | ||
13d6b9c1 | 1422 | if (nb_running_lock_check(context->client, context->user)) { |
df5eda3d RW |
1423 | strlcpy(errmsg, |
1424 | "running configuration is locked by another client", | |
1425 | errmsg_len); | |
364ad673 RW |
1426 | return NULL; |
1427 | } | |
1428 | ||
1c2facd1 | 1429 | if (transaction_in_progress) { |
df5eda3d RW |
1430 | strlcpy(errmsg, |
1431 | "there's already another transaction in progress", | |
1432 | errmsg_len); | |
1c2facd1 RW |
1433 | return NULL; |
1434 | } | |
1435 | transaction_in_progress = true; | |
1436 | ||
1437 | transaction = XCALLOC(MTYPE_TMP, sizeof(*transaction)); | |
13d6b9c1 | 1438 | transaction->context = context; |
1c2facd1 RW |
1439 | if (comment) |
1440 | strlcpy(transaction->comment, comment, | |
1441 | sizeof(transaction->comment)); | |
1442 | transaction->config = config; | |
1443 | transaction->changes = *changes; | |
1444 | ||
1445 | return transaction; | |
1446 | } | |
1447 | ||
1448 | static void nb_transaction_free(struct nb_transaction *transaction) | |
1449 | { | |
1450 | nb_config_diff_del_changes(&transaction->changes); | |
1451 | XFREE(MTYPE_TMP, transaction); | |
1452 | transaction_in_progress = false; | |
1453 | } | |
1454 | ||
1455 | /* Process all configuration changes associated to a transaction. */ | |
1456 | static int nb_transaction_process(enum nb_event event, | |
df5eda3d RW |
1457 | struct nb_transaction *transaction, |
1458 | char *errmsg, size_t errmsg_len) | |
1c2facd1 RW |
1459 | { |
1460 | struct nb_config_cb *cb; | |
1461 | ||
8685be73 RW |
1462 | RB_FOREACH (cb, nb_config_cbs, &transaction->changes) { |
1463 | struct nb_config_change *change = (struct nb_config_change *)cb; | |
1464 | int ret; | |
1c2facd1 | 1465 | |
8685be73 RW |
1466 | /* |
1467 | * Only try to release resources that were allocated | |
1468 | * successfully. | |
1469 | */ | |
a8f58eb6 | 1470 | if (event == NB_EV_ABORT && !change->prepare_ok) |
8685be73 RW |
1471 | break; |
1472 | ||
1473 | /* Call the appropriate callback. */ | |
13d6b9c1 | 1474 | ret = nb_callback_configuration(transaction->context, event, |
df5eda3d | 1475 | change, errmsg, errmsg_len); |
8685be73 RW |
1476 | switch (event) { |
1477 | case NB_EV_PREPARE: | |
1478 | if (ret != NB_OK) | |
1479 | return ret; | |
1480 | change->prepare_ok = true; | |
1481 | break; | |
1482 | case NB_EV_ABORT: | |
1483 | case NB_EV_APPLY: | |
1c2facd1 | 1484 | /* |
8685be73 RW |
1485 | * At this point it's not possible to reject the |
1486 | * transaction anymore, so any failure here can lead to | |
1487 | * inconsistencies and should be treated as a bug. | |
1488 | * Operations prone to errors, like validations and | |
1489 | * resource allocations, should be performed during the | |
1490 | * 'prepare' phase. | |
1c2facd1 | 1491 | */ |
8685be73 RW |
1492 | break; |
1493 | default: | |
1494 | break; | |
1c2facd1 RW |
1495 | } |
1496 | } | |
1497 | ||
1498 | return NB_OK; | |
1499 | } | |
1500 | ||
1501 | static struct nb_config_cb * | |
0de19c0e | 1502 | nb_apply_finish_cb_new(struct nb_config_cbs *cbs, const struct nb_node *nb_node, |
1c2facd1 RW |
1503 | const struct lyd_node *dnode) |
1504 | { | |
1505 | struct nb_config_cb *cb; | |
1506 | ||
1507 | cb = XCALLOC(MTYPE_TMP, sizeof(*cb)); | |
1c2facd1 RW |
1508 | cb->nb_node = nb_node; |
1509 | cb->dnode = dnode; | |
1510 | RB_INSERT(nb_config_cbs, cbs, cb); | |
1511 | ||
1512 | return cb; | |
1513 | } | |
1514 | ||
1515 | static struct nb_config_cb * | |
fe3f2c61 RW |
1516 | nb_apply_finish_cb_find(struct nb_config_cbs *cbs, |
1517 | const struct nb_node *nb_node, | |
1518 | const struct lyd_node *dnode) | |
1c2facd1 RW |
1519 | { |
1520 | struct nb_config_cb s; | |
1521 | ||
fe3f2c61 | 1522 | s.seq = 0; |
1c2facd1 | 1523 | s.nb_node = nb_node; |
fe3f2c61 | 1524 | s.dnode = dnode; |
1c2facd1 RW |
1525 | return RB_FIND(nb_config_cbs, cbs, &s); |
1526 | } | |
1527 | ||
1528 | /* Call the 'apply_finish' callbacks. */ | |
df5eda3d RW |
1529 | static void nb_transaction_apply_finish(struct nb_transaction *transaction, |
1530 | char *errmsg, size_t errmsg_len) | |
1c2facd1 RW |
1531 | { |
1532 | struct nb_config_cbs cbs; | |
1533 | struct nb_config_cb *cb; | |
1534 | ||
1535 | /* Initialize tree of 'apply_finish' callbacks. */ | |
1536 | RB_INIT(nb_config_cbs, &cbs); | |
1537 | ||
1538 | /* Identify the 'apply_finish' callbacks that need to be called. */ | |
1539 | RB_FOREACH (cb, nb_config_cbs, &transaction->changes) { | |
1540 | struct nb_config_change *change = (struct nb_config_change *)cb; | |
1541 | const struct lyd_node *dnode = change->cb.dnode; | |
1542 | ||
1543 | /* | |
1544 | * Iterate up to the root of the data tree. When a node is being | |
1545 | * deleted, skip its 'apply_finish' callback if one is defined | |
1546 | * (the 'apply_finish' callbacks from the node ancestors should | |
1547 | * be called though). | |
1548 | */ | |
95ce849b | 1549 | if (change->cb.operation == NB_OP_DESTROY) { |
97cd8493 RW |
1550 | char xpath[XPATH_MAXLEN]; |
1551 | ||
3bb513c3 | 1552 | dnode = lyd_parent(dnode); |
1c2facd1 RW |
1553 | if (!dnode) |
1554 | break; | |
1555 | ||
1556 | /* | |
1557 | * The dnode from 'delete' callbacks point to elements | |
1558 | * from the running configuration. Use yang_dnode_get() | |
1559 | * to get the corresponding dnode from the candidate | |
1560 | * configuration that is being committed. | |
1561 | */ | |
1562 | yang_dnode_get_path(dnode, xpath, sizeof(xpath)); | |
1563 | dnode = yang_dnode_get(transaction->config->dnode, | |
1564 | xpath); | |
1565 | } | |
1566 | while (dnode) { | |
1c2facd1 RW |
1567 | struct nb_node *nb_node; |
1568 | ||
1569 | nb_node = dnode->schema->priv; | |
f267201b | 1570 | if (!nb_node || !nb_node->cbs.apply_finish) |
1c2facd1 RW |
1571 | goto next; |
1572 | ||
1573 | /* | |
1574 | * Don't call the callback more than once for the same | |
1575 | * data node. | |
1576 | */ | |
fe3f2c61 | 1577 | if (nb_apply_finish_cb_find(&cbs, nb_node, dnode)) |
1c2facd1 RW |
1578 | goto next; |
1579 | ||
0de19c0e | 1580 | nb_apply_finish_cb_new(&cbs, nb_node, dnode); |
1c2facd1 RW |
1581 | |
1582 | next: | |
3bb513c3 | 1583 | dnode = lyd_parent(dnode); |
1c2facd1 RW |
1584 | } |
1585 | } | |
1586 | ||
1587 | /* Call the 'apply_finish' callbacks, sorted by their priorities. */ | |
97cd8493 | 1588 | RB_FOREACH (cb, nb_config_cbs, &cbs) |
13d6b9c1 | 1589 | nb_callback_apply_finish(transaction->context, cb->nb_node, |
df5eda3d | 1590 | cb->dnode, errmsg, errmsg_len); |
1c2facd1 RW |
1591 | |
1592 | /* Release memory. */ | |
1593 | while (!RB_EMPTY(nb_config_cbs, &cbs)) { | |
1594 | cb = RB_ROOT(nb_config_cbs, &cbs); | |
1595 | RB_REMOVE(nb_config_cbs, &cbs, cb); | |
1596 | XFREE(MTYPE_TMP, cb); | |
1597 | } | |
1598 | } | |
1599 | ||
3bb513c3 | 1600 | static int nb_oper_data_iter_children(const struct lysc_node *snode, |
1a4bc045 RW |
1601 | const char *xpath, const void *list_entry, |
1602 | const struct yang_list_keys *list_keys, | |
1603 | struct yang_translator *translator, | |
1604 | bool first, uint32_t flags, | |
1605 | nb_oper_data_cb cb, void *arg) | |
1606 | { | |
3bb513c3 | 1607 | const struct lysc_node *child; |
1a4bc045 | 1608 | |
3bb513c3 | 1609 | LY_LIST_FOR (lysc_node_child(snode), child) { |
1a4bc045 RW |
1610 | int ret; |
1611 | ||
1612 | ret = nb_oper_data_iter_node(child, xpath, list_entry, | |
1613 | list_keys, translator, false, | |
1614 | flags, cb, arg); | |
1615 | if (ret != NB_OK) | |
1616 | return ret; | |
1617 | } | |
1618 | ||
1619 | return NB_OK; | |
1620 | } | |
1621 | ||
1622 | static int nb_oper_data_iter_leaf(const struct nb_node *nb_node, | |
1623 | const char *xpath, const void *list_entry, | |
1624 | const struct yang_list_keys *list_keys, | |
1625 | struct yang_translator *translator, | |
1626 | uint32_t flags, nb_oper_data_cb cb, void *arg) | |
1627 | { | |
1628 | struct yang_data *data; | |
1629 | ||
1630 | if (CHECK_FLAG(nb_node->snode->flags, LYS_CONFIG_W)) | |
1631 | return NB_OK; | |
1632 | ||
1633 | /* Ignore list keys. */ | |
3bb513c3 | 1634 | if (lysc_is_key(nb_node->snode)) |
1a4bc045 RW |
1635 | return NB_OK; |
1636 | ||
9eb2c0a1 | 1637 | data = nb_callback_get_elem(nb_node, xpath, list_entry); |
1a4bc045 RW |
1638 | if (data == NULL) |
1639 | /* Leaf of type "empty" is not present. */ | |
1640 | return NB_OK; | |
1641 | ||
1642 | return (*cb)(nb_node->snode, translator, data, arg); | |
1643 | } | |
1644 | ||
1645 | static int nb_oper_data_iter_container(const struct nb_node *nb_node, | |
1646 | const char *xpath, | |
1647 | const void *list_entry, | |
1648 | const struct yang_list_keys *list_keys, | |
1649 | struct yang_translator *translator, | |
1650 | uint32_t flags, nb_oper_data_cb cb, | |
1651 | void *arg) | |
1652 | { | |
1653 | if (CHECK_FLAG(nb_node->flags, F_NB_NODE_CONFIG_ONLY)) | |
1654 | return NB_OK; | |
1655 | ||
1656 | /* Presence containers. */ | |
1657 | if (nb_node->cbs.get_elem) { | |
1658 | struct yang_data *data; | |
1659 | int ret; | |
1660 | ||
9eb2c0a1 | 1661 | data = nb_callback_get_elem(nb_node, xpath, list_entry); |
1a4bc045 RW |
1662 | if (data == NULL) |
1663 | /* Presence container is not present. */ | |
1664 | return NB_OK; | |
1665 | ||
1666 | ret = (*cb)(nb_node->snode, translator, data, arg); | |
1667 | if (ret != NB_OK) | |
1668 | return ret; | |
1669 | } | |
1670 | ||
1671 | /* Iterate over the child nodes. */ | |
1672 | return nb_oper_data_iter_children(nb_node->snode, xpath, list_entry, | |
1673 | list_keys, translator, false, flags, | |
1674 | cb, arg); | |
1675 | } | |
1676 | ||
1677 | static int | |
1678 | nb_oper_data_iter_leaflist(const struct nb_node *nb_node, const char *xpath, | |
1679 | const void *parent_list_entry, | |
1680 | const struct yang_list_keys *parent_list_keys, | |
1681 | struct yang_translator *translator, uint32_t flags, | |
1682 | nb_oper_data_cb cb, void *arg) | |
1683 | { | |
1684 | const void *list_entry = NULL; | |
1685 | ||
1686 | if (CHECK_FLAG(nb_node->snode->flags, LYS_CONFIG_W)) | |
1687 | return NB_OK; | |
1688 | ||
1689 | do { | |
1690 | struct yang_data *data; | |
1691 | int ret; | |
1692 | ||
9eb2c0a1 RW |
1693 | list_entry = nb_callback_get_next(nb_node, parent_list_entry, |
1694 | list_entry); | |
1a4bc045 RW |
1695 | if (!list_entry) |
1696 | /* End of the list. */ | |
1697 | break; | |
1698 | ||
9eb2c0a1 | 1699 | data = nb_callback_get_elem(nb_node, xpath, list_entry); |
1a4bc045 RW |
1700 | if (data == NULL) |
1701 | continue; | |
1702 | ||
1703 | ret = (*cb)(nb_node->snode, translator, data, arg); | |
1704 | if (ret != NB_OK) | |
1705 | return ret; | |
1706 | } while (list_entry); | |
1707 | ||
1708 | return NB_OK; | |
1709 | } | |
1710 | ||
1711 | static int nb_oper_data_iter_list(const struct nb_node *nb_node, | |
1712 | const char *xpath_list, | |
1713 | const void *parent_list_entry, | |
1714 | const struct yang_list_keys *parent_list_keys, | |
1715 | struct yang_translator *translator, | |
1716 | uint32_t flags, nb_oper_data_cb cb, void *arg) | |
1717 | { | |
3bb513c3 | 1718 | const struct lysc_node *snode = nb_node->snode; |
1a4bc045 | 1719 | const void *list_entry = NULL; |
99fb518f | 1720 | uint32_t position = 1; |
1a4bc045 RW |
1721 | |
1722 | if (CHECK_FLAG(nb_node->flags, F_NB_NODE_CONFIG_ONLY)) | |
1723 | return NB_OK; | |
1724 | ||
1725 | /* Iterate over all list entries. */ | |
1726 | do { | |
3bb513c3 | 1727 | const struct lysc_node_leaf *skey; |
1a4bc045 | 1728 | struct yang_list_keys list_keys; |
f999f11e | 1729 | char xpath[XPATH_MAXLEN * 2]; |
1a4bc045 RW |
1730 | int ret; |
1731 | ||
1732 | /* Obtain list entry. */ | |
9eb2c0a1 RW |
1733 | list_entry = nb_callback_get_next(nb_node, parent_list_entry, |
1734 | list_entry); | |
1a4bc045 RW |
1735 | if (!list_entry) |
1736 | /* End of the list. */ | |
1737 | break; | |
1738 | ||
99fb518f RW |
1739 | if (!CHECK_FLAG(nb_node->flags, F_NB_NODE_KEYLESS_LIST)) { |
1740 | /* Obtain the list entry keys. */ | |
9eb2c0a1 RW |
1741 | if (nb_callback_get_keys(nb_node, list_entry, |
1742 | &list_keys) | |
99fb518f RW |
1743 | != NB_OK) { |
1744 | flog_warn(EC_LIB_NB_CB_STATE, | |
1745 | "%s: failed to get list keys", | |
1746 | __func__); | |
1747 | return NB_ERR; | |
1748 | } | |
1749 | ||
1750 | /* Build XPath of the list entry. */ | |
1751 | strlcpy(xpath, xpath_list, sizeof(xpath)); | |
3bb513c3 CH |
1752 | unsigned int i = 0; |
1753 | LY_FOR_KEYS (snode, skey) { | |
1754 | assert(i < list_keys.num); | |
99fb518f RW |
1755 | snprintf(xpath + strlen(xpath), |
1756 | sizeof(xpath) - strlen(xpath), | |
3bb513c3 | 1757 | "[%s='%s']", skey->name, |
99fb518f | 1758 | list_keys.key[i]); |
3bb513c3 | 1759 | i++; |
99fb518f | 1760 | } |
3bb513c3 | 1761 | assert(i == list_keys.num); |
99fb518f RW |
1762 | } else { |
1763 | /* | |
1764 | * Keyless list - build XPath using a positional index. | |
1765 | */ | |
1766 | snprintf(xpath, sizeof(xpath), "%s[%u]", xpath_list, | |
1767 | position); | |
1768 | position++; | |
1a4bc045 RW |
1769 | } |
1770 | ||
1771 | /* Iterate over the child nodes. */ | |
1772 | ret = nb_oper_data_iter_children( | |
1773 | nb_node->snode, xpath, list_entry, &list_keys, | |
1774 | translator, false, flags, cb, arg); | |
1775 | if (ret != NB_OK) | |
1776 | return ret; | |
1777 | } while (list_entry); | |
1778 | ||
1779 | return NB_OK; | |
1780 | } | |
1781 | ||
3bb513c3 | 1782 | static int nb_oper_data_iter_node(const struct lysc_node *snode, |
1a4bc045 RW |
1783 | const char *xpath_parent, |
1784 | const void *list_entry, | |
1785 | const struct yang_list_keys *list_keys, | |
1786 | struct yang_translator *translator, | |
1787 | bool first, uint32_t flags, | |
1788 | nb_oper_data_cb cb, void *arg) | |
1789 | { | |
1790 | struct nb_node *nb_node; | |
1791 | char xpath[XPATH_MAXLEN]; | |
1792 | int ret = NB_OK; | |
1793 | ||
1794 | if (!first && CHECK_FLAG(flags, NB_OPER_DATA_ITER_NORECURSE) | |
1795 | && CHECK_FLAG(snode->nodetype, LYS_CONTAINER | LYS_LIST)) | |
1796 | return NB_OK; | |
1797 | ||
1798 | /* Update XPath. */ | |
1799 | strlcpy(xpath, xpath_parent, sizeof(xpath)); | |
6cd301e0 | 1800 | if (!first && snode->nodetype != LYS_USES) { |
3bb513c3 | 1801 | struct lysc_node *parent; |
6cd301e0 RW |
1802 | |
1803 | /* Get the real parent. */ | |
1804 | parent = snode->parent; | |
6cd301e0 RW |
1805 | |
1806 | /* | |
1807 | * When necessary, include the namespace of the augmenting | |
1808 | * module. | |
1809 | */ | |
3bb513c3 | 1810 | if (parent && parent->module != snode->module) |
6cd301e0 RW |
1811 | snprintf(xpath + strlen(xpath), |
1812 | sizeof(xpath) - strlen(xpath), "/%s:%s", | |
1813 | snode->module->name, snode->name); | |
1814 | else | |
1815 | snprintf(xpath + strlen(xpath), | |
1816 | sizeof(xpath) - strlen(xpath), "/%s", | |
1817 | snode->name); | |
1818 | } | |
1a4bc045 RW |
1819 | |
1820 | nb_node = snode->priv; | |
1821 | switch (snode->nodetype) { | |
1822 | case LYS_CONTAINER: | |
1823 | ret = nb_oper_data_iter_container(nb_node, xpath, list_entry, | |
1824 | list_keys, translator, flags, | |
1825 | cb, arg); | |
1826 | break; | |
1827 | case LYS_LEAF: | |
1828 | ret = nb_oper_data_iter_leaf(nb_node, xpath, list_entry, | |
1829 | list_keys, translator, flags, cb, | |
1830 | arg); | |
1831 | break; | |
1832 | case LYS_LEAFLIST: | |
1833 | ret = nb_oper_data_iter_leaflist(nb_node, xpath, list_entry, | |
1834 | list_keys, translator, flags, | |
1835 | cb, arg); | |
1836 | break; | |
1837 | case LYS_LIST: | |
1838 | ret = nb_oper_data_iter_list(nb_node, xpath, list_entry, | |
1839 | list_keys, translator, flags, cb, | |
1840 | arg); | |
1841 | break; | |
1842 | case LYS_USES: | |
1843 | ret = nb_oper_data_iter_children(snode, xpath, list_entry, | |
1844 | list_keys, translator, false, | |
1845 | flags, cb, arg); | |
1846 | break; | |
1847 | default: | |
1848 | break; | |
1849 | } | |
1850 | ||
1851 | return ret; | |
1852 | } | |
1853 | ||
1854 | int nb_oper_data_iterate(const char *xpath, struct yang_translator *translator, | |
1855 | uint32_t flags, nb_oper_data_cb cb, void *arg) | |
1856 | { | |
1857 | struct nb_node *nb_node; | |
1858 | const void *list_entry = NULL; | |
1859 | struct yang_list_keys list_keys; | |
1860 | struct list *list_dnodes; | |
1861 | struct lyd_node *dnode, *dn; | |
1862 | struct listnode *ln; | |
1863 | int ret; | |
1864 | ||
1865 | nb_node = nb_node_find(xpath); | |
1866 | if (!nb_node) { | |
1867 | flog_warn(EC_LIB_YANG_UNKNOWN_DATA_PATH, | |
1868 | "%s: unknown data path: %s", __func__, xpath); | |
1869 | return NB_ERR; | |
1870 | } | |
1871 | ||
1872 | /* For now this function works only with containers and lists. */ | |
1873 | if (!CHECK_FLAG(nb_node->snode->nodetype, LYS_CONTAINER | LYS_LIST)) { | |
1874 | flog_warn( | |
1875 | EC_LIB_NB_OPERATIONAL_DATA, | |
1876 | "%s: can't iterate over YANG leaf or leaf-list [xpath %s]", | |
1877 | __func__, xpath); | |
1878 | return NB_ERR; | |
1879 | } | |
1880 | ||
1881 | /* | |
1882 | * Create a data tree from the XPath so that we can parse the keys of | |
1883 | * all YANG lists (if any). | |
1884 | */ | |
3bb513c3 CH |
1885 | |
1886 | LY_ERR err = lyd_new_path(NULL, ly_native_ctx, xpath, NULL, | |
1887 | LYD_NEW_PATH_UPDATE, &dnode); | |
1888 | if (err || !dnode) { | |
1889 | const char *errmsg = | |
1890 | err ? ly_errmsg(ly_native_ctx) : "node not found"; | |
1891 | flog_warn(EC_LIB_LIBYANG, "%s: lyd_new_path() failed %s", | |
1892 | __func__, errmsg); | |
1a4bc045 RW |
1893 | return NB_ERR; |
1894 | } | |
1a4bc045 RW |
1895 | |
1896 | /* | |
1897 | * Create a linked list to sort the data nodes starting from the root. | |
1898 | */ | |
1899 | list_dnodes = list_new(); | |
3bb513c3 CH |
1900 | for (dn = dnode; dn; dn = lyd_parent(dn)) { |
1901 | if (dn->schema->nodetype != LYS_LIST || !lyd_child(dn)) | |
1a4bc045 RW |
1902 | continue; |
1903 | listnode_add_head(list_dnodes, dn); | |
1904 | } | |
1905 | /* | |
1906 | * Use the northbound callbacks to find list entry pointer corresponding | |
1907 | * to the given XPath. | |
1908 | */ | |
1909 | for (ALL_LIST_ELEMENTS_RO(list_dnodes, ln, dn)) { | |
1910 | struct lyd_node *child; | |
1911 | struct nb_node *nn; | |
1912 | unsigned int n = 0; | |
1913 | ||
1914 | /* Obtain the list entry keys. */ | |
1915 | memset(&list_keys, 0, sizeof(list_keys)); | |
3bb513c3 CH |
1916 | LY_LIST_FOR (lyd_child(dn), child) { |
1917 | if (!lysc_is_key(child->schema)) | |
1918 | break; | |
1a4bc045 RW |
1919 | strlcpy(list_keys.key[n], |
1920 | yang_dnode_get_string(child, NULL), | |
1921 | sizeof(list_keys.key[n])); | |
1922 | n++; | |
1923 | } | |
1924 | list_keys.num = n; | |
3bb513c3 | 1925 | if (list_keys.num != yang_snode_num_keys(dn->schema)) { |
9f6de299 RW |
1926 | list_delete(&list_dnodes); |
1927 | yang_dnode_free(dnode); | |
1928 | return NB_ERR_NOT_FOUND; | |
1929 | } | |
1a4bc045 RW |
1930 | |
1931 | /* Find the list entry pointer. */ | |
1932 | nn = dn->schema->priv; | |
baa1d4af IR |
1933 | if (!nn->cbs.lookup_entry) { |
1934 | flog_warn( | |
1935 | EC_LIB_NB_OPERATIONAL_DATA, | |
1936 | "%s: data path doesn't support iteration over operational data: %s", | |
1937 | __func__, xpath); | |
1938 | list_delete(&list_dnodes); | |
1939 | yang_dnode_free(dnode); | |
1940 | return NB_ERR; | |
1941 | } | |
1942 | ||
9eb2c0a1 RW |
1943 | list_entry = |
1944 | nb_callback_lookup_entry(nn, list_entry, &list_keys); | |
1a4bc045 RW |
1945 | if (list_entry == NULL) { |
1946 | list_delete(&list_dnodes); | |
1947 | yang_dnode_free(dnode); | |
1948 | return NB_ERR_NOT_FOUND; | |
1949 | } | |
1950 | } | |
1951 | ||
1952 | /* If a list entry was given, iterate over that list entry only. */ | |
3bb513c3 | 1953 | if (dnode->schema->nodetype == LYS_LIST && lyd_child(dnode)) |
1a4bc045 RW |
1954 | ret = nb_oper_data_iter_children( |
1955 | nb_node->snode, xpath, list_entry, &list_keys, | |
1956 | translator, true, flags, cb, arg); | |
1957 | else | |
1958 | ret = nb_oper_data_iter_node(nb_node->snode, xpath, list_entry, | |
1959 | &list_keys, translator, true, | |
1960 | flags, cb, arg); | |
1961 | ||
1962 | list_delete(&list_dnodes); | |
1963 | yang_dnode_free(dnode); | |
1964 | ||
1965 | return ret; | |
1966 | } | |
1967 | ||
1c2facd1 | 1968 | bool nb_operation_is_valid(enum nb_operation operation, |
3bb513c3 | 1969 | const struct lysc_node *snode) |
1c2facd1 | 1970 | { |
544ca69a | 1971 | struct nb_node *nb_node = snode->priv; |
3bb513c3 CH |
1972 | struct lysc_node_container *scontainer; |
1973 | struct lysc_node_leaf *sleaf; | |
1c2facd1 RW |
1974 | |
1975 | switch (operation) { | |
1976 | case NB_OP_CREATE: | |
db452508 | 1977 | if (!CHECK_FLAG(snode->flags, LYS_CONFIG_W)) |
1c2facd1 RW |
1978 | return false; |
1979 | ||
1980 | switch (snode->nodetype) { | |
1981 | case LYS_LEAF: | |
3bb513c3 CH |
1982 | sleaf = (struct lysc_node_leaf *)snode; |
1983 | if (sleaf->type->basetype != LY_TYPE_EMPTY) | |
1c2facd1 RW |
1984 | return false; |
1985 | break; | |
1986 | case LYS_CONTAINER: | |
3bb513c3 CH |
1987 | scontainer = (struct lysc_node_container *)snode; |
1988 | if (!CHECK_FLAG(scontainer->flags, LYS_PRESENCE)) | |
1c2facd1 RW |
1989 | return false; |
1990 | break; | |
1991 | case LYS_LIST: | |
1992 | case LYS_LEAFLIST: | |
1993 | break; | |
1994 | default: | |
1995 | return false; | |
1996 | } | |
1997 | return true; | |
1998 | case NB_OP_MODIFY: | |
db452508 | 1999 | if (!CHECK_FLAG(snode->flags, LYS_CONFIG_W)) |
1c2facd1 RW |
2000 | return false; |
2001 | ||
2002 | switch (snode->nodetype) { | |
2003 | case LYS_LEAF: | |
3bb513c3 CH |
2004 | sleaf = (struct lysc_node_leaf *)snode; |
2005 | if (sleaf->type->basetype == LY_TYPE_EMPTY) | |
1c2facd1 RW |
2006 | return false; |
2007 | ||
2008 | /* List keys can't be modified. */ | |
3bb513c3 | 2009 | if (lysc_is_key(sleaf)) |
1c2facd1 RW |
2010 | return false; |
2011 | break; | |
2012 | default: | |
2013 | return false; | |
2014 | } | |
2015 | return true; | |
95ce849b | 2016 | case NB_OP_DESTROY: |
db452508 | 2017 | if (!CHECK_FLAG(snode->flags, LYS_CONFIG_W)) |
1c2facd1 RW |
2018 | return false; |
2019 | ||
2020 | switch (snode->nodetype) { | |
2021 | case LYS_LEAF: | |
3bb513c3 | 2022 | sleaf = (struct lysc_node_leaf *)snode; |
1c2facd1 RW |
2023 | |
2024 | /* List keys can't be deleted. */ | |
3bb513c3 | 2025 | if (lysc_is_key(sleaf)) |
1c2facd1 RW |
2026 | return false; |
2027 | ||
2028 | /* | |
2029 | * Only optional leafs can be deleted, or leafs whose | |
2030 | * parent is a case statement. | |
2031 | */ | |
2032 | if (snode->parent->nodetype == LYS_CASE) | |
2033 | return true; | |
2034 | if (sleaf->when) | |
2035 | return true; | |
db452508 RW |
2036 | if (CHECK_FLAG(sleaf->flags, LYS_MAND_TRUE) |
2037 | || sleaf->dflt) | |
1c2facd1 RW |
2038 | return false; |
2039 | break; | |
2040 | case LYS_CONTAINER: | |
3bb513c3 CH |
2041 | scontainer = (struct lysc_node_container *)snode; |
2042 | if (!CHECK_FLAG(scontainer->flags, LYS_PRESENCE)) | |
1c2facd1 RW |
2043 | return false; |
2044 | break; | |
2045 | case LYS_LIST: | |
2046 | case LYS_LEAFLIST: | |
2047 | break; | |
2048 | default: | |
2049 | return false; | |
2050 | } | |
2051 | return true; | |
2052 | case NB_OP_MOVE: | |
db452508 | 2053 | if (!CHECK_FLAG(snode->flags, LYS_CONFIG_W)) |
1c2facd1 RW |
2054 | return false; |
2055 | ||
2056 | switch (snode->nodetype) { | |
2057 | case LYS_LIST: | |
2058 | case LYS_LEAFLIST: | |
3bb513c3 | 2059 | if (!CHECK_FLAG(snode->flags, LYS_ORDBY_USER)) |
1c2facd1 RW |
2060 | return false; |
2061 | break; | |
2062 | default: | |
2063 | return false; | |
2064 | } | |
2065 | return true; | |
34224f0c | 2066 | case NB_OP_PRE_VALIDATE: |
1c2facd1 | 2067 | case NB_OP_APPLY_FINISH: |
db452508 | 2068 | if (!CHECK_FLAG(snode->flags, LYS_CONFIG_W)) |
1c2facd1 RW |
2069 | return false; |
2070 | return true; | |
2071 | case NB_OP_GET_ELEM: | |
db452508 | 2072 | if (!CHECK_FLAG(snode->flags, LYS_CONFIG_R)) |
1c2facd1 RW |
2073 | return false; |
2074 | ||
2075 | switch (snode->nodetype) { | |
2076 | case LYS_LEAF: | |
1a4bc045 | 2077 | case LYS_LEAFLIST: |
1c2facd1 RW |
2078 | break; |
2079 | case LYS_CONTAINER: | |
3bb513c3 CH |
2080 | scontainer = (struct lysc_node_container *)snode; |
2081 | if (!CHECK_FLAG(scontainer->flags, LYS_PRESENCE)) | |
1c2facd1 RW |
2082 | return false; |
2083 | break; | |
2084 | default: | |
2085 | return false; | |
2086 | } | |
2087 | return true; | |
2088 | case NB_OP_GET_NEXT: | |
1a4bc045 RW |
2089 | switch (snode->nodetype) { |
2090 | case LYS_LIST: | |
2091 | if (CHECK_FLAG(nb_node->flags, F_NB_NODE_CONFIG_ONLY)) | |
2092 | return false; | |
2093 | break; | |
2094 | case LYS_LEAFLIST: | |
2095 | if (CHECK_FLAG(snode->flags, LYS_CONFIG_W)) | |
2096 | return false; | |
2097 | break; | |
2098 | default: | |
2099 | return false; | |
2100 | } | |
2101 | return true; | |
1c2facd1 RW |
2102 | case NB_OP_GET_KEYS: |
2103 | case NB_OP_LOOKUP_ENTRY: | |
1c2facd1 RW |
2104 | switch (snode->nodetype) { |
2105 | case LYS_LIST: | |
544ca69a RW |
2106 | if (CHECK_FLAG(nb_node->flags, F_NB_NODE_CONFIG_ONLY)) |
2107 | return false; | |
99fb518f RW |
2108 | if (CHECK_FLAG(nb_node->flags, F_NB_NODE_KEYLESS_LIST)) |
2109 | return false; | |
1c2facd1 RW |
2110 | break; |
2111 | default: | |
2112 | return false; | |
2113 | } | |
2114 | return true; | |
2115 | case NB_OP_RPC: | |
db452508 | 2116 | if (CHECK_FLAG(snode->flags, LYS_CONFIG_W | LYS_CONFIG_R)) |
1c2facd1 RW |
2117 | return false; |
2118 | ||
2119 | switch (snode->nodetype) { | |
2120 | case LYS_RPC: | |
2121 | case LYS_ACTION: | |
2122 | break; | |
2123 | default: | |
2124 | return false; | |
2125 | } | |
2126 | return true; | |
2127 | default: | |
2128 | return false; | |
2129 | } | |
2130 | } | |
2131 | ||
2132 | DEFINE_HOOK(nb_notification_send, (const char *xpath, struct list *arguments), | |
2133 | (xpath, arguments)); | |
2134 | ||
2135 | int nb_notification_send(const char *xpath, struct list *arguments) | |
2136 | { | |
2137 | int ret; | |
2138 | ||
9eb2c0a1 RW |
2139 | DEBUGD(&nb_dbg_notif, "northbound notification: %s", xpath); |
2140 | ||
1c2facd1 RW |
2141 | ret = hook_call(nb_notification_send, xpath, arguments); |
2142 | if (arguments) | |
2143 | list_delete(&arguments); | |
2144 | ||
2145 | return ret; | |
2146 | } | |
2147 | ||
ccd43ada RW |
2148 | /* Running configuration user pointers management. */ |
2149 | struct nb_config_entry { | |
2150 | char xpath[XPATH_MAXLEN]; | |
2151 | void *entry; | |
2152 | }; | |
2153 | ||
2154 | static bool running_config_entry_cmp(const void *value1, const void *value2) | |
2155 | { | |
2156 | const struct nb_config_entry *c1 = value1; | |
2157 | const struct nb_config_entry *c2 = value2; | |
2158 | ||
2159 | return strmatch(c1->xpath, c2->xpath); | |
2160 | } | |
2161 | ||
d8b87afe | 2162 | static unsigned int running_config_entry_key_make(const void *value) |
ccd43ada RW |
2163 | { |
2164 | return string_hash_make(value); | |
2165 | } | |
2166 | ||
2167 | static void *running_config_entry_alloc(void *p) | |
2168 | { | |
2169 | struct nb_config_entry *new, *key = p; | |
2170 | ||
2171 | new = XCALLOC(MTYPE_NB_CONFIG_ENTRY, sizeof(*new)); | |
2172 | strlcpy(new->xpath, key->xpath, sizeof(new->xpath)); | |
2173 | ||
2174 | return new; | |
2175 | } | |
2176 | ||
2177 | static void running_config_entry_free(void *arg) | |
2178 | { | |
2179 | XFREE(MTYPE_NB_CONFIG_ENTRY, arg); | |
2180 | } | |
2181 | ||
2182 | void nb_running_set_entry(const struct lyd_node *dnode, void *entry) | |
2183 | { | |
2184 | struct nb_config_entry *config, s; | |
2185 | ||
2186 | yang_dnode_get_path(dnode, s.xpath, sizeof(s.xpath)); | |
2187 | config = hash_get(running_config_entries, &s, | |
2188 | running_config_entry_alloc); | |
2189 | config->entry = entry; | |
2190 | } | |
2191 | ||
f7c20aa1 QY |
2192 | void nb_running_move_tree(const char *xpath_from, const char *xpath_to) |
2193 | { | |
2194 | struct nb_config_entry *entry; | |
2195 | struct list *entries = hash_to_list(running_config_entries); | |
2196 | struct listnode *ln; | |
2197 | ||
2198 | for (ALL_LIST_ELEMENTS_RO(entries, ln, entry)) { | |
2199 | if (!frrstr_startswith(entry->xpath, xpath_from)) | |
2200 | continue; | |
2201 | ||
2202 | hash_release(running_config_entries, entry); | |
2203 | ||
2204 | char *newpath = | |
2205 | frrstr_replace(entry->xpath, xpath_from, xpath_to); | |
2206 | strlcpy(entry->xpath, newpath, sizeof(entry->xpath)); | |
2207 | XFREE(MTYPE_TMP, newpath); | |
2208 | ||
2209 | hash_get(running_config_entries, entry, hash_alloc_intern); | |
2210 | } | |
2211 | ||
2212 | list_delete(&entries); | |
2213 | } | |
2214 | ||
ccd43ada RW |
2215 | static void *nb_running_unset_entry_helper(const struct lyd_node *dnode) |
2216 | { | |
2217 | struct nb_config_entry *config, s; | |
2218 | struct lyd_node *child; | |
2219 | void *entry = NULL; | |
2220 | ||
2221 | yang_dnode_get_path(dnode, s.xpath, sizeof(s.xpath)); | |
2222 | config = hash_release(running_config_entries, &s); | |
2223 | if (config) { | |
2224 | entry = config->entry; | |
2225 | running_config_entry_free(config); | |
2226 | } | |
2227 | ||
2228 | /* Unset user pointers from the child nodes. */ | |
2229 | if (CHECK_FLAG(dnode->schema->nodetype, LYS_LIST | LYS_CONTAINER)) { | |
3bb513c3 | 2230 | LY_LIST_FOR (lyd_child(dnode), child) { |
ccd43ada RW |
2231 | (void)nb_running_unset_entry_helper(child); |
2232 | } | |
2233 | } | |
2234 | ||
2235 | return entry; | |
2236 | } | |
2237 | ||
2238 | void *nb_running_unset_entry(const struct lyd_node *dnode) | |
2239 | { | |
2240 | void *entry; | |
2241 | ||
2242 | entry = nb_running_unset_entry_helper(dnode); | |
2243 | assert(entry); | |
2244 | ||
2245 | return entry; | |
2246 | } | |
2247 | ||
b112b1ab G |
2248 | static void *nb_running_get_entry_worker(const struct lyd_node *dnode, |
2249 | const char *xpath, | |
2250 | bool abort_if_not_found, | |
2251 | bool rec_search) | |
ccd43ada RW |
2252 | { |
2253 | const struct lyd_node *orig_dnode = dnode; | |
2254 | char xpath_buf[XPATH_MAXLEN]; | |
b112b1ab | 2255 | bool rec_flag = true; |
ccd43ada RW |
2256 | |
2257 | assert(dnode || xpath); | |
2258 | ||
2259 | if (!dnode) | |
2260 | dnode = yang_dnode_get(running_config->dnode, xpath); | |
2261 | ||
b112b1ab | 2262 | while (rec_flag && dnode) { |
ccd43ada RW |
2263 | struct nb_config_entry *config, s; |
2264 | ||
2265 | yang_dnode_get_path(dnode, s.xpath, sizeof(s.xpath)); | |
2266 | config = hash_lookup(running_config_entries, &s); | |
2267 | if (config) | |
2268 | return config->entry; | |
2269 | ||
b112b1ab G |
2270 | rec_flag = rec_search; |
2271 | ||
3bb513c3 | 2272 | dnode = lyd_parent(dnode); |
ccd43ada RW |
2273 | } |
2274 | ||
2275 | if (!abort_if_not_found) | |
2276 | return NULL; | |
2277 | ||
2278 | yang_dnode_get_path(orig_dnode, xpath_buf, sizeof(xpath_buf)); | |
2279 | flog_err(EC_LIB_YANG_DNODE_NOT_FOUND, | |
2280 | "%s: failed to find entry [xpath %s]", __func__, xpath_buf); | |
2281 | zlog_backtrace(LOG_ERR); | |
2282 | abort(); | |
2283 | } | |
2284 | ||
b112b1ab G |
2285 | void *nb_running_get_entry(const struct lyd_node *dnode, const char *xpath, |
2286 | bool abort_if_not_found) | |
2287 | { | |
2288 | return nb_running_get_entry_worker(dnode, xpath, abort_if_not_found, | |
2289 | true); | |
2290 | } | |
2291 | ||
2292 | void *nb_running_get_entry_non_rec(const struct lyd_node *dnode, | |
2293 | const char *xpath, bool abort_if_not_found) | |
2294 | { | |
2295 | return nb_running_get_entry_worker(dnode, xpath, abort_if_not_found, | |
2296 | false); | |
2297 | } | |
2298 | ||
ccd43ada | 2299 | /* Logging functions. */ |
1c2facd1 RW |
2300 | const char *nb_event_name(enum nb_event event) |
2301 | { | |
2302 | switch (event) { | |
2303 | case NB_EV_VALIDATE: | |
2304 | return "validate"; | |
2305 | case NB_EV_PREPARE: | |
2306 | return "prepare"; | |
2307 | case NB_EV_ABORT: | |
2308 | return "abort"; | |
2309 | case NB_EV_APPLY: | |
2310 | return "apply"; | |
2311 | default: | |
2312 | return "unknown"; | |
2313 | } | |
2314 | } | |
2315 | ||
2316 | const char *nb_operation_name(enum nb_operation operation) | |
2317 | { | |
2318 | switch (operation) { | |
2319 | case NB_OP_CREATE: | |
2320 | return "create"; | |
2321 | case NB_OP_MODIFY: | |
2322 | return "modify"; | |
95ce849b MS |
2323 | case NB_OP_DESTROY: |
2324 | return "destroy"; | |
1c2facd1 RW |
2325 | case NB_OP_MOVE: |
2326 | return "move"; | |
34224f0c RW |
2327 | case NB_OP_PRE_VALIDATE: |
2328 | return "pre_validate"; | |
1c2facd1 RW |
2329 | case NB_OP_APPLY_FINISH: |
2330 | return "apply_finish"; | |
2331 | case NB_OP_GET_ELEM: | |
2332 | return "get_elem"; | |
2333 | case NB_OP_GET_NEXT: | |
2334 | return "get_next"; | |
2335 | case NB_OP_GET_KEYS: | |
2336 | return "get_keys"; | |
2337 | case NB_OP_LOOKUP_ENTRY: | |
2338 | return "lookup_entry"; | |
2339 | case NB_OP_RPC: | |
2340 | return "rpc"; | |
2341 | default: | |
2342 | return "unknown"; | |
2343 | } | |
2344 | } | |
2345 | ||
2346 | const char *nb_err_name(enum nb_error error) | |
2347 | { | |
2348 | switch (error) { | |
2349 | case NB_OK: | |
2350 | return "ok"; | |
2351 | case NB_ERR: | |
2352 | return "generic error"; | |
2353 | case NB_ERR_NO_CHANGES: | |
2354 | return "no changes"; | |
2355 | case NB_ERR_NOT_FOUND: | |
2356 | return "element not found"; | |
2357 | case NB_ERR_LOCKED: | |
2358 | return "resource is locked"; | |
2359 | case NB_ERR_VALIDATION: | |
df5eda3d | 2360 | return "validation"; |
1c2facd1 RW |
2361 | case NB_ERR_RESOURCE: |
2362 | return "failed to allocate resource"; | |
2363 | case NB_ERR_INCONSISTENCY: | |
2364 | return "internal inconsistency"; | |
2365 | default: | |
2366 | return "unknown"; | |
2367 | } | |
2368 | } | |
2369 | ||
2370 | const char *nb_client_name(enum nb_client client) | |
2371 | { | |
2372 | switch (client) { | |
2373 | case NB_CLIENT_CLI: | |
2374 | return "CLI"; | |
5bce33b3 RW |
2375 | case NB_CLIENT_CONFD: |
2376 | return "ConfD"; | |
a7ca2199 RW |
2377 | case NB_CLIENT_SYSREPO: |
2378 | return "Sysrepo"; | |
ec2ac5f2 RW |
2379 | case NB_CLIENT_GRPC: |
2380 | return "gRPC"; | |
1c2facd1 RW |
2381 | default: |
2382 | return "unknown"; | |
2383 | } | |
2384 | } | |
2385 | ||
2386 | static void nb_load_callbacks(const struct frr_yang_module_info *module) | |
2387 | { | |
2388 | for (size_t i = 0; module->nodes[i].xpath; i++) { | |
2389 | struct nb_node *nb_node; | |
2390 | uint32_t priority; | |
2391 | ||
dc397e4c RW |
2392 | if (i > YANG_MODULE_MAX_NODES) { |
2393 | zlog_err( | |
2394 | "%s: %s.yang has more than %u nodes. Please increase YANG_MODULE_MAX_NODES to fix this problem.", | |
2395 | __func__, module->name, YANG_MODULE_MAX_NODES); | |
2396 | exit(1); | |
2397 | } | |
2398 | ||
1c2facd1 RW |
2399 | nb_node = nb_node_find(module->nodes[i].xpath); |
2400 | if (!nb_node) { | |
2401 | flog_warn(EC_LIB_YANG_UNKNOWN_DATA_PATH, | |
2402 | "%s: unknown data path: %s", __func__, | |
2403 | module->nodes[i].xpath); | |
2404 | continue; | |
2405 | } | |
2406 | ||
2407 | nb_node->cbs = module->nodes[i].cbs; | |
2408 | priority = module->nodes[i].priority; | |
2409 | if (priority != 0) | |
2410 | nb_node->priority = priority; | |
2411 | } | |
2412 | } | |
2413 | ||
59e85ca1 | 2414 | void nb_validate_callbacks(void) |
1c2facd1 RW |
2415 | { |
2416 | unsigned int errors = 0; | |
2417 | ||
8d869d37 | 2418 | yang_snodes_iterate(NULL, nb_node_validate, 0, &errors); |
1c2facd1 RW |
2419 | if (errors > 0) { |
2420 | flog_err( | |
2421 | EC_LIB_NB_CBS_VALIDATION, | |
2422 | "%s: failed to validate northbound callbacks: %u error(s)", | |
2423 | __func__, errors); | |
2424 | exit(1); | |
2425 | } | |
59e85ca1 RW |
2426 | } |
2427 | ||
59e85ca1 RW |
2428 | |
2429 | void nb_init(struct thread_master *tm, | |
2430 | const struct frr_yang_module_info *const modules[], | |
2431 | size_t nmodules, bool db_enabled) | |
2432 | { | |
3bb513c3 CH |
2433 | struct yang_module *loaded[nmodules], **loadedp = loaded; |
2434 | bool explicit_compile; | |
2435 | ||
2436 | /* | |
2437 | * Currently using this explicit compile feature in libyang2 leads to | |
2438 | * incorrect behavior in FRR. The functionality suppresses the compiling | |
2439 | * of modules until they have all been loaded into the context. This | |
2440 | * avoids multiple recompiles of the same modules as they are | |
2441 | * imported/augmented etc. | |
2442 | */ | |
2443 | explicit_compile = false; | |
2444 | ||
390a8862 CS |
2445 | nb_db_enabled = db_enabled; |
2446 | ||
3bb513c3 CH |
2447 | yang_init(true, explicit_compile); |
2448 | ||
59e85ca1 | 2449 | /* Load YANG modules and their corresponding northbound callbacks. */ |
3bb513c3 CH |
2450 | for (size_t i = 0; i < nmodules; i++) { |
2451 | DEBUGD(&nb_dbg_events, "northbound: loading %s.yang", | |
2452 | modules[i]->name); | |
2453 | *loadedp++ = yang_module_load(modules[i]->name); | |
2454 | } | |
2455 | ||
2456 | if (explicit_compile) | |
2457 | yang_init_loading_complete(); | |
2458 | ||
2459 | /* Initialize the compiled nodes with northbound data */ | |
2460 | for (size_t i = 0; i < nmodules; i++) { | |
2461 | yang_snodes_iterate(loaded[i]->info, nb_node_new_cb, 0, NULL); | |
2462 | nb_load_callbacks(modules[i]); | |
2463 | } | |
59e85ca1 RW |
2464 | |
2465 | /* Validate northbound callbacks. */ | |
2466 | nb_validate_callbacks(); | |
2467 | ||
1c2facd1 RW |
2468 | /* Create an empty running configuration. */ |
2469 | running_config = nb_config_new(NULL); | |
ccd43ada RW |
2470 | running_config_entries = hash_create(running_config_entry_key_make, |
2471 | running_config_entry_cmp, | |
2472 | "Running Configuration Entries"); | |
364ad673 | 2473 | pthread_mutex_init(&running_config_mgmt_lock.mtx, NULL); |
1c2facd1 RW |
2474 | |
2475 | /* Initialize the northbound CLI. */ | |
fbdc1c0a | 2476 | nb_cli_init(tm); |
1c2facd1 RW |
2477 | } |
2478 | ||
2479 | void nb_terminate(void) | |
2480 | { | |
2481 | /* Terminate the northbound CLI. */ | |
2482 | nb_cli_terminate(); | |
2483 | ||
2484 | /* Delete all nb_node's from all YANG modules. */ | |
544ca69a | 2485 | nb_nodes_delete(); |
1c2facd1 RW |
2486 | |
2487 | /* Delete the running configuration. */ | |
ccd43ada RW |
2488 | hash_clean(running_config_entries, running_config_entry_free); |
2489 | hash_free(running_config_entries); | |
1c2facd1 | 2490 | nb_config_free(running_config); |
364ad673 | 2491 | pthread_mutex_destroy(&running_config_mgmt_lock.mtx); |
1c2facd1 | 2492 | } |