4 * Created on: Feb 19, 2019
18 #include "sysrepo/values.h"
21 #include "simulator-operations.h"
23 volatile int exit_application = 0;
25 volatile unsigned int simulated_devices_config = 0;
26 volatile unsigned int mounted_devices_config = 0;
29 static device_stack_t *device_list = NULL;
31 controller_t controller_details;
33 #define XPATH_MAX_LEN 500
34 #define CONTROLLER_LIST_MAX_LEN 1
37 print_current_config(sr_session_ctx_t *session, const char *module_name)
39 sr_val_t *values = NULL;
42 char xpath[XPATH_MAX_LEN] = {0};
43 snprintf(xpath, XPATH_MAX_LEN, "/%s:*//.", module_name);
45 sr_val_t *odl_ip = NULL;
46 sr_val_t *odl_port = NULL;
47 sr_val_t *odl_username = NULL;
48 sr_val_t *odl_password = NULL;
50 rc = sr_get_items(session, xpath, &values, &count);
51 if (SR_ERR_OK != rc) {
52 printf("Error by sr_get_items: %s\n", sr_strerror(rc));
55 for (size_t i = 0; i < count; i++){
57 sr_print_val(&values[i]);
59 if (sr_xpath_node_name_eq(values[i].xpath, "controller-ip"))
61 rc = sr_dup_val(&values[i], &odl_ip);
63 else if (sr_xpath_node_name_eq(values[i].xpath, "controller-port"))
65 rc = sr_dup_val(&values[i], &odl_port);
67 else if (sr_xpath_node_name_eq(values[i].xpath, "controller-username"))
69 rc = sr_dup_val(&values[i], &odl_username);
71 else if (sr_xpath_node_name_eq(values[i].xpath, "controller-password"))
73 rc = sr_dup_val(&values[i], &odl_password);
77 //URL used for mounting/unmounting a device; the device name needs to be appended
78 char url[URL_AND_CREDENTIALS_MAX_LEN];
79 sprintf(url, "http://%s:%d/restconf/config/network-topology:network-topology/topology/"
80 "topology-netconf/node/",
81 odl_ip->data.string_val, odl_port->data.uint32_val);
83 char credentials[URL_AND_CREDENTIALS_MAX_LEN];
84 sprintf(credentials, "%s:%s", odl_username->data.string_val, odl_password->data.string_val);
86 //URLs used for adding key pair to ODL, for TLS connections
87 char url_for_keystore_add[URL_AND_CREDENTIALS_MAX_LEN];
88 sprintf(url_for_keystore_add, "http://%s:%d/restconf/operations/netconf-keystore:add-keystore-entry",
89 odl_ip->data.string_val, odl_port->data.uint32_val);
91 char url_for_private_key_add[URL_AND_CREDENTIALS_MAX_LEN];
92 sprintf(url_for_private_key_add, "http://%s:%d/restconf/operations/netconf-keystore:add-private-key",
93 odl_ip->data.string_val, odl_port->data.uint32_val);
95 char url_for_trusted_ca_add[URL_AND_CREDENTIALS_MAX_LEN];
96 sprintf(url_for_trusted_ca_add, "http://%s:%d/restconf/operations/netconf-keystore:add-trusted-certificate",
97 odl_ip->data.string_val, odl_port->data.uint32_val);
99 strcpy(controller_details.url, url);
100 strcpy(controller_details.credentials, credentials);
101 strcpy(controller_details.url_for_keystore_add, url_for_keystore_add);
102 strcpy(controller_details.url_for_private_key_add, url_for_private_key_add);
103 strcpy(controller_details.url_for_trusted_ca_add, url_for_trusted_ca_add);
106 sr_free_val(odl_port);
107 sr_free_val(odl_username);
108 sr_free_val(odl_password);
110 sr_free_values(values, count);
113 static void clean_current_docker_configuration(void);
115 static int simulated_devices_changed(int new_value)
119 if (simulated_devices_config > new_value)
121 //we are configuring less elements that currently
122 for (int i = 0; i < simulated_devices_config - new_value; ++i)
124 rc = stop_device(device_list);
127 else if (simulated_devices_config < new_value)
129 //we are configuring more elements that currently
130 for (int i = 0; i < new_value - simulated_devices_config; ++i)
132 rc = start_device(device_list);
136 simulated_devices_config = new_value;
141 int mounted_devices_changed(sr_session_ctx_t *session, int new_value)
145 if (mounted_devices_config > new_value)
147 //we need have less mounted elements
148 for (int i = 0; i < mounted_devices_config - new_value; ++i)
150 printf("Sending unmount device...\n");
151 rc = unmount_device(device_list, controller_details);
154 else if (mounted_devices_config < new_value)
156 //we are configuring more elements that currently
157 for (int i = 0; i < new_value - mounted_devices_config; ++i)
159 printf("Sending mount device...\n");
160 rc = mount_device(device_list, controller_details);
164 mounted_devices_config = new_value;
170 simulator_config_change_cb(sr_session_ctx_t *session, const char *module_name, sr_notif_event_t event, void *private_ctx)
174 printf("\n\n ========== CONFIG HAS CHANGED, CURRENT RUNNING CONFIG %s: ==========\n\n", module_name);
175 print_current_config(session, module_name);
179 /* get the value from sysrepo, we do not care if the value did not change in our case */
180 rc = sr_get_item(session, "/network-topology-simulator:simulator-config/simulated-devices", &val);
181 if (rc != SR_ERR_OK) {
185 rc = simulated_devices_changed(val->data.uint32_val);
186 if (rc != SR_ERR_OK) {
192 /* get the value from sysrepo, we do not care if the value did not change in our case */
193 rc = sr_get_item(session, "/network-topology-simulator:simulator-config/mounted-devices", &val);
194 if (rc != SR_ERR_OK) {
198 if (mounted_devices_config != val->data.uint32_val)
200 if (val->data.uint32_val > simulated_devices_config)
202 printf("Cannot set mount value greater than number of simulated devices.\n");
207 rc = mounted_devices_changed(session, val->data.uint32_val);
208 if (rc != SR_ERR_OK) {
215 /* get the value from sysrepo, we do not care if the value did not change in our case */
216 rc = sr_get_item(session, "/network-topology-simulator:simulator-config/notification-config/fault-notification-delay-period", &val);
217 if (rc != SR_ERR_OK) {
221 rc = notification_delay_period_changed(val->data.uint32_val);
222 if (rc != SR_ERR_OK) {
228 /* get the value from sysrepo, we do not care if the value did not change in our case */
229 rc = sr_get_item(session, "/network-topology-simulator:simulator-config/notification-config/ves-heartbeat-period", &val);
230 if (rc != SR_ERR_OK) {
234 rc = ves_heartbeat_period_changed(val->data.uint32_val);
235 if (rc != SR_ERR_OK) {
241 /* get the value from sysrepo, we do not care if the value did not change in our case */
242 rc = sr_get_item(session, "/network-topology-simulator:simulator-config/ves-endpoint-details/ves-endpoint-ip", &val);
243 if (rc != SR_ERR_OK) {
247 rc = ves_ip_changed(val->data.string_val);
248 if (rc != SR_ERR_OK) {
254 /* get the value from sysrepo, we do not care if the value did not change in our case */
255 rc = sr_get_item(session, "/network-topology-simulator:simulator-config/ves-endpoint-details/ves-endpoint-port", &val);
256 if (rc != SR_ERR_OK) {
260 rc = ves_port_changed(val->data.uint16_val);
261 if (rc != SR_ERR_OK) {
267 /* get the value from sysrepo, we do not care if the value did not change in our case */
268 rc = sr_get_item(session, "/network-topology-simulator:simulator-config/ves-endpoint-details/ves-registration", &val);
269 if (rc != SR_ERR_OK) {
273 rc = ves_registration_changed(val->data.bool_val);
274 if (rc != SR_ERR_OK) {
280 /* get the value from sysrepo, we do not care if the value did not change in our case */
281 rc = sr_get_item(session, "/network-topology-simulator:simulator-config/notification-config/is-netconf-available", &val);
282 if (rc != SR_ERR_OK) {
286 rc = is_netconf_available_changed(val->data.bool_val);
287 if (rc != SR_ERR_OK) {
293 /* get the value from sysrepo, we do not care if the value did not change in our case */
294 rc = sr_get_item(session, "/network-topology-simulator:simulator-config/notification-config/is-ves-available", &val);
295 if (rc != SR_ERR_OK) {
299 rc = is_ves_available_changed(val->data.bool_val);
300 if (rc != SR_ERR_OK) {
309 printf("NTSimulator config change callback failed: %s.", sr_strerror(rc));
314 simulator_status_cb(const char *xpath, sr_val_t **values, size_t *values_cnt,
315 uint64_t request_id, const char *original_xpath, void *private_ctx)
319 printf("\n\n ========== Called simulator_status_cb for xpath: %s ==========\n\n", xpath);
321 if (sr_xpath_node_name_eq(xpath, "simulated-devices-list")) {
323 size_t current_num_of_values= 0;
325 if (simulated_devices_config == 0) //nothing to return if no devices are running
333 rc = get_docker_containers_operational_state_curl(device_list);
336 printf("Could not get the operational state for the devices simulated.\n");
339 device_t *current_device = device_list->head;
341 while (current_device != NULL)
343 CREATE_NEW_VALUE(rc, v, current_num_of_values);
345 sr_val_build_xpath(&v[current_num_of_values - 1], "%s[uuid='%s']/%s", xpath, current_device->device_id, "device-ip");
346 v[current_num_of_values - 1].type = SR_STRING_T;
347 v[current_num_of_values - 1].data.string_val = getenv("NTS_IP");
349 for (int i = 0; i < NETCONF_CONNECTIONS_PER_DEVICE; ++i)
351 CREATE_NEW_VALUE(rc, v, current_num_of_values);
353 sr_val_build_xpath(&v[current_num_of_values - 1], "%s[uuid='%s']/%s", xpath, current_device->device_id, "device-port");
354 v[current_num_of_values - 1].type = SR_UINT32_T;
355 v[current_num_of_values - 1].data.uint32_val = current_device->netconf_port + i;
358 CREATE_NEW_VALUE(rc, v, current_num_of_values);
360 sr_val_build_xpath(&v[current_num_of_values - 1], "%s[uuid='%s']/%s", xpath, current_device->device_id, "is-mounted");
361 v[current_num_of_values - 1].type = SR_BOOL_T;
362 v[current_num_of_values - 1].data.bool_val = current_device->is_mounted;
364 char *operational_state = get_docker_container_operational_state(device_list, current_device->device_id);
366 CREATE_NEW_VALUE(rc, v, current_num_of_values);
368 sr_val_build_xpath(&v[current_num_of_values - 1], "%s[uuid='%s']/%s", xpath, current_device->device_id, "operational-state");
369 sr_val_build_str_data(&v[current_num_of_values - 1], SR_ENUM_T, "%s", operational_state);
371 current_device = current_device->next;
374 //return the values that we have just created
376 *values_cnt = current_num_of_values;
378 else if (sr_xpath_node_name_eq(xpath, "simulation-usage-details"))
380 float cpu_usage = 0.0, mem_usage = 0.0;
382 char *resource_usage_from_script = get_docker_container_resource_stats();
384 if (resource_usage_from_script != NULL)
386 printf("Received line: %s\n", resource_usage_from_script);
387 sscanf(resource_usage_from_script, "CPU=%f%%;RAM=%fMiB", &cpu_usage, &mem_usage);
388 printf("Read cpu=\"%f\" and mem=\"%f\"\n", cpu_usage, mem_usage);
389 free(resource_usage_from_script);
393 /* convenient functions such as this can be found in sysrepo/values.h */
394 size_t current_num_of_values= 0;
396 CREATE_NEW_VALUE(rc, v, current_num_of_values);
398 sr_val_build_xpath(&v[current_num_of_values - 1], "%s/%s", xpath, "running-simulated-devices");
399 v[current_num_of_values - 1].type = SR_UINT32_T;
400 v[current_num_of_values - 1].data.uint32_val = get_current_number_of_devices(device_list);
402 CREATE_NEW_VALUE(rc, v, current_num_of_values);
404 sr_val_build_xpath(&v[current_num_of_values - 1], "%s/%s", xpath, "running-mounted-devices");
405 v[current_num_of_values - 1].type = SR_UINT32_T;
406 v[current_num_of_values - 1].data.uint32_val = get_current_number_of_mounted_devices(device_list);
408 CREATE_NEW_VALUE(rc, v, current_num_of_values);
410 sr_val_build_xpath(&v[current_num_of_values - 1], "%s/%s", xpath, "base-netconf-port");
411 v[current_num_of_values - 1].type = SR_UINT32_T;
412 v[current_num_of_values - 1].data.uint32_val = get_netconf_port_base();
414 CREATE_NEW_VALUE(rc, v, current_num_of_values);
416 sr_val_build_xpath(&v[current_num_of_values - 1], "%s/%s", xpath, "cpu-usage");
417 v[current_num_of_values - 1].type = SR_DECIMAL64_T;
418 v[current_num_of_values - 1].data.decimal64_val = cpu_usage;
420 CREATE_NEW_VALUE(rc, v, current_num_of_values);
422 sr_val_build_xpath(&v[current_num_of_values - 1], "%s/%s", xpath, "mem-usage");
423 v[current_num_of_values - 1].type = SR_UINT32_T;
424 v[current_num_of_values - 1].data.uint32_val = (int)mem_usage;
426 //return the values that we have just created
428 *values_cnt = current_num_of_values;
434 int odl_add_key_pair_cb(const char *xpath, const sr_val_t *input, const size_t input_cnt,
435 sr_val_t **output, size_t *output_cnt, void *private_ctx)
438 sr_session_ctx_t *session = (sr_session_ctx_t *)private_ctx;
439 controller_t controller_list[CONTROLLER_LIST_MAX_LEN];
440 int controller_list_size = 0;
442 controller_list[0] = controller_details;
443 controller_list_size++;
445 for (int i = 0; i < controller_list_size; ++i)
447 printf("%d iteration: Got back url=%s and credentials=%s\n", i, controller_list[i].url, controller_list[i].credentials);
450 rc = add_key_pair_to_odl(controller_list, controller_list_size);
453 printf("Failed to add key pair to ODL.\n");
461 sigint_handler(int signum)
463 exit_application = 1;
467 main(int argc, char **argv)
469 sr_conn_ctx_t *connection = NULL;
470 sr_session_ctx_t *session = NULL;
471 sr_subscription_ctx_t *subscription = NULL;
474 setbuf(stdout, NULL);
476 device_list = new_device_stack();
480 fprintf(stderr, "Could not initialize cURL: %s\n", sr_strerror(rc));
483 /* connect to sysrepo */
484 rc = sr_connect("network-topology-simulator", SR_CONN_DEFAULT, &connection);
485 if (SR_ERR_OK != rc) {
486 fprintf(stderr, "Error by sr_connect: %s\n", sr_strerror(rc));
491 rc = sr_session_start(connection, SR_DS_STARTUP, SR_SESS_DEFAULT, &session);
492 if (SR_ERR_OK != rc) {
493 fprintf(stderr, "Error by sr_session_start: %s\n", sr_strerror(rc));
497 /* read startup config */
498 printf("\n\n ========== READING STARTUP CONFIG network-topology-simulator: ==========\n\n");
499 print_current_config(session, "network-topology-simulator");
501 /* subscribe for changes in running config */
502 rc = sr_module_change_subscribe(session, "network-topology-simulator", simulator_config_change_cb, NULL,
503 0, SR_SUBSCR_DEFAULT | SR_SUBSCR_APPLY_ONLY, &subscription);
504 if (SR_ERR_OK != rc) {
505 fprintf(stderr, "Error by sr_module_change_subscribe: %s\n", sr_strerror(rc));
509 /* subscribe as state data provider for the ntsimulator state data */
510 rc = sr_dp_get_items_subscribe(session, "/network-topology-simulator:simulator-status", simulator_status_cb, NULL,
511 SR_SUBSCR_CTX_REUSE, &subscription);
512 if (rc != SR_ERR_OK) {
516 rc = notification_delay_period_changed(0);
517 if (rc != SR_ERR_OK) {
518 printf("Could not write the delay period to file!\n");
522 rc = _init_curl_odl();
525 fprintf(stderr, "Could not initialize cURL for ODL connection: %s\n", sr_strerror(rc));
528 rc = sr_rpc_subscribe(session, "/network-topology-simulator:add-key-pair-to-odl", odl_add_key_pair_cb, (void *)session,
529 SR_SUBSCR_CTX_REUSE, &subscription);
531 printf("\n\n ========== STARTUP CONFIG network-topology-simulator APPLIED AS RUNNING ==========\n\n");
533 /* loop until ctrl-c is pressed / SIGINT is received */
534 signal(SIGINT, sigint_handler);
535 signal(SIGTERM, sigint_handler);
536 signal(SIGPIPE, SIG_IGN);
538 while (!exit_application) {
540 sleep(1); /* or do some more useful work... */
543 printf("Application exit requested, exiting.\n");
546 if (NULL != subscription) {
547 sr_unsubscribe(session, subscription);
549 if (NULL != session) {
550 sr_session_stop(session);
552 if (NULL != connection) {
553 sr_disconnect(connection);
556 clean_current_docker_configuration();
558 rc = cleanup_curl_odl();
563 static void clean_current_docker_configuration(void)
565 printf("Cleaning docker containers...\n");
567 if (device_list == NULL)
572 for (int i = 0; i < simulated_devices_config; ++i)
574 stop_device(device_list);
577 printf("Cleaning completed!\n");