1 /*************************************************************************
3 * Copyright 2020 highstreet technologies GmbH and others
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 ***************************************************************************/
21 #include "core/framework.h"
22 #include "utils/log_utils.h"
26 sr_conn_ctx_t *session_connection = 0;
27 sr_session_ctx_t *session_running = 0;
28 sr_session_ctx_t *session_operational = 0;
29 struct ly_ctx *session_context = 0;
30 sr_subscription_ctx_t *session_subscription = 0;
32 int session_init(void) {
35 /* connect to sysrepo */
36 rc = sr_connect(0, &session_connection);
38 log_error("sr_connect failed\n");
39 goto session_init_cleanup;
43 rc = sr_session_start(session_connection, SR_DS_OPERATIONAL, &session_operational);
44 if (rc != SR_ERR_OK) {
45 log_error("sr_session_start operational failed\n");
46 goto session_init_cleanup;
49 rc = sr_session_start(session_connection, SR_DS_RUNNING, &session_running);
50 if (rc != SR_ERR_OK) {
51 log_error("sr_session_start running failed\n");
52 goto session_init_cleanup;
56 session_context = (struct ly_ctx *)sr_get_context(session_connection);
57 if(session_context == 0) {
58 log_error("sr_get_context failed\n");
59 goto session_init_cleanup;
65 return NTS_ERR_FAILED;
68 void session_free(void) {
69 log_add_verbose(2, "session_free()... ");
70 if(session_subscription) {
71 sr_unsubscribe(session_subscription);
74 sr_session_stop(session_operational);
75 sr_session_stop(session_running);
77 sr_disconnect(session_connection);
79 session_connection = 0;
81 session_operational = 0;