1 /*************************************************************************
3 * Copyright 2019 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 ***************************************************************************/
28 #include "sysrepo/values.h"
31 #include "simulator-operations.h"
33 volatile int exit_application = 0;
35 volatile unsigned int simulated_devices_config = 0;
36 volatile unsigned int mounted_devices_config = 0;
39 static device_stack_t *device_list = NULL;
41 controller_t controller_details;
43 #define XPATH_MAX_LEN 500
44 #define CONTROLLER_LIST_MAX_LEN 1
47 print_current_config(sr_session_ctx_t *session, const char *module_name)
49 sr_val_t *values = NULL;
52 char xpath[XPATH_MAX_LEN] = {0};
53 snprintf(xpath, XPATH_MAX_LEN, "/%s:*//.", module_name);
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;
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));
65 for (size_t i = 0; i < count; i++){
67 sr_print_val(&values[i]);
69 if (sr_xpath_node_name_eq(values[i].xpath, "controller-ip"))
71 rc = sr_dup_val(&values[i], &odl_ip);
73 else if (sr_xpath_node_name_eq(values[i].xpath, "controller-port"))
75 rc = sr_dup_val(&values[i], &odl_port);
77 else if (sr_xpath_node_name_eq(values[i].xpath, "controller-username"))
79 rc = sr_dup_val(&values[i], &odl_username);
81 else if (sr_xpath_node_name_eq(values[i].xpath, "controller-password"))
83 rc = sr_dup_val(&values[i], &odl_password);
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);
93 char credentials[URL_AND_CREDENTIALS_MAX_LEN];
94 sprintf(credentials, "%s:%s", odl_username->data.string_val, odl_password->data.string_val);
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);
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);
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);
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);
116 sr_free_val(odl_port);
117 sr_free_val(odl_username);
118 sr_free_val(odl_password);
120 sr_free_values(values, count);
123 static void clean_current_docker_configuration(void);
125 static int simulated_devices_changed(int new_value)
129 if (simulated_devices_config > new_value)
131 //we are configuring less elements that currently
132 for (int i = 0; i < simulated_devices_config - new_value; ++i)
134 rc = stop_device(device_list);
137 else if (simulated_devices_config < new_value)
139 //we are configuring more elements that currently
140 for (int i = 0; i < new_value - simulated_devices_config; ++i)
142 rc = start_device(device_list);
146 simulated_devices_config = new_value;
151 int mounted_devices_changed(sr_session_ctx_t *session, int new_value)
155 if (mounted_devices_config > new_value)
157 //we need have less mounted elements
158 for (int i = 0; i < mounted_devices_config - new_value; ++i)
160 printf("Sending unmount device...\n");
161 rc = unmount_device(device_list, controller_details);
164 else if (mounted_devices_config < new_value)
166 //we are configuring more elements that currently
167 for (int i = 0; i < new_value - mounted_devices_config; ++i)
169 printf("Sending mount device...\n");
170 rc = mount_device(device_list, controller_details);
174 mounted_devices_config = new_value;
180 simulator_config_change_cb(sr_session_ctx_t *session, const char *module_name, sr_notif_event_t event, void *private_ctx)
184 printf("\n\n ========== CONFIG HAS CHANGED, CURRENT RUNNING CONFIG %s: ==========\n\n", module_name);
185 print_current_config(session, module_name);
187 sr_val_t *val = NULL;
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) {
195 rc = simulated_devices_changed(val->data.uint32_val);
196 if (rc != SR_ERR_OK) {
203 /* get the value from sysrepo, we do not care if the value did not change in our case */
204 rc = sr_get_item(session, "/network-topology-simulator:simulator-config/mounted-devices", &val);
205 if (rc != SR_ERR_OK) {
209 if (mounted_devices_config != val->data.uint32_val)
211 if (val->data.uint32_val > simulated_devices_config)
213 printf("Cannot set mount value greater than number of simulated devices.\n");
219 rc = mounted_devices_changed(session, val->data.uint32_val);
220 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/fault-notification-delay-period", &val);
230 if (rc != SR_ERR_OK) {
234 rc = notification_delay_period_changed(val->data.uint32_val);
235 if (rc != SR_ERR_OK) {
242 /* get the value from sysrepo, we do not care if the value did not change in our case */
243 rc = sr_get_item(session, "/network-topology-simulator:simulator-config/notification-config/ves-heartbeat-period", &val);
244 if (rc != SR_ERR_OK) {
248 rc = ves_heartbeat_period_changed(val->data.uint32_val);
249 if (rc != SR_ERR_OK) {
256 /* get the value from sysrepo, we do not care if the value did not change in our case */
257 rc = sr_get_item(session, "/network-topology-simulator:simulator-config/ves-endpoint-details/ves-endpoint-ip", &val);
258 if (rc != SR_ERR_OK) {
262 rc = ves_ip_changed(val->data.string_val);
263 if (rc != SR_ERR_OK) {
270 /* get the value from sysrepo, we do not care if the value did not change in our case */
271 rc = sr_get_item(session, "/network-topology-simulator:simulator-config/ves-endpoint-details/ves-endpoint-port", &val);
272 if (rc != SR_ERR_OK) {
276 rc = ves_port_changed(val->data.uint16_val);
277 if (rc != SR_ERR_OK) {
284 /* get the value from sysrepo, we do not care if the value did not change in our case */
285 rc = sr_get_item(session, "/network-topology-simulator:simulator-config/ves-endpoint-details/ves-registration", &val);
286 if (rc != SR_ERR_OK) {
290 rc = ves_registration_changed(val->data.bool_val);
291 if (rc != SR_ERR_OK) {
298 /* get the value from sysrepo, we do not care if the value did not change in our case */
299 rc = sr_get_item(session, "/network-topology-simulator:simulator-config/notification-config/is-netconf-available", &val);
300 if (rc != SR_ERR_OK) {
304 rc = is_netconf_available_changed(val->data.bool_val);
305 if (rc != SR_ERR_OK) {
312 /* get the value from sysrepo, we do not care if the value did not change in our case */
313 rc = sr_get_item(session, "/network-topology-simulator:simulator-config/notification-config/is-ves-available", &val);
314 if (rc != SR_ERR_OK) {
318 rc = is_ves_available_changed(val->data.bool_val);
319 if (rc != SR_ERR_OK) {
329 printf("NTSimulator config change callback failed: %s.", sr_strerror(rc));
339 simulator_status_cb(const char *xpath, sr_val_t **values, size_t *values_cnt,
340 uint64_t request_id, const char *original_xpath, void *private_ctx)
344 printf("\n\n ========== Called simulator_status_cb for xpath: %s ==========\n\n", xpath);
346 if (sr_xpath_node_name_eq(xpath, "simulated-devices-list")) {
348 size_t current_num_of_values= 0;
350 if (simulated_devices_config == 0) //nothing to return if no devices are running
358 rc = get_docker_containers_operational_state_curl(device_list);
361 printf("Could not get the operational state for the devices simulated.\n");
362 return SR_ERR_OPERATION_FAILED;
365 device_t *current_device = device_list->head;
367 while (current_device != NULL)
369 CREATE_NEW_VALUE(rc, v, current_num_of_values);
371 sr_val_build_xpath(&v[current_num_of_values - 1], "%s[uuid='%s']/%s", xpath, current_device->device_id, "device-ip");
372 v[current_num_of_values - 1].type = SR_STRING_T;
373 v[current_num_of_values - 1].data.string_val = getenv("NTS_IP");
375 for (int i = 0; i < NETCONF_CONNECTIONS_PER_DEVICE; ++i)
377 CREATE_NEW_VALUE(rc, v, current_num_of_values);
379 sr_val_build_xpath(&v[current_num_of_values - 1], "%s[uuid='%s']/%s", xpath, current_device->device_id, "device-port");
380 v[current_num_of_values - 1].type = SR_UINT32_T;
381 v[current_num_of_values - 1].data.uint32_val = current_device->netconf_port + i;
384 CREATE_NEW_VALUE(rc, v, current_num_of_values);
386 sr_val_build_xpath(&v[current_num_of_values - 1], "%s[uuid='%s']/%s", xpath, current_device->device_id, "is-mounted");
387 v[current_num_of_values - 1].type = SR_BOOL_T;
388 v[current_num_of_values - 1].data.bool_val = current_device->is_mounted;
390 char *operational_state = get_docker_container_operational_state(device_list, current_device->device_id);
392 CREATE_NEW_VALUE(rc, v, current_num_of_values);
394 sr_val_build_xpath(&v[current_num_of_values - 1], "%s[uuid='%s']/%s", xpath, current_device->device_id, "operational-state");
395 sr_val_build_str_data(&v[current_num_of_values - 1], SR_ENUM_T, "%s", operational_state);
397 current_device = current_device->next;
400 //return the values that we have just created
402 *values_cnt = current_num_of_values;
404 else if (sr_xpath_node_name_eq(xpath, "simulation-usage-details"))
406 float cpu_usage = 0.0, mem_usage = 0.0;
408 char *resource_usage_from_script = get_docker_container_resource_stats();
410 if (resource_usage_from_script != NULL)
412 printf("Received line: %s\n", resource_usage_from_script);
413 sscanf(resource_usage_from_script, "CPU=%f%%;RAM=%fMiB", &cpu_usage, &mem_usage);
414 printf("Read cpu=\"%f\" and mem=\"%f\"\n", cpu_usage, mem_usage);
415 free(resource_usage_from_script);
419 /* convenient functions such as this can be found in sysrepo/values.h */
420 size_t current_num_of_values= 0;
422 CREATE_NEW_VALUE(rc, v, current_num_of_values);
424 sr_val_build_xpath(&v[current_num_of_values - 1], "%s/%s", xpath, "running-simulated-devices");
425 v[current_num_of_values - 1].type = SR_UINT32_T;
426 v[current_num_of_values - 1].data.uint32_val = get_current_number_of_devices(device_list);
428 CREATE_NEW_VALUE(rc, v, current_num_of_values);
430 sr_val_build_xpath(&v[current_num_of_values - 1], "%s/%s", xpath, "running-mounted-devices");
431 v[current_num_of_values - 1].type = SR_UINT32_T;
432 v[current_num_of_values - 1].data.uint32_val = get_current_number_of_mounted_devices(device_list);
434 CREATE_NEW_VALUE(rc, v, current_num_of_values);
436 sr_val_build_xpath(&v[current_num_of_values - 1], "%s/%s", xpath, "base-netconf-port");
437 v[current_num_of_values - 1].type = SR_UINT32_T;
438 v[current_num_of_values - 1].data.uint32_val = get_netconf_port_base();
440 CREATE_NEW_VALUE(rc, v, current_num_of_values);
442 sr_val_build_xpath(&v[current_num_of_values - 1], "%s/%s", xpath, "cpu-usage");
443 v[current_num_of_values - 1].type = SR_DECIMAL64_T;
444 v[current_num_of_values - 1].data.decimal64_val = cpu_usage;
446 CREATE_NEW_VALUE(rc, v, current_num_of_values);
448 sr_val_build_xpath(&v[current_num_of_values - 1], "%s/%s", xpath, "mem-usage");
449 v[current_num_of_values - 1].type = SR_UINT32_T;
450 v[current_num_of_values - 1].data.uint32_val = (int)mem_usage;
452 //return the values that we have just created
454 *values_cnt = current_num_of_values;
460 int odl_add_key_pair_cb(const char *xpath, const sr_val_t *input, const size_t input_cnt,
461 sr_val_t **output, size_t *output_cnt, void *private_ctx)
464 controller_t controller_list[CONTROLLER_LIST_MAX_LEN];
465 int controller_list_size = 0;
467 controller_list[0] = controller_details;
468 controller_list_size++;
470 for (int i = 0; i < controller_list_size; ++i)
472 printf("%d iteration: Got back url=%s and credentials=%s\n", i, controller_list[i].url, controller_list[i].credentials);
475 rc = add_key_pair_to_odl(controller_list, controller_list_size);
478 printf("Failed to add key pair to ODL.\n");
479 return SR_ERR_OPERATION_FAILED;
487 sigint_handler(int signum)
489 exit_application = 1;
493 main(int argc, char **argv)
495 sr_conn_ctx_t *connection = NULL;
496 sr_session_ctx_t *session = NULL;
497 sr_subscription_ctx_t *subscription = NULL;
500 setbuf(stdout, NULL);
502 device_list = new_device_stack();
506 fprintf(stderr, "Could not initialize cURL: %s\n", sr_strerror(rc));
509 /* connect to sysrepo */
510 rc = sr_connect("network-topology-simulator", SR_CONN_DEFAULT, &connection);
511 if (SR_ERR_OK != rc) {
512 fprintf(stderr, "Error by sr_connect: %s\n", sr_strerror(rc));
517 rc = sr_session_start(connection, SR_DS_STARTUP, SR_SESS_DEFAULT, &session);
518 if (SR_ERR_OK != rc) {
519 fprintf(stderr, "Error by sr_session_start: %s\n", sr_strerror(rc));
523 /* read startup config */
524 printf("\n\n ========== READING STARTUP CONFIG network-topology-simulator: ==========\n\n");
525 print_current_config(session, "network-topology-simulator");
527 /* subscribe for changes in running config */
528 rc = sr_module_change_subscribe(session, "network-topology-simulator", simulator_config_change_cb, NULL,
529 0, SR_SUBSCR_DEFAULT | SR_SUBSCR_APPLY_ONLY, &subscription);
530 if (SR_ERR_OK != rc) {
531 fprintf(stderr, "Error by sr_module_change_subscribe: %s\n", sr_strerror(rc));
535 /* subscribe as state data provider for the ntsimulator state data */
536 rc = sr_dp_get_items_subscribe(session, "/network-topology-simulator:simulator-status", simulator_status_cb, NULL,
537 SR_SUBSCR_CTX_REUSE, &subscription);
538 if (rc != SR_ERR_OK) {
542 rc = notification_delay_period_changed(0);
543 if (rc != SR_ERR_OK) {
544 printf("Could not write the delay period to file!\n");
548 rc = _init_curl_odl();
551 fprintf(stderr, "Could not initialize cURL for ODL connection: %s\n", sr_strerror(rc));
554 rc = sr_rpc_subscribe(session, "/network-topology-simulator:add-key-pair-to-odl", odl_add_key_pair_cb, (void *)session,
555 SR_SUBSCR_CTX_REUSE, &subscription);
557 printf("\n\n ========== STARTUP CONFIG network-topology-simulator APPLIED AS RUNNING ==========\n\n");
559 /* loop until ctrl-c is pressed / SIGINT is received */
560 signal(SIGINT, sigint_handler);
561 signal(SIGTERM, sigint_handler);
562 signal(SIGPIPE, SIG_IGN);
564 while (!exit_application) {
566 sleep(1); /* or do some more useful work... */
569 printf("Application exit requested, exiting.\n");
572 if (NULL != subscription) {
573 sr_unsubscribe(session, subscription);
575 if (NULL != session) {
576 sr_session_stop(session);
578 if (NULL != connection) {
579 sr_disconnect(connection);
582 clean_current_docker_configuration();
584 rc = cleanup_curl_odl();
589 static void clean_current_docker_configuration(void)
591 printf("Cleaning docker containers...\n");
593 if (device_list == NULL)
598 for (int i = 0; i < simulated_devices_config; ++i)
600 stop_device(device_list);
603 printf("Cleaning completed!\n");