f9a4640c23ce49a19746f1eac7d73a948260da3c
[ric-app/hw.git] / src / xapp-utils / xapp_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  * xapp_config.cc
21  * Created on: 2019
22  * Author: Ashwin Shridharan, Shraboni Jana
23  */
24
25 #include "xapp_config.hpp"
26
27 string& XappSettings::operator[](const SettingName& theName){
28     return theSettings[theName];
29 }
30
31 void XappSettings::loadCmdlineSettings(int argc, char **argv){
32
33            // Parse command line options to over ride
34           static struct option long_options[] =
35             {
36                         {"xappname", required_argument, 0, 'n'},
37                                 {"xappid", required_argument, 0, 'x'},
38                                 {"port", required_argument, 0, 'p'},
39                                 {"threads", required_argument,    0, 't'},
40                                 {"a1-schema", required_argument, 0, 'a'},
41                                 {"ves-schema", required_argument, 0, 'v'},
42                                 {"ves-url", required_argument, 0, 'u'},
43                                 {"ves-interval", required_argument, 0, 'i'},
44                                 {"gNodeB", required_argument, 0, 'g'},
45                                 {"opmode", required_argument, 0, 'c'}
46
47             };
48
49
50            while(1) {
51
52                 int option_index = 0;
53                 char c = getopt_long(argc, argv, "n:p:t:s:g:a:v:u:i:c:x:", long_options, &option_index);
54
55                 if(c == -1){
56                     break;
57                  }
58
59                 switch(c)
60                   {
61
62                   case 'n':
63                     theSettings[XAPP_NAME].assign(optarg);
64                     break;
65
66                   case 'p':
67                     theSettings[HW_PORTS].assign(optarg);
68                     break;
69
70                   case 't':
71                         theSettings[THREADS].assign(optarg);
72                     mdclog_write(MDCLOG_INFO, "Number of threads set to %s from command line e\n", theSettings[THREADS].c_str());
73                     break;
74
75                   case 'a':
76                         theSettings[A1_SCHEMA_FILE].assign(optarg);
77                     mdclog_write(MDCLOG_INFO, "Schema file set to %s from command line ", theSettings[A1_SCHEMA_FILE].c_str());
78                     break;
79
80                   case 'v':
81                     theSettings[VES_SCHEMA_FILE].assign(optarg);
82                     mdclog_write(MDCLOG_INFO, "VES Schema file set to %s from command line ", theSettings[VES_SCHEMA_FILE].c_str());
83                     break;
84
85                   case 'c':
86                     theSettings[OPERATING_MODE].assign(optarg);
87                     mdclog_write(MDCLOG_INFO, "Operating mode set from command line to %s\n", theSettings[OPERATING_MODE].c_str());
88                     break;
89
90                   case 'u':
91                     theSettings[VES_COLLECTOR_URL].assign(optarg);
92                     mdclog_write(MDCLOG_INFO, "VES collector url set to %s from command line ", theSettings[VES_COLLECTOR_URL].c_str());
93                     break;
94
95                   case 'x':
96                     theSettings[XAPP_ID].assign(optarg);
97                     mdclog_write(MDCLOG_INFO, "XAPP ID set to  %s from command line ", theSettings[XAPP_ID].c_str());
98                     break;
99
100                   case 'i':
101                         theSettings[VES_MEASUREMENT_INTERVAL].assign(optarg);
102                     mdclog_write(MDCLOG_INFO, "Measurement interval set to %s from command line\n", theSettings[VES_MEASUREMENT_INTERVAL].c_str());
103                     break;
104
105                   case 'g':
106                     theSettings[GNODEB].assign(optarg);
107                     mdclog_write(MDCLOG_INFO, "gNodeB List set to %s from command line ", theSettings[GNODEB].c_str());
108                     break;
109
110                   case 'h':
111                     usage(argv[0]);
112                     exit(0);
113
114                   default:
115                     usage(argv[0]);
116                     exit(1);
117                   }
118            };
119
120 }
121
122 void XappSettings::loadDefaultSettings(){
123
124
125                  if(theSettings[XAPP_NAME].empty()){
126                   theSettings[XAPP_NAME] = DEFAULT_PORT;
127                   }
128
129                   if(theSettings[XAPP_ID].empty()){
130                           theSettings[XAPP_ID] = DEFAULT_PORT;
131                   }
132                   if(theSettings[LOG_LEVEL].empty()){
133                           theSettings[LOG_LEVEL] = DEFAULT_LOG_LEVEL;
134                   }
135                   if(theSettings[HW_PORTS].empty()){
136                           theSettings[HW_PORTS] = DEFAULT_PORT;
137                   }
138                   if(theSettings[MSG_MAX_BUFFER].empty()){
139                           theSettings[MSG_MAX_BUFFER] = DEFAULT_BUFFER;
140                   }
141
142                   if(theSettings[A1_SCHEMA_FILE].empty()){
143                           theSettings[A1_SCHEMA_FILE] = DEFAULT_A1_SCHEMA_FILE;
144                   }
145
146                   if(theSettings[VES_SCHEMA_FILE].empty()){
147                           theSettings[VES_SCHEMA_FILE] = DEFAULT_VES_SCHEMA_FILE;
148                   }
149
150                   if(theSettings[VES_COLLECTOR_URL].empty()){
151                           theSettings[VES_COLLECTOR_URL] = DEFAULT_VES_COLLECTOR_URL;
152                   }
153
154                  if(theSettings[VES_MEASUREMENT_INTERVAL].empty()){
155                           theSettings[VES_MEASUREMENT_INTERVAL] = DEFAULT_VES_MEASUREMENT_INTERVAL;
156                   }
157
158                  if(theSettings[GNODEB].empty()){
159                           theSettings[GNODEB] = DEFAULT_GNODEB;
160                   }
161
162                  if(theSettings[OPERATING_MODE].empty()){
163                           theSettings[OPERATING_MODE] = DEFAULT_OPERATING_MODE;
164                   }
165
166 }
167
168 void XappSettings::loadEnvVarSettings(){
169
170           if (const char *env_xname = std::getenv("XAPP_NAME")){
171                   theSettings[XAPP_NAME].assign(env_xname);
172                   mdclog_write(MDCLOG_INFO,"Xapp Name set to %s from environment variable", theSettings[XAPP_NAME].c_str());
173           }
174           if (const char *env_xid = std::getenv("XAPP_ID")){
175                    theSettings[XAPP_ID].assign(env_xid);
176                    mdclog_write(MDCLOG_INFO,"Xapp ID set to %s from environment variable", theSettings[XAPP_ID].c_str());
177           }
178
179           if (const char *env_ports = std::getenv("HW_PORTS")){
180                   theSettings[HW_PORTS].assign(env_ports);
181                   mdclog_write(MDCLOG_INFO,"Ports set to %s from environment variable", theSettings[HW_PORTS].c_str());
182           }
183           if (const char *env_ports = std::getenv("MSG_MAX_BUFFER")){
184                           theSettings[MSG_MAX_BUFFER].assign(env_ports);
185                           mdclog_write(MDCLOG_INFO,"Ports set to %s from environment variable", theSettings[MSG_MAX_BUFFER].c_str());
186                   }
187
188          if (const char *env_schema = std::getenv("A1_SCHEMA_FILE")){
189                   theSettings[A1_SCHEMA_FILE].assign(env_schema);
190                   mdclog_write(MDCLOG_INFO, "A1 Schema file set to %s from environment variable", theSettings[A1_SCHEMA_FILE].c_str());
191           }
192           if (const char *env_schema = std::getenv("VES_SCHEMA_FILE")){
193                   theSettings[VES_SCHEMA_FILE].assign(env_schema);
194                   mdclog_write(MDCLOG_INFO, "VES Schema file set to %s from environment variable", theSettings[VES_SCHEMA_FILE].c_str());
195           }
196           if (const char *env_schema = std::getenv("VES_COLLECTOR_URL")){
197                   theSettings[VES_COLLECTOR_URL].assign(env_schema);
198                   mdclog_write(MDCLOG_INFO, "VES Collector url set to %s from environment variable", theSettings[VES_COLLECTOR_URL].c_str());
199
200           }
201           if (const char *env_schema = std::getenv("VES_MEASUREMENT_INTERVAL")){
202                   theSettings[VES_MEASUREMENT_INTERVAL].assign(env_schema);
203                   mdclog_write(MDCLOG_INFO, "VES Measurement Interval set to %s from environment variable", theSettings[VES_MEASUREMENT_INTERVAL].c_str());
204           }
205
206           if (char *env_gnodeb = std::getenv("GNODEB")){
207                   theSettings[GNODEB].assign(env_gnodeb);
208                   mdclog_write(MDCLOG_INFO, "GNODEB file set to %s from environment variable", theSettings[GNODEB].c_str());
209           }
210
211
212 }
213
214 void XappSettings::usage(char *command){
215         std::cout <<"Usage : " << command << " " << std::endl;
216         std::cout <<" --name[-n] xapp_instance_name "<< std::endl;
217     std::cout <<" --port[-p] port to listen on e.g tcp:4561  "<< std::endl;
218     std::cout << "--threads[-t] number of listener threads "<< std::endl ;
219     std::cout << "--a1-schema[-a] a1 schema file location" << std::endl;
220     std::cout << "--ves-schema[-v] ves schema file location" << std::endl;
221     std::cout << "--samples [-s]  samples file location with samples for all jsons" << std::endl;
222     std::cout << "--ves-url [-u] ves collector url" << std::endl;
223     std::cout << "--gNodeB[][-g] gNodeB" << std::endl;
224     std::cout << "--interval[-i] measurement interval to send to ves collector (in seconds)" << std::endl;
225     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;
226 }