2
0
mirror of https://gitlab.isc.org/isc-projects/kea synced 2025-08-30 05:27:55 +00:00

[5030] SubnetID a parameter of parse method rather than ctor.

This commit is contained in:
Marcin Siodelski 2017-01-05 21:35:11 +01:00
parent 1bd9870c36
commit 7e66a9d112
7 changed files with 71 additions and 107 deletions

View File

@ -152,9 +152,8 @@ public:
// Parse Host Reservations for this subnet if any. // Parse Host Reservations for this subnet if any.
ConstElementPtr reservations = subnet->get("reservations"); ConstElementPtr reservations = subnet->get("reservations");
if (reservations) { if (reservations) {
HostReservationsListParser<HostReservationParser4> HostReservationsListParser<HostReservationParser4> parser;
parser(subnet_->getID()); parser.parse(subnet_->getID(), reservations);
parser.parse(reservations);
} }
} }

View File

@ -385,9 +385,8 @@ public:
// Parse Host Reservations for this subnet if any. // Parse Host Reservations for this subnet if any.
ConstElementPtr reservations = subnet->get("reservations"); ConstElementPtr reservations = subnet->get("reservations");
if (reservations) { if (reservations) {
HostReservationsListParser<HostReservationParser6> HostReservationsListParser<HostReservationParser6> parser;
parser(subnet_->getID()); parser.parse(subnet_->getID(), reservations);
parser.parse(reservations);
} }
} }
} }

View File

