Add License information in every source file or script.
[sim/o1-interface.git] / ntsimulator / src / ntsimulator-manager / ntsimulator-manager.c
1 /*************************************************************************
2 *
3 * Copyright 2019 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
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <unistd.h>
22 #include <signal.h>
23 #include <inttypes.h>
24 #include <limits.h>
25 #include <string.h>
26
27 #include "sysrepo.h"
28 #include "sysrepo/values.h"
29
30 #include "utils.h"
31 #include "simulator-operations.h"
32
33 volatile int exit_application = 0;
34
35 volatile unsigned int simulated_devices_config = 0;
36 volatile unsigned int mounted_devices_config = 0;
37
38
39 static device_stack_t *device_list = NULL;
40
41 controller_t controller_details;
42
43 #define XPATH_MAX_LEN 500
44 #define CONTROLLER_LIST_MAX_LEN 1
45
46 static void
47 print_current_config(sr_session_ctx_t *session, const char *module_name)
48 {
49     sr_val_t *values = NULL;
50     size_t count = 0;
51     int rc = SR_ERR_OK;
52     char xpath[XPATH_MAX_LEN] = {0};
53     snprintf(xpath, XPATH_MAX_LEN, "/%s:*//.", module_name);
54
55     sr_val_t *odl_ip = NULL;
56     sr_val_t *odl_port = NULL;
57     sr_val_t *odl_username = NULL;
58     sr_val_t *odl_password = NULL;
59
60     rc = sr_get_items(session, xpath, &values, &count);
61     if (SR_ERR_OK != rc) {
62         printf("Error by sr_get_items: %s\n", sr_strerror(rc));
63         return;
64     }
65     for (size_t i = 0; i < count; i++){
66
67         sr_print_val(&values[i]);
68
69         if (sr_xpath_node_name_eq(values[i].xpath, "controller-ip"))
70         {
71                 rc = sr_dup_val(&values[i], &odl_ip);
72         }
73         else if (sr_xpath_node_name_eq(values[i].xpath, "controller-port"))
74         {
75                 rc = sr_dup_val(&values[i], &odl_port);
76         }
77         else if (sr_xpath_node_name_eq(values[i].xpath, "controller-username"))
78         {
79                 rc = sr_dup_val(&values[i], &odl_username);
80         }
81         else if (sr_xpath_node_name_eq(values[i].xpath, "controller-password"))
82         {
83                 rc = sr_dup_val(&values[i], &odl_password);
84         }
85     }
86
87     //URL used for mounting/unmounting a device; the device name needs to be appended
88    char url[URL_AND_CREDENTIALS_MAX_LEN];
89    sprintf(url, "http://%s:%d/restconf/config/network-topology:network-topology/topology/"
90                  "topology-netconf/node/",
91                  odl_ip->data.string_val, odl_port->data.uint32_val);
92
93    char credentials[URL_AND_CREDENTIALS_MAX_LEN];
94    sprintf(credentials, "%s:%s", odl_username->data.string_val, odl_password->data.string_val);
95
96    //URLs used for adding key pair to ODL, for TLS connections
97    char url_for_keystore_add[URL_AND_CREDENTIALS_MAX_LEN];
98    sprintf(url_for_keystore_add, "http://%s:%d/restconf/operations/netconf-keystore:add-keystore-entry",
99                          odl_ip->data.string_val, odl_port->data.uint32_val);
100
101    char url_for_private_key_add[URL_AND_CREDENTIALS_MAX_LEN];
102    sprintf(url_for_private_key_add, "http://%s:%d/restconf/operations/netconf-keystore:add-private-key",
103                          odl_ip->data.string_val, odl_port->data.uint32_val);
104
105    char url_for_trusted_ca_add[URL_AND_CREDENTIALS_MAX_LEN];
106    sprintf(url_for_trusted_ca_add, "http://%s:%d/restconf/operations/netconf-keystore:add-trusted-certificate",
107                          odl_ip->data.string_val, odl_port->data.uint32_val);
108
109    strcpy(controller_details.url, url);
110    strcpy(controller_details.credentials, credentials);
111    strcpy(controller_details.url_for_keystore_add, url_for_keystore_add);
112    strcpy(controller_details.url_for_private_key_add, url_for_private_key_add);
113    strcpy(controller_details.url_for_trusted_ca_add, url_for_trusted_ca_add);
114
115    sr_free_val(odl_ip);
116    sr_free_val(odl_port);
117    sr_free_val(odl_username);
118    sr_free_val(odl_password);
119
120     sr_free_values(values, count);
121 }
122
123 static void clean_current_docker_configuration(void);
124
125 static int simulated_devices_changed(int new_value)
126 {
127         int rc = SR_ERR_OK;
128
129     if (simulated_devices_config > new_value)
130     {
131         //we are configuring less elements that currently
132         for (int i = 0; i < simulated_devices_config - new_value; ++i)
133         {
134                 rc = stop_device(device_list);
135         }
136     }
137     else if (simulated_devices_config < new_value)
138     {
139         //we are configuring more elements that currently
140         for (int i = 0; i < new_value - simulated_devices_config; ++i)
141         {
142                 rc = start_device(device_list);
143         }
144     }
145
146     simulated_devices_config = new_value;
147
148     return rc;
149 }
150
151 int mounted_devices_changed(sr_session_ctx_t *session, int new_value)
152 {
153         int rc = SR_ERR_OK;
154
155         if (mounted_devices_config > new_value)
156         {
157           //we need have less mounted elements
158           for (int i = 0; i < mounted_devices_config - new_value; ++i)
159           {
160                   printf("Sending unmount device...\n");
161                   rc = unmount_device(device_list, controller_details);
162           }
163         }
164         else if (mounted_devices_config < new_value)
165         {
166           //we are configuring more elements that currently
167           for (int i = 0; i < new_value - mounted_devices_config; ++i)
168           {
169                   printf("Sending mount device...\n");
170                   rc = mount_device(device_list, controller_details);
171           }
172         }
173
174         mounted_devices_config = new_value;
175
176     return rc;
177 }
178
179 static int
180 simulator_config_change_cb(sr_session_ctx_t *session, const char *module_name, sr_notif_event_t event, void *private_ctx)
181 {
182         int rc;
183
184     printf("\n\n ========== CONFIG HAS CHANGED, CURRENT RUNNING CONFIG %s: ==========\n\n", module_name);
185     print_current_config(session, module_name);
186
187     sr_val_t *val;
188
189     /* get the value from sysrepo, we do not care if the value did not change in our case */
190     rc = sr_get_item(session, "/network-topology-simulator:simulator-config/simulated-devices", &val);
191     if (rc != SR_ERR_OK) {
192         goto sr_error;
193     }
194
195     rc = simulated_devices_changed(val->data.uint32_val);
196     if (rc != SR_ERR_OK) {
197         goto sr_error;
198     }
199
200     sr_free_val(val);
201
202     /* get the value from sysrepo, we do not care if the value did not change in our case */
203     rc = sr_get_item(session, "/network-topology-simulator:simulator-config/mounted-devices", &val);
204     if (rc != SR_ERR_OK) {
205         goto sr_error;
206     }
207
208     if (mounted_devices_config != val->data.uint32_val)
209     {
210         if (val->data.uint32_val > simulated_devices_config)
211         {
212                 printf("Cannot set mount value greater than number of simulated devices.\n");
213                 sr_free_val(val);
214                 return SR_ERR_OK;
215         }
216
217                 rc = mounted_devices_changed(session, val->data.uint32_val);
218                 if (rc != SR_ERR_OK) {
219                         goto sr_error;
220                 }
221     }
222
223     sr_free_val(val);
224
225     /* get the value from sysrepo, we do not care if the value did not change in our case */
226     rc = sr_get_item(session, "/network-topology-simulator:simulator-config/notification-config/fault-notification-delay-period", &val);
227     if (rc != SR_ERR_OK) {
228         goto sr_error;
229     }
230
231     rc = notification_delay_period_changed(val->data.uint32_val);
232     if (rc != SR_ERR_OK) {
233         goto sr_error;
234     }
235
236     sr_free_val(val);
237
238     /* get the value from sysrepo, we do not care if the value did not change in our case */
239         rc = sr_get_item(session, "/network-topology-simulator:simulator-config/notification-config/ves-heartbeat-period", &val);
240         if (rc != SR_ERR_OK) {
241                 goto sr_error;
242         }
243
244         rc = ves_heartbeat_period_changed(val->data.uint32_val);
245         if (rc != SR_ERR_OK) {
246                 goto sr_error;
247         }
248
249         sr_free_val(val);
250
251         /* get the value from sysrepo, we do not care if the value did not change in our case */
252         rc = sr_get_item(session, "/network-topology-simulator:simulator-config/ves-endpoint-details/ves-endpoint-ip", &val);
253         if (rc != SR_ERR_OK) {
254                 goto sr_error;
255         }
256
257         rc = ves_ip_changed(val->data.string_val);
258         if (rc != SR_ERR_OK) {
259                 goto sr_error;
260         }
261
262         sr_free_val(val);
263
264         /* get the value from sysrepo, we do not care if the value did not change in our case */
265         rc = sr_get_item(session, "/network-topology-simulator:simulator-config/ves-endpoint-details/ves-endpoint-port", &val);
266         if (rc != SR_ERR_OK) {
267                 goto sr_error;
268         }
269
270         rc = ves_port_changed(val->data.uint16_val);
271         if (rc != SR_ERR_OK) {
272                 goto sr_error;
273         }
274
275         sr_free_val(val);
276
277         /* get the value from sysrepo, we do not care if the value did not change in our case */
278         rc = sr_get_item(session, "/network-topology-simulator:simulator-config/ves-endpoint-details/ves-registration", &val);
279         if (rc != SR_ERR_OK) {
280                 goto sr_error;
281         }
282
283         rc = ves_registration_changed(val->data.bool_val);
284         if (rc != SR_ERR_OK) {
285                 goto sr_error;
286         }
287
288         sr_free_val(val);
289
290         /* get the value from sysrepo, we do not care if the value did not change in our case */
291         rc = sr_get_item(session, "/network-topology-simulator:simulator-config/notification-config/is-netconf-available", &val);
292         if (rc != SR_ERR_OK) {
293                 goto sr_error;
294         }
295
296         rc = is_netconf_available_changed(val->data.bool_val);
297         if (rc != SR_ERR_OK) {
298                 goto sr_error;
299         }
300
301         sr_free_val(val);
302
303         /* get the value from sysrepo, we do not care if the value did not change in our case */
304         rc = sr_get_item(session, "/network-topology-simulator:simulator-config/notification-config/is-ves-available", &val);
305         if (rc != SR_ERR_OK) {
306                 goto sr_error;
307         }
308
309         rc = is_ves_available_changed(val->data.bool_val);
310         if (rc != SR_ERR_OK) {
311                 goto sr_error;
312         }
313
314         sr_free_val(val);
315
316     return SR_ERR_OK;
317
318 sr_error:
319         printf("NTSimulator config change callback failed: %s.", sr_strerror(rc));
320         return rc;
321 }
322
323 static int
324 simulator_status_cb(const char *xpath, sr_val_t **values, size_t *values_cnt,
325         uint64_t request_id, const char *original_xpath, void *private_ctx)
326 {
327         int rc;
328
329         printf("\n\n ========== Called simulator_status_cb for xpath: %s ==========\n\n", xpath);
330
331         if (sr_xpath_node_name_eq(xpath, "simulated-devices-list")) {
332                 sr_val_t *v;
333                 size_t current_num_of_values= 0;
334
335                 if (simulated_devices_config == 0) //nothing to return if no devices are running
336                 {
337                         *values = NULL;
338                         *values_cnt = 0;
339
340                         return SR_ERR_OK;
341                 }
342
343                 rc = get_docker_containers_operational_state_curl(device_list);
344                 if (rc != SR_ERR_OK)
345                 {
346                         printf("Could not get the operational state for the devices simulated.\n");
347                 }
348
349                 device_t *current_device = device_list->head;
350
351                 while (current_device != NULL)
352                 {
353                         CREATE_NEW_VALUE(rc, v, current_num_of_values);
354
355                         sr_val_build_xpath(&v[current_num_of_values - 1], "%s[uuid='%s']/%s", xpath, current_device->device_id, "device-ip");
356                         v[current_num_of_values - 1].type = SR_STRING_T;
357                         v[current_num_of_values - 1].data.string_val = getenv("NTS_IP");
358
359                         for (int i = 0; i < NETCONF_CONNECTIONS_PER_DEVICE; ++i)
360                         {
361                                 CREATE_NEW_VALUE(rc, v, current_num_of_values);
362
363                                 sr_val_build_xpath(&v[current_num_of_values - 1], "%s[uuid='%s']/%s", xpath, current_device->device_id, "device-port");
364                                 v[current_num_of_values - 1].type = SR_UINT32_T;
365                                 v[current_num_of_values - 1].data.uint32_val = current_device->netconf_port + i;
366                         }
367
368                         CREATE_NEW_VALUE(rc, v, current_num_of_values);
369
370                         sr_val_build_xpath(&v[current_num_of_values - 1], "%s[uuid='%s']/%s", xpath, current_device->device_id, "is-mounted");
371                         v[current_num_of_values - 1].type = SR_BOOL_T;
372                         v[current_num_of_values - 1].data.bool_val = current_device->is_mounted;
373
374                         char *operational_state = get_docker_container_operational_state(device_list, current_device->device_id);
375
376                         CREATE_NEW_VALUE(rc, v, current_num_of_values);
377
378                         sr_val_build_xpath(&v[current_num_of_values - 1], "%s[uuid='%s']/%s", xpath, current_device->device_id, "operational-state");
379                         sr_val_build_str_data(&v[current_num_of_values - 1], SR_ENUM_T, "%s", operational_state);
380
381                         current_device = current_device->next;
382                 }
383
384                 //return the values that we have just created
385                 *values = v;
386                 *values_cnt = current_num_of_values;
387          }
388          else if (sr_xpath_node_name_eq(xpath, "simulation-usage-details"))
389          {
390                 float cpu_usage = 0.0, mem_usage = 0.0;
391
392                 char *resource_usage_from_script = get_docker_container_resource_stats();
393
394                 if (resource_usage_from_script != NULL)
395                 {
396                         printf("Received line: %s\n", resource_usage_from_script);
397                         sscanf(resource_usage_from_script, "CPU=%f%%;RAM=%fMiB", &cpu_usage, &mem_usage);
398                         printf("Read cpu=\"%f\" and mem=\"%f\"\n", cpu_usage, mem_usage);
399                         free(resource_usage_from_script);
400                 }
401
402                 sr_val_t *v;
403                 /* convenient functions such as this can be found in sysrepo/values.h */
404                 size_t current_num_of_values= 0;
405
406                 CREATE_NEW_VALUE(rc, v, current_num_of_values);
407
408                 sr_val_build_xpath(&v[current_num_of_values - 1], "%s/%s", xpath, "running-simulated-devices");
409                 v[current_num_of_values - 1].type = SR_UINT32_T;
410                 v[current_num_of_values - 1].data.uint32_val = get_current_number_of_devices(device_list);
411
412                 CREATE_NEW_VALUE(rc, v, current_num_of_values);
413
414                 sr_val_build_xpath(&v[current_num_of_values - 1], "%s/%s", xpath, "running-mounted-devices");
415                 v[current_num_of_values - 1].type = SR_UINT32_T;
416                 v[current_num_of_values - 1].data.uint32_val = get_current_number_of_mounted_devices(device_list);
417
418                 CREATE_NEW_VALUE(rc, v, current_num_of_values);
419
420                 sr_val_build_xpath(&v[current_num_of_values - 1], "%s/%s", xpath, "base-netconf-port");
421                 v[current_num_of_values - 1].type = SR_UINT32_T;
422                 v[current_num_of_values - 1].data.uint32_val = get_netconf_port_base();
423
424                 CREATE_NEW_VALUE(rc, v, current_num_of_values);
425
426                 sr_val_build_xpath(&v[current_num_of_values - 1], "%s/%s", xpath, "cpu-usage");
427                 v[current_num_of_values - 1].type = SR_DECIMAL64_T;
428                 v[current_num_of_values - 1].data.decimal64_val = cpu_usage;
429
430                 CREATE_NEW_VALUE(rc, v, current_num_of_values);
431
432                 sr_val_build_xpath(&v[current_num_of_values - 1], "%s/%s", xpath, "mem-usage");
433                 v[current_num_of_values - 1].type = SR_UINT32_T;
434                 v[current_num_of_values - 1].data.uint32_val = (int)mem_usage;
435
436                 //return the values that we have just created
437                 *values = v;
438                 *values_cnt = current_num_of_values;
439          }
440
441     return SR_ERR_OK;
442 }
443
444 int odl_add_key_pair_cb(const char *xpath, const sr_val_t *input, const size_t input_cnt,
445                 sr_val_t **output, size_t *output_cnt, void *private_ctx)
446 {
447         int rc = SR_ERR_OK;
448     sr_session_ctx_t *session = (sr_session_ctx_t *)private_ctx;
449         controller_t controller_list[CONTROLLER_LIST_MAX_LEN];
450         int controller_list_size = 0;
451
452         controller_list[0] = controller_details;
453         controller_list_size++;
454
455         for (int i = 0; i < controller_list_size; ++i)
456         {
457                 printf("%d iteration: Got back url=%s and credentials=%s\n", i, controller_list[i].url, controller_list[i].credentials);
458         }
459
460         rc = add_key_pair_to_odl(controller_list, controller_list_size);
461         if (rc != SR_ERR_OK)
462         {
463                 printf("Failed to add key pair to ODL.\n");
464         }
465
466         return rc;
467 }
468
469
470 static void
471 sigint_handler(int signum)
472 {
473     exit_application = 1;
474 }
475
476 int
477 main(int argc, char **argv)
478 {
479     sr_conn_ctx_t *connection = NULL;
480     sr_session_ctx_t *session = NULL;
481     sr_subscription_ctx_t *subscription = NULL;
482     int rc = SR_ERR_OK;
483
484     setbuf(stdout, NULL);
485
486     device_list = new_device_stack();
487     rc = _init_curl();
488     if (rc != SR_ERR_OK)
489     {
490         fprintf(stderr, "Could not initialize cURL: %s\n", sr_strerror(rc));
491     }
492
493     /* connect to sysrepo */
494     rc = sr_connect("network-topology-simulator", SR_CONN_DEFAULT, &connection);
495     if (SR_ERR_OK != rc) {
496         fprintf(stderr, "Error by sr_connect: %s\n", sr_strerror(rc));
497         goto cleanup;
498     }
499
500     /* start session */
501     rc = sr_session_start(connection, SR_DS_STARTUP, SR_SESS_DEFAULT, &session);
502     if (SR_ERR_OK != rc) {
503         fprintf(stderr, "Error by sr_session_start: %s\n", sr_strerror(rc));
504         goto cleanup;
505     }
506
507         /* read startup config */
508         printf("\n\n ========== READING STARTUP CONFIG network-topology-simulator: ==========\n\n");
509         print_current_config(session, "network-topology-simulator");
510
511         /* subscribe for changes in running config */
512         rc = sr_module_change_subscribe(session, "network-topology-simulator", simulator_config_change_cb, NULL,
513                         0, SR_SUBSCR_DEFAULT | SR_SUBSCR_APPLY_ONLY, &subscription);
514         if (SR_ERR_OK != rc) {
515                 fprintf(stderr, "Error by sr_module_change_subscribe: %s\n", sr_strerror(rc));
516                 goto cleanup;
517         }
518
519     /* subscribe as state data provider for the ntsimulator state data */
520     rc = sr_dp_get_items_subscribe(session, "/network-topology-simulator:simulator-status", simulator_status_cb, NULL,
521                 SR_SUBSCR_CTX_REUSE, &subscription);
522     if (rc != SR_ERR_OK) {
523         goto cleanup;
524     }
525
526     rc = notification_delay_period_changed(0);
527     if (rc != SR_ERR_OK) {
528         printf("Could not write the delay period to file!\n");
529         goto cleanup;
530     }
531
532     rc = _init_curl_odl();
533     if (rc != SR_ERR_OK)
534     {
535         fprintf(stderr, "Could not initialize cURL for ODL connection: %s\n", sr_strerror(rc));
536     }
537
538     rc = sr_rpc_subscribe(session, "/network-topology-simulator:add-key-pair-to-odl", odl_add_key_pair_cb, (void *)session,
539                 SR_SUBSCR_CTX_REUSE, &subscription);
540
541         printf("\n\n ========== STARTUP CONFIG network-topology-simulator APPLIED AS RUNNING ==========\n\n");
542
543     /* loop until ctrl-c is pressed / SIGINT is received */
544     signal(SIGINT, sigint_handler);
545     signal(SIGTERM, sigint_handler);
546     signal(SIGPIPE, SIG_IGN);
547
548     while (!exit_application) {
549
550                 sleep(1);  /* or do some more useful work... */
551     }
552
553     printf("Application exit requested, exiting.\n");
554
555 cleanup:
556     if (NULL != subscription) {
557         sr_unsubscribe(session, subscription);
558     }
559     if (NULL != session) {
560         sr_session_stop(session);
561     }
562     if (NULL != connection) {
563         sr_disconnect(connection);
564     }
565
566     clean_current_docker_configuration();
567     rc = cleanup_curl();
568     rc = cleanup_curl_odl();
569
570     return rc;
571 }
572
573 static void clean_current_docker_configuration(void)
574 {
575         printf("Cleaning docker containers...\n");
576
577         if (device_list == NULL)
578         {
579                 return;
580         }
581
582         for (int i = 0; i < simulated_devices_config; ++i)
583         {
584                 stop_device(device_list);
585         }
586
587         printf("Cleaning completed!\n");
588 }