76abef661fe562e64a963cf9c3a9bed97267c00b
[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/container.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/supervisor.h"
36 #include "core/app/manager.h"
37 #include "core/app/network_function.h"
38 #include "core/app/blank.h"
39 #include "core/datastore/schema.h"
40 #include "core/datastore/generate.h"
41 #include "core/datastore/populate.h"
42
43 int main(int argc, char **argv) {
44     int return_code = EXIT_SUCCESS;
45
46     if(framework_init(argc, argv) != NTS_ERR_OK) {
47         log_error(LOG_COLOR_BOLD_RED"framework_init() error\n");
48         framework_free();
49         return EXIT_FAILURE;
50     }
51
52     //common init
53     switch(framework_arguments.nts_mode) {
54         case NTS_MODE_MANAGER:
55         case NTS_MODE_NETWORK_FUNCTION:
56         case NTS_MODE_GENERATE_DATA:
57         case NTS_MODE_TEST:
58         case NTS_MODE_DEFAULT:
59             sr_log_stderr(SR_LL_INF);   //checkAL WRN
60         
61             if(session_init() != NTS_ERR_OK) {
62                 log_error("session_init() failed\n");
63                 return_code = EXIT_FAILURE;
64                 goto main_clean_session;
65             }
66
67             if(context_init(session_context) != 0) {
68                 log_error("context_init() failed\n");
69                 return_code = EXIT_FAILURE;
70                 goto main_clean_context;
71             }
72
73             nc_client_init();
74             break;
75
76         default:
77             break;
78     }
79
80     //netconf server configure
81     switch(framework_arguments.nts_mode) {
82         case NTS_MODE_MANAGER:
83         case NTS_MODE_NETWORK_FUNCTION:
84             //configure local netconf server
85             if(netconf_configure() != NTS_ERR_OK) {
86                 log_error("netconf_configure() failed\n")
87                 return_code = EXIT_FAILURE;
88                 goto main_clean;
89             }
90             break;
91
92         default:
93             break;
94     }
95
96     switch(framework_arguments.nts_mode) {
97         case NTS_MODE_CONTAINER_INIT:
98             if(!container_self_init()) {
99                 log_error("container_self_init() failed\n");
100                 return_code = EXIT_FAILURE;
101             }
102
103             goto main_clean_framework;
104             break;
105
106         case NTS_MODE_SUPERVISOR:
107             //run in supervisor mode
108             if(supervisor_run(argc, argv) != NTS_ERR_OK) {
109                 log_error("supervisor_run() failed\n");
110                 return_code = EXIT_FAILURE;
111             }
112
113             goto main_clean_framework;
114             break;
115
116         case NTS_MODE_MANAGER:
117             if(manager_run() != NTS_ERR_OK) {
118                 log_error("manager_run() failed\n");
119                 return_code = EXIT_FAILURE;
120             }
121
122             goto main_clean;
123             break;
124
125         case NTS_MODE_NETWORK_FUNCTION:
126             if(network_function_run() != NTS_ERR_OK) {
127                 log_error("network_function_run() failed\n");
128                 return_code = EXIT_FAILURE;
129             }
130
131             goto main_clean;
132             break;
133
134         case NTS_MODE_BLANK:
135             if(blank_run() != NTS_ERR_OK) {
136                 log_error("blank_run() failed\n");
137                 return_code = EXIT_FAILURE;
138             }
139
140             goto main_clean_framework;
141             break;
142
143         case NTS_MODE_GENERATE_DATA:
144             if(datastore_generate_data(DATASTORE_RUNNING_PATH, DATASTORE_OPERATIONAL_PATH) != NTS_ERR_OK) {
145                 log_error("datastore_generate_data() failed\n");
146                 return_code = EXIT_FAILURE;
147             }
148
149             goto main_clean;
150             break;
151
152         case NTS_MODE_TEST:
153             if(exhaustive_test_run() != NTS_ERR_OK) {
154                 log_error("exhaustive_test_run() failed\n");
155                 return_code = EXIT_FAILURE;
156             }
157         
158             goto main_clean;
159             break;
160
161         case NTS_MODE_DEFAULT:
162             if(framework_arguments.print_root_paths) {
163                 if(datastore_schema_print_root_paths() != NTS_ERR_OK) {
164                     log_error("datastore_schema_print_root_paths() failed\n");
165                     return_code = EXIT_FAILURE;
166                     goto main_clean;
167                 }
168             }
169             
170             if(framework_arguments.print_structure_xpath != 0) {
171                 //print the associated structure
172                 if(datastore_schema_print_xpath(framework_arguments.print_structure_xpath) != NTS_ERR_OK) {
173                     log_error("datastore_schema_print_xpath() failed\n");
174                     return_code = EXIT_FAILURE;
175                     goto main_clean;
176                 }
177             }
178
179             goto main_clean;
180             break;
181
182         default:
183             assert(0);
184             break;
185     }
186
187 main_clean:
188     log_add_verbose(1, LOG_COLOR_BOLD_RED"stopping now...\n"LOG_COLOR_RESET);
189     nc_client_destroy();
190 main_clean_context:
191     context_free();
192 main_clean_session:
193     session_free();
194 main_clean_framework:
195     framework_free();
196     return return_code;
197 }