Commit 392e7502 authored by Tomek Mrugalski's avatar Tomek Mrugalski 🛰

[5272] getAddress moved to SimpleParser

parent 1300562b
......@@ -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<IOAddress,
buildIOAddress>(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()) {
......
......@@ -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
///
......
......@@ -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)
......
......@@ -5,12 +5,14 @@
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
#include <cc/simple_parser.h>
#include <asiolink/io_address.h>
#include <boost/foreach.hpp>
#include <boost/lexical_cast.hpp>
#include <cc/data.h>
#include <string>
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) {
......
......@@ -7,6 +7,7 @@
#ifndef SIMPLE_PARSER_H
#define SIMPLE_PARSER_H
#include <asiolink/io_address.h>
#include <cc/data.h>
#include <cc/dhcp_config_error.h>
#include <vector>
......@@ -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
......
......@@ -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
......
......@@ -9,6 +9,7 @@
#include <gtest/gtest.h>
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());
}
......@@ -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<IOAddress,
buildIOAddress>(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<IOAddress,
buildIOAddress>(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");
......
......@@ -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
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment