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");
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");
46 goto session_init_cleanup;
49 if(framework_arguments.operational_only == false) {
50 rc = sr_session_start(session_connection, SR_DS_RUNNING, &session_running);
51 if (rc != SR_ERR_OK) {
52 log_error("sr_session_start running failed");
53 goto session_init_cleanup;
57 session_running = session_operational;
62 session_context = (struct ly_ctx *)sr_get_context(session_connection);
63 if(session_context == 0) {
64 log_error("sr_get_context failed");
65 goto session_init_cleanup;
71 return NTS_ERR_FAILED;
74 void session_free(void) {
75 log_message(2, "session_free()... ");
76 if(session_subscription) {
77 sr_unsubscribe(session_subscription);
80 sr_session_stop(session_operational);
81 if(framework_arguments.operational_only == false) {
82 sr_session_stop(session_running);
84 sr_disconnect(session_connection);
86 session_connection = 0;
88 session_operational = 0;
90 log_message(2, "done\n");