CI: Update RTD configuration file
[sim/o1-interface.git] / ntsimulator / ntsim-ng / utils / nts_utils.c
1 /*************************************************************************
2 *
3 * Copyright 2020 highstreet technologies GmbH and others
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 *     http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 ***************************************************************************/
17
18 #define _GNU_SOURCE
19
20 #include "nts_utils.h"
21 #include "utils/log_utils.h"
22 #include "utils/sys_utils.h"
23 #include "core/framework.h"
24 #include "core/session.h"
25 #include "core/xpath.h"
26 #include <stdlib.h>
27 #include <stdio.h>
28 #include <assert.h>
29
30
31 cJSON* ves_create_common_event_header(const char *domain, const char *event_type, const char *hostname, int port, const char *priority, int seq_id) {
32     assert(domain);
33     assert(event_type);
34     assert(hostname);
35     assert(priority);
36
37     char *eventId = 0;
38     long useconds = get_microseconds_since_epoch();
39
40     asprintf(&eventId, "%s-%d", event_type, seq_id);
41     if(eventId == 0) {
42         log_error("asprintf failed\n");
43         return 0;
44     }
45
46     cJSON *common_event_header = cJSON_CreateObject();
47     if(common_event_header == 0) {
48         log_error("could not create JSON object\n");
49         free(eventId);
50         return 0;
51     }
52
53     if(cJSON_AddStringToObject(common_event_header, "domain", domain) == 0) {
54         log_error("cJSON AddStringToObject error\n");
55         free(eventId);
56         cJSON_Delete(common_event_header);
57         return 0;
58     }
59
60     if(cJSON_AddStringToObject(common_event_header, "eventId", eventId) == 0) {
61         log_error("cJSON AddStringToObject error\n");
62         free(eventId);
63         cJSON_Delete(common_event_header);
64         return 0;
65     }
66
67     free(eventId);
68
69     if(cJSON_AddStringToObject(common_event_header, "eventName", event_type) == 0) {
70         log_error("cJSON AddStringToObject error\n");
71         cJSON_Delete(common_event_header);
72         return 0;
73     }
74
75     if(cJSON_AddNumberToObject(common_event_header, "sequence", (double)(seq_id)) == 0) {
76         log_error("cJSON AddNumberToObject error\n");
77         cJSON_Delete(common_event_header);
78         return 0;
79     }
80
81     if(cJSON_AddStringToObject(common_event_header, "priority", priority) == 0) {
82         log_error("cJSON AddStringToObject error\n");
83         cJSON_Delete(common_event_header);
84         return 0;
85     }
86
87     if(cJSON_AddStringToObject(common_event_header, "reportingEntityId", "") == 0) {
88         log_error("cJSON AddStringToObject error\n");
89         cJSON_Delete(common_event_header);
90         return 0;
91     }
92
93     char source_name[512];
94     if(port) {
95         sprintf(source_name, "%s-%d", hostname, port);
96     }
97     else {
98         sprintf(source_name, "%s", hostname);
99     }
100
101     if(cJSON_AddStringToObject(common_event_header, "reportingEntityName", source_name) == 0) {
102         log_error("cJSON AddStringToObject error\n");
103         cJSON_Delete(common_event_header);
104         return 0;
105     }
106
107     if(cJSON_AddStringToObject(common_event_header, "sourceId", "") == 0) {
108         log_error("cJSON AddStringToObject error\n");
109         cJSON_Delete(common_event_header);
110         return 0;
111     }
112
113     if(cJSON_AddStringToObject(common_event_header, "sourceName", source_name) == 0) {
114         log_error("cJSON AddStringToObject error\n");
115         cJSON_Delete(common_event_header);
116         return 0;
117     }
118
119     if(cJSON_AddNumberToObject(common_event_header, "startEpochMicrosec", (double)(useconds)) == 0) {
120         log_error("cJSON AddNumberToObject error\n");
121         cJSON_Delete(common_event_header);
122         return 0;
123     }
124
125     if(cJSON_AddNumberToObject(common_event_header, "lastEpochMicrosec", (double)(useconds)) == 0) {
126         log_error("cJSON AddNumberToObject error\n");
127         cJSON_Delete(common_event_header);
128         return 0;
129     }
130
131     if(cJSON_AddStringToObject(common_event_header, "nfNamingCode", "sdn controller") == 0) {
132         log_error("cJSON AddStringToObject error\n");
133         cJSON_Delete(common_event_header);
134         return 0;
135     }
136
137     if(cJSON_AddStringToObject(common_event_header, "nfVendorName", "sdn") == 0) {
138         log_error("cJSON AddStringToObject error\n");
139         cJSON_Delete(common_event_header);
140         return 0;
141     }
142
143     if(cJSON_AddStringToObject(common_event_header, "timeZoneOffset", "+00:00") == 0) {
144         log_error("cJSON AddStringToObject error\n");
145         cJSON_Delete(common_event_header);
146         return 0;
147     }
148
149     if(cJSON_AddStringToObject(common_event_header, "version", "4.1") == 0) {
150         log_error("cJSON AddStringToObject error\n");
151         cJSON_Delete(common_event_header);
152         return 0;
153     }
154
155     if(cJSON_AddStringToObject(common_event_header, "vesEventListenerVersion", framework_environment.ves_endpoint.common_header_version) == 0) {
156         log_error("cJSON AddStringToObject error\n");
157         cJSON_Delete(common_event_header);
158         return 0;
159     }
160
161     return common_event_header;
162 }
163
164 cJSON* ves_create_common_event_header_721(const char *domain, const char *event_type, const char *hostname, int port, const char *priority, int seq_id, const char *stnd_defined_namespace) {
165     assert(domain);
166     assert(event_type);
167     assert(hostname);
168     assert(priority);
169
170     char *eventId = 0;
171     long useconds = get_microseconds_since_epoch();
172
173     asprintf(&eventId, "%s-%d", event_type, seq_id);
174     if(eventId == 0) {
175         log_error("asprintf failed\n");
176         return 0;
177     }
178
179     cJSON *common_event_header = cJSON_CreateObject();
180     if(common_event_header == 0) {
181         log_error("could not create JSON object\n");
182         free(eventId);
183         return 0;
184     }
185
186     if(cJSON_AddStringToObject(common_event_header, "domain", domain) == 0) {
187         log_error("cJSON AddStringToObject error\n");
188         free(eventId);
189         cJSON_Delete(common_event_header);
190         return 0;
191     }
192
193     if(cJSON_AddStringToObject(common_event_header, "eventId", eventId) == 0) {
194         log_error("cJSON AddStringToObject error\n");
195         free(eventId);
196         cJSON_Delete(common_event_header);
197         return 0;
198     }
199
200     free(eventId);
201
202     char *eventName = 0;
203     asprintf(&eventName, "ves-stndDefined-%s", event_type);
204     if(eventId == 0) {
205         log_error("asprintf failed\n");
206         return 0;
207     }
208
209     if(cJSON_AddStringToObject(common_event_header, "eventName", eventName) == 0) {
210         log_error("cJSON AddStringToObject error\n");
211         cJSON_Delete(common_event_header);
212         free(eventName);
213         return 0;
214     }
215     free(eventName);
216
217     if(cJSON_AddStringToObject(common_event_header, "eventType", event_type) == 0) {
218         log_error("cJSON AddStringToObject error\n");
219         cJSON_Delete(common_event_header);
220         return 0;
221     }
222
223     if(cJSON_AddNumberToObject(common_event_header, "sequence", (double)(seq_id)) == 0) {
224         log_error("cJSON AddNumberToObject error\n");
225         cJSON_Delete(common_event_header);
226         return 0;
227     }
228
229     if(cJSON_AddStringToObject(common_event_header, "priority", priority) == 0) {
230         log_error("cJSON AddStringToObject error\n");
231         cJSON_Delete(common_event_header);
232         return 0;
233     }
234
235     if(cJSON_AddStringToObject(common_event_header, "reportingEntityId", "") == 0) {
236         log_error("cJSON AddStringToObject error\n");
237         cJSON_Delete(common_event_header);
238         return 0;
239     }
240
241     char source_name[512];
242     if(port) {
243         sprintf(source_name, "%s-%d", hostname, port);
244     }
245     else {
246         sprintf(source_name, "%s", hostname);
247     }
248
249     if(cJSON_AddStringToObject(common_event_header, "reportingEntityName", source_name) == 0) {
250         log_error("cJSON AddStringToObject error\n");
251         cJSON_Delete(common_event_header);
252         return 0;
253     }
254
255     if(cJSON_AddStringToObject(common_event_header, "sourceId", "") == 0) {
256         log_error("cJSON AddStringToObject error\n");
257         cJSON_Delete(common_event_header);
258         return 0;
259     }
260
261     if(cJSON_AddStringToObject(common_event_header, "sourceName", source_name) == 0) {
262         log_error("cJSON AddStringToObject error\n");
263         cJSON_Delete(common_event_header);
264         return 0;
265     }
266
267     if(cJSON_AddNumberToObject(common_event_header, "startEpochMicrosec", (double)(useconds)) == 0) {
268         log_error("cJSON AddNumberToObject error\n");
269         cJSON_Delete(common_event_header);
270         return 0;
271     }
272
273     if(cJSON_AddNumberToObject(common_event_header, "lastEpochMicrosec", (double)(useconds)) == 0) {
274         log_error("cJSON AddNumberToObject error\n");
275         cJSON_Delete(common_event_header);
276         return 0;
277     }
278
279     if(cJSON_AddStringToObject(common_event_header, "nfNamingCode", "sdn controller") == 0) {
280         log_error("cJSON AddStringToObject error\n");
281         cJSON_Delete(common_event_header);
282         return 0;
283     }
284
285     if(cJSON_AddStringToObject(common_event_header, "nfVendorName", "sdn") == 0) {
286         log_error("cJSON AddStringToObject error\n");
287         cJSON_Delete(common_event_header);
288         return 0;
289     }
290
291     if(cJSON_AddStringToObject(common_event_header, "timeZoneOffset", "+00:00") == 0) {
292         log_error("cJSON AddStringToObject error\n");
293         cJSON_Delete(common_event_header);
294         return 0;
295     }
296
297     if(cJSON_AddStringToObject(common_event_header, "version", "4.1") == 0) {
298         log_error("cJSON AddStringToObject error\n");
299         cJSON_Delete(common_event_header);
300         return 0;
301     }
302
303     if(cJSON_AddStringToObject(common_event_header, "vesEventListenerVersion", "7.2.1") == 0) {
304         log_error("cJSON AddStringToObject error\n");
305         cJSON_Delete(common_event_header);
306         return 0;
307     }
308
309     if(cJSON_AddStringToObject(common_event_header, "stndDefinedNamespace", stnd_defined_namespace) == 0) {
310         log_error("cJSON AddStringToObject error\n");
311         cJSON_Delete(common_event_header);
312         return 0;
313     }
314
315     if(cJSON_AddStringToObject(common_event_header, "nfcNamingCode", "NFC") == 0) {
316         log_error("cJSON AddStringToObject error\n");
317         cJSON_Delete(common_event_header);
318         return 0;
319     }
320
321     return common_event_header;
322 }
323
324
325
326 nts_mount_point_addressing_method_t nts_mount_point_addressing_method_get(sr_session_ctx_t *current_session) {
327     assert_session();
328
329     nts_mount_point_addressing_method_t ret = UNKNOWN_MAPPING;
330
331     int rc;
332     bool session_started = false;
333     if(current_session == 0) {
334         rc = sr_session_start(session_connection, SR_DS_RUNNING, &current_session);
335         if(rc != SR_ERR_OK) {
336             log_error("could not start sysrepo session\n");
337             return ret;
338         }
339         session_started = true;
340     }
341
342     sr_val_t *value = 0;
343     rc = sr_get_item(current_session, NTS_NF_NETWORK_FUNCTION_MPAM_SCHEMA_XPATH, 0, &value);
344     if(rc == SR_ERR_OK) {
345         if(strcmp(value->data.enum_val, "host-mapping") == 0) {
346             ret = HOST_MAPPING;
347         }
348         else {
349             ret = DOCKER_MAPPING;
350         }
351         sr_free_val(value);
352     }
353
354     if(session_started) {
355         rc = sr_session_stop(current_session);
356         if(rc != SR_ERR_OK) {
357             log_error("could not stop sysrepo session\n");
358             return ret;
359         }
360     }
361
362     return ret;
363 }
364
365 // checkAS authentication via certificate not supported yet
366 ves_details_t *ves_endpoint_details_get(sr_session_ctx_t *current_session, const char *custom_path) {
367     assert_session();
368
369     int rc;
370     bool session_started = false;
371     if(current_session == 0) {
372         rc = sr_session_start(session_connection, SR_DS_RUNNING, &current_session);
373         if(rc != SR_ERR_OK) {
374             log_error("could not start sysrepo session\n");
375             return 0;
376         }
377         session_started = true;
378     }
379
380     struct lyd_node *data = 0;
381     char *xpath_to_get;
382
383     if(custom_path == 0) {
384         if(framework_arguments.nts_mode == NTS_MODE_MANAGER) {
385             xpath_to_get = NTS_MANAGER_VES_ENDPOINT_CONFIG_XPATH;
386         }
387         else {
388             xpath_to_get = NTS_NF_VES_ENDPOINT_CONFIG_XPATH;
389         }
390     }
391     else {
392         xpath_to_get = (char *)custom_path;
393     }
394
395     rc = sr_get_subtree(current_session, xpath_to_get, 0, &data);
396     if(rc != SR_ERR_OK) {
397         log_error("could not get value for xPath=%s from the running datastore\n", xpath_to_get);
398         if(session_started) {
399             sr_session_stop(current_session);
400         }
401         return 0;
402     }
403
404     if(session_started) {
405         rc = sr_session_stop(current_session);
406         if(rc != SR_ERR_OK) {
407             log_error("could not stop sysrepo session\n");
408             lyd_free(data);
409             return 0;
410         }
411     }
412
413     if(data->child == 0) {
414         log_error("ves-endpoint probably not set yet\n", xpath_to_get);
415         lyd_free(data);
416         return 0;
417     }
418
419     ves_details_t *ret = (ves_details_t *)malloc(sizeof(ves_details_t));
420     if(!ret) {
421         log_error("malloc failed\n");
422         lyd_free(data);
423         return 0;
424     }
425
426     ret->protocol = 0;
427     ret->ip = 0;
428     ret->port = 0;
429     ret->auth_method = 0;
430     ret->username = 0;
431     ret->password = 0;
432
433     struct lyd_node *chd = 0;
434     LY_TREE_FOR(data->child, chd) {
435         const char *val = ((const struct lyd_node_leaf_list *)chd)->value_str;
436
437         if(strcmp(chd->schema->name, "ves-endpoint-protocol") == 0) {
438             ret->protocol = strdup(val);
439         }
440         else if(strcmp(chd->schema->name, "ves-endpoint-ip") == 0) {
441             ret->ip = strdup(val);
442         }
443         else if(strcmp(chd->schema->name, "ves-endpoint-port") == 0) {
444             ret->port = ((const struct lyd_node_leaf_list *)chd)->value.uint16;
445         }
446         else if(strcmp(chd->schema->name, "ves-endpoint-auth-method") == 0) {
447             ret->auth_method = strdup(val);
448         }
449         else if(strcmp(chd->schema->name, "ves-endpoint-username") == 0) {
450             ret->username = strdup(val);
451         }
452         else if(strcmp(chd->schema->name, "ves-endpoint-password") == 0) {
453             ret->password = strdup(val);
454         }
455     }
456     lyd_free(data);
457
458     if (strstr(ret->ip, ":")) {
459         // IPv6 address
460         asprintf(&ret->url, "%s://[%s]:%d/eventListener/v7", ret->protocol, ret->ip, ret->port);
461     }
462     else {
463         if (framework_environment.ves_endpoint.port_absent == true) {
464             // hostname addressing with port missing
465             asprintf(&ret->url, "%s://%s/eventListener/v7", ret->protocol, ret->ip);
466         }
467         else {
468             // normal addressing with IP and Port
469             asprintf(&ret->url, "%s://%s:%d/eventListener/v7", ret->protocol, ret->ip, ret->port);
470         }
471
472     }
473
474     if((ret->protocol == 0) || (ret->ip == 0) || (ret->auth_method == 0) || (ret->username == 0) || (ret->password == 0) || (ret->url == 0)) {
475         free(ret->protocol);
476         free(ret->ip);
477         free(ret->auth_method);
478         free(ret->username);
479         free(ret->password);
480         free(ret->url);
481         free(ret);
482         ret = 0;
483     }
484
485     return ret;
486 }
487
488 void ves_details_free(ves_details_t *instance) {
489     assert(instance);
490
491     free(instance->protocol);
492     free(instance->ip);
493     free(instance->url);
494     free(instance->auth_method);
495     free(instance->username);
496     free(instance->password);
497     free(instance);
498 }
499
500
501 // checkAS authentication via certificate not supported yet
502 controller_details_t *controller_details_get(sr_session_ctx_t *current_session) {
503     assert_session();
504
505     int rc;
506     bool session_started = false;
507     if(current_session == 0) {
508         rc = sr_session_start(session_connection, SR_DS_RUNNING, &current_session);
509         if(rc != SR_ERR_OK) {
510             log_error("could not start sysrepo session\n");
511             return 0;
512         }
513         session_started = true;
514     }
515
516     struct lyd_node *data = 0;
517     char *xpath_to_get;
518
519     if(framework_arguments.nts_mode == NTS_MODE_MANAGER) {
520         xpath_to_get = NTS_MANAGER_SDN_CONTROLLER_CONFIG_XPATH;
521     }
522     else {
523         xpath_to_get = NTS_NF_SDN_CONTROLLER_CONFIG_XPATH;
524     }
525
526     rc = sr_get_subtree(current_session, xpath_to_get, 0, &data);
527     if(rc != SR_ERR_OK) {
528         log_error("could not get value for xPath=%s from the running datastore\n", xpath_to_get);
529         if(session_started) {
530             sr_session_stop(current_session);
531         }
532         return 0;
533     }
534
535     if(session_started) {
536         rc = sr_session_stop(current_session);
537         if(rc != SR_ERR_OK) {
538             log_error("could not stop sysrepo session\n");
539             lyd_free(data);
540             return 0;
541         }
542     }
543
544     if(data->child == 0) {
545         log_error("sdn-controller probably not set yet\n");
546         lyd_free(data);
547         return 0;
548     }
549
550     controller_details_t *ret = (controller_details_t *)malloc(sizeof(controller_details_t));
551     if(!ret) {
552         log_error("malloc failed\n");
553         lyd_free(data);
554         return 0;
555     }
556
557     ret->protocol = 0;
558     ret->ip = 0;
559     ret->port = 0;
560     ret->nc_callhome_ip = 0;
561     ret->nc_callhome_port = 0;
562     ret->auth_method = 0;
563     ret->username = 0;
564     ret->password = 0;
565
566     ret->auth_method = strdup("basic");
567
568     struct lyd_node *chd = 0;
569     LY_TREE_FOR(data->child, chd) {
570         const char *val = ((const struct lyd_node_leaf_list *)chd)->value_str;
571
572         if(strcmp(chd->schema->name, "controller-protocol") == 0) {
573             ret->protocol = strdup(val);
574         }
575         else if(strcmp(chd->schema->name, "controller-ip") == 0) {
576             ret->ip = strdup(val);
577         }
578         else if(strcmp(chd->schema->name, "controller-port") == 0) {
579             ret->port = ((const struct lyd_node_leaf_list *)chd)->value.uint16;
580         }
581         else if(strcmp(chd->schema->name, "controller-netconf-call-home-ip") == 0) {
582             ret->nc_callhome_ip = strdup(val);
583         }
584         else if(strcmp(chd->schema->name, "controller-netconf-call-home-port") == 0) {
585             ret->nc_callhome_port = ((const struct lyd_node_leaf_list *)chd)->value.uint16;
586         }
587         else if(strcmp(chd->schema->name, "controller-username") == 0) {
588             ret->username = strdup(val);
589         }
590         else if(strcmp(chd->schema->name, "controller-password") == 0) {
591             ret->password = strdup(val);
592         }
593     }
594     lyd_free(data);
595
596     if (strstr(ret->ip, ":")) {
597         // IPv6 address
598         asprintf(&ret->base_url, "%s://[%s]:%d", ret->protocol, ret->ip, ret->port);
599     }
600     else {
601         if (framework_environment.sdn_controller.port_absent == true) {
602             // hostname without port addressing
603             asprintf(&ret->base_url, "%s://%s", ret->protocol, ret->ip);
604         }
605         else {
606             // normal IP and Port addressing
607             asprintf(&ret->base_url, "%s://%s:%d", ret->protocol, ret->ip, ret->port);
608         }
609     }
610
611     if((ret->protocol == 0) || (ret->ip == 0) || (ret->nc_callhome_ip == 0) || (ret->auth_method == 0) || (ret->username == 0) || (ret->password == 0) || (ret->base_url == 0)) {
612         free(ret->protocol);
613         free(ret->ip);
614         free(ret->nc_callhome_ip);
615         free(ret->auth_method);
616         free(ret->username);
617         free(ret->password);
618         free(ret->base_url);
619         free(ret);
620         ret = 0;
621     }
622
623     return ret;
624 }
625
626 void controller_details_free(controller_details_t *instance) {
627     assert(instance);
628
629     free(instance->protocol);
630     free(instance->ip);
631     free(instance->nc_callhome_ip);
632     free(instance->base_url);
633     free(instance->auth_method);
634     free(instance->username);
635     free(instance->password);
636     free(instance);
637 }
638
639 int nts_vercmp(const char *ver1, const char *ver2) {
640     assert(ver1);
641     assert(ver2);
642
643     int i = 0;
644     int v1 = 0, v2 = 0, v3 = 0;
645     while(ver1[i] && (ver1[i] != '.')) {
646         v1 *= 10;
647         v1 += ver1[i] - '0';
648         i++;
649     }
650
651     if(ver1[i]) {
652         i++;
653         while(ver1[i] && (ver1[i] != '.')) {
654             v2 *= 10;
655             v2 += ver1[i] - '0';
656             i++;
657         }
658
659         if(ver1[i]) {
660             i++;
661             while(ver1[i] && (ver1[i] != '.')) {
662                 v3 *= 10;
663                 v3 += ver1[i] - '0';
664                 i++;
665             }
666         }
667     }
668
669
670     int V1 = 0, V2 = 0, V3 = 0;
671     i = 0;
672     while(ver2[i] && (ver2[i] != '.')) {
673         V1 *= 10;
674         V1 += ver2[i] - '0';
675         i++;
676     }
677
678     if(ver2[i]) {
679         i++;
680         while(ver2[i] && (ver2[i] != '.')) {
681             V2 *= 10;
682             V2 += ver2[i] - '0';
683             i++;
684         }
685
686         if(ver2[i]) {
687             i++;
688             while(ver2[i] && (ver2[i] != '.')) {
689                 V3 *= 10;
690                 V3 += ver2[i] - '0';
691                 i++;
692             }
693         }
694     }
695
696     if(v1 < V1) {
697         return -1;
698     }
699     else if(v1 > V1) {
700         return 1;
701     }
702
703     if(v2 < V2) {
704         return -1;
705     }
706     else if(v2 > V2) {
707         return 1;
708     }
709
710     if(v3 < V3) {
711         return -1;
712     }
713     else if(v3 > V3) {
714         return 1;
715     }
716
717     return 0;
718 }
719
720 int nts_utils_populate_info(sr_session_ctx_t *current_session, const char *function_type) {
721     assert(current_session);
722     assert(function_type);
723
724     bool manager = (strcmp(function_type, "NTS_FUNCTION_TYPE_MANAGER") == 0);
725
726     int rc;
727     char int_to_str[30];
728     //setup sdn-controller defaults
729     if(strlen(framework_environment.sdn_controller.protocol)) {
730         if(manager) {
731             rc = sr_set_item_str(current_session, NTS_MANAGER_SDN_CONTROLLER_CONFIG_XPATH"/controller-protocol", (const char*)framework_environment.sdn_controller.protocol, 0, 0);
732         }
733         else {
734             rc = sr_set_item_str(current_session, NTS_NF_SDN_CONTROLLER_CONFIG_XPATH"/controller-protocol", (const char*)framework_environment.sdn_controller.protocol, 0, 0);
735         }
736         if(rc != SR_ERR_OK) {
737             log_error("sr_set_item_str failed\n");
738             return NTS_ERR_FAILED;
739         }
740     }
741
742     if(strlen(framework_environment.sdn_controller.ip)) {
743         if(manager) {
744             rc = sr_set_item_str(current_session, NTS_MANAGER_SDN_CONTROLLER_CONFIG_XPATH"/controller-ip", (const char*)framework_environment.sdn_controller.ip, 0, 0);
745         }
746         else {
747             rc = sr_set_item_str(current_session, NTS_NF_SDN_CONTROLLER_CONFIG_XPATH"/controller-ip", (const char*)framework_environment.sdn_controller.ip, 0, 0);
748         }
749         if(rc != SR_ERR_OK) {
750             log_error("sr_set_item_str failed\n");
751             return NTS_ERR_FAILED;
752         }
753     }
754
755     sprintf(int_to_str, "%d", framework_environment.sdn_controller.port);
756     if(manager) {
757         rc = sr_set_item_str(current_session, NTS_MANAGER_SDN_CONTROLLER_CONFIG_XPATH"/controller-port", (const char*)int_to_str, 0, 0);
758     }
759     else {
760         rc = sr_set_item_str(current_session, NTS_NF_SDN_CONTROLLER_CONFIG_XPATH"/controller-port", (const char*)int_to_str, 0, 0);
761     }
762     if(rc != SR_ERR_OK) {
763         log_error("sr_set_item_str failed\n");
764         return NTS_ERR_FAILED;
765     }
766
767     if(strlen(framework_environment.sdn_controller.callhome_ip)) {
768         if(manager) {
769             rc = sr_set_item_str(current_session, NTS_MANAGER_SDN_CONTROLLER_CONFIG_XPATH"/controller-netconf-call-home-ip", (const char*)framework_environment.sdn_controller.callhome_ip, 0, 0);
770         }
771         else {
772             rc = sr_set_item_str(current_session, NTS_NF_SDN_CONTROLLER_CONFIG_XPATH"/controller-netconf-call-home-ip", (const char*)framework_environment.sdn_controller.callhome_ip, 0, 0);
773         }
774         if(rc != SR_ERR_OK) {
775             log_error("sr_set_item_str failed\n");
776             return NTS_ERR_FAILED;
777         }
778     }
779
780     sprintf(int_to_str, "%d", framework_environment.sdn_controller.callhome_port);
781     if(manager) {
782         rc = sr_set_item_str(current_session, NTS_MANAGER_SDN_CONTROLLER_CONFIG_XPATH"/controller-netconf-call-home-port", (const char*)int_to_str, 0, 0);
783     }
784     else {
785         rc = sr_set_item_str(current_session, NTS_NF_SDN_CONTROLLER_CONFIG_XPATH"/controller-netconf-call-home-port", (const char*)int_to_str, 0, 0);
786     }
787     if(rc != SR_ERR_OK) {
788         log_error("sr_set_item_str failed\n");
789         return NTS_ERR_FAILED;
790     }
791
792     if(strlen(framework_environment.sdn_controller.username)) {
793         if(manager) {
794             rc = sr_set_item_str(current_session, NTS_MANAGER_SDN_CONTROLLER_CONFIG_XPATH"/controller-username", (const char*)framework_environment.sdn_controller.username, 0, 0);
795         }
796         else {
797             rc = sr_set_item_str(current_session, NTS_NF_SDN_CONTROLLER_CONFIG_XPATH"/controller-username", (const char*)framework_environment.sdn_controller.username, 0, 0);
798         }
799         if(rc != SR_ERR_OK) {
800             log_error("sr_set_item_str failed\n");
801             return NTS_ERR_FAILED;
802         }
803     }
804
805     if(strlen(framework_environment.sdn_controller.password)) {
806         if(manager) {
807             rc = sr_set_item_str(current_session, NTS_MANAGER_SDN_CONTROLLER_CONFIG_XPATH"/controller-password", (const char*)framework_environment.sdn_controller.password, 0, 0);
808         }
809         else {
810             rc = sr_set_item_str(current_session, NTS_NF_SDN_CONTROLLER_CONFIG_XPATH"/controller-password", (const char*)framework_environment.sdn_controller.password, 0, 0);
811         }
812         if(rc != SR_ERR_OK) {
813             log_error("sr_set_item_str failed\n");
814             return NTS_ERR_FAILED;
815         }
816     }
817
818     //setup ves-endpoint details
819     if(strlen(framework_environment.ves_endpoint.protocol)) {
820         if(manager) {
821             rc = sr_set_item_str(current_session, NTS_MANAGER_VES_ENDPOINT_CONFIG_XPATH"/ves-endpoint-protocol", (const char*)framework_environment.ves_endpoint.protocol, 0, 0);
822         }
823         else {
824             rc = sr_set_item_str(current_session, NTS_NF_VES_ENDPOINT_CONFIG_XPATH"/ves-endpoint-protocol", (const char*)framework_environment.ves_endpoint.protocol, 0, 0);
825         }
826         if(rc != SR_ERR_OK) {
827             log_error("sr_set_item_str failed\n");
828             return NTS_ERR_FAILED;
829         }
830     }
831
832     if(strlen(framework_environment.ves_endpoint.ip)) {
833         if(manager) {
834             rc = sr_set_item_str(current_session, NTS_MANAGER_VES_ENDPOINT_CONFIG_XPATH"/ves-endpoint-ip", (const char*)framework_environment.ves_endpoint.ip, 0, 0);
835         }
836         else {
837             rc = sr_set_item_str(current_session, NTS_NF_VES_ENDPOINT_CONFIG_XPATH"/ves-endpoint-ip", (const char*)framework_environment.ves_endpoint.ip, 0, 0);
838         }
839         if(rc != SR_ERR_OK) {
840             log_error("sr_set_item_str failed\n");
841             return NTS_ERR_FAILED;
842         }
843     }
844
845     sprintf(int_to_str, "%d", framework_environment.ves_endpoint.port);
846     if(manager) {
847         rc = sr_set_item_str(current_session, NTS_MANAGER_VES_ENDPOINT_CONFIG_XPATH"/ves-endpoint-port", (const char*)int_to_str, 0, 0);
848     }
849     else {
850         rc = sr_set_item_str(current_session, NTS_NF_VES_ENDPOINT_CONFIG_XPATH"/ves-endpoint-port", (const char*)int_to_str, 0, 0);
851     }
852     if(rc != SR_ERR_OK) {
853         log_error("sr_set_item_str failed\n");
854         return NTS_ERR_FAILED;
855     }
856
857     if(strlen(framework_environment.ves_endpoint.auth_method)) {
858         if(manager) {
859             rc = sr_set_item_str(current_session, NTS_MANAGER_VES_ENDPOINT_CONFIG_XPATH"/ves-endpoint-auth-method", (const char*)framework_environment.ves_endpoint.auth_method, 0, 0);
860         }
861         else {
862             rc = sr_set_item_str(current_session, NTS_NF_VES_ENDPOINT_CONFIG_XPATH"/ves-endpoint-auth-method", (const char*)framework_environment.ves_endpoint.auth_method, 0, 0);
863         }
864         if(rc != SR_ERR_OK) {
865             log_error("sr_set_item_str failed\n");
866             return NTS_ERR_FAILED;
867         }
868     }
869
870     if(strlen(framework_environment.ves_endpoint.username)) {
871         if(manager) {
872             rc = sr_set_item_str(current_session, NTS_MANAGER_VES_ENDPOINT_CONFIG_XPATH"/ves-endpoint-username", (const char*)framework_environment.ves_endpoint.username, 0, 0);
873         }
874         else {
875             rc = sr_set_item_str(current_session, NTS_NF_VES_ENDPOINT_CONFIG_XPATH"/ves-endpoint-username", (const char*)framework_environment.ves_endpoint.username, 0, 0);
876         }
877         if(rc != SR_ERR_OK) {
878             log_error("sr_set_item_str failed\n");
879             return NTS_ERR_FAILED;
880         }
881     }
882
883     if(strlen(framework_environment.ves_endpoint.password)) {
884         if(manager) {
885             rc = sr_set_item_str(current_session, NTS_MANAGER_VES_ENDPOINT_CONFIG_XPATH"/ves-endpoint-password", (const char*)framework_environment.ves_endpoint.password, 0, 0);
886         }
887         else {
888             rc = sr_set_item_str(current_session, NTS_NF_VES_ENDPOINT_CONFIG_XPATH"/ves-endpoint-password", (const char*)framework_environment.ves_endpoint.password, 0, 0);
889         }
890         if(rc != SR_ERR_OK) {
891             log_error("sr_set_item_str failed\n");
892             return NTS_ERR_FAILED;
893         }
894     }
895
896     if(strlen(framework_environment.ves_endpoint.certificate)) {
897         if(manager) {
898             rc = sr_set_item_str(current_session, NTS_MANAGER_VES_ENDPOINT_CONFIG_XPATH"/ves-endpoint-certificate", (const char*)framework_environment.ves_endpoint.certificate, 0, 0);
899         }
900         else {
901             rc = sr_set_item_str(current_session, NTS_NF_VES_ENDPOINT_CONFIG_XPATH"/ves-endpoint-certificate", (const char*)framework_environment.ves_endpoint.certificate, 0, 0);
902         }
903         if(rc != SR_ERR_OK) {
904             log_error("sr_set_item_str failed\n");
905             return NTS_ERR_FAILED;
906         }
907     }
908
909     if(manager == false) {
910         //presence containers
911         rc = sr_set_item_str(current_session, NTS_NF_FAULT_GENERATION_SCHEMA_XPATH, 0, 0, 0);
912         if(rc != SR_ERR_OK) {
913             log_error("sr_set_item_str failed\n");
914             return NTS_ERR_FAILED;
915         }
916
917         rc = sr_set_item_str(current_session, NTS_NF_NETCONF_SCHEMA_XPATH, 0, 0, 0);
918         if(rc != SR_ERR_OK) {
919             log_error("sr_set_item_str failed\n");
920             return NTS_ERR_FAILED;
921         }
922
923         rc = sr_set_item_str(current_session, NTS_NF_VES_SCHEMA_XPATH, 0, 0, 0);
924         if(rc != SR_ERR_OK) {
925             log_error("sr_set_item_str failed\n");
926             return NTS_ERR_FAILED;
927         }
928     }
929
930     //also set the network-function module for easy identifying the function type
931     rc = sr_set_item_str(current_session, NTS_NF_NETWORK_FUNCTION_FTYPE_SCHEMA_XPATH, function_type, 0, 0);
932     if(rc != SR_ERR_OK) {
933         log_error("sr_set_item_str failed\n");
934         return NTS_ERR_FAILED;
935     }
936
937     //mount-point-addressing-method
938     rc = sr_set_item_str(current_session, NTS_NF_NETWORK_FUNCTION_MPAM_SCHEMA_XPATH, framework_environment.nts.nf_mount_point_addressing_method, 0, 0);
939     if(rc != SR_ERR_OK) {
940         log_error("sr_set_item_str failed\n");
941         return NTS_ERR_FAILED;
942     }
943
944     //apply all changes
945     rc = sr_apply_changes(current_session, 0, 0);
946     if(rc != SR_ERR_OK) {
947         log_error("sr_apply_changes failed: %s\n", sr_strerror(rc));
948         return NTS_ERR_FAILED;
949     }
950
951     return NTS_ERR_OK;
952 }