OCG.c 8.25 KB
Newer Older
1
/*******************************************************************************
2 3
    OpenAirInterface
    Copyright(c) 1999 - 2014 Eurecom
4

5 6 7 8
    OpenAirInterface is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.
9 10


11 12 13 14
    OpenAirInterface is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.
15

16 17 18 19
    You should have received a copy of the GNU General Public License
    along with OpenAirInterface.The full GNU General Public License is
    included in this distribution in the file called "COPYING". If not,
    see <http://www.gnu.org/licenses/>.
20 21

  Contact Information
22 23 24 25 26
  OpenAirInterface Admin: openair_admin@eurecom.fr
  OpenAirInterface Tech : openair_tech@eurecom.fr
  OpenAirInterface Dev  : openair4g-devel@eurecom.fr

  Address      : Eurecom, Compus SophiaTech 450, route des chappes, 06451 Biot, France.
27 28 29 30 31

*******************************************************************************/

/*! \file OCG.c
* \brief Main function containing the FSM of OCG
32
* \author Navid Nikaein and Lusheng Wang
33 34 35
* \date 2011
* \version 0.1
* \company Eurecom
36
* \email: navid.nikaein@eurecom.fr 
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250
* \note
* \warning
*/

/*--- INCLUDES ---------------------------------------------------------------*/
#include <libxml/tree.h>
#include <libxml/parser.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <math.h>
#include "OCG.h"
#include "OCG_vars.h"
#include "OCG_detect_file.h"
#include "OCG_parse_filename.h"
#include "OCG_create_dir.h"
#include "OCG_parse_XML.h"
#include "OCG_save_XML.h"
#include "OCG_generate_report.h"
#include "UTIL/LOG/log.h"
/*----------------------------------------------------------------------------*/
//#define TEST_OCG



