Kea: simple_parser.cc Source File

| Kea 2.2.0

bin/netconf/simple_parser.cc
Go to the documentation of this file.
1// Copyright (C) 2018-2021 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 <netconf/simple_parser.h>
10#include <netconf/netconf_config.h>
11#include <cc/data.h>
12#include <cc/dhcp_config_error.h>
13#include <hooks/hooks_manager.h>
14#include <hooks/hooks_parser.h>
15#include <boost/foreach.hpp>
16
17using namespace isc::data;
18
19namespace isc {
20namespace netconf {
35
39const SimpleDefaults NetconfSimpleParser::NETCONF_DEFAULTS = {
40 { "boot-update", Element::boolean, "true" },
41 { "subscribe-changes", Element::boolean, "true" },
42 { "validate-changes", Element::boolean, "true" }
43};
44
46const SimpleDefaults NetconfSimpleParser::CTRL_SOCK_DEFAULTS = {
47 { "socket-type", Element::string, "stdout" },
48 { "socket-name", Element::string, "" },
49 { "socket-url" , Element::string, "http://127.0.0.1:8000/" }
50};
51
53const SimpleDefaults NetconfSimpleParser::DHCP4_DEFAULTS = {
54 { "model", Element::string, "kea-dhcp4-server" }
55};
56
58const SimpleDefaults NetconfSimpleParser::DHCP6_DEFAULTS = {
59 { "model", Element::string, "kea-dhcp6-server" }
60};
61
63const SimpleDefaults NetconfSimpleParser::D2_DEFAULTS = {
64 { "model", Element::string, "kea-dhcp-ddns" }
65};
66
68const SimpleDefaults NetconfSimpleParser::CA_DEFAULTS = {
69 { "model", Element::string, "kea-ctrl-agent" }
70};
71
78const ParamsList NetconfSimpleParser::INHERIT_TO_SERVERS = {
79 "boot-update",
80 "subscribe-changes",
81 "validate-changes"
82};
83
85
89
90 size_t NetconfSimpleParser::setAllDefaults(const ElementPtr& global) {
91 size_t cnt = 0;
92
93 // Set global defaults first.
94 cnt = setDefaults(global, NETCONF_DEFAULTS);
95
96 ConstElementPtr servers = global->get("managed-servers");
97 if (servers) {
98 for (auto it : servers->mapValue()) {
99 cnt += setServerDefaults(it.first, it.second);
100 }
101 }
102
103 return (cnt);
104}
105
106 size_t NetconfSimpleParser::deriveParameters(ConstElementPtr global) {
107 size_t cnt = 0;
108
109 // Now derive global parameters into managed-servers.
110 ConstElementPtr servers = global->get("managed-servers");
111 if (servers) {
112 for (auto it : servers->mapValue()) {
113 ElementPtr mutable_server =
114 boost::const_pointer_cast<Element>(it.second);
115 cnt += SimpleParser::deriveParams(global,
116 mutable_server,
117 INHERIT_TO_SERVERS);
118 }
119 }
120
121 return (cnt);
122}
123
124size_t
125NetconfSimpleParser::setServerDefaults(const std::string name,
126 ConstElementPtr server) {
127 size_t cnt = 0;
128
129 ElementPtr mutable_server =
130 boost::const_pointer_cast<Element>(server);
131 if (name == "dhcp4") {
132 cnt += setDefaults(mutable_server, DHCP4_DEFAULTS);
133 } else if (name == "dhcp6") {
134 cnt += setDefaults(mutable_server, DHCP6_DEFAULTS);
135 } else if (name == "d2") {
136 cnt += setDefaults(mutable_server, D2_DEFAULTS);
137 } else if (name == "ca") {
138 cnt += setDefaults(mutable_server, CA_DEFAULTS);
139 }
140
141 ConstElementPtr ctrl_sock = server->get("control-socket");
142 if (!ctrl_sock) {
143 return (cnt);
144 }
145 ElementPtr mutable_ctrl_sock =
146 boost::const_pointer_cast<Element>(ctrl_sock);
147 cnt += setDefaults(mutable_ctrl_sock, CTRL_SOCK_DEFAULTS);
148
149 return (cnt);
150}
151
152void
153NetconfSimpleParser::parse(const NetconfConfigPtr& ctx,
154 const ConstElementPtr& config,
155 bool check_only) {
156
157 // User context can be done at anytime.
158 ConstElementPtr user_context = config->get("user-context");
159 if (user_context) {
160 ctx->setContext(user_context);
161 }
162
163 // get managed servers.
164 ConstElementPtr servers = config->get("managed-servers");
165 if (servers) {
166 for (auto it : servers->mapValue()) {
167 ServerConfigParser server_parser;
168 CfgServerPtr server = server_parser.parse(it.second);
169 ctx->getCfgServersMap()->insert(make_pair(it.first, server));
170 }
171 }
172
173 // Finally, let's get the hook libs!
174 using namespace isc::hooks;
175 HooksConfig& libraries = ctx->getHooksConfig();
176 ConstElementPtr hooks = config->get("hooks-libraries");
177 if (hooks) {
178 HooksLibrariesParser hooks_parser;
179 hooks_parser.parse(libraries, hooks);
180 libraries.verifyLibraries(hooks->getPosition());
181 }
182
183 if (!check_only) {
184 // This occurs last as if it succeeds, there is no easy way
185 // revert it. As a result, the failure to commit a subsequent
186 // change causes problems when trying to roll back.
187 HooksManager::prepareUnloadLibraries();
188 static_cast<void>(HooksManager::unloadLibraries());
189 libraries.loadLibraries();
190 }
191}
192
193} // namespace netconf
194} // namespace isc
simple_parser.h
isc::data::SimpleParser::setDefaults
static size_t setDefaults(isc::data::ElementPtr scope, const SimpleDefaults &default_values)
Sets the default values.
Definition: lib/cc/simple_parser.cc:171
isc::hooks::HooksConfig
Wrapper class that holds hooks libraries configuration.
Definition: hooks_config.h:36
isc::hooks::HooksConfig::get
const isc::hooks::HookLibsCollection & get() const
Provides access to the configured hooks libraries.
Definition: hooks_config.h:54
isc::hooks::HooksConfig::verifyLibraries
void verifyLibraries(const isc::data::Element::Position &position) const
Verifies that libraries stored in libraries_ are valid.
Definition: hooks_config.cc:20
isc::hooks::HooksConfig::loadLibraries
void loadLibraries() const
Commits hooks libraries configuration.
Definition: hooks_config.cc:55
isc::hooks::HooksLibrariesParser
Parser for hooks library list.
Definition: hooks_parser.h:21
isc::hooks::HooksLibrariesParser::parse
void parse(HooksConfig &libraries, isc::data::ConstElementPtr value)
Parses parameters value.
Definition: hooks_parser.cc:28
isc::netconf::NetconfSimpleParser::NETCONF_DEFAULTS
static const isc::data::SimpleDefaults NETCONF_DEFAULTS
This table defines default values for global options.
Definition: bin/netconf/simple_parser.h:63
isc::netconf::NetconfSimpleParser::DHCP6_DEFAULTS
static const isc::data::SimpleDefaults DHCP6_DEFAULTS
Supplies defaults for dhcp6 managed server.
Definition: bin/netconf/simple_parser.h:66
isc::netconf::NetconfSimpleParser::DHCP4_DEFAULTS
static const isc::data::SimpleDefaults DHCP4_DEFAULTS
Supplies defaults for dhcp4 managed server.
Definition: bin/netconf/simple_parser.h:65
isc::netconf::NetconfSimpleParser::setAllDefaults
static size_t setAllDefaults(const isc::data::ElementPtr &global)
Sets all defaults for Netconf configuration.
Definition: bin/netconf/simple_parser.cc:90
isc::netconf::NetconfSimpleParser::CA_DEFAULTS
static const isc::data::SimpleDefaults CA_DEFAULTS
Supplies defaults for ca managed server.
Definition: bin/netconf/simple_parser.h:68
isc::netconf::NetconfSimpleParser::INHERIT_TO_SERVERS
static const isc::data::ParamsList INHERIT_TO_SERVERS
List of parameters that can be inherited to managed-servers scope.
Definition: bin/netconf/simple_parser.h:69
isc::netconf::NetconfSimpleParser::parse
void parse(const NetconfConfigPtr &ctx, const isc::data::ConstElementPtr &config, bool check_only)
Parses the netconf configuration.
Definition: bin/netconf/simple_parser.cc:153
isc::netconf::NetconfSimpleParser::setServerDefaults
static size_t setServerDefaults(const std::string name, isc::data::ConstElementPtr server)
Adds default values to a Managed server entry.
Definition: bin/netconf/simple_parser.cc:125
isc::netconf::NetconfSimpleParser::deriveParameters
static size_t deriveParameters(isc::data::ConstElementPtr global)
Derives (inherits) all parameters from global to more specific scopes.
Definition: bin/netconf/simple_parser.cc:106
isc::netconf::NetconfSimpleParser::CTRL_SOCK_DEFAULTS
static const isc::data::SimpleDefaults CTRL_SOCK_DEFAULTS
Supplies defaults for control-socket elements.
Definition: bin/netconf/simple_parser.h:64
isc::netconf::NetconfSimpleParser::D2_DEFAULTS
static const isc::data::SimpleDefaults D2_DEFAULTS
Supplies defaults for d2 managed server.
Definition: bin/netconf/simple_parser.h:67
isc::netconf::ServerConfigParser
Parser for CfgServer.
Definition: netconf_config.h:312
isc::netconf::ServerConfigParser::parse
CfgServerPtr parse(data::ConstElementPtr server_config)
Performs the actual parsing of the given value from the "managed-servers" map.
Definition: netconf_config.cc:180
data.h
dhcp_config_error.h
hooks_manager.h
hooks_parser.h
isc::data
Definition: base_stamped_element.cc:12
isc::data::ParamsList
std::vector< std::string > ParamsList
This defines a list of all parameters that are derived (or inherited) between contexts.
Definition: lib/cc/simple_parser.h:43
isc::data::ConstElementPtr
boost::shared_ptr< const Element > ConstElementPtr
Definition: data.h:27
isc::data::SimpleDefaults
std::vector< SimpleDefault > SimpleDefaults
This specifies all default values in a given scope (e.g. a subnet).
Definition: lib/cc/simple_parser.h:39
isc::data::ElementPtr
boost::shared_ptr< Element > ElementPtr
Definition: data.h:24
isc::hooks
Definition: callout_handle.cc:21
isc::netconf::NetconfConfigPtr
boost::shared_ptr< NetconfConfig > NetconfConfigPtr
Pointer to a configuration context.
Definition: netconf_cfg_mgr.h:21
isc::netconf::CfgServerPtr
boost::shared_ptr< CfgServer > CfgServerPtr
Defines a pointer for CfgServer instances.
Definition: netconf_config.h:273
isc
Defines the logger used by the top-level component of kea-lfc.
Definition: agent_parser.cc:143
netconf_config.h
A collection of classes for housing and parsing the application configuration necessary for the Netco...