Kea  1.9.9-git
netconf_process.cc
Go to the documentation of this file.
1 // Copyright (C) 2018-2020 Internet Systems Consortium, Inc. ("ISC")
2 //
3 // This Source Code Form is subject to the terms of the Mozilla Public
4 // License, v. 2.0. If a copy of the MPL was not distributed with this
5 // file, You can obtain one at http://mozilla.org/MPL/2.0/.
6 
7 #include <config.h>
9 #include <netconf/netconf.h>
12 #include <netconf/netconf_log.h>
13 #include <asiolink/io_address.h>
14 #include <asiolink/io_error.h>
15 #include <cc/command_interpreter.h>
16 #include <config/timeouts.h>
17 #include <boost/pointer_cast.hpp>
18 #include <thread>
19 
20 using namespace isc::asiolink;
21 using namespace isc::config;
22 using namespace isc::data;
23 using namespace isc::http;
24 using namespace isc::process;
25 
26 namespace isc {
27 namespace netconf {
28 
29 std::atomic<bool> NetconfProcess::shut_down(false);
30 
31 NetconfProcess::NetconfProcess(const char* name,
32  const asiolink::IOServicePtr& io_service)
33  : DProcessBase(name, io_service, DCfgMgrBasePtr(new NetconfCfgMgr())) {
34 }
35 
37 }
38 
39 void
41 }
42 
43 void
46 
47  try {
48  // Initialize netconf agent in a thread.
49  std::thread th([this]() {
50  try {
51  if (shouldShutdown()) {
52  return;
53  }
54  // Initialize sysrepo.
55  agent_.initSysrepo();
56  if (shouldShutdown()) {
57  return;
58  }
59 
60  // Get the configuration manager.
61  NetconfCfgMgrPtr cfg_mgr;
62  if (shouldShutdown()) {
63  return;
64  } else {
65  cfg_mgr = getNetconfCfgMgr();
66  }
67 
68  // Call init.
69  agent_.init(cfg_mgr);
70  } catch (...) {
71  // Should not happen but in case...
72  std::exception_ptr eptr = std::current_exception();
73  getIoService()->post([eptr] () {
74  if (eptr) {
75  std::rethrow_exception(eptr);
76  }
77  });
78  }
79  });
80 
81  // Detach the thread.
82  th.detach();
83 
84  // Let's process incoming data or expiring timers in a loop until
85  // shutdown condition is detected.
86  while (!shouldShutdown()) {
87  runIO();
88  }
89  stopIOService();
90 
91  } catch (const std::exception& ex) {
92  LOG_FATAL(netconf_logger, NETCONF_FAILED).arg(ex.what());
93  try {
94  stopIOService();
95  } catch (...) {
96  // Ignore double errors
97  }
99  "Process run method failed: " << ex.what());
100  }
101 
103 }
104 
105 size_t
106 NetconfProcess::runIO() {
107  size_t cnt = getIoService()->get_io_service().poll();
108  if (!cnt) {
109  cnt = getIoService()->get_io_service().run_one();
110  }
111  return (cnt);
112 }
113 
116  shut_down = true;
117  setShutdownFlag(true);
118  return (isc::config::createAnswer(0, "Netconf is shutting down"));
119 }
120 
123  bool check_only) {
124  ConstElementPtr answer =
125  getCfgMgr()->simpleParseConfig(config_set, check_only);
126  int rcode = 0;
127  config::parseAnswer(rcode, answer);
128  return (answer);
129 }
130 
133  return (boost::dynamic_pointer_cast<NetconfCfgMgr>(getCfgMgr()));
134 }
135 
136 } // namespace isc::netconf
137 } // namespace isc
void initSysrepo()
Initialize sysrepo sessions.
Definition: netconf.cc:278
static std::atomic< bool > shut_down
Global (globally visible) shutdown flag.
virtual isc::data::ConstElementPtr configure(isc::data::ConstElementPtr config_set, bool check_only=false)
Processes the given configuration.
void setShutdownFlag(bool value)
Sets the process shut down flag to the given value.
Definition: d_process.h:166
#define LOG_INFO(LOGGER, MESSAGE)
Macro to conveniently test info output and log it.
Definition: macros.h:20
ConstElementPtr createAnswer(const int status_code, const std::string &text, const ConstElementPtr &arg)
boost::shared_ptr< DCfgMgrBase > DCfgMgrBasePtr
Defines a shared pointer to DCfgMgrBase.
Definition: d_cfg_mgr.h:247
const isc::log::MessageID NETCONF_STARTED
const isc::log::MessageID NETCONF_FAILED
virtual ~NetconfProcess()
Destructor.
virtual void init()
Initialize the Netconf process.
Ctrl Netconf Configuration Manager.
bool shouldShutdown() const
Checks if the process has been instructed to shut down.
Definition: d_process.h:159
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
DCfgMgrBasePtr & getCfgMgr()
Fetches the process's configuration manager.
Definition: d_process.h:195
boost::shared_ptr< const Element > ConstElementPtr
Definition: data.h:23
virtual isc::data::ConstElementPtr shutdown(isc::data::ConstElementPtr args)
Initiates the process's shutdown process.
const isc::log::MessageID NETCONF_RUN_EXIT
isc::log::Logger netconf_logger(NETCONF_LOGGER_NAME)
Base logger for the netconf agent.
Definition: netconf_log.h:49
void stopIOService()
Convenience method for stopping IOservice processing.
Definition: d_process.h:188
virtual const char * what() const
Returns a C-style character string of the cause of the exception.
ConstElementPtr parseAnswer(int &rcode, const ConstElementPtr &msg)
Defines the logger used by the top-level component of kea-dhcp-ddns.
boost::shared_ptr< NetconfCfgMgr > NetconfCfgMgrPtr
Defines a shared pointer to NetconfCfgMgr.
This file contains several functions and constants that are used for handling commands and responses ...
asiolink::IOServicePtr & getIoService()
Fetches the controller's IOService.
Definition: d_process.h:180
Exception thrown if the process encountered an operational error.
Definition: d_process.h:24
#define LOG_DEBUG(LOGGER, LEVEL, MESSAGE)
Macro to conveniently test debug output and log it.
Definition: macros.h:14
Contains declarations for loggers used by the Kea netconf agent.
Application Process Interface.
Definition: d_process.h:81
NetconfCfgMgrPtr getNetconfCfgMgr()
Returns a pointer to the configuration manager.
#define LOG_FATAL(LOGGER, MESSAGE)
Macro to conveniently test fatal output and log it.
Definition: macros.h:38
void init(NetconfCfgMgrPtr cfg_mgr)
Initialization.
Definition: netconf.cc:136
const int DBGLVL_START_SHUT
This is given a value of 0 as that is the level selected if debugging is enabled without giving a lev...
Definition: log_dbglevels.h:50
virtual void run()
Implements the process's event loop.