From: Marcin Siodelski Date: Thu, 5 Jan 2017 14:23:35 +0000 (+0100) Subject: [5030] Migrated Host Reservation related parsers to SimpleParser. X-Git-Tag: trac5035_base^2~1 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=1bd9870c363daf56f7cdf18148b4ddd0ac590422;p=thirdparty%2Fkea.git [5030] Migrated Host Reservation related parsers to SimpleParser. --- diff --git a/src/bin/dhcp4/json_config_parser.cc b/src/bin/dhcp4/json_config_parser.cc index d291e1444a..297bd0b513 100644 --- a/src/bin/dhcp4/json_config_parser.cc +++ b/src/bin/dhcp4/json_config_parser.cc @@ -152,9 +152,9 @@ public: // Parse Host Reservations for this subnet if any. ConstElementPtr reservations = subnet->get("reservations"); if (reservations) { - ParserPtr parser(new HostReservationsListParser< - HostReservationParser4>(subnet_->getID())); - parser->build(reservations); + HostReservationsListParser + parser(subnet_->getID()); + parser.parse(reservations); } } @@ -446,8 +446,7 @@ DhcpConfigParser* createGlobalDhcp4ConfigParser(const std::string& config_id, parser = new ExpirationConfigParser(); } else if (config_id.compare("client-classes") == 0) { parser = new ClientClassDefListParser(config_id, globalContext()); - } else if (config_id.compare("host-reservation-identifiers") == 0) { - parser = new HostReservationIdsParser4(); + // host-reservation-identifiers have been converted to SimpleParser already. } else { isc_throw(DhcpConfigError, "unsupported global configuration parameter: " @@ -637,6 +636,12 @@ configureDhcp4Server(Dhcpv4Srv&, isc::data::ConstElementPtr config_set) { continue; } + if (config_pair.first == "host-reservation-identifiers") { + HostReservationIdsParser4 parser; + parser.parse(config_pair.second); + continue; + } + ParserPtr parser(createGlobalDhcp4ConfigParser(config_pair.first, config_pair.second)); LOG_DEBUG(dhcp4_logger, DBG_DHCP4_DETAIL, DHCP4_PARSER_CREATED) diff --git a/src/bin/dhcp6/json_config_parser.cc b/src/bin/dhcp6/json_config_parser.cc index 04e1e0fcf0..0586898a81 100644 --- a/src/bin/dhcp6/json_config_parser.cc +++ b/src/bin/dhcp6/json_config_parser.cc @@ -385,9 +385,9 @@ public: // Parse Host Reservations for this subnet if any. ConstElementPtr reservations = subnet->get("reservations"); if (reservations) { - ParserPtr parser(new HostReservationsListParser< - HostReservationParser6>(subnet_->getID())); - parser->build(reservations); + HostReservationsListParser + parser(subnet_->getID()); + parser.parse(reservations); } } } @@ -731,8 +731,7 @@ DhcpConfigParser* createGlobal6DhcpConfigParser(const std::string& config_id, parser = new ClientClassDefListParser(config_id, globalContext()); } else if (config_id.compare("server-id") == 0) { parser = new DUIDConfigParser(); - } else if (config_id.compare("host-reservation-identifiers") == 0) { - parser = new HostReservationIdsParser6(); + // host-reservation-identifiers have been converted to SimpleParser already. } else { isc_throw(DhcpConfigError, "unsupported global configuration parameter: " @@ -911,6 +910,12 @@ configureDhcp6Server(Dhcpv6Srv&, isc::data::ConstElementPtr config_set) { continue; } + if (config_pair.first == "host-reservation-identifiers") { + HostReservationIdsParser6 parser; + parser.parse(config_pair.second); + continue; + } + ParserPtr parser(createGlobal6DhcpConfigParser(config_pair.first, config_pair.second)); LOG_DEBUG(dhcp6_logger, DBG_DHCP6_DETAIL, DHCP6_PARSER_CREATED) diff --git a/src/lib/dhcpsrv/parsers/host_reservation_parser.cc b/src/lib/dhcpsrv/parsers/host_reservation_parser.cc index 49c046edb3..17212d70c3 100644 --- a/src/lib/dhcpsrv/parsers/host_reservation_parser.cc +++ b/src/lib/dhcpsrv/parsers/host_reservation_parser.cc @@ -1,4 +1,4 @@ -// Copyright (C) 2014-2016 Internet Systems Consortium, Inc. ("ISC") +// Copyright (C) 2014-2017 Internet Systems Consortium, Inc. ("ISC") // // This Source Code Form is subject to the terms of the Mozilla Public // License, v. 2.0. If a copy of the MPL was not distributed with this @@ -95,11 +95,17 @@ namespace isc { namespace dhcp { HostReservationParser::HostReservationParser(const SubnetID& subnet_id) - : DhcpConfigParser(), subnet_id_(subnet_id) { + : SimpleParser(), subnet_id_(subnet_id) { +} + + +void +HostReservationParser::parse(isc::data::ConstElementPtr reservation_data) { + parseInternal(reservation_data); } void -HostReservationParser::build(isc::data::ConstElementPtr reservation_data) { +HostReservationParser::parseInternal(isc::data::ConstElementPtr reservation_data) { std::string identifier; std::string identifier_name; std::string hostname; @@ -185,8 +191,8 @@ HostReservationParser4::HostReservationParser4(const SubnetID& subnet_id) } void -HostReservationParser4::build(isc::data::ConstElementPtr reservation_data) { - HostReservationParser::build(reservation_data); +HostReservationParser4::parseInternal(isc::data::ConstElementPtr reservation_data) { + HostReservationParser::parseInternal(reservation_data); host_->setIPv4SubnetID(subnet_id_); @@ -247,8 +253,8 @@ HostReservationParser6::HostReservationParser6(const SubnetID& subnet_id) } void -HostReservationParser6::build(isc::data::ConstElementPtr reservation_data) { - HostReservationParser::build(reservation_data); +HostReservationParser6::parseInternal(isc::data::ConstElementPtr reservation_data) { + HostReservationParser::parseInternal(reservation_data); host_->setIPv6SubnetID(subnet_id_); @@ -359,7 +365,12 @@ HostReservationIdsParser::HostReservationIdsParser() } void -HostReservationIdsParser::build(isc::data::ConstElementPtr ids_list) { +HostReservationIdsParser::parse(isc::data::ConstElementPtr ids_list) { + parseInternal(ids_list); +} + +void +HostReservationIdsParser::parseInternal(isc::data::ConstElementPtr ids_list) { // Remove existing identifier types. staging_cfg_->clearIdentifierTypes(); diff --git a/src/lib/dhcpsrv/parsers/host_reservation_parser.h b/src/lib/dhcpsrv/parsers/host_reservation_parser.h index 42ded9c68f..13e6750b68 100644 --- a/src/lib/dhcpsrv/parsers/host_reservation_parser.h +++ b/src/lib/dhcpsrv/parsers/host_reservation_parser.h @@ -1,4 +1,4 @@ -// Copyright (C) 2014-2016 Internet Systems Consortium, Inc. ("ISC") +// Copyright (C) 2014-2017 Internet Systems Consortium, Inc. ("ISC") // // This Source Code Form is subject to the terms of the Mozilla Public // License, v. 2.0. If a copy of the MPL was not distributed with this @@ -8,21 +8,24 @@ #define HOST_RESERVATION_PARSER_H #include +#include #include -#include namespace isc { namespace dhcp { /// @brief Parser for a single host reservation entry. -class HostReservationParser : public DhcpConfigParser { +class HostReservationParser : public isc::data::SimpleParser { public: /// @brief Constructor. /// /// @param subnet_id Identifier of the subnet that the host is /// connected to. - HostReservationParser(const SubnetID& subnet_id); + explicit HostReservationParser(const SubnetID& subnet_id); + + /// @brief Destructor. + virtual ~HostReservationParser() { } /// @brief Parses a single entry for host reservation. /// @@ -30,13 +33,21 @@ public: /// reservation configuration. /// /// @throw DhcpConfigError If the configuration is invalid. - virtual void build(isc::data::ConstElementPtr reservation_data); - - /// @brief Commit, unused. - virtual void commit() { } + void parse(isc::data::ConstElementPtr reservation_data); protected: + /// @brief Parses a single entry for host reservation. + /// + /// This method is called by @ref parse and it can be overriden in the + /// derived classes to provide class specific parsing logic. + /// + /// @param reservation_data Data element holding map with a host + /// reservation configuration. + /// + /// @throw DhcpConfigError If the configuration is invalid. + virtual void parseInternal(isc::data::ConstElementPtr reservation_data); + /// @brief Inserts @c host_ object to the staging configuration. /// /// This method should be called by derived classes to insert the fully @@ -92,15 +103,15 @@ public: /// connected to. HostReservationParser4(const SubnetID& subnet_id); +protected: + /// @brief Parses a single host reservation for DHCPv4. /// /// @param reservation_data Data element holding map with a host /// reservation configuration. /// /// @throw DhcpConfigError If the configuration is invalid. - virtual void build(isc::data::ConstElementPtr reservation_data); - -protected: + virtual void parseInternal(isc::data::ConstElementPtr reservation_data); /// @brief Returns set of the supported parameters for DHCPv4. /// @@ -111,7 +122,6 @@ protected: /// @return Set of supported parameter names. virtual const std::set& getSupportedParameters(const bool identifiers_only) const; - }; /// @brief Parser for a single host reservation for DHCPv6. @@ -124,15 +134,15 @@ public: /// connected to. HostReservationParser6(const SubnetID& subnet_id); +protected: + /// @brief Parses a single host reservation for DHCPv6. /// /// @param reservation_data Data element holding map with a host /// reservation configuration. /// /// @throw DhcpConfigError If the configuration is invalid. - virtual void build(isc::data::ConstElementPtr reservation_data); - -protected: + virtual void parseInternal(isc::data::ConstElementPtr reservation_data); /// @brief Returns set of the supported parameters for DHCPv6. /// @@ -151,25 +161,36 @@ protected: /// This is a parent parser class for parsing "host-reservation-identifiers" /// global configuration parmeter. The DHCPv4 and DHCPv6 specific parsers /// derive from this class. -class HostReservationIdsParser : public DhcpConfigParser { +class HostReservationIdsParser : public isc::data::SimpleParser { public: /// @brief Constructor. HostReservationIdsParser(); + /// @brief Destructor. + virtual ~HostReservationIdsParser() { } + /// @brief Parses a list of host identifiers. /// /// @param ids_list Data element pointing to an ordered list of host /// identifier names. /// /// @throw DhcpConfigError If specified configuration is invalid. - virtual void build(isc::data::ConstElementPtr ids_list); - - /// @brief Commit, unused. - virtual void commit() { } + void parse(isc::data::ConstElementPtr ids_list); protected: + /// @brief Parses a list of host identifiers. + /// + /// This method is called by @ref parse and it can be overriden in the + /// derived classes to provide class specific parsing logic. + /// + /// @param ids_list Data element pointing to an ordered list of host + /// identifier names. + /// + /// @throw DhcpConfigError If specified configuration is invalid. + virtual void parseInternal(isc::data::ConstElementPtr ids_list); + /// @brief Checks if specified identifier name is supported in the /// context of the parser. /// diff --git a/src/lib/dhcpsrv/parsers/host_reservations_list_parser.h b/src/lib/dhcpsrv/parsers/host_reservations_list_parser.h index 86a4895d95..6795b273c4 100644 --- a/src/lib/dhcpsrv/parsers/host_reservations_list_parser.h +++ b/src/lib/dhcpsrv/parsers/host_reservations_list_parser.h @@ -1,4 +1,4 @@ -// Copyright (C) 2014-2015 Internet Systems Consortium, Inc. ("ISC") +// Copyright (C) 2014-2017 Internet Systems Consortium, Inc. ("ISC") // // This Source Code Form is subject to the terms of the Mozilla Public // License, v. 2.0. If a copy of the MPL was not distributed with this @@ -8,8 +8,8 @@ #define HOST_RESERVATIONS_LIST_PARSER_H #include +#include #include -#include #include namespace isc { @@ -21,7 +21,7 @@ namespace dhcp { /// parse individual reservations: @c HostReservationParser4 or /// @c HostReservationParser6. template -class HostReservationsListParser : public DhcpConfigParser { +class HostReservationsListParser : public isc::data::SimpleParser { public: /// @brief Constructor. @@ -39,16 +39,13 @@ public: /// /// @throw DhcpConfigError If the configuration if any of the reservations /// is invalid. - virtual void build(isc::data::ConstElementPtr hr_list) { + void parse(isc::data::ConstElementPtr hr_list) { BOOST_FOREACH(data::ConstElementPtr reservation, hr_list->listValue()) { - ParserPtr parser(new HostReservationParserType(subnet_id_)); - parser->build(reservation); + HostReservationParserType parser(subnet_id_); + parser.parse(reservation); } } - /// @brief Commit, unused. - virtual void commit() { } - private: /// @brief Identifier of the subnet to whic the reservations belong. diff --git a/src/lib/dhcpsrv/tests/host_reservation_parser_unittest.cc b/src/lib/dhcpsrv/tests/host_reservation_parser_unittest.cc index 5c72c91149..a4a65fa97e 100644 --- a/src/lib/dhcpsrv/tests/host_reservation_parser_unittest.cc +++ b/src/lib/dhcpsrv/tests/host_reservation_parser_unittest.cc @@ -1,4 +1,4 @@ -// Copyright (C) 2014-2016 Internet Systems Consortium, Inc. ("ISC") +// Copyright (C) 2014-2017 Internet Systems Consortium, Inc. ("ISC") // // This Source Code Form is subject to the terms of the Mozilla Public // License, v. 2.0. If a copy of the MPL was not distributed with this @@ -15,6 +15,7 @@ #include #include #include +#include #include #include #include @@ -116,7 +117,7 @@ protected: ElementPtr config_element = Element::fromJSON(config); ParserType parser(SubnetID(10)); - ASSERT_NO_THROW(parser.build(config_element)); + ASSERT_NO_THROW(parser.parse(config_element)); // Retrieve a host. HostCollection hosts; @@ -147,7 +148,7 @@ protected: ElementPtr config_element = Element::fromJSON(config.str()); HostReservationParser4 parser(SubnetID(10)); - ASSERT_NO_THROW(parser.build(config_element)); + ASSERT_NO_THROW(parser.parse(config_element)); CfgHostsPtr cfg_hosts = CfgMgr::instance().getStagingCfg()->getCfgHosts(); HostCollection hosts; @@ -172,7 +173,7 @@ protected: void testInvalidConfig(const std::string& config) const { ElementPtr config_element = Element::fromJSON(config); ParserType parser(SubnetID(10)); - EXPECT_THROW(parser.build(config_element), DhcpConfigError); + EXPECT_THROW(parser.parse(config_element), DhcpConfigError); } /// @brief HW Address object used by tests. @@ -286,7 +287,7 @@ TEST_F(HostReservationParserTest, dhcp4NoHostname) { ElementPtr config_element = Element::fromJSON(config); HostReservationParser4 parser(SubnetID(10)); - ASSERT_NO_THROW(parser.build(config_element)); + ASSERT_NO_THROW(parser.parse(config_element)); CfgHostsPtr cfg_hosts = CfgMgr::instance().getStagingCfg()->getCfgHosts(); HostCollection hosts; @@ -309,7 +310,7 @@ TEST_F(HostReservationParserTest, dhcp4ClientClasses) { ElementPtr config_element = Element::fromJSON(config); HostReservationParser4 parser(SubnetID(10)); - ASSERT_NO_THROW(parser.build(config_element)); + ASSERT_NO_THROW(parser.parse(config_element)); CfgHostsPtr cfg_hosts = CfgMgr::instance().getStagingCfg()->getCfgHosts(); HostCollection hosts; @@ -335,7 +336,7 @@ TEST_F(HostReservationParserTest, dhcp4MessageFields) { ElementPtr config_element = Element::fromJSON(config); HostReservationParser4 parser(SubnetID(10)); - ASSERT_NO_THROW(parser.build(config_element)); + ASSERT_NO_THROW(parser.parse(config_element)); CfgHostsPtr cfg_hosts = CfgMgr::instance().getStagingCfg()->getCfgHosts(); HostCollection hosts; @@ -422,7 +423,7 @@ TEST_F(HostReservationParserTest, noIPAddress) { ElementPtr config_element = Element::fromJSON(config); HostReservationParser4 parser(SubnetID(10)); - ASSERT_NO_THROW(parser.build(config_element)); + ASSERT_NO_THROW(parser.parse(config_element)); CfgHostsPtr cfg_hosts = CfgMgr::instance().getStagingCfg()->getCfgHosts(); HostCollection hosts; @@ -517,7 +518,7 @@ TEST_F(HostReservationParserTest, dhcp6HWaddr) { ElementPtr config_element = Element::fromJSON(config); HostReservationParser6 parser(SubnetID(10)); - ASSERT_NO_THROW(parser.build(config_element)); + ASSERT_NO_THROW(parser.parse(config_element)); CfgHostsPtr cfg_hosts = CfgMgr::instance().getStagingCfg()->getCfgHosts(); HostCollection hosts; @@ -564,7 +565,7 @@ TEST_F(HostReservationParserTest, dhcp6DUID) { ElementPtr config_element = Element::fromJSON(config); HostReservationParser6 parser(SubnetID(12)); - ASSERT_NO_THROW(parser.build(config_element)); + ASSERT_NO_THROW(parser.parse(config_element)); CfgHostsPtr cfg_hosts = CfgMgr::instance().getStagingCfg()->getCfgHosts(); HostCollection hosts; @@ -623,7 +624,7 @@ TEST_F(HostReservationParserTest, dhcp6NoHostname) { ElementPtr config_element = Element::fromJSON(config); HostReservationParser6 parser(SubnetID(12)); - ASSERT_NO_THROW(parser.build(config_element)); + ASSERT_NO_THROW(parser.parse(config_element)); CfgHostsPtr cfg_hosts = CfgMgr::instance().getStagingCfg()->getCfgHosts(); HostCollection hosts; @@ -659,7 +660,7 @@ TEST_F(HostReservationParserTest, dhcp6ClientClasses) { ElementPtr config_element = Element::fromJSON(config); HostReservationParser6 parser(SubnetID(10)); - ASSERT_NO_THROW(parser.build(config_element)); + ASSERT_NO_THROW(parser.parse(config_element)); CfgHostsPtr cfg_hosts = CfgMgr::instance().getStagingCfg()->getCfgHosts(); HostCollection hosts; @@ -770,7 +771,7 @@ TEST_F(HostReservationParserTest, options4) { ElementPtr config_element = Element::fromJSON(config); HostReservationParser4 parser(SubnetID(10)); - ASSERT_NO_THROW(parser.build(config_element)); + ASSERT_NO_THROW(parser.parse(config_element)); CfgHostsPtr cfg_hosts = CfgMgr::instance().getStagingCfg()->getCfgHosts(); HostCollection hosts; @@ -827,7 +828,7 @@ TEST_F(HostReservationParserTest, options6) { ElementPtr config_element = Element::fromJSON(config); HostReservationParser6 parser(SubnetID(10)); - ASSERT_NO_THROW(parser.build(config_element)); + ASSERT_NO_THROW(parser.parse(config_element)); // One host should have been added to the configuration. CfgHostsPtr cfg_hosts = CfgMgr::instance().getStagingCfg()->getCfgHosts(); @@ -981,7 +982,7 @@ public: void testInvalidConfig(const std::string& config) const { ElementPtr config_element = Element::fromJSON(config); ParserType parser; - EXPECT_THROW(parser.build(config_element), DhcpConfigError); + EXPECT_THROW(parser.parse(config_element), DhcpConfigError); } }; @@ -995,7 +996,7 @@ TEST_F(HostReservationIdsParserTest, dhcp4Identifiers) { ElementPtr config_element = Element::fromJSON(config); HostReservationIdsParser4 parser; - ASSERT_NO_THROW(parser.build(config_element)); + ASSERT_NO_THROW(parser.parse(config_element)); ConstCfgHostOperationsPtr cfg = CfgMgr::instance().getStagingCfg()-> getCfgHostOperations4(); @@ -1017,7 +1018,7 @@ TEST_F(HostReservationIdsParserTest, dhcp6Identifiers) { ElementPtr config_element = Element::fromJSON(config); HostReservationIdsParser6 parser; - ASSERT_NO_THROW(parser.build(config_element)); + ASSERT_NO_THROW(parser.parse(config_element)); ConstCfgHostOperationsPtr cfg = CfgMgr::instance().getStagingCfg()-> getCfgHostOperations6(); @@ -1051,7 +1052,7 @@ TEST_F(HostReservationIdsParserTest, dhcp4AutoIdentifiers) { ElementPtr config_element = Element::fromJSON(config); HostReservationIdsParser4 parser; - ASSERT_NO_THROW(parser.build(config_element)); + ASSERT_NO_THROW(parser.parse(config_element)); ConstCfgHostOperationsPtr cfg = CfgMgr::instance().getStagingCfg()-> getCfgHostOperations4(); @@ -1094,7 +1095,7 @@ TEST_F(HostReservationIdsParserTest, dhcp6AutoIdentifiers) { ElementPtr config_element = Element::fromJSON(config); HostReservationIdsParser6 parser; - ASSERT_NO_THROW(parser.build(config_element)); + ASSERT_NO_THROW(parser.parse(config_element)); ConstCfgHostOperationsPtr cfg = CfgMgr::instance().getStagingCfg()-> getCfgHostOperations6(); diff --git a/src/lib/dhcpsrv/tests/host_reservations_list_parser_unittest.cc b/src/lib/dhcpsrv/tests/host_reservations_list_parser_unittest.cc index 0103793a71..f59e01d6aa 100644 --- a/src/lib/dhcpsrv/tests/host_reservations_list_parser_unittest.cc +++ b/src/lib/dhcpsrv/tests/host_reservations_list_parser_unittest.cc @@ -1,4 +1,4 @@ -// Copyright (C) 2014-2016 Internet Systems Consortium, Inc. ("ISC") +// Copyright (C) 2014-2017 Internet Systems Consortium, Inc. ("ISC") // // This Source Code Form is subject to the terms of the Mozilla Public // License, v. 2.0. If a copy of the MPL was not distributed with this @@ -13,6 +13,7 @@ #include #include #include +#include #include #include #include @@ -86,7 +87,7 @@ TEST_F(HostReservationsListParserTest, ipv4Reservations) { ElementPtr config_element = Element::fromJSON(config); HostReservationsListParser parser(SubnetID(1)); - ASSERT_NO_THROW(parser.build(config_element)); + ASSERT_NO_THROW(parser.parse(config_element)); CfgHostsPtr cfg_hosts = CfgMgr::instance().getStagingCfg()->getCfgHosts(); HostCollection hosts; @@ -139,7 +140,7 @@ TEST_F(HostReservationsListParserTest, duplicatedIdentifierValue4) { ElementPtr config_element = Element::fromJSON(config.str()); HostReservationsListParser parser(SubnetID(1)); - EXPECT_THROW(parser.build(config_element), DhcpConfigError); + EXPECT_THROW(parser.parse(config_element), DhcpConfigError); } } @@ -164,7 +165,7 @@ TEST_F(HostReservationsListParserTest, ipv6Reservations) { // Parse configuration. HostReservationsListParser parser(SubnetID(2)); - ASSERT_NO_THROW(parser.build(config_element)); + ASSERT_NO_THROW(parser.parse(config_element)); CfgHostsPtr cfg_hosts = CfgMgr::instance().getStagingCfg()->getCfgHosts(); HostCollection hosts; @@ -231,7 +232,7 @@ TEST_F(HostReservationsListParserTest, duplicatedIdentifierValue6) { ElementPtr config_element = Element::fromJSON(config.str()); HostReservationsListParser parser(SubnetID(1)); - EXPECT_THROW(parser.build(config_element), DhcpConfigError); + EXPECT_THROW(parser.parse(config_element), DhcpConfigError); } }