/
discovery_agent.cc
213 lines (171 loc) · 6.74 KB
/
discovery_agent.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
/*
* Copyright (c) 2013 Juniper Networks, Inc. All rights reserved.
*/
#include <boost/program_options.hpp>
#include <sandesh/sandesh.h>
#include "base/logging.h"
#include "cmn/agent_cmn.h"
#include "init/agent_param.h"
#include "discovery_agent.h"
#include "controller/controller_init.h"
#include "cmn/agent_cmn.h"
#include "cfg/cfg_init.h"
#include <discovery/client/discovery_client_stats_types.h>
using namespace boost::asio;
void DiscoveryAgentClient::Init(AgentParam *param) {
param_ = param;
uint32_t port = param_->discovery_server_port();
if (!port) {
port = DISCOVERY_SERVER_PORT;
}
ip::tcp::endpoint dss_ep;
DiscoveryServiceClient *ds_client = NULL;
if (DiscoveryServiceClient::ParseDiscoveryServerConfig(
agent_cfg_->agent()->discovery_server(), port, &dss_ep)) {
std::string subscriber_name =
agent_cfg_->agent()->discovery_client_name();
ds_client =
(new DiscoveryServiceClient(agent_cfg_->agent()->event_manager(),
dss_ep, subscriber_name));
ds_client->Init();
} else {
LOG(ERROR, "Invalid Discovery Server hostname or ip " <<
agent_cfg_->agent()->discovery_server());
}
agent_cfg_->agent()->set_discovery_service_client(ds_client);
}
void DiscoveryAgentClient::DiscoverController() {
DiscoveryServiceClient *ds_client =
agent_cfg_->agent()->discovery_service_client();
if (ds_client) {
int xs_instances =
agent_cfg_->agent()->discovery_xmpp_server_instances();
if ((xs_instances < 0) || (xs_instances > 2)) {
xs_instances = 2;
}
ds_client->Subscribe(
g_vns_constants.XMPP_SERVER_DISCOVERY_SERVICE_NAME, xs_instances,
boost::bind(&DiscoveryAgentClient::DiscoverySubscribeXmppHandler,
this, _1));
}
}
void DiscoveryAgentClient::ReDiscoverController() {
DiscoveryServiceClient *ds_client =
agent_cfg_->agent()->discovery_service_client();
if (ds_client) {
int xs_instances =
agent_cfg_->agent()->discovery_xmpp_server_instances();
if ((xs_instances < 0) || (xs_instances > 2)) {
xs_instances = 2;
}
ds_client->Subscribe(
g_vns_constants.XMPP_SERVER_DISCOVERY_SERVICE_NAME, xs_instances);
}
}
void DiscoveryAgentClient::DiscoverDNS() {
DiscoveryServiceClient *ds_client =
agent_cfg_->agent()->discovery_service_client();
if (ds_client) {
int dns_instances =
agent_cfg_->agent()->discovery_xmpp_server_instances();
if ((dns_instances < 0) || (dns_instances > 2)) {
dns_instances = 2;
}
ds_client->Subscribe(
g_vns_constants.DNS_SERVER_DISCOVERY_SERVICE_NAME, dns_instances,
boost::bind(&DiscoveryAgentClient::DiscoverySubscribeDNSHandler,
this, _1));
}
}
void DiscoveryAgentClient::ReDiscoverDNS() {
DiscoveryServiceClient *ds_client =
agent_cfg_->agent()->discovery_service_client();
if (ds_client) {
int dns_instances =
agent_cfg_->agent()->discovery_xmpp_server_instances();
if ((dns_instances < 0) || (dns_instances > 2)) {
dns_instances = 2;
}
ds_client->Subscribe(
g_vns_constants.DNS_SERVER_DISCOVERY_SERVICE_NAME, dns_instances);
}
}
void DiscoveryAgentClient::DiscoverySubscribeDNSHandler(std::vector<DSResponse> resp) {
agent_cfg_->agent()->controller()->ApplyDiscoveryDnsXmppServices(resp);
}
void DiscoveryAgentClient::DiscoverySubscribeXmppHandler(std::vector<DSResponse> resp) {
agent_cfg_->agent()->controller()->ApplyDiscoveryXmppServices(resp);
}
void DiscoveryAgentClient::DiscoverServices() {
Agent *agent = agent_cfg_->agent();
if (!agent->discovery_server().empty()) {
DiscoveryServiceClient *ds_client =
agent->discovery_service_client();
if (ds_client) {
//subscribe to collector service
if (param_->collector_server_list().size() == 0) {
Module::type module = static_cast<Module::type>
(agent->module_type());
NodeType::type node_type =
g_vns_constants.Module2NodeType.find(module)->second;
std::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);
std::vector<std::string> list;
list.clear();
Sandesh::set_send_rate_limit(agent->sandesh_send_rate_limit());
Sandesh::InitGenerator(agent->module_name(),
agent->host_name(),
node_type_name,
agent->instance_id(),
agent->event_manager(),
agent->introspect_port(),
csf,
list,
NULL);
}
//subscribe to Xmpp Server on controller
if (agent->controller_ifmap_xmpp_server(0).empty()) {
DiscoveryAgentClient::DiscoverController();
}
//subscribe to DNServer
if (agent->dns_server(0).empty()) {
DiscoveryAgentClient::DiscoverDNS();
}
}
}
}
void DiscoveryAgentClient::Shutdown() {
DiscoveryServiceClient *ds_client =
agent_cfg_->agent()->discovery_service_client();
if (ds_client) {
//unsubscribe to services
ds_client->Shutdown();
}
}
// sandesh discovery subscriber stats
void DiscoveryClientSubscriberStatsReq::HandleRequest() const {
DiscoveryClientSubscriberStatsResponse *resp =
new DiscoveryClientSubscriberStatsResponse();
resp->set_context(context());
std::vector<DiscoveryClientSubscriberStats> stats_list;
DiscoveryServiceClient *ds =
Agent::GetInstance()->discovery_service_client();
//DiscoveryAgentClient::GetAgentDiscoveryServiceClient();
if (ds) {
ds->FillDiscoveryServiceSubscriberStats(stats_list);
}
resp->set_subscriber(stats_list);
resp->set_more(false);
resp->Response();
}
// sandesh discovery publisher stats
void DiscoveryClientPublisherStatsReq::HandleRequest() const {
DiscoveryClientPublisherStatsResponse *resp =
new DiscoveryClientPublisherStatsResponse();
resp->set_context(context());
resp->set_more(false);
resp->Response();
}