Rewrite NTS Framework.
[sim/o1-interface.git] / ntsimulator / ntsim-ng / utils / nts_utils.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 "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"
25 #include <stdlib.h>
26 #include <stdio.h>
27 #include <assert.h>
28
29 #define MOUNT_POINT_ADDRESSING_METHOD_SCHEMA_XPATH  "/nts-network-function:simulation/network-function/mount-point-addressing-method"
30
31 cJSON* ves_create_common_event_header(const char *domain, const char *event_type, const char *source_name, const char *priority, int seq_id) {
32     assert(domain);
33     assert(event_type);
34     assert(source_name);
35     assert(priority);
36
37     char *eventId = 0;
38     long useconds = get_microseconds_since_epoch();
39
40     asprintf(&eventId, "%s-%d", event_type, seq_id);
41     if(eventId == 0) {
42         log_error("asprintf failed");
43         return 0;
44     }
45
46     cJSON *common_event_header = cJSON_CreateObject();
47     if(common_event_header == 0) {
48         log_error("could not create JSON object");
49         free(eventId);
50         return 0;
51     }
52
53     if(cJSON_AddStringToObject(common_event_header, "domain", domain) == 0) {
54         log_error("cJSON AddStringToObject error");
55         free(eventId);
56         cJSON_Delete(common_event_header);
57         return 0;
58     }
59
60     if(cJSON_AddStringToObject(common_event_header, "eventId", eventId) == 0) {
61         log_error("cJSON AddStringToObject error");
62         free(eventId);
63         cJSON_Delete(common_event_header);
64         return 0;
65     }
66
67     free(eventId);
68
69     if(cJSON_AddStringToObject(common_event_header, "eventName", event_type) == 0) {
70         log_error("cJSON AddStringToObject error");
71         cJSON_Delete(common_event_header);
72         return 0;
73     }
74
75     if(cJSON_AddNumberToObject(common_event_header, "sequence", (double)(seq_id)) == 0) {
76         log_error("cJSON AddNumberToObject error");
77         cJSON_Delete(common_event_header);
78         return 0;
79     }
80
81     if(cJSON_AddStringToObject(common_event_header, "priority", priority) == 0) {
82         log_error("cJSON AddStringToObject error");
83         cJSON_Delete(common_event_header);
84         return 0;
85     }
86
87     if(cJSON_AddStringToObject(common_event_header, "reportingEntityId", "") == 0) {
88         log_error("cJSON AddStringToObject error");
89         cJSON_Delete(common_event_header);
90         return 0;
91     }
92
93     if(cJSON_AddStringToObject(common_event_header, "reportingEntityName", source_name) == 0) {
94         log_error("cJSON AddStringToObject error");
95         cJSON_Delete(common_event_header);
96         return 0;
97     }
98
99     if(cJSON_AddStringToObject(common_event_header, "sourceId", "") == 0) {
100         log_error("cJSON AddStringToObject error");
101         cJSON_Delete(common_event_header);
102         return 0;
103     }
104
105     if(cJSON_AddStringToObject(common_event_header, "sourceName", source_name) == 0) {
106         log_error("cJSON AddStringToObject error");
107         cJSON_Delete(common_event_header);
108         return 0;
109     }
110
111     if(cJSON_AddNumberToObject(common_event_header, "startEpochMicrosec", (double)(useconds)) == 0) {
112         log_error("cJSON AddNumberToObject error");
113         cJSON_Delete(common_event_header);
114         return 0;
115     }
116
117     if(cJSON_AddNumberToObject(common_event_header, "lastEpochMicrosec", (double)(useconds)) == 0) {
118         log_error("cJSON AddNumberToObject error");
119         cJSON_Delete(common_event_header);
120         return 0;
121     }
122
123     if(cJSON_AddStringToObject(common_event_header, "nfNamingCode", "sdn controller") == 0) {
124         log_error("cJSON AddStringToObject error");
125         cJSON_Delete(common_event_header);
126         return 0;
127     }
128
129     if(cJSON_AddStringToObject(common_event_header, "nfVendorName", "sdn") == 0) {
130         log_error("cJSON AddStringToObject error");
131         cJSON_Delete(common_event_header);
132         return 0;
133     }
134
135     if(cJSON_AddStringToObject(common_event_header, "timeZoneOffset", "+00:00") == 0) {
136         log_error("cJSON AddStringToObject error");
137         cJSON_Delete(common_event_header);
138         return 0;
139     }
140
141     if(cJSON_AddStringToObject(common_event_header, "version", "4.1") == 0) {
142         log_error("cJSON AddStringToObject error");
143         cJSON_Delete(common_event_header);
144         return 0;
145     }
146
147     if(cJSON_AddStringToObject(common_event_header, "vesEventListenerVersion", "7.2") == 0) {
148         log_error("cJSON AddStringToObject error");
149         cJSON_Delete(common_event_header);
150         return 0;
151     }
152
153     return common_event_header;
154 }
155
156 nts_mount_point_addressing_method_t nts_mount_point_addressing_method_get(sr_session_ctx_t *current_session) {
157     assert_session();
158
159     nts_mount_point_addressing_method_t ret = UNKNOWN_MAPPING;
160
161     int rc;
162     bool session_started = false;
163     if(current_session == 0) {
164         rc = sr_session_start(session_connection, SR_DS_RUNNING, &current_session);
165         if(rc != SR_ERR_OK) {
166             log_error("could not start sysrepo session");
167             return ret;
168         }
169         session_started = true;
170     }
171
172     sr_val_t *value = 0;
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) {
176             ret = HOST_MAPPING;
177         }
178         else {
179             ret = DOCKER_MAPPING;
180         }
181         sr_free_val(value);
182     }
183
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");
188             return ret;
189         }
190     }
191
192     return ret;
193 }
194
195 // checkAS authentication via certificate not supported yet
196 ves_details_t *ves_endpoint_details_get(sr_session_ctx_t *current_session) {
197     assert_session();
198
199     int rc;
200     bool session_started = false;
201     if(current_session == 0) {
202         rc = sr_session_start(session_connection, SR_DS_RUNNING, &current_session);
203         if(rc != SR_ERR_OK) {
204             log_error("could not start sysrepo session");
205             return 0;
206         }
207         session_started = true;
208     }
209
210     struct lyd_node *data = 0;
211     char *xpath_to_get;
212
213     if(framework_arguments.manager) {
214         xpath_to_get = "/nts-manager:simulation/ves-endpoint";
215     }
216     else {
217         xpath_to_get = "/nts-network-function:simulation/ves-endpoint";
218     }
219
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);
225         }
226         return 0;
227     }
228
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");
233             lyd_free(data);
234             return 0;
235         }
236     }
237
238     if(data->child == 0) {
239         log_error("ves-endpoint probably not set yet\n", xpath_to_get);
240         lyd_free(data);
241         return 0;
242     }
243
244     ves_details_t *ret = (ves_details_t *)malloc(sizeof(ves_details_t));
245     if(!ret) {
246         log_error("malloc failed");
247         lyd_free(data);
248         return 0;
249     }
250
251     ret->protocol = 0;
252     ret->ip = 0;
253     ret->port = 0;
254     ret->auth_method = 0;
255     ret->username = 0;
256     ret->password = 0;
257
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;
261
262         if(strcmp(chd->schema->name, "ves-endpoint-protocol") == 0) {
263             ret->protocol = strdup(val);
264         }
265         else if(strcmp(chd->schema->name, "ves-endpoint-ip") == 0) {
266             ret->ip = strdup(val);
267         }
268         else if(strcmp(chd->schema->name, "ves-endpoint-port") == 0) {
269             ret->port = ((const struct lyd_node_leaf_list *)chd)->value.uint16;
270         }
271         else if(strcmp(chd->schema->name, "ves-endpoint-auth-method") == 0) {
272             ret->auth_method = strdup(val);
273         }
274         else if(strcmp(chd->schema->name, "ves-endpoint-username") == 0) {
275             ret->username = strdup(val);
276         }
277         else if(strcmp(chd->schema->name, "ves-endpoint-password") == 0) {
278             ret->password = strdup(val);
279         }
280     }
281     lyd_free(data);
282
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)) {
285         free(ret->protocol);
286         free(ret->ip);
287         free(ret->auth_method);
288         free(ret->username);
289         free(ret->password);
290         free(ret->url);
291         free(ret);
292         ret = 0;
293     }
294
295     return ret;
296 }
297
298 void ves_details_free(ves_details_t *instance) {
299     assert(instance);
300
301     free(instance->protocol);
302     free(instance->ip);
303     free(instance->url);
304     free(instance->auth_method);
305     free(instance->username);
306     free(instance->password);
307     free(instance);
308 }
309
310
311 // checkAS authentication via certificate not supported yet
312 controller_details_t *controller_details_get(sr_session_ctx_t *current_session) {
313     assert_session();
314
315     int rc;
316     bool session_started = false;
317     if(current_session == 0) {
318         rc = sr_session_start(session_connection, SR_DS_RUNNING, &current_session);
319         if(rc != SR_ERR_OK) {
320             log_error("could not start sysrepo session");
321             return 0;
322         }
323         session_started = true;
324     }
325
326     struct lyd_node *data = 0;
327     char *xpath_to_get;
328
329     if(framework_arguments.manager) {
330         xpath_to_get = "/nts-manager:simulation/sdn-controller";
331     }
332     else {
333         xpath_to_get = "/nts-network-function:simulation/sdn-controller";
334     }
335
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);
341         }
342         return 0;
343     }
344
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");
349             lyd_free(data);
350             return 0;
351         }
352     }
353
354     if(data->child == 0) {
355         log_error("sdn-controller probably not set yet\n");
356         lyd_free(data);
357         return 0;
358     }
359
360     controller_details_t *ret = (controller_details_t *)malloc(sizeof(controller_details_t));
361     if(!ret) {
362         log_error("malloc failed");
363         lyd_free(data);
364         return 0;
365     }
366
367     ret->protocol = 0;
368     ret->ip = 0;
369     ret->port = 0;
370     ret->nc_callhome_port = 0;
371     ret->auth_method = 0;
372     ret->username = 0;
373     ret->password = 0;
374
375     ret->protocol = strdup("http");
376     ret->auth_method = strdup("basic");
377
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;
381
382         if(strcmp(chd->schema->name, "controller-ip") == 0) {
383             ret->ip = strdup(val);
384         }
385         else if(strcmp(chd->schema->name, "controller-port") == 0) {
386             ret->port = ((const struct lyd_node_leaf_list *)chd)->value.uint16;
387         }
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;
390         }
391         else if(strcmp(chd->schema->name, "controller-username") == 0) {
392             ret->username = strdup(val);
393         }
394         else if(strcmp(chd->schema->name, "controller-password") == 0) {
395             ret->password = strdup(val);
396         }
397     }
398     lyd_free(data);
399
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)) {
402         free(ret->protocol);
403         free(ret->ip);
404         free(ret->auth_method);
405         free(ret->username);
406         free(ret->password);
407         free(ret->base_url);
408         free(ret);
409         ret = 0;
410     }
411
412     return ret;
413 }
414
415 void controller_details_free(controller_details_t *instance) {
416     assert(instance);
417
418     free(instance->protocol);
419     free(instance->ip);
420     free(instance->base_url);
421     free(instance->auth_method);
422     free(instance->username);
423     free(instance->password);
424     free(instance);
425 }