Kea  2.1.7-git
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 
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 
17 using namespace isc::data;
18 
19 namespace isc {
20 namespace netconf {
35 
39 const SimpleDefaults NetconfSimpleParser::NETCONF_DEFAULTS = {
40  { "boot-update", Element::boolean, "true" },
41  { "subscribe-changes", Element::boolean, "true" },
42  { "validate-changes", Element::boolean, "true" }
43 };
44 
46 const 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 
53 const SimpleDefaults NetconfSimpleParser::DHCP4_DEFAULTS = {
54  { "model", Element::string, "kea-dhcp4-server" }
55 };
56 
58 const SimpleDefaults NetconfSimpleParser::DHCP6_DEFAULTS = {
59  { "model", Element::string, "kea-dhcp6-server" }
60 };
61 
63 const SimpleDefaults NetconfSimpleParser::D2_DEFAULTS = {
64  { "model", Element::string, "kea-dhcp-ddns" }
65 };
66 
68 const SimpleDefaults NetconfSimpleParser::CA_DEFAULTS = {
69  { "model", Element::string, "kea-ctrl-agent" }
70 };
71 
78 const 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 
124 size_t
125 NetconfSimpleParser::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 
152 void
153 NetconfSimpleParser::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
Parser for hooks library list.
Definition: hooks_parser.h:21
std::vector< SimpleDefault > SimpleDefaults
This specifies all default values in a given scope (e.g. a subnet).
boost::shared_ptr< Element > ElementPtr
Definition: data.h:24
boost::shared_ptr< CfgServer > CfgServerPtr
Defines a pointer for CfgServer instances.
Wrapper class that holds hooks libraries configuration.
Definition: hooks_config.h:36
static size_t deriveParams(isc::data::ConstElementPtr parent, isc::data::ElementPtr child, const ParamsList &params)
Derives (inherits) parameters from parent scope to a child.
A collection of classes for housing and parsing the application configuration necessary for the Netco...
boost::shared_ptr< const Element > ConstElementPtr
Definition: data.h:27
void verifyLibraries(const isc::data::Element::Position &position) const
Verifies that libraries stored in libraries_ are valid.
Definition: hooks_config.cc:20
void loadLibraries() const
Commits hooks libraries configuration.
Definition: hooks_config.cc:55
Defines the logger used by the top-level component of kea-lfc.
std::vector< std::string > ParamsList
This defines a list of all parameters that are derived (or inherited) between contexts.
const isc::hooks::HookLibsCollection & get() const
Provides access to the configured hooks libraries.
Definition: hooks_config.h:54
The Element class represents a piece of data, used by the command channel and configuration parts...
Definition: data.h:70
void parse(HooksConfig &libraries, isc::data::ConstElementPtr value)
Parses parameters value.
Definition: hooks_parser.cc:28
CfgServerPtr parse(data::ConstElementPtr server_config)
Performs the actual parsing of the given value from the "managed-servers" map.
boost::shared_ptr< NetconfConfig > NetconfConfigPtr
Pointer to a configuration context.