aboutsummaryrefslogtreecommitdiffstats
path: root/test/mocks/pnfsimulator/netconfsimulator/netopeer-change-saver-native/sysrepo/Session.hpp
blob: 02d03ed3862ec0d2dcd5d5076332f18d84cf1371 (plain)
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
/**
 * @file Session.h
 * @author Mislav Novakovic <mislav.novakovic@sartura.hr>
 * @brief Sysrepo Session class header.
 *
 * @copyright
 * Copyright 2016 Deutsche Telekom AG.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifndef SESSION_H
#define SESSION_H

#include <iostream>
#include <memory>
#include <map>
#include <vector>

#include "Sysrepo.hpp"
#include "Internal.hpp"
#include "Struct.hpp"
#include "Tree.hpp"
#include "Connection.hpp"
#include "Session.hpp"

extern "C" {
#include "../sysrepo.h"
}

namespace sysrepo {

/**
 * @defgroup classes C++/Python
 * @{
 */

/**
 * @brief Class for wrapping sr_session_ctx_t.
 * @class Session
 */
class Session
{

public:
    /** Wrapper for [sr_session_start](@ref sr_session_start) and [sr_session_start_user](@ref sr_session_start_user)
     * if user_name is set.*/
    Session(S_Connection conn, sr_datastore_t datastore = (sr_datastore_t) DS_RUNNING, \
            const sr_sess_options_t opts = SESS_DEFAULT, const char *user_name = nullptr);
    /** Wrapper for [sr_session_ctx_t](@ref sr_session_ctx_t), for internal use only.*/
    Session(sr_session_ctx_t *sess, sr_sess_options_t opts = SESS_DEFAULT, S_Deleter deleter = nullptr);
    /** Wrapper for [sr_session_stop](@ref sr_session_stop) */
    void session_stop();
    /** Wrapper for [sr_session_switch_ds](@ref sr_session_switch_ds) */
    void session_switch_ds(sr_datastore_t ds);
    /** Wrapper for [sr_get_last_error](@ref sr_get_last_error) */
    S_Error get_last_error();
    /** Wrapper for [sr_get_last_errors](@ref sr_get_last_errors) */
    S_Errors get_last_errors();
    /** Wrapper for [sr_list_schemas](@ref sr_list_schemas) */
    S_Yang_Schemas list_schemas();
    /** Wrapper for [sr_get_schema](@ref sr_get_schema) */
    std::string get_schema(const char *module_name, const char *revision,
                           const char *submodule_name, sr_schema_format_t format);
    /** Wrapper for [sr_get_item](@ref sr_get_item) */
    S_Val get_item(const char *xpath);
    /** Wrapper for [sr_get_items](@ref sr_get_items) */
    S_Vals get_items(const char *xpath);
    /** Wrapper for [sr_get_items_iter](@ref sr_get_items_iter) */
    S_Iter_Value get_items_iter(const char *xpath);
    /** Wrapper for [sr_get_item_next](@ref sr_get_item_next) */
    S_Val get_item_next(S_Iter_Value iter);
    /** Wrapper for [sr_get_subtree](@ref sr_get_subtree) */
    S_Tree get_subtree(const char *xpath, sr_get_subtree_options_t opts = GET_SUBTREE_DEFAULT);
    /** Wrapper for [sr_get_subtrees](@ref sr_get_subtrees) */
    S_Trees get_subtrees(const char *xpath, sr_get_subtree_options_t opts = GET_SUBTREE_DEFAULT);

    /** Wrapper for [sr_node_get_child](@ref sr_node_get_child) */
    S_Tree get_child(S_Tree in_tree);
    /** Wrapper for [sr_node_get_next_sibling](@ref sr_node_get_next_sibling) */
    S_Tree get_next_sibling(S_Tree in_tree);
    /** Wrapper for [sr_node_get_parent](@ref sr_node_get_parent) */
    S_Tree get_parent(S_Tree in_tree);

