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) {
}
// 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()) {
}
// 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()) {
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:
/// "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
/// }
/// 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
///
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)
// 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 {
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) {
#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>
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.
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
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
#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
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());
+}
: 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) ||
//**************************** 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) {
// 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");
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_;
};
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