diff options
Diffstat (limited to 'src/lib/dhcpsrv/parsers/duid_config_parser.cc')
-rw-r--r-- | src/lib/dhcpsrv/parsers/duid_config_parser.cc | 150 |
1 files changed, 51 insertions, 99 deletions
diff --git a/src/lib/dhcpsrv/parsers/duid_config_parser.cc b/src/lib/dhcpsrv/parsers/duid_config_parser.cc index f64c783d10..355e4f7e71 100644 --- a/src/lib/dhcpsrv/parsers/duid_config_parser.cc +++ b/src/lib/dhcpsrv/parsers/duid_config_parser.cc @@ -1,4 +1,4 @@ -// Copyright (C) 2015 Internet Systems Consortium, Inc. ("ISC") +// Copyright (C) 2015,2017 Internet Systems Consortium, Inc. ("ISC") // // This Source Code Form is subject to the terms of the Mozilla Public // License, v. 2.0. If a copy of the MPL was not distributed with this @@ -11,6 +11,7 @@ #include <dhcpsrv/cfgmgr.h> #include <dhcpsrv/dhcpsrv_log.h> #include <dhcpsrv/parsers/duid_config_parser.h> +#include <dhcpsrv/parsers/dhcp_parsers.h> #include <exceptions/exceptions.h> #include <boost/foreach.hpp> #include <boost/lexical_cast.hpp> @@ -22,115 +23,66 @@ using namespace isc::data; namespace isc { namespace dhcp { -DUIDConfigParser::DUIDConfigParser() - : DhcpConfigParser() { -} - void -DUIDConfigParser::build(isc::data::ConstElementPtr duid_configuration) { - bool type_present = false; - BOOST_FOREACH(ConfigPair element, duid_configuration->mapValue()) { - try { - if (element.first == "type") { - type_present = true; - setType(element.second->stringValue()); - } else if (element.first == "identifier") { - setIdentifier(element.second->stringValue()); - } else if (element.first == "htype") { - setHType(element.second->intValue()); - } else if (element.first == "time") { - setTime(element.second->intValue()); - } else if (element.first == "enterprise-id") { - setEnterpriseId(element.second->intValue()); - } else if (element.first == "persist") { - setPersist(element.second->boolValue()); - } else { - isc_throw(DhcpConfigError, "unsupported configuration " - "parameter '" << element.first << "'"); - } - } catch (const std::exception& ex) { - // Append position. - isc_throw(DhcpConfigError, ex.what() << " (" - << element.second->getPosition() << ")"); - } - } - - // "type" is mandatory - if (!type_present) { - isc_throw(DhcpConfigError, "mandatory parameter \"type\" not specified" - " for the DUID configuration (" - << duid_configuration->getPosition() << ")"); +DUIDConfigParser::parse(const CfgDUIDPtr& cfg, + isc::data::ConstElementPtr duid_configuration) { + if (!cfg) { + isc_throw(DhcpConfigError, "Must provide valid pointer to cfg when parsing duid"); } - LOG_WARN(dhcpsrv_logger, DHCPSRV_CFGMGR_CONFIGURE_SERVERID); -} - -void -DUIDConfigParser::setType(const std::string& duid_type) const { - // Map DUID type represented as text into numeric value. - DUID::DUIDType numeric_type = DUID::DUID_UNKNOWN; - if (duid_type == "LLT") { - numeric_type = DUID::DUID_LLT; - } else if (duid_type == "EN") { - numeric_type = DUID::DUID_EN; - } else if (duid_type == "LL") { - numeric_type = DUID::DUID_LL; - } else { - isc_throw(DhcpConfigError, "unsupported DUID type '" - << duid_type << "'. Expected: LLT, EN or LL"); - } - - const CfgDUIDPtr& cfg = CfgMgr::instance().getStagingCfg()->getCfgDUID(); - cfg->setType(static_cast<DUID::DUIDType>(numeric_type)); -} - -void -DUIDConfigParser::setIdentifier(const std::string& identifier) const { - const CfgDUIDPtr& cfg = CfgMgr::instance().getStagingCfg()->getCfgDUID(); - cfg->setIdentifier(identifier); -} + std::string param; + try { + param = "type"; + std::string duid_type = getString(duid_configuration, "type"); + // Map DUID type represented as text into numeric value. + DUID::DUIDType numeric_type = DUID::DUID_UNKNOWN; + if (duid_type == "LLT") { + numeric_type = DUID::DUID_LLT; + } else if (duid_type == "EN") { + numeric_type = DUID::DUID_EN; + } else if (duid_type == "LL") { + numeric_type = DUID::DUID_LL; + } else { + isc_throw(BadValue, "unsupported DUID type '" + << duid_type << "'. Expected: LLT, EN or LL"); + } -void -DUIDConfigParser::setHType(const int64_t htype) const { - const CfgDUIDPtr& cfg = CfgMgr::instance().getStagingCfg()->getCfgDUID(); - checkRange<uint16_t>("htype", htype); - cfg->setHType(static_cast<uint16_t>(htype)); + cfg->setType(static_cast<DUID::DUIDType>(numeric_type)); -} + param = "identifier"; + if (duid_configuration->contains(param)) { + cfg->setIdentifier(getString(duid_configuration, param)); + } -void -DUIDConfigParser::setTime(const int64_t new_time) const { - const CfgDUIDPtr& cfg = CfgMgr::instance().getStagingCfg()->getCfgDUID(); - checkRange<uint32_t>("time", new_time); - cfg->setTime(static_cast<uint32_t>(new_time)); -} + param = "htype"; + if (duid_configuration->contains(param)) { + cfg->setHType(getUint16(duid_configuration, param)); + } -void -DUIDConfigParser::setEnterpriseId(const int64_t enterprise_id) const { - const CfgDUIDPtr& cfg = CfgMgr::instance().getStagingCfg()->getCfgDUID(); - checkRange<uint32_t>("enterprise-id", enterprise_id); - cfg->setEnterpriseId(static_cast<uint32_t>(enterprise_id)); -} + param = "time"; + if (duid_configuration->contains(param)) { + cfg->setTime(getUint32(duid_configuration, param)); + } -void -DUIDConfigParser::setPersist(const bool persist) { - const CfgDUIDPtr& cfg = CfgMgr::instance().getStagingCfg()->getCfgDUID(); - cfg->setPersist(persist); -} + param = "enterprise-id"; + if (duid_configuration->contains(param)) { + cfg->setEnterpriseId(getUint32(duid_configuration, param)); + } -template<typename NumericType> -void -DUIDConfigParser::checkRange(const std::string& parameter_name, - const int64_t parameter_value) const { - if ((parameter_value < 0) || - (parameter_value > std::numeric_limits<NumericType>::max())) { - isc_throw(DhcpConfigError, "out of range value '" << parameter_value - << "' specified for parameter '" << parameter_name - << "'; expected value in range of [0.." - << std::numeric_limits<NumericType>::max() << "]"); + param = "persist"; + if (duid_configuration->contains(param)) { + cfg->setPersist(getBoolean(duid_configuration, param)); + } + } catch (const DhcpConfigError&) { + throw; + } catch (const std::exception& ex) { + // Append position. + isc_throw(DhcpConfigError, ex.what() << " (" + << getPosition(param, duid_configuration) << ")"); } -} + LOG_WARN(dhcpsrv_logger, DHCPSRV_CFGMGR_CONFIGURE_SERVERID); +} } // end of namespace isc::dhcp } // end of namespace isc |