Kea  2.3.5-git
bin/netconf/simple_parser.cc
Go to the documentation of this file.
1 // Copyright (C) 2018-2022 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 <cc/data.h>
10 #include <cc/dhcp_config_error.h>
11 #include <hooks/hooks_manager.h>
12 #include <hooks/hooks_parser.h>
13 #include <netconf/netconf_config.h>
14 #include <netconf/simple_parser.h>
15 
16 using namespace isc::data;
17 
18 namespace isc {
19 namespace netconf {
34 
38 const SimpleDefaults NetconfSimpleParser::NETCONF_DEFAULTS = {
39  { "boot-update", Element::boolean, "true" },
40  { "subscribe-changes", Element::boolean, "true" },
41  { "validate-changes", Element::boolean, "true" }
42 };
43 
45 const SimpleDefaults NetconfSimpleParser::CTRL_SOCK_DEFAULTS = {
46  { "socket-type", Element::string, "stdout" },
47  { "socket-name", Element::string, "" },
48  { "socket-url" , Element::string, "http://127.0.0.1:8000/" }
49 };
50 
52 const SimpleDefaults NetconfSimpleParser::DHCP4_DEFAULTS = {
53  { "model", Element::string, "kea-dhcp4-server" }
54 };
55 
57 const SimpleDefaults NetconfSimpleParser::DHCP6_DEFAULTS = {
58  { "model", Element::string, "kea-dhcp6-server" }
59 };
60 
62 const SimpleDefaults NetconfSimpleParser::D2_DEFAULTS = {
63  { "model", Element::string, "kea-dhcp-ddns" }
64 };
65 
67 const SimpleDefaults NetconfSimpleParser::CA_DEFAULTS = {
68  { "model", Element::string, "kea-ctrl-agent" }
69 };
70 
77 const ParamsList NetconfSimpleParser::INHERIT_TO_SERVERS = {
78  "boot-update",
79  "subscribe-changes",
80  "validate-changes"
81 };
82 
84 
88 
89 size_t NetconfSimpleParser::setAllDefaults(const ElementPtr& global) {
90  size_t cnt = 0;
91 
92  // Set global defaults first.
93  cnt = setDefaults(global, NETCONF_DEFAULTS);
94 
95  ConstElementPtr servers = global->get("managed-servers");
96  if (servers) {
97  ElementPtr mutable_servers(copy(servers, 0));
98  for (auto it : mutable_servers->mapValue()) {
99  ElementPtr server(copy(it.second, 0));
100  cnt += setServerDefaults(it.first, server);
101  mutable_servers->set(it.first, server);
102  }
103  global->set("managed-servers", mutable_servers);
104  }
105 
106  return (cnt);
107 }
108 
109 size_t NetconfSimpleParser::deriveParameters(ElementPtr global) {
110  size_t cnt = 0;
111 
112  // Now derive global parameters into managed-servers.
113  ConstElementPtr servers = global->get("managed-servers");
114  if (servers) {
115  ElementPtr mutable_servers(copy(servers, 0));
116  for (auto it : mutable_servers->mapValue()) {
117  ElementPtr mutable_server = copy(it.second, 0);
118  cnt += SimpleParser::deriveParams(global,
119  mutable_server,
120  INHERIT_TO_SERVERS);
121  mutable_servers->set(it.first, mutable_server);
122  }
123  global->set("managed-servers", mutable_servers);
124  }
125 
126  return (cnt);
127 }
128 
129 size_t
130 NetconfSimpleParser::setServerDefaults(const std::string name,
131  ElementPtr server) {
132  size_t cnt = 0;
133 
134  if (name == "dhcp4") {
135  cnt += setDefaults(server, DHCP4_DEFAULTS);
136  } else if (name == "dhcp6") {
137  cnt += setDefaults(server, DHCP6_DEFAULTS);
138  } else if (name == "d2") {
139  cnt += setDefaults(server, D2_DEFAULTS);
140  } else if (name == "ca") {
141  cnt += setDefaults(server, CA_DEFAULTS);
142  }
143 
144  ConstElementPtr ctrl_sock = server->get("control-socket");
145  if (!ctrl_sock) {
146  return (cnt);
147  }
148  ElementPtr mutable_ctrl_sock(copy(ctrl_sock, 0));
149  cnt += setDefaults(mutable_ctrl_sock, CTRL_SOCK_DEFAULTS);
150  server->set("control-socket", mutable_ctrl_sock);
151 
152  return (cnt);
153 }
154 
155 void
156 NetconfSimpleParser::parse(const NetconfConfigPtr& ctx,
157  const ElementPtr& config,
158  bool check_only) {
159 
160  // User context can be done at anytime.
161  ConstElementPtr user_context = config->get("user-context");
162  if (user_context) {
163  ctx->setContext(user_context);
164  }
165 
166  // get managed servers.
167  ConstElementPtr servers = config->get("managed-servers");
168  if (servers) {
169  for (auto it : servers->mapValue()) {
170  ServerConfigParser server_parser;
171  CfgServerPtr server = server_parser.parse(it.second);
172  ctx->getCfgServersMap()->insert(make_pair(it.first, server));
173  }
174  }
175 
176  // Finally, let's get the hook libs!
177  using namespace isc::hooks;
178  HooksConfig& libraries = ctx->getHooksConfig();
179  ConstElementPtr hooks = config->get("hooks-libraries");
180  if (hooks) {
181  HooksLibrariesParser hooks_parser;
182  hooks_parser.parse(libraries, hooks);
183  libraries.verifyLibraries(hooks->getPosition());
184  }
185 
186  if (!check_only) {
187  // This occurs last as if it succeeds, there is no easy way
188  // revert it. As a result, the failure to commit a subsequent
189  // change causes problems when trying to roll back.
190  HooksManager::prepareUnloadLibraries();
191  static_cast<void>(HooksManager::unloadLibraries());
192  libraries.loadLibraries();
193  }
194 }
195 
196 } // namespace netconf
197 } // 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
Wrapper class that holds hooks libraries configuration.
Definition: hooks_config.h:36
boost::shared_ptr< NetconfConfig > NetconfConfigPtr
Pointer to a configuration context.
static size_t deriveParams(isc::data::ConstElementPtr parent, isc::data::ElementPtr child, const ParamsList &params)
Derives (inherits) parameters from parent scope to a child.
ElementPtr copy(ConstElementPtr from, int level)
Copy the data up to a nesting level.
Definition: data.cc:1360
std::shared_ptr< CfgServer > CfgServerPtr
Defines a pointer for CfgServer instances.
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
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.