Rewrite NTS Framework.
[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
47 int main(int argc, char **argv) {
48     int return_code = EXIT_SUCCESS;
49
50     framework_init(argc, argv);
51
52     if(framework_arguments.container_init) {
53         if(!docker_container_init()) {
54             log_error("docker_container_init() failed");
55             return_code = EXIT_FAILURE;   
56         }
57
58         framework_free();
59         return return_code;
60     }
61     else { //not in container-init mode
62         sr_log_stderr(SR_LL_NONE);
63
64         int rc;
65         rc = session_init();
66         if(rc != 0) {
67             log_error("session_init() failed");
68             return_code = EXIT_FAILURE;
69             goto non_container_init_cleanup;
70         }
71
72         rc = context_init(session_context);
73         if(rc != 0) {
74             log_error("context_init() failed");
75             return_code = EXIT_FAILURE;
76             goto non_container_init_cleanup;
77         }
78
79         nc_client_init();
80
81         if(framework_arguments.nc_server_init) {
82             //configure local netconf server
83             rc = netconf_configure();
84             if(rc != 0) {
85                 log_error("netconf_configure() failed");
86                 return_code = EXIT_FAILURE;
87                 goto non_container_init_cleanup;
88             }
89         }
90
91         if(framework_arguments.manager) {
92             //run in manager mode
93             if(manager_run() != NTS_ERR_OK) {
94                 return_code = EXIT_FAILURE;
95                 goto non_container_init_cleanup;
96             }
97         }
98         else if(framework_arguments.network_function) {
99             //run in network function mode
100             if(network_function_run() != NTS_ERR_OK) {
101                 return_code = EXIT_FAILURE;
102                 goto non_container_init_cleanup;
103             }
104         }
105         else {
106             if(framework_arguments.test_mode) {
107                 if(test_mode_run() != NTS_ERR_OK) {
108                     return_code = EXIT_FAILURE;
109                     goto non_container_init_cleanup;
110                 }
111             }
112             else if(framework_arguments.exhaustive_test) {
113                 //exhaustive test
114                 if(exhaustive_test_run() != NTS_ERR_OK) {
115                     return_code = EXIT_FAILURE;
116                     goto non_container_init_cleanup;
117                 }
118             }
119
120             if(framework_arguments.print_root_paths) {
121                 //print all root paths with their attributes
122                 if(schema_print_root_paths() != NTS_ERR_OK) {
123                     return_code = EXIT_FAILURE;
124                     goto non_container_init_cleanup;
125                 }
126             }
127             
128             if(framework_arguments.print_structure_xpath != 0) {
129                 //print the associated structure
130                 if(schema_print_xpath(framework_arguments.print_structure_xpath) != NTS_ERR_OK) {
131                     return_code = EXIT_FAILURE;
132                     goto non_container_init_cleanup;
133                 }
134             }
135             
136             if(framework_arguments.populate_all) {
137                 // populate all
138                 if(schema_populate() != NTS_ERR_OK) {
139                     return_code = EXIT_FAILURE;
140                     goto non_container_init_cleanup;
141                 }
142             }
143
144             if(framework_arguments.enable_features) {
145                 // check if PNF registration is enabled and send PNF registration message if so
146                 rc = ves_pnf_registration_feature_start(session_running);
147                 if(rc != 0) {
148                     log_error("ves_pnf_registration_feature_start() failed");
149                     return_code = EXIT_FAILURE;
150                     goto non_container_init_cleanup;
151                 }
152
153                 // start feature for handling the heartbeat VES message
154                 rc = ves_heartbeat_feature_start(session_running);
155                 if(rc != 0) {
156                     log_error("ves_heartbeat_feature_start() failed");
157                     return_code = EXIT_FAILURE;
158                     goto non_container_init_cleanup;
159                 }
160
161                 // start feature for handling the fileReady VES message
162                 rc = ves_file_ready_feature_start(session_running);
163                 if(rc != 0) {
164                     log_error("ves_file_ready_feature_start() failed");
165                     return_code = EXIT_FAILURE;
166                     goto non_container_init_cleanup;
167                 }
168
169                 // start feature for manual notification
170                 rc = manual_notification_feature_start(session_running);
171                 if(rc != 0) {
172                     log_error("manual_notification_feature_start() failed");
173                     return_code = EXIT_FAILURE;
174                     goto non_container_init_cleanup;
175                 }
176
177                 // start feature for NETCONF Call Home
178                 rc = netconf_call_home_feature_start(session_running);
179                 if(rc != 0) {
180                     log_error("netconf_call_home_feature_start() failed");
181                     return_code = EXIT_FAILURE;
182                     goto non_container_init_cleanup;
183                 }
184             }
185
186             if(framework_arguments.loop) {
187                 while(!framework_sigint) {
188                     sleep(1);
189                 }
190             }
191         }
192
193         non_container_init_cleanup:
194         log_message(1, LOG_COLOR_BOLD_RED"\nstopping now...\n"LOG_COLOR_RESET);
195
196         nc_client_destroy();
197         context_free();
198         session_free();
199         framework_free();
200
201         return return_code;
202     }
203
204     return EXIT_FAILURE;
205 }