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 "utils/log_utils.h"
22 #include "utils/rand_utils.h"
23 #include "utils/nts_utils.h"
24 #include "utils/sys_utils.h"
25 #include "utils/http_client.h"
30 #include "core/framework.h"
31 #include <cjson/cJSON.h>
33 static uint32_t *fault_ves_sequence_number = 0;
35 static cJSON *ves_create_fault_fields(const char *alarm_condition, const char *alarm_object, const char *severity, const char *date_time, const char *specific_problem);
36 static int ves_message_send_internal(sr_session_ctx_t *session, const char *condition, const char *object, const char *severity, const char *date_time, const char *specific_problem, int port, uint32_t *seq_id);
38 int faults_ves_init(void) {
39 fault_ves_sequence_number = (uint32_t *)malloc(sizeof(uint32_t) * (framework_environment.settings.ssh_connections + framework_environment.settings.tls_connections));
40 if(fault_ves_sequence_number == 0) {
41 log_error("malloc failed\n");
42 return NTS_ERR_FAILED;
45 for(int i = 0; i < (framework_environment.settings.ssh_connections + framework_environment.settings.tls_connections); i++) {
46 fault_ves_sequence_number[i] = 0;
52 void faults_ves_free(void) {
53 free(fault_ves_sequence_number);
54 fault_ves_sequence_number = 0;
57 int faults_ves_message_send(sr_session_ctx_t *session, const char *condition, const char *object, const char *severity, const char *date_time, const char *specific_problem) {
62 assert(specific_problem);
64 int sequence_index = 0;
65 int ssh_base_port = 0;
66 int tls_base_port = 0;
67 nts_mount_point_addressing_method_t mp = nts_mount_point_addressing_method_get(session);
68 if(mp == UNKNOWN_MAPPING) {
69 log_error("mount-point-addressing-method failed\n");
70 return NTS_ERR_FAILED;
72 else if(mp == DOCKER_MAPPING) {
73 ssh_base_port = STANDARD_NETCONF_PORT;
74 tls_base_port = ssh_base_port + framework_environment.settings.ssh_connections;
77 ssh_base_port = framework_environment.host.ssh_base_port;
78 tls_base_port = framework_environment.host.tls_base_port;
81 for(int port = ssh_base_port; port < ssh_base_port + framework_environment.settings.ssh_connections; port++) {
82 uint32_t *seq_id = &fault_ves_sequence_number[sequence_index++];
83 int rc = ves_message_send_internal(session, condition, object, severity, date_time, specific_problem, port, seq_id);
84 if(rc != NTS_ERR_OK) {
85 log_error("ves_message_send_internal failed\n");
89 for(int port = tls_base_port; port < tls_base_port + framework_environment.settings.tls_connections; port++) {
90 uint32_t *seq_id = &fault_ves_sequence_number[sequence_index++];
91 int rc = ves_message_send_internal(session, condition, object, severity, date_time, specific_problem, port, seq_id);
92 if(rc != NTS_ERR_OK) {
93 log_error("ves_message_send_internal failed\n");
100 static cJSON *ves_create_fault_fields(const char *alarm_condition, const char *alarm_object, const char *severity, const char *date_time, const char *specific_problem) {
101 assert(alarm_condition);
102 assert(alarm_object);
105 assert(specific_problem);
107 cJSON *faultFields = cJSON_CreateObject();
108 if(faultFields == 0) {
109 log_error("could not create JSON object: faultFields\n");
113 if(cJSON_AddStringToObject(faultFields, "faultFieldsVersion", "4.0") == 0) {
114 log_error("could not create JSON object: faultFieldsVersion\n");
115 cJSON_Delete(faultFields);
119 if(cJSON_AddStringToObject(faultFields, "alarmCondition", alarm_condition) == 0) {
120 log_error("could not create JSON object: alarmCondition\n");
121 cJSON_Delete(faultFields);
125 if(cJSON_AddStringToObject(faultFields, "alarmInterfaceA", alarm_object) == 0) {
126 log_error("could not create JSON object: alarmInterfaceA\n");
127 cJSON_Delete(faultFields);
131 if(cJSON_AddStringToObject(faultFields, "eventSourceType", "O_RAN_COMPONENT") == 0) {
132 log_error("could not create JSON object: eventSourceType\n");
133 cJSON_Delete(faultFields);
137 if(cJSON_AddStringToObject(faultFields, "specificProblem", specific_problem) == 0) {
138 log_error("could not create JSON object: specificProblem\n");
139 cJSON_Delete(faultFields);
143 if(cJSON_AddStringToObject(faultFields, "eventSeverity", severity) == 0) {
144 log_error("could not create JSON object: eventSeverity\n");
145 cJSON_Delete(faultFields);
149 if(cJSON_AddStringToObject(faultFields, "vfStatus", "Active") == 0) {
150 log_error("could not create JSON object: vfStatus\n");
151 cJSON_Delete(faultFields);
155 cJSON *alarmAdditionalInformation = cJSON_CreateObject();
156 if(alarmAdditionalInformation == 0) {
157 log_error("could not create JSON object: alarmAdditionalInformation\n");
158 cJSON_Delete(faultFields);
161 cJSON_AddItemToObject(faultFields, "alarmAdditionalInformation", alarmAdditionalInformation);
163 if(cJSON_AddStringToObject(alarmAdditionalInformation, "eventTime", date_time) == 0) {
164 log_error("could not create JSON object: eventTime\n");
165 cJSON_Delete(faultFields);
169 if(cJSON_AddStringToObject(alarmAdditionalInformation, "equipType", "O-RAN-sim") == 0) {
170 log_error("could not create JSON object: equipType\n");
171 cJSON_Delete(faultFields);
175 if(cJSON_AddStringToObject(alarmAdditionalInformation, "vendor", "Melacon") == 0) {
176 log_error("could not create JSON object: vendor\n");
177 cJSON_Delete(faultFields);
181 if(cJSON_AddStringToObject(alarmAdditionalInformation, "model", "Simulated Device") == 0) {
182 log_error("could not create JSON object: model\n");
183 cJSON_Delete(faultFields);
190 static int ves_message_send_internal(sr_session_ctx_t *session, const char *condition, const char *object, const char *severity, const char *date_time, const char *specific_problem, int port, uint32_t *seq_id) {
195 assert(specific_problem);
197 char *hostname_string = framework_environment.settings.hostname;
198 cJSON *post_data_json = cJSON_CreateObject();
199 if(post_data_json == 0) {
200 log_error("cJSON_CreateObject failed\n");
201 return NTS_ERR_FAILED;
204 cJSON *event = cJSON_CreateObject();
206 log_error("cJSON_CreateObject failed\n");
207 cJSON_Delete(post_data_json);
208 return NTS_ERR_FAILED;
210 cJSON_AddItemToObject(post_data_json, "event", event);
212 char *source_name = 0;
213 if (framework_environment.settings.ssh_connections + framework_environment.settings.tls_connections == 1) {
214 // we don't want to append the port to the source name if we only expose one port
215 asprintf(&source_name, "%s", hostname_string);
218 asprintf(&source_name, "%s_%d", hostname_string, port);
220 cJSON *common_event_header = ves_create_common_event_header("fault", "O_RAN_COMPONENT_Alarms", source_name, "Low", (*seq_id)++);
223 if(common_event_header == 0) {
224 log_error("ves_create_common_event_header failed\n");
225 cJSON_Delete(post_data_json);
226 return NTS_ERR_FAILED;
228 cJSON_AddItemToObject(event, "commonEventHeader", common_event_header);
230 cJSON *fault_fields = ves_create_fault_fields(condition, object, severity, date_time, specific_problem);
231 if(fault_fields == 0) {
232 log_error("ves_create_fault_fields failed\n");
233 cJSON_Delete(post_data_json);
234 return NTS_ERR_FAILED;
236 cJSON_AddItemToObject(event, "faultFields", fault_fields);
238 char *post_data = cJSON_PrintUnformatted(post_data_json);
239 ves_details_t *ves_details = ves_endpoint_details_get(session);
241 log_error("ves_endpoint_details_get failed\n");
242 return NTS_ERR_FAILED;
245 int rc = http_request(ves_details->url, ves_details->username, ves_details->password, "POST", post_data, 0, 0);
246 ves_details_free(ves_details);
247 cJSON_Delete(post_data_json);
250 if(rc != NTS_ERR_OK) {
251 log_error("http_request failed\n");
252 return NTS_ERR_FAILED;