From: Francis Dupont Date: Wed, 25 Jan 2017 15:06:26 +0000 (+0100) Subject: [master] Finished merge of trac5097 (migrate pool config) use SimpleParser templates X-Git-Tag: trac5119_base X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=1414dc6164ad690c6dac59a5ba1c4f0f3884557c;p=thirdparty%2Fkea.git [master] Finished merge of trac5097 (migrate pool config) use SimpleParser templates --- diff --git a/src/bin/dhcp6/json_config_parser.cc b/src/bin/dhcp6/json_config_parser.cc index 363b10ef6c..f7a7bb4939 100644 --- a/src/bin/dhcp6/json_config_parser.cc +++ b/src/bin/dhcp6/json_config_parser.cc @@ -199,9 +199,9 @@ public: // Check the pool parameters. It will throw an exception if any // of the required parameters are not present or invalid. - require_("prefix", pd_pool_); - require_("prefix-len", pd_pool_); - require_("delegated-len", pd_pool_); + requireParam("prefix", pd_pool_); + requireParam("prefix-len", pd_pool_); + requireParam("delegated-len", pd_pool_); try { // Attempt to construct the local pool. pool_.reset(new Pool6(IOAddress(addr_str), @@ -234,7 +234,7 @@ private: /// @param name Entry name /// @param config Pools configuration /// @throw isc::dhcp::DhcpConfigError if not present - void require_(const std::string& name, ConstElementPtr config) const { + void requireParam(const std::string& name, ConstElementPtr config) const { if (!config->contains(name)) { isc_throw(isc::dhcp::DhcpConfigError, "Missing parameter '" << name << "' (" diff --git a/src/lib/dhcpsrv/parsers/dhcp_parsers.cc b/src/lib/dhcpsrv/parsers/dhcp_parsers.cc index 4224aa2a1c..5aef682539 100644 --- a/src/lib/dhcpsrv/parsers/dhcp_parsers.cc +++ b/src/lib/dhcpsrv/parsers/dhcp_parsers.cc @@ -1202,78 +1202,54 @@ SubnetConfigParser::getOptionalParam(const std::string& name) { //**************************** D2ClientConfigParser ********************** -namespace { - -template int_type -getInt(const std::string& name, ConstElementPtr value) { - int64_t val_int = value->intValue(); - if ((val_int < std::numeric_limits::min()) || - (val_int > std::numeric_limits::max())) { - isc_throw(DhcpConfigError, "out of range value (" << val_int - << ") specified for parameter '" << name - << "' (" << value->getPosition() << ")"); - } - return (static_cast(val_int)); -} - uint32_t -getUint32(const std::string& name, ConstElementPtr value) { - return (getInt(name, value)); +D2ClientConfigParser::getUint32(const std::string& name, + ConstElementPtr value) const { + return (extractInt(name, value)); } +namespace { +IOAddress buildIOAddress(const std::string& str) { return (IOAddress(str)); } +}; + IOAddress -getIOAddress(const std::string& name, ConstElementPtr value) { - std::string str = value->stringValue(); - try { - return (IOAddress(str)); - } catch (const std::exception& ex) { - isc_throw(DhcpConfigError, "invalid address (" << str - << ") specified for parameter '" << name - << "' (" << value->getPosition() << ")"); - } +D2ClientConfigParser::getIOAddress(const std::string& name, + ConstElementPtr value) const { + return (extractConvert(name, "address", value)); } dhcp_ddns::NameChangeProtocol -getProtocol(const std::string& name, ConstElementPtr value) { - std::string str = value->stringValue(); - try { - return (dhcp_ddns::stringToNcrProtocol(str)); - } catch (const std::exception& ex) { - isc_throw(DhcpConfigError, - "invalid NameChangeRequest protocol (" << str - << ") specified for parameter '" << name - << "' (" << value->getPosition() << ")"); - } +D2ClientConfigParser::getProtocol(const std::string& name, + ConstElementPtr value) const { + return (extractConvert(name, + "NameChangeRequest protocol", + value)); } dhcp_ddns::NameChangeFormat -getFormat(const std::string& name, ConstElementPtr value) { - std::string str = value->stringValue(); - try { - return (dhcp_ddns::stringToNcrFormat(str)); - } catch (const std::exception& ex) { - isc_throw(DhcpConfigError, - "invalid NameChangeRequest format (" << str - << ") specified for parameter '" << name - << "' (" << value->getPosition() << ")"); - } +D2ClientConfigParser::getFormat(const std::string& name, + ConstElementPtr value) const { + return (extractConvert(name, + "NameChangeRequest format", + value)); } D2ClientConfig::ReplaceClientNameMode -getMode(const std::string& name, ConstElementPtr value) { - std::string str = value->stringValue(); - try { - return (D2ClientConfig::stringToReplaceClientNameMode(str)); - } catch (const std::exception& ex) { - isc_throw(DhcpConfigError, - "invalid ReplaceClientName mode (" << str - << ") specified for parameter '" << name - << "' (" << value->getPosition() << ")"); - } +D2ClientConfigParser::getMode(const std::string& name, + ConstElementPtr value) const { + return (extractConvert(name, + "ReplaceClientName mode", + value)); } -}; - D2ClientConfigPtr D2ClientConfigParser::parse(isc::data::ConstElementPtr client_config) { D2ClientConfigPtr new_config; diff --git a/src/lib/dhcpsrv/parsers/dhcp_parsers.h b/src/lib/dhcpsrv/parsers/dhcp_parsers.h index df88217f51..166b05224d 100644 --- a/src/lib/dhcpsrv/parsers/dhcp_parsers.h +++ b/src/lib/dhcpsrv/parsers/dhcp_parsers.h @@ -986,6 +986,57 @@ public: // to ElementPtr) /// @return number of parameters inserted static size_t setAllDefaults(isc::data::ConstElementPtr d2_config); + +private: + + /// @brief Returns a value converted to uint32_t + /// + /// Instantiation of extractInt() to uint32_t + /// + /// @param value value of the parameter + /// @return an uint32_t value + uint32_t + getUint32(const std::string& name, isc::data::ConstElementPtr value) const; + + /// @brief Returns a value converted to IOAddress + /// + /// Instantiation of extractConvert() to IOAddress + /// + /// @param value value of the parameter + /// @return an IOAddress value + isc::asiolink::IOAddress + getIOAddress(const std::string& name, + isc::data::ConstElementPtr value) const; + + /// @brief Returns a value converted to NameChangeProtocol + /// + /// Instantiation of extractInt() to NameChangeProtocol + /// + /// @param value value of the parameter + /// @return a NameChangeProtocol value + dhcp_ddns::NameChangeProtocol + getProtocol(const std::string& name, + isc::data::ConstElementPtr value) const; + + /// @brief Returns a value converted to NameChangeFormat + /// + /// Instantiation of extractConvert() to NameChangeFormat + /// + /// @param value value of the parameter + /// @return a NameChangeFormat value + dhcp_ddns::NameChangeFormat + getFormat(const std::string& name, + isc::data::ConstElementPtr value) const; + + /// @brief Returns a value converted to ReplaceClientNameMode + /// + /// Instantiation of extractConvert() to ReplaceClientNameMode + /// + /// @param value value of the parameter + /// @return a NameChangeFormat value + D2ClientConfig::ReplaceClientNameMode + getMode(const std::string& name, + isc::data::ConstElementPtr value) const; }; // Pointers to various parser objects.