61fc129e14074566cdbec3f98040370e17277519
[sim/o1-interface.git] / ntsimulator / ntsim-ng / main.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 <stdio.h>
21 #include <stddef.h>
22 #include <string.h>
23 #include <assert.h>
24
25 #include "utils/log_utils.h"
26 #include "utils/nc_client.h"
27
28 #include "core/framework.h"
29 #include "core/docker.h"
30 #include "core/session.h"
31 #include "core/context.h"
32 #include "core/test.h"
33 #include "core/nc_config.h"
34
35 #include "core/app/manager.h"
36 #include "core/app/network_function.h"
37 #include "core/datastore/schema.h"
38 #include "core/datastore/populate.h"
39 #include "core/faults/faults.h"
40
41 #include "features/ves_pnf_registration/ves_pnf_registration.h"
42 #include "features/ves_heartbeat/ves_heartbeat.h"
43 #include "features/ves_file_ready/ves_file_ready.h"
44 #include "features/manual_notification/manual_notification.h"
45 #include "features/netconf_call_home/netconf_call_home.h"
46 #include "features/web_cut_through/web_cut_through.h"
47
48 int main(int argc, char **argv) {
49     int return_code = EXIT_SUCCESS;
50
51     framework_init(argc, argv);
52
53     if(framework_arguments.container_init) {
54         if(!docker_container_init()) {
55             log_error("docker_container_init() failed");
56             return_code = EXIT_FAILURE;   
57         }
58
59         framework_free();
60         return return_code;
61     }
62     else { //not in container-init mode
63         sr_log_stderr(SR_LL_NONE);
64
65         int rc;
66         rc = session_init();
67         if(rc != 0) {
68             log_error("session_init() failed");
69             return_code = EXIT_FAILURE;
70             goto non_container_init_cleanup;
71         }
72
73         rc = context_init(session_context);
74         if(rc != 0) {
75             log_error("context_init() failed");
76             return_code = EXIT_FAILURE;
77             goto non_container_init_cleanup;
78         }
79
80         nc_client_init();
81
82         if(framework_arguments.nc_server_init) {
83             //configure local netconf server
84             rc = netconf_configure();
85             if(rc != 0) {
86                 log_error("netconf_configure() failed");
87                 return_code = EXIT_FAILURE;
88                 goto non_container_init_cleanup;
89             }
90         }
91
92         if(framework_arguments.manager) {
93             //run in manager mode
94             if(manager_run() != NTS_ERR_OK) {
95                 return_code = EXIT_FAILURE;
96                 goto non_container_init_cleanup;
97             }
98         }
99         else if(framework_arguments.network_function) {
100             //run in network function mode
101             if(network_function_run() != NTS_ERR_OK) {
102                 return_code = EXIT_FAILURE;
103                 goto non_container_init_cleanup;
104             }
105         }
106         else {
107             if(framework_arguments.test_mode) {
108                 if(test_mode_run() != NTS_ERR_OK) {
109                     return_code = EXIT_FAILURE;
110                     goto non_container_init_cleanup;
111                 }
112             }
113             else if(framework_arguments.exhaustive_test) {
114                 //exhaustive test
115                 if(exhaustive_test_run() != NTS_ERR_OK) {
116                     return_code = EXIT_FAILURE;
117                     goto non_container_init_cleanup;
118                 }
119             }
120
121             if(framework_arguments.print_root_paths) {
122                 //print all root paths with their attributes
123                 if(schema_print_root_paths() != NTS_ERR_OK) {
124                     return_code = EXIT_FAILURE;
125                     goto non_container_init_cleanup;
126                 }
127             }
128             
129             if(framework_arguments.print_structure_xpath != 0) {
130                 //print the associated structure
131                 if(schema_print_xpath(framework_arguments.print_structure_xpath) != NTS_ERR_OK) {
132                     return_code = EXIT_FAILURE;
133                     goto non_container_init_cleanup;
134                 }
135             }
136             
137             if(framework_arguments.populate_all) {
138                 // populate all
139                 if(schema_populate() != NTS_ERR_OK) {
140                     return_code = EXIT_FAILURE;
141                     goto non_container_init_cleanup;
142                 }
143             }
144
145             if(framework_arguments.enable_features) {
146                 // check if PNF registration is enabled and send PNF registration message if so
147                 rc = ves_pnf_registration_feature_start(session_running);
148                 if(rc != 0) {
149                     log_error("ves_pnf_registration_feature_start() failed");
150                     return_code = EXIT_FAILURE;
151                     goto non_container_init_cleanup;
152                 }
153
154                 // start feature for handling the heartbeat VES message
155                 rc = ves_heartbeat_feature_start(session_running);
156                 if(rc != 0) {
157                     log_error("ves_heartbeat_feature_start() failed");
158                     return_code = EXIT_FAILURE;
159                     goto non_container_init_cleanup;
160                 }
161
162                 // start feature for handling the fileReady VES message
163                 rc = ves_file_ready_feature_start(session_running);
164                 if(rc != 0) {
165                     log_error("ves_file_ready_feature_start() failed");
166                     return_code = EXIT_FAILURE;
167                     goto non_container_init_cleanup;
168                 }
169
170                 // start feature for manual notification
171                 rc = manual_notification_feature_start(session_running);
172                 if(rc != 0) {
173                     log_error("manual_notification_feature_start() failed");
174                     return_code = EXIT_FAILURE;
175                     goto non_container_init_cleanup;
176                 }
177
178                 // start feature for NETCONF Call Home
179                 rc = netconf_call_home_feature_start(session_running);
180                 if(rc != 0) {
181                     log_error("netconf_call_home_feature_start() failed");
182                     return_code = EXIT_FAILURE;
183                     goto non_container_init_cleanup;
184                 }
185
186                 // start feature for web cut through
187                 rc = web_cut_through_feature_start(session_running);
188                 if(rc != 0) {
189                     log_error("web_cut_through_feature_start() failed");
190                     return_code = EXIT_FAILURE;
191                     goto non_container_init_cleanup;
192                 }
193             }
194
195             if(framework_arguments.loop) {
196                 while(!framework_sigint) {
197                     sleep(1);
198                 }
199             }
200         }
201
202         non_container_init_cleanup:
203         log_message(1, LOG_COLOR_BOLD_RED"\nstopping now...\n"LOG_COLOR_RESET);
204
205         nc_client_destroy();
206         context_free();
207         session_free();
208         framework_free();
209
210         return return_code;
211     }
212
213     return EXIT_FAILURE;
214 }