Kea  1.5.0
netconf_process.cc
Go to the documentation of this file.
1 // Copyright (C) 2018 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 <util/threads/thread.h>
18 #include <boost/pointer_cast.hpp>
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 using namespace isc::util::thread;
26 
27 namespace isc {
28 namespace netconf {
29 
30 bool NetconfProcess::shut_down = false;
31 
32 NetconfProcess::NetconfProcess(const char* name,
33  const asiolink::IOServicePtr& io_service)
34  : DProcessBase(name, io_service, DCfgMgrBasePtr(new NetconfCfgMgr())) {
35 }
36 
38 }
39 
40 void
42 }
43 
44 void
46  LOG_INFO(netconf_logger, NETCONF_STARTED).arg(VERSION);
47 
48  try {
49  // Initialize netconf agent in a thread.
50  Thread th([this]() {
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  });
71 
72  // Let's process incoming data or expiring timers in a loop until
73  // shutdown condition is detected.
74  while (!shouldShutdown()) {
75  runIO();
76  }
77  stopIOService();
78  } catch (const std::exception& ex) {
79  LOG_FATAL(netconf_logger, NETCONF_FAILED).arg(ex.what());
80  try {
81  stopIOService();
82  } catch (...) {
83  // Ignore double errors
84  }
86  "Process run method failed: " << ex.what());
87  }
88 
90 }
91 
92 size_t
93 NetconfProcess::runIO() {
94  size_t cnt = getIoService()->get_io_service().poll();
95  if (!cnt) {
96  cnt = getIoService()->get_io_service().run_one();
97  }
98  return (cnt);
99 }
100 
103  shut_down = true;
104  setShutdownFlag(true);
105  return (isc::config::createAnswer(0, "Netconf is shutting down"));
106 }
107 
110  bool check_only) {
111  ConstElementPtr answer =
112  getCfgMgr()->simpleParseConfig(config_set, check_only);
113  int rcode = 0;
114  config::parseAnswer(rcode, answer);
115  return (answer);
116 }
117 
120  return (boost::dynamic_pointer_cast<NetconfCfgMgr>(getCfgMgr()));
121 }
122 
123 } // namespace isc::netconf
124 } // namespace isc
void initSysrepo()
Initialize sysrepo sessions.
Definition: netconf.cc:278
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:152
#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:219
virtual ~NetconfProcess()
Destructor.
virtual void init()
Initialize the Netconf process.
Ctrl Netconf Configuration Manager.
virtual const char * what() const
Returns a C-style character string of the cause of the exception.
A separate thread.
Definition: thread.h:36
#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:181
Wrappers for thread related functionality.
Definition: sync.cc:24
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.
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:174
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:166
Exception thrown if the process encountered an operational error.
Definition: d_process.h:22
#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:67
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
static bool shut_down
Global (globally visible) shutdown flag.
bool shouldShutdown() const
Checks if the process has been instructed to shut down.
Definition: d_process.h:145
virtual void run()
Implements the process's event loop.