From: Francis Dupont Date: Wed, 5 Jun 2019 20:49:06 +0000 (+0200) Subject: [630-cb-can-be-used-to-remove-mandatory-global-parameters] (checkpoint) Added applyDe... X-Git-Tag: Kea-1.6.0-beta2~98 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=715cbe4d0427b13d5268fdd3e8888504239d580c;p=thirdparty%2Fkea.git [630-cb-can-be-used-to-remove-mandatory-global-parameters] (checkpoint) Added applyDefaultsConfiguredGlobals --- diff --git a/src/lib/dhcpsrv/cb_ctl_dhcp4.cc b/src/lib/dhcpsrv/cb_ctl_dhcp4.cc index dccdc22cfd..b08a9dd0ac 100644 --- a/src/lib/dhcpsrv/cb_ctl_dhcp4.cc +++ b/src/lib/dhcpsrv/cb_ctl_dhcp4.cc @@ -8,6 +8,7 @@ #include #include #include +#include using namespace isc::db; using namespace isc::data; @@ -51,6 +52,9 @@ CBControlDHCPv4::databaseConfigApply(const BackendSelector& backend_selector, globals = getMgr().getPool()->getAllGlobalParameters4(backend_selector, server_selector); addGlobalsToConfig(external_cfg, globals); + // Add defaults. + external_cfg->applyDefaultsConfiguredGlobals(SimpleParser4::GLOBAL4_DEFAULTS); + // Now that we successfully fetched the new global parameters, let's // remove existing ones and merge them into the current configuration. cfg->clearConfiguredGlobals(); diff --git a/src/lib/dhcpsrv/cb_ctl_dhcp6.cc b/src/lib/dhcpsrv/cb_ctl_dhcp6.cc index 01c9383a3e..3e58da9c82 100644 --- a/src/lib/dhcpsrv/cb_ctl_dhcp6.cc +++ b/src/lib/dhcpsrv/cb_ctl_dhcp6.cc @@ -8,6 +8,7 @@ #include #include #include +#include using namespace isc::db; using namespace isc::data; @@ -50,6 +51,9 @@ CBControlDHCPv6::databaseConfigApply(const db::BackendSelector& backend_selector globals = getMgr().getPool()->getAllGlobalParameters6(backend_selector, server_selector); addGlobalsToConfig(external_cfg, globals); + // Add defaults. + external_cfg->applyDefaultsConfiguredGlobals(SimpleParser6::GLOBAL6_DEFAULTS); + // Now that we successfully fetched the new global parameters, let's // remove existing ones and merge them into the current configuration. cfg->clearConfiguredGlobals(); diff --git a/src/lib/dhcpsrv/srv_config.cc b/src/lib/dhcpsrv/srv_config.cc index 5914918f24..02b5fbbfa0 100644 --- a/src/lib/dhcpsrv/srv_config.cc +++ b/src/lib/dhcpsrv/srv_config.cc @@ -277,6 +277,75 @@ SrvConfig::clearConfiguredGlobals() { configured_globals_ = isc::data::Element::createMap(); } +void +SrvConfig::applyDefaultsConfiguredGlobals(const SimpleDefaults& defaults) { + // Code from SimpleParser::setDefaults + // This is the position representing a default value. As the values + // we're inserting here are not present in whatever the config file + // came from, we need to make sure it's clearly labeled as default. + const Element::Position pos("", 0, 0); + ConstElementPtr globals = getConfiguredGlobals(); + + // Let's go over all parameters we have defaults for. + for (auto def_value : defaults) { + + // Try if such a parameter is there. If it is, let's + // skip it, because user knows best *cough*. + ConstElementPtr x = globals->get(def_value.name_); + if (x) { + // There is such a value already, skip it. + continue; + } + + // There isn't such a value defined, let's create the default + // value... + switch (def_value.type_) { + case Element::string: { + x.reset(new StringElement(def_value.value_, pos)); + break; + } + case Element::integer: { + try { + int int_value = boost::lexical_cast(def_value.value_); + x.reset(new IntElement(int_value, pos)); + } + catch (const std::exception& ex) { + isc_throw(BadValue, + "Internal error. Integer value expected for: " + << def_value.name_ << ", value is: " + << def_value.value_ ); + } + + break; + } + case Element::boolean: { + bool bool_value; + if (def_value.value_ == std::string("true")) { + bool_value = true; + } else if (def_value.value_ == std::string("false")) { + bool_value = false; + } else { + isc_throw(BadValue, + "Internal error. Boolean value specified as " + << def_value.value_ << ", expected true or false"); + } + x.reset(new BoolElement(bool_value, pos)); + break; + } + case Element::real: { + double dbl_value = boost::lexical_cast(def_value.value_); + x.reset(new DoubleElement(dbl_value, pos)); + break; + } + default: + // No default values for null, list or map + isc_throw(BadValue, + "Internal error. Incorrect default value type."); + } + addConfiguredGlobal(def_value.name_, x); + } +} + void SrvConfig::extractConfiguredGlobals(isc::data::ConstElementPtr config) { if (config->getType() != Element::map) { diff --git a/src/lib/dhcpsrv/srv_config.h b/src/lib/dhcpsrv/srv_config.h index 3d957ea723..ba320d18fb 100644 --- a/src/lib/dhcpsrv/srv_config.h +++ b/src/lib/dhcpsrv/srv_config.h @@ -28,6 +28,7 @@ #include #include #include +#include #include #include #include @@ -610,8 +611,16 @@ public: } /// @brief Removes all configured global parameters. + /// @note This removes the default values too so either + /// @c applyDefaultsConfiguredGlobals and @c mergeGlobals, + /// or @c isc::data::SimpleParser::setDefaults and + /// @c extractConfiguredGlobals should be called after. void clearConfiguredGlobals(); + /// @brief Applies defaults to global parameters. + /// @param defaults vector of (name, type, value) defaults to apply. + void applyDefaultsConfiguredGlobals(const isc::data::SimpleDefaults& defaults); + /// @brief Saves scalar elements from the global scope of a configuration void extractConfiguredGlobals(isc::data::ConstElementPtr config);