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 ***************************************************************************/
20 #include "nts_utils.h"
21 #include "utils/log_utils.h"
22 #include "utils/sys_utils.h"
23 #include "core/framework.h"
24 #include "core/session.h"
29 #define MOUNT_POINT_ADDRESSING_METHOD_SCHEMA_XPATH "/nts-network-function:simulation/network-function/mount-point-addressing-method"
31 cJSON* ves_create_common_event_header(const char *domain, const char *event_type, const char *source_name, const char *priority, int seq_id) {
38 long useconds = get_microseconds_since_epoch();
40 asprintf(&eventId, "%s-%d", event_type, seq_id);
42 log_error("asprintf failed");
46 cJSON *common_event_header = cJSON_CreateObject();
47 if(common_event_header == 0) {
48 log_error("could not create JSON object");
53 if(cJSON_AddStringToObject(common_event_header, "domain", domain) == 0) {
54 log_error("cJSON AddStringToObject error");
56 cJSON_Delete(common_event_header);
60 if(cJSON_AddStringToObject(common_event_header, "eventId", eventId) == 0) {
61 log_error("cJSON AddStringToObject error");
63 cJSON_Delete(common_event_header);
69 if(cJSON_AddStringToObject(common_event_header, "eventName", event_type) == 0) {
70 log_error("cJSON AddStringToObject error");
71 cJSON_Delete(common_event_header);
75 if(cJSON_AddNumberToObject(common_event_header, "sequence", (double)(seq_id)) == 0) {
76 log_error("cJSON AddNumberToObject error");
77 cJSON_Delete(common_event_header);
81 if(cJSON_AddStringToObject(common_event_header, "priority", priority) == 0) {
82 log_error("cJSON AddStringToObject error");
83 cJSON_Delete(common_event_header);
87 if(cJSON_AddStringToObject(common_event_header, "reportingEntityId", "") == 0) {
88 log_error("cJSON AddStringToObject error");
89 cJSON_Delete(common_event_header);
93 if(cJSON_AddStringToObject(common_event_header, "reportingEntityName", source_name) == 0) {
94 log_error("cJSON AddStringToObject error");
95 cJSON_Delete(common_event_header);
99 if(cJSON_AddStringToObject(common_event_header, "sourceId", "") == 0) {
100 log_error("cJSON AddStringToObject error");
101 cJSON_Delete(common_event_header);
105 if(cJSON_AddStringToObject(common_event_header, "sourceName", source_name) == 0) {
106 log_error("cJSON AddStringToObject error");
107 cJSON_Delete(common_event_header);
111 if(cJSON_AddNumberToObject(common_event_header, "startEpochMicrosec", (double)(useconds)) == 0) {
112 log_error("cJSON AddNumberToObject error");
113 cJSON_Delete(common_event_header);
117 if(cJSON_AddNumberToObject(common_event_header, "lastEpochMicrosec", (double)(useconds)) == 0) {
118 log_error("cJSON AddNumberToObject error");
119 cJSON_Delete(common_event_header);
123 if(cJSON_AddStringToObject(common_event_header, "nfNamingCode", "sdn controller") == 0) {
124 log_error("cJSON AddStringToObject error");
125 cJSON_Delete(common_event_header);
129 if(cJSON_AddStringToObject(common_event_header, "nfVendorName", "sdn") == 0) {
130 log_error("cJSON AddStringToObject error");
131 cJSON_Delete(common_event_header);
135 if(cJSON_AddStringToObject(common_event_header, "timeZoneOffset", "+00:00") == 0) {
136 log_error("cJSON AddStringToObject error");
137 cJSON_Delete(common_event_header);
141 if(cJSON_AddStringToObject(common_event_header, "version", "4.1") == 0) {
142 log_error("cJSON AddStringToObject error");
143 cJSON_Delete(common_event_header);
147 if(cJSON_AddStringToObject(common_event_header, "vesEventListenerVersion", "7.2") == 0) {
148 log_error("cJSON AddStringToObject error");
149 cJSON_Delete(common_event_header);
153 return common_event_header;
156 nts_mount_point_addressing_method_t nts_mount_point_addressing_method_get(sr_session_ctx_t *current_session) {
159 nts_mount_point_addressing_method_t ret = UNKNOWN_MAPPING;
162 bool session_started = false;
163 if(current_session == 0) {
164 rc = sr_session_start(session_connection, SR_DS_RUNNING, ¤t_session);
165 if(rc != SR_ERR_OK) {
166 log_error("could not start sysrepo session");
169 session_started = true;
173 rc = sr_get_item(session_running, MOUNT_POINT_ADDRESSING_METHOD_SCHEMA_XPATH, 0, &value);
174 if(rc == SR_ERR_OK) {
175 if(strcmp(value->data.enum_val, "host-mapping") == 0) {
179 ret = DOCKER_MAPPING;
184 if(session_started) {
185 rc = sr_session_stop(current_session);
186 if(rc != SR_ERR_OK) {
187 log_error("could not stop sysrepo session");
195 // checkAS authentication via certificate not supported yet
196 ves_details_t *ves_endpoint_details_get(sr_session_ctx_t *current_session) {
200 bool session_started = false;
201 if(current_session == 0) {
202 rc = sr_session_start(session_connection, SR_DS_RUNNING, ¤t_session);
203 if(rc != SR_ERR_OK) {
204 log_error("could not start sysrepo session");
207 session_started = true;
210 struct lyd_node *data = 0;
213 if(framework_arguments.manager) {
214 xpath_to_get = "/nts-manager:simulation/ves-endpoint";
217 xpath_to_get = "/nts-network-function:simulation/ves-endpoint";
220 rc = sr_get_subtree(current_session, xpath_to_get, 0, &data);
221 if(rc != SR_ERR_OK) {
222 log_error("could not get value for xPath=%s from the running datastore\n", xpath_to_get);
223 if(session_started) {
224 sr_session_stop(current_session);
229 if(session_started) {
230 rc = sr_session_stop(current_session);
231 if(rc != SR_ERR_OK) {
232 log_error("could not stop sysrepo session");
238 if(data->child == 0) {
239 log_error("ves-endpoint probably not set yet\n", xpath_to_get);
244 ves_details_t *ret = (ves_details_t *)malloc(sizeof(ves_details_t));
246 log_error("malloc failed");
254 ret->auth_method = 0;
258 struct lyd_node *chd = 0;
259 LY_TREE_FOR(data->child, chd) {
260 const char *val = ((const struct lyd_node_leaf_list *)chd)->value_str;
262 if(strcmp(chd->schema->name, "ves-endpoint-protocol") == 0) {
263 ret->protocol = strdup(val);
265 else if(strcmp(chd->schema->name, "ves-endpoint-ip") == 0) {
266 ret->ip = strdup(val);
268 else if(strcmp(chd->schema->name, "ves-endpoint-port") == 0) {
269 ret->port = ((const struct lyd_node_leaf_list *)chd)->value.uint16;
271 else if(strcmp(chd->schema->name, "ves-endpoint-auth-method") == 0) {
272 ret->auth_method = strdup(val);
274 else if(strcmp(chd->schema->name, "ves-endpoint-username") == 0) {
275 ret->username = strdup(val);
277 else if(strcmp(chd->schema->name, "ves-endpoint-password") == 0) {
278 ret->password = strdup(val);
283 asprintf(&ret->url, "%s://%s:%d/eventListener/v7", ret->protocol, ret->ip, ret->port);
284 if((ret->protocol == 0) || (ret->ip == 0) || (ret->auth_method == 0) || (ret->username == 0) || (ret->password == 0) || (ret->url == 0)) {
287 free(ret->auth_method);
298 void ves_details_free(ves_details_t *instance) {
301 free(instance->protocol);
304 free(instance->auth_method);
305 free(instance->username);
306 free(instance->password);
311 // checkAS authentication via certificate not supported yet
312 controller_details_t *controller_details_get(sr_session_ctx_t *current_session) {
316 bool session_started = false;
317 if(current_session == 0) {
318 rc = sr_session_start(session_connection, SR_DS_RUNNING, ¤t_session);
319 if(rc != SR_ERR_OK) {
320 log_error("could not start sysrepo session");
323 session_started = true;
326 struct lyd_node *data = 0;
329 if(framework_arguments.manager) {
330 xpath_to_get = "/nts-manager:simulation/sdn-controller";
333 xpath_to_get = "/nts-network-function:simulation/sdn-controller";
336 rc = sr_get_subtree(current_session, xpath_to_get, 0, &data);
337 if(rc != SR_ERR_OK) {
338 log_error("could not get value for xPath=%s from the running datastore\n", xpath_to_get);
339 if(session_started) {
340 sr_session_stop(current_session);
345 if(session_started) {
346 rc = sr_session_stop(current_session);
347 if(rc != SR_ERR_OK) {
348 log_error("could not stop sysrepo session");
354 if(data->child == 0) {
355 log_error("sdn-controller probably not set yet\n");
360 controller_details_t *ret = (controller_details_t *)malloc(sizeof(controller_details_t));
362 log_error("malloc failed");
370 ret->nc_callhome_port = 0;
371 ret->auth_method = 0;
375 ret->protocol = strdup("http");
376 ret->auth_method = strdup("basic");
378 struct lyd_node *chd = 0;
379 LY_TREE_FOR(data->child, chd) {
380 const char *val = ((const struct lyd_node_leaf_list *)chd)->value_str;
382 if(strcmp(chd->schema->name, "controller-ip") == 0) {
383 ret->ip = strdup(val);
385 else if(strcmp(chd->schema->name, "controller-port") == 0) {
386 ret->port = ((const struct lyd_node_leaf_list *)chd)->value.uint16;
388 else if(strcmp(chd->schema->name, "controller-netconf-call-home-port") == 0) {
389 ret->nc_callhome_port = ((const struct lyd_node_leaf_list *)chd)->value.uint16;
391 else if(strcmp(chd->schema->name, "controller-username") == 0) {
392 ret->username = strdup(val);
394 else if(strcmp(chd->schema->name, "controller-password") == 0) {
395 ret->password = strdup(val);
400 asprintf(&ret->base_url, "%s://%s:%d", ret->protocol, ret->ip, ret->port);
401 if((ret->protocol == 0) || (ret->ip == 0) || (ret->auth_method == 0) || (ret->username == 0) || (ret->password == 0) || (ret->base_url == 0)) {
404 free(ret->auth_method);
415 void controller_details_free(controller_details_t *instance) {
418 free(instance->protocol);
420 free(instance->base_url);
421 free(instance->auth_method);
422 free(instance->username);
423 free(instance->password);