7d12fce7b22b0f6bc02e1a84c0c8111b5c171b7c
[sim/o1-interface.git] / ntsimulator / ntsim-ng / core / app / nf_oran_ru_supervision.c
1 /*************************************************************************
2 *
3 * Copyright 2021 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 "nf_oran_ru_supervision.h"
21 #include "utils/log_utils.h"
22 #include "utils/sys_utils.h"
23 #include "utils/nts_utils.h"
24 #include "utils/rand_utils.h"
25 #include "utils/http_client.h"
26 #include <stdio.h>
27 #include <pthread.h>
28 #include <assert.h>
29
30 #include <sysrepo.h>
31 #include <sysrepo/values.h>
32 #include <libnetconf2/netconf.h>
33 #include <libyang/libyang.h>
34 #include <errno.h>
35 #include <pthread.h>
36 #include <signal.h>
37 #include <stdio.h>
38 #include <unistd.h>
39
40 #include "core/framework.h"
41 #include "core/context.h"
42 #include "core/session.h"
43 #include "core/xpath.h"
44
45 int notification_timer_seconds=60;
46 int supervision_timer_seconds=10;
47
48 static pthread_t o_ran_supervision_thread = NULL;
49 static void *o_ran_supervision_thread_routine(void *arg);
50
51 static int
52 supervision_watchdog_reset_rpc_cb(sr_session_ctx_t *session, const char *path, const sr_val_t *input, const size_t input_cnt,
53         sr_event_t event, uint32_t request_id, sr_val_t **output, size_t *output_cnt, void *private_data)
54 {
55     size_t i;
56
57     (void)session;
58     (void)event;
59     (void)request_id;
60     (void)private_data;
61
62     bool notification_default = true;
63     bool supervision_default = true;
64
65     for (i = 0; i < input_cnt; ++i) {
66         if (!strcmp(input[i].xpath, "/o-ran-supervision:supervision-watchdog-reset/supervision-notification-interval")) {
67             notification_timer_seconds = input[i].data.uint16_val;
68             notification_default = false;
69         }
70         else if (!strcmp(input[i].xpath, "/o-ran-supervision:supervision-watchdog-reset/guard-timer-overhead")) {
71             supervision_timer_seconds = input[i].data.uint16_val;
72             supervision_default = false;
73         }
74     }
75
76     if (notification_default) {
77         notification_timer_seconds = 60;
78     }
79     if (supervision_default) {
80         supervision_timer_seconds = 10;
81     }
82
83     if (!strcmp(path, "/o-ran-supervision:supervision-watchdog-reset")) {
84         *output = malloc(sizeof **output);
85         *output_cnt = 1;
86
87         (*output)[0].xpath = strdup("/o-ran-supervision:supervision-watchdog-reset/next-update-at");
88         (*output)[0].type = SR_STRING_T;
89         (*output)[0].dflt = 0;
90         (*output)[0].data.string_val = get_current_date_and_time_delay_seconds(notification_timer_seconds);
91     }
92
93     if (o_ran_supervision_thread != NULL) {
94         pthread_cancel(o_ran_supervision_thread);
95         o_ran_supervision_thread = NULL;
96     }
97
98     if(pthread_create(&o_ran_supervision_thread, 0, o_ran_supervision_thread_routine, 0)) {
99         log_error("could not create thread for o-ran-supervision\n");
100         return NTS_ERR_FAILED;
101     }
102
103     return NTS_ERR_OK;
104 }
105
106 int nf_oran_ru_supervision_init(void) {
107     int rc;
108
109     rc = sr_rpc_subscribe(session_running, O_RAN_SUPERVISION_WATCHDOG_RESET_RPC_SCHEMA_XPATH, supervision_watchdog_reset_rpc_cb, NULL, 0, 0, &session_subscription);
110     if (rc != SR_ERR_OK) {
111         log_add_verbose(1, "Subscribing for RPC \"%s\" failed.\n", O_RAN_SUPERVISION_WATCHDOG_RESET_RPC_SCHEMA_XPATH);
112         return NTS_ERR_FAILED;
113     }
114
115     if(pthread_create(&o_ran_supervision_thread, 0, o_ran_supervision_thread_routine, 0)) {
116         log_error("could not create thread for o-ran-supervision\n");
117         return NTS_ERR_FAILED;
118     }
119
120     return NTS_ERR_OK;
121 }
122
123 static void *o_ran_supervision_thread_routine(void *arg) {
124     int notification_counter = notification_timer_seconds;
125     int supervision_counter = notification_timer_seconds + supervision_timer_seconds;
126     int rc;
127
128     log_add_verbose(1, "Starting thread for o-ran-supervision...\n");
129
130     struct lyd_node *notif = NULL;
131     notif = lyd_new_path(NULL, session_context, O_RAN_SUPERVISION_NOTIFICATION_SCHEMA_XPATH, NULL, 0, 0);
132     if (!notif) {
133         log_error("Creating notification \"%s\" failed.\n", O_RAN_SUPERVISION_NOTIFICATION_SCHEMA_XPATH);
134         return NTS_ERR_FAILED;
135     }
136
137     while (supervision_counter > 0) {
138         sleep(1);
139         supervision_counter--;
140         notification_counter--;
141
142         if (notification_counter == 0) {
143             log_add_verbose(1, "Sending o-ran-supervision supervision-notification..\n");
144             rc = sr_event_notif_send_tree(session_running, notif);
145             if (rc != SR_ERR_OK) {
146                 lyd_free(notif);
147                 return NTS_ERR_FAILED;
148             }
149         }
150     }
151
152     log_add_verbose(1, "Failed to receive watchdog reset, terminating supervision timer for o-ran-supervision...\n");
153     pthread_exit((void *)1);
154 }
155
156 void nf_oran_ru_supervision_free(void) {
157
158 }
159