#ifdef TEST_OCG 
int main(int argc, char *argv[]) {
#else
int OCG_main(char is_local_server[FILENAME_LENGTH_MAX]) { 
#endif  
	int state = STATE_START_OCG;
	char web_XML_folder[DIR_LENGTH_MAX] = "";
	char output_dir[DIR_LENGTH_MAX] = ""; // the output folder during the OCG procedure. Change step by step

	char *OPENAIR_TARGETS=getenv("OPENAIR_TARGETS");
	if (OPENAIR_TARGETS == NULL) {
		LOG_E(OCG, "please set the PATH for OPENAIR_TARGETS");
		exit(EXIT_FAILURE);
	}

	if (!strcmp(is_local_server, "0")) { // Eurecom web server
		strcpy(web_XML_folder, WEB_XML_FOLDER);
		strcat(output_dir, OUTPUT_DIR);
	} else { // local user
		
	  sprintf(web_XML_folder,"%s/SIMU/EXAMPLES/OSD/WEBXML/",getenv("OPENAIR_TARGETS"));
	  sprintf(output_dir,"%s/SIMU/EXAMPLES/OSD/RESULTS/",getenv("OPENAIR_TARGETS"));
	  /*
	  strcpy(web_XML_folder, OPENAIR_TARGETS);
		strcpy(output_dir, OPENAIR_TARGETS);
		
		char *slash;
		slash = web_XML_folder + strlen(web_XML_folder) - 1;
		if (strcmp(slash, "/")) { // check if the path OPENAIR_TARGETS is ended with a '/'
			strcat(web_XML_folder, "/");
			strcat(output_dir, "/");
		}
		strcat(web_XML_folder, "SIMU/EXAMPLES/OSD/WEBXML/");
		strcat(output_dir, "SIMU/EXAMPLES/OSD/RESULTS/");
	  */ }

	LOG_I(OCG, "Folder for detecting the XML configuration file is %s\n", web_XML_folder);
	LOG_I(OCG, "Folder for generating the results is %s\n", output_dir);
	LOG_I(OCG, "OSD XML config file is %s\n", is_local_server);


	while(state != STATE_END) {
	
		switch(state) {
			
			case STATE_START_OCG :
				LOG_I(OCG, "OCG starts ...\n\n");
				get_opt_OK = MODULE_NOT_PROCESSED; // -9999 is the initial value, representing the module not processed
				detect_file_OK = MODULE_NOT_PROCESSED;
				create_dir_OK = MODULE_NOT_PROCESSED;

				// to write the pid into a file 'OCG.pid' so that the web portal part could check if OCG is running
				/*
				pid_t pid;
				pid = getpid();
				FILE *OCG_pid;
				OCG_pid = fopen("OCG.pid", "w");
				fprintf(OCG_pid, "%d\n", pid);
				fclose(OCG_pid);
				*/
#ifdef TEST_OCG
				state = STATE_GET_OPT;

			case STATE_GET_OPT :
				get_opt_OK = get_opt(argc, argv);
				if (get_opt_OK == MODULE_OK) {
					strcpy(src_file, LOCAL_XML_FOLDER);
					copy_or_move = 1;
					state = STATE_INI_EMU;
				} else if (get_opt_OK == GET_HELP) state = STATE_END;
				else state = STATE_DETECT_FILE;
				break;
#else
				state = STATE_DETECT_FILE;
				//get_opt_OK == MODULE_OK;
#endif

			case STATE_DETECT_FILE :
				strcpy(src_file, web_XML_folder);
				detect_file_OK = detect_file(src_file, is_local_server);
				if (detect_file_OK == MODULE_OK) {
					if ((!strcmp(is_local_server, "0")) || (!strcmp(is_local_server, "-1"))) copy_or_move = 2;
					state = STATE_INI_EMU;
				} else if (detect_file_OK == MODULE_ERROR) state = STATE_GENERATE_REPORT;
				else if (detect_file_OK == NO_FILE) {
					state = STATE_DETECT_FILE;
					sleep(1);
				}
				break;
				
			case STATE_INI_EMU : // before initiating an emu, an XML file should be found above 
				parse_filename_OK = MODULE_NOT_PROCESSED;
				create_dir_OK = MODULE_NOT_PROCESSED;
				parse_XML_OK = MODULE_NOT_PROCESSED;
				save_XML_OK = MODULE_NOT_PROCESSED;
//				call_emu_OK = MODULE_NOT_PROCESSED;
				//config_mobi_OK = MODULE_NOT_PROCESSED;
				generate_report_OK = MODULE_NOT_PROCESSED;

				LOG_I(OCG, "An emulation for file %s is initiated\n", filename);
				state = STATE_PARSE_FILENAME;
				break;

			case STATE_PARSE_FILENAME :
				strcat(src_file, filename);
				if ((parse_filename_OK = parse_filename(filename)) == MODULE_OK) state = STATE_CREATE_DIR;
				else {
					if (copy_or_move == 2) {
						remove(src_file);
						state = STATE_DETECT_FILE;
					} else state = STATE_GENERATE_REPORT;
				}
				break;
				
			case STATE_CREATE_DIR :
				if ((create_dir_OK = create_dir(output_dir, user_name, file_date)) == MODULE_OK) {
					state = STATE_PARSE_XML;
					strcat(output_dir, user_name);
					strcat(output_dir, "/");
					strcat(output_dir, file_date);
					strcat(output_dir, "/");
					strcpy(dst_dir, output_dir);
					oai_emulation.info.output_path = &dst_dir; // information for other modules within OAI
				} else state = STATE_GENERATE_REPORT;
				break;
				
			case STATE_PARSE_XML :
				if ((parse_XML_OK = parse_XML(src_file)) == MODULE_OK) state = STATE_SAVE_XML;
				else {
					if (copy_or_move == 2) remove(src_file);
					state = STATE_GENERATE_REPORT;
				}
				break;
				
			case STATE_SAVE_XML :
			  if ((save_XML_OK = save_XML(copy_or_move, src_file, output_dir, filename)) == MODULE_OK) 
				state = STATE_CALL_EMU;
			  else state = STATE_GENERATE_REPORT;
			  break;
			  
		        case STATE_CALL_EMU : // change this state to set_params

				if ((detect_file_OK == MODULE_OK) && (parse_filename_OK == MODULE_OK) && (create_dir_OK == MODULE_OK) && (parse_XML_OK == MODULE_OK) && (save_XML_OK == MODULE_OK)) { 
				// if the above tasks are all successful, we could tell the oaisim_config.c that everything is ready before running the emulation
					oai_emulation.info.ocg_ok = 1;
				}
							
#ifdef TEST_OCG				
				call_emu_OK = call_emu(output_dir);
				// config_mobi_OK = config_mobi("RWMEmulator.xml", filename); // generate the XML for Mobigen
#endif
				state = STATE_GENERATE_REPORT;
				break;
				
			case STATE_GENERATE_REPORT :
				if (create_dir_OK != MODULE_OK) {
					// a temp folder is required when the output folder could not be correctly generated
					strcpy(output_dir, OPENAIR_TARGETS);
					strcat(output_dir, "SIMU/EXAMPLES/OSD/");
					strcat(output_dir, TEMP_OUTPUT_DIR);
					struct stat st;
					if(stat(output_dir, &st) != 0) { // if temp output directory does not exist, we create it here
						mkdir(output_dir, S_IRWXU | S_IRWXG | S_IRWXO);
						LOG_I(OCG, "temp output directory %s is created", output_dir);
					}
				} else {
					//strcat(output_dir, "SCENARIO/STATE/"); // if needeed the outpur directory can be further set
					LOG_I(OCG, "Output directory is %s \n", output_dir);
				}
				generate_report(output_dir, "OCG_report.xml");
				if (copy_or_move == 1) state = STATE_END;
				else state = STATE_END;
				//else state = STATE_DETECT_FILE;
				break;
		}
	} // end while
	// Cleanup function for the XML library.
	xmlCleanupParser();
	// this is to debug memory for regression tests
	xmlMemoryDump();
	LOG_I(OCG, "... OCG ends\n\n");
#ifdef TEST_OCG
	return 0;
#else
	return 1;
#endif
}