2 * Copyright (C) 2018 NetDEF, Inc.
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)
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
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
23 #include "lib_errors.h"
29 #include "northbound.h"
32 #include <sysrepo/values.h>
33 #include <sysrepo/xpath.h>
35 DEFINE_MTYPE_STATIC(LIB
, SYSREPO
, "Sysrepo module")
37 static struct debug nb_dbg_client_sysrepo
= {0, "Northbound client: Sysrepo"};
39 static struct thread_master
*master
;
40 static struct list
*sysrepo_threads
;
41 static sr_session_ctx_t
*session
;
42 static sr_conn_ctx_t
*connection
;
43 static struct nb_transaction
*transaction
;
45 static int frr_sr_read_cb(struct thread
*thread
);
46 static int frr_sr_write_cb(struct thread
*thread
);
47 static int frr_sr_finish(void);
49 /* Convert FRR YANG data value to sysrepo YANG data value. */
50 static int yang_data_frr2sr(struct yang_data
*frr_data
, sr_val_t
*sr_data
)
52 struct nb_node
*nb_node
;
53 const struct lys_node
*snode
;
54 struct lys_node_container
*scontainer
;
55 struct lys_node_leaf
*sleaf
;
56 struct lys_node_leaflist
*sleaflist
;
59 sr_val_set_xpath(sr_data
, frr_data
->xpath
);
61 nb_node
= nb_node_find(frr_data
->xpath
);
63 flog_warn(EC_LIB_YANG_UNKNOWN_DATA_PATH
,
64 "%s: unknown data path: %s", __func__
,
69 snode
= nb_node
->snode
;
70 switch (snode
->nodetype
) {
72 scontainer
= (struct lys_node_container
*)snode
;
73 if (!scontainer
->presence
)
75 sr_data
->type
= SR_CONTAINER_PRESENCE_T
;
78 sr_data
->type
= SR_LIST_T
;
81 sleaf
= (struct lys_node_leaf
*)snode
;
82 type
= sleaf
->type
.base
;
85 sleaflist
= (struct lys_node_leaflist
*)snode
;
86 type
= sleaflist
->type
.base
;
94 sr_val_set_str_data(sr_data
, SR_BINARY_T
, frr_data
->value
);
97 sr_val_set_str_data(sr_data
, SR_BITS_T
, frr_data
->value
);
100 sr_data
->type
= SR_BOOL_T
;
101 sr_data
->data
.bool_val
= yang_str2bool(frr_data
->value
);
104 sr_data
->type
= SR_DECIMAL64_T
;
105 sr_data
->data
.decimal64_val
=
106 yang_str2dec64(frr_data
->xpath
, frr_data
->value
);
109 sr_data
->type
= SR_LEAF_EMPTY_T
;
112 sr_val_set_str_data(sr_data
, SR_ENUM_T
, frr_data
->value
);
115 sr_val_set_str_data(sr_data
, SR_IDENTITYREF_T
, frr_data
->value
);
118 sr_val_set_str_data(sr_data
, SR_INSTANCEID_T
, frr_data
->value
);
121 sr_data
->type
= SR_INT8_T
;
122 sr_data
->data
.int8_val
= yang_str2int8(frr_data
->value
);
125 sr_data
->type
= SR_INT16_T
;
126 sr_data
->data
.int16_val
= yang_str2int16(frr_data
->value
);
129 sr_data
->type
= SR_INT32_T
;
130 sr_data
->data
.int32_val
= yang_str2int32(frr_data
->value
);
133 sr_data
->type
= SR_INT64_T
;
134 sr_data
->data
.int64_val
= yang_str2int64(frr_data
->value
);
137 sr_val_set_str_data(sr_data
, SR_STRING_T
, frr_data
->value
);
140 sr_data
->type
= SR_UINT8_T
;
141 sr_data
->data
.uint8_val
= yang_str2uint8(frr_data
->value
);
144 sr_data
->type
= SR_UINT16_T
;
145 sr_data
->data
.uint16_val
= yang_str2uint16(frr_data
->value
);
148 sr_data
->type
= SR_UINT32_T
;
149 sr_data
->data
.uint32_val
= yang_str2uint32(frr_data
->value
);
152 sr_data
->type
= SR_UINT64_T
;
153 sr_data
->data
.uint64_val
= yang_str2uint64(frr_data
->value
);
162 static int frr_sr_process_change(struct nb_config
*candidate
,
163 sr_change_oper_t sr_op
, sr_val_t
*sr_old_val
,
164 sr_val_t
*sr_new_val
)
166 struct nb_node
*nb_node
;
167 enum nb_operation nb_op
;
170 char value_str
[YANG_VALUE_MAXLEN
];
171 struct yang_data
*data
;
174 sr_data
= sr_new_val
? sr_new_val
: sr_old_val
;
177 xpath
= sr_data
->xpath
;
179 /* Non-presence container - nothing to do. */
180 if (sr_data
->type
== SR_CONTAINER_T
)
183 nb_node
= nb_node_find(xpath
);
185 flog_warn(EC_LIB_YANG_UNKNOWN_DATA_PATH
,
186 "%s: unknown data path: %s", __func__
, xpath
);
190 /* Map operation values. */
194 if (nb_operation_is_valid(NB_OP_CREATE
, nb_node
->snode
))
195 nb_op
= NB_OP_CREATE
;
196 else if (nb_operation_is_valid(NB_OP_MODIFY
, nb_node
->snode
)) {
197 nb_op
= NB_OP_MODIFY
;
199 /* Ignore list keys modifications. */
204 * When a list is deleted or one of its keys is changed, we are
205 * notified about the removal of all of its leafs, even the ones
206 * that are non-optional. We need to ignore these notifications.
208 if (!nb_operation_is_valid(NB_OP_DESTROY
, nb_node
->snode
))
211 nb_op
= NB_OP_DESTROY
;
217 flog_err(EC_LIB_DEVELOPMENT
,
218 "%s: unexpected operation %u [xpath %s]", __func__
,
223 sr_val_to_buff(sr_data
, value_str
, sizeof(value_str
));
224 data
= yang_data_new(xpath
, value_str
);
226 ret
= nb_candidate_edit(candidate
, nb_node
, nb_op
, xpath
, NULL
, data
);
227 yang_data_free(data
);
230 EC_LIB_NB_CANDIDATE_EDIT_ERROR
,
231 "%s: failed to edit candidate configuration: operation [%s] xpath [%s]",
232 __func__
, nb_operation_name(nb_op
), xpath
);
239 static int frr_sr_config_change_cb_verify(sr_session_ctx_t
*session
,
240 const char *module_name
,
243 sr_change_iter_t
*it
;
245 sr_change_oper_t sr_op
;
246 sr_val_t
*sr_old_val
, *sr_new_val
;
247 char xpath
[XPATH_MAXLEN
];
248 struct nb_config
*candidate
;
250 snprintf(xpath
, sizeof(xpath
), "/%s:*", module_name
);
251 ret
= sr_get_changes_iter(session
, xpath
, &it
);
252 if (ret
!= SR_ERR_OK
) {
253 flog_err(EC_LIB_LIBSYSREPO
,
254 "%s: sr_get_changes_iter() failed for xpath %s",
259 candidate
= nb_config_dup(running_config
);
261 while ((ret
= sr_get_change_next(session
, it
, &sr_op
, &sr_old_val
,
264 ret
= frr_sr_process_change(candidate
, sr_op
, sr_old_val
,
266 sr_free_val(sr_old_val
);
267 sr_free_val(sr_new_val
);
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
;
279 if (startup_config
) {
281 * sysrepod sends the entire startup configuration using a
282 * single event (SR_EV_ENABLED). This means we need to perform
283 * the full two-phase commit protocol in one go here.
285 ret
= nb_candidate_commit(candidate
, NB_CLIENT_SYSREPO
, NULL
,
289 * Validate the configuration changes and allocate all resources
290 * required to apply them.
292 ret
= nb_candidate_commit_prepare(candidate
, NB_CLIENT_SYSREPO
,
293 NULL
, NULL
, &transaction
);
296 /* Map northbound return code to sysrepo return code. */
300 case NB_ERR_NO_CHANGES
:
301 nb_config_free(candidate
);
304 return SR_ERR_LOCKED
;
305 case NB_ERR_RESOURCE
:
308 return SR_ERR_VALIDATION_FAILED
;
312 static int frr_sr_config_change_cb_apply(sr_session_ctx_t
*session
,
313 const char *module_name
)
315 /* Apply the transaction. */
317 struct nb_config
*candidate
= transaction
->config
;
319 nb_candidate_commit_apply(transaction
, true, NULL
);
320 nb_config_free(candidate
);
326 static int frr_sr_config_change_cb_abort(sr_session_ctx_t
*session
,
327 const char *module_name
)
329 /* Abort the transaction. */
331 struct nb_config
*candidate
= transaction
->config
;
333 nb_candidate_commit_abort(transaction
);
334 nb_config_free(candidate
);
340 /* Callback for changes in the running configuration. */
341 static int frr_sr_config_change_cb(sr_session_ctx_t
*session
,
342 const char *module_name
,
343 sr_notif_event_t sr_ev
, void *private_ctx
)
347 return frr_sr_config_change_cb_verify(session
, module_name
,
350 return frr_sr_config_change_cb_verify(session
, module_name
,
353 return frr_sr_config_change_cb_apply(session
, module_name
);
355 return frr_sr_config_change_cb_abort(session
, module_name
);
357 flog_err(EC_LIB_LIBSYSREPO
, "%s: unknown sysrepo event: %u",
359 return SR_ERR_INTERNAL
;
363 static int frr_sr_state_data_iter_cb(const struct lys_node
*snode
,
364 struct yang_translator
*translator
,
365 struct yang_data
*data
, void *arg
)
367 struct list
*elements
= arg
;
369 listnode_add(elements
, data
);
374 /* Callback for state retrieval. */
375 static int frr_sr_state_cb(const char *xpath
, sr_val_t
**values
,
376 size_t *values_cnt
, uint64_t request_id
,
377 const char *original_xpath
, void *private_ctx
)
379 struct list
*elements
;
380 struct yang_data
*data
;
381 struct listnode
*node
;
383 int ret
, count
, i
= 0;
385 elements
= yang_data_list_new();
386 if (nb_oper_data_iterate(xpath
, NULL
, NB_OPER_DATA_ITER_NORECURSE
,
387 frr_sr_state_data_iter_cb
, elements
)
389 flog_warn(EC_LIB_NB_OPERATIONAL_DATA
,
390 "%s: failed to obtain operational data [xpath %s]",
395 if (list_isempty(elements
))
398 count
= listcount(elements
);
399 ret
= sr_new_values(count
, &v
);
400 if (ret
!= SR_ERR_OK
) {
401 flog_err(EC_LIB_LIBSYSREPO
, "%s: sr_new_values(): %s", __func__
,
406 for (ALL_LIST_ELEMENTS_RO(elements
, node
, data
)) {
407 if (yang_data_frr2sr(data
, &v
[i
++]) != 0) {
408 flog_err(EC_LIB_SYSREPO_DATA_CONVERT
,
409 "%s: failed to convert data to sysrepo format",
417 list_delete(&elements
);
422 list_delete(&elements
);
429 static int frr_sr_config_rpc_cb(const char *xpath
, const sr_val_t
*sr_input
,
430 const size_t input_cnt
, sr_val_t
**sr_output
,
431 size_t *sr_output_cnt
, void *private_ctx
)
433 struct nb_node
*nb_node
;
436 struct yang_data
*data
;
437 size_t cb_output_cnt
;
440 nb_node
= nb_node_find(xpath
);
442 flog_warn(EC_LIB_YANG_UNKNOWN_DATA_PATH
,
443 "%s: unknown data path: %s", __func__
, xpath
);
444 return SR_ERR_INTERNAL
;
447 input
= yang_data_list_new();
448 output
= yang_data_list_new();
451 for (size_t i
= 0; i
< input_cnt
; i
++) {
452 char value_str
[YANG_VALUE_MAXLEN
];
454 sr_val_to_buff(&sr_input
[i
], value_str
, sizeof(value_str
));
456 data
= yang_data_new(xpath
, value_str
);
457 listnode_add(input
, data
);
460 /* Execute callback registered for this XPath. */
461 if (nb_callback_rpc(nb_node
, xpath
, input
, output
) != NB_OK
) {
462 flog_warn(EC_LIB_NB_CB_RPC
, "%s: rpc callback failed: %s",
464 ret
= SR_ERR_OPERATION_FAILED
;
468 /* Process output. */
469 if (listcount(output
) > 0) {
470 sr_val_t
*values
= NULL
;
471 struct listnode
*node
;
474 cb_output_cnt
= listcount(output
);
475 ret
= sr_new_values(cb_output_cnt
, &values
);
476 if (ret
!= SR_ERR_OK
) {
477 flog_err(EC_LIB_LIBSYSREPO
, "%s: sr_new_values(): %s",
478 __func__
, sr_strerror(ret
));
482 for (ALL_LIST_ELEMENTS_RO(output
, node
, data
)) {
483 if (yang_data_frr2sr(data
, &values
[i
++]) != 0) {
485 EC_LIB_SYSREPO_DATA_CONVERT
,
486 "%s: failed to convert data to Sysrepo format",
488 ret
= SR_ERR_INTERNAL
;
489 sr_free_values(values
, cb_output_cnt
);
495 *sr_output_cnt
= cb_output_cnt
;
499 /* Release memory. */
501 list_delete(&output
);
506 static int frr_sr_notification_send(const char *xpath
, struct list
*arguments
)
508 sr_val_t
*values
= NULL
;
509 size_t values_cnt
= 0;
512 if (arguments
&& listcount(arguments
) > 0) {
513 struct yang_data
*data
;
514 struct listnode
*node
;
517 values_cnt
= listcount(arguments
);
518 ret
= sr_new_values(values_cnt
, &values
);
519 if (ret
!= SR_ERR_OK
) {
520 flog_err(EC_LIB_LIBSYSREPO
, "%s: sr_new_values(): %s",
521 __func__
, sr_strerror(ret
));
525 for (ALL_LIST_ELEMENTS_RO(arguments
, node
, data
)) {
526 if (yang_data_frr2sr(data
, &values
[i
++]) != 0) {
528 EC_LIB_SYSREPO_DATA_CONVERT
,
529 "%s: failed to convert data to sysrepo format",
531 sr_free_values(values
, values_cnt
);
537 ret
= sr_event_notif_send(session
, xpath
, values
, values_cnt
,
538 SR_EV_NOTIF_DEFAULT
);
539 if (ret
!= SR_ERR_OK
) {
540 flog_err(EC_LIB_LIBSYSREPO
,
541 "%s: sr_event_notif_send() failed for xpath %s",
549 /* Code to integrate the sysrepo client into FRR main event loop. */
550 struct sysrepo_thread
{
551 struct thread
*thread
;
556 static struct sysrepo_thread
*frr_sr_fd_lookup(sr_fd_event_t event
, int fd
)
558 struct sysrepo_thread
*sr_thread
;
559 struct listnode
*node
;
561 for (ALL_LIST_ELEMENTS_RO(sysrepo_threads
, node
, sr_thread
)) {
562 if (sr_thread
->event
== event
&& sr_thread
->fd
== fd
)
569 static void frr_sr_fd_add(int event
, int fd
)
571 struct sysrepo_thread
*sr_thread
;
573 if (frr_sr_fd_lookup(event
, fd
) != NULL
)
576 sr_thread
= XCALLOC(MTYPE_SYSREPO
, sizeof(*sr_thread
));
577 sr_thread
->event
= event
;
579 listnode_add(sysrepo_threads
, sr_thread
);
582 case SR_FD_INPUT_READY
:
583 thread_add_read(master
, frr_sr_read_cb
, NULL
, fd
,
586 case SR_FD_OUTPUT_READY
:
587 thread_add_write(master
, frr_sr_write_cb
, NULL
, fd
,
595 static void frr_sr_fd_free(struct sysrepo_thread
*sr_thread
)
597 THREAD_OFF(sr_thread
->thread
);
598 XFREE(MTYPE_SYSREPO
, sr_thread
);
601 static void frr_sr_fd_del(int event
, int fd
)
603 struct sysrepo_thread
*sr_thread
;
605 sr_thread
= frr_sr_fd_lookup(event
, fd
);
609 listnode_delete(sysrepo_threads
, sr_thread
);
610 frr_sr_fd_free(sr_thread
);
613 static void frr_sr_fd_update(sr_fd_change_t
*fd_change_set
,
614 size_t fd_change_set_cnt
)
616 for (size_t i
= 0; i
< fd_change_set_cnt
; i
++) {
617 int fd
= fd_change_set
[i
].fd
;
618 int event
= fd_change_set
[i
].events
;
620 if (event
!= SR_FD_INPUT_READY
&& event
!= SR_FD_OUTPUT_READY
)
623 switch (fd_change_set
[i
].action
) {
624 case SR_FD_START_WATCHING
:
625 frr_sr_fd_add(event
, fd
);
627 case SR_FD_STOP_WATCHING
:
628 frr_sr_fd_del(event
, fd
);
636 static int frr_sr_read_cb(struct thread
*thread
)
638 int fd
= THREAD_FD(thread
);
639 sr_fd_change_t
*fd_change_set
= NULL
;
640 size_t fd_change_set_cnt
= 0;
643 ret
= sr_fd_event_process(fd
, SR_FD_INPUT_READY
, &fd_change_set
,
645 if (ret
!= SR_ERR_OK
) {
646 flog_err(EC_LIB_LIBSYSREPO
, "%s: sr_fd_event_process(): %s",
647 __func__
, sr_strerror(ret
));
652 thread_add_read(master
, frr_sr_read_cb
, NULL
, fd
, &thread
);
654 frr_sr_fd_update(fd_change_set
, fd_change_set_cnt
);
660 static int frr_sr_write_cb(struct thread
*thread
)
662 int fd
= THREAD_FD(thread
);
663 sr_fd_change_t
*fd_change_set
= NULL
;
664 size_t fd_change_set_cnt
= 0;
667 ret
= sr_fd_event_process(fd
, SR_FD_OUTPUT_READY
, &fd_change_set
,
669 if (ret
!= SR_ERR_OK
) {
670 flog_err(EC_LIB_LIBSYSREPO
, "%s: sr_fd_event_process(): %s",
671 __func__
, sr_strerror(ret
));
675 frr_sr_fd_update(fd_change_set
, fd_change_set_cnt
);
681 static void frr_sr_subscribe_config(struct yang_module
*module
)
685 ret
= sr_module_change_subscribe(
686 session
, module
->name
, frr_sr_config_change_cb
, NULL
, 0,
687 SR_SUBSCR_DEFAULT
| SR_SUBSCR_EV_ENABLED
,
688 &module
->sr_subscription
);
689 if (ret
!= SR_ERR_OK
)
690 flog_err(EC_LIB_LIBSYSREPO
, "sr_module_change_subscribe(): %s",
694 static int frr_sr_subscribe_state(const struct lys_node
*snode
, void *arg
)
696 struct yang_module
*module
= arg
;
697 struct nb_node
*nb_node
;
700 if (!CHECK_FLAG(snode
->flags
, LYS_CONFIG_R
))
701 return YANG_ITER_CONTINUE
;
702 /* We only need to subscribe to the root of the state subtrees. */
703 if (snode
->parent
&& CHECK_FLAG(snode
->parent
->flags
, LYS_CONFIG_R
))
704 return YANG_ITER_CONTINUE
;
706 nb_node
= snode
->priv
;
708 DEBUGD(&nb_dbg_client_sysrepo
, "%s: providing data to '%s'", __func__
,
711 ret
= sr_dp_get_items_subscribe(
712 session
, nb_node
->xpath
, frr_sr_state_cb
, NULL
,
713 SR_SUBSCR_CTX_REUSE
, &module
->sr_subscription
);
714 if (ret
!= SR_ERR_OK
)
715 flog_err(EC_LIB_LIBSYSREPO
, "sr_dp_get_items_subscribe(): %s",
718 return YANG_ITER_CONTINUE
;
721 static int frr_sr_subscribe_rpc(const struct lys_node
*snode
, void *arg
)
723 struct yang_module
*module
= arg
;
724 struct nb_node
*nb_node
;
727 if (snode
->nodetype
!= LYS_RPC
)
728 return YANG_ITER_CONTINUE
;
730 nb_node
= snode
->priv
;
732 DEBUGD(&nb_dbg_client_sysrepo
, "%s: providing RPC to '%s'", __func__
,
735 ret
= sr_rpc_subscribe(session
, nb_node
->xpath
, frr_sr_config_rpc_cb
,
736 NULL
, SR_SUBSCR_CTX_REUSE
,
737 &module
->sr_subscription
);
738 if (ret
!= SR_ERR_OK
)
739 flog_err(EC_LIB_LIBSYSREPO
, "sr_rpc_subscribe(): %s",
742 return YANG_ITER_CONTINUE
;
745 static int frr_sr_subscribe_action(const struct lys_node
*snode
, void *arg
)
747 struct yang_module
*module
= arg
;
748 struct nb_node
*nb_node
;
751 if (snode
->nodetype
!= LYS_ACTION
)
752 return YANG_ITER_CONTINUE
;
754 nb_node
= snode
->priv
;
756 DEBUGD(&nb_dbg_client_sysrepo
, "%s: providing action to '%s'", __func__
,
759 ret
= sr_action_subscribe(session
, nb_node
->xpath
, frr_sr_config_rpc_cb
,
760 NULL
, SR_SUBSCR_CTX_REUSE
,
761 &module
->sr_subscription
);
762 if (ret
!= SR_ERR_OK
)
763 flog_err(EC_LIB_LIBSYSREPO
, "sr_action_subscribe(): %s",
766 return YANG_ITER_CONTINUE
;
772 "[no] debug northbound client sysrepo",
775 "Northbound debugging\n"
776 "Northbound client\n"
779 uint32_t mode
= DEBUG_NODE2MODE(vty
->node
);
780 bool no
= strmatch(argv
[0]->text
, "no");
782 DEBUG_MODE_SET(&nb_dbg_client_sysrepo
, mode
, !no
);
787 static int frr_sr_debug_config_write(struct vty
*vty
)
789 if (DEBUG_MODE_CHECK(&nb_dbg_client_sysrepo
, DEBUG_MODE_CONF
))
790 vty_out(vty
, "debug northbound client sysrepo\n");
795 static int frr_sr_debug_set_all(uint32_t flags
, bool set
)
797 DEBUG_FLAGS_SET(&nb_dbg_client_sysrepo
, flags
, set
);
799 /* If all modes have been turned off, don't preserve options. */
800 if (!DEBUG_MODE_CHECK(&nb_dbg_client_sysrepo
, DEBUG_MODE_ALL
))
801 DEBUG_CLEAR(&nb_dbg_client_sysrepo
);
806 static void frr_sr_cli_init(void)
808 hook_register(nb_client_debug_config_write
, frr_sr_debug_config_write
);
809 hook_register(nb_client_debug_set_all
, frr_sr_debug_set_all
);
811 install_element(ENABLE_NODE
, &debug_nb_sr_cmd
);
812 install_element(CONFIG_NODE
, &debug_nb_sr_cmd
);
815 /* FRR's Sysrepo initialization. */
816 static int frr_sr_init(const char *program_name
)
818 struct yang_module
*module
;
821 sysrepo_threads
= list_new();
823 ret
= sr_fd_watcher_init(&sysrepo_fd
, NULL
);
824 if (ret
!= SR_ERR_OK
) {
825 flog_err(EC_LIB_SYSREPO_INIT
, "%s: sr_fd_watcher_init(): %s",
826 __func__
, sr_strerror(ret
));
830 /* Connect to Sysrepo. */
831 ret
= sr_connect(program_name
, SR_CONN_DEFAULT
, &connection
);
832 if (ret
!= SR_ERR_OK
) {
833 flog_err(EC_LIB_SYSREPO_INIT
, "%s: sr_connect(): %s", __func__
,
839 ret
= sr_session_start(connection
, SR_DS_RUNNING
, SR_SESS_DEFAULT
,
841 if (ret
!= SR_ERR_OK
) {
842 flog_err(EC_LIB_SYSREPO_INIT
, "%s: sr_session_start(): %s",
843 __func__
, sr_strerror(ret
));
847 /* Perform subscriptions. */
848 RB_FOREACH (module
, yang_modules
, &yang_modules
) {
849 frr_sr_subscribe_config(module
);
850 yang_snodes_iterate_module(module
->info
, frr_sr_subscribe_state
,
852 yang_snodes_iterate_module(module
->info
, frr_sr_subscribe_rpc
,
854 yang_snodes_iterate_module(module
->info
,
855 frr_sr_subscribe_action
, 0, module
);
858 hook_register(nb_notification_send
, frr_sr_notification_send
);
860 frr_sr_fd_add(SR_FD_INPUT_READY
, sysrepo_fd
);
870 static int frr_sr_finish(void)
872 struct yang_module
*module
;
874 RB_FOREACH (module
, yang_modules
, &yang_modules
) {
875 if (!module
->sr_subscription
)
877 sr_unsubscribe(session
, module
->sr_subscription
);
881 sr_session_stop(session
);
883 sr_disconnect(connection
);
885 sysrepo_threads
->del
= (void (*)(void *))frr_sr_fd_free
;
886 list_delete(&sysrepo_threads
);
887 sr_fd_watcher_cleanup();
892 static int frr_sr_module_late_init(struct thread_master
*tm
)
896 if (frr_sr_init(frr_get_progname()) < 0) {
897 flog_err(EC_LIB_SYSREPO_INIT
,
898 "failed to initialize the Sysrepo module");
902 hook_register(frr_fini
, frr_sr_finish
);
908 static int frr_sr_module_init(void)
910 hook_register(frr_late_init
, frr_sr_module_late_init
);
915 FRR_MODULE_SETUP(.name
= "frr_sysrepo", .version
= FRR_VERSION
,
916 .description
= "FRR sysrepo integration module",
917 .init
= frr_sr_module_init
, )