2 * simulator-operations.c
4 * Created on: Mar 9, 2019
8 #include "simulator-operations.h"
10 #include "sysrepo/values.h"
13 #include <linux/limits.h>
17 #define LINE_BUFSIZE 128
19 static CURL *curl; //share the same curl connection for communicating with the Docker Engine API
20 static CURL *curl_odl; //share the same curl connection for mounting servers in ODL
22 static size_t WriteMemoryCallback(void *contents, size_t size, size_t nmemb, void *userp)
24 size_t realsize = size * nmemb;
25 struct MemoryStruct *mem = (struct MemoryStruct *)userp;
27 char *ptr = realloc(mem->memory, mem->size + realsize + 1);
30 printf("not enough memory (realloc returned NULL)\n");
35 memcpy(&(mem->memory[mem->size]), contents, realsize);
36 mem->size += realsize;
37 mem->memory[mem->size] = 0;
42 static void set_curl_common_info()
44 struct curl_slist *chunk = NULL;
45 chunk = curl_slist_append(chunk, "Content-Type: application/json");
46 chunk = curl_slist_append(chunk, "Accept: application/json");
48 curl_easy_setopt(curl, CURLOPT_HTTPHEADER, chunk);
50 curl_easy_setopt(curl, CURLOPT_UNIX_SOCKET_PATH, "/var/run/docker.sock");
52 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteMemoryCallback);
53 curl_easy_setopt(curl_odl, CURLOPT_CONNECTTIMEOUT, 2L); // seconds timeout for a connection
54 curl_easy_setopt(curl_odl, CURLOPT_TIMEOUT, 5L); //seconds timeout for an operation
56 curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
59 static void set_curl_common_info_odl()
61 struct curl_slist *chunk = NULL;
62 chunk = curl_slist_append(chunk, "Content-Type: application/xml");
63 chunk = curl_slist_append(chunk, "Accept: application/xml");
65 curl_easy_setopt(curl_odl, CURLOPT_HTTPHEADER, chunk);
67 curl_easy_setopt(curl_odl, CURLOPT_CONNECTTIMEOUT, 2L); // seconds timeout for a connection
68 curl_easy_setopt(curl_odl, CURLOPT_TIMEOUT, 5L); //seconds timeout for an operation
70 curl_easy_setopt(curl_odl, CURLOPT_VERBOSE, 1L);
73 static cJSON* get_docker_container_bindings(void)
75 struct MemoryStruct curl_response_mem;
77 curl_response_mem.memory = malloc(1); /* will be grown as needed by the realloc above */
78 curl_response_mem.size = 0; /* no data at this point */
82 curl_easy_reset(curl);
83 set_curl_common_info();
86 sprintf(url, "http:/v%s/containers/NTS_Manager/json", getenv("DOCKER_ENGINE_VERSION"));
88 curl_easy_setopt(curl, CURLOPT_URL, url);
90 curl_easy_setopt(curl, CURLOPT_POSTFIELDS, "");
91 curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "GET");
93 curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&curl_response_mem);
95 res = curl_easy_perform(curl);
103 cJSON *json_response = cJSON_Parse(curl_response_mem.memory);
105 printf("%lu bytes retrieved\n", (unsigned long)curl_response_mem.size);
107 if (json_response == NULL)
109 printf("Could not parse JSON response for url=\"%s\"\n", url);
113 cJSON *hostConfig = cJSON_GetObjectItemCaseSensitive(json_response, "HostConfig");
115 if (hostConfig == NULL)
117 printf("Could not get HostConfig object\n");
121 cJSON *binds = cJSON_GetObjectItemCaseSensitive(hostConfig, "Binds");
125 printf("Could not get Binds object\n");
129 cJSON *bindsCopy = cJSON_Duplicate(binds, 1);
131 cJSON_Delete(json_response);
139 static char* create_docker_container_curl(int base_netconf_port, cJSON* managerBinds)
141 if (managerBinds == NULL)
143 printf("Could not retrieve JSON object: Binds\n");
146 cJSON *binds = cJSON_Duplicate(managerBinds, 1);
148 struct MemoryStruct curl_response_mem;
150 curl_response_mem.memory = malloc(1); /* will be grown as needed by the realloc above */
151 curl_response_mem.size = 0; /* no data at this point */
155 curl_easy_reset(curl);
156 set_curl_common_info();
159 sprintf(url, "http:/v%s/containers/create", getenv("DOCKER_ENGINE_VERSION"));
161 // the docker image name to be used is defined in the Dockerfile of the NTS Manager,
162 // under the MODELS_IMAGE env variable
164 sprintf(models_var, "%s", getenv("MODELS_IMAGE"));
166 curl_easy_setopt(curl, CURLOPT_URL, url);
168 cJSON *postDataJson = cJSON_CreateObject();
170 if (cJSON_AddStringToObject(postDataJson, "Image", models_var) == NULL)
172 printf("Could not create JSON object: Image\n");
176 cJSON *hostConfig = cJSON_CreateObject();
177 if (hostConfig == NULL)
179 printf("Could not create JSON object: HostConfig\n");
183 cJSON_AddItemToObject(postDataJson, "HostConfig", hostConfig);
185 cJSON *portBindings = cJSON_CreateObject();
186 if (portBindings == NULL)
188 printf("Could not create JSON object: PortBindings\n");
192 cJSON_AddItemToObject(hostConfig, "PortBindings", portBindings);
194 for (int i = 0; i < NETCONF_CONNECTIONS_PER_DEVICE; ++i)
196 cJSON *port = cJSON_CreateArray();
199 printf("Could not create JSON object: port\n");
203 char dockerContainerPort[20];
204 sprintf(dockerContainerPort, "%d/tcp", 830 + i);
206 cJSON_AddItemToObject(portBindings, dockerContainerPort, port);
208 cJSON *hostPort = cJSON_CreateObject();
209 if (hostPort == NULL)
211 printf("Could not create JSON object: HostPort\n");
215 char dockerHostPort[10];
216 sprintf(dockerHostPort, "%d", base_netconf_port + i);
217 if (cJSON_AddStringToObject(hostPort, "HostPort", dockerHostPort) == NULL)
219 printf("Could not create JSON object: HostPortString\n");
222 if (cJSON_AddStringToObject(hostPort, "HostIp", getenv("NTS_IP")) == NULL)
224 printf("Could not create JSON object: HostIpString\n");
228 cJSON_AddItemToArray(port, hostPort);
231 cJSON *labels = cJSON_CreateObject();
234 printf("Could not create JSON object: Labels\n");
238 cJSON_AddItemToObject(postDataJson, "Labels", labels);
240 if (cJSON_AddStringToObject(labels, "NTS", "") == NULL)
242 printf("Could not create JSON object: NTS\n");
246 cJSON *env_variables_array = cJSON_CreateArray();
247 if (env_variables_array == NULL)
249 printf("Could not create JSON object: Env array\n");
253 cJSON_AddItemToObject(postDataJson, "Env", env_variables_array);
255 char environment_var[50];
256 sprintf(environment_var, "NTS_IP=%s", getenv("NTS_IP"));
258 cJSON *env_var_obj = cJSON_CreateString(environment_var);
259 if (env_var_obj == NULL)
261 printf("Could not create JSON object: Env array object NTS_IP\n");
264 cJSON_AddItemToArray(env_variables_array, env_var_obj);
266 sprintf(environment_var, "NETCONF_BASE=%d", base_netconf_port);
267 cJSON *env_var_obj_2 = cJSON_CreateString(environment_var);
268 if (env_var_obj_2 == NULL)
270 printf("Could not create JSON object: Env array object NETCONF_BASE\n");
273 cJSON_AddItemToArray(env_variables_array, env_var_obj_2);
275 char scripts_dir[200];
276 sprintf(scripts_dir, "SCRIPTS_DIR=%s", getenv("SCRIPTS_DIR"));
277 cJSON *env_var_obj_3 = cJSON_CreateString(scripts_dir);
278 if (env_var_obj_3 == NULL)
280 printf("Could not create JSON object: Env array object SCRIPTS_DIR\n");
283 cJSON_AddItemToArray(env_variables_array, env_var_obj_3);
285 cJSON_AddItemToObject(hostConfig, "Binds", binds);
287 char *post_data_string = NULL;
289 post_data_string = cJSON_PrintUnformatted(postDataJson);
291 printf("Post data JSON:\n%s\n", post_data_string);
293 if (postDataJson != NULL)
295 cJSON_Delete(postDataJson);
298 curl_easy_setopt(curl, CURLOPT_POSTFIELDS, post_data_string);
300 curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&curl_response_mem);
302 res = curl_easy_perform(curl);
310 cJSON *json_response = cJSON_Parse(curl_response_mem.memory);
311 const cJSON *container_id = NULL;
313 printf("%lu bytes retrieved\n", (unsigned long)curl_response_mem.size);
315 container_id = cJSON_GetObjectItemCaseSensitive(json_response, "Id");
317 if (cJSON_IsString(container_id) && (container_id->valuestring != NULL))
319 printf("Container id: \"%s\"\n", container_id->valuestring);
321 char container_id_short[13];
323 memset(container_id_short, '\0', sizeof(container_id_short));
324 strncpy(container_id_short, container_id->valuestring, 12);
326 printf("Container id short: \"%s\"\n", container_id_short);
328 cJSON_Delete(json_response);
329 return strdup(container_id_short);
332 cJSON_Delete(json_response);
338 static int start_docker_container_curl(char *container_id)
340 struct MemoryStruct curl_response_mem;
342 curl_response_mem.memory = malloc(1); /* will be grown as needed by the realloc above */
343 curl_response_mem.size = 0; /* no data at this point */
347 curl_easy_reset(curl);
348 set_curl_common_info();
351 sprintf(url, "http:/v%s/containers/%s/start", getenv("DOCKER_ENGINE_VERSION"), container_id);
353 curl_easy_setopt(curl, CURLOPT_URL, url);
355 curl_easy_setopt(curl, CURLOPT_POSTFIELDS, "");
357 curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&curl_response_mem);
359 res = curl_easy_perform(curl);
363 return SR_ERR_OPERATION_FAILED;
367 printf("Container %s started successfully!\n", container_id);
373 static int kill_and_remove_docker_container_curl(char *container_id)
375 struct MemoryStruct curl_response_mem;
377 curl_response_mem.memory = malloc(1); /* will be grown as needed by the realloc above */
378 curl_response_mem.size = 0; /* no data at this point */
382 curl_easy_reset(curl);
383 set_curl_common_info();
386 sprintf(url, "http:/v%s/containers/%s?force=true", getenv("DOCKER_ENGINE_VERSION"), container_id);
388 curl_easy_setopt(curl, CURLOPT_URL, url);
390 curl_easy_setopt(curl, CURLOPT_POSTFIELDS, "");
391 curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "DELETE");
393 curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&curl_response_mem);
395 res = curl_easy_perform(curl);
399 return SR_ERR_OPERATION_FAILED;
403 printf("Container %s removed successfully!\n", container_id);
409 static int send_mount_device_instance_ssh(char *url, char *credentials, char *device_name, int device_port)
413 curl_easy_reset(curl_odl);
414 set_curl_common_info_odl();
416 char url_for_curl[200];
417 sprintf(url_for_curl, "%s%s_%d", url, device_name, device_port);
419 curl_easy_setopt(curl_odl, CURLOPT_URL, url_for_curl);
421 char post_data_xml[1000];
423 sprintf(post_data_xml,
424 "<node xmlns=\"urn:TBD:params:xml:ns:yang:network-topology\">"
425 "<node-id>%s_%d</node-id>"
426 "<host xmlns=\"urn:opendaylight:netconf-node-topology\">%s</host>"
427 "<port xmlns=\"urn:opendaylight:netconf-node-topology\">%d</port>"
428 "<username xmlns=\"urn:opendaylight:netconf-node-topology\">%s</username>"
429 "<password xmlns=\"urn:opendaylight:netconf-node-topology\">%s</password>"
430 "<tcp-only xmlns=\"urn:opendaylight:netconf-node-topology\">false</tcp-only>"
431 "<keepalive-delay xmlns=\"urn:opendaylight:netconf-node-topology\">120</keepalive-delay>"
433 device_name, device_port, getenv("NTS_IP"), device_port, "netconf", "netconf");
435 printf("Post data:\n%s\n", post_data_xml);
437 curl_easy_setopt(curl_odl, CURLOPT_POSTFIELDS, post_data_xml);
438 curl_easy_setopt(curl_odl, CURLOPT_CUSTOMREQUEST, "PUT");
439 curl_easy_setopt(curl_odl, CURLOPT_USERPWD, credentials);
441 res = curl_easy_perform(curl_odl);
444 printf("cURL failed to url=%s\n", url_for_curl);
447 long http_response_code = 0;
448 curl_easy_getinfo (curl_odl, CURLINFO_RESPONSE_CODE, &http_response_code);
449 if (http_response_code >= 200 && http_response_code <= 226 && http_response_code != CURLE_ABORTED_BY_CALLBACK)
451 printf("cURL succeeded to url=%s\n", url_for_curl);
455 printf("cURL to url=%s failed with code=%ld\n", url_for_curl, http_response_code);
456 return SR_ERR_OPERATION_FAILED;
462 static int send_mount_device_instance_tls(char *url, char *credentials, char *device_name, int device_port)
466 curl_easy_reset(curl_odl);
467 set_curl_common_info_odl();
469 char url_for_curl[200];
470 sprintf(url_for_curl, "%s%s_%d", url, device_name, device_port);
472 curl_easy_setopt(curl_odl, CURLOPT_URL, url_for_curl);
474 char post_data_xml[1000];
476 sprintf(post_data_xml,
477 "<node xmlns=\"urn:TBD:params:xml:ns:yang:network-topology\">"
478 "<protocol xmlns=\"urn:opendaylight:netconf-node-topology\">"
481 "<node-id>%s_%d</node-id>"
482 "<host xmlns=\"urn:opendaylight:netconf-node-topology\">%s</host>"
483 "<key-based xmlns=\"urn:opendaylight:netconf-node-topology\">"
484 "<username>%s</username>"
485 "<key-id>device-key</key-id>"
487 "<port xmlns=\"urn:opendaylight:netconf-node-topology\">%d</port>"
488 "<tcp-only xmlns=\"urn:opendaylight:netconf-node-topology\">false</tcp-only>"
489 "<keepalive-delay xmlns=\"urn:opendaylight:netconf-node-topology\">120</keepalive-delay>"
491 device_name, device_port, getenv("NTS_IP"), "netconf", device_port);
493 printf("Post data:\n%s\n", post_data_xml);
495 curl_easy_setopt(curl_odl, CURLOPT_POSTFIELDS, post_data_xml);
496 curl_easy_setopt(curl_odl, CURLOPT_CUSTOMREQUEST, "PUT");
497 curl_easy_setopt(curl_odl, CURLOPT_USERPWD, credentials);
499 res = curl_easy_perform(curl_odl);
502 printf("cURL failed to url=%s\n", url_for_curl);
505 long http_response_code = 0;
506 curl_easy_getinfo (curl_odl, CURLINFO_RESPONSE_CODE, &http_response_code);
507 if (http_response_code >= 200 && http_response_code <= 226 && http_response_code != CURLE_ABORTED_BY_CALLBACK)
509 printf("cURL succeeded to url=%s\n", url_for_curl);
513 printf("cURL to url=%s failed with code=%ld\n", url_for_curl, http_response_code);
514 return SR_ERR_OPERATION_FAILED;
520 static int send_unmount_device_instance(char *url, char *credentials, char *device_name, int device_port)
524 curl_easy_reset(curl_odl);
525 set_curl_common_info_odl();
527 char url_for_curl[200];
528 sprintf(url_for_curl, "%s%s_%d", url, device_name, device_port);
530 curl_easy_setopt(curl_odl, CURLOPT_URL, url_for_curl);
532 curl_easy_setopt(curl_odl, CURLOPT_POSTFIELDS, "");
533 curl_easy_setopt(curl_odl, CURLOPT_CUSTOMREQUEST, "DELETE");
534 curl_easy_setopt(curl_odl, CURLOPT_USERPWD, credentials);
536 res = curl_easy_perform(curl_odl);
539 printf("cURL failed to url=%s\n", url_for_curl);
542 long http_response_code = 0;
543 curl_easy_getinfo (curl_odl, CURLINFO_RESPONSE_CODE, &http_response_code);
544 if (http_response_code == 200 && http_response_code != CURLE_ABORTED_BY_CALLBACK)
546 printf("cURL succeeded to url=%s\n", url_for_curl);
550 printf("cURL to url=%s failed with code=%ld\n", url_for_curl, http_response_code);
551 return SR_ERR_OPERATION_FAILED;
559 static int send_mount_device(device_t *current_device, controller_t controller_details)
562 bool is_mounted = true;
564 //This is where we hardcoded: 7 devices will have SSH connections and 3 devices will have TLS connections
565 for (int port = 0; port < NETCONF_CONNECTIONS_PER_DEVICE - 3; ++port)
567 rc = send_mount_device_instance_ssh(controller_details.url, controller_details.credentials,
568 current_device->device_id, current_device->netconf_port + port);
574 for (int port = NETCONF_CONNECTIONS_PER_DEVICE - 3; port < NETCONF_CONNECTIONS_PER_DEVICE; ++port)
576 rc = send_mount_device_instance_tls(controller_details.url, controller_details.credentials,
577 current_device->device_id, current_device->netconf_port + port);
584 current_device->is_mounted = is_mounted;
589 static int send_unmount_device(device_t *current_device, controller_t controller_details)
593 for (int port = 0; port < NETCONF_CONNECTIONS_PER_DEVICE; ++port)
595 rc = send_unmount_device_instance(controller_details.url, controller_details.credentials,
596 current_device->device_id, current_device->netconf_port + port);
599 printf("Could not send unmount for ODL with url=\"%s\", for device=\"%s\" and port=%d\n",
600 controller_details.url, current_device->device_id, current_device->netconf_port);
603 current_device->is_mounted = false;
608 device_stack_t *new_device_stack(void)
610 device_stack_t *stack = malloc(sizeof(*stack));
614 stack->stack_size = 0;
619 void push_device(device_stack_t *theStack, char *dev_id, int port)
621 device_t *new_dev = malloc(sizeof(*new_dev));
624 new_dev->device_id = strdup(dev_id);
625 new_dev->netconf_port = port;
626 new_dev->is_mounted = false;
627 new_dev->operational_state = strdup("not-specified");
629 new_dev->next = theStack->head;
631 theStack->head = new_dev;
632 theStack->stack_size++;
636 void pop_device(device_stack_t *theStack)
638 if (theStack && theStack->head) {
639 device_t *temp = theStack->head;
640 theStack->head = theStack->head->next;
642 free(temp->device_id);
643 free(temp->operational_state);
645 theStack->stack_size--;
649 int get_netconf_port_next(device_stack_t *theStack)
651 if (theStack && theStack->stack_size > 0) {
652 return theStack->head->netconf_port + NETCONF_CONNECTIONS_PER_DEVICE;
655 return get_netconf_port_base();
658 int get_netconf_port_base()
660 int netconf_port_base = 0, rc;
662 char *netconf_base_string = getenv("NETCONF_BASE");
664 if (netconf_base_string != NULL)
666 rc = sscanf(netconf_base_string, "%d", &netconf_port_base);
669 printf("Could not get the NETCONF_BASE port! Using the default 30.000...\n");
670 netconf_port_base = 30000;
674 return netconf_port_base;
678 char *get_id_last_device(device_stack_t *theStack)
680 if (theStack && theStack->head) {
681 return theStack->head->device_id;
686 int get_current_number_of_mounted_devices(device_stack_t *theStack)
688 int mounted_devices = 0;
690 if (theStack && theStack->head)
692 device_t *current_device = theStack->head;
694 while (current_device != NULL)
696 if (current_device->is_mounted)
700 current_device = current_device->next;
704 return mounted_devices;
707 int get_current_number_of_devices(device_stack_t *theStack)
709 struct MemoryStruct curl_response_mem;
711 curl_response_mem.memory = malloc(1); /* will be grown as needed by the realloc above */
712 curl_response_mem.size = 0; /* no data at this point */
716 curl_easy_reset(curl);
717 set_curl_common_info();
720 sprintf(url, "http:/v%s/containers/json?all=true&filters={\"label\":[\"NTS\"],\"status\":[\"running\"]}",
721 getenv("DOCKER_ENGINE_VERSION"));
723 curl_easy_setopt(curl, CURLOPT_URL, url);
725 curl_easy_setopt(curl, CURLOPT_POSTFIELDS, "");
726 curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "GET");
728 curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&curl_response_mem);
730 res = curl_easy_perform(curl);
734 return SR_ERR_OPERATION_FAILED;
738 cJSON *json_response = cJSON_Parse(curl_response_mem.memory);
740 printf("%lu bytes retrieved\n", (unsigned long)curl_response_mem.size);
742 if (json_response == NULL || !cJSON_IsArray(json_response))
744 printf("Could not parse JSON response for url=\"%s\"\n", url);
745 return SR_ERR_OPERATION_FAILED;
748 int num_of_devices = cJSON_GetArraySize(json_response);
749 cJSON_Delete(json_response);
751 return num_of_devices;
757 static int set_operational_state_of_device(device_stack_t *theStack, char *device_id, char *operational_state)
759 if (theStack && theStack->head)
761 device_t *current_device = theStack->head;
763 while (current_device != NULL)
765 if (strcmp(current_device->device_id, device_id) == 0)
767 free(current_device->operational_state);
768 current_device->operational_state = strdup(operational_state);
773 current_device = current_device->next;
777 printf("Could not find device with uuid=\"%s\"\n", device_id);
778 return SR_ERR_OPERATION_FAILED;
781 char* get_docker_container_operational_state(device_stack_t *theStack, char *container_id)
783 if (theStack && theStack->head)
785 device_t *current_device = theStack->head;
787 while (current_device != NULL)
789 if (strcmp(current_device->device_id, container_id) == 0)
791 return current_device->operational_state;
794 current_device = current_device->next;
801 int start_device(device_stack_t *theStack)
804 static cJSON* managerBindings = NULL;
806 if (managerBindings == NULL)
808 managerBindings = get_docker_container_bindings();
811 int netconf_base = get_netconf_port_next(theStack);
813 char *dev_id = create_docker_container_curl(netconf_base, managerBindings);
815 push_device(theStack, dev_id, netconf_base);
817 rc = start_docker_container_curl(dev_id);
820 printf("Could not start device with device_id=\"%s\"\n", dev_id);
832 curl = curl_easy_init();
835 printf("cURL initialization error! Aborting call!\n");
836 return SR_ERR_OPERATION_FAILED;
846 curl_easy_cleanup(curl);
854 curl_odl = curl_easy_init();
856 if (curl_odl == NULL) {
857 printf("cURL initialization error! Aborting call!\n");
858 return SR_ERR_OPERATION_FAILED;
864 int cleanup_curl_odl()
866 if (curl_odl != NULL)
868 curl_easy_cleanup(curl_odl);
874 int stop_device(device_stack_t *theStack)
877 char *last_id = get_id_last_device(theStack);
879 rc = kill_and_remove_docker_container_curl(last_id);
882 printf("Could not kill and remove docker container with uuid=\"%s\"\n", last_id);
885 pop_device(theStack);
890 int mount_device(device_stack_t *theStack, controller_t controller_details)
894 if (theStack && theStack->head)
896 device_t *current_device = theStack->head;
897 while (current_device != NULL && current_device->is_mounted == true)
899 printf("Device \"%s\" is already mounted, skipping...\n", current_device->device_id);
900 current_device = current_device->next;
903 if (current_device != NULL)
905 printf("Sending mount device for device \"%s\"...\n", current_device->device_id);
906 rc = send_mount_device(current_device, controller_details);
909 return SR_ERR_OPERATION_FAILED;
917 int unmount_device(device_stack_t *theStack, controller_t controller_list)
921 if (theStack && theStack->head)
923 device_t *current_device = theStack->head;
924 while (current_device != NULL && current_device->is_mounted == false)
926 printf("Device \"%s\" is already unmounted, skipping...\n", current_device->device_id);
927 current_device = current_device->next;
930 if (current_device != NULL)
932 printf("Sending unmount device for device \"%s\"...\n", current_device->device_id);
933 rc = send_unmount_device(current_device, controller_list);
936 return SR_ERR_OPERATION_FAILED;
944 int get_docker_containers_operational_state_curl(device_stack_t *theStack)
947 struct MemoryStruct curl_response_mem;
949 curl_response_mem.memory = malloc(1); /* will be grown as needed by the realloc above */
950 curl_response_mem.size = 0; /* no data at this point */
954 curl_easy_reset(curl);
955 set_curl_common_info();
958 sprintf(url, "http:/v%s/containers/json?all=true&filters={\"label\":[\"NTS\"]}", getenv("DOCKER_ENGINE_VERSION"));
960 curl_easy_setopt(curl, CURLOPT_URL, url);
962 curl_easy_setopt(curl, CURLOPT_POSTFIELDS, "");
963 curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "GET");
965 curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&curl_response_mem);
967 res = curl_easy_perform(curl);
971 return SR_ERR_OPERATION_FAILED;
975 cJSON *json_response = cJSON_Parse(curl_response_mem.memory);
976 const cJSON *container = NULL;
978 printf("%lu bytes retrieved\n", (unsigned long)curl_response_mem.size);
980 if (json_response == NULL || !cJSON_IsArray(json_response))
982 printf("Could not parse JSON response for url=\"%s\"\n", url);
983 return SR_ERR_OPERATION_FAILED;
986 cJSON_ArrayForEach(container, json_response)
988 cJSON *container_id_long = cJSON_GetObjectItemCaseSensitive(container, "Id");
989 cJSON *state = cJSON_GetObjectItemCaseSensitive(container, "State");
991 if (cJSON_IsString(container_id_long) && (container_id_long->valuestring != NULL))
993 char container_id_short[13];
995 memset(container_id_short, '\0', sizeof(container_id_short));
996 strncpy(container_id_short, container_id_long->valuestring, 12);
998 if (cJSON_IsString(state) && (state->valuestring != NULL))
1000 rc = set_operational_state_of_device(theStack, container_id_short, state->valuestring);
1001 if (rc != SR_ERR_OK)
1003 printf("Could not set the operational state for the device with uuid=\"%s\"\n", container_id_short);
1009 cJSON_Delete(json_response);
1015 char* get_docker_container_resource_stats(device_stack_t *theStack)
1017 char line[LINE_BUFSIZE];
1021 /* Get a pipe where the output from the scripts comes in */
1023 sprintf(script, "%s/docker_stats.sh", getenv("SCRIPTS_DIR"));
1025 pipe = popen(script, "r");
1026 if (pipe == NULL) { /* check for errors */
1027 printf("Could not open script.\n");
1028 return NULL; /* return with exit code indicating error */
1031 /* Read script output from the pipe line by line */
1033 while (fgets(line, LINE_BUFSIZE, pipe) != NULL) {
1034 printf("Script output line %d: %s", linenr, line);
1037 pclose(pipe); /* Close the pipe */
1038 return strdup(line);
1041 /* Once here, out of the loop, the script has ended. */
1042 pclose(pipe); /* Close the pipe */
1043 return NULL; /* return with exit code indicating success. */
1046 int notification_delay_period_changed(int period)
1048 char *stringConfiguration = readConfigFileInString();
1050 if (stringConfiguration == NULL)
1052 printf("Could not read configuration file!\n");
1053 return SR_ERR_OPERATION_FAILED;
1056 cJSON *jsonConfig = cJSON_Parse(stringConfiguration);
1057 if (jsonConfig == NULL)
1059 free(stringConfiguration);
1060 const char *error_ptr = cJSON_GetErrorPtr();
1061 if (error_ptr != NULL)
1063 fprintf(stderr, "Could not parse JSON configuration! Error before: %s\n", error_ptr);
1065 return SR_ERR_OPERATION_FAILED;
1067 //we don't need the string anymore
1068 free(stringConfiguration);
1069 stringConfiguration = NULL;
1071 cJSON *notifConfig = cJSON_GetObjectItemCaseSensitive(jsonConfig, "notification-config");
1072 if (!cJSON_IsObject(notifConfig))
1074 printf("Configuration JSON is not as expected: notification-config is not an object");
1076 return SR_ERR_OPERATION_FAILED;
1079 cJSON *faultNotifDelay = cJSON_GetObjectItemCaseSensitive(notifConfig, "fault-notification-delay-period");
1080 if (!cJSON_IsNumber(faultNotifDelay))
1082 printf("Configuration JSON is not as expected: fault-notification-delay-period is not an object");
1084 return SR_ERR_OPERATION_FAILED;
1087 //we set the value of the fault-notification-delay-period object
1088 cJSON_SetNumberValue(faultNotifDelay, period);
1090 //writing the new JSON to the configuration file
1091 stringConfiguration = cJSON_Print(jsonConfig);
1092 writeConfigFile(stringConfiguration);
1099 int ves_heartbeat_period_changed(int period)
1101 char *stringConfiguration = readConfigFileInString();
1103 if (stringConfiguration == NULL)
1105 printf("Could not read configuration file!\n");
1106 return SR_ERR_OPERATION_FAILED;
1109 cJSON *jsonConfig = cJSON_Parse(stringConfiguration);
1110 if (jsonConfig == NULL)
1112 free(stringConfiguration);
1113 const char *error_ptr = cJSON_GetErrorPtr();
1114 if (error_ptr != NULL)
1116 fprintf(stderr, "Could not parse JSON configuration! Error before: %s\n", error_ptr);
1118 return SR_ERR_OPERATION_FAILED;
1120 //we don't need the string anymore
1121 free(stringConfiguration);
1122 stringConfiguration = NULL;
1124 cJSON *notifConfig = cJSON_GetObjectItemCaseSensitive(jsonConfig, "notification-config");
1125 if (!cJSON_IsObject(notifConfig))
1127 printf("Configuration JSON is not as expected: notification-config is not an object");
1129 return SR_ERR_OPERATION_FAILED;
1132 cJSON *vesHeartbeatPeriod = cJSON_GetObjectItemCaseSensitive(notifConfig, "ves-heartbeat-period");
1133 if (!cJSON_IsNumber(vesHeartbeatPeriod))
1135 printf("Configuration JSON is not as expected: ves-heartbeat-period is not an object");
1137 return SR_ERR_OPERATION_FAILED;
1140 //we set the value of the fault-notification-delay-period object
1141 cJSON_SetNumberValue(vesHeartbeatPeriod, period);
1143 //writing the new JSON to the configuration file
1144 stringConfiguration = cJSON_Print(jsonConfig);
1145 writeConfigFile(stringConfiguration);
1152 static int add_keystore_entry_odl(char *url, char *credentials)
1156 curl_easy_reset(curl_odl);
1157 set_curl_common_info_odl();
1159 char url_for_curl[200];
1160 sprintf(url_for_curl, "%s", url);
1162 curl_easy_setopt(curl_odl, CURLOPT_URL, url_for_curl);
1164 char post_data_xml[2000];
1166 sprintf(post_data_xml,
1167 "<input xmlns=\"urn:opendaylight:netconf:keystore\">"
1169 "<key-id>device-key</key-id>"
1170 "<private-key>MIIEpAIBAAKCAQEAueCQaNQWoNmFK6LKu1p8U8ZWdWg/PvDdLsJyzfzl/Qw4UA68"
1171 "SfFNaY06zZl8QB9W02nr5kWeeMY0VA3adrPgOlvfx3oWlFbkETnMaN4OT3WTQ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1187 "dxV1g98K6f58x+MDsQu4tYWV4mmrQgaPH2dtwizvlMwmdpkh+LNWNtWuumowkJHc"
1188 "akIFo3XExQIFg6wYnGtQb4e5xrGa2xMpKlIJaXjb+YLiCYqJDG2ALFZrTrvuU2kV"
1189 "9a5qfqTc1qigvNolTM0iaaUCgYApmrZWhnLUdEKV2wP813PNxfioI4afxlpHD8LG"
1190 "sCn48gymR6E+Lihn7vuwq5B+8fYEH1ISWxLwW+RQUjIneNhy/jjfV8TgjyFqg7or"
1191 "0Sy4KjpiNI6kLBXOakELRNNMkeSPopGR2E7v5rr3bGD9oAD+aqX1G7oJH/KgPPYd"
1192 "Vl7+ZwKBgQDcHyWYrimjyUgKaQD2GmoO9wdcJYQ59ke9K+OuGlp4ti5arsi7N1tP"
1193 "B4f09aeELM2ASIuk8Q/Mx0jQFnm8lzRFXdewgvdPoZW/7VufM9O7dGPOc41cm2Dh"
1194 "yrTcXx/VmUBb+/fnXVEgCv7gylp/wtdTGHQBQJHR81jFBz0lnLj+gg==</private-key>"
1195 "<passphrase></passphrase>"
1199 printf("Post data:\n%s\n", post_data_xml);
1201 curl_easy_setopt(curl_odl, CURLOPT_POSTFIELDS, post_data_xml);
1202 curl_easy_setopt(curl_odl, CURLOPT_CUSTOMREQUEST, "POST");
1203 curl_easy_setopt(curl_odl, CURLOPT_USERPWD, credentials);
1205 res = curl_easy_perform(curl_odl);
1206 if (res != CURLE_OK)
1208 printf("cURL failed to url=%s\n", url_for_curl);
1211 long http_response_code = 0;
1212 curl_easy_getinfo (curl_odl, CURLINFO_RESPONSE_CODE, &http_response_code);
1213 if (http_response_code >= 200 && http_response_code <= 226 && http_response_code != CURLE_ABORTED_BY_CALLBACK)
1215 printf("cURL succeeded to url=%s\n", url_for_curl);
1219 printf("cURL to url=%s failed with code=%ld\n", url_for_curl, http_response_code);
1220 return SR_ERR_OPERATION_FAILED;
1226 static int add_private_key_odl(char *url, char *credentials)
1230 curl_easy_reset(curl_odl);
1231 set_curl_common_info_odl();
1233 char url_for_curl[200];
1234 sprintf(url_for_curl, "%s", url);
1236 curl_easy_setopt(curl_odl, CURLOPT_URL, url_for_curl);
1238 char post_data_xml[4000];
1240 sprintf(post_data_xml,
1241 "<input xmlns=\"urn:opendaylight:netconf:keystore\">"
1243 "<name>device-key</name>"
1244 "<data>MIIEpAIBAAKCAQEAueCQaNQWoNmFK6LKu1p8U8ZWdWg/PvDdLsJyzfzl/Qw4UA68SfFNaY06zZl8QB9W02nr5kWeeMY0VA3adrPgOlvfx3oWlFbkETnMaN4OT3WTQ0Wt6jAWZDzVfopwpJPAzRPxACDftIqFGagYcF32hZlVNqqnVdbXh0S0EViweqp/dbG4VDUHSNVbglc+u4UbEzNIFXMdEFsJZpkynOmSiTsIATqIhb+2srkVgLwhfkC2qkuHQwAHdubuB07ObM2z01UhyEdDvEYGHwtYAGDBL2TAcsI0oGeVkRyuOkV0QY0UN7UEFI1yTYw+xZ42HgFx3uGwApCImxhbj69GBYWFqwIDAQABAoIBAQCZN9kR8DGu6V7yt0Ax68asL8O5B/OKaHWKQ9LqpVrXmikZJOxkbzoGldow/CIFoU+q+Zbwu9aDa65a0wiP7Hoa4Py3q5XNNUrOQDyU/OYC7cI0I83WS0lJ2zOJGYj8wKae5Z81IeQFKGHK4lsy1OGPAvPRGh7RjUUgRavA2MCwe07rWRuDb/OJFe4Oh56UMEjwMiNBtMNtncogj1vr/qgRJdf9tf0zlJmLvUJ9+HSFFV9I/97LJyFhb95gAfHkjdVroLVgT3Cho+4PWtZaKCIGD0OwfOG2nLV4leXvRUk62/LMlB8NI9+JF7Xm+HCKbaWHNWC7mvWSLV58Zl4AbUWRAoGBANyJ6SFHFRHSPDY026SsdMzXR0eUxBAK7G70oSBKKhY+O1j0ocLEjI2krHJBhHbLlnvJVyMUaCUOTS5m0uDw9hgSsAqeSL3hL38kxVZw+KNG9Ouno1FlKnE/xXHlPQyeGs/P8nAMzHZxQtEsQdQayJEhK2XXHTsy7Q3MxDisfVJ1AoGBANfD34g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 curl_easy_setopt(curl_odl, CURLOPT_POSTFIELDS, post_data_xml);
1252 curl_easy_setopt(curl_odl, CURLOPT_CUSTOMREQUEST, "POST");
1253 curl_easy_setopt(curl_odl, CURLOPT_USERPWD, credentials);
1255 res = curl_easy_perform(curl_odl);
1256 if (res != CURLE_OK)
1258 printf("cURL failed to url=%s\n", url_for_curl);
1261 long http_response_code = 0;
1262 curl_easy_getinfo (curl_odl, CURLINFO_RESPONSE_CODE, &http_response_code);
1263 if (http_response_code >= 200 && http_response_code <= 226 && http_response_code != CURLE_ABORTED_BY_CALLBACK)
1265 printf("cURL succeeded to url=%s\n", url_for_curl);
1269 printf("cURL to url=%s failed with code=%ld\n", url_for_curl, http_response_code);
1270 return SR_ERR_OPERATION_FAILED;
1276 static int add_trusted_ca_odl(char *url, char *credentials)
1280 curl_easy_reset(curl_odl);
1281 set_curl_common_info_odl();
1283 char url_for_curl[200];
1284 sprintf(url_for_curl, "%s", url);
1286 curl_easy_setopt(curl_odl, CURLOPT_URL, url_for_curl);
1288 char post_data_xml[2000];
1290 sprintf(post_data_xml,
1291 "<input xmlns=\"urn:opendaylight:netconf:keystore\">"
1292 "<trusted-certificate>"
1293 "<name>test_trusted_cert</name>"
1294 "<certificate>MIID7TCCAtWgAwIBAgIJAMtE1NGAR5KoMA0GCSqGSIb3DQEBBQUAMIGMMQswCQYDVQQGEwJDWjEWMBQGA1UECAwNU291dGggTW9yYXZpYTENMAsGA1UEBwwEQnJubzEPMA0GA1UECgwGQ0VTTkVUMQwwCgYDVQQLDANUTUMxEzARBgNVBAMMCmV4YW1wbGUgQ0ExIjAgBgkqhkiG9w0BCQEWE2V4YW1wbGVjYUBsb2NhbGhvc3QwHhcNMTQwNzI0MTQxOTAyWhcNMjQwNzIxMTQxOTAyWjCBjDELMAkGA1UEBhMCQ1oxFjAUBgNVBAgMDVNvdXRoIE1vcmF2aWExDTALBgNVBAcMBEJybm8xDzANBgNVBAoMBkNFU05FVDEMMAoGA1UECwwDVE1DMRMwEQYDVQQDDApleGFtcGxlIENBMSIwIAYJKoZIhvcNAQkBFhNleGFtcGxlY2FAbG9jYWxob3N0MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEArD3TDHPAMT2Z84orK4lMlarbgooIUCcRZyLe+QM+8KY8Hn+mGaxPEOTSL3ywszqefB/Utm2hPKLHX684iRC14ID9WDGHxPjvoPArhgFhfV+qnPfxKTgxZC12uOj4u1V9y+SkTCocFbRfXVBGpojrBuDHXkDMDEWNvr8/52YCv7bGaiBwUHolcLCUbmtKILCG0RNJyTaJpXQdAeq5Z1SJotpbfYFFtAXB32hVoLug1dzl2tjG9sb1wq3QaDExcbC5w6P65qOkNoyym9ne6QlQagCqVDyFn3vcqkRaTjvZmxauCeUxXgJoXkyWcm0lM1KMHdoTArmchw2Dz0yHHSyDAQIDAQABo1AwTjAdBgNVHQ4EFgQUc1YQIqjZsHVwlea0AB4N+ilNI2gwHwYDVR0jBBgwFoAUc1YQIqjZsHVwlea0AB4N+ilNI2gwDAYDVR0TBAUwAwEB/zANBgkqhkiG9w0BAQUFAAOCAQEAI/1KH60qnw9Xs2RGfi0/IKf5EynXt4bQX8EIyVKwSkYKe04zZxYfLIl/Q2HOPYoFmm3daj5ddr0ZS1i4p4fTUhstjsYWvXs3W/HhVmFUslakkn3PrswhP77fCk6eEJLxdfyJ1C7Uudq2m1isZbKih+XF0mG1LxJaDMocSz4eAya7M5brwjy8DoOmA1TnLQFCVcpn+sCr7VC4wE/JqxyVhBCk/MuGqqM3B1j90bGFZ112ZOecyE0EDSr6IbiRBtmeNbEwOFjKXhNLYdxpBZ9D8A/368OckZkCrVLGuJNxK9UwCVTe8IhotHUqU9EqFDmxdV8oIdU/OzUwwNPA/Bd/9g==</certificate>"
1295 "</trusted-certificate>"
1298 printf("Post data:\n%s\n", post_data_xml);
1300 curl_easy_setopt(curl_odl, CURLOPT_POSTFIELDS, post_data_xml);
1301 curl_easy_setopt(curl_odl, CURLOPT_CUSTOMREQUEST, "POST");
1302 curl_easy_setopt(curl_odl, CURLOPT_USERPWD, credentials);
1304 res = curl_easy_perform(curl_odl);
1305 if (res != CURLE_OK)
1307 printf("cURL failed to url=%s\n", url_for_curl);
1310 long http_response_code = 0;
1311 curl_easy_getinfo (curl_odl, CURLINFO_RESPONSE_CODE, &http_response_code);
1312 if (http_response_code >= 200 && http_response_code <= 226 && http_response_code != CURLE_ABORTED_BY_CALLBACK)
1314 printf("cURL succeeded to url=%s\n", url_for_curl);
1318 printf("cURL to url=%s failed with code=%ld\n", url_for_curl, http_response_code);
1319 return SR_ERR_OPERATION_FAILED;
1325 int add_key_pair_to_odl(controller_t *controller_list, int controller_list_size)
1329 rc = add_keystore_entry_odl(controller_list[0].url_for_keystore_add, controller_list[0].credentials);
1330 if (rc != SR_ERR_OK)
1332 printf("Failed to add keystore entry to ODL.\n");
1335 rc = add_private_key_odl(controller_list[0].url_for_private_key_add, controller_list[0].credentials);
1336 if (rc != SR_ERR_OK)
1338 printf("Failed to add private key entry to ODL.\n");
1341 rc = add_trusted_ca_odl(controller_list[0].url_for_trusted_ca_add, controller_list[0].credentials);
1342 if (rc != SR_ERR_OK)
1344 printf("Failed to add trusted CA entry to ODL.\n");
1350 int ves_ip_changed(char *new_ip)
1352 char *stringConfiguration = readConfigFileInString();
1354 if (stringConfiguration == NULL)
1356 printf("Could not read configuration file!\n");
1357 return SR_ERR_OPERATION_FAILED;
1360 cJSON *jsonConfig = cJSON_Parse(stringConfiguration);
1361 if (jsonConfig == NULL)
1363 free(stringConfiguration);
1364 const char *error_ptr = cJSON_GetErrorPtr();
1365 if (error_ptr != NULL)
1367 fprintf(stderr, "Could not parse JSON configuration! Error before: %s\n", error_ptr);
1369 return SR_ERR_OPERATION_FAILED;
1371 //we don't need the string anymore
1372 free(stringConfiguration);
1373 stringConfiguration = NULL;
1375 cJSON *vesDetails = cJSON_GetObjectItemCaseSensitive(jsonConfig, "ves-endpoint-details");
1376 if (!cJSON_IsObject(vesDetails))
1378 printf("Configuration JSON is not as expected: ves-endpoint-details is not an object");
1380 return SR_ERR_OPERATION_FAILED;
1383 cJSON *vesIp = cJSON_GetObjectItemCaseSensitive(vesDetails, "ves-endpoint-ip");
1384 if (!cJSON_IsString(vesIp))
1386 printf("Configuration JSON is not as expected: ves-endpoint-ip is not a string");
1388 return SR_ERR_OPERATION_FAILED;
1391 //we set the value of the fault-notification-delay-period object
1392 cJSON_ReplaceItemInObject(vesDetails, "ves-endpoint-ip", cJSON_CreateString(new_ip));
1394 //writing the new JSON to the configuration file
1395 stringConfiguration = cJSON_Print(jsonConfig);
1396 writeConfigFile(stringConfiguration);
1403 int ves_port_changed(int new_port)
1405 char *stringConfiguration = readConfigFileInString();
1407 if (stringConfiguration == NULL)
1409 printf("Could not read configuration file!\n");
1410 return SR_ERR_OPERATION_FAILED;
1413 cJSON *jsonConfig = cJSON_Parse(stringConfiguration);
1414 if (jsonConfig == NULL)
1416 free(stringConfiguration);
1417 const char *error_ptr = cJSON_GetErrorPtr();
1418 if (error_ptr != NULL)
1420 fprintf(stderr, "Could not parse JSON configuration! Error before: %s\n", error_ptr);
1422 return SR_ERR_OPERATION_FAILED;
1424 //we don't need the string anymore
1425 free(stringConfiguration);
1426 stringConfiguration = NULL;
1428 cJSON *vesDetails = cJSON_GetObjectItemCaseSensitive(jsonConfig, "ves-endpoint-details");
1429 if (!cJSON_IsObject(vesDetails))
1431 printf("Configuration JSON is not as expected: ves-endpoint-details is not an object");
1433 return SR_ERR_OPERATION_FAILED;
1436 cJSON *vesPort = cJSON_GetObjectItemCaseSensitive(vesDetails, "ves-endpoint-port");
1437 if (!cJSON_IsNumber(vesPort))
1439 printf("Configuration JSON is not as expected: ves-endpoint-port is not a number.");
1441 return SR_ERR_OPERATION_FAILED;
1444 //we set the value of the fault-notification-delay-period object
1445 cJSON_SetNumberValue(vesPort, new_port);
1447 //writing the new JSON to the configuration file
1448 stringConfiguration = cJSON_Print(jsonConfig);
1449 writeConfigFile(stringConfiguration);
1456 int ves_registration_changed(cJSON_bool new_bool)
1458 char *stringConfiguration = readConfigFileInString();
1460 if (stringConfiguration == NULL)
1462 printf("Could not read configuration file!\n");
1463 return SR_ERR_OPERATION_FAILED;
1466 cJSON *jsonConfig = cJSON_Parse(stringConfiguration);
1467 if (jsonConfig == NULL)
1469 free(stringConfiguration);
1470 const char *error_ptr = cJSON_GetErrorPtr();
1471 if (error_ptr != NULL)
1473 fprintf(stderr, "Could not parse JSON configuration! Error before: %s\n", error_ptr);
1475 return SR_ERR_OPERATION_FAILED;
1477 //we don't need the string anymore
1478 free(stringConfiguration);
1479 stringConfiguration = NULL;
1481 cJSON *vesDetails = cJSON_GetObjectItemCaseSensitive(jsonConfig, "ves-endpoint-details");
1482 if (!cJSON_IsObject(vesDetails))
1484 printf("Configuration JSON is not as expected: ves-endpoint-details is not an object");
1486 return SR_ERR_OPERATION_FAILED;
1489 cJSON *vesRegistration = cJSON_GetObjectItemCaseSensitive(vesDetails, "ves-registration");
1490 if (!cJSON_IsBool(vesRegistration))
1492 printf("Configuration JSON is not as expected: ves-registration is not a bool.");
1494 return SR_ERR_OPERATION_FAILED;
1497 //we set the value of the ves-registration object
1498 cJSON_ReplaceItemInObject(vesDetails, "ves-registration", cJSON_CreateBool(new_bool));
1500 //writing the new JSON to the configuration file
1501 stringConfiguration = cJSON_Print(jsonConfig);
1502 writeConfigFile(stringConfiguration);
1509 int is_netconf_available_changed(cJSON_bool new_bool)
1511 char *stringConfiguration = readConfigFileInString();
1513 if (stringConfiguration == NULL)
1515 printf("Could not read configuration file!\n");
1516 return SR_ERR_OPERATION_FAILED;
1519 cJSON *jsonConfig = cJSON_Parse(stringConfiguration);
1520 if (jsonConfig == NULL)
1522 free(stringConfiguration);
1523 const char *error_ptr = cJSON_GetErrorPtr();
1524 if (error_ptr != NULL)
1526 fprintf(stderr, "Could not parse JSON configuration! Error before: %s\n", error_ptr);
1528 return SR_ERR_OPERATION_FAILED;
1530 //we don't need the string anymore
1531 free(stringConfiguration);
1532 stringConfiguration = NULL;
1534 cJSON *notifConfig = cJSON_GetObjectItemCaseSensitive(jsonConfig, "notification-config");
1535 if (!cJSON_IsObject(notifConfig))
1537 printf("Configuration JSON is not as expected: notification-config is not an object");
1539 return SR_ERR_OPERATION_FAILED;
1542 cJSON *isNetconfAvailable = cJSON_GetObjectItemCaseSensitive(notifConfig, "is-netconf-available");
1543 if (!cJSON_IsBool(isNetconfAvailable))
1545 printf("Configuration JSON is not as expected: is-netconf-available is not a bool.");
1547 return SR_ERR_OPERATION_FAILED;
1550 //we set the value of the ves-registration object
1551 cJSON_ReplaceItemInObject(notifConfig, "is-netconf-available", cJSON_CreateBool(new_bool));
1553 //writing the new JSON to the configuration file
1554 stringConfiguration = cJSON_Print(jsonConfig);
1555 writeConfigFile(stringConfiguration);
1562 int is_ves_available_changed(cJSON_bool new_bool)
1564 char *stringConfiguration = readConfigFileInString();
1566 if (stringConfiguration == NULL)
1568 printf("Could not read configuration file!\n");
1569 return SR_ERR_OPERATION_FAILED;
1572 cJSON *jsonConfig = cJSON_Parse(stringConfiguration);
1573 if (jsonConfig == NULL)
1575 free(stringConfiguration);
1576 const char *error_ptr = cJSON_GetErrorPtr();
1577 if (error_ptr != NULL)
1579 fprintf(stderr, "Could not parse JSON configuration! Error before: %s\n", error_ptr);
1581 return SR_ERR_OPERATION_FAILED;
1583 //we don't need the string anymore
1584 free(stringConfiguration);
1585 stringConfiguration = NULL;
1587 cJSON *notifConfig = cJSON_GetObjectItemCaseSensitive(jsonConfig, "notification-config");
1588 if (!cJSON_IsObject(notifConfig))
1590 printf("Configuration JSON is not as expected: notification-config is not an object");
1592 return SR_ERR_OPERATION_FAILED;
1595 cJSON *isVesAvailable = cJSON_GetObjectItemCaseSensitive(notifConfig, "is-ves-available");
1596 if (!cJSON_IsBool(isVesAvailable))
1598 printf("Configuration JSON is not as expected: is-ves-available is not a bool.");
1600 return SR_ERR_OPERATION_FAILED;
1603 //we set the value of the ves-registration object
1604 cJSON_ReplaceItemInObject(notifConfig, "is-ves-available", cJSON_CreateBool(new_bool));
1606 //writing the new JSON to the configuration file
1607 stringConfiguration = cJSON_Print(jsonConfig);
1608 writeConfigFile(stringConfiguration);