Updated INFO.yaml file
[ric-app/kpimon.git] / src / get_config.cc
1 /*\r
2 ==================================================================================\r
3 \r
4         Copyright (c) 2018-2019 SAMSUNG and AT&T Intellectual Property.\r
5 \r
6    Licensed under the Apache License, Version 2.0 (the "License");\r
7    you may not use this file except in compliance with the License.\r
8    You may obtain a copy of the License at\r
9 \r
10        http://www.apache.org/licenses/LICENSE-2.0\r
11 \r
12    Unless required by applicable law or agreed to in writing, software\r
13    distributed under the License is distributed on an "AS IS" BASIS,\r
14    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
15    See the License for the specific language governing permissions and\r
16    limitations under the License.\r
17 ==================================================================================\r
18 */\r
19 \r
20 #include "kpi-xapp.hpp"\r
21 \r
22 void get_environment_config(configuration & config_instance){\r
23 \r
24   // Order of priority for setting variables\r
25   // So we start in reverse order\r
26   //  -- command line \r
27   // -- environment variable\r
28   // -- default path \r
29    \r
30   if (char *env_gnodeb = std::getenv("GNODEB")){\r
31     config_instance.fill_gnodeb_list(env_gnodeb);\r
32     mdclog_write(MDCLOG_INFO, "gNodeB List set to %s from environment variable", env_gnodeb);\r
33   }\r
34    \r
35   if (const char *threads = std::getenv("THREADS")){\r
36     config_instance.num_threads = atoi(threads);\r
37     if (    config_instance.num_threads <= 0 or     config_instance.num_threads  > MAX_THREADS){\r
38       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);\r
39       exit(-1);\r
40     }\r
41     else{\r
42       mdclog_write(MDCLOG_INFO, "Number of threads set to %d from environment variable\n",     config_instance.num_threads);\r
43     }\r
44   }\r
45 \r
46   if (const char *log_env = std::getenv("LOG_LEVEL")){\r
47     if (!strcmp(log_env, "MDCLOG_INFO")){\r
48       config_instance.log_level = MDCLOG_INFO;\r
49     }\r
50     else if (!strcmp(log_env, "MDCLOG_WARN")){\r
51       config_instance.log_level = MDCLOG_WARN;\r
52     }\r
53     else if (!strcmp(log_env, "MDCLOG_ERR")){\r
54       config_instance.log_level = MDCLOG_ERR;\r
55     }\r
56     else if (!strcmp(log_env, "MDCLOG_DEBUG")){\r
57       config_instance.log_level = MDCLOG_DEBUG;\r
58     }\r
59     else{\r
60       config_instance.log_level = MDCLOG_WARN;\r
61       std::cerr <<"Error ! Illegal environment option for log level  ignored. Setting log level to " << config_instance.log_level << std::endl;\r
62     }\r
63   }\r
64   \r
65 }\r
66 \r
67 void get_command_line_config(int argc, char **argv, configuration &config_instance){\r
68 \r
69     // Parse command line options to over ride\r
70   static struct option long_options[] = \r
71     {\r
72         /* Thse options require arguments */\r
73         {"name", required_argument, 0, 'n'},\r
74     {"port", required_argument, 0, 'p'},\r
75     {"redisport", required_argument,0, 'r'},\r
76         {"threads", required_argument,    0, 't'},\r
77         {"gNodeB", required_argument, 0, 'g'},\r
78         {"verbose", no_argument, &config_instance.log_level, MDCLOG_INFO},\r
79     };\r
80 \r
81 \r
82    while(1) {\r
83 \r
84         int option_index = 0;\r
85         char c = getopt_long(argc, argv, "n:p:r:t:g:", long_options, &option_index);\r
86 \r
87         if(c == -1){\r
88             break;\r
89          }\r
90 \r
91         switch(c)\r
92           {\r
93 \r
94           case 0:\r
95             /* An option flag was set. \r
96                Do nothing for now */\r
97             break;\r
98           \r
99           case 'n':\r
100             strcpy(config_instance.name, optarg);\r
101             break;\r
102           \r
103           case 'p':\r
104             strcpy(config_instance.port, optarg);\r
105             break;\r
106           \r
107           case 't':\r
108             config_instance.num_threads = atoi(optarg);\r
109             mdclog_write(MDCLOG_INFO, "Number of threads set to %d from command line e\n", config_instance.num_threads);\r
110             break;\r
111             \r
112           case 'g':\r
113             config_instance.fill_gnodeb_list(optarg);\r
114             mdclog_write(MDCLOG_INFO, "gNodeB List set to %s from command line ", optarg);\r
115             break;\r
116 \r
117       case 'r':\r
118         config_instance.redis_port = atoi(optarg);\r
119         mdclog_write(MDCLOG_INFO, "Redis Port set to %d from command line e\n", config_instance.redis_port);\r
120             break;\r
121           \r
122           case 'h':\r
123             usage(argv[0]);\r
124             exit(0);\r
125           \r
126           default:\r
127             usage(argv[0]);\r
128             exit(1);\r
129           }\r
130    };\r
131 \r
132 }\r
133 \r
134 \r
135 void usage(char *command){\r
136   std::cout <<"Usage : " << command << " " << std::endl;\r
137   std::cout <<" --name[-n] xapp_instance_name "<< std::endl;\r
138     std::cout <<" --port[-p] port to listen on e.g tcp:4561  "<< std::endl;\r
139     std::cout << "--threads[-t] number of listener threads "<< std::endl ;\r
140     std::cout <<"[--gNodeB[][-g] gNodeB" << std::endl;\r
141     std::cout <<"--redisport[-r] port to connect to redis DB eg., tcp:6379 "<<std::endl;\r
142 }\r