From 7e66a9d1121268dd5ad95ac95c5faff3bc3b379c Mon Sep 17 00:00:00 2001 From: Marcin Siodelski Date: Thu, 5 Jan 2017 21:35:11 +0100 Subject: [PATCH] [5030] SubnetID a parameter of parse method rather than ctor. --- src/bin/dhcp4/json_config_parser.cc | 5 +- src/bin/dhcp6/json_config_parser.cc | 5 +- .../parsers/host_reservation_parser.cc | 35 +++++-------- .../dhcpsrv/parsers/host_reservation_parser.h | 45 ++++++---------- .../parsers/host_reservations_list_parser.h | 20 ++----- .../tests/host_reservation_parser_unittest.cc | 52 +++++++++---------- .../host_reservations_list_parser_unittest.cc | 16 +++--- 7 files changed, 71 insertions(+), 107 deletions(-) diff --git a/src/bin/dhcp4/json_config_parser.cc b/src/bin/dhcp4/json_config_parser.cc index 297bd0b513..8791d44ed1 100644 --- a/src/bin/dhcp4/json_config_parser.cc +++ b/src/bin/dhcp4/json_config_parser.cc @@ -152,9 +152,8 @@ public: // Parse Host Reservations for this subnet if any. ConstElementPtr reservations = subnet->get("reservations"); if (reservations) { - HostReservationsListParser - parser(subnet_->getID()); - parser.parse(reservations); + HostReservationsListParser parser; + parser.parse(subnet_->getID(), reservations); } } diff --git a/src/bin/dhcp6/json_config_parser.cc b/src/bin/dhcp6/json_config_parser.cc index 0586898a81..b14d7c8c77 100644 --- a/src/bin/dhcp6/json_config_parser.cc +++ b/src/bin/dhcp6/json_config_parser.cc @@ -385,9 +385,8 @@ public: // Parse Host Reservations for this subnet if any. ConstElementPtr reservations = subnet->get("reservations"); if (reservations) { - HostReservationsListParser - parser(subnet_->getID()); - parser.parse(reservations); + HostReservationsListParser parser; + parser.parse(subnet_->getID(), reservations); } } } diff --git a/src/lib/dhcpsrv/parsers/host_reservation_parser.cc b/src/lib/dhcpsrv/parsers/host_reservation_parser.cc index 17212d70c3..19426fd9de 100644 --- a/src/lib/dhcpsrv/parsers/host_reservation_parser.cc +++ b/src/lib/dhcpsrv/parsers/host_reservation_parser.cc @@ -94,18 +94,15 @@ getSupportedParams6(const bool identifiers_only = false) { namespace isc { namespace dhcp { -HostReservationParser::HostReservationParser(const SubnetID& subnet_id) - : SimpleParser(), subnet_id_(subnet_id) { -} - - void -HostReservationParser::parse(isc::data::ConstElementPtr reservation_data) { - parseInternal(reservation_data); +HostReservationParser::parse(const SubnetID& subnet_id, + isc::data::ConstElementPtr reservation_data) { + parseInternal(subnet_id, reservation_data); } void -HostReservationParser::parseInternal(isc::data::ConstElementPtr reservation_data) { +HostReservationParser::parseInternal(const SubnetID& subnet_id, + isc::data::ConstElementPtr reservation_data) { std::string identifier; std::string identifier_name; std::string hostname; @@ -186,15 +183,12 @@ HostReservationParser::isSupportedParameter(const std::string& param_name) const return (getSupportedParameters(false).count(param_name) > 0); } -HostReservationParser4::HostReservationParser4(const SubnetID& subnet_id) - : HostReservationParser(subnet_id) { -} - void -HostReservationParser4::parseInternal(isc::data::ConstElementPtr reservation_data) { - HostReservationParser::parseInternal(reservation_data); +HostReservationParser4::parseInternal(const SubnetID& subnet_id, + isc::data::ConstElementPtr reservation_data) { + HostReservationParser::parseInternal(subnet_id, reservation_data); - host_->setIPv4SubnetID(subnet_id_); + host_->setIPv4SubnetID(subnet_id); BOOST_FOREACH(ConfigPair element, reservation_data->mapValue()) { // For 'option-data' element we will use another parser which @@ -248,15 +242,12 @@ HostReservationParser4::getSupportedParameters(const bool identifiers_only) cons return (getSupportedParams4(identifiers_only)); } -HostReservationParser6::HostReservationParser6(const SubnetID& subnet_id) - : HostReservationParser(subnet_id) { -} - void -HostReservationParser6::parseInternal(isc::data::ConstElementPtr reservation_data) { - HostReservationParser::parseInternal(reservation_data); +HostReservationParser6::parseInternal(const SubnetID& subnet_id, + isc::data::ConstElementPtr reservation_data) { + HostReservationParser::parseInternal(subnet_id, reservation_data); - host_->setIPv6SubnetID(subnet_id_); + host_->setIPv6SubnetID(subnet_id); BOOST_FOREACH(ConfigPair element, reservation_data->mapValue()) { // Parse option values. Note that the configuration option parser diff --git a/src/lib/dhcpsrv/parsers/host_reservation_parser.h b/src/lib/dhcpsrv/parsers/host_reservation_parser.h index 13e6750b68..4087401ea3 100644 --- a/src/lib/dhcpsrv/parsers/host_reservation_parser.h +++ b/src/lib/dhcpsrv/parsers/host_reservation_parser.h @@ -18,22 +18,19 @@ namespace dhcp { class HostReservationParser : public isc::data::SimpleParser { public: - /// @brief Constructor. - /// - /// @param subnet_id Identifier of the subnet that the host is - /// connected to. - explicit HostReservationParser(const SubnetID& subnet_id); - /// @brief Destructor. virtual ~HostReservationParser() { } /// @brief Parses a single entry for host reservation. /// + /// @param subnet_id Identifier of the subnet that the host is + /// connected to. /// @param reservation_data Data element holding map with a host /// reservation configuration. /// /// @throw DhcpConfigError If the configuration is invalid. - void parse(isc::data::ConstElementPtr reservation_data); + void parse(const SubnetID& subnet_id, + isc::data::ConstElementPtr reservation_data); protected: @@ -42,11 +39,14 @@ protected: /// This method is called by @ref parse and it can be overriden in the /// derived classes to provide class specific parsing logic. /// + /// @param subnet_id Identifier of the subnet that the host is + /// connected to. /// @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); + virtual void parseInternal(const SubnetID& subnet_id, + isc::data::ConstElementPtr reservation_data); /// @brief Inserts @c host_ object to the staging configuration. /// @@ -84,9 +84,6 @@ protected: virtual const std::set& getSupportedParameters(const bool identifiers_only) const = 0; - /// @brief Identifier of the subnet that the host is connected to. - SubnetID subnet_id_; - /// @brief Holds a pointer to @c Host object representing a parsed /// host reservation configuration. HostPtr host_; @@ -95,23 +92,18 @@ protected: /// @brief Parser for a single host reservation for DHCPv4. class HostReservationParser4 : public HostReservationParser { -public: - - /// @brief Constructor. - /// - /// @param subnet_id Identifier of the subnet that the host is - /// connected to. - HostReservationParser4(const SubnetID& subnet_id); - protected: /// @brief Parses a single host reservation for DHCPv4. /// + /// @param subnet_id Identifier of the subnet that the host is + /// connected to. /// @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); + virtual void parseInternal(const SubnetID& subnet_id, + isc::data::ConstElementPtr reservation_data); /// @brief Returns set of the supported parameters for DHCPv4. /// @@ -126,23 +118,18 @@ protected: /// @brief Parser for a single host reservation for DHCPv6. class HostReservationParser6 : public HostReservationParser { -public: - - /// @brief Constructor. - /// - /// @param subnet_id Identifier of the subnet that the host is - /// connected to. - HostReservationParser6(const SubnetID& subnet_id); - protected: /// @brief Parses a single host reservation for DHCPv6. /// + /// @param subnet_id Identifier of the subnet that the host is + /// connected to. /// @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); + virtual void parseInternal(const SubnetID& subnet_id, + isc::data::ConstElementPtr reservation_data); /// @brief Returns set of the supported parameters for DHCPv6. /// diff --git a/src/lib/dhcpsrv/parsers/host_reservations_list_parser.h b/src/lib/dhcpsrv/parsers/host_reservations_list_parser.h index 6795b273c4..80ca2c3543 100644 --- a/src/lib/dhcpsrv/parsers/host_reservations_list_parser.h +++ b/src/lib/dhcpsrv/parsers/host_reservations_list_parser.h @@ -24,33 +24,21 @@ template class HostReservationsListParser : public isc::data::SimpleParser { public: - /// @brief Constructor. + /// @brief Parses a list of host reservation entries for a subnet. /// /// @param subnet_id Identifier of the subnet to which the reservations /// belong. - HostReservationsListParser(const SubnetID& subnet_id) - : subnet_id_(subnet_id) { - } - - /// @brief Parses a list of host reservation entries for a subnet. - /// /// @param hr_list Data element holding a list of host reservations. /// Each host reservation is described by a map object. /// /// @throw DhcpConfigError If the configuration if any of the reservations /// is invalid. - void parse(isc::data::ConstElementPtr hr_list) { + void parse(const SubnetID& subnet_id, isc::data::ConstElementPtr hr_list) { BOOST_FOREACH(data::ConstElementPtr reservation, hr_list->listValue()) { - HostReservationParserType parser(subnet_id_); - parser.parse(reservation); + HostReservationParserType parser; + parser.parse(subnet_id, reservation); } } - -private: - - /// @brief Identifier of the subnet to whic the reservations belong. - SubnetID subnet_id_; - }; } diff --git a/src/lib/dhcpsrv/tests/host_reservation_parser_unittest.cc b/src/lib/dhcpsrv/tests/host_reservation_parser_unittest.cc index a4a65fa97e..79217708a4 100644 --- a/src/lib/dhcpsrv/tests/host_reservation_parser_unittest.cc +++ b/src/lib/dhcpsrv/tests/host_reservation_parser_unittest.cc @@ -116,8 +116,8 @@ protected: ElementPtr config_element = Element::fromJSON(config); - ParserType parser(SubnetID(10)); - ASSERT_NO_THROW(parser.parse(config_element)); + ParserType parser; + ASSERT_NO_THROW(parser.parse(SubnetID(10), config_element)); // Retrieve a host. HostCollection hosts; @@ -147,8 +147,8 @@ protected: ElementPtr config_element = Element::fromJSON(config.str()); - HostReservationParser4 parser(SubnetID(10)); - ASSERT_NO_THROW(parser.parse(config_element)); + HostReservationParser4 parser; + ASSERT_NO_THROW(parser.parse(SubnetID(10), config_element)); CfgHostsPtr cfg_hosts = CfgMgr::instance().getStagingCfg()->getCfgHosts(); HostCollection hosts; @@ -172,8 +172,8 @@ protected: template void testInvalidConfig(const std::string& config) const { ElementPtr config_element = Element::fromJSON(config); - ParserType parser(SubnetID(10)); - EXPECT_THROW(parser.parse(config_element), DhcpConfigError); + ParserType parser; + EXPECT_THROW(parser.parse(SubnetID(10), config_element), DhcpConfigError); } /// @brief HW Address object used by tests. @@ -286,8 +286,8 @@ TEST_F(HostReservationParserTest, dhcp4NoHostname) { ElementPtr config_element = Element::fromJSON(config); - HostReservationParser4 parser(SubnetID(10)); - ASSERT_NO_THROW(parser.parse(config_element)); + HostReservationParser4 parser; + ASSERT_NO_THROW(parser.parse(SubnetID(10), config_element)); CfgHostsPtr cfg_hosts = CfgMgr::instance().getStagingCfg()->getCfgHosts(); HostCollection hosts; @@ -309,8 +309,8 @@ TEST_F(HostReservationParserTest, dhcp4ClientClasses) { ElementPtr config_element = Element::fromJSON(config); - HostReservationParser4 parser(SubnetID(10)); - ASSERT_NO_THROW(parser.parse(config_element)); + HostReservationParser4 parser; + ASSERT_NO_THROW(parser.parse(SubnetID(10), config_element)); CfgHostsPtr cfg_hosts = CfgMgr::instance().getStagingCfg()->getCfgHosts(); HostCollection hosts; @@ -335,8 +335,8 @@ TEST_F(HostReservationParserTest, dhcp4MessageFields) { ElementPtr config_element = Element::fromJSON(config); - HostReservationParser4 parser(SubnetID(10)); - ASSERT_NO_THROW(parser.parse(config_element)); + HostReservationParser4 parser; + ASSERT_NO_THROW(parser.parse(SubnetID(10), config_element)); CfgHostsPtr cfg_hosts = CfgMgr::instance().getStagingCfg()->getCfgHosts(); HostCollection hosts; @@ -422,8 +422,8 @@ TEST_F(HostReservationParserTest, noIPAddress) { ElementPtr config_element = Element::fromJSON(config); - HostReservationParser4 parser(SubnetID(10)); - ASSERT_NO_THROW(parser.parse(config_element)); + HostReservationParser4 parser; + ASSERT_NO_THROW(parser.parse(SubnetID(10), config_element)); CfgHostsPtr cfg_hosts = CfgMgr::instance().getStagingCfg()->getCfgHosts(); HostCollection hosts; @@ -517,8 +517,8 @@ TEST_F(HostReservationParserTest, dhcp6HWaddr) { ElementPtr config_element = Element::fromJSON(config); - HostReservationParser6 parser(SubnetID(10)); - ASSERT_NO_THROW(parser.parse(config_element)); + HostReservationParser6 parser; + ASSERT_NO_THROW(parser.parse(SubnetID(10), config_element)); CfgHostsPtr cfg_hosts = CfgMgr::instance().getStagingCfg()->getCfgHosts(); HostCollection hosts; @@ -564,8 +564,8 @@ TEST_F(HostReservationParserTest, dhcp6DUID) { ElementPtr config_element = Element::fromJSON(config); - HostReservationParser6 parser(SubnetID(12)); - ASSERT_NO_THROW(parser.parse(config_element)); + HostReservationParser6 parser; + ASSERT_NO_THROW(parser.parse(SubnetID(12), config_element)); CfgHostsPtr cfg_hosts = CfgMgr::instance().getStagingCfg()->getCfgHosts(); HostCollection hosts; @@ -623,8 +623,8 @@ TEST_F(HostReservationParserTest, dhcp6NoHostname) { ElementPtr config_element = Element::fromJSON(config); - HostReservationParser6 parser(SubnetID(12)); - ASSERT_NO_THROW(parser.parse(config_element)); + HostReservationParser6 parser; + ASSERT_NO_THROW(parser.parse(SubnetID(12), config_element)); CfgHostsPtr cfg_hosts = CfgMgr::instance().getStagingCfg()->getCfgHosts(); HostCollection hosts; @@ -659,8 +659,8 @@ TEST_F(HostReservationParserTest, dhcp6ClientClasses) { ElementPtr config_element = Element::fromJSON(config); - HostReservationParser6 parser(SubnetID(10)); - ASSERT_NO_THROW(parser.parse(config_element)); + HostReservationParser6 parser; + ASSERT_NO_THROW(parser.parse(SubnetID(10), config_element)); CfgHostsPtr cfg_hosts = CfgMgr::instance().getStagingCfg()->getCfgHosts(); HostCollection hosts; @@ -770,8 +770,8 @@ TEST_F(HostReservationParserTest, options4) { ElementPtr config_element = Element::fromJSON(config); - HostReservationParser4 parser(SubnetID(10)); - ASSERT_NO_THROW(parser.parse(config_element)); + HostReservationParser4 parser; + ASSERT_NO_THROW(parser.parse(SubnetID(10), config_element)); CfgHostsPtr cfg_hosts = CfgMgr::instance().getStagingCfg()->getCfgHosts(); HostCollection hosts; @@ -827,8 +827,8 @@ TEST_F(HostReservationParserTest, options6) { ElementPtr config_element = Element::fromJSON(config); - HostReservationParser6 parser(SubnetID(10)); - ASSERT_NO_THROW(parser.parse(config_element)); + HostReservationParser6 parser; + ASSERT_NO_THROW(parser.parse(SubnetID(10), config_element)); // One host should have been added to the configuration. CfgHostsPtr cfg_hosts = CfgMgr::instance().getStagingCfg()->getCfgHosts(); 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 f59e01d6aa..9c12d9174c 100644 --- a/src/lib/dhcpsrv/tests/host_reservations_list_parser_unittest.cc +++ b/src/lib/dhcpsrv/tests/host_reservations_list_parser_unittest.cc @@ -86,8 +86,8 @@ TEST_F(HostReservationsListParserTest, ipv4Reservations) { ElementPtr config_element = Element::fromJSON(config); - HostReservationsListParser parser(SubnetID(1)); - ASSERT_NO_THROW(parser.parse(config_element)); + HostReservationsListParser parser; + ASSERT_NO_THROW(parser.parse(SubnetID(1), config_element)); CfgHostsPtr cfg_hosts = CfgMgr::instance().getStagingCfg()->getCfgHosts(); HostCollection hosts; @@ -139,8 +139,8 @@ TEST_F(HostReservationsListParserTest, duplicatedIdentifierValue4) { ElementPtr config_element = Element::fromJSON(config.str()); - HostReservationsListParser parser(SubnetID(1)); - EXPECT_THROW(parser.parse(config_element), DhcpConfigError); + HostReservationsListParser parser; + EXPECT_THROW(parser.parse(SubnetID(1), config_element), DhcpConfigError); } } @@ -164,8 +164,8 @@ TEST_F(HostReservationsListParserTest, ipv6Reservations) { ElementPtr config_element = Element::fromJSON(config); // Parse configuration. - HostReservationsListParser parser(SubnetID(2)); - ASSERT_NO_THROW(parser.parse(config_element)); + HostReservationsListParser parser; + ASSERT_NO_THROW(parser.parse(SubnetID(2), config_element)); CfgHostsPtr cfg_hosts = CfgMgr::instance().getStagingCfg()->getCfgHosts(); HostCollection hosts; @@ -231,8 +231,8 @@ TEST_F(HostReservationsListParserTest, duplicatedIdentifierValue6) { ElementPtr config_element = Element::fromJSON(config.str()); - HostReservationsListParser parser(SubnetID(1)); - EXPECT_THROW(parser.parse(config_element), DhcpConfigError); + HostReservationsListParser parser; + EXPECT_THROW(parser.parse(SubnetID(1), config_element), DhcpConfigError); } } -- 2.47.2