Add supoprt for D release use-case.
[sim/o1-interface.git] / ntsimulator / ntsim-ng / core / session.c
1 /*************************************************************************
2 *
3 * Copyright 2020 highstreet technologies GmbH and others
4 *
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
8 *
9 *     http://www.apache.org/licenses/LICENSE-2.0
10 *
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 ***************************************************************************/
17
18 #define _GNU_SOURCE
19
20 #include "session.h"
21 #include "core/framework.h"
22 #include "utils/log_utils.h"
23 #include <stdio.h>
24 #include <assert.h>
25
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;
31
32 int session_init(void) {
33     int rc = SR_ERR_OK;
34     
35     /* connect to sysrepo */
36     rc = sr_connect(0, &session_connection);
37     if(SR_ERR_OK != rc) {
38         log_error("sr_connect failed\n");
39         goto session_init_cleanup;
40     }
41
42     /* start session */
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;
47     }
48
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;
53     }
54
55     /* get context */
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;
60     }
61
62     return NTS_ERR_OK;
63
64 session_init_cleanup:
65     return NTS_ERR_FAILED;
66 }
67
68 void session_free(void) {
69     log_add_verbose(2, "session_free()... ");
70     if(session_subscription) {
71         sr_unsubscribe(session_subscription);
72     }
73
74     sr_session_stop(session_operational);
75     sr_session_stop(session_running);
76
77     sr_disconnect(session_connection);
78
79     session_connection = 0;
80     session_running = 0;
81     session_operational = 0;
82     session_context = 0;
83     log_add(2, "done\n");
84 }