@ -94,18 +94,15 @@ getSupportedParams6(const bool identifiers_only = false) {
namespace isc { namespace isc {
namespace dhcp { namespace dhcp {
HostReservationParser::HostReservationParser(const SubnetID& subnet_id)
: SimpleParser(), subnet_id_(subnet_id) {
}
void void
HostReservationParser::parse(isc::data::ConstElementPtr reservation_data) { HostReservationParser::parse(const SubnetID& subnet_id,
parseInternal(reservation_data); isc::data::ConstElementPtr reservation_data) {
parseInternal(subnet_id, reservation_data);
} }
void 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;
std::string identifier_name; std::string identifier_name;
std::string hostname; std::string hostname;
@ -186,15 +183,12 @@ HostReservationParser::isSupportedParameter(const std::string& param_name) const
return (getSupportedParameters(false).count(param_name) > 0); return (getSupportedParameters(false).count(param_name) > 0);
} }
HostReservationParser4::HostReservationParser4(const SubnetID& subnet_id)
: HostReservationParser(subnet_id) {
}
void void
HostReservationParser4::parseInternal(isc::data::ConstElementPtr reservation_data) { HostReservationParser4::parseInternal(const SubnetID& subnet_id,
HostReservationParser::parseInternal(reservation_data); 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()) { BOOST_FOREACH(ConfigPair element, reservation_data->mapValue()) {
// For 'option-data' element we will use another parser which // 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)); return (getSupportedParams4(identifiers_only));
} }
HostReservationParser6::HostReservationParser6(const SubnetID& subnet_id)
: HostReservationParser(subnet_id) {
}
void void
HostReservationParser6::parseInternal(isc::data::ConstElementPtr reservation_data) { HostReservationParser6::parseInternal(const SubnetID& subnet_id,
HostReservationParser::parseInternal(reservation_data); 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()) { BOOST_FOREACH(ConfigPair element, reservation_data->mapValue()) {
// Parse option values. Note that the configuration option parser // Parse option values. Note that the configuration option parser

View File

@ -18,22 +18,19 @@ namespace dhcp {
class HostReservationParser : public isc::data::SimpleParser { class HostReservationParser : public isc::data::SimpleParser {
public: public:
/// @brief Constructor.
///
/// @param subnet_id Identifier of the subnet that the host is
/// connected to.
explicit HostReservationParser(const SubnetID& subnet_id);
/// @brief Destructor. /// @brief Destructor.
virtual ~HostReservationParser() { } virtual ~HostReservationParser() { }
/// @brief Parses a single entry for host reservation. /// @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 /// @param reservation_data Data element holding map with a host
/// reservation configuration. /// reservation configuration.
/// ///
/// @throw DhcpConfigError If the configuration is invalid. /// @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: protected:
@ -42,11 +39,14 @@ protected:
/// This method is called by @ref parse and it can be overriden in the /// This method is called by @ref parse and it can be overriden in the
/// derived classes to provide class specific parsing logic. /// 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 /// @param reservation_data Data element holding map with a host
/// reservation configuration. /// reservation configuration.
/// ///
/// @throw DhcpConfigError If the configuration is invalid. /// @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. /// @brief Inserts @c host_ object to the staging configuration.
/// ///
@ -84,9 +84,6 @@ protected:
virtual const std::set<std::string>& virtual const std::set<std::string>&
getSupportedParameters(const bool identifiers_only) const = 0; 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 /// @brief Holds a pointer to @c Host object representing a parsed
/// host reservation configuration. /// host reservation configuration.
HostPtr host_; HostPtr host_;
@ -95,23 +92,18 @@ protected:
/// @brief Parser for a single host reservation for DHCPv4. /// @brief Parser for a single host reservation for DHCPv4.
class HostReservationParser4 : public HostReservationParser { 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: protected:
/// @brief Parses a single host reservation for DHCPv4. /// @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 /// @param reservation_data Data element holding map with a host
/// reservation configuration. /// reservation configuration.
/// ///
/// @throw DhcpConfigError If the configuration is invalid. /// @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. /// @brief Returns set of the supported parameters for DHCPv4.
/// ///
@ -126,23 +118,18 @@ protected:
/// @brief Parser for a single host reservation for DHCPv6. /// @brief Parser for a single host reservation for DHCPv6.
class HostReservationParser6 : public HostReservationParser { 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: protected:
/// @brief Parses a single host reservation for DHCPv6. /// @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 /// @param reservation_data Data element holding map with a host
/// reservation configuration. /// reservation configuration.
/// ///
/// @throw DhcpConfigError If the configuration is invalid. /// @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. /// @brief Returns set of the supported parameters for DHCPv6.
/// ///

View File

@ -24,33 +24,21 @@ template<typename HostReservationParserType>
class HostReservationsListParser : public isc::data::SimpleParser { class HostReservationsListParser : public isc::data::SimpleParser {
public: 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 /// @param subnet_id Identifier of the subnet to which the reservations
/// belong. /// 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. /// @param hr_list Data element holding a list of host reservations.
/// Each host reservation is described by a map object. /// Each host reservation is described by a map object.
/// ///
/// @throw DhcpConfigError If the configuration if any of the reservations /// @throw DhcpConfigError If the configuration if any of the reservations
/// is invalid. /// 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()) { BOOST_FOREACH(data::ConstElementPtr reservation, hr_list->listValue()) {
HostReservationParserType parser(subnet_id_); HostReservationParserType parser;
parser.parse(reservation); parser.parse(subnet_id, reservation);
} }
} }
private:
/// @brief Identifier of the subnet to whic the reservations belong.
SubnetID subnet_id_;
}; };
} }

View File

@ -116,8 +116,8 @@ protected:
ElementPtr config_element = Element::fromJSON(config); ElementPtr config_element = Element::fromJSON(config);
ParserType parser(SubnetID(10)); ParserType parser;
ASSERT_NO_THROW(parser.parse(config_element)); ASSERT_NO_THROW(parser.parse(SubnetID(10), config_element));
// Retrieve a host. // Retrieve a host.
HostCollection hosts; HostCollection hosts;
@ -147,8 +147,8 @@ protected:
ElementPtr config_element = Element::fromJSON(config.str()); ElementPtr config_element = Element::fromJSON(config.str());
HostReservationParser4 parser(SubnetID(10)); HostReservationParser4 parser;
ASSERT_NO_THROW(parser.parse(config_element)); ASSERT_NO_THROW(parser.parse(SubnetID(10), config_element));
CfgHostsPtr cfg_hosts = CfgMgr::instance().getStagingCfg()->getCfgHosts(); CfgHostsPtr cfg_hosts = CfgMgr::instance().getStagingCfg()->getCfgHosts();
HostCollection hosts; HostCollection hosts;
@ -172,8 +172,8 @@ protected:
template<typename ParserType> template<typename ParserType>
void testInvalidConfig(const std::string& config) const { void testInvalidConfig(const std::string& config) const {
ElementPtr config_element = Element::fromJSON(config); ElementPtr config_element = Element::fromJSON(config);
ParserType parser(SubnetID(10)); ParserType parser;
EXPECT_THROW(parser.parse(config_element), DhcpConfigError); EXPECT_THROW(parser.parse(SubnetID(10), config_element), DhcpConfigError);
} }
/// @brief HW Address object used by tests. /// @brief HW Address object used by tests.
@ -286,8 +286,8 @@ TEST_F(HostReservationParserTest, dhcp4NoHostname) {
ElementPtr config_element = Element::fromJSON(config); ElementPtr config_element = Element::fromJSON(config);
HostReservationParser4 parser(SubnetID(10)); HostReservationParser4 parser;
ASSERT_NO_THROW(parser.parse(config_element)); ASSERT_NO_THROW(parser.parse(SubnetID(10), config_element));
CfgHostsPtr cfg_hosts = CfgMgr::instance().getStagingCfg()->getCfgHosts(); CfgHostsPtr cfg_hosts = CfgMgr::instance().getStagingCfg()->getCfgHosts();
HostCollection hosts; HostCollection hosts;
@ -309,8 +309,8 @@ TEST_F(HostReservationParserTest, dhcp4ClientClasses) {
ElementPtr config_element = Element::fromJSON(config); ElementPtr config_element = Element::fromJSON(config);
HostReservationParser4 parser(SubnetID(10)); HostReservationParser4 parser;
ASSERT_NO_THROW(parser.parse(config_element)); ASSERT_NO_THROW(parser.parse(SubnetID(10), config_element));
CfgHostsPtr cfg_hosts = CfgMgr::instance().getStagingCfg()->getCfgHosts(); CfgHostsPtr cfg_hosts = CfgMgr::instance().getStagingCfg()->getCfgHosts();
HostCollection hosts; HostCollection hosts;
@ -335,8 +335,8 @@ TEST_F(HostReservationParserTest, dhcp4MessageFields) {
ElementPtr config_element = Element::fromJSON(config); ElementPtr config_element = Element::fromJSON(config);
HostReservationParser4 parser(SubnetID(10)); HostReservationParser4 parser;
ASSERT_NO_THROW(parser.parse(config_element)); ASSERT_NO_THROW(parser.parse(SubnetID(10), config_element));
CfgHostsPtr cfg_hosts = CfgMgr::instance().getStagingCfg()->getCfgHosts(); CfgHostsPtr cfg_hosts = CfgMgr::instance().getStagingCfg()->getCfgHosts();
HostCollection hosts; HostCollection hosts;
@ -422,8 +422,8 @@ TEST_F(HostReservationParserTest, noIPAddress) {
ElementPtr config_element = Element::fromJSON(config); ElementPtr config_element = Element::fromJSON(config);
HostReservationParser4 parser(SubnetID(10)); HostReservationParser4 parser;
ASSERT_NO_THROW(parser.parse(config_element)); ASSERT_NO_THROW(parser.parse(SubnetID(10), config_element));
CfgHostsPtr cfg_hosts = CfgMgr::instance().getStagingCfg()->getCfgHosts(); CfgHostsPtr cfg_hosts = CfgMgr::instance().getStagingCfg()->getCfgHosts();
HostCollection hosts; HostCollection hosts;
@ -517,8 +517,8 @@ TEST_F(HostReservationParserTest, dhcp6HWaddr) {
ElementPtr config_element = Element::fromJSON(config); ElementPtr config_element = Element::fromJSON(config);
HostReservationParser6 parser(SubnetID(10)); HostReservationParser6 parser;
ASSERT_NO_THROW(parser.parse(config_element)); ASSERT_NO_THROW(parser.parse(SubnetID(10), config_element));
CfgHostsPtr cfg_hosts = CfgMgr::instance().getStagingCfg()->getCfgHosts(); CfgHostsPtr cfg_hosts = CfgMgr::instance().getStagingCfg()->getCfgHosts();
HostCollection hosts; HostCollection hosts;
@ -564,8 +564,8 @@ TEST_F(HostReservationParserTest, dhcp6DUID) {
ElementPtr config_element = Element::fromJSON(config); ElementPtr config_element = Element::fromJSON(config);
HostReservationParser6 parser(SubnetID(12)); HostReservationParser6 parser;
ASSERT_NO_THROW(parser.parse(config_element)); ASSERT_NO_THROW(parser.parse(SubnetID(12), config_element));
CfgHostsPtr cfg_hosts = CfgMgr::instance().getStagingCfg()->getCfgHosts(); CfgHostsPtr cfg_hosts = CfgMgr::instance().getStagingCfg()->getCfgHosts();
HostCollection hosts; HostCollection hosts;
@ -623,8 +623,8 @@ TEST_F(HostReservationParserTest, dhcp6NoHostname) {
ElementPtr config_element = Element::fromJSON(config); ElementPtr config_element = Element::fromJSON(config);
HostReservationParser6 parser(SubnetID(12)); HostReservationParser6 parser;
ASSERT_NO_THROW(parser.parse(config_element)); ASSERT_NO_THROW(parser.parse(SubnetID(12), config_element));
CfgHostsPtr cfg_hosts = CfgMgr::instance().getStagingCfg()->getCfgHosts(); CfgHostsPtr cfg_hosts = CfgMgr::instance().getStagingCfg()->getCfgHosts();
HostCollection hosts; HostCollection hosts;
@ -659,8 +659,8 @@ TEST_F(HostReservationParserTest, dhcp6ClientClasses) {
ElementPtr config_element = Element::fromJSON(config); ElementPtr config_element = Element::fromJSON(config);
HostReservationParser6 parser(SubnetID(10)); HostReservationParser6 parser;
ASSERT_NO_THROW(parser.parse(config_element)); ASSERT_NO_THROW(parser.parse(SubnetID(10), config_element));
CfgHostsPtr cfg_hosts = CfgMgr::instance().getStagingCfg()->getCfgHosts(); CfgHostsPtr cfg_hosts = CfgMgr::instance().getStagingCfg()->getCfgHosts();
HostCollection hosts; HostCollection hosts;
@ -770,8 +770,8 @@ TEST_F(HostReservationParserTest, options4) {
ElementPtr config_element = Element::fromJSON(config); ElementPtr config_element = Element::fromJSON(config);
HostReservationParser4 parser(SubnetID(10)); HostReservationParser4 parser;
ASSERT_NO_THROW(parser.parse(config_element)); ASSERT_NO_THROW(parser.parse(SubnetID(10), config_element));
CfgHostsPtr cfg_hosts = CfgMgr::instance().getStagingCfg()->getCfgHosts(); CfgHostsPtr cfg_hosts = CfgMgr::instance().getStagingCfg()->getCfgHosts();
HostCollection hosts; HostCollection hosts;
@ -827,8 +827,8 @@ TEST_F(HostReservationParserTest, options6) {
ElementPtr config_element = Element::fromJSON(config); ElementPtr config_element = Element::fromJSON(config);
HostReservationParser6 parser(SubnetID(10)); HostReservationParser6 parser;
ASSERT_NO_THROW(parser.parse(config_element)); ASSERT_NO_THROW(parser.parse(SubnetID(10), config_element));
// One host should have been added to the configuration. // One host should have been added to the configuration.
CfgHostsPtr cfg_hosts = CfgMgr::instance().getStagingCfg()->getCfgHosts(); CfgHostsPtr cfg_hosts = CfgMgr::instance().getStagingCfg()->getCfgHosts();

View File

@ -86,8 +86,8 @@ TEST_F(HostReservationsListParserTest, ipv4Reservations) {
ElementPtr config_element = Element::fromJSON(config); ElementPtr config_element = Element::fromJSON(config);
HostReservationsListParser<HostReservationParser4> parser(SubnetID(1)); HostReservationsListParser<HostReservationParser4> parser;
ASSERT_NO_THROW(parser.parse(config_element)); ASSERT_NO_THROW(parser.parse(SubnetID(1), config_element));
CfgHostsPtr cfg_hosts = CfgMgr::instance().getStagingCfg()->getCfgHosts(); CfgHostsPtr cfg_hosts = CfgMgr::instance().getStagingCfg()->getCfgHosts();
HostCollection hosts; HostCollection hosts;
@ -139,8 +139,8 @@ TEST_F(HostReservationsListParserTest, duplicatedIdentifierValue4) {
ElementPtr config_element = Element::fromJSON(config.str()); ElementPtr config_element = Element::fromJSON(config.str());
HostReservationsListParser<HostReservationParser4> parser(SubnetID(1)); HostReservationsListParser<HostReservationParser4> parser;
EXPECT_THROW(parser.parse(config_element), DhcpConfigError); EXPECT_THROW(parser.parse(SubnetID(1), config_element), DhcpConfigError);
} }
} }
@ -164,8 +164,8 @@ TEST_F(HostReservationsListParserTest, ipv6Reservations) {
ElementPtr config_element = Element::fromJSON(config); ElementPtr config_element = Element::fromJSON(config);
// Parse configuration. // Parse configuration.
HostReservationsListParser<HostReservationParser6> parser(SubnetID(2)); HostReservationsListParser<HostReservationParser6> parser;
ASSERT_NO_THROW(parser.parse(config_element)); ASSERT_NO_THROW(parser.parse(SubnetID(2), config_element));
CfgHostsPtr cfg_hosts = CfgMgr::instance().getStagingCfg()->getCfgHosts(); CfgHostsPtr cfg_hosts = CfgMgr::instance().getStagingCfg()->getCfgHosts();
HostCollection hosts; HostCollection hosts;
@ -231,8 +231,8 @@ TEST_F(HostReservationsListParserTest, duplicatedIdentifierValue6) {
ElementPtr config_element = Element::fromJSON(config.str()); ElementPtr config_element = Element::fromJSON(config.str());
HostReservationsListParser<HostReservationParser6> parser(SubnetID(1)); HostReservationsListParser<HostReservationParser6> parser;
EXPECT_THROW(parser.parse(config_element), DhcpConfigError); EXPECT_THROW(parser.parse(SubnetID(1), config_element), DhcpConfigError);
} }
} }