Kea  1.5.0
bin/netconf/simple_parser.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>
8 
10 #include <netconf/netconf_config.h>
11 #include <cc/data.h>
12 #include <cc/dhcp_config_error.h>
13 #include <hooks/hooks_parser.h>
14 #include <boost/foreach.hpp>
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  for (auto it : servers->mapValue()) {
98  cnt += setServerDefaults(it.first, it.second);
99  }
100  }
101 
102  return (cnt);
103 }
104 
105 size_t NetconfSimpleParser::deriveParameters(ConstElementPtr global) {
106  size_t cnt = 0;
107 
108  // Now derive global parameters into managed-servers.
109  ConstElementPtr servers = global->get("managed-servers");
110  if (servers) {
111  for (auto it : servers->mapValue()) {
112  ElementPtr mutable_server =
113  boost::const_pointer_cast<Element>(it.second);
114  cnt += SimpleParser::deriveParams(global,
115  mutable_server,
116  INHERIT_TO_SERVERS);
117  }
118  }
119 
120  return (cnt);
121 }
122 
123 size_t
124 NetconfSimpleParser::setServerDefaults(const std::string name,
125  ConstElementPtr server) {
126  size_t cnt = 0;
127 
128  ElementPtr mutable_server =
129  boost::const_pointer_cast<Element>(server);
130  if (name == "dhcp4") {
131  cnt += setDefaults(mutable_server, DHCP4_DEFAULTS);
132  } else if (name == "dhcp6") {
133  cnt += setDefaults(mutable_server, DHCP6_DEFAULTS);
134  } else if (name == "d2") {
135  cnt += setDefaults(mutable_server, D2_DEFAULTS);
136  } else if (name == "ca") {
137  cnt += setDefaults(mutable_server, CA_DEFAULTS);
138  }
139 
140  ConstElementPtr ctrl_sock = server->get("control-socket");
141  if (!ctrl_sock) {
142  return (cnt);
143  }
144  ElementPtr mutable_ctrl_sock =
145  boost::const_pointer_cast<Element>(ctrl_sock);
146  cnt += setDefaults(mutable_ctrl_sock, CTRL_SOCK_DEFAULTS);
147 
148  return (cnt);
149 }
150 
151 void
152 NetconfSimpleParser::parse(const NetconfConfigPtr& ctx,
153  const ConstElementPtr& config,
154  bool check_only) {
155 
156  // User context can be done at anytime.
157  ConstElementPtr user_context = config->get("user-context");
158  if (user_context) {
159  ctx->setContext(user_context);
160  }
161 
162  // get managed servers.
163  ConstElementPtr servers = config->get("managed-servers");
164  if (servers) {
165  for (auto it : servers->mapValue()) {
166  ServerConfigParser server_parser;
167  CfgServerPtr server = server_parser.parse(it.second);
168  ctx->getCfgServersMap()->insert(make_pair(it.first, server));
169  }
170  }
171 
172  // Finally, let's get the hook libs!
173  using namespace isc::hooks;
174  HooksConfig& libraries = ctx->getHooksConfig();
175  ConstElementPtr hooks = config->get("hooks-libraries");
176  if (hooks) {
177  HooksLibrariesParser hooks_parser;
178  hooks_parser.parse(libraries, hooks);
179  libraries.verifyLibraries(hooks->getPosition());
180  }
181 
182  if (!check_only) {
183  // This occurs last as if it succeeds, there is no easy way
184  // revert it. As a result, the failure to commit a subsequent
185  // change causes problems when trying to roll back.
186  libraries.loadLibraries();
187  }
188 }
189 
190 };
191 };
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:185
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
simple_parser.h
hooks_parser.h
dhcp_config_error.h
isc::hooks::HooksConfig::get
const isc::hooks::HookLibsCollection & get() const
Provides access to the configured hooks libraries.
Definition: hooks_config.h:54
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:31
isc::data
Definition: cfg_to_element.h:25
isc::hooks::HooksLibrariesParser
Parser for hooks library list.
Definition: hooks_parser.h:21
isc
Defines the logger used by the top-level component of kea-dhcp-ddns.
Definition: agent_parser.cc:144
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:35
isc::netconf::CfgServerPtr
boost::shared_ptr< CfgServer > CfgServerPtr
Defines a pointer for CfgServer instances.
Definition: netconf_config.h:252
isc::hooks::HooksConfig
Wrapper class that holds hooks libraries configuration.
Definition: hooks_config.h:36
isc::hooks::HooksConfig::loadLibraries
void loadLibraries() const
Commits hooks libraries configuration.
Definition: hooks_config.cc:55
isc::netconf::NetconfConfigPtr
boost::shared_ptr< NetconfConfig > NetconfConfigPtr
Pointer to a configuration context.
Definition: netconf_cfg_mgr.h:21
netconf_config.h
A collection of classes for housing and parsing the application configuration necessary for the Netco...
isc::hooks::HooksLibrariesParser::parse
void parse(HooksConfig &libraries, isc::data::ConstElementPtr value)
Parses parameters value.
Definition: hooks_parser.cc:28
isc::data::SimpleParser::deriveParams
static size_t deriveParams(isc::data::ConstElementPtr parent, isc::data::ElementPtr child, const ParamsList &params)
Derives (inherits) parameters from parent scope to a child.
Definition: lib/cc/simple_parser.cc:183
isc::data::Element::boolean
@ boolean
Definition: data.h:151
isc::data::Element::string
@ string
Definition: data.h:151
data.h
isc::netconf::ServerConfigParser
Parser for CfgServer.
Definition: netconf_config.h:291
isc::hooks
Definition: callout_handle.cc:21
isc::data::ElementPtr
boost::shared_ptr< Element > ElementPtr
Definition: data.h:20
isc::data::ConstElementPtr
boost::shared_ptr< const Element > ConstElementPtr
Definition: data.h:23