Updated documentation for mock a1 tool
[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 *id = std::getenv("XAPP_ID")){
113     config_instance.xapp_id.assign(id);
114     mdclog_write(MDCLOG_INFO, "xAPP ID  set to Test Mode state %d from Environment Variable",     config_instance.xapp_id);
115   }
116
117   if (const char *log_env = std::getenv("LOG_LEVEL")){
118     if (!strcmp(log_env, "MDCLOG_INFO")){
119       config_instance.log_level = MDCLOG_INFO;
120     }
121     else if (!strcmp(log_env, "MDCLOG_WARN")){
122       config_instance.log_level = MDCLOG_WARN;
123     }
124     else if (!strcmp(log_env, "MDCLOG_ERR")){
125       config_instance.log_level = MDCLOG_ERR;
126     }
127     else if (!strcmp(log_env, "MDCLOG_DEBUG")){
128       config_instance.log_level = MDCLOG_DEBUG;
129     }
130     else{
131       config_instance.log_level = MDCLOG_WARN;
132       std::cerr <<"Error ! Illegal environment option for log level  ignored. Setting log level to " << config_instance.log_level << std::endl;
133     }
134   }
135   
136 }
137
138 void get_command_line_config(int argc, char **argv, configuration &config_instance){
139
140     // Parse command line options to over ride
141   static struct option long_options[] = 
142     {
143         /* Thse options require arguments */
144         {"name", required_argument, 0, 'n'},
145         {"port", required_argument, 0, 'p'},
146         {"threads", required_argument,    0, 't'},
147         {"a1-schema", required_argument, 0, 'a'},
148         {"ves-schema", required_argument, 0, 'v'},
149         {"samples", required_argument, 0, 's'},
150         {"ves-url", required_argument, 0, 'u'},
151         {"interval", required_argument, 0, 'i'},
152         {"gNodeB", required_argument, 0, 'g'},
153         {"opmode", required_argument, 0, 'c'},
154         {"xappid", required_argument, 0, 'x'},
155         {"verbose", no_argument, &config_instance.log_level, MDCLOG_DEBUG},
156         {"test", no_argument, &config_instance.test_mode, 1},
157          
158     };
159
160
161    while(1) {
162
163         int option_index = 0;
164         char c = getopt_long(argc, argv, "n:p:t:s:g:a:v:u:i:c:x:", long_options, &option_index);
165
166         if(c == -1){
167             break;
168          }
169
170         switch(c)
171           {
172
173           case 0:
174             /* An option flag was set. 
175                Do nothing for now */
176             break;
177           
178           case 'n':
179             strcpy(config_instance.name, optarg);
180             break;
181           
182           case 'p':
183             strcpy(config_instance.port, optarg);
184             break;
185           
186           case 't':
187             config_instance.num_threads = atoi(optarg);
188             mdclog_write(MDCLOG_INFO, "Number of threads set to %d from command line e\n", config_instance.num_threads);
189             break;
190
191           case 's':
192             config_instance.sample_file.assign(optarg);
193             mdclog_write(MDCLOG_INFO, "Samples JSON  file set to %s from command line ", config_instance.sample_file.c_str());     
194             break;
195
196           case 'a':
197             config_instance.a1_schema_file.assign(optarg);
198             mdclog_write(MDCLOG_INFO, "Schema file set to %s from command line ", config_instance.a1_schema_file.c_str());
199             break;
200           
201           case 'v':
202             config_instance.ves_schema_file.assign(optarg);
203             mdclog_write(MDCLOG_INFO, "VES Schema file set to %s from command line ", config_instance.ves_schema_file.c_str());
204             break;
205
206           case 'c':
207             config_instance.operating_mode.assign(optarg);
208             mdclog_write(MDCLOG_INFO, "Operating mode set from command line to %s\n", config_instance.operating_mode.c_str());
209             break;
210           
211           case 'u':
212             config_instance.ves_collector_url.assign(optarg);
213             mdclog_write(MDCLOG_INFO, "VES collector url set to %s from command line ", config_instance.ves_collector_url.c_str());
214             break;
215
216           case 'x':
217             config_instance.xapp_id.assign(optarg);
218             mdclog_write(MDCLOG_INFO, "XAPP ID set to  %s from command line ", config_instance.xapp_id.c_str());
219             break; 
220
221           case 'i':
222             config_instance.measurement_interval = atoi(optarg);
223             if (config_instance.measurement_interval < 1 || config_instance.measurement_interval > MAX_SLEEP){
224               throw std::runtime_error("Invalid measurmeent interval provided. Must between [1 and " + std::to_string(MAX_SLEEP) + "] seconds");
225             }
226             mdclog_write(MDCLOG_INFO, "Measurement interval set to %d from command line\n", config_instance.measurement_interval);
227             break;
228             
229           case 'g':
230             config_instance.fill_gnodeb_list(optarg);
231             mdclog_write(MDCLOG_INFO, "gNodeB List set to %s from command line ", optarg);
232             break;
233           
234           case 'h':
235             usage(argv[0]);
236             exit(0);
237           
238           default:
239             usage(argv[0]);
240             exit(1);
241           }
242    };
243
244 }
245
246
247 void usage(char *command){
248   std::cout <<"Usage : " << command << " " << std::endl;
249   std::cout <<" --name[-n] xapp_instance_name "<< std::endl;
250     std::cout <<" --port[-p] port to listen on e.g tcp:4561  "<< std::endl;
251     std::cout << "--threads[-t] number of listener threads "<< std::endl ;
252     std::cout << "--a1-schema[-a] a1 schema file location" << std::endl;
253     std::cout << "--ves-schema[-v] ves schema file location" << std::endl;
254     std::cout << "--samples [-s]  samples file location with samples for all jsons" << std::endl;
255     std::cout << "--ves-url [-u] ves collector url" << std::endl;
256     std::cout <<"[--gNodeB[][-g] gNodeB" << std::endl;
257     std::cout <<"--interval[-i] measurement interval to send to ves collector (in seconds)" << std::endl;
258     std::cout <<"--test puts xapp in test mode where it sends subscription, waits for interval and then sends delete subscription " << std::endl;
259     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;
260     std::cout << "--verbose " << std::endl;
261 }