/
main.cc
280 lines (242 loc) · 9.8 KB
/
main.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
/*
* Copyright (c) 2013 Juniper Networks, Inc. All rights reserved.
*/
#include <fstream>
#include <pthread.h>
#include <boost/program_options.hpp>
#include <base/logging.h>
#include <base/misc_utils.h>
#include <base/contrail_ports.h>
#include <base/task.h>
#include <db/db_graph.h>
#include <ifmap/ifmap_link_table.h>
#include "ifmap/ifmap_sandesh_context.h"
#include <ifmap/ifmap_server_parser.h>
#include <ifmap/ifmap_server.h>
#include <ifmap/ifmap_xmpp.h>
#include <ifmap/client/ifmap_manager.h>
#include <io/event_manager.h>
#include <sandesh/sandesh.h>
#include <vnc_cfg_types.h>
#include <cmn/dns.h>
#include <bind/bind_util.h>
#include <mgr/dns_mgr.h>
#include <mgr/dns_oper.h>
#include <bind/named_config.h>
#include <cfg/dns_config.h>
#include <cfg/dns_config_parser.h>
#include <agent/agent_xmpp_init.h>
#include "cmn/dns_options.h"
#include <sandesh/common/vns_types.h>
#include <sandesh/common/vns_constants.h>
#include <uve/uve.h>
#include "xmpp/xmpp_sandesh.h"
namespace opt = boost::program_options;
using namespace boost::asio::ip;
using namespace std;
uint64_t start_time;
TaskTrigger *dns_info_trigger;
Timer *dns_info_log_timer;
bool DnsInfoLogTimer() {
dns_info_trigger->Set();
return false;
}
bool DnsInfoLogger() {
DnsUveClient::SendDnsUve(start_time);
dns_info_log_timer->Cancel();
dns_info_log_timer->Start(60*1000, boost::bind(&DnsInfoLogTimer),NULL);
return true;
}
static string FileRead(const string &filename) {
ifstream file(filename.c_str());
string content((istreambuf_iterator<char>(file)),
istreambuf_iterator<char>());
return content;
}
static void IFMap_Initialize(IFMapServer *server) {
IFMapLinkTable_Init(server->database(), server->graph());
IFMapServerParser *parser = IFMapServerParser::GetInstance("vnc_cfg");
vnc_cfg_ParserInit(parser);
vnc_cfg_Server_ModuleInit(server->database(), server->graph());
// bgp_schema_ParserInit(parser);
// bgp_schema_Server_ModuleInit(server->database(), server->graph());
server->Initialize();
}
void Dns::ShutdownDiscoveryClient(DiscoveryServiceClient *ds_client) {
if (ds_client) {
ds_client->Shutdown();
delete ds_client;
}
}
static bool OptionsParse(Options &options, int argc, char *argv[]) {
try {
options.Parse(*Dns::GetEventManager(), argc, argv);
return true;
} catch (boost::program_options::error &e) {
cout << "Error " << e.what() << endl;
} catch (...) {
cout << "Options Parser: Caught fatal unknown exception" << endl;
}
return false;
}
int main(int argc, char *argv[]) {
// Create DB table and event manager
Dns::Init();
Options options;
// Process options from command-line and configuration file.
if (!OptionsParse(options, argc, argv)) {
exit(-1);
}
Dns::SetProgramName(argv[0]);
Module::type module = Module::DNS;
string module_name = g_vns_constants.ModuleNames.find(module)->second;
std::string log_property_file = options.log_property_file();
if (log_property_file.size()) {
LoggingInit(log_property_file);
}
else {
LoggingInit(options.log_file(), options.log_file_size(),
options.log_files_count(), options.use_syslog(),
options.syslog_facility(), module_name);
}
string build_info_str;
Dns::GetVersion(build_info_str);
MiscUtils::LogVersionInfo(build_info_str, Category::DNSAGENT);
if (options.collectors_configured()) {
Dns::SetCollector(options.collector_server_list()[0]);
}
Dns::SetHttpPort(options.http_server_port());
Dns::SetDnsPort(options.dns_server_port());
boost::system::error_code ec;
string hostname = host_name(ec);
Dns::SetHostName(hostname);
Sandesh::set_send_rate_limit(options.sandesh_send_rate_limit());
if (options.discovery_server().empty()) {
NodeType::type node_type =
g_vns_constants.Module2NodeType.find(module)->second;
bool success(Sandesh::InitGenerator(
module_name,
options.hostname(),
g_vns_constants.NodeTypeNames.find(node_type)->second,
g_vns_constants.INSTANCE_ID_DEFAULT,
Dns::GetEventManager(),
options.http_server_port(), 0,
options.collector_server_list(),
NULL));
if (!success) {
LOG(ERROR, "SANDESH: Initialization FAILED ... exiting");
Sandesh::Uninit();
exit(1);
}
}
Sandesh::SetLoggingParams(options.log_local(), options.log_category(),
options.log_level());
// XXX Disable logging -- for test purposes only
if (options.log_disable()) {
SetLoggingDisabled(true);
}
// DNS::SetTestMode(options.test_mode());
DB config_db;
DBGraph config_graph;
IFMapServer ifmap_server(&config_db, &config_graph,
Dns::GetEventManager()->io_service());
IFMap_Initialize(&ifmap_server);
DnsManager dns_manager;
Dns::SetDnsManager(&dns_manager);
dns_manager.Initialize(&config_db, &config_graph,
options.named_config_dir(),
options.named_config_file(),
options.named_log_file(),
options.rndc_config_file(),
options.rndc_secret(),
options.named_max_cache_size());
DnsConfigParser parser(&config_db);
parser.Parse(FileRead(options.config_file()));
if (!DnsAgentXmppManager::Init(options.xmpp_auth_enabled(),
options.xmpp_server_cert(),
options.xmpp_server_key())) {
LOG(ERROR, "Address already in use " << ContrailPorts::DnsXmpp());
exit(1);
}
XmppSandeshContext xmpp_sandesh_context;
xmpp_sandesh_context.xmpp_server = Dns::GetXmppServer();
Sandesh::set_module_context("XMPP", &xmpp_sandesh_context);
IFMapSandeshContext ifmap_sandesh_context(&ifmap_server);
Sandesh::set_module_context("IFMap", &ifmap_sandesh_context);
start_time = UTCTimestampUsec();
dns_info_trigger =
new TaskTrigger(boost::bind(&DnsInfoLogger),
TaskScheduler::GetInstance()->GetTaskId("dns::Config"), 0);
dns_info_log_timer =
TimerManager::CreateTimer(*(Dns::GetEventManager()->io_service()),
"Dns Info log timer");
dns_info_log_timer->Start(60*1000, boost::bind(&DnsInfoLogTimer), NULL);
//Register services with Discovery Service Server
DiscoveryServiceClient *ds_client = NULL;
tcp::endpoint dss_ep;
if (DiscoveryServiceClient::ParseDiscoveryServerConfig(
options.discovery_server(), options.discovery_port(), &dss_ep)) {
ds_client = new DiscoveryServiceClient(Dns::GetEventManager(), dss_ep,
g_vns_constants.ModuleNames.find(Module::DNS)->second);
ds_client->Init();
// Publish DNServer Service
Dns::SetSelfIp(options.host_ip());
if (!options.host_ip().empty()) {
stringstream pub_ss;
const std::string &sname(
g_vns_constants.DNS_SERVER_DISCOVERY_SERVICE_NAME);
pub_ss << "<" << sname << "><ip-address>" << options.host_ip() <<
"</ip-address><port>" << options.dns_server_port() <<
"</port></" << sname << ">";
std::string pub_msg;
pub_msg = pub_ss.str();
ds_client->Publish(sname, pub_msg);
}
//subscribe to collector service if not configured
if (!options.collectors_configured()) {
Module::type module = Module::DNS;
NodeType::type node_type =
g_vns_constants.Module2NodeType.find(module)->second;
string subscriber_name =
g_vns_constants.ModuleNames.find(module)->second;
string node_type_name =
g_vns_constants.NodeTypeNames.find(node_type)->second;
Sandesh::CollectorSubFn csf = 0;
csf = boost::bind(&DiscoveryServiceClient::Subscribe, ds_client,
_1, _2, _3);
vector<string> list;
list.clear();
bool success(Sandesh::InitGenerator(subscriber_name,
options.hostname(),
node_type_name,
g_vns_constants.INSTANCE_ID_DEFAULT,
Dns::GetEventManager(),
options.http_server_port(),
csf,
list,
NULL));
if (!success) {
LOG(ERROR, "SANDESH: Initialization FAILED ... exiting");
Sandesh::Uninit();
Dns::ShutdownDiscoveryClient(ds_client);
exit(1);
}
}
} else {
LOG(ERROR, "Invalid Discovery Server hostname or ip " <<
options.discovery_server());
}
Dns::SetDiscoveryServiceClient(ds_client);
IFMapServerParser *ifmap_parser = IFMapServerParser::GetInstance("vnc_cfg");
IFMapManager *ifmapmgr = new IFMapManager(&ifmap_server,
options.ifmap_server_url(), options.ifmap_user(),
options.ifmap_password(), options.ifmap_certs_store(),
boost::bind(&IFMapServerParser::Receive, ifmap_parser,
&config_db, _1, _2, _3),
Dns::GetEventManager()->io_service());
ifmap_server.set_ifmap_manager(ifmapmgr);
ifmapmgr->InitializeDiscovery(ds_client, options.ifmap_server_url());
Dns::GetEventManager()->Run();
Dns::ShutdownDiscoveryClient(ds_client);
return 0;
}