b4a1bb2416f04613a57b9971f4d3bd47649bef12
[ric-app/bouncer.git] / Bouncer / src / xapp-utils / xapp_config.cc
1 /*
2 # ==================================================================================
3 # Copyright (c) 2020 HCL Technologies Limited.
4 #
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
8 #
9 # http://www.apache.org/licenses/LICENSE-2.0
10 #
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
16 # ==================================================================================
17 */
18
19 #include "xapp_config.hpp"
20
21 string& XappSettings::operator[](const SettingName& theName){
22     return theSettings[theName];
23 }
24
25 void XappSettings::loadCmdlineSettings(int argc, char **argv){
26
27            // Parse command line options to over ride
28           static struct option long_options[] =
29             {
30                         {"xappname", required_argument, 0, 'n'},
31                                 {"xappid", required_argument, 0, 'x'},
32                                 {"port", required_argument, 0, 'p'},
33                                 {"threads", required_argument,    0, 't'},
34                                 {"ves-interval", required_argument, 0, 'i'},
35                                 {"gNodeB", required_argument, 0, 'g'}
36
37             };
38
39
40            while(1) {
41
42                 int option_index = 0;
43                 char c = getopt_long(argc, argv, "n:p:t:s:g:a:v:u:i:c:x:", long_options, &option_index);
44
45                 if(c == -1){
46                     break;
47                  }
48
49                 switch(c)
50                   {
51
52                   case 'n':
53                     theSettings[XAPP_NAME].assign(optarg);
54                     break;
55
56                   case 'p':
57                     theSettings[BOUNCER_PORT].assign(optarg);
58                     break;
59
60                   case 't':
61                         theSettings[THREADS].assign(optarg);
62                     mdclog_write(MDCLOG_INFO, "Number of threads set to %s from command line e\n", theSettings[THREADS].c_str());
63                     break;
64
65
66                   case 'x':
67                     theSettings[XAPP_ID].assign(optarg);
68                     mdclog_write(MDCLOG_INFO, "XAPP ID set to  %s from command line ", theSettings[XAPP_ID].c_str());
69                     break;
70
71                   case 'h':
72                     usage(argv[0]);
73                     exit(0);
74
75                   default:
76                     usage(argv[0]);
77                     exit(1);
78                   }
79            };
80
81 }
82
83 void XappSettings::loadDefaultSettings(){
84
85
86                  if(theSettings[XAPP_NAME].empty()){
87                   theSettings[XAPP_NAME] = DEFAULT_XAPP_NAME;
88                   }
89
90                   if(theSettings[XAPP_ID].empty()){
91                           theSettings[XAPP_ID] = DEFAULT_XAPP_NAME; //for now xapp_id is same as xapp_name since single xapp instance.
92                   }
93                   if(theSettings[LOG_LEVEL].empty()){
94                           theSettings[LOG_LEVEL] = DEFAULT_LOG_LEVEL;
95                   }
96                   if(theSettings[BOUNCER_PORT].empty()){
97                           theSettings[BOUNCER_PORT] = DEFAULT_PORT;
98                   }
99                   if(theSettings[MSG_MAX_BUFFER].empty()){
100                           theSettings[MSG_MAX_BUFFER] = DEFAULT_MSG_MAX_BUFFER;
101                   }
102
103                  if(theSettings[THREADS].empty()){
104                                           theSettings[THREADS] = DEFAULT_THREADS;
105                                   }
106
107
108 }
109
110 void XappSettings::loadEnvVarSettings(){
111
112           if (const char *env_xname = std::getenv("XAPP_NAME")){
113                   theSettings[XAPP_NAME].assign(env_xname);
114                   mdclog_write(MDCLOG_INFO,"Xapp Name set to %s from environment variable", theSettings[XAPP_NAME].c_str());
115           }
116           if (const char *env_xid = std::getenv("XAPP_NAME")){
117                    theSettings[XAPP_ID].assign(env_xid);
118                    mdclog_write(MDCLOG_INFO,"Xapp ID set to %s from environment variable", theSettings[XAPP_ID].c_str());
119           }
120
121           if (const char *env_ports = std::getenv("BOUNCER_PORT")){
122                   theSettings[BOUNCER_PORT].assign(env_ports);
123                   mdclog_write(MDCLOG_INFO,"Ports set to %s from environment variable", theSettings[BOUNCER_PORT].c_str());
124           }
125           if (const char *env_ports = std::getenv("MSG_MAX_BUFFER")){
126                           theSettings[MSG_MAX_BUFFER].assign(env_ports);
127                           mdclog_write(MDCLOG_INFO,"Ports set to %s from environment variable", theSettings[MSG_MAX_BUFFER].c_str());
128                   }
129
130 }
131
132 void XappSettings::usage(char *command){
133         std::cout <<"Usage : " << command << " " << std::endl;
134         std::cout <<" --name[-n] xapp_instance_name "<< std::endl;
135     std::cout <<" --port[-p] port to listen on e.g tcp:4561  "<< std::endl;
136     std::cout << "--threads[-t] number of listener threads "<< std::endl ;
137
138 }