Add blank type of NTS.
[sim/o1-interface.git] / ntsimulator / ntsim-ng / core / app / blank.c
1 /*************************************************************************
2 *
3 * Copyright 2021 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 "blank.h"
21 #include "utils/log_utils.h"
22 #include "utils/sys_utils.h"
23 #include "utils/nts_utils.h"
24 #include <stdio.h>
25 #include <assert.h>
26
27 #include "core/framework.h"
28 #include "core/container.h"
29 #include "core/session.h"
30 #include "core/nc_config.h"
31
32 #define DOCKER_DEPLOY_ZIP   "/opt/dev/deploy.zip"
33 #define FTP_DEPLOY_ZIP      "/ftp/deploy.zip"
34
35 //checkAL: see all todos if doing also YANG-install through netopeer
36
37 int blank_run(void) {
38     log_add_verbose(1, LOG_COLOR_BOLD_YELLOW"running as BLANK NTS daemon...\n"LOG_COLOR_RESET);
39     log_add_verbose(1, LOG_COLOR_BOLD_YELLOW"Docker IP:"LOG_COLOR_RESET" %s\n", framework_environment.settings.ip_v6_enabled ? framework_environment.settings.ip_v6 : framework_environment.settings.ip_v4);
40
41     char shell_command[512];
42
43     if(session_init() != NTS_ERR_OK) {
44         log_error("session_init() failed\n");
45         return NTS_ERR_FAILED;
46     }
47
48     if(netconf_configure() != NTS_ERR_OK) {
49         log_error("netconf_configure() failed\n")
50         return NTS_ERR_FAILED;
51     }
52
53     session_free();
54
55     vsftp_daemon_init();
56     sftp_daemon_init();
57
58     //todo: run netopeer
59
60     char *zipfile = DOCKER_DEPLOY_ZIP;
61     if(file_exists(zipfile)) {
62         goto blank_install_zip;
63     }
64     else {
65         log_add_verbose(2, "%s was not found, waiting for deploy.zip on other means...\n");
66     }
67
68 blank_run_wait_for_file:
69     while(!framework_sigint) {
70         zipfile = FTP_DEPLOY_ZIP;
71         if(file_exists(zipfile)) {
72             goto blank_install_zip;
73         }
74
75         //todo: check netopeer for data
76
77         sleep(1);
78     }
79
80     vsftp_daemon_deinit();
81     sftp_daemon_deinit();
82
83     log_error("blank image daemon was called to stop before installing anything\n");
84     return NTS_ERR_FAILED;
85
86
87 blank_install_zip: {
88         log_add_verbose(1, "found deploy.zip in "LOG_COLOR_BOLD_BLUE"%s"LOG_COLOR_RESET"\n", zipfile);
89         log_add_verbose(1, "starting install...\n");
90         
91         //check if zip is ok
92         sprintf(shell_command, "unzip -qq -t %s", zipfile);
93         if(system(shell_command) != 0) {
94             log_error("%s invalid ZIP file\n", zipfile);
95             goto blank_install_failed;
96         }
97         
98         //unzip to /opt/dev/deploy
99         sprintf(shell_command, "unzip -qq %s -d /opt/dev/deploy", zipfile);
100         if(system(shell_command) != 0) {
101             log_error("unzip filed for unknown reason\n", zipfile);
102             goto blank_install_failed;
103         }
104
105         if(strcmp(zipfile, FTP_DEPLOY_ZIP) == 0) {
106             sprintf(shell_command, "rm -f %s", zipfile);
107             if(system(shell_command) != 0) {
108                 log_error("failed to remove %s\n", zipfile);
109             }
110         }
111
112         if(!file_exists("/opt/dev/deploy/config.json")) {
113             log_error("/opt/dev/deploy/config.json not found!\n");
114             goto blank_install_failed;
115         }
116
117         //move /opt/dev/deploy/config.json to /opt/dev/ntsim-ng/config/config.json
118         system("mv /opt/dev/deploy/config.json /opt/dev/ntsim-ng/config/config.json");
119
120         //todo: kill netopeer
121
122         //run container_self_init()
123         if(!container_self_init()) {
124             log_error("container_self_init() error\n");
125             goto blank_install_failed;
126         }
127
128         //send SIGUSR1 to supervisor to reload everything and start fresh
129         kill(1, SIGUSR1);
130
131         log_add_verbose(1, LOG_COLOR_BOLD_GREEN"blank image successfully replaced!"LOG_COLOR_RESET"\n");
132         vsftp_daemon_deinit();
133         sftp_daemon_deinit();
134
135         return NTS_ERR_OK;
136     }
137
138 blank_install_failed: {
139         //remove zipfile
140         sprintf(shell_command, "rm -rf %s", zipfile);
141         system(shell_command);
142
143         system("rm -rf /opt/dev/deploy");
144
145         log_error("%s failed to install...\n", zipfile);
146         log_error("try again with new file...\n");
147         goto blank_run_wait_for_file;
148     }
149 }