OpenSDN source code
agent_uve_base.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 <base/cpuinfo.h>
7 #include <db/db.h>
8 #include <cmn/agent_cmn.h>
10 #include <oper/interface.h>
11 
12 #include "vr_genetlink.h"
13 #include "nl_util.h"
14 
15 #include <uve/agent_uve_base.h>
16 #include <uve/vn_uve_table_base.h>
17 #include <uve/stats_interval_types.h>
18 #include <init/agent_param.h>
19 #include <oper/mirror_table.h>
20 #include <oper/global_vrouter.h>
21 #include <oper/tag.h>
23 #include <cmn/agent_stats.h>
24 
25 using process::ConnectionInfo;
26 using process::ConnectionType;
27 using process::ProcessState;
28 using process::ConnectionStatus;
31 using process::g_process_info_constants;
32 
33 
34 const uint32_t AgentUveBase::kUveCountPerTimer;
35 
36 const uint32_t AgentUveBase::kDefaultInterval;
37 
39 
41 
42 
44 
45 AgentUveBase::AgentUveBase(Agent *agent, uint64_t intvl,
46  uint32_t default_intvl, uint32_t incremental_intvl)
47  : vn_uve_table_(NULL), vm_uve_table_(NULL), vrouter_uve_entry_(NULL),
48  prouter_uve_table_(new ProuterUveTable(agent, default_intvl)),
49  interface_uve_table_(NULL),
50  default_interval_(default_intvl),
51  incremental_interval_(incremental_intvl),
52  agent_(agent), bandwidth_intvl_(intvl),
53  vrouter_stats_collector_(new VrouterStatsCollector(
54  *(agent->event_manager()->io_service()),
55  this)) {
56  singleton_ = this;
57 }
58 
60 }
61 
63  const {
64  TagTable *table = agent_->tag_table();
65  TagList::const_iterator it = tl.begin();
66  while (it != tl.end()) {
67  uint64_t type = ((uint64_t)*it >> TagEntry::kTagTypeBitShift);
68  switch (type) {
70  info->application = table->TagName(*it);
71  break;
72  case TagTable::TIER:
73  info->tier = table->TagName(*it);
74  break;
75  case TagTable::SITE:
76  info->site = table->TagName(*it);
77  break;
79  info->deployment = table->TagName(*it);
80  break;
81  case TagTable::LABEL:
82  if (info->fill_type == UveTagData::SET) {
83  info->label_set.insert(table->TagName(*it));
84  break;
85  } else if (info->fill_type == UveTagData::VECTOR) {
86  info->label_vector.push_back(table->TagName(*it));
87  break;
88  }
89  if (!info->labels.empty()) {
90  info->labels += ";";
91  }
92  info->labels.append(table->TagName(*it));
93  break;
95  info->application = table->TagName(*it);
96  break;
97  default:
98  if (info->fill_type == UveTagData::SET) {
99  info->custom_tag_set.insert(table->TagName(*it));
100  break;
101  } else if (info->fill_type == UveTagData::VECTOR) {
102  info->custom_tag_vector.push_back(table->TagName(*it));
103  break;
104  }
105  if (!info->custom_tags.empty()) {
106  info->custom_tags += ";";
107  }
108  info->custom_tags.append(table->TagName(*it));
109  break;
110  }
111  ++it;
112  }
113 }
114 
115 /* Web-UI requires tag-id to returned as hex string. This should start with 0x
116  * and have exactly 12 digits/characters. Zero should be used for filling leading
117  * characters if the tag-id is not 12 digits wide. Web-UI uses this id to do
118  * lookup in API server */
119 string AgentUveBase::TagIDToHexString(uint64_t value) const {
120  std::stringstream ss;
121  ss << "0x" << std::setfill('0') <<
122  std::setw(12) << std::hex << (uint64_t)value;
123  return ss.str();
124 }
125 
127  const {
128  TagList::const_iterator it = tl.begin();
129  while (it != tl.end()) {
130  uint64_t type = ((uint64_t)*it >> TagEntry::kTagTypeBitShift);
131  switch (type) {
133  info->application = TagIDToHexString(*it);
134  break;
135  case TagTable::TIER:
136  info->tier = TagIDToHexString(*it);
137  break;
138  case TagTable::SITE:
139  info->site = TagIDToHexString(*it);
140  break;
142  info->deployment = TagIDToHexString(*it);
143  break;
144  case TagTable::LABEL:
145  if (info->fill_type == UveTagData::SET) {
146  info->label_set.insert(TagIDToHexString(*it));
147  break;
148  } else if (info->fill_type == UveTagData::VECTOR) {
149  info->label_vector.push_back(TagIDToHexString(*it));
150  break;
151  }
152  if (!info->labels.empty()) {
153  info->labels += ";";
154  }
155  info->labels.append(TagIDToHexString(*it));
156  break;
158  info->application = TagIDToHexString(*it);
159  break;
160  default:
161  if (info->fill_type == UveTagData::SET) {
162  info->custom_tag_set.insert(TagIDToHexString(*it));
163  break;
164  } else if (info->fill_type == UveTagData::VECTOR) {
165  info->custom_tag_vector.push_back(TagIDToHexString(*it));
166  break;
167  }
168  if (!info->custom_tags.empty()) {
169  info->custom_tags += ";";
170  }
171  info->custom_tags.append(TagIDToHexString(*it));
172  break;
173  }
174  ++it;
175  }
176 }
177 
178 void AgentUveBase::set_default_interval(uint32_t new_interval) {
179  if (new_interval == 0) {
181  } else {
182  default_interval_ = new_interval;
183  }
184 }
185 
186 void AgentUveBase::set_incremental_interval(uint32_t new_interval) {
187  if (new_interval == 0) {
189  } else {
190  incremental_interval_ = new_interval;
191  }
192 }
193 
195  vn_uve_table_.get()->Shutdown();
196  vm_uve_table_.get()->Shutdown();
197  vrouter_uve_entry_.get()->Shutdown();
198  prouter_uve_table_.get()->Shutdown();
199  interface_uve_table_.get()->Shutdown();
201  vrouter_stats_collector_->Shutdown();
202 }
203 
205  std::string module_id(agent_->module_name());
206  std::string instance_id(agent_->instance_id());
208  boost::asio::io_context &io = *evm->io_service();
209 
212  ConnectionStateManager::
213  GetInstance();
214  agent_->set_connection_state(ConnectionState::GetInstance());
216  module_id, instance_id,
218  this, _1, _2, _3), "ObjectVRouter");
219 }
220 
221 uint8_t AgentUveBase::ExpectedConnections(uint8_t &num_control_nodes,
222  uint8_t &num_dns_servers) {
223  uint8_t count = 0;
224  AgentParam *cfg = agent_->params();
225 
226  for (int i = 0; i < MAX_XMPP_SERVERS; i++) {
227  if (!agent_->controller_ifmap_xmpp_server(i).empty()) {
228  num_control_nodes++;
229  count++;
230  }
231  if (agent_->services() && !agent_->dns_server(i).empty()) {
232  num_dns_servers++;
233  count++;
234  }
235  }
236  //Increment 1 for collector service
237  if (cfg->collector_server_list().size() != 0) {
238  count++;
239  }
240 
241  return count;
242 }
243 
244 void AgentUveBase::UpdateMessage(const ConnectionInfo &cinfo,
245  std::string &message) {
246  if (message.empty()) {
247  message = cinfo.get_type();
248  } else {
249  message += ", " + cinfo.get_type();
250  }
251  const std::string &name(cinfo.get_name());
252  if (!name.empty()) {
253  message += ":" + name;
254  }
255 }
256 
258  if (!agent_ || !agent_->oper_db() || !agent_->oper_db()->global_vrouter()) {
259  return false;
260  }
261  return agent_->oper_db()->global_vrouter()->configured();
262 }
263 
265  (const std::vector<ConnectionInfo> &cinfos,
266  ProcessState::type &pstate, std::string &message) {
267  size_t num_conns = 0;
268  uint8_t num_control_nodes = 0, num_dns_servers = 0;
269  uint8_t down_control_nodes = 0;
270  uint8_t expected_conns = ExpectedConnections(num_control_nodes,
271  num_dns_servers);
272  std::string cup(g_process_info_constants.ConnectionStatusNames.
273  find(ConnectionStatus::UP)->second);
274  bool is_cup = true;
275  bool is_tor_connected = false;
276  string tor_type(g_process_info_constants.ConnectionTypeNames.
277  find(ConnectionType::TOR)->second);
278  // Iterate to determine process connectivity status
279  for (std::vector<ConnectionInfo>::const_iterator it = cinfos.begin();
280  it != cinfos.end(); it++) {
281  const ConnectionInfo &cinfo(*it);
282  const std::string &conn_status(cinfo.get_status());
283  if (cinfo.get_type() == tor_type) {
284  is_tor_connected = true;
285  continue;
286  }
287  /* Don't consider ConnectionType::TOR type for counting connections.
288  * contrail-tor-agent is not supposed to report as Non-Functional when
289  * it is in backup mode, but contrail-tor-agent does not have a way to
290  * figure out that it is in backup mode. Hence for contrail-tor-agent
291  * (both active and backup modes) we don't consider connection to TOR
292  * for reporting Node Status */
293  num_conns++;
294  if (conn_status != cup) {
295  if (cinfo.get_name().compare(0, 13,
296  agent_->xmpp_control_node_prefix()) == 0) {
297  down_control_nodes++;
298  }
299  is_cup = false;
300  UpdateMessage(cinfo, message);
301  }
302  }
303  if ((num_control_nodes == 0) || (num_control_nodes == down_control_nodes)) {
304  pstate = ProcessState::NON_FUNCTIONAL;
305  if ((num_control_nodes == 0) && message.empty()) {
306  message = "No control-nodes configured";
307  }
308  } else if (!is_tor_connected && agent_->tor_agent_enabled()) {
309  // waiting for first TOR config to arrive
310  pstate = ProcessState::NON_FUNCTIONAL;
311  message += " No ToR Config";
312  } else {
313  pstate = ProcessState::FUNCTIONAL;
314  }
315  if (!is_cup) {
316  message += " connection down";
317  }
318  if (!HasSelfConfiguration()) {
319  // waiting for Global vrouter config
320  pstate = ProcessState::NON_FUNCTIONAL;
321  if (message.empty()) {
322  message = "No Configuration for self";
323  } else {
324  message += ", No Configuration for self";
325  }
326  }
327  for (int i = 0; i < MAX_XMPP_SERVERS; i++) {
328  if (!agent_->controller_ifmap_xmpp_server(i).empty()) {
329  if (agent_->stats()->xmpp_reconnects(i) >= 1) {
330  break;
331  }
332  }
333  }
334 
335  if (num_conns != expected_conns) {
336  message += " Number of connections:" + integerToString(num_conns) +
337  ", Expected: " + integerToString(expected_conns);
338  return;
339  }
340  return;
341 }
342 
344  vn_uve_table_.get()->RegisterDBClients();
345  vm_uve_table_.get()->RegisterDBClients();
346  vrouter_uve_entry_.get()->RegisterDBClients();
347  prouter_uve_table_.get()->RegisterDBClients();
348  interface_uve_table_.get()->RegisterDBClients();
349 }
350 
352  vrouter_stats_collector_->InitDone();
353 }
std::vector< uint64_t > TagList
Definition: agent.h:203
#define MAX_XMPP_SERVERS
Definition: agent.h:292
const std::vector< std::string > collector_server_list() const
Definition: agent_param.h:305
uint32_t xmpp_reconnects(uint8_t idx) const
Definition: agent_stats.h:60
static const uint32_t kUveCountPerTimer
boost::scoped_ptr< VrouterStatsCollector > vrouter_stats_collector_
static const uint64_t kBandwidthInterval
virtual ~AgentUveBase()
void VrouterAgentProcessState(const std::vector< process::ConnectionInfo > &c, process::ProcessState::type &state, std::string &message)
virtual void RegisterDBClients()
void BuildTagIdsFromList(const TagList &tl, UveTagData *info) const
boost::scoped_ptr< VnUveTableBase > vn_uve_table_
boost::scoped_ptr< VrouterUveEntryBase > vrouter_uve_entry_
boost::scoped_ptr< InterfaceUveTable > interface_uve_table_
AgentUveBase(Agent *agent, uint64_t intvl, uint32_t default_intvl, uint32_t incremental_intvl)
uint32_t incremental_interval_
void set_incremental_interval(uint32_t new_interval=kIncrementalInterval)
Sets the incremental interval (time between dispatches of Agent's information and its remnant to UVE)...
static AgentUveBase * singleton_
void set_default_interval(uint32_t new_interval=kDefaultInterval)
Sets the default interval (time between two dispatches of Agent's information to UVE).
boost::scoped_ptr< VmUveTableBase > vm_uve_table_
void UpdateMessage(const process::ConnectionInfo &info, std::string &message)
static const uint32_t kDefaultInterval
static const uint32_t kIncrementalInterval
virtual void Shutdown()
uint8_t ExpectedConnections(uint8_t &num_c_nodes, uint8_t &num_d_servers)
boost::scoped_ptr< ProuterUveTable > prouter_uve_table_
uint32_t default_interval_
std::string TagIDToHexString(uint64_t value) const
virtual void InitDone()
process::ConnectionStateManager * connection_state_manager_
void BuildTagNamesFromList(const TagList &tl, UveTagData *info) const
bool HasSelfConfiguration() const
Definition: agent.h:361
ServicesModule * services() const
Definition: agent.cc:977
TagTable * tag_table() const
Definition: agent.h:508
OperDB * oper_db() const
Definition: agent.cc:1017
const std::string & dns_server(uint8_t idx) const
Definition: agent.h:860
AgentParam * params() const
Definition: agent.h:1227
const std::string & agent_name() const
Definition: agent.h:881
EventManager * event_manager() const
Definition: agent.h:1106
void set_connection_state(process::ConnectionState *state)
Definition: agent.h:959
const std::string & controller_ifmap_xmpp_server(uint8_t idx) const
Definition: agent.h:733
const std::string & module_name() const
Definition: agent.h:895
static const std::string & xmpp_control_node_prefix()
Definition: agent.h:450
bool tor_agent_enabled() const
Definition: agent.h:1167
AgentStats * stats() const
Definition: agent.cc:885
const std::string & instance_id() const
Definition: agent.h:889
static void Init()
boost::asio::io_context * io_service()
Definition: event_manager.h:43
bool configured() const
GlobalVrouter * global_vrouter() const
Definition: operdb_init.h:55
static const uint32_t kTagTypeBitShift
const std::string & TagName(uint64_t id)
void Init(boost::asio::io_context &service, const std::string &hostname, const std::string &module, const std::string &instance_id, ProcessStateFn status_cb, std::string table)
static EventManager evm
uint8_t type
Definition: load_balance.h:2
Definition: io_utils.cc:12
static const std::string integerToString(const NumberType &num)
Definition: string_util.h:20
std::vector< string > label_vector
std::vector< string > custom_tag_vector
std::set< string > label_set
std::string application
std::string tier
std::string site
std::string deployment
FillType fill_type
std::set< string > custom_tag_set
std::string labels
std::string custom_tags