Add VES stndDefined PM and subscription for O-DU.
[sim/o1-interface.git] / ntsimulator / ntsim-ng / features / ves_pnf_registration / ves_pnf_registration.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 "ves_pnf_registration.h"
21 #include "utils/log_utils.h"
22 #include "utils/sys_utils.h"
23 #include "utils/rand_utils.h"
24 #include "utils/http_client.h"
25 #include "utils/nts_utils.h"
26 #include <stdio.h>
27 #include <assert.h>
28 #include <pthread.h>
29
30 #include "core/session.h"
31 #include "core/framework.h"
32 #include "core/xpath.h"
33
34 static int ves_pnf_sequence_number = 0;
35 static pthread_t ves_pnf_registration_thread;
36 static void* ves_pnf_registration_thread_routine(void *arg);
37 static int ves_pnf_registration_send(sr_session_ctx_t *current_session, const char *nf_ip_v4_address, const char *nf_ip_v6_address, int nf_port, nts_mount_point_addressing_method_t mp, bool is_tls);
38 static cJSON* ves_create_pnf_registration_fields(const char *nf_ip_v4_address, const char *nf_ip_v6_address, int nf_port, bool is_tls);
39
40 static int ves_pnf_registration_status = 0;
41
42 int ves_pnf_registration_feature_get_status(void) {
43     return ves_pnf_registration_status;
44 }
45
46 int ves_pnf_registration_feature_start(sr_session_ctx_t *current_session) {
47     assert(current_session);
48
49     ves_pnf_sequence_number = 0;
50
51     sr_val_t *value = 0;
52     int rc = NTS_ERR_OK;
53     bool pnf_registration_enabled = false;
54     if(strlen(framework_environment.nts.nf_standalone_start_features)) {
55         pnf_registration_enabled = true;
56     }
57     else {
58         rc = sr_get_item(current_session, NTS_NF_VES_PNF_REGISTRATION_SCHEMA_XPATH, 0, &value);
59         if(rc == SR_ERR_OK) {
60             pnf_registration_enabled = value->data.bool_val;
61             sr_free_val(value);
62         }
63         else if(rc != SR_ERR_NOT_FOUND) {
64             log_error("sr_get_item failed\n");
65             return NTS_ERR_FAILED;
66         }
67     }
68
69     if(pnf_registration_enabled == false) {
70         log_add_verbose(2, "PNF registration is disabled\n");
71         return NTS_ERR_OK;
72     }
73
74     if(pthread_create(&ves_pnf_registration_thread, 0, ves_pnf_registration_thread_routine, current_session)) {
75         log_error("could not create thread for heartbeat\n");
76         return NTS_ERR_FAILED;
77     }
78
79     return NTS_ERR_OK;
80 }
81
82 static void* ves_pnf_registration_thread_routine(void *arg) {
83     sr_session_ctx_t *current_session = arg;
84
85     int ssh_base_port = 0;
86     int tls_base_port = 0;
87     char nf_ip_v4_address[128];
88     char nf_ip_v6_address[128];
89
90     nf_ip_v4_address[0] = 0;
91     nf_ip_v6_address[0] = 0;
92     
93     nts_mount_point_addressing_method_t mp = nts_mount_point_addressing_method_get(current_session);
94     if(mp == UNKNOWN_MAPPING) {
95         log_error("mount-point-addressing-method failed\n");
96         return (void*)NTS_ERR_FAILED;
97     }
98     else if(mp == DOCKER_MAPPING) {
99         if (framework_environment.settings.ip_v4 != 0) {
100             strcpy(nf_ip_v4_address, framework_environment.settings.ip_v4);
101         }
102         if (framework_environment.settings.ip_v6 && framework_environment.settings.ip_v6_enabled) {
103             strcpy(nf_ip_v6_address, framework_environment.settings.ip_v6);
104         }
105
106         ssh_base_port = STANDARD_NETCONF_PORT;
107         tls_base_port = ssh_base_port + framework_environment.settings.ssh_connections;
108     }
109     else {
110         if(framework_environment.settings.ip_v6_enabled) {
111             strcpy(nf_ip_v6_address, framework_environment.host.ip);
112         }
113         else {
114             strcpy(nf_ip_v4_address, framework_environment.host.ip);
115         }
116
117         ssh_base_port = framework_environment.host.ssh_base_port;
118         tls_base_port = framework_environment.host.tls_base_port;
119     }
120
121     uint32_t total_regs = 0;
122     struct regs_s {
123         bool sent;
124         uint16_t port;
125         bool is_tls;
126     } *regs;
127
128     regs = (struct regs_s *)malloc(sizeof(struct regs_s) * (1 + framework_environment.settings.ssh_connections + framework_environment.settings.tls_connections));
129     if(regs == 0) {
130         log_error("malloc failed\n");
131         return (void*)NTS_ERR_FAILED;
132     }
133
134
135     if((framework_environment.settings.ssh_connections + framework_environment.settings.tls_connections) > 1) {
136         for(int port = ssh_base_port; port < ssh_base_port + framework_environment.settings.ssh_connections; port++) {
137             regs[total_regs].sent = false;
138             regs[total_regs].port = port;
139             regs[total_regs].is_tls = false;
140             total_regs++;
141         }
142
143         for(int port = tls_base_port; port < tls_base_port + framework_environment.settings.tls_connections; port++) {
144             regs[total_regs].sent = false;
145             regs[total_regs].port = port;
146             regs[total_regs].is_tls = true;
147             total_regs++;
148         }
149     }
150     else {
151         bool tls;
152         if(framework_environment.settings.tls_connections == 0) {
153             tls = false;
154         }
155         else {
156             tls = true;
157         }
158
159         regs[total_regs].sent = false;
160         regs[total_regs].port = 0;
161         regs[total_regs].is_tls = tls;
162         total_regs++;
163     }
164
165     uint32_t remaining = total_regs;
166     while(remaining) {
167         for(int i = 0; i < total_regs; i++) {
168             if(regs[i].sent == false) {
169                 uint16_t port = regs[i].port;
170                 bool is_tls = regs[i].is_tls;
171                 int rc = ves_pnf_registration_send(current_session, nf_ip_v4_address, nf_ip_v6_address, port, mp, is_tls);
172                 if(rc == NTS_ERR_OK) {
173                     remaining--;
174                     regs[i].sent = true;
175                 }
176                 else {
177                     log_error("pnfRegistration failed for ipv4=%s ipv6=%s port=%d is_tls=%d\n", nf_ip_v4_address, nf_ip_v6_address, port, is_tls);
178                 }
179             }
180         }
181         if(remaining) {
182             log_error("pnfRegistration could not register all ports; retrying in 5 seconds...\n");
183             sleep(5);
184         }
185     }
186     free(regs);
187     log_add_verbose(2, "PNF registration finished\n");
188     ves_pnf_registration_status = 1;
189
190     return NTS_ERR_OK;
191 }
192
193 static int ves_pnf_registration_send(sr_session_ctx_t *current_session, const char *nf_ip_v4_address, const char *nf_ip_v6_address, int nf_port, nts_mount_point_addressing_method_t mp, bool is_tls) {
194     assert(current_session);
195
196     cJSON *post_data_json = cJSON_CreateObject();
197     if(post_data_json == 0) {
198         log_error("could not create cJSON object\n");
199         return NTS_ERR_FAILED;
200     }
201
202     cJSON *event = cJSON_CreateObject();
203     if(event == 0) {
204         log_error("could not create cJSON object\n");
205         cJSON_Delete(post_data_json);
206         return NTS_ERR_FAILED;
207     }
208     
209     if(cJSON_AddItemToObject(post_data_json, "event", event) == 0) {
210         log_error("cJSON_AddItemToObject failed\n");
211         cJSON_Delete(post_data_json);
212         return NTS_ERR_FAILED;
213     }
214
215     char *hostname_string = framework_environment.settings.hostname;
216     cJSON *common_event_header = ves_create_common_event_header("pnfRegistration", "EventType5G", hostname_string, nf_port, "Normal", ves_pnf_sequence_number++);
217     if(common_event_header == 0) {
218         log_error("could not create cJSON object\n");
219         cJSON_Delete(post_data_json);
220         return NTS_ERR_FAILED;
221     }
222
223     if(nf_port == 0) {
224         if(mp == DOCKER_MAPPING) {
225             nf_port = STANDARD_NETCONF_PORT;
226         }
227         else {
228             if(is_tls) {
229                 nf_port = framework_environment.host.tls_base_port;
230             }
231             else {
232                 nf_port = framework_environment.host.ssh_base_port;
233             }
234         }
235     }
236     
237     if(cJSON_AddItemToObject(event, "commonEventHeader", common_event_header) == 0) {
238         log_error("cJSON_AddItemToObject failed\n");
239         cJSON_Delete(post_data_json);
240         return NTS_ERR_FAILED;
241     }
242
243         cJSON *pnf_registration_fields = ves_create_pnf_registration_fields(nf_ip_v4_address, nf_ip_v6_address, nf_port, is_tls);
244     if(pnf_registration_fields == 0) {
245         log_error("could not create cJSON object\n");
246         cJSON_Delete(post_data_json);
247         return NTS_ERR_FAILED;
248     }
249     
250     if(cJSON_AddItemToObject(event, "pnfRegistrationFields", pnf_registration_fields) == 0) {
251         log_error("cJSON_AddItemToObject failed\n");
252         cJSON_Delete(post_data_json);
253         return NTS_ERR_FAILED;
254     }
255
256     char *post_data = cJSON_PrintUnformatted(post_data_json);
257     cJSON_Delete(post_data_json);
258     if(post_data == 0) {
259         log_error("cJSON_PrintUnformatted failed\n");
260         return NTS_ERR_FAILED;
261     }
262
263
264     ves_details_t *ves_details = ves_endpoint_details_get(current_session, 0);
265     if(!ves_details) {
266         log_error("ves_endpoint_details_get failed\n");
267         free(post_data);
268         return NTS_ERR_FAILED;
269     }
270     
271     int rc = http_request(ves_details->url, ves_details->username, ves_details->password, "POST", post_data, 0, 0);
272     ves_details_free(ves_details);
273     free(post_data);
274     
275     if(rc != NTS_ERR_OK) {
276         log_error("http_request failed\n");
277         return NTS_ERR_FAILED;
278     }
279
280     return NTS_ERR_OK;
281 }
282
283 static cJSON* ves_create_pnf_registration_fields(const char *nf_ip_v4_address, const char *nf_ip_v6_address, int nf_port, bool is_tls) {
284
285     //checkAL aici n-ar trebui niste valori "adevarate" ?
286
287     cJSON *pnf_registration_fields = cJSON_CreateObject();
288     if(pnf_registration_fields == 0) {
289         log_error("could not create JSON object\n");
290         return 0;
291     }
292
293     if(cJSON_AddStringToObject(pnf_registration_fields, "pnfRegistrationFieldsVersion", "2.0") == 0) {
294         log_error("cJSON_AddItemToObject failed\n");
295         cJSON_Delete(pnf_registration_fields);
296         return 0;
297     }
298
299     if(cJSON_AddStringToObject(pnf_registration_fields, "lastServiceDate", "2019-08-16") == 0) {
300         log_error("cJSON_AddItemToObject failed\n");
301         cJSON_Delete(pnf_registration_fields);
302         return 0;
303     }
304
305     char *mac_addr = rand_mac_address();
306     if(mac_addr == 0) {
307         log_error("rand_mac_address failed\n")
308         cJSON_Delete(pnf_registration_fields);
309         return 0;
310     }
311
312     if(cJSON_AddStringToObject(pnf_registration_fields, "macAddress", mac_addr) == 0) {
313         log_error("cJSON_AddItemToObject failed\n");
314         cJSON_Delete(pnf_registration_fields);
315         free(mac_addr);
316         return 0;
317     }
318     free(mac_addr);
319
320     if(cJSON_AddStringToObject(pnf_registration_fields, "manufactureDate", "2019-08-16") == 0) {
321         log_error("cJSON_AddItemToObject failed\n");
322         cJSON_Delete(pnf_registration_fields);
323         return 0;
324     }
325
326     if(cJSON_AddStringToObject(pnf_registration_fields, "modelNumber", "Simulated Device Melacon") == 0) {
327         log_error("cJSON_AddItemToObject failed\n");
328         cJSON_Delete(pnf_registration_fields);
329         return 0;
330     }
331
332     if (nf_ip_v4_address != 0 && strlen(nf_ip_v4_address) > 0) {
333         if(cJSON_AddStringToObject(pnf_registration_fields, "oamV4IpAddress", nf_ip_v4_address) == 0) {
334             log_error("cJSON_AddItemToObject failed\n");
335             cJSON_Delete(pnf_registration_fields);
336             return 0;
337         }
338     }
339
340     if (nf_ip_v6_address != 0 && strlen(nf_ip_v6_address) > 0) {
341         if(cJSON_AddStringToObject(pnf_registration_fields, "oamV6IpAddress", nf_ip_v6_address) == 0) {
342             log_error("cJSON_AddItemToObject failed\n");
343             cJSON_Delete(pnf_registration_fields);
344             return 0;
345         }
346     }
347
348     char serial_number[512];
349     sprintf(serial_number, "%s-%s-%d-Simulated Device Melacon", framework_environment.settings.hostname, nf_ip_v4_address, nf_port);
350
351     if(cJSON_AddStringToObject(pnf_registration_fields, "serialNumber", serial_number) == 0) {
352         log_error("cJSON_AddItemToObject failed\n");
353         cJSON_Delete(pnf_registration_fields);
354         return 0;
355     }
356
357     if(cJSON_AddStringToObject(pnf_registration_fields, "softwareVersion", "2.3.5") == 0) {
358         log_error("cJSON_AddItemToObject failed\n");
359         cJSON_Delete(pnf_registration_fields);
360         return 0;
361     }
362
363     if(cJSON_AddStringToObject(pnf_registration_fields, "unitFamily", "Simulated Device") == 0) {
364         log_error("cJSON_AddItemToObject failed\n");
365         cJSON_Delete(pnf_registration_fields);
366         return 0;
367     }
368
369     if(cJSON_AddStringToObject(pnf_registration_fields, "unitType", "O-RAN-sim") == 0) {
370         log_error("cJSON_AddItemToObject failed\n");
371         cJSON_Delete(pnf_registration_fields);
372         return 0;
373     }
374
375     if(cJSON_AddStringToObject(pnf_registration_fields, "vendorName", "Melacon") == 0) {
376         log_error("cJSON_AddItemToObject failed\n");
377         cJSON_Delete(pnf_registration_fields);
378         return 0;
379     }
380
381     cJSON *additional_fields = cJSON_CreateObject();
382     if(additional_fields == 0) {
383         log_error("could not create JSON object\n");
384         cJSON_Delete(pnf_registration_fields);
385         return 0;
386     }
387     cJSON_AddItemToObject(pnf_registration_fields, "additionalFields", additional_fields);
388
389     char port_string[10];
390     sprintf(port_string, "%d", nf_port);
391
392     if(cJSON_AddStringToObject(additional_fields, "oamPort", port_string) == 0) {
393         log_error("cJSON_AddItemToObject failed\n");
394         cJSON_Delete(pnf_registration_fields);
395         return 0;
396     }
397
398     if(is_tls) {
399         //TLS specific configuration
400         if(cJSON_AddStringToObject(additional_fields, "protocol", "TLS") == 0) {
401             log_error("cJSON_AddItemToObject failed\n");
402             cJSON_Delete(pnf_registration_fields);
403             return 0;
404         }
405
406         if(cJSON_AddStringToObject(additional_fields, "username", "netconf") == 0) {
407             log_error("cJSON_AddItemToObject failed\n");
408             cJSON_Delete(pnf_registration_fields);
409             return 0;
410         }
411
412         if(cJSON_AddStringToObject(additional_fields, "keyId", KS_KEY_NAME) == 0) {
413             log_error("cJSON_AddItemToObject failed\n");
414             cJSON_Delete(pnf_registration_fields);
415             return 0;
416         }
417     }
418     else {
419         //SSH specific configuration
420         if(cJSON_AddStringToObject(additional_fields, "protocol", "SSH") == 0) {
421             log_error("cJSON_AddItemToObject failed\n");
422             cJSON_Delete(pnf_registration_fields);
423             return 0;
424         }
425
426         if(cJSON_AddStringToObject(additional_fields, "username", "netconf") == 0) {
427             log_error("cJSON_AddItemToObject failed\n");
428             cJSON_Delete(pnf_registration_fields);
429             return 0;
430         }
431
432         // hardcoded password here
433         if(cJSON_AddStringToObject(additional_fields, "password", "netconf!") == 0) {
434             log_error("cJSON_AddItemToObject failed\n");
435             cJSON_Delete(pnf_registration_fields);
436             return 0;
437         }
438     }
439
440     if(cJSON_AddStringToObject(additional_fields, "reconnectOnChangedSchema", "false") == 0) {
441         log_error("cJSON_AddItemToObject failed\n");
442         cJSON_Delete(pnf_registration_fields);
443         return 0;
444     }
445
446     if(cJSON_AddStringToObject(additional_fields, "sleep-factor", "1.5") == 0) {
447         log_error("cJSON_AddItemToObject failed\n");
448         cJSON_Delete(pnf_registration_fields);
449         return 0;
450     }
451
452     if(cJSON_AddStringToObject(additional_fields, "tcpOnly", "false") == 0) {
453         log_error("cJSON_AddItemToObject failed\n");
454         cJSON_Delete(pnf_registration_fields);
455         return 0;
456     }
457
458     if(cJSON_AddStringToObject(additional_fields, "connectionTimeout", "20000") == 0) {
459         log_error("cJSON_AddItemToObject failed\n");
460         cJSON_Delete(pnf_registration_fields);
461         return 0;
462     }
463
464     if(cJSON_AddStringToObject(additional_fields, "maxConnectionAttempts", "100") == 0) {
465         log_error("cJSON_AddItemToObject failed\n");
466         cJSON_Delete(pnf_registration_fields);
467         return 0;
468     }
469
470     if(cJSON_AddStringToObject(additional_fields, "betweenAttemptsTimeout", "2000") == 0) {
471         log_error("cJSON_AddItemToObject failed\n");
472         cJSON_Delete(pnf_registration_fields);
473         return 0;
474     }
475
476     if(cJSON_AddStringToObject(additional_fields, "keepaliveDelay", "120") == 0) {
477         log_error("cJSON_AddItemToObject failed\n");
478         cJSON_Delete(pnf_registration_fields);
479         return 0;
480     }
481
482     return pnf_registration_fields;
483 }