From: Marcin Siodelski Date: Wed, 29 Aug 2018 08:52:41 +0000 (+0200) Subject: [#92,!13] Moved DbAccessParser to libkea-database. X-Git-Tag: gitlab116_base~31 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=11f9193ef8e8c816eb48dc4a479db8fe33033c26;p=thirdparty%2Fkea.git [#92,!13] Moved DbAccessParser to libkea-database. --- diff --git a/src/bin/dhcp4/json_config_parser.cc b/src/bin/dhcp4/json_config_parser.cc index d94e30916e..b8f4790754 100644 --- a/src/bin/dhcp4/json_config_parser.cc +++ b/src/bin/dhcp4/json_config_parser.cc @@ -429,25 +429,31 @@ configureDhcp4Server(Dhcpv4Srv& server, isc::data::ConstElementPtr config_set, // Please move at the end when migration will be finished. if (config_pair.first == "lease-database") { - DbAccessParser parser(DBType::LEASE_DB); + DbAccessParser parser; + std::string access_string; + parser.parse(access_string, config_pair.second); CfgDbAccessPtr cfg_db_access = srv_cfg->getCfgDbAccess(); - parser.parse(cfg_db_access, config_pair.second); + cfg_db_access->setLeaseDbAccessString(access_string); continue; } if (config_pair.first == "hosts-database") { - DbAccessParser parser(DBType::HOSTS_DB); + DbAccessParser parser; + std::string access_string; + parser.parse(access_string, config_pair.second); CfgDbAccessPtr cfg_db_access = srv_cfg->getCfgDbAccess(); - parser.parse(cfg_db_access, config_pair.second); + cfg_db_access->setHostDbAccessString(access_string); continue; } if (config_pair.first == "hosts-databases") { CfgDbAccessPtr cfg_db_access = srv_cfg->getCfgDbAccess(); - DbAccessParser parser(DBType::HOSTS_DB); + DbAccessParser parser; auto list = config_pair.second->listValue(); for (auto it : list) { - parser.parse(cfg_db_access, it); + std::string access_string; + parser.parse(access_string, it); + cfg_db_access->setHostDbAccessString(access_string); } continue; } diff --git a/src/bin/dhcp6/json_config_parser.cc b/src/bin/dhcp6/json_config_parser.cc index b6dbab69b1..261e586660 100644 --- a/src/bin/dhcp6/json_config_parser.cc +++ b/src/bin/dhcp6/json_config_parser.cc @@ -542,25 +542,31 @@ configureDhcp6Server(Dhcpv6Srv& server, isc::data::ConstElementPtr config_set, // Please move at the end when migration will be finished. if (config_pair.first == "lease-database") { - DbAccessParser parser(DBType::LEASE_DB); + DbAccessParser parser; + std::string access_string; + parser.parse(access_string, config_pair.second); CfgDbAccessPtr cfg_db_access = srv_config->getCfgDbAccess(); - parser.parse(cfg_db_access, config_pair.second); + cfg_db_access->setLeaseDbAccessString(access_string); continue; } if (config_pair.first == "hosts-database") { - DbAccessParser parser(DBType::HOSTS_DB); + DbAccessParser parser; + std::string access_string; + parser.parse(access_string, config_pair.second); CfgDbAccessPtr cfg_db_access = srv_config->getCfgDbAccess(); - parser.parse(cfg_db_access, config_pair.second); + cfg_db_access->setHostDbAccessString(access_string); continue; } if (config_pair.first == "hosts-databases") { CfgDbAccessPtr cfg_db_access = srv_config->getCfgDbAccess(); - DbAccessParser parser(DBType::HOSTS_DB); + DbAccessParser parser; auto list = config_pair.second->listValue(); for (auto it : list) { - parser.parse(cfg_db_access, it); + std::string access_string; + parser.parse(access_string, it); + cfg_db_access->setHostDbAccessString(access_string); } continue; } diff --git a/src/lib/dhcpsrv/parsers/dbaccess_parser.cc b/src/lib/dhcpsrv/parsers/dbaccess_parser.cc index 1665b74f7c..f87448d87b 100644 --- a/src/lib/dhcpsrv/parsers/dbaccess_parser.cc +++ b/src/lib/dhcpsrv/parsers/dbaccess_parser.cc @@ -31,13 +31,13 @@ namespace dhcp { // Factory function to build the parser -DbAccessParser::DbAccessParser(DBType db_type) - : values_(), type_(db_type) { +DbAccessParser::DbAccessParser() + : values_() { } // Parse the configuration and check that the various keywords are consistent. void -DbAccessParser::parse(CfgDbAccessPtr& cfg_db, +DbAccessParser::parse(std::string& access_string, ConstElementPtr database_config) { // To cope with incremental updates, the strategy is: @@ -122,8 +122,7 @@ DbAccessParser::parse(CfgDbAccessPtr& cfg_db, StringPairMap::const_iterator type_ptr = values_copy.find("type"); if (type_ptr == values_copy.end()) { isc_throw(DhcpConfigError, - (type_ == DBType::LEASE_DB ? "lease" : "host") - << " database access parameters must " + "database access parameters must " "include the keyword 'type' to determine type of database " "to be accessed (" << database_config->getPosition() << ")"); } @@ -212,11 +211,7 @@ DbAccessParser::parse(CfgDbAccessPtr& cfg_db, values_.swap(values_copy); // 5. Save the database access string in the Configuration Manager. - if (type_ == DBType::LEASE_DB) { - cfg_db->setLeaseDbAccessString(getDbAccessString()); - } else if (type_ == DBType::HOSTS_DB) { - cfg_db->setHostDbAccessString(getDbAccessString(), false); - } + access_string = getDbAccessString(); } // Create the database access string diff --git a/src/lib/dhcpsrv/parsers/dbaccess_parser.h b/src/lib/dhcpsrv/parsers/dbaccess_parser.h index e2013cc17e..c6356893d0 100644 --- a/src/lib/dhcpsrv/parsers/dbaccess_parser.h +++ b/src/lib/dhcpsrv/parsers/dbaccess_parser.h @@ -17,14 +17,12 @@ namespace isc { namespace dhcp { -/// @brief Parse Lease Database Parameters +/// @brief Parse Database Parameters /// -/// This class is the parser for the lease database configuration. This is a -/// map under the top-level "lease-database" element, and comprises a map of -/// strings. +/// This class is the parser for the database configuration. This is a +/// map under the top-level "lease-database", "host-database" and +/// "config-database" elements, and comprises a map of strings. /// -/// Only the "type" sub-element is mandatory: the remaining sub-elements -/// depend on the database chosen. class DbAccessParser: public isc::data::SimpleParser { public: @@ -35,9 +33,7 @@ public: typedef std::map StringPairMap; /// @brief Constructor - /// - /// @param db_type Specifies database type (lease or hosts) - explicit DbAccessParser(DBType db_type); + DbAccessParser(); /// The destructor. virtual ~DbAccessParser() @@ -53,17 +49,16 @@ public: /// - "connect-timeout" is a number from the range of 0 to 4294967295. /// - "port" is a number from the range of 0 to 65535. /// - /// Once all has been validated, constructs the database access string - /// expected by the lease manager. + /// Once all has been validated, constructs the database access string. /// - /// @param cfg_db The configuration where the access string will be set + /// @param [out] access_string Generated database access string. /// @param database_config The configuration value for the "*-database" /// identifier. /// /// @throw isc::dhcp::DhcpConfigError The 'type' keyword contains an /// unknown database type or is missing from the list of /// database access keywords. - void parse(isc::dhcp::CfgDbAccessPtr& cfg_db, + void parse(std::string& access_string, isc::data::ConstElementPtr database_config); @@ -91,8 +86,6 @@ protected: private: std::map values_; ///< Stored parameter values - - DBType type_; ///< Database type (leases or hosts) }; }; // namespace dhcp diff --git a/src/lib/dhcpsrv/tests/dbaccess_parser_unittest.cc b/src/lib/dhcpsrv/tests/dbaccess_parser_unittest.cc index 0b8f833f16..97d437a9d4 100644 --- a/src/lib/dhcpsrv/tests/dbaccess_parser_unittest.cc +++ b/src/lib/dhcpsrv/tests/dbaccess_parser_unittest.cc @@ -198,7 +198,7 @@ public: /// /// @brief Keyword/value collection of database access parameters TestDbAccessParser(DBType type) - : DbAccessParser(type) + : DbAccessParser(), type_(type) {} /// @brief Destructor @@ -206,9 +206,18 @@ public: {} /// @brief Parse configuration value + /// + /// @param database_config Configuration to be parsed. void parse(ConstElementPtr database_config) { CfgDbAccessPtr cfg_db(new CfgDbAccess()); - DbAccessParser::parse(cfg_db, database_config); + std::string db_access_string; + DbAccessParser::parse(db_access_string, database_config); + + if (type_ == DBType::LEASE_DB) { + cfg_db->setLeaseDbAccessString(db_access_string); + } else { + cfg_db->setHostDbAccessString(db_access_string); + } } /// Allow use of superclass's protected functions. @@ -234,6 +243,8 @@ public: std::string getDbAccessString() const { return (DbAccessParser::getDbAccessString()); } + + DBType type_; }; // Check that the parser works with a simple configuration.