From: Tomek Mrugalski Date: Fri, 4 Aug 2017 14:04:15 +0000 (+0200) Subject: [5272] getAddress moved to SimpleParser X-Git-Tag: trac5124a_base~20^2~2 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=392e75023c69b88fc975d303e4a34dc1d9049a8b;p=thirdparty%2Fkea.git [5272] getAddress moved to SimpleParser --- diff --git a/src/hooks/dhcp/lease_cmds/lease_parser.cc b/src/hooks/dhcp/lease_cmds/lease_parser.cc index 52a1d5de45..67b7a43c3e 100644 --- a/src/hooks/dhcp/lease_cmds/lease_parser.cc +++ b/src/hooks/dhcp/lease_cmds/lease_parser.cc @@ -17,21 +17,9 @@ using namespace isc::dhcp; using namespace isc::data; using namespace isc::asiolink; -// Can't use a constructor as a function -namespace { -IOAddress buildIOAddress(const std::string& str) { return (IOAddress(str)); } -}; - namespace isc { namespace lease_cmds { -IOAddress -LeaseParser::getIOAddress(const ConstElementPtr& scope, - const std::string& name) { - return (getAndConvert(scope, name, "address")); -} - Lease4Ptr Lease4Parser::parse(ConstSrvConfigPtr& cfg, const ConstElementPtr& lease_info) { @@ -40,7 +28,7 @@ Lease4Parser::parse(ConstSrvConfigPtr& cfg, } // These are mandatory parameters. - IOAddress addr = getIOAddress(lease_info, "ip-address"); + IOAddress addr = getAddress(lease_info, "ip-address"); SubnetID subnet_id = getUint32(lease_info, "subnet-id"); if (!addr.isV4()) { @@ -134,7 +122,7 @@ Lease6Parser::parse(ConstSrvConfigPtr& cfg, } // These are mandatory parameters. - IOAddress addr = getIOAddress(lease_info, "ip-address"); + IOAddress addr = getAddress(lease_info, "ip-address"); SubnetID subnet_id = getUint32(lease_info, "subnet-id"); if (addr.isV4()) { diff --git a/src/hooks/dhcp/lease_cmds/lease_parser.h b/src/hooks/dhcp/lease_cmds/lease_parser.h index f5d2dafbad..40400bd94a 100644 --- a/src/hooks/dhcp/lease_cmds/lease_parser.h +++ b/src/hooks/dhcp/lease_cmds/lease_parser.h @@ -15,19 +15,6 @@ namespace isc { namespace lease_cmds { -/// @brief Base class for Lease4 and Lease6 parsers -class LeaseParser : public isc::data::SimpleParser { -protected: - - /// @brief Returns an address from JSON structure - /// - /// @param scope a map the element will be searched at - /// @param name key name to be searched for - /// @return IOAddress representation - isc::asiolink::IOAddress getIOAddress(const isc::data::ConstElementPtr& scope, - const std::string& name); -}; - /// @brief Parser for Lease4 structure /// /// It expects the data in the following format: @@ -44,7 +31,7 @@ protected: /// "hostname": "myhost.example.org", /// "state": 0 /// } -class Lease4Parser : public LeaseParser { +class Lease4Parser : public isc::data::SimpleParser { public: /// @brief Parses Element tree and tries to convert to Lease4 @@ -81,7 +68,7 @@ public: /// } /// It expects the input data to use the following format: -class Lease6Parser : public LeaseParser { +class Lease6Parser : public isc::data::SimpleParser { public: /// @brief Parses Element tree and tries to convert to Lease4 /// diff --git a/src/lib/cc/Makefile.am b/src/lib/cc/Makefile.am index da107aede6..0cf1634773 100644 --- a/src/lib/cc/Makefile.am +++ b/src/lib/cc/Makefile.am @@ -12,6 +12,7 @@ libkea_cc_la_SOURCES += json_feed.cc json_feed.h libkea_cc_la_SOURCES += simple_parser.cc simple_parser.h libkea_cc_la_LIBADD = $(top_builddir)/src/lib/util/libkea-util.la +libkea_cc_la_LIBADD += $(top_builddir)/src/lib/asiolink/libkea-asiolink.la libkea_cc_la_LIBADD += $(top_builddir)/src/lib/exceptions/libkea-exceptions.la libkea_cc_la_LIBADD += $(BOOST_LIBS) diff --git a/src/lib/cc/simple_parser.cc b/src/lib/cc/simple_parser.cc index 240576d3ba..a8defad781 100644 --- a/src/lib/cc/simple_parser.cc +++ b/src/lib/cc/simple_parser.cc @@ -5,12 +5,14 @@ // file, You can obtain one at http://mozilla.org/MPL/2.0/. #include +#include #include #include #include #include using namespace std; +using namespace isc::asiolink; using isc::dhcp::DhcpConfigError; namespace isc { @@ -67,6 +69,19 @@ SimpleParser::getBoolean(isc::data::ConstElementPtr scope, const std::string& na return (x->boolValue()); } +IOAddress +SimpleParser::getAddress(const ConstElementPtr& scope, + const std::string& name) { + std::string str = getString(scope, name); + try { + return (IOAddress(str)); + } catch (const std::exception& e) { + isc_throw(DhcpConfigError, "Failed to convert '" << str + << "' to address: " << e.what() << "(" + << getPosition(name, scope) << ")"); + } +} + const data::Element::Position& SimpleParser::getPosition(const std::string& name, const data::ConstElementPtr parent) { if (!parent) { diff --git a/src/lib/cc/simple_parser.h b/src/lib/cc/simple_parser.h index 29d6ff7bad..b3aac37b32 100644 --- a/src/lib/cc/simple_parser.h +++ b/src/lib/cc/simple_parser.h @@ -7,6 +7,7 @@ #ifndef SIMPLE_PARSER_H #define SIMPLE_PARSER_H +#include #include #include #include @@ -114,8 +115,6 @@ class SimpleParser { static const data::Element::Position& getPosition(const std::string& name, const data::ConstElementPtr parent); -protected: - /// @brief Returns a string parameter from a scope /// /// Unconditionally returns a parameter. @@ -152,6 +151,22 @@ protected: static bool getBoolean(isc::data::ConstElementPtr scope, const std::string& name); + + /// @brief Returns a IOAddress parameter from a scope + /// + /// Unconditionally returns a parameter. + /// + /// @param scope specified parameter will be extracted from this scope + /// @param name name of the parameter + /// @return an IOAddress representing the value of the parameter + /// @throw DhcpConfigError if the parameter is not there or is not of + /// appropriate type (or its conversion to IOAddress fails due to not + /// being a proper address). + static isc::asiolink::IOAddress + getAddress(const ConstElementPtr& scope, const std::string& name); + +protected: + /// @brief Returns an integer value with range checking from a scope /// /// This template should be instantiated in parsers when useful diff --git a/src/lib/cc/tests/Makefile.am b/src/lib/cc/tests/Makefile.am index 57445254ea..f07e9c75cb 100644 --- a/src/lib/cc/tests/Makefile.am +++ b/src/lib/cc/tests/Makefile.am @@ -24,6 +24,7 @@ run_unittests_LDFLAGS = $(AM_LDFLAGS) $(GTEST_LDFLAGS) run_unittests_LDADD = $(top_builddir)/src/lib/cc/libkea-cc.la run_unittests_LDADD += $(top_builddir)/src/lib/log/libkea-log.la +run_unittests_LDADD += $(top_builddir)/src/lib/asiolink/libkea-asiolink.la run_unittests_LDADD += $(top_builddir)/src/lib/util/threads/libkea-threads.la run_unittests_LDADD += $(top_builddir)/src/lib/util/unittests/libutil_unittests.la run_unittests_LDADD += $(top_builddir)/src/lib/util/libkea-util.la diff --git a/src/lib/cc/tests/simple_parser_unittest.cc b/src/lib/cc/tests/simple_parser_unittest.cc index 2ee115303d..9febfe868e 100644 --- a/src/lib/cc/tests/simple_parser_unittest.cc +++ b/src/lib/cc/tests/simple_parser_unittest.cc @@ -9,6 +9,7 @@ #include using namespace isc::data; +using namespace isc::asiolink; using isc::dhcp::DhcpConfigError; /// This table defines sample default values. Although these are DHCPv6 @@ -210,3 +211,27 @@ TEST_F(SimpleParserTest, getAndConvert) { EXPECT_THROW(parser.getAsBool(bad_bool, "bar"), DhcpConfigError); } +// This test exercises the getIOAddress +TEST_F(SimpleParserTest, getIOAddress) { + + SimpleParserClassTest parser; + + // getAddress checks it can be found + ElementPtr not_found = Element::fromJSON("{ \"bar\": 1 }"); + EXPECT_THROW(parser.getAddress(not_found, "foo"), DhcpConfigError); + + // getAddress checks if it is a string + ElementPtr not_addr = Element::fromJSON("{ \"foo\": 1234 }"); + EXPECT_THROW(parser.getAddress(not_addr, "foo"), DhcpConfigError); + + // checks if getAddress can return the expected value of v4 address + ElementPtr v4 = Element::fromJSON("{ \"foo\": \"192.0.2.1\" }"); + IOAddress val("::"); + EXPECT_NO_THROW(val = parser.getAddress(v4, "foo")); + EXPECT_EQ("192.0.2.1" , val.toText()); + + // checks if getAddress can return the expected value of v4 address + ElementPtr v6 = Element::fromJSON("{ \"foo\": \"2001:db8::1\" }"); + EXPECT_NO_THROW(val = parser.getAddress(v6, "foo")); + EXPECT_EQ("2001:db8::1" , val.toText()); +} diff --git a/src/lib/dhcpsrv/parsers/dhcp_parsers.cc b/src/lib/dhcpsrv/parsers/dhcp_parsers.cc index 74521cd8c9..89839acc59 100644 --- a/src/lib/dhcpsrv/parsers/dhcp_parsers.cc +++ b/src/lib/dhcpsrv/parsers/dhcp_parsers.cc @@ -601,23 +601,11 @@ RelayInfoParser::RelayInfoParser(const Option::Universe& family) : family_(family) { }; -// Can't use a constructor as a function -namespace { -IOAddress buildIOAddress(const std::string& str) { return (IOAddress(str)); } -}; - -IOAddress -RelayInfoParser::getIOAddress(ConstElementPtr scope, - const std::string& name) { - return (getAndConvert(scope, name, "address")); -} - void RelayInfoParser::parse(const isc::dhcp::Subnet::RelayInfoPtr& cfg, ConstElementPtr relay_info) { // There is only one parameter which is mandatory - IOAddress ip = getIOAddress(relay_info, "ip-address"); + IOAddress ip = getAddress(relay_info, "ip-address"); // Check if the address family matches. if ((ip.isV4() && family_ != Option::V4) || @@ -905,13 +893,6 @@ SubnetConfigParser::createSubnet(ConstElementPtr params) { //**************************** D2ClientConfigParser ********************** -IOAddress -D2ClientConfigParser::getIOAddress(ConstElementPtr scope, - const std::string& name) { - return (getAndConvert(scope, name, "address")); -} - dhcp_ddns::NameChangeProtocol D2ClientConfigParser::getProtocol(ConstElementPtr scope, const std::string& name) { @@ -943,7 +924,7 @@ D2ClientConfigParser::parse(isc::data::ConstElementPtr client_config) { // Get all parameters that are needed to create the D2ClientConfig. bool enable_updates = getBoolean(client_config, "enable-updates"); - IOAddress server_ip = getIOAddress(client_config, "server-ip"); + IOAddress server_ip = getAddress(client_config, "server-ip"); uint32_t server_port = getUint32(client_config, "server-port"); diff --git a/src/lib/dhcpsrv/parsers/dhcp_parsers.h b/src/lib/dhcpsrv/parsers/dhcp_parsers.h index 6ddf10bf5d..d1854ae807 100644 --- a/src/lib/dhcpsrv/parsers/dhcp_parsers.h +++ b/src/lib/dhcpsrv/parsers/dhcp_parsers.h @@ -634,16 +634,6 @@ public: private: - /// @brief Returns a value converted to IOAddress - /// - /// Instantiation of getAndConvert() to IOAddress - /// - /// @param scope specified parameter will be extracted from this scope - /// @param name name of the parameter - /// @return an IOAddress value - isc::asiolink::IOAddress - getIOAddress(isc::data::ConstElementPtr scope, const std::string& name); - /// Protocol family (IPv4 or IPv6) Option::Universe family_; }; @@ -787,16 +777,6 @@ public: private: - /// @brief Returns a value converted to IOAddress - /// - /// Instantiation of getAndConvert() to IOAddress - /// - /// @param scope specified parameter will be extracted from this scope - /// @param name name of the parameter - /// @return an IOAddress value - isc::asiolink::IOAddress - getIOAddress(isc::data::ConstElementPtr scope, const std::string& name); - /// @brief Returns a value converted to NameChangeProtocol /// /// Instantiation of getAndConvert() to NameChangeProtocol