OpenSDN source code
options.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2013 Juniper Networks, Inc. All rights reserved.
3  * Copyright Contributors to the OpenSDN project
4  */
5 
6 #include "options.h"
7 
8 #include <fstream>
9 #include <iostream>
10 #include <boost/asio/ip/host_name.hpp>
11 #include <boost/functional/hash.hpp>
12 
13 #include "control-node/buildinfo.h"
14 #include "base/contrail_ports.h"
15 #include "base/logging.h"
16 #include "base/misc_utils.h"
17 #include "base/util.h"
18 #include "config_client_manager.h"
19 #include <base/address_util.h>
20 #include <base/options_util.h>
21 
22 using namespace std;
23 using namespace boost::asio::ip;
24 namespace opt = boost::program_options;
25 using namespace options::util;
26 
27 // Process command line options for control-node.
29 }
30 
31 bool Options::Parse(EventManager &evm, int argc, char *argv[]) {
32  opt::options_description cmdline_options("Allowed options");
33  Initialize(evm, cmdline_options);
34 
35  try {
36  return Process(argc, argv, cmdline_options);
37  } catch (boost::program_options::error &e) {
38  cout << "Error " << e.what() << endl;
39  } catch (...) {
40  cout << "Options Parser: Caught fatal unknown exception" << endl;
41  }
42 
43  return false;
44 }
45 
46 // Initialize control-node's command line option tags with appropriate default
47 // values. Options can from a config file as well. By default, we read
48 // options from /etc/contrail/contrail-control.conf
50  opt::options_description &cmdline_options) {
51  boost::system::error_code error;
52  string hostname = host_name(error);
53  string host_ip = GetHostIp(evm.io_service(), hostname);
54 
55  if (host_ip.empty())
56  host_ip = "127.0.0.1";
57 
58  opt::options_description generic("Generic options");
59 
60  // Command line only options.
61  generic.add_options()
62  ("conf_file", opt::value<string>()->default_value(
63  "/etc/contrail/contrail-control.conf"),
64  "Configuration file")
65  ("help", "help message")
66  ("version", "Display version information")
67  ;
68 
69  uint16_t default_bgp_port = ContrailPorts::ControlBgp();
70  uint16_t default_http_server_port = ContrailPorts::HttpPortControl();
71  uint16_t default_xmpp_port = ContrailPorts::ControlXmpp();
72 
73  default_collector_server_list_.push_back("127.0.0.1:8086");
74 
75  vector<string> default_config_db_server_list;
76  string default_config_db_server(host_ip + ":9042");
77  default_config_db_server_list.push_back(default_config_db_server);
78 
79  vector<string> default_rabbitmq_server_list;
80  string default_rabbitmq_server(host_ip + ":5672");
81  default_rabbitmq_server_list.push_back(default_rabbitmq_server);
82 
83  // Command line and config file options.
84  opt::options_description config("Configuration options");
85  config.add_options()
86  ("DEFAULT.bgp_config_file",
87  opt::value<string>()->default_value("bgp_config.xml"),
88  "BGP Configuration file")
89  ("DEFAULT.bgp_port",
90  opt::value<uint16_t>()->default_value(default_bgp_port),
91  "BGP listener port")
92  ("DEFAULT.collectors",
93  opt::value<vector<string> >()->default_value(
94  default_collector_server_list_, "127.0.0.1:8086"),
95  "Collector server list")
96 
97  ("DEFAULT.gr_helper_bgp_disable",
98  opt::bool_switch(&gr_helper_bgp_disable_),
99  "Disable Graceful Restart Helper functionality for BGP peers")
100  ("DEFAULT.gr_helper_xmpp_disable",
101  opt::bool_switch(&gr_helper_xmpp_disable_),
102  "Disable Graceful Restart Helper functionality for XMPP agents")
103 
104  ("DEFAULT.hostip", opt::value<string>()->default_value(host_ip),
105  "IP address of control-node")
106  ("DEFAULT.hostname", opt::value<string>()->default_value(hostname),
107  "Hostname of control-node")
108  ("DEFAULT.http_server_port",
109  opt::value<uint16_t>()->default_value(default_http_server_port),
110  "Sandesh HTTP listener port")
111 
112  ("DEFAULT.log_category",
113  opt::value<string>()->default_value(log_category_),
114  "Category filter for local logging of sandesh messages")
115  ("DEFAULT.log_disable", opt::bool_switch(&log_disable_),
116  "Disable sandesh logging")
117  ("DEFAULT.log_file", opt::value<string>()->default_value(
118  "/var/log/contrail/contrail-control.log"),
119  "Filename for the logs to be written to")
120  ("DEFAULT.log_property_file", opt::value<string>()->default_value(""),
121  "log4cplus property file name")
122  ("DEFAULT.log_files_count",
123  opt::value<int>()->default_value(10),
124  "Maximum log file roll over index")
125  ("DEFAULT.log_file_size",
126  opt::value<long>()->default_value(10*1024*1024),
127  "Maximum size of the log file")
128  ("DEFAULT.log_level", opt::value<string>()->default_value("SYS_NOTICE"),
129  "Severity level for local logging of sandesh messages")
130  ("DEFAULT.log_local",
131  opt::bool_switch(&log_local_)->default_value(true),
132  "Enable local logging of sandesh messages")
133  ("DEFAULT.mvpn_ipv4_enable", opt::bool_switch(&mvpn_ipv4_enable_),
134  "Enable NGEN Multicast VPN support for IPv4 routes")
135  ("DEFAULT.use_syslog", opt::bool_switch(&use_syslog_),
136  "Enable logging to syslog")
137  ("DEFAULT.syslog_facility",
138  opt::value<string>()->default_value("LOG_LOCAL0"),
139  "Syslog facility to receive log lines")
140  ("DEFAULT.task_track_run_time", opt::bool_switch(&task_track_run_time_),
141  "Enable tracking of run time per task id")
142  ("DEFAULT.test_mode", opt::bool_switch(&test_mode_),
143  "Enable control-node to run in test-mode")
144  ("DEFAULT.tcp_hold_time", opt::value<int>()->default_value(30),
145  "Configurable TCP hold time")
146  ("DEFAULT.optimize_snat", opt::bool_switch(&optimize_snat_),
147  "Enable control-node optimizations for SNAT (deprecated)")
148  ("DEFAULT.xmpp_server_port",
149  opt::value<uint16_t>()->default_value(default_xmpp_port),
150  "XMPP listener port")
151  ("DEFAULT.xmpp_auth_enable", opt::bool_switch(&xmpp_auth_enable_),
152  "Enable authentication over Xmpp")
153  ("DEFAULT.xmpp_server_cert",
154  opt::value<string>()->default_value(
155  "/etc/contrail/ssl/certs/server.pem"),
156  "XMPP Server ssl certificate")
157  ("DEFAULT.xmpp_server_key",
158  opt::value<string>()->default_value(
159  "/etc/contrail/ssl/private/server-privkey.pem"),
160  "XMPP Server ssl private key")
161  ("DEFAULT.xmpp_ca_cert",
162  opt::value<string>()->default_value(
163  "/etc/contrail/ssl/certs/ca-cert.pem"),
164  "XMPP CA ssl certificate")
165 
166  ("CONFIGDB.config_db_server_list",
167  opt::value<vector<string> >()->default_value(
168  default_config_db_server_list, default_config_db_server),
169  "Config database server list")
170  ("CONFIGDB.config_db_username",
171  opt::value<string>()->default_value(""),
172  "ConfigDB user")
173  ("CONFIGDB.config_db_password",
174  opt::value<string>()->default_value(""),
175  "ConfigDB password")
176  ("CONFIGDB.config_db_use_ssl",
177  opt::value<bool>()->default_value(false),
178  "Use SSL for Cassandra connection")
179  ("CONFIGDB.config_db_ca_certs",
180  opt::value<string>()->default_value(""),
181  "CA Certificate file for SSL Cassandra connection")
182  ("CONFIGDB.rabbitmq_server_list",
183  opt::value<vector<string> >()->default_value(
184  default_rabbitmq_server_list, default_rabbitmq_server),
185  "RabbitMQ server list")
186  ("CONFIGDB.rabbitmq_user",
187  opt::value<string>()->default_value("guest"),
188  "RabbitMQ user")
189  ("CONFIGDB.rabbitmq_password",
190  opt::value<string>()->default_value("guest"),
191  "RabbitMQ password")
192  ("CONFIGDB.rabbitmq_vhost",
193  opt::value<string>()->default_value(""),
194  "RabbitMQ vhost")
195  ("CONFIGDB.rabbitmq_use_ssl",
196  opt::value<bool>()->default_value(false),
197  "Use SSL for RabbitMQ connection")
198  ("CONFIGDB.rabbitmq_ssl_version",
199  opt::value<string>()->default_value(""),
200  "SSL version for RabbitMQ connection")
201  ("CONFIGDB.rabbitmq_ssl_keyfile",
202  opt::value<string>()->default_value(""),
203  "Keyfile for SSL RabbitMQ connection")
204  ("CONFIGDB.rabbitmq_ssl_certfile",
205  opt::value<string>()->default_value(""),
206  "Certificate file for SSL RabbitMQ connection")
207  ("CONFIGDB.rabbitmq_ssl_ca_certs",
208  opt::value<string>()->default_value(""),
209  "CA Certificate file for SSL RabbitMQ connection")
210 
211  ("CONFIGDB.config_db_use_etcd",
212  opt::value<bool>()->default_value(false),
213  "Use etcd as the contrail DB client")
214  ;
215 
216  sandesh::options::AddOptions(&config, &sandesh_config_);
217 
218  config_file_options_.add(config);
219  cmdline_options.add(generic).add(config);
220 }
221 
222 uint32_t Options::GenerateHash(const std::vector<std::string> &list) {
223  std::string concat_servers;
224  std::vector<std::string>::const_iterator iter;
225  for (iter = list.begin(); iter != list.end(); iter++) {
226  concat_servers += *iter;
227  }
228  boost::hash<std::string> string_hash;
229  return(string_hash(concat_servers));
230 }
231 
233  uint32_t chk_sum = GenerateHash(config.config_db_server_list);
234  chk_sum += GenerateHash(config.rabbitmq_server_list);
235  boost::hash<std::string> string_hash;
236  chk_sum += string_hash(config.rabbitmq_user);
237  chk_sum += string_hash(config.rabbitmq_password);
238  chk_sum += string_hash(config.config_db_username);
239  chk_sum += string_hash(config.config_db_password);
240  return chk_sum;
241 }
242 
243 // Process command line options. They can come from a conf file as well. Options
244 // from command line always overrides those that come from the config file.
245 bool Options::Process(int argc, char *argv[],
246  opt::options_description &cmdline_options) {
247  // Process options off command line first.
248  opt::variables_map var_map;
249  opt::store(opt::parse_command_line(argc, argv, cmdline_options), var_map);
250 
251  // Process options off configuration file.
252  GetOptValue<string>(var_map, config_file_, "conf_file");
253  ifstream config_file_in;
254  config_file_in.open(config_file_.c_str());
255  if (config_file_in.good()) {
256  opt::store(opt::parse_config_file(config_file_in, config_file_options_),
257  var_map);
258  }
259  config_file_in.close();
260 
261  opt::notify(var_map);
262 
263  if (var_map.count("help")) {
264  cout << cmdline_options << endl;
265  return false;
266  }
267 
268  if (var_map.count("version")) {
269  cout << BuildInfo << endl;
270  return false;
271  }
272 
273  // Retrieve the options.
274  GetOptValue<string>(var_map, bgp_config_file_, "DEFAULT.bgp_config_file");
275  GetOptValue<uint16_t>(var_map, bgp_port_, "DEFAULT.bgp_port");
276  GetOptValue< vector<string> >(var_map, collector_server_list_,
277  "DEFAULT.collectors");
278  string error_msg;
279  if (!ValidateServerEndpoints(collector_server_list_, &error_msg)) {
280  cout << "Invalid endpoint : " << error_msg;
281  return false;
282  }
283 
284  collectors_configured_ = collector_server_list_.size();
285  if (collector_server_list_.size() == 1 &&
286  !collector_server_list_[0].compare(default_collector_server_list_[0])) {
287  collectors_configured_ = false;
288  }
289 
290  // Randomize Collector List
291  collector_chksum_ = GenerateHash(collector_server_list_);
292  randomized_collector_server_list_ = collector_server_list_;
293  std::random_shuffle(randomized_collector_server_list_.begin(),
294  randomized_collector_server_list_.end());
295 
296  GetOptValue<string>(var_map, host_ip_, "DEFAULT.hostip");
297  if (!ValidateIPAddressString(host_ip_, &error_msg)) {
298  cout << "Invalid IP address: " << host_ip_ << error_msg;
299  return false;
300  }
301 
302  GetOptValue<string>(var_map, hostname_, "DEFAULT.hostname");
303 
304  GetOptValue<uint16_t>(var_map, http_server_port_,
305  "DEFAULT.http_server_port");
306 
307  GetOptValue<string>(var_map, log_category_, "DEFAULT.log_category");
308  GetOptValue<string>(var_map, log_file_, "DEFAULT.log_file");
309  GetOptValue<string>(var_map, log_property_file_, "DEFAULT.log_property_file");
310  GetOptValue<int>(var_map, log_files_count_, "DEFAULT.log_files_count");
311  GetOptValue<long>(var_map, log_file_size_, "DEFAULT.log_file_size");
312  GetOptValue<string>(var_map, log_level_, "DEFAULT.log_level");
313  GetOptValue<string>(var_map, syslog_facility_, "DEFAULT.syslog_facility");
314  GetOptValue<int>(var_map, tcp_hold_time_, "DEFAULT.tcp_hold_time");
315  GetOptValue<uint16_t>(var_map, xmpp_port_, "DEFAULT.xmpp_server_port");
316  GetOptValue<string>(var_map, xmpp_server_cert_, "DEFAULT.xmpp_server_cert");
317  GetOptValue<string>(var_map, xmpp_server_key_, "DEFAULT.xmpp_server_key");
318  GetOptValue<string>(var_map, xmpp_ca_cert_, "DEFAULT.xmpp_ca_cert");
319 
320  ParseConfigOptions(var_map);
321 
322  sandesh::options::ProcessOptions(var_map, &sandesh_config_);
323  return true;
324 }
325 
326 void Options::ParseReConfig(bool force_reinit) {
327  // ReParse the filtered config params
328  opt::variables_map var_map;
329  ifstream config_file_in;
330  config_file_in.open(config_file_.c_str());
331  if (config_file_in.good()) {
332  opt::store(opt::parse_config_file(config_file_in, config_file_options_),
333  var_map);
334  }
335  config_file_in.close();
336 
337  collector_server_list_.clear();
338  GetOptValue< vector<string> >(var_map, collector_server_list_,
339  "DEFAULT.collectors");
340 
341  uint32_t new_chksum = GenerateHash(collector_server_list_);
342  if (collector_chksum_ != new_chksum) {
343  collector_chksum_ = new_chksum;
344  randomized_collector_server_list_.clear();
345  randomized_collector_server_list_ = collector_server_list_;
346  std::random_shuffle(randomized_collector_server_list_.begin(),
347  randomized_collector_server_list_.end());
348  }
349  // ReConnect Collectors irrespective of change list to achieve
350  // rebalance when older collector node/s are up again.
351  Sandesh::ReConfigCollectors(randomized_collector_server_list_);
352 
353  uint32_t old_config_chksum = configdb_chksum_;
354  ParseConfigOptions(var_map);
355  if ((force_reinit || old_config_chksum != configdb_chksum_) &&
356  config_client_manager_) {
357  config_client_manager_->ReinitConfigClient(configdb_options());
358  }
359 }
360 
361 void Options::ParseConfigOptions(const boost::program_options::variables_map
362  &var_map) {
363  configdb_options_.config_db_server_list.clear();
364  GetOptValue< vector<string> >(var_map,
365  configdb_options_.config_db_server_list,
366  "CONFIGDB.config_db_server_list");
367  GetOptValue<string>(var_map,
368  configdb_options_.config_db_username,
369  "CONFIGDB.config_db_username");
370  GetOptValue<string>(var_map,
371  configdb_options_.config_db_password,
372  "CONFIGDB.config_db_password");
373  GetOptValue<bool>(var_map,
374  configdb_options_.config_db_use_ssl,
375  "CONFIGDB.config_db_use_ssl");
376  GetOptValue<string>(var_map,
377  configdb_options_.config_db_ca_certs,
378  "CONFIGDB.config_db_ca_certs");
379  configdb_options_.rabbitmq_server_list.clear();
380  GetOptValue< vector<string> >(var_map,
381  configdb_options_.rabbitmq_server_list,
382  "CONFIGDB.rabbitmq_server_list");
383  GetOptValue<string>(var_map,
384  configdb_options_.rabbitmq_user,
385  "CONFIGDB.rabbitmq_user");
386  GetOptValue<string>(var_map,
387  configdb_options_.rabbitmq_password,
388  "CONFIGDB.rabbitmq_password");
389  GetOptValue<string>(var_map,
390  configdb_options_.rabbitmq_vhost,
391  "CONFIGDB.rabbitmq_vhost");
392  GetOptValue<bool>(var_map,
393  configdb_options_.rabbitmq_use_ssl,
394  "CONFIGDB.rabbitmq_use_ssl");
395  GetOptValue<string>(var_map,
396  configdb_options_.rabbitmq_ssl_version,
397  "CONFIGDB.rabbitmq_ssl_version");
398  GetOptValue<string>(var_map,
399  configdb_options_.rabbitmq_ssl_keyfile,
400  "CONFIGDB.rabbitmq_ssl_keyfile");
401  GetOptValue<string>(var_map,
402  configdb_options_.rabbitmq_ssl_certfile,
403  "CONFIGDB.rabbitmq_ssl_certfile");
404  GetOptValue<string>(var_map,
405  configdb_options_.rabbitmq_ssl_ca_certs,
406  "CONFIGDB.rabbitmq_ssl_ca_certs");
407  GetOptValue<bool>(var_map,
408  configdb_options_.config_db_use_etcd,
409  "CONFIGDB.config_db_use_etcd");
410  configdb_chksum_ = GenerateHash(configdb_options_);
411 }
std::string GetHostIp(boost::asio::io_context *io_service, const std::string &hostname)
bool ValidateServerEndpoints(std::vector< std::string > list, std::string *error_msg)
bool ValidateIPAddressString(std::string ip_address_str, std::string *error_msg)
static const uint16_t ControlBgp()
static const uint16_t HttpPortControl()
static const uint16_t ControlXmpp()
boost::asio::io_context * io_service()
Definition: event_manager.h:43
void ParseConfigOptions(const boost::program_options::variables_map &var_map)
Definition: options.cc:361
uint32_t GenerateHash(const std::vector< std::string > &list)
Definition: options.cc:222
bool Process(int argc, char *argv[], boost::program_options::options_description &cmdline_options)
void ParseReConfig()
Definition: dns_options.cc:368
void Initialize(EventManager &evm, boost::program_options::options_description &options)
Options()
Definition: options.cc:28
bool Parse(EventManager &evm, int argc, char **argv)
static void ReConfigCollectors(const std::vector< std::string > &collector_list)
Definition: sandesh.cc:232
static EventManager evm
static bool use_syslog_
Definition: logging.cc:21
void AddOptions(opt::options_description *sandesh_options, SandeshConfig *sandesh_config)
void ProcessOptions(const opt::variables_map &var_map, SandeshConfig *sandesh_config)
std::vector< std::string > config_db_server_list
std::vector< std::string > rabbitmq_server_list
const std::string BuildInfo