Kea: netconf_process.cc Source File

| Kea 3.2.0-git

netconf_process.cc
Go to the documentation of this file.
1// Copyright (C) 2018-2026 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>
8
9#include <asiolink/asio_wrapper.h>
10#include <asiolink/io_address.h>
11#include <asiolink/io_error.h>
12#include <asiolink/io_service_mgr.h>
13#include <cc/command_interpreter.h>
14#include <config/timeouts.h>
15#include <netconf/netconf.h>
16#include <netconf/netconf_controller.h>
17#include <netconf/netconf_log.h>
18#include <netconf/netconf_process.h>
19
20using namespace isc::asiolink;
21using namespace isc::config;
22using namespace isc::data;
23using namespace isc::http;
24using namespace isc::process;
25
26namespace isc {
27namespace netconf {
28
29NetconfProcess::NetconfProcess(const char* name,
30 const asiolink::IOServicePtr& io_service)
31 : DProcessBase(name, io_service, DCfgMgrBasePtr(new NetconfCfgMgr())) {
32}
33
34void
35NetconfProcess::init() {
36}
37
38void
39NetconfProcess::run() {
40 LOG_INFO(netconf_logger, NETCONF_STARTED).arg(VERSION);
41
42 try {
43 // Call init.
44 agent_.init(getNetconfCfgMgr());
45
46 // Let's process incoming data or expiring timers in a loop until
47 // shutdown condition is detected.
48 while (!shouldShutdown()) {
49 runIO();
50 }
51 stopIOService();
52
53 } catch (const std::exception& ex) {
54 LOG_FATAL(netconf_logger, NETCONF_FAILED).arg(ex.what());
55 try {
56 stopIOService();
57 } catch (...) {
58 // Ignore double errors
59 }
60 isc_throw(DProcessBaseError, "Process run method failed: " << ex.what());
61 }
62
63 LOG_DEBUG(netconf_logger, isc::log::DBGLVL_START_SHUT, NETCONF_RUN_EXIT);
64}
65
66size_t
67NetconfProcess::runIO() {
68 // Handle events registered by hooks using external IOService objects.
69 IOServiceMgr::instance().pollIOServices();
70 size_t cnt = getIOService()->poll();
71 if (!cnt) {
72 cnt = getIOService()->runOne();
73 }
74 return (cnt);
75}
76
77 isc::data::ConstElementPtr
78NetconfProcess::shutdown(isc::data::ConstElementPtr /*args*/) {
79 setShutdownFlag(true);
80 return (isc::config::createAnswer(CONTROL_RESULT_SUCCESS, "Netconf is shutting down"));
81}
82
83 isc::data::ConstElementPtr
84NetconfProcess::configure(isc::data::ConstElementPtr config_set,
85 bool check_only) {
86 ConstElementPtr answer =
87 getCfgMgr()->simpleParseConfig(config_set, check_only);
88 int rcode = 0;
89 config::parseAnswer(rcode, answer);
90
92 try {
93 // Handle events registered by hooks using external IOService objects.
94 IOServiceMgr::instance().pollIOServices();
95 } catch (const std::exception& ex) {
96 if (rcode == CONTROL_RESULT_FATAL_ERROR) {
97 return (answer);
98 }
99 std::ostringstream err;
100 err << "Error initializing hooks: " << ex.what();
101 return (isc::config::createAnswer(CONTROL_RESULT_ERROR, err.str()));
102 }
103
104 return (answer);
105}
106
107 NetconfCfgMgrPtr
108NetconfProcess::getNetconfCfgMgr() {
109 return (boost::dynamic_pointer_cast<NetconfCfgMgr>(getCfgMgr()));
110}
111
112} // namespace netconf
113} // namespace isc
asio_wrapper.h
isc::asiolink::IOServiceMgr::instance
static IOServiceMgr & instance()
Access the IOServiceMgr singleton instance.
Definition io_service_mgr.cc:14
isc::asiolink::IOServiceMgr::pollIOServices
size_t pollIOServices()
Poll IOService objects.
Definition io_service_mgr.cc:39
isc::netconf::NetconfCfgMgr
Ctrl Netconf Configuration Manager.
Definition netconf_cfg_mgr.h:121
isc::netconf::NetconfProcess::getNetconfCfgMgr
NetconfCfgMgrPtr getNetconfCfgMgr()
Returns a pointer to the configuration manager.
Definition netconf_process.cc:108
isc::netconf::NetconfProcess::init
void init() override final
Initialize the Netconf process.
Definition netconf_process.cc:35
isc::netconf::NetconfProcess::run
void run() override final
Implements the process's event loop.
Definition netconf_process.cc:39
isc::netconf::NetconfProcess::shutdown
isc::data::ConstElementPtr shutdown(isc::data::ConstElementPtr args) override final
Initiates the process's shutdown process.
Definition netconf_process.cc:78
isc::netconf::NetconfProcess::NetconfProcess
NetconfProcess(const char *name, const asiolink::IOServicePtr &io_service)
Constructor.
Definition netconf_process.cc:29
isc::netconf::NetconfProcess::configure
isc::data::ConstElementPtr configure(isc::data::ConstElementPtr config_set, bool check_only=false) override final
Processes the given configuration.
Definition netconf_process.cc:84
isc::process::DProcessBaseError
Exception thrown if the process encountered an operational error.
Definition d_process.h:24
isc::process::DProcessBase::setShutdownFlag
void setShutdownFlag(bool value)
Sets the process shut down flag to the given value.
Definition d_process.h:162
isc::process::DProcessBase::DProcessBase
DProcessBase(const char *app_name, asiolink::IOServicePtr io_service, DCfgMgrBasePtr cfg_mgr)
Constructor.
Definition d_process.h:87
isc::process::DProcessBase::stopIOService
void stopIOService()
Convenience method for stopping IOservice processing.
Definition d_process.h:184
isc::process::DProcessBase::shouldShutdown
bool shouldShutdown() const
Checks if the process has been instructed to shut down.
Definition d_process.h:155
isc::process::DProcessBase::getIOService
asiolink::IOServicePtr & getIOService()
Fetches the controller's IOService.
Definition d_process.h:176
isc::process::DProcessBase::getCfgMgr
DCfgMgrBasePtr & getCfgMgr()
Fetches the process's configuration manager.
Definition d_process.h:191
command_interpreter.h
This file contains several functions and constants that are used for handling commands and responses ...
isc_throw
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
Definition exceptions/exceptions.h:210
io_address.h
io_error.h
io_service_mgr.h
LOG_INFO
#define LOG_INFO(LOGGER, MESSAGE)
Macro to conveniently test info output and log it.
Definition macros.h:20
LOG_FATAL
#define LOG_FATAL(LOGGER, MESSAGE)
Macro to conveniently test fatal output and log it.
Definition macros.h:38
LOG_DEBUG
#define LOG_DEBUG(LOGGER, LEVEL, MESSAGE)
Macro to conveniently test debug output and log it.
Definition macros.h:14
isc::asiolink
Definition addr_utilities.cc:170
isc::asiolink::IOServicePtr
boost::shared_ptr< IOService > IOServicePtr
Defines a smart pointer to an IOService instance.
Definition io_service.h:35
isc::config
Definition command_interpreter.cc:23
isc::config::parseAnswer
ConstElementPtr parseAnswer(int &rcode, const ConstElementPtr &msg)
Parses a standard config/command level answer and returns arguments or text status code.
Definition command_interpreter.cc:96
isc::config::CONTROL_RESULT_ERROR
const int CONTROL_RESULT_ERROR
Status code indicating a general failure.
Definition command_interpreter.h:45
isc::config::createAnswer
ConstElementPtr createAnswer(const int status_code, const std::string &text, const ConstElementPtr &arg)
Creates a standard config/command level answer message.
Definition command_interpreter.cc:34
isc::config::CONTROL_RESULT_FATAL_ERROR
const int CONTROL_RESULT_FATAL_ERROR
Status code indicating that the command was unsuccessful and the configuration could not be reverted ...
Definition command_interpreter.h:63
isc::config::CONTROL_RESULT_SUCCESS
const int CONTROL_RESULT_SUCCESS
Status code indicating a successful operation.
Definition command_interpreter.h:42
isc::data
Definition base_stamped_element.cc:12
isc::data::ConstElementPtr
boost::shared_ptr< const Element > ConstElementPtr
Definition data.h:30
isc::http
Definition auth_config.h:19
isc::log::DBGLVL_START_SHUT
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
isc::netconf
Definition control_socket.cc:20
isc::netconf::NETCONF_FAILED
const isc::log::MessageID NETCONF_FAILED
Definition netconf_messages.h:18
isc::netconf::netconf_logger
isc::log::Logger netconf_logger(NETCONF_LOGGER_NAME)
Base logger for the netconf agent.
Definition netconf_log.h:49
isc::netconf::NETCONF_STARTED
const isc::log::MessageID NETCONF_STARTED
Definition netconf_messages.h:34
isc::netconf::NETCONF_RUN_EXIT
const isc::log::MessageID NETCONF_RUN_EXIT
Definition netconf_messages.h:30
isc::netconf::NetconfCfgMgrPtr
boost::shared_ptr< NetconfCfgMgr > NetconfCfgMgrPtr
Defines a shared pointer to NetconfCfgMgr.
Definition netconf_cfg_mgr.h:176
isc::process
Definition cb_ctl_base.h:22
isc::process::DCfgMgrBasePtr
boost::shared_ptr< DCfgMgrBase > DCfgMgrBasePtr
Defines a shared pointer to DCfgMgrBase.
Definition d_cfg_mgr.h:244
isc
Defines the logger used by the top-level component of kea-lfc.
Definition check_exists_add.cc:18
netconf.h
netconf_controller.h
netconf_log.h
Contains declarations for loggers used by the Kea netconf agent.
netconf_process.h
timeouts.h