1. Transitioned to using latest asn1c compiler
[ric-app/admin.git] / src / get_config.cc
1 /*
2 ==================================================================================
3
4         Copyright (c) 2018-2019 AT&T Intellectual Property.
5
6    Licensed under the Apache License, Version 2.0 (the "License");
7    you may not use this file except in compliance with the License.
8    You may obtain a copy of the License at
9
10        http://www.apache.org/licenses/LICENSE-2.0
11
12    Unless required by applicable law or agreed to in writing, software
13    distributed under the License is distributed on an "AS IS" BASIS,
14    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15    See the License for the specific language governing permissions and
16    limitations under the License.
17 ==================================================================================
18 */
19
20 #include "adm-ctrl-xapp.hpp"
21
22 void get_environment_config(configuration & config_instance){
23
24   // Order of priority for setting variables
25   // So we start in reverse order
26   //  -- command line 
27   // -- environment variable
28   // -- default path 
29
30   if (const char *env_schema = std::getenv("A1_SCHEMA_FILE")){
31     config_instance.a1_schema_file.assign(env_schema);
32     mdclog_write(MDCLOG_INFO, "Schema file set to %s from environment variable",     config_instance.a1_schema_file.c_str());
33   }
34   else{
35     config_instance.a1_schema_file.assign(DEFAULT_A1_SCHEMA_FILE);
36     mdclog_write(MDCLOG_INFO, "Using default schema file %s\n", config_instance.a1_schema_file.c_str());
37   }
38  
39   if (const char *env_schema = std::getenv("VES_SCHEMA_FILE")){
40     config_instance.ves_schema_file.assign(env_schema);
41     mdclog_write(MDCLOG_INFO, "VES Schema file set to %s from environment variable",     config_instance.ves_schema_file.c_str());
42   }
43   else{
44     config_instance.ves_schema_file.assign(DEFAULT_VES_SCHEMA_FILE);
45     mdclog_write(MDCLOG_INFO, "Using default ves schema file %s\n",     config_instance.ves_schema_file.c_str());
46   }
47    
48    
49   if (const char *env_schema = std::getenv("SAMPLES_FILE")){
50     config_instance.sample_file.assign(env_schema);
51     mdclog_write(MDCLOG_INFO, "JSON Sample file set to %s from environment variable",     config_instance.sample_file.c_str());
52   }
53   else{
54     config_instance.sample_file.assign(DEFAULT_SAMPLE_FILE);
55     mdclog_write(MDCLOG_INFO, "Using default sample file %s\n",     config_instance.sample_file.c_str());
56   }
57
58       
59   if (const char *env_schema = std::getenv("VES_COLLECTOR_URL")){
60     config_instance.ves_collector_url.assign(env_schema);
61     mdclog_write(MDCLOG_INFO, "VES Collector URL  set to %s from environment variable",     config_instance.ves_collector_url.c_str());
62   }
63   else{
64     config_instance.ves_collector_url.assign(DEFAULT_VES_COLLECTOR_URL);
65     mdclog_write(MDCLOG_INFO, "Using default ves collector url  %s\n",     config_instance.ves_collector_url.c_str());
66   }
67
68    
69   if (const char *env_schema = std::getenv("VES_MEASUREMENT_INTERVAL")){
70     config_instance.measurement_interval = atoi(env_schema);
71     if (    config_instance.measurement_interval < 1 ||     config_instance.measurement_interval > MAX_SLEEP){
72       throw std::runtime_error("Invalid measurmeent interval provided. Must between [1 and " + std::to_string(MAX_SLEEP) + "] seconds");
73        
74     }
75     mdclog_write(MDCLOG_INFO, "Interval set to %d from environment variable",     config_instance.measurement_interval);
76   }
77   else{
78     config_instance.measurement_interval = DEFAULT_VES_MEASUREMENT_INTERVAL;
79     mdclog_write(MDCLOG_INFO, "Using default measurement interval %d\n",     config_instance.measurement_interval);
80   }
81
82
83    
84   if (char *env_gnodeb = std::getenv("GNODEB")){
85     config_instance.fill_gnodeb_list(env_gnodeb);
86     mdclog_write(MDCLOG_INFO, "gNodeB List set to %s from environment variable", env_gnodeb);
87   }
88
89
90   if (const char *env_opmode = std::getenv("OPERATING_MODE")){
91     config_instance.operating_mode.assign(env_opmode);
92     mdclog_write(MDCLOG_INFO, "Operating mode set from environment variable to %s\n",     config_instance.operating_mode.c_str());
93   }
94
95    
96   if (const char *threads = std::getenv("THREADS")){
97     config_instance.num_threads = atoi(threads);
98     if (    config_instance.num_threads <= 0 or     config_instance.num_threads  > MAX_THREADS){
99       mdclog_write(MDCLOG_ERR, "Error :: %s, %d :: Must specify numnber of threads between [1 and %d]. Specified = %d\n", __FILE__, __LINE__, MAX_THREADS,     config_instance.num_threads);
100       exit(-1);
101     }
102     else{
103       mdclog_write(MDCLOG_INFO, "Number of threads set to %d from environment variable\n",     config_instance.num_threads);
104     }
105   }
106
107   if (const char *test= std::getenv("TEST_MODE")){
108     config_instance.test_mode = atoi(test);
109     mdclog_write(MDCLOG_INFO, "xAPP set to Test Mode state %d from Environment Variable",     config_instance.test_mode);
110   }
111
112   if (const char *log_env = std::getenv("LOG_LEVEL")){
113     if (!strcmp(log_env, "MDCLOG_INFO")){
114       config_instance.log_level = MDCLOG_INFO;
115     }
116     else if (!strcmp(log_env, "MDCLOG_WARN")){
117       config_instance.log_level = MDCLOG_WARN;
118     }
119     else if (!strcmp(log_env, "MDCLOG_ERR")){
120       config_instance.log_level = MDCLOG_ERR;
121     }
122     else if (!strcmp(log_env, "MDCLOG_DEBUG")){
123       config_instance.log_level = MDCLOG_DEBUG;
124     }
125     else{
126       config_instance.log_level = MDCLOG_WARN;
127       std::cerr <<"Error ! Illegal environment option for log level  ignored. Setting log level to " << config_instance.log_level << std::endl;
128     }
129   }
130   
131 }
132
133 void get_command_line_config(int argc, char **argv, configuration &config_instance){
134
135     // Parse command line options to over ride
136   static struct option long_options[] = 
137     {
138         /* Thse options require arguments */
139         {"name", required_argument, 0, 'n'},
140         {"port", required_argument, 0, 'p'},
141         {"threads", required_argument,    0, 't'},
142         {"a1-schema", required_argument, 0, 'a'},
143         {"ves-schema", required_argument, 0, 'v'},
144         {"samples", required_argument, 0, 's'},
145         {"ves-url", required_argument, 0, 'u'},
146         {"interval", required_argument, 0, 'i'},
147         {"gNodeB", required_argument, 0, 'g'},
148         {"opmode", required_argument, 0, 'c'},
149         {"verbose", no_argument, &config_instance.log_level, MDCLOG_DEBUG},
150         {"test", no_argument, &config_instance.test_mode, 1},
151          
152     };
153
154
155    while(1) {
156
157         int option_index = 0;
158         char c = getopt_long(argc, argv, "n:p:t:s:g:a:v:u:i:c:", long_options, &option_index);
159
160         if(c == -1){
161             break;
162          }
163
164         switch(c)
165           {
166
167           case 0:
168             /* An option flag was set. 
169                Do nothing for now */
170             break;
171           
172           case 'n':
173             strcpy(config_instance.name, optarg);
174             break;
175           
176           case 'p':
177             strcpy(config_instance.port, optarg);
178             break;
179           
180           case 't':
181             config_instance.num_threads = atoi(optarg);
182             mdclog_write(MDCLOG_INFO, "Number of threads set to %d from command line e\n", config_instance.num_threads);
183             break;
184
185           case 's':
186             config_instance.sample_file.assign(optarg);
187             mdclog_write(MDCLOG_INFO, "Samples JSON  file set to %s from command line ", config_instance.sample_file.c_str());     
188             break;
189
190           case 'a':
191             config_instance.a1_schema_file.assign(optarg);
192             mdclog_write(MDCLOG_INFO, "Schema file set to %s from command line ", config_instance.a1_schema_file.c_str());
193             break;
194           
195           case 'v':
196             config_instance.ves_schema_file.assign(optarg);
197             mdclog_write(MDCLOG_INFO, "VES Schema file set to %s from command line ", config_instance.ves_schema_file.c_str());
198             break;
199
200           case 'c':
201             config_instance.operating_mode.assign(optarg);
202             mdclog_write(MDCLOG_INFO, "Operating mode set from command line to %s\n", config_instance.operating_mode.c_str());
203             break;
204           
205           case 'u':
206             config_instance.ves_collector_url.assign(optarg);
207             mdclog_write(MDCLOG_INFO, "VES collector url set to %s from command line ", config_instance.ves_collector_url.c_str());
208             break;
209
210           case 'i':
211             config_instance.measurement_interval = atoi(optarg);
212             if (config_instance.measurement_interval < 1 || config_instance.measurement_interval > MAX_SLEEP){
213               throw std::runtime_error("Invalid measurmeent interval provided. Must between [1 and " + std::to_string(MAX_SLEEP) + "] seconds");
214             }
215             mdclog_write(MDCLOG_INFO, "Measurement interval set to %d from command line\n", config_instance.measurement_interval);
216             break;
217             
218           case 'g':
219             config_instance.fill_gnodeb_list(optarg);
220             mdclog_write(MDCLOG_INFO, "gNodeB List set to %s from command line ", optarg);
221             break;
222           
223           case 'h':
224             usage(argv[0]);
225             exit(0);
226           
227           default:
228             usage(argv[0]);
229             exit(1);
230           }
231    };
232
233 }
234
235
236 void usage(char *command){
237   std::cout <<"Usage : " << command << " " << std::endl;
238   std::cout <<" --name[-n] xapp_instance_name "<< std::endl;
239     std::cout <<" --port[-p] port to listen on e.g tcp:4561  "<< std::endl;
240     std::cout << "--threads[-t] number of listener threads "<< std::endl ;
241     std::cout << "--a1-schema[-a] a1 schema file location" << std::endl;
242     std::cout << "--ves-schema[-v] ves schema file location" << std::endl;
243     std::cout << "--samples [-s]  samples file location with samples for all jsons" << std::endl;
244     std::cout << "--ves-url [-u] ves collector url" << std::endl;
245     std::cout <<"[--gNodeB[][-g] gNodeB" << std::endl;
246     std::cout <<"--interval[-i] measurement interval to send to ves collector (in seconds)" << std::endl;
247     std::cout <<"--test puts xapp in test mode where it sends subscription, waits for interval and then sends delete subscription " << std::endl;
248     std::cout <<"--opmode [-c] type of operatoring mode : either REPORT or CONTROL. In REPORT, does not send a control message back to gNodeB" << std::endl;
249     std::cout << "--verbose " << std::endl;
250 }