]> git.proxmox.com Git - mirror_frr.git/blame - lib/northbound_sysrepo.c
tests: Topotest fixes to skip comparing InterfaceIndex and Internal status in json_cmp
[mirror_frr.git] / lib / northbound_sysrepo.c
CommitLineData
a7ca2199
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 "log.h"
23#include "lib_errors.h"
24#include "command.h"
9eb2c0a1 25#include "debug.h"
a7ca2199
RW
26#include "memory.h"
27#include "libfrr.h"
28#include "version.h"
29#include "northbound.h"
30
31#include <sysrepo.h>
32#include <sysrepo/values.h>
33#include <sysrepo/xpath.h>
34
35DEFINE_MTYPE_STATIC(LIB, SYSREPO, "Sysrepo module")
36
9eb2c0a1
RW
37static struct debug nb_dbg_client_sysrepo = {0, "Northbound client: Sysrepo"};
38
a7ca2199 39static struct thread_master *master;
a7ca2199
RW
40static sr_session_ctx_t *session;
41static sr_conn_ctx_t *connection;
88a7d121 42static struct nb_transaction *transaction;
a7ca2199
RW
43
44static int frr_sr_read_cb(struct thread *thread);
a7ca2199
RW
45static int frr_sr_finish(void);
46
47/* Convert FRR YANG data value to sysrepo YANG data value. */
48static int yang_data_frr2sr(struct yang_data *frr_data, sr_val_t *sr_data)
49{
80243aef 50 struct nb_node *nb_node;
a7ca2199
RW
51 const struct lys_node *snode;
52 struct lys_node_container *scontainer;
53 struct lys_node_leaf *sleaf;
54 struct lys_node_leaflist *sleaflist;
55 LY_DATA_TYPE type;
56
57 sr_val_set_xpath(sr_data, frr_data->xpath);
58
80243aef
RW
59 nb_node = nb_node_find(frr_data->xpath);
60 if (!nb_node) {
61 flog_warn(EC_LIB_YANG_UNKNOWN_DATA_PATH,
62 "%s: unknown data path: %s", __func__,
63 frr_data->xpath);
64 return -1;
65 }
66
67 snode = nb_node->snode;
a7ca2199
RW
68 switch (snode->nodetype) {
69 case LYS_CONTAINER:
70 scontainer = (struct lys_node_container *)snode;
71 if (!scontainer->presence)
72 return -1;
73 sr_data->type = SR_CONTAINER_PRESENCE_T;
74 return 0;
75 case LYS_LIST:
76 sr_data->type = SR_LIST_T;
77 return 0;
78 case LYS_LEAF:
79 sleaf = (struct lys_node_leaf *)snode;
80 type = sleaf->type.base;
81 break;
82 case LYS_LEAFLIST:
83 sleaflist = (struct lys_node_leaflist *)snode;
84 type = sleaflist->type.base;
85 break;
86 default:
87 return -1;
88 }
89
90 switch (type) {
91 case LY_TYPE_BINARY:
92 sr_val_set_str_data(sr_data, SR_BINARY_T, frr_data->value);
93 break;
94 case LY_TYPE_BITS:
95 sr_val_set_str_data(sr_data, SR_BITS_T, frr_data->value);
96 break;
97 case LY_TYPE_BOOL:
98 sr_data->type = SR_BOOL_T;
99 sr_data->data.bool_val = yang_str2bool(frr_data->value);
100 break;
101 case LY_TYPE_DEC64:
102 sr_data->type = SR_DECIMAL64_T;
103 sr_data->data.decimal64_val =
104 yang_str2dec64(frr_data->xpath, frr_data->value);
105 break;
106 case LY_TYPE_EMPTY:
107 sr_data->type = SR_LEAF_EMPTY_T;
108 break;
109 case LY_TYPE_ENUM:
110 sr_val_set_str_data(sr_data, SR_ENUM_T, frr_data->value);
111 break;
112 case LY_TYPE_IDENT:
113 sr_val_set_str_data(sr_data, SR_IDENTITYREF_T, frr_data->value);
114 break;
115 case LY_TYPE_INST:
116 sr_val_set_str_data(sr_data, SR_INSTANCEID_T, frr_data->value);
117 break;
118 case LY_TYPE_INT8:
119 sr_data->type = SR_INT8_T;
120 sr_data->data.int8_val = yang_str2int8(frr_data->value);
121 break;
122 case LY_TYPE_INT16:
123 sr_data->type = SR_INT16_T;
124 sr_data->data.int16_val = yang_str2int16(frr_data->value);
125 break;
126 case LY_TYPE_INT32:
127 sr_data->type = SR_INT32_T;
128 sr_data->data.int32_val = yang_str2int32(frr_data->value);
129 break;
130 case LY_TYPE_INT64:
131 sr_data->type = SR_INT64_T;
132 sr_data->data.int64_val = yang_str2int64(frr_data->value);
133 break;
134 case LY_TYPE_STRING:
135 sr_val_set_str_data(sr_data, SR_STRING_T, frr_data->value);
136 break;
137 case LY_TYPE_UINT8:
138 sr_data->type = SR_UINT8_T;
139 sr_data->data.uint8_val = yang_str2uint8(frr_data->value);
140 break;
141 case LY_TYPE_UINT16:
142 sr_data->type = SR_UINT16_T;
143 sr_data->data.uint16_val = yang_str2uint16(frr_data->value);
144 break;
145 case LY_TYPE_UINT32:
146 sr_data->type = SR_UINT32_T;
147 sr_data->data.uint32_val = yang_str2uint32(frr_data->value);
148 break;
149 case LY_TYPE_UINT64:
150 sr_data->type = SR_UINT64_T;
151 sr_data->data.uint64_val = yang_str2uint64(frr_data->value);
152 break;
153 default:
154 return -1;
155 }
156
157 return 0;
158}
159
160static int frr_sr_process_change(struct nb_config *candidate,
161 sr_change_oper_t sr_op, sr_val_t *sr_old_val,
162 sr_val_t *sr_new_val)
163{
164 struct nb_node *nb_node;
165 enum nb_operation nb_op;
166 sr_val_t *sr_data;
167 const char *xpath;
168 char value_str[YANG_VALUE_MAXLEN];
169 struct yang_data *data;
170 int ret;
171
172 sr_data = sr_new_val ? sr_new_val : sr_old_val;
173 assert(sr_data);
174
175 xpath = sr_data->xpath;
176
bbeaa033
RW
177 DEBUGD(&nb_dbg_client_sysrepo, "sysrepo: processing change [xpath %s]",
178 xpath);
179
a7ca2199
RW
180 /* Non-presence container - nothing to do. */
181 if (sr_data->type == SR_CONTAINER_T)
182 return NB_OK;
183
184 nb_node = nb_node_find(xpath);
185 if (!nb_node) {
186 flog_warn(EC_LIB_YANG_UNKNOWN_DATA_PATH,
187 "%s: unknown data path: %s", __func__, xpath);
188 return NB_ERR;
189 }
190
191 /* Map operation values. */
192 switch (sr_op) {
193 case SR_OP_CREATED:
194 case SR_OP_MODIFIED:
195 if (nb_operation_is_valid(NB_OP_CREATE, nb_node->snode))
196 nb_op = NB_OP_CREATE;
197 else if (nb_operation_is_valid(NB_OP_MODIFY, nb_node->snode)) {
198 nb_op = NB_OP_MODIFY;
199 } else
200 /* Ignore list keys modifications. */
201 return NB_OK;
202 break;
203 case SR_OP_DELETED:
204 /*
205 * When a list is deleted or one of its keys is changed, we are
206 * notified about the removal of all of its leafs, even the ones
207 * that are non-optional. We need to ignore these notifications.
208 */
95ce849b 209 if (!nb_operation_is_valid(NB_OP_DESTROY, nb_node->snode))
a7ca2199
RW
210 return NB_OK;
211
95ce849b 212 nb_op = NB_OP_DESTROY;
a7ca2199
RW
213 break;
214 case SR_OP_MOVED:
215 nb_op = NB_OP_MOVE;
216 break;
217 default:
218 flog_err(EC_LIB_DEVELOPMENT,
219 "%s: unexpected operation %u [xpath %s]", __func__,
220 sr_op, xpath);
221 return NB_ERR;
222 }
223
224 sr_val_to_buff(sr_data, value_str, sizeof(value_str));
225 data = yang_data_new(xpath, value_str);
226
227 ret = nb_candidate_edit(candidate, nb_node, nb_op, xpath, NULL, data);
228 yang_data_free(data);
7dac19f7 229 if (ret != NB_OK && ret != NB_ERR_NOT_FOUND) {
a7ca2199
RW
230 flog_warn(
231 EC_LIB_NB_CANDIDATE_EDIT_ERROR,
232 "%s: failed to edit candidate configuration: operation [%s] xpath [%s]",
233 __func__, nb_operation_name(nb_op), xpath);
234 return NB_ERR;
235 }
236
237 return NB_OK;
238}
239
24ed137c 240static int frr_sr_config_change_cb_prepare(sr_session_ctx_t *session,
bbeaa033 241 const char *module_name)
a7ca2199
RW
242{
243 sr_change_iter_t *it;
244 int ret;
245 sr_change_oper_t sr_op;
246 sr_val_t *sr_old_val, *sr_new_val;
13d6b9c1 247 struct nb_context context = {};
a7ca2199 248 struct nb_config *candidate;
df5eda3d 249 char errmsg[BUFSIZ] = {0};
a7ca2199 250
24ed137c 251 ret = sr_get_changes_iter(session, "//*", &it);
a7ca2199
RW
252 if (ret != SR_ERR_OK) {
253 flog_err(EC_LIB_LIBSYSREPO,
24ed137c
RW
254 "%s: sr_get_changes_iter() failed for \"%s\"",
255 __func__, module_name);
a7ca2199
RW
256 return ret;
257 }
258
8685be73 259 candidate = nb_config_dup(running_config);
a7ca2199
RW
260
261 while ((ret = sr_get_change_next(session, it, &sr_op, &sr_old_val,
262 &sr_new_val))
263 == SR_ERR_OK) {
264 ret = frr_sr_process_change(candidate, sr_op, sr_old_val,
265 sr_new_val);
266 sr_free_val(sr_old_val);
267 sr_free_val(sr_new_val);
268 if (ret != NB_OK)
269 break;
270 }
271
272 sr_free_change_iter(it);
273 if (ret != NB_OK && ret != SR_ERR_NOT_FOUND) {
274 nb_config_free(candidate);
275 return SR_ERR_INTERNAL;
276 }
277
88a7d121 278 transaction = NULL;
13d6b9c1 279 context.client = NB_CLIENT_SYSREPO;
bbeaa033
RW
280 /*
281 * Validate the configuration changes and allocate all resources
282 * required to apply them.
283 */
284 ret = nb_candidate_commit_prepare(&context, candidate, NULL,
285 &transaction, errmsg, sizeof(errmsg));
286 if (ret != NB_OK && ret != NB_ERR_NO_CHANGES)
287 flog_warn(
288 EC_LIB_LIBSYSREPO,
289 "%s: failed to prepare configuration transaction: %s (%s)",
290 __func__, nb_err_name(ret), errmsg);
a7ca2199 291
24ed137c
RW
292 if (!transaction)
293 nb_config_free(candidate);
294
a7ca2199
RW
295 /* Map northbound return code to sysrepo return code. */
296 switch (ret) {
297 case NB_OK:
88a7d121 298 return SR_ERR_OK;
a7ca2199
RW
299 case NB_ERR_NO_CHANGES:
300 return SR_ERR_OK;
301 case NB_ERR_LOCKED:
302 return SR_ERR_LOCKED;
303 case NB_ERR_RESOURCE:
304 return SR_ERR_NOMEM;
305 default:
306 return SR_ERR_VALIDATION_FAILED;
307 }
308}
309
88a7d121
RW
310static int frr_sr_config_change_cb_apply(sr_session_ctx_t *session,
311 const char *module_name)
312{
313 /* Apply the transaction. */
314 if (transaction) {
315 struct nb_config *candidate = transaction->config;
0fe5b904 316 char errmsg[BUFSIZ] = {0};
88a7d121 317
0fe5b904
RW
318 nb_candidate_commit_apply(transaction, true, NULL, errmsg,
319 sizeof(errmsg));
88a7d121
RW
320 nb_config_free(candidate);
321 }
322
323 return SR_ERR_OK;
324}
325
326static int frr_sr_config_change_cb_abort(sr_session_ctx_t *session,
327 const char *module_name)
328{
329 /* Abort the transaction. */
330 if (transaction) {
331 struct nb_config *candidate = transaction->config;
0fe5b904 332 char errmsg[BUFSIZ] = {0};
88a7d121 333
0fe5b904 334 nb_candidate_commit_abort(transaction, errmsg, sizeof(errmsg));
88a7d121
RW
335 nb_config_free(candidate);
336 }
337
338 return SR_ERR_OK;
339}
340
341/* Callback for changes in the running configuration. */
342static int frr_sr_config_change_cb(sr_session_ctx_t *session,
24ed137c
RW
343 const char *module_name, const char *xpath,
344 sr_event_t sr_ev, uint32_t request_id,
345 void *private_data)
88a7d121
RW
346{
347 switch (sr_ev) {
348 case SR_EV_ENABLED:
24ed137c 349 case SR_EV_CHANGE:
bbeaa033 350 return frr_sr_config_change_cb_prepare(session, module_name);
24ed137c 351 case SR_EV_DONE:
88a7d121
RW
352 return frr_sr_config_change_cb_apply(session, module_name);
353 case SR_EV_ABORT:
354 return frr_sr_config_change_cb_abort(session, module_name);
355 default:
24ed137c 356 flog_err(EC_LIB_LIBSYSREPO, "%s: unexpected sysrepo event: %u",
88a7d121
RW
357 __func__, sr_ev);
358 return SR_ERR_INTERNAL;
359 }
360}
361
1a4bc045
RW
362static int frr_sr_state_data_iter_cb(const struct lys_node *snode,
363 struct yang_translator *translator,
364 struct yang_data *data, void *arg)
a7ca2199 365{
24ed137c
RW
366 struct lyd_node *dnode = arg;
367
368 ly_errno = 0;
369 dnode = lyd_new_path(dnode, ly_native_ctx, data->xpath, data->value, 0,
370 LYD_PATH_OPT_UPDATE);
371 if (!dnode && ly_errno) {
372 flog_warn(EC_LIB_LIBYANG, "%s: lyd_new_path() failed",
373 __func__);
374 yang_data_free(data);
375 return NB_ERR;
376 }
a7ca2199 377
24ed137c 378 yang_data_free(data);
1a4bc045 379 return NB_OK;
a7ca2199
RW
380}
381
382/* Callback for state retrieval. */
24ed137c
RW
383static int frr_sr_state_cb(sr_session_ctx_t *session, const char *module_name,
384 const char *xpath, const char *request_xpath,
385 uint32_t request_id, struct lyd_node **parent,
386 void *private_ctx)
a7ca2199 387{
24ed137c 388 struct lyd_node *dnode;
a7ca2199 389
24ed137c
RW
390 dnode = *parent;
391 if (nb_oper_data_iterate(request_xpath, NULL, 0,
392 frr_sr_state_data_iter_cb, dnode)
1a4bc045
RW
393 != NB_OK) {
394 flog_warn(EC_LIB_NB_OPERATIONAL_DATA,
395 "%s: failed to obtain operational data [xpath %s]",
396 __func__, xpath);
24ed137c 397 return SR_ERR_INTERNAL;
a7ca2199
RW
398 }
399
24ed137c 400 *parent = dnode;
a7ca2199
RW
401
402 return SR_ERR_OK;
403}
404
24ed137c
RW
405static int frr_sr_config_rpc_cb(sr_session_ctx_t *session, const char *xpath,
406 const sr_val_t *sr_input,
407 const size_t input_cnt, sr_event_t sr_ev,
408 uint32_t request_id, sr_val_t **sr_output,
a7ca2199
RW
409 size_t *sr_output_cnt, void *private_ctx)
410{
411 struct nb_node *nb_node;
412 struct list *input;
413 struct list *output;
414 struct yang_data *data;
415 size_t cb_output_cnt;
416 int ret = SR_ERR_OK;
417
418 nb_node = nb_node_find(xpath);
419 if (!nb_node) {
420 flog_warn(EC_LIB_YANG_UNKNOWN_DATA_PATH,
421 "%s: unknown data path: %s", __func__, xpath);
422 return SR_ERR_INTERNAL;
423 }
424
425 input = yang_data_list_new();
426 output = yang_data_list_new();
427
428 /* Process input. */
429 for (size_t i = 0; i < input_cnt; i++) {
430 char value_str[YANG_VALUE_MAXLEN];
431
432 sr_val_to_buff(&sr_input[i], value_str, sizeof(value_str));
433
434 data = yang_data_new(xpath, value_str);
435 listnode_add(input, data);
436 }
437
438 /* Execute callback registered for this XPath. */
9eb2c0a1 439 if (nb_callback_rpc(nb_node, xpath, input, output) != NB_OK) {
a7ca2199
RW
440 flog_warn(EC_LIB_NB_CB_RPC, "%s: rpc callback failed: %s",
441 __func__, xpath);
442 ret = SR_ERR_OPERATION_FAILED;
443 goto exit;
444 }
445
446 /* Process output. */
447 if (listcount(output) > 0) {
448 sr_val_t *values = NULL;
449 struct listnode *node;
450 int i = 0;
451
452 cb_output_cnt = listcount(output);
453 ret = sr_new_values(cb_output_cnt, &values);
454 if (ret != SR_ERR_OK) {
455 flog_err(EC_LIB_LIBSYSREPO, "%s: sr_new_values(): %s",
456 __func__, sr_strerror(ret));
457 goto exit;
458 }
459
460 for (ALL_LIST_ELEMENTS_RO(output, node, data)) {
461 if (yang_data_frr2sr(data, &values[i++]) != 0) {
462 flog_err(
463 EC_LIB_SYSREPO_DATA_CONVERT,
464 "%s: failed to convert data to Sysrepo format",
465 __func__);
466 ret = SR_ERR_INTERNAL;
467 sr_free_values(values, cb_output_cnt);
468 goto exit;
469 }
470 }
471
472 *sr_output = values;
473 *sr_output_cnt = cb_output_cnt;
474 }
475
476exit:
477 /* Release memory. */
478 list_delete(&input);
479 list_delete(&output);
480
481 return ret;
482}
483
484static int frr_sr_notification_send(const char *xpath, struct list *arguments)
485{
486 sr_val_t *values = NULL;
487 size_t values_cnt = 0;
488 int ret;
489
490 if (arguments && listcount(arguments) > 0) {
491 struct yang_data *data;
492 struct listnode *node;
493 int i = 0;
494
495 values_cnt = listcount(arguments);
496 ret = sr_new_values(values_cnt, &values);
497 if (ret != SR_ERR_OK) {
498 flog_err(EC_LIB_LIBSYSREPO, "%s: sr_new_values(): %s",
499 __func__, sr_strerror(ret));
500 return NB_ERR;
501 }
502
503 for (ALL_LIST_ELEMENTS_RO(arguments, node, data)) {
504 if (yang_data_frr2sr(data, &values[i++]) != 0) {
505 flog_err(
506 EC_LIB_SYSREPO_DATA_CONVERT,
507 "%s: failed to convert data to sysrepo format",
508 __func__);
509 sr_free_values(values, values_cnt);
510 return NB_ERR;
511 }
512 }
513 }
514
24ed137c 515 ret = sr_event_notif_send(session, xpath, values, values_cnt);
a7ca2199
RW
516 if (ret != SR_ERR_OK) {
517 flog_err(EC_LIB_LIBSYSREPO,
518 "%s: sr_event_notif_send() failed for xpath %s",
519 __func__, xpath);
520 return NB_ERR;
521 }
522
523 return NB_OK;
524}
525
a7ca2199
RW
526static int frr_sr_read_cb(struct thread *thread)
527{
24ed137c 528 sr_subscription_ctx_t *sr_subscription = THREAD_ARG(thread);
a7ca2199 529 int fd = THREAD_FD(thread);
a7ca2199
RW
530 int ret;
531
24ed137c 532 ret = sr_process_events(sr_subscription, session, NULL);
a7ca2199
RW
533 if (ret != SR_ERR_OK) {
534 flog_err(EC_LIB_LIBSYSREPO, "%s: sr_fd_event_process(): %s",
535 __func__, sr_strerror(ret));
536 return -1;
537 }
538
539 thread = NULL;
24ed137c 540 thread_add_read(master, frr_sr_read_cb, sr_subscription, fd, &thread);
a7ca2199
RW
541
542 return 0;
543}
544
545static void frr_sr_subscribe_config(struct yang_module *module)
546{
547 int ret;
548
bbeaa033
RW
549 DEBUGD(&nb_dbg_client_sysrepo,
550 "sysrepo: subscribing for configuration changes made in the '%s' module",
551 module->name);
552
a7ca2199 553 ret = sr_module_change_subscribe(
24ed137c
RW
554 session, module->name, NULL, frr_sr_config_change_cb, NULL, 0,
555 SR_SUBSCR_DEFAULT | SR_SUBSCR_ENABLED | SR_SUBSCR_NO_THREAD,
a7ca2199
RW
556 &module->sr_subscription);
557 if (ret != SR_ERR_OK)
558 flog_err(EC_LIB_LIBSYSREPO, "sr_module_change_subscribe(): %s",
559 sr_strerror(ret));
560}
561
e0ccfad2 562static int frr_sr_subscribe_state(const struct lys_node *snode, void *arg)
a7ca2199 563{
e0ccfad2 564 struct yang_module *module = arg;
a7ca2199
RW
565 struct nb_node *nb_node;
566 int ret;
567
db452508 568 if (!CHECK_FLAG(snode->flags, LYS_CONFIG_R))
e0ccfad2 569 return YANG_ITER_CONTINUE;
a7ca2199 570 /* We only need to subscribe to the root of the state subtrees. */
db452508 571 if (snode->parent && CHECK_FLAG(snode->parent->flags, LYS_CONFIG_R))
e0ccfad2 572 return YANG_ITER_CONTINUE;
a7ca2199
RW
573
574 nb_node = snode->priv;
9eb2c0a1 575
bbeaa033 576 DEBUGD(&nb_dbg_client_sysrepo, "sysrepo: providing data to '%s'",
9eb2c0a1 577 nb_node->xpath);
a7ca2199 578
24ed137c
RW
579 ret = sr_oper_get_items_subscribe(
580 session, snode->module->name, nb_node->xpath, frr_sr_state_cb,
581 NULL, SR_SUBSCR_CTX_REUSE, &module->sr_subscription);
a7ca2199 582 if (ret != SR_ERR_OK)
24ed137c 583 flog_err(EC_LIB_LIBSYSREPO, "sr_oper_get_items_subscribe(): %s",
a7ca2199 584 sr_strerror(ret));
e0ccfad2
RW
585
586 return YANG_ITER_CONTINUE;
a7ca2199
RW
587}
588
e0ccfad2 589static int frr_sr_subscribe_rpc(const struct lys_node *snode, void *arg)
a7ca2199 590{
e0ccfad2 591 struct yang_module *module = arg;
a7ca2199
RW
592 struct nb_node *nb_node;
593 int ret;
594
595 if (snode->nodetype != LYS_RPC)
e0ccfad2 596 return YANG_ITER_CONTINUE;
a7ca2199
RW
597
598 nb_node = snode->priv;
9eb2c0a1 599
bbeaa033 600 DEBUGD(&nb_dbg_client_sysrepo, "sysrepo: providing RPC to '%s'",
9eb2c0a1 601 nb_node->xpath);
a7ca2199
RW
602
603 ret = sr_rpc_subscribe(session, nb_node->xpath, frr_sr_config_rpc_cb,
24ed137c 604 NULL, 0, SR_SUBSCR_CTX_REUSE,
a7ca2199
RW
605 &module->sr_subscription);
606 if (ret != SR_ERR_OK)
607 flog_err(EC_LIB_LIBSYSREPO, "sr_rpc_subscribe(): %s",
608 sr_strerror(ret));
e0ccfad2
RW
609
610 return YANG_ITER_CONTINUE;
a7ca2199
RW
611}
612
9eb2c0a1
RW
613/* CLI commands. */
614DEFUN (debug_nb_sr,
615 debug_nb_sr_cmd,
616 "[no] debug northbound client sysrepo",
617 NO_STR
618 DEBUG_STR
619 "Northbound debugging\n"
620 "Northbound client\n"
621 "Sysrepo\n")
622{
623 uint32_t mode = DEBUG_NODE2MODE(vty->node);
624 bool no = strmatch(argv[0]->text, "no");
625
626 DEBUG_MODE_SET(&nb_dbg_client_sysrepo, mode, !no);
627
628 return CMD_SUCCESS;
629}
630
631static int frr_sr_debug_config_write(struct vty *vty)
632{
633 if (DEBUG_MODE_CHECK(&nb_dbg_client_sysrepo, DEBUG_MODE_CONF))
634 vty_out(vty, "debug northbound client sysrepo\n");
635
636 return 0;
637}
638
639static int frr_sr_debug_set_all(uint32_t flags, bool set)
640{
641 DEBUG_FLAGS_SET(&nb_dbg_client_sysrepo, flags, set);
642
643 /* If all modes have been turned off, don't preserve options. */
644 if (!DEBUG_MODE_CHECK(&nb_dbg_client_sysrepo, DEBUG_MODE_ALL))
645 DEBUG_CLEAR(&nb_dbg_client_sysrepo);
646
647 return 0;
648}
649
650static void frr_sr_cli_init(void)
651{
652 hook_register(nb_client_debug_config_write, frr_sr_debug_config_write);
653 hook_register(nb_client_debug_set_all, frr_sr_debug_set_all);
654
655 install_element(ENABLE_NODE, &debug_nb_sr_cmd);
656 install_element(CONFIG_NODE, &debug_nb_sr_cmd);
657}
658
a7ca2199 659/* FRR's Sysrepo initialization. */
24ed137c 660static int frr_sr_init(void)
a7ca2199
RW
661{
662 struct yang_module *module;
24ed137c 663 int ret;
a7ca2199
RW
664
665 /* Connect to Sysrepo. */
24ed137c 666 ret = sr_connect(SR_CONN_DEFAULT, &connection);
a7ca2199
RW
667 if (ret != SR_ERR_OK) {
668 flog_err(EC_LIB_SYSREPO_INIT, "%s: sr_connect(): %s", __func__,
669 sr_strerror(ret));
670 goto cleanup;
671 }
672
673 /* Start session. */
24ed137c 674 ret = sr_session_start(connection, SR_DS_RUNNING, &session);
a7ca2199
RW
675 if (ret != SR_ERR_OK) {
676 flog_err(EC_LIB_SYSREPO_INIT, "%s: sr_session_start(): %s",
677 __func__, sr_strerror(ret));
678 goto cleanup;
679 }
680
681 /* Perform subscriptions. */
682 RB_FOREACH (module, yang_modules, &yang_modules) {
24ed137c
RW
683 int event_pipe;
684
a7ca2199 685 frr_sr_subscribe_config(module);
e0ccfad2
RW
686 yang_snodes_iterate_module(module->info, frr_sr_subscribe_state,
687 0, module);
688 yang_snodes_iterate_module(module->info, frr_sr_subscribe_rpc,
689 0, module);
24ed137c
RW
690
691 /* Watch subscriptions. */
692 ret = sr_get_event_pipe(module->sr_subscription, &event_pipe);
693 if (ret != SR_ERR_OK) {
694 flog_err(EC_LIB_SYSREPO_INIT,
695 "%s: sr_get_event_pipe(): %s", __func__,
696 sr_strerror(ret));
697 goto cleanup;
698 }
699 thread_add_read(master, frr_sr_read_cb, module->sr_subscription,
700 event_pipe, &module->sr_thread);
a7ca2199
RW
701 }
702
703 hook_register(nb_notification_send, frr_sr_notification_send);
704
a7ca2199
RW
705 return 0;
706
707cleanup:
708 frr_sr_finish();
709
710 return -1;
711}
712
713static int frr_sr_finish(void)
714{
715 struct yang_module *module;
716
717 RB_FOREACH (module, yang_modules, &yang_modules) {
718 if (!module->sr_subscription)
719 continue;
24ed137c
RW
720 sr_unsubscribe(module->sr_subscription);
721 THREAD_OFF(module->sr_thread);
a7ca2199
RW
722 }
723
724 if (session)
725 sr_session_stop(session);
726 if (connection)
727 sr_disconnect(connection);
728
a7ca2199
RW
729 return 0;
730}
731
88e635ee 732static int frr_sr_module_very_late_init(struct thread_master *tm)
a7ca2199
RW
733{
734 master = tm;
735
24ed137c 736 if (frr_sr_init() < 0) {
a7ca2199
RW
737 flog_err(EC_LIB_SYSREPO_INIT,
738 "failed to initialize the Sysrepo module");
739 return -1;
740 }
741
742 hook_register(frr_fini, frr_sr_finish);
88e635ee
RW
743
744 return 0;
745}
746
747static int frr_sr_module_late_init(struct thread_master *tm)
748{
9eb2c0a1 749 frr_sr_cli_init();
a7ca2199
RW
750
751 return 0;
752}
753
754static int frr_sr_module_init(void)
755{
756 hook_register(frr_late_init, frr_sr_module_late_init);
88e635ee 757 hook_register(frr_very_late_init, frr_sr_module_very_late_init);
a7ca2199
RW
758
759 return 0;
760}
761
762FRR_MODULE_SETUP(.name = "frr_sysrepo", .version = FRR_VERSION,
763 .description = "FRR sysrepo integration module",
764 .init = frr_sr_module_init, )