1 /*************************************************************************
3 * Copyright 2021 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 ***************************************************************************/
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"
31 #include <sysrepo/values.h>
32 #include <libnetconf2/netconf.h>
33 #include <libyang/libyang.h>
40 #include "core/framework.h"
41 #include "core/context.h"
42 #include "core/session.h"
43 #include "core/xpath.h"
45 int notification_timer_seconds=60;
46 int supervision_timer_seconds=10;
48 static pthread_t o_ran_supervision_thread = NULL;
49 static void *o_ran_supervision_thread_routine(void *arg);
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)
62 bool notification_default = true;
63 bool supervision_default = true;
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;
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;
76 if (notification_default) {
77 notification_timer_seconds = 60;
79 if (supervision_default) {
80 supervision_timer_seconds = 10;
83 if (!strcmp(path, "/o-ran-supervision:supervision-watchdog-reset")) {
84 *output = malloc(sizeof **output);
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);
93 if (o_ran_supervision_thread != NULL) {
94 pthread_cancel(o_ran_supervision_thread);
95 o_ran_supervision_thread = NULL;
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;
106 int nf_oran_ru_supervision_init(void) {
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;
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;
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;
128 log_add_verbose(1, "Starting thread for o-ran-supervision...\n");
130 struct lyd_node *notif = NULL;
131 notif = lyd_new_path(NULL, session_context, O_RAN_SUPERVISION_NOTIFICATION_SCHEMA_XPATH, NULL, 0, 0);
133 log_error("Creating notification \"%s\" failed.\n", O_RAN_SUPERVISION_NOTIFICATION_SCHEMA_XPATH);
134 return NTS_ERR_FAILED;
137 while (supervision_counter > 0) {
139 supervision_counter--;
140 notification_counter--;
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) {
147 return NTS_ERR_FAILED;
152 log_add_verbose(1, "Failed to receive watchdog reset, terminating supervision timer for o-ran-supervision...\n");
153 pthread_exit((void *)1);
156 void nf_oran_ru_supervision_free(void) {