    /** Wrapper for [sr_set_item](@ref sr_set_item) */
    void set_item(const char *xpath, S_Val value = nullptr, const sr_edit_options_t opts = EDIT_DEFAULT);
    /** Wrapper for [sr_set_item_str](@ref sr_set_item_str) */
    void set_item_str(const char *xpath, const char *value, const sr_edit_options_t opts = EDIT_DEFAULT);
    /** Wrapper for [sr_delete_item](@ref sr_delete_item) */
    void delete_item(const char *xpath, const sr_edit_options_t opts = EDIT_DEFAULT);
    /** Wrapper for [sr_move_item](@ref sr_move_item) */
    void move_item(const char *xpath, const sr_move_position_t position, const char *relative_item = nullptr);
    /** Wrapper for [sr_session_refresh](@ref sr_session_refresh) */
    void refresh();
    /** Wrapper for [sr_validate](@ref sr_validate) */
    void validate();
    /** Wrapper for [sr_commit](@ref sr_commit) */
    void commit();
    /** Wrapper for [sr_lock_datastore](@ref sr_lock_datastore) */
    void lock_datastore();
    /** Wrapper for [sr_unlock_datastore](@ref sr_unlock_datastore) */
    void unlock_datastore();
    /** Wrapper for [sr_lock_module](@ref sr_lock_module) */
    void lock_module(const char *module_name);
    /** Wrapper for [sr_unlock_module](@ref sr_unlock_module) */
    void unlock_module(const char *module_name);
    /** Wrapper for [sr_discard_changes](@ref sr_discard_changes) */
    void discard_changes();
    /** Wrapper for [sr_copy_config](@ref sr_copy_config) */
    void copy_config(const char *module_name, sr_datastore_t src_datastore, sr_datastore_t dst_datastore);
    /** Wrapper for [sr_session_set_options](@ref sr_session_set_options) */
    void set_options(const sr_sess_options_t opts);
    /** Wrapper for [sr_get_changes_iter](@ref sr_get_changes_iter) */
    S_Iter_Change get_changes_iter(const char *xpath);
    /** Wrapper for [sr_get_change_next](@ref sr_get_change_next) */
    S_Change get_change_next(S_Iter_Change iter);
    ~Session();

    /** Wrapper for [sr_rpc_send](@ref sr_rpc_send) */
    S_Vals rpc_send(const char *xpath, S_Vals input);
    /** Wrapper for [sr_rpc_send_tree](@ref sr_rpc_send_tree) */
    S_Trees rpc_send(const char *xpath, S_Trees input);
    /** Wrapper for [sr_action_send](@ref sr_action_send) */
    S_Vals action_send(const char *xpath, S_Vals input);
    /** Wrapper for [sr_action_send_tree](@ref sr_action_send_tree) */
    S_Trees action_send(const char *xpath, S_Trees input);
    /** Wrapper for [sr_event_notif_send](@ref sr_event_notif_send) */
    void event_notif_send(const char *xpath, S_Vals values, const sr_ev_notif_flag_t options = SR_EV_NOTIF_DEFAULT);
    /** Wrapper for [sr_event_notif_send_tree](@ref sr_event_notif_send_tree) */
    void event_notif_send(const char *xpath, S_Trees trees, const sr_ev_notif_flag_t options = SR_EV_NOTIF_DEFAULT);

    friend class Subscribe;

private:
    sr_session_ctx_t *_sess;
    sr_datastore_t _datastore;
    sr_sess_options_t _opts;
    S_Connection _conn;
    S_Deleter _deleter;
};

/**
 * @brief Helper class for calling C callbacks, C++ only.
 * @class Callback
 */
class Callback
{
public:
    Callback();
    virtual ~Callback();

    /** Wrapper for [sr_module_change_cb](@ref sr_module_change_cb) callback.*/
    virtual int module_change(S_Session session, const char *module_name, sr_notif_event_t event, void *private_ctx) {return SR_ERR_OK;};
    /** Wrapper for [sr_subtree_change_cb](@ref sr_subtree_change_cb) callback.*/
    virtual int subtree_change(S_Session session, const char *xpath, sr_notif_event_t event, void *private_ctx) {return SR_ERR_OK;};
    /** Wrapper for [sr_module_install_cb](@ref sr_module_install_cb) callback.*/
    virtual void module_install(const char *module_name, const char *revision, sr_module_state_t state, void *private_ctx) {return;};
    /** Wrapper for [sr_feature_enable_cb](@ref sr_feature_enable_cb) callback.*/
    virtual void feature_enable(const char *module_name, const char *feature_name, bool enabled, void *private_ctx) {return;};
    /** Wrapper for [sr_rpc_cb](@ref sr_rpc_cb) callback.*/
    virtual int rpc(const char *xpath, const S_Vals input, S_Vals_Holder output, void *private_ctx) {return SR_ERR_OK;};
    /** Wrapper for [sr_action_cb](@ref sr_action_cb) callback.*/
    virtual int action(const char *xpath, const S_Vals input, S_Vals_Holder output, void *private_ctx) {return SR_ERR_OK;};
    /** Wrapper for [sr_rpc_tree_cb](@ref sr_rpc_tree_cb) callback.*/
    virtual int rpc_tree(const char *xpath, const S_Trees input, S_Trees_Holder output, void *private_ctx) {return SR_ERR_OK;};
    /** Wrapper for [sr_action_tree_cb](@ref sr_action_tree_cb) callback.*/
    virtual int action_tree(const char *xpath, const S_Trees input, S_Trees_Holder output, void *private_ctx) {return SR_ERR_OK;};
    /** Wrapper for [sr_dp_get_items_cb](@ref sr_dp_get_items_cb) callback.*/
    virtual int dp_get_items(const char *xpath, S_Vals_Holder vals, uint64_t request_id, void *private_ctx) {return SR_ERR_OK;};
    /** Wrapper for [sr_event_notif_cb](@ref sr_event_notif_cb) callback.*/
    virtual void event_notif(const sr_ev_notif_type_t notif_type, const char *xpath, S_Vals vals, time_t timestamp, void *private_ctx) {return;};
    /** Wrapper for [sr_event_notif_tree_cb](@ref sr_event_notif_tree_cb) callback.*/
    virtual void event_notif_tree(const sr_ev_notif_type_t notif_type, const char *xpath, S_Trees trees, time_t timestamp, void *private_ctx) {return;};
    Callback *get() {return this;};

    std::map<const char *, void*> private_ctx;
};

/**
 * @brief Class for wrapping sr_subscription_ctx_t.
 * @class Subscribe
 */
class Subscribe
{

public:
    /** Wrapper for [sr_subscription_ctx_t](@ref sr_subscription_ctx_t), for internal use only.*/
    Subscribe(S_Session sess);

    /** Wrapper for [sr_module_change_subscribe](@ref sr_module_change_subscribe) */
    void module_change_subscribe(const char *module_name, S_Callback callback, void *private_ctx = nullptr, uint32_t priority = 0, sr_subscr_options_t opts = SUBSCR_DEFAULT);
    /** Wrapper for [sr_subtree_change_subscribe](@ref sr_subtree_change_subscribe) */
    void subtree_change_subscribe(const char *xpath, S_Callback callback, void *private_ctx = nullptr, uint32_t priority = 0, sr_subscr_options_t opts = SUBSCR_DEFAULT);
    /** Wrapper for [sr_module_install_subscribe](@ref sr_module_install_subscribe) */
    void module_install_subscribe(S_Callback callback, void *private_ctx = nullptr, sr_subscr_options_t opts = SUBSCR_DEFAULT);
    /** Wrapper for [sr_feature_enable_subscribe](@ref sr_feature_enable_subscribe) */
    void feature_enable_subscribe(S_Callback callback, void *private_ctx = nullptr, sr_subscr_options_t opts = SUBSCR_DEFAULT);
    /** Wrapper for [sr_rpc_subscribe](@ref sr_rpc_subscribe) */
    void rpc_subscribe(const char *xpath, S_Callback callback, void *private_ctx = nullptr, sr_subscr_options_t opts = SUBSCR_DEFAULT);
    /** Wrapper for [sr_action_subscribe](@ref sr_action_subscribe) */
    void action_subscribe(const char *xpath, S_Callback callback, void *private_ctx = nullptr, sr_subscr_options_t opts = SUBSCR_DEFAULT);
    /** Wrapper for [sr_event_notif_subscribe_tree](@ref sr_event_notif_subscribe_tree) */
    void event_notif_subscribe_tree(const char *xpath, S_Callback callback, void *private_ctx = nullptr, sr_subscr_options_t opts = SUBSCR_DEFAULT);
    /** Wrapper for [sr_event_notif_subscribe](@ref sr_event_notif_subscribe) */
    void event_notif_subscribe(const char *xpath, S_Callback callback, void *private_ctx = nullptr, sr_subscr_options_t opts = SUBSCR_DEFAULT);
    /** Wrapper for [sr_rpc_subscribe_tree](@ref sr_rpc_subscribe_tree) */
    void rpc_subscribe_tree(const char *xpath, S_Callback callback, void *private_ctx = nullptr, sr_subscr_options_t opts = SUBSCR_DEFAULT);
    /** Wrapper for [sr_action_subscribe_tree](@ref sr_action_subscribe_tree) */
    void action_subscribe_tree(const char *xpath, S_Callback callback, void *private_ctx = nullptr, sr_subscr_options_t opts = SUBSCR_DEFAULT);
    /** Wrapper for [sr_dp_get_items_subscribe](@ref sr_dp_get_items_subscribe) */
    void dp_get_items_subscribe(const char *xpath, S_Callback callback, void *private_ctx = nullptr, sr_subscr_options_t opts = SUBSCR_DEFAULT);
    std::vector<S_Callback > cb_list;

    /** Wrapper for [sr_unsubscribe](@ref sr_unsubscribe) */
    void unsubscribe();
    ~Subscribe();

    /** SWIG specific, internal use only.*/
    sr_subscription_ctx_t **swig_sub() { return &_sub;};
    /** SWIG specific, internal use only.*/
    sr_session_ctx_t *swig_sess() {return _sess->_sess;};
    /** SWIG specific, internal use only.*/
    std::vector<void*> wrap_cb_l;
    /** SWIG specific, internal use only.*/
    void additional_cleanup(void *private_ctx) {return;};

private:
    sr_subscription_ctx_t *_sub;
    S_Session _sess;
    S_Deleter sess_deleter;
};

/**@} */
}
#endif