-
Notifications
You must be signed in to change notification settings - Fork 227
/
sysrepo.h
1874 lines (1703 loc) · 84.2 KB
/
sysrepo.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* @file sysrepo.h
* @author Michal Vasko <mvasko@cesnet.cz>
* @brief public API sysrepo header
*
* @copyright
* Copyright (c) 2018 - 2022 Deutsche Telekom AG.
* Copyright (c) 2018 - 2022 CESNET, z.s.p.o.
*
* This source code is licensed under BSD 3-Clause License (the "License").
* You may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://opensource.org/licenses/BSD-3-Clause
*/
#ifndef _SYSREPO_H
#define _SYSREPO_H
#include <stdint.h>
#include <stdlib.h>
#include <sys/types.h>
#include <time.h>
#include <unistd.h>
#include <libyang/libyang.h>
#include "sysrepo_types.h"
#ifdef __cplusplus
extern "C" {
#endif
////////////////////////////////////////////////////////////////////////////////
// Logging API
////////////////////////////////////////////////////////////////////////////////
/**
* @defgroup log_api Logging API
* @{
*/
/**
* @brief Returns the error message corresponding to the error code.
*
* @param[in] err_code Error code.
* @return Error message (statically allocated, do not free).
*/
const char *sr_strerror(int err_code);
/**
* @brief Enables / disables / changes log level (verbosity) of logging to
* standard error output.
*
* By default, logging to stderr is disabled. Setting log level to any value
* other than ::SR_LL_NONE enables the logging to stderr. Setting log level
* back to ::SR_LL_NONE disables the logging to stderr.
*
* @param[in] log_level Requested log level (verbosity).
*/
void sr_log_stderr(sr_log_level_t log_level);
/**
* @brief Learn current standard error output log level.
*
* @return stderr log level.
*/
sr_log_level_t sr_log_get_stderr(void);
/**
* @brief Enables / disables / changes log level (verbosity) of logging to system log.
*
* By default, logging into syslog is disabled. Setting log level to any value
* other than ::SR_LL_NONE enables the logging into syslog. Setting log level
* back to ::SR_LL_NONE disables the logging into syslog.
*
* Library messages are logged with LOG_USER facility and plugin (syrepo-plugind) messages are
* logged with LOG_DAEMON facility.
*
* @note Please note that enabling logging into syslog will overwrite your syslog
* connection settings (calls openlog), if you are connected to syslog already.
*
* @param[in] app_name Name of the application. If not set, "sysrepo" will be used.
* @param[in] log_level Requested log level (verbosity).
*/
void sr_log_syslog(const char *app_name, sr_log_level_t log_level);
/**
* @brief Learn current system log log level.
*
* @return syslog log level.
*/
sr_log_level_t sr_log_get_syslog(void);
/**
* @brief Sets callback that will be called when a log entry would be populated.
* Callback will be called for every message __regardless__ of any log level.
*
* @param[in] log_callback Callback to be called when a log entry would populated.
*/
void sr_log_set_cb(sr_log_cb log_callback);
/** @} logging */
////////////////////////////////////////////////////////////////////////////////
// Connection / Session Management
////////////////////////////////////////////////////////////////////////////////
/**
* @defgroup conn_sess_api Connection and Session API
* @{
*/
/**
* @brief Connects to the sysrepo datastore.
*
* @note Do not use `fork(2)` after creating a connection. Sysrepo internally stores the connection
* ID of every connection. Forking will duplicate the connection and ID resulting in a mismatch.
*
* @param[in] opts Connection options.
* @param[out] conn Created connection.
* @return Error code (::SR_ERR_OK on success).
*/
int sr_connect(const sr_conn_options_t opts, sr_conn_ctx_t **conn);
/**
* @brief Disconnect from the sysrepo datastore.
*
* Cleans up and frees connection context allocated by ::sr_connect. All sessions and subscriptions
* started within the connection will be automatically stopped and cleaned up too.
*
* @note On error the function should be retried and must eventually succeed.
*
* @param[in] conn Connection acquired with ::sr_connect call to free.
* @return Error code (::SR_ERR_OK on success).
*/
int sr_disconnect(sr_conn_ctx_t *conn);
/**
* @brief Get the _libyang_ context used by a connection. Can be used in an application for working with data
* and schemas.
*
* @note This context **must not** be changed. Also, to prevent the context from being destroyed by sysrepo,
* it is locked and after no longer needing the context ::sr_release_context() must be called. Otherwise,
* API functions changing the context will fail with time out.
*
* @param[in] conn Connection to use.
* @return Const libyang context.
*/
const struct ly_ctx *sr_acquire_context(sr_conn_ctx_t *conn);
/**
* @brief Get the _libyang_ context used by a connection. Can be used in an application for working with data
* and schemas.
*
* Similar to and interchangeable with ::sr_acquire_context().
*
* @param[in] session Session whose connection to use.
* @return Const libyang context.
*/
const struct ly_ctx *sr_session_acquire_context(sr_session_ctx_t *session);
/**
* @brief Release _libyang_ context obtained from a session/connection.
*
* @note Must be called for each ::sr_acquire_context() call.
*
* @param[in] conn Connection to use.
*/
void sr_release_context(sr_conn_ctx_t *conn);
/**
* @brief Release _libyang_ context obtained from a session/connection.
*
* Similar to and interchangeable with ::sr_release_context().
*
* @param[in] session Session whose connection to use.
*/
void sr_session_release_context(sr_session_ctx_t *session);
/**
* @brief Get content ID of the current YANG module set. It conforms to the requirements for ietf-yang-library
* "content-id" node value.
*
* @param[in] conn Connection to use.
* @return Content ID.
*/
uint32_t sr_get_content_id(sr_conn_ctx_t *conn);
/**
* @brief Get loaded plugins of a connection.
*
* @param[in] conn Connection to use.
* @param[out] ds_plugins Optional pointer to array of datastore plugins ended by NULL.
* @param[out] ntf_plugins Optional pointer to array of notification plugins ended by NULL.
* @return Error code (::SR_ERR_OK on success).
*/
int sr_get_plugins(sr_conn_ctx_t *conn, const char ***ds_plugins, const char ***ntf_plugins);
/**
* @brief Get the sysrepo SUPERUSER UID.
*
* @return Sysrepo SU UID.
*/
uid_t sr_get_su_uid(void);
/**
* @brief Deprecated, use ::sr_discard_items().
*/
int sr_discard_oper_changes(sr_conn_ctx_t *conn, sr_session_ctx_t *session, const char *xpath, uint32_t timeout_ms);
/**
* @brief Start a new session.
*
* @param[in] conn Connection to use.
* @param[in] datastore Datastore on which to operate, can be later changed using
* ::sr_session_switch_ds().
* @param[out] session Created session.
* @return Error code (::SR_ERR_OK on success).
*/
int sr_session_start(sr_conn_ctx_t *conn, const sr_datastore_t datastore, sr_session_ctx_t **session);
/**
* @brief Stop the session and releases resources tied to it.
*
* Also releases any locks held and frees subscriptions created (only) by this session.
*
* @note On error the function should be retried and must eventually succeed.
* Subscriptions, even if they no longer handle any events are **never** freed and
* should be freed manually using ::sr_unsubscribe.
*
* @param[in] session Session to free.
* @return Error code (::SR_ERR_OK on success).
*/
int sr_session_stop(sr_session_ctx_t *session);
/**
* @brief Unsubscribe all subscriptions created by this session.
*
* @note Subscriptions, even if they no longer handle any events are **never** freed
* and should be freed manually using ::sr_unsubscribe.
*
* @param[in] session Session to use.
* @return Error code (::SR_ERR_OK on success).
*/
int sr_session_unsubscribe(sr_session_ctx_t *session);
/**
* @brief Use notification buffering for the session.
*
* When a notification is sent using this session for
* a module that supports replay (notification should be stored),
* the notification function does not wait until it is stored
* but delegates this work to a special thread and returns.
*
* @param[in] session Session (not [DS](@ref sr_datastore_t)-specific) whose notifications will be buffered.
* @return Error code (::SR_ERR_OK on success).
*/
int sr_session_notif_buffer(sr_session_ctx_t *session);
/**
* @brief Change datastore which the session operates on. All subsequent
* calls will be issued on the chosen datastore. Previous calls are not
* affected.
*
* @param[in] session Session to modify.
* @param[in] ds New datastore that will be operated on.
* @return Error code (::SR_ERR_OK on success).
*/
int sr_session_switch_ds(sr_session_ctx_t *session, sr_datastore_t ds);
/**
* @brief Learn the datastore a session operates on.
*
* @param[in] session Session to use.
* @return Datastore of the session.
*/
sr_datastore_t sr_session_get_ds(sr_session_ctx_t *session);
/**
* @brief Set event originator name used for all events sent on this session.
* It can then be read from the implicit event session in the callbacks using ::sr_session_get_orig_name().
* This name should be used for interpreting the data set by ::sr_session_push_orig_data().
*
* The following originator names are well-known:
*
* - `netopeer2` for the [NETCONF server](https://github.com/CESNET/Netopeer2/#sysrepo-callbacks)
* - `rousette` for the [RESTCONF server](https://github.com/CESNET/rousette)
* - `sysrepo-cli` for the direct sysrepo mode of the [interactive CLI](https://github.com/CESNET/netconf-cli)
*
* @param[in] session Session (not [DS](@ref sr_datastore_t)-specific) to use.
* @param[in] orig_name Arbitrary originator name.
* @return Error code (::SR_ERR_OK on success).
*/
int sr_session_set_orig_name(sr_session_ctx_t *session, const char *orig_name);
/**
* @brief Get event originator name.
*
* @param[in] session Implicit session provided in a callback.
* @return Originator name if set, empty string "" otherwise.
*/
const char *sr_session_get_orig_name(sr_session_ctx_t *session);
/**
* @brief Push (add) another chunk of event originator data used for all events sent on this session.
* Its meaning is specific to the originator name (which must be [set](@ref sr_session_set_orig_name)
* prior to calling this function) and can be read from the implicit event session in the callbacks
* using ::sr_session_get_orig_data().
*
* @param[in] session Session (not [DS](@ref sr_datastore_t)-specific) to use.
* @param[in] size Size of the @p data chunk.
* @param[in] data Pointer to an opaque data chunk.
* @return Error code (::SR_ERR_OK on success).
*/
int sr_session_push_orig_data(sr_session_ctx_t *session, uint32_t size, const void *data);
/**
* @brief Remove all pushed event originator data.
*
* @param[in] session Session (not [DS](@ref sr_datastore_t)-specific) to use.
*/
void sr_session_del_orig_data(sr_session_ctx_t *session);
/**
* @brief Get a specific chunk of event originator data in a callback.
*
* @param[in] session Implicit session provided in a callback.
* @param[in] idx Index of the data chunk, starts at 0.
* @param[out] size Optional size of the @p data chunk.
* @param[out] data Pointer to an opaque data chunk.
* @return Error code (::SR_ERR_OK on success).
*/
int sr_session_get_orig_data(sr_session_ctx_t *session, uint32_t idx, uint32_t *size, const void **data);
/**
* @brief Retrieve information about the error that has occurred
* during the last operation executed within provided session.
*
* @param[in] session Session (not [DS](@ref sr_datastore_t)-specific) to use.
* @param[out] error_info Detailed error information. Be aware that
* returned pointer may change by the next API call executed within the provided
* session. Do not free or modify returned values.
* @return Error code (::SR_ERR_OK on success).
*/
int sr_session_get_error(sr_session_ctx_t *session, const sr_error_info_t **error_info);
/**
* @brief Copy the first error (if any) from a session to a callback session.
*
* @param[in] src_session Session (not [DS](@ref sr_datastore_t)-specific) to read the error from.
* @param[in] trg_session Implicit session provided in a callback.
* @return Error code (::SR_ERR_OK on success).
*/
int sr_session_dup_error(sr_session_ctx_t *src_session, sr_session_ctx_t *trg_session);
/**
* @brief Set an error for a failed callback communicated back to the originator.
* Does not print the message.
*
* @note Intended for diff-check, change, RPC/action, or operational callbacks to be used
* on the provided session.
*
* @note Error format @p err_format_name should be used for interpreting the error data set by
* ::sr_session_push_error_data(). There are some well-known error formats defined and those errors can be
* written/read using [helper utility functions](@ref utils_error_format).
*
* @param[in] session Implicit session provided in a callback.
* @param[in] err_format_name Optional arbitrary error format identifier, set its error data using
* ::sr_session_push_error_data().
* @param[in] err_code Error code of the error.
* @param[in] err_msg_format Error message format.
* @param[in] ... Error message format arguments.
*/
int sr_session_set_error(sr_session_ctx_t *session, const char *err_format_name, sr_error_t err_code,
const char *err_msg_format, ...);
/**
* @brief Deprecated, use ::sr_session_set_error().
*/
int sr_session_set_error_message(sr_session_ctx_t *session, const char *format, ...);
/**
* @brief Deprecated, use ::sr_session_set_error().
*/
int sr_session_set_error_format(sr_session_ctx_t *session, const char *error_format);
/**
* @brief Push (add) another chunk of error data for a failed callback communicated back to the originator.
* Its meaning is specific to the error data format (which must be set prior to calling this function) identifier and
* can be read from the error structure by the originator using ::sr_get_error_data().
*
* @param[in] session Implicit session provided in a callback.
* @param[in] size Size of the error @p data chunk.
* @param[in] data Pointer to an opaque error data chunk.
* @return Error code (::SR_ERR_OK on success).
*/
int sr_session_push_error_data(sr_session_ctx_t *session, uint32_t size, const void *data);
/**
* @brief Get a specific chunk of error data.
*
* If the error is a well-known one, it is possible to use [helper utility functions](@ref utils_error_format)
* instead of repeatedly calling this function.
*
* @param[in] err Error structure to use.
* @param[in] idx Index of the error data chunk, starts at 0.
* @param[out] size Optional size of the error @p data chunk.
* @param[out] data Pointer to an opaque error data chunk.
* @return Error code (::SR_ERR_OK on success).
*/
int sr_get_error_data(const sr_error_info_err_t *err, uint32_t idx, uint32_t *size, const void **data);
/**
* @brief Return the assigned session ID of the sysrepo session.
*
* @param [in] session Session (not [DS](@ref sr_datastore_t)-specific) to use.
* @return sysrepo SID or 0 in case of error.
*/
uint32_t sr_session_get_id(sr_session_ctx_t *session);
/**
* @brief Set the effective user of a session to a different one than the process owner.
*
* Required SUPERUSER access.
*
* @param[in] session Session (not [DS](@ref sr_datastore_t)-specific) to change.
* @param[in] user System user.
* @return Error code (::SR_ERR_OK on success).
*/
int sr_session_set_user(sr_session_ctx_t *session, const char *user);
/**
* @brief Get the effective user of a session.
*
* @param[in] session Session (not [DS](@ref sr_datastore_t)-specific) to use.
* @return Session user.
*/
const char *sr_session_get_user(sr_session_ctx_t *session);
/**
* @brief Get the connection the session was created on.
*
* @param[in] session Session (not [DS](@ref sr_datastore_t)-specific) to use.
* @return Sysrepo connection.
*/
sr_conn_ctx_t *sr_session_get_connection(sr_session_ctx_t *session);
/** @} connsess */
////////////////////////////////////////////////////////////////////////////////
// Schema Manipulation API
////////////////////////////////////////////////////////////////////////////////
/**
* @defgroup schema_api Schema API
* @{
*/
/**
* @brief Get the common path prefix for all sysrepo files.
*
* @note If a specific path was changed during compilation, it does not use this
* path prefix.
*
* @return Sysrepo repository path.
*/
const char *sr_get_repo_path(void);
/**
* @brief Install a new schema (module) into sysrepo.
*
* For all datastores and notifications the default plugins are used.
*
* @param[in] conn Connection to use.
* @param[in] schema_path Path to the new schema. Can have either YANG or YIN extension/format.
* @param[in] search_dirs Optional search directories for import schemas, supports the format `<dir>[:<dir>]*`.
* @param[in] features Optional array of enabled features ended with NULL. Feature '*' enables them all.
* @return Error code (::SR_ERR_OK on success).
*/
int sr_install_module(sr_conn_ctx_t *conn, const char *schema_path, const char *search_dirs, const char **features);
/**
* @brief Get the default datastore plugins.
*
* New modules without explicitly set DS plugins will use these plugins. Use ::sr_install_module2 to install a module
* with a custom set of datastore plugins.
*
* @return Default datastore plugin structure.
*/
const sr_module_ds_t *sr_get_module_ds_default(void);
/**
* @brief Install a new schema (module) into sysrepo with all the available options.
*
* Any initial data are used as `running`, `startup`, and `factory-default` datastore data. If not set,
* the datastore plugin will be used to get the initial data for each datastore, which should generally be empty
* but may not be for custom DS plugins.
*
* @param[in] conn Connection to use.
* @param[in] schema_path Path to the new schema. Can have either YANG or YIN extension/format.
* @param[in] search_dirs Optional search directories for import schemas, supports the format `<dir>[:<dir>]*`.
* @param[in] features Optional array of enabled features ended with NULL, all disabled by default. Feature '*' enables
* them all.
* @param[in] module_ds Optional datastore implementation plugin names for each datastore, NULL for all defaults.
* If only ::SR_DS_RUNNING plugin name is NULL, it is disabled and effectively always mirrors ::SR_DS_STARTUP.
* @param[in] owner Optional initial owner of the module data, process user by default.
* @param[in] group Optional initial group of the module data, process group by default.
* @param[in] perm Optional initial permissions of the module data, otherwise system defaults are applied.
* @param[in] data Optional initial data in @p format to set, only if @p data_path is not set.
* @param[in] data_path Optional path to an initial data file in @p format to set, only if @p data is not set.
* @param[in] format Format of @p data or @p data_path file.
* @return Error code (::SR_ERR_OK on success).
*/
int sr_install_module2(sr_conn_ctx_t *conn, const char *schema_path, const char *search_dirs, const char **features,
const sr_module_ds_t *module_ds, const char *owner, const char *group, mode_t perm, const char *data,
const char *data_path, LYD_FORMAT format);
/**
* @brief Install new schemas (modules) into sysrepo in a batch.
*
* For all datastores and notifications the default plugins are used.
*
* @param[in] conn Connection to use.
* @param[in] schema_paths Array of paths to the new schemas terminated by NULL. Can have either YANG or YIN extension/format.
* @param[in] search_dirs Optional search directories for import schemas, supports the format `<dir>[:<dir>]*`.
* @param[in] features Array of the same length as @p schema_paths (minus the last NULL). Each item is an array of
* enabled features ended with NULL for the @p schema_paths on the same index. Can be NULL for leaving all
* the features disabled.
* @return Error code (::SR_ERR_OK on success).
*/
int sr_install_modules(sr_conn_ctx_t *conn, const char **schema_paths, const char *search_dirs,
const char ***features);
/**
* @brief Install new schemas (modules) into sysrepo in a batch with all the available options.
*
* See ::sr_install_module2 for details.
*
* @param[in] conn Connection to use.
* @param[in] modules Array of new modules to be installed with all their information.
* @param[in] module_count Count of @p modules.
* @param[in] search_dirs Optional search directories for import schemas, supports the format `<dir>[:<dir>]*`.
* @param[in] data Optional initial data of all the modules in @p format to set, only if @p data_path is not set.
* @param[in] data_path Optional path to an initial data file of all the modules in @p format to set, only if @p data
* is not set.
* @param[in] format Format of @p data or @p data_path file.
* @return Error code (::SR_ERR_OK on success).
*/
int sr_install_modules2(sr_conn_ctx_t *conn, const sr_install_mod_t *modules, uint32_t module_count,
const char *search_dirs, const char *data, const char *data_path, LYD_FORMAT format);
/**
* @brief Remove an installed module from sysrepo.
*
* Required WRITE access.
*
* @param[in] conn Connection to use.
* @param[in] module_name Name of the module to remove.
* @param[in] force If there are other installed modules depending on @p module_name, remove them, too.
* @return Error code (::SR_ERR_OK on success).
*/
int sr_remove_module(sr_conn_ctx_t *conn, const char *module_name, int force);
/**
* @brief Remove installed modules from sysrepo.
*
* Required WRITE access.
*
* @param[in] conn Connection to use.
* @param[in] module_names Array of names of modules to remove terminated by NULL.
* @param[in] force If there are other installed modules depending on @p module_names, remove them, too.
* @return Error code (::SR_ERR_OK on success).
*/
int sr_remove_modules(sr_conn_ctx_t *conn, const char **module_names, int force);
/**
* @brief Update an installed schema (module) to a new revision.
*
* Required WRITE access.
*
* @param[in] conn Connection to use.
* @param[in] schema_path Path to the updated schema. Can have either YANG or YIN extension/format.
* @param[in] search_dirs Optional search directories for import schemas, supports the format `<dir>[:<dir>]*`.
* @return Error code (::SR_ERR_OK on success).
*/
int sr_update_module(sr_conn_ctx_t *conn, const char *schema_path, const char *search_dirs);
/**
* @brief Update installed schemas (modules) to new revisions in a batch.
*
* Required WRITE access.
*
* @param[in] conn Connection to use.
* @param[in] schema_paths Array of paths to the new schemas terminated by NULL. Can have either YANG or YIN extension/format.
* @param[in] search_dirs Optional search directories for import schemas, supports the format `<dir>[:<dir>]*`.
* @return Error code (::SR_ERR_OK on success).
*/
int sr_update_modules(sr_conn_ctx_t *conn, const char **schema_paths, const char *search_dirs);
/**
* @brief Change module replay support.
*
* @param[in] conn Connection to use.
* @param[in] module_name Name of the module to change. NULL to change all the modules.
* @param[in] enable 0 to disable, non-zero to enable.
* @return Error code (::SR_ERR_OK on success).
*/
int sr_set_module_replay_support(sr_conn_ctx_t *conn, const char *module_name, int enable);
/**
* @brief Learn replay support of a module.
*
* @param[in] conn Connection to use.
* @param[in] module_name Name of the module to check.
* @param[out] earliest_notif Optional timestamp of the earliest stored notification, zeroed if none are stored. Can
* be set even if @p enabled is false when replay was enabled in the past.
* @param[out] enabled Whether replay support is enabled or disabled.
* @return Error code (::SR_ERR_OK on success).
*/
int sr_get_module_replay_support(sr_conn_ctx_t *conn, const char *module_name, struct timespec *earliest_notif,
int *enabled);
/**
* @brief Change module permissions.
*
* @param[in] conn Connection to use.
* @param[in] module_name Name of the module to change, NULL for all the modules.
* @param[in] mod_ds Affected datastore, ::sr_datastore_t value or ::SR_MOD_DS_NOTIF.
* @param[in] owner Optional, new owner of the module.
* @param[in] group Optional, new group of the module.
* @param[in] perm Optional, new permissions of the module.
* @return Error code (::SR_ERR_OK on success).
*/
int sr_set_module_ds_access(sr_conn_ctx_t *conn, const char *module_name, int mod_ds, const char *owner,
const char *group, mode_t perm);
/**
* @brief Learn about module permissions.
*
* @param[in] conn Connection to use.
* @param[in] module_name Name of the module to use.
* @param[in] mod_ds Affected datastore, ::sr_datastore_t value or ::SR_MOD_DS_NOTIF.
* @param[out] owner Optional, read the owner of the module.
* @param[out] group Optional, read the group of the module.
* @param[out] perm Optional, read the permissions of the module.
* @return Error code (::SR_ERR_OK on success).
*/
int sr_get_module_ds_access(sr_conn_ctx_t *conn, const char *module_name, int mod_ds, char **owner, char **group,
mode_t *perm);
/**
* @brief Check whether the current application has read/write access to a module.
*
* @param[in] conn Connection to use.
* @param[in] module_name Name of the module to use.
* @param[in] mod_ds Affected datastore, ::sr_datastore_t value or ::SR_MOD_DS_NOTIF.
* @param[out] read Optional, set if read access was granted.
* @param[out] write Optional, set if write access was granted.
* @return Error code (::SR_ERR_OK on success).
*/
int sr_check_module_ds_access(sr_conn_ctx_t *conn, const char *module_name, int mod_ds, int *read, int *write);
/**
* @brief Enable a module feature.
*
* Required WRITE access.
*
* @param[in] conn Connection to use.
* @param[in] module_name Name of the module to change.
* @param[in] feature_name Name of the feature to enable.
* @return Error code (::SR_ERR_OK on success).
*/
int sr_enable_module_feature(sr_conn_ctx_t *conn, const char *module_name, const char *feature_name);
/**
* @brief Disable a module feature.
*
* Required WRITE access.
*
* @param[in] conn Connection to use.
* @param[in] module_name Name of the module to change.
* @param[in] feature_name Name of the feature to disable.
* @return Error code (::SR_ERR_OK on success).
*/
int sr_disable_module_feature(sr_conn_ctx_t *conn, const char *module_name, const char *feature_name);
/**
* @brief Get internal sysrepo data tree, which holds detailed information about installed modules. It should
* not be needed except for some specific use-cases. These data are from the _sysrepo_ module found in
* `modules/sysrepo.yang`.
*
* @param[in] conn Connection to use.
* @param[out] sysrepo_data Sysrepo internal data tree.
* @return Error code (::SR_ERR_OK on success).
*/
int sr_get_module_info(sr_conn_ctx_t *conn, sr_data_t **sysrepo_data);
/**
* @brief Check whether a module is an internal *libyang* or *sysrepo* module. Evaluates to true
* for all the modules that are installed by default when no modules were explicitly added.
*
* @param[in] ly_mod Module to check.
* @return true (0) for an internal module.
* @return false (non-zero) for other modules.
*/
int sr_is_module_internal(const struct lys_module *ly_mod);
/** @} schema */
////////////////////////////////////////////////////////////////////////////////
// Data Retrieval API (get / get-config functionality)
////////////////////////////////////////////////////////////////////////////////
/**
* @defgroup get_data_api Getting Data API
* @{
*/
/**
* @brief Retrieve a single data element selected by the provided path.
* Data are represented as ::sr_val_t structures.
*
* If the path identifies an empty leaf, a list or a container, the value
* has no data filled in and its type is set properly
* (::SR_LEAF_EMPTY_T / ::SR_LIST_T / ::SR_CONTAINER_T / ::SR_CONTAINER_PRESENCE_T).
*
* Required READ access, but if the access check fails, the module data are simply ignored without an error.
*
* @see Use ::sr_get_items for retrieving larger chunks
* of data from the datastore. Since it retrieves the data from datastore in
* larger chunks, it can work much more efficiently than multiple ::sr_get_item calls.
*
* @param[in] session Session ([DS](@ref sr_datastore_t)-specific) to use.
* @param[in] path [Path](@ref paths) of the data element to be retrieved.
* @param[in] timeout_ms Operational callback timeout in milliseconds. If 0, default is used.
* @param[out] value Requested node, allocated dynamically (free using ::sr_free_val).
* @return Error code (::SR_ERR_OK on success, ::SR_ERR_INVAL_ARG if multiple nodes match the path,
* ::SR_ERR_NOT_FOUND if no nodes match the path).
*/
int sr_get_item(sr_session_ctx_t *session, const char *path, uint32_t timeout_ms, sr_val_t **value);
/**
* @brief Retrieve an array of data elements selected by the provided XPath.
* Data are represented as ::sr_val_t structures.
*
* All data elements are transferred within one message from the datastore,
* which is more efficient that calling multiple ::sr_get_item calls.
*
* Required READ access, but if the access check fails, the module data are simply ignored without an error.
*
* @param[in] session Session ([DS](@ref sr_datastore_t)-specific) to use.
* @param[in] xpath [XPath](@ref paths) of the data elements to be retrieved.
* @param[in] timeout_ms Operational callback timeout in milliseconds. If 0, default is used.
* @param[in] opts Options overriding default get behaviour.
* @param[out] values Array of requested nodes, if any, allocated dynamically (free using ::sr_free_values).
* @param[out] value_cnt Number of returned elements in the values array.
* @return Error code (::SR_ERR_OK on success).
*/
int sr_get_items(sr_session_ctx_t *session, const char *xpath, uint32_t timeout_ms, const sr_get_options_t opts,
sr_val_t **values, size_t *value_cnt);
/**
* @brief Acquire libyang data tree together with its context lock in a SR data structure.
*
* Before a libyang data tree used in sysrepo can be created, ::sr_acquire_context() must be called
* to get the connection context. If the created libyang data tree is then passed to this function,
* it will handle the full cleanup of releasing the context and freeing the data.
*
* @param[in] session Session (not [DS](@ref sr_datastore_t)-specific) to use.
* @param[in] tree libyang data tree, ownership is passed to @p data in all cases.
* @param[out] data Created SR data, free with ::sr_release_data().
* @return Error code (::SR_ERR_OK on success), even on error @p tree is freed and context released.
*/
int sr_session_acquire_data(sr_session_ctx_t *session, struct lyd_node *tree, sr_data_t **data);
/**
* @brief Acquire libyang data tree together with its context lock in a SR data structure.
*
* Similar functionality as ::sr_session_acquire_data().
*
* @param[in] conn Connection to use.
* @param[in] tree libyang data tree, ownership is passed to @p data in all cases.
* @param[out] data Created SR data, free with ::sr_release_data().
* @return Error code (::SR_ERR_OK on success), even on error @p tree is freed and context released.
*/
int sr_acquire_data(sr_conn_ctx_t *conn, struct lyd_node *tree, sr_data_t **data);
/**
* @brief Retrieve a single subtree whose root node is selected by the provided path.
* Data are represented as _libyang_ subtrees.
*
* The functions returns values and all associated information stored under the root node and
* all its descendants. While the same data can be obtained using ::sr_get_items in combination
* with the expressive power of XPath addressing, the recursive nature of the output data type
* also preserves the hierarchical relationships between data elements.
*
* Required READ access, but if the access check fails, the module data are simply ignored without an error.
*
* @param[in] session Session ([DS](@ref sr_datastore_t)-specific) to use.
* @param[in] path [Path](@ref paths) selecting the root node of the subtree to be retrieved.
* @param[in] timeout_ms Operational callback timeout in milliseconds. If 0, default is used.
* @param[out] subtree SR data with the requested subtree. NULL if none found.
* @return Error code (::SR_ERR_OK on success, ::SR_ERR_INVAL_ARG if multiple nodes match the path,
* ::SR_ERR_NOT_FOUND if path is invalid - no nodes will ever match it).
*/
int sr_get_subtree(sr_session_ctx_t *session, const char *path, uint32_t timeout_ms, sr_data_t **subtree);
/**
* @brief Retrieve a tree whose root nodes match the provided XPath.
* Data are represented as _libyang_ subtrees.
*
* Top-level trees are always returned so if an inner node is selected, all of its descendants
* and its direct parents (lists also with keys) are returned.
*
* If the subtree selection process results in too many node overlaps, the cost of the operation
* may be unnecessarily big. As an example, a common XPath expression `//.` is normally used
* to select all nodes in a data tree, but for this operation it would result in an excessive duplication
* of data nodes. Since all the descendants of each matched node are returned implicitly, `//` in the XPath
* should never be used (i.e. `/\asterisk` is the correct XPath for all the nodes).
*
* Required READ access, but if the access check fails, the module data are simply ignored without an error.
*
* @param[in] session Session ([DS](@ref sr_datastore_t)-specific) to use.
* @param[in] xpath [XPath](@ref paths) selecting root nodes of subtrees to be retrieved.
* @param[in] max_depth Maximum depth of the selected subtrees. 0 is unlimited, 1 will not return any
* descendant nodes. If a list should be returned, its keys are always returned as well.
* @param[in] timeout_ms Operational callback timeout in milliseconds. If 0, default is used.
* @param[in] opts Options overriding default get behaviour.
* @param[out] data SR data with connected top-level data trees of all the requested data. NULL if none found.
* @return Error code (::SR_ERR_OK on success, ::SR_ERR_NOT_FOUND if xpath is invalid - no nodes will ever match it).
*/
int sr_get_data(sr_session_ctx_t *session, const char *xpath, uint32_t max_depth, uint32_t timeout_ms,
const sr_get_options_t opts, sr_data_t **data);
/**
* @brief Retrieve a single value matching the provided XPath.
* Data are represented as a single _libyang_ node.
*
* Compared to ::sr_get_data() or ::sr_get_subtree() this function is a bit more efficient because it returns
* only the selected node which is *disconnected* from its parents.
*
* Required READ access, but if the access check fails, the module data are simply ignored without an error.
*
* @param[in] session Session ([DS](@ref sr_datastore_t)-specific) to use.
* @param[in] path [Path](@ref paths) of the data element to be retrieved.
* @param[in] timeout_ms Operational callback timeout in milliseconds. If 0, default is used.
* @param[out] node SR data with the found node. NULL if none found.
* @return Error code (::SR_ERR_OK on success, ::SR_ERR_INVAL_ARG if multiple nodes match the path,
* ::SR_ERR_NOT_FOUND if no nodes match the path).
*/
int sr_get_node(sr_session_ctx_t *session, const char *path, uint32_t timeout_ms, sr_data_t **node);
/**
* @brief Release SR data structure, whoch consists of freeing the data tree, releasing the context,
* and freeing the structure itself.
*
* @param[in] data SR data to release and free.
*/
void sr_release_data(sr_data_t *data);
/**
* @brief Free ::sr_val_t structure and all memory allocated within it.
*
* @param[in] value Value to be freed.
*/
void sr_free_val(sr_val_t *value);
/**
* @brief Free array of ::sr_val_t structures (and all memory allocated
* within of each array element).
*
* @param[in] values Array of values to be freed.
* @param[in] count Number of elements stored in the array.
*/
void sr_free_values(sr_val_t *values, size_t count);
/** @} getdata */
////////////////////////////////////////////////////////////////////////////////
// Data Manipulation API (edit-config functionality)
////////////////////////////////////////////////////////////////////////////////
/**
* @defgroup edit_data_api Editing Data API
* @{
*/
/**
* @brief Prepare to set (create) the value of a leaf, leaf-list, list, or presence container.
* These changes are applied only after calling ::sr_apply_changes().
* Data are represented as ::sr_val_t structures.
*
* With default options it recursively creates all missing nodes (containers and
* lists including their key leaves) in the xpath to the specified node (can be
* turned off with ::SR_EDIT_NON_RECURSIVE option). If ::SR_EDIT_STRICT flag is set,
* the node must not exist (otherwise an error is returned). Neither option is allowed
* for ::SR_DS_OPERATIONAL.
*
* To create a list use @p path with key values included in predicates, @p value will be ignored.
* When creating key-less lists and state leaf-lists, use positional predicates such as `[1]` to refer to the instances.
* Using no predicate means the instance should be created.
*
* The value of a leaf-list can be specified either by predicate in xpath or by value argument.
* If both are present, value argument is ignored and xpath predicate is used.
*
* Edits preserve their order only if ::SR_EDIT_ISOLATE is used and in some cases it may
* affect the result.
*
* @param[in] session Session ([DS](@ref sr_datastore_t)-specific) to use.
* @param[in] path [Path](@ref paths) identifier of the data element to be set.
* @param[in] value Value to be set. `xpath` member of the ::sr_val_t structure can be NULL.
* @param[in] opts Options overriding default behavior of this call.
* @return Error code (::SR_ERR_OK on success, ::SR_ERR_OPERATION_FAILED if the whole edit was discarded).
*/
int sr_set_item(sr_session_ctx_t *session, const char *path, const sr_val_t *value, const sr_edit_options_t opts);
/**
* @brief Prepare to set (create) the value of a leaf, leaf-list, list, or presence container.
* These changes are applied only after calling ::sr_apply_changes().
* Data are represented as pairs of a path and string value.
*
* Function provides the same functionality as ::sr_set_item().
*
* @param[in] session Session ([DS](@ref sr_datastore_t)-specific) to use.
* @param[in] path [Path](@ref paths) identifier of the data element to be set.
* @param[in] value String representation of the value to be set.
* @param[in] origin Origin of the value, used only for ::SR_DS_OPERATIONAL edits. Module ietf-origin is assumed
* if no prefix used.
* @param[in] opts Options overriding default behavior of this call.
* @return Error code (::SR_ERR_OK on success, ::SR_ERR_OPERATION_FAILED if the whole edit was discarded).
*/
int sr_set_item_str(sr_session_ctx_t *session, const char *path, const char *value, const char *origin,
const sr_edit_options_t opts);
/**
* @brief Prepare to delete the nodes matching the specified xpath. These changes are applied only
* after calling ::sr_apply_changes(). The accepted values are the same as for ::sr_set_item_str.
*
* If ::SR_EDIT_STRICT flag is set the specified node must must exist in the datastore.
* If the @p path includes the list keys/leaf-list value, the specified instance is deleted.
* If the @p path of list/leaf-list does not include keys/value, all instances are deleted but there can be no further
* changes merged into the list, use ::SR_EDIT_ISOLATE in such a case.
*
* For ::SR_DS_OPERATIONAL, the flag is not allowed. However, when trying to **remove a value stored in the push
* operational data** (set before using ::sr_set_item_str() on ::SR_DS_OPERATIONAL, for example), use
* ::sr_discard_items() instead.
*
* @param[in] session Session ([DS](@ref sr_datastore_t)-specific) to use.
* @param[in] path [Path](@ref paths) identifier of the data element to be deleted.
* @param[in] opts Options overriding default behavior of this call.
* @return Error code (::SR_ERR_OK on success, ::SR_ERR_OPERATION_FAILED if the whole edit was discarded).
*/
int sr_delete_item(sr_session_ctx_t *session, const char *path, const sr_edit_options_t opts);
/**
* @brief Deprecated, use ::sr_delete_item().
*/
int sr_oper_delete_item_str(sr_session_ctx_t *session, const char *path, const char *value, const sr_edit_options_t opts);
/**
* @brief Prepare to discard nodes matching the specified xpath (or all if not set) previously set by the
* session connection. Usable only for ::SR_DS_OPERATIONAL datastore. These changes are applied only
* after calling ::sr_apply_changes().
*
* Creates an opaque node `discard-items` in the `sysrepo` YANG module namespace with @p xpath used as the value.
* Such a node can be a part of the edit in ::sr_edit_batch() and will discard nodes like this function does.
*
* @param[in] session Session ([DS](@ref sr_datastore_t)-specific) to use.
* @param[in] xpath [XPath](@ref paths) expression filtering the nodes to discard, all if NULL.
* @return Error code (::SR_ERR_OK on success).
*/
int sr_discard_items(sr_session_ctx_t *session, const char *xpath);
/**
* @brief Prepare to move/create the instance of an user-ordered list or leaf-list to the specified position.
* These changes are applied only after calling ::sr_apply_changes().
*
* Item can be moved to the first or last position or positioned relatively to its sibling.
*
* With default options it recursively creates all missing nodes (containers and
* lists including their key leaves) in the xpath to the specified node (can be
* turned off with ::SR_EDIT_NON_RECURSIVE option). If ::SR_EDIT_STRICT flag is set,
* the node must not exist (otherwise an error is returned).
*
* For ::SR_DS_OPERATIONAL, neither option is allowed.
*
* @note To determine current order, you can issue a ::sr_get_items() call
* (without specifying keys of particular list).
*
* @param[in] session Session ([DS](@ref sr_datastore_t)-specific) to use
* @param[in] path [Path](@ref paths) identifier of the data element to be moved.
* @param[in] position Requested move direction.
* @param[in] list_keys Predicate identifying the relative list instance (example input `[key1="val1"][key2="val2"]...`).
* @param[in] leaflist_value Value of the relative leaf-list instance (example input `val1`) used
* to determine relative position, needed only if position argument is ::SR_MOVE_BEFORE or ::SR_MOVE_AFTER.
* @param[in] origin Origin of the value, used only for ::SR_DS_OPERATIONAL edits. Module ietf-origin is assumed
* if no prefix used.
* @param[in] opts Options overriding default behavior of this call.
* @return Error code (::SR_ERR_OK on success, ::SR_ERR_OPERATION_FAILED if the whole edit was discarded).