OpenSDN source code
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
control_node_uve.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2016 Juniper Networks, Inc. All rights reserved.
3  */
4 
5 #include "control_node.h"
6 
7 #include "base/misc_utils.h"
8 #include "base/task.h"
10 #include "base/task_trigger.h"
11 #include "base/timer.h"
12 #include "bgp/bgp_server.h"
13 #include "bgp/bgp_xmpp_channel.h"
14 #include "control-node/sandesh/control_node_types.h"
15 #include "ifmap/ifmap_server.h"
16 
17 std::string ControlNode::hostname_;
18 std::string ControlNode::prog_name_;
19 std::string ControlNode::self_ip_;
21 
22 static std::unique_ptr<Timer> node_info_log_timer;
23 static std::unique_ptr<TaskTrigger> node_info_trigger;
24 
26  const BgpXmppChannelManager *xmpp_channel_mgr,
27  const IFMapServer *ifmap_server, const string &build_info) {
28  CHECK_CONCURRENCY("bgp::ShowCommand");
29  static bool first = true;
30  static BgpRouterState state;
31  bool change = false;
32 
33  state.set_name(server->localname());
34 
35  // Send self information.
36  if (first) {
37  state.set_uptime(UTCTimestampUsec());
38  change = true;
39  }
40 
41  vector<string> ip_list;
42  ip_list.push_back(ControlNode::GetSelfIp());
43  if (first || state.get_bgp_router_ip_list() != ip_list) {
44  state.set_bgp_router_ip_list(ip_list);
45  change = true;
46  }
47 
48  // Send Build information.
49  if (first || build_info != state.get_build_info()) {
50  state.set_build_info(build_info);
51  change = true;
52  }
53 
54  change |= server->CollectStats(&state, first);
55  change |= xmpp_channel_mgr->CollectStats(&state, first);
56  change |= ifmap_server->CollectStats(&state, first);
57 
58  if (change) {
59  assert(!state.get_name().empty());
60  BGPRouterInfo::Send(state);
61 
62  // Reset changed flags in the uve structure.
63  memset(static_cast<void*>(&state.__isset), 0, sizeof(state.__isset));
64  }
65 
66  /* Release process free memory to back to system */
67  int rc = malloc_trim(0);
68  if (!rc) {
69  LOG(ERROR, "Control-node free memory is not released to system,"
70  " rc: " << rc);
71  }
72 
73  first = false;
74  return true;
75 }
76 
78  node_info_trigger->Set();
79  // Periodic timer. Restart
80  return true;
81 }
82 
84  EventManager &evm, uint64_t period_msecs,
85  const BgpServer *bgp_server,
86  const BgpXmppChannelManager *xmpp_channel_mgr,
87  const IFMapServer *ifmap_server, const string &build_info) {
88  node_info_trigger.reset(
89  new TaskTrigger(
90  boost::bind(&ControlNode::ControlNodeInfoLogger, bgp_server,
91  xmpp_channel_mgr, ifmap_server, build_info),
92  TaskScheduler::GetInstance()->GetTaskId("bgp::ShowCommand"), 0));
93 
95  "ControlNode Info log timer"));
96 
97  // Start periodic timer to send BGPRouterInfo UVE.
98  node_info_log_timer->Start(period_msecs,
100  node_info_trigger.get()), NULL);
101 }
102 
104  node_info_trigger->set_disable();
105  while (node_info_trigger->IsSet())
106  usleep(100);
107  node_info_trigger.reset();
108  if (node_info_log_timer.get()) {
109  node_info_log_timer->Cancel();
110  while (node_info_log_timer->running() || node_info_log_timer->fired())
111  usleep(100);
113  node_info_log_timer.release();
114  }
115 }
static std::string GetSelfIp()
Definition: control_node.h:33
static std::string prog_name_
Definition: control_node.h:54
boost::asio::io_context * io_service()
Definition: event_manager.h:42
static std::string self_ip_
Definition: control_node.h:55
static std::unique_ptr< Timer > node_info_log_timer
static TaskScheduler * GetInstance()
Definition: task.cc:547
static std::unique_ptr< TaskTrigger > node_info_trigger
#define CHECK_CONCURRENCY(...)
bool CollectStats(BgpRouterState *state, bool first) const
Definition: bgp_server.cc:1065
static bool ControlNodeInfoLogTimer(TaskTrigger *node_info_trigger)
static void Shutdown()
static Timer * CreateTimer(boost::asio::io_context &service, const std::string &name, int task_id=Timer::GetTimerTaskId(), int task_instance=Timer::GetTimerInstanceId(), bool delete_on_completion=false)
Definition: timer.cc:201
static bool test_mode_
Definition: control_node.h:56
bool CollectStats(BgpRouterState *state, bool first) const
static uint64_t UTCTimestampUsec()
Definition: time_util.h:13
bool CollectStats(BgpRouterState *state, bool first) const
#define LOG(_Level, _Msg)
Definition: logging.h:33
static bool ControlNodeInfoLogger(const BgpServer *server, const BgpXmppChannelManager *xmpp_channel_mgr, const IFMapServer *ifmap_server, const std::string &build_info)
static void StartControlNodeInfoLogger(EventManager &evm, uint64_t period_msecs, const BgpServer *server, const BgpXmppChannelManager *xmpp_channel_mgr, const IFMapServer *ifmap_server, const string &build_info)
static std::string hostname_
Definition: control_node.h:53
static EventManager evm
static bool DeleteTimer(Timer *Timer)
Definition: timer.cc:222
const std::string & localname() const
Definition: bgp_server.cc:678