Parcourir la source

[5126] Flatten DUIDConfigParser

Francis Dupont il y a 8 ans
Parent
commit
d21293c35a

+ 47 - 92
src/lib/dhcpsrv/parsers/duid_config_parser.cc

@@ -24,107 +24,62 @@ namespace isc {
 namespace dhcp {
 
 void
-DUIDConfigParser::parse(const CfgDUIDPtr& cfg, isc::data::ConstElementPtr duid_configuration) {
-    if (!cfg) {
-        isc_throw(DhcpConfigError, "Must provide valid pointer to cfg when parsing duid");
-    }
-
-    bool type_present = false;
-    BOOST_FOREACH(ConfigPair element, duid_configuration->mapValue()) {
-        try {
-            if (element.first == "type") {
-                type_present = true;
-                setType(cfg, element.second->stringValue());
-            } else if (element.first == "identifier") {
-                setIdentifier(cfg, element.second->stringValue());
-            } else if (element.first == "htype") {
-                setHType(cfg, element.second->intValue());
-            } else if (element.first == "time") {
-                setTime(cfg, element.second->intValue());
-            } else if (element.first == "enterprise-id") {
-                setEnterpriseId(cfg, element.second->intValue());
-            } else if (element.first == "persist") {
-                setPersist(cfg, 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() << ")");
+DUIDConfigParser::parse(const CfgDUIDPtr& cfg,
+                        isc::data::ConstElementPtr duid_configuration) {
+
+    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");
         }
-    }
-
-    // "type" is mandatory
-    if (!type_present) {
-        isc_throw(DhcpConfigError, "mandatory parameter \"type\" not specified"
-                  " for the DUID configuration ("
-                  << duid_configuration->getPosition() << ")");
-    }
-
-    LOG_WARN(dhcpsrv_logger, DHCPSRV_CFGMGR_CONFIGURE_SERVERID);
-}
 
-void
-DUIDConfigParser::setType(const CfgDUIDPtr& cfg, 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");
-    }
-
-    cfg->setType(static_cast<DUID::DUIDType>(numeric_type));
-}
+        cfg->setType(static_cast<DUID::DUIDType>(numeric_type));
 
-void
-DUIDConfigParser::setIdentifier(const CfgDUIDPtr& cfg, const std::string& identifier) const {
-    cfg->setIdentifier(identifier);
-}
-
-void
-DUIDConfigParser::setHType(const CfgDUIDPtr& cfg, const int64_t htype) const {
-    checkRange<uint16_t>("htype", htype);
-    cfg->setHType(static_cast<uint16_t>(htype));
-}
+        param = "identifier";
+        if (duid_configuration->contains(param)) {
+            cfg->setIdentifier(getString(duid_configuration, param));
+        }
 
-void
-DUIDConfigParser::setTime(const CfgDUIDPtr& cfg, const int64_t new_time) const {
-    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 CfgDUIDPtr& cfg, const int64_t enterprise_id) const {
-    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 CfgDUIDPtr& cfg, const bool persist) {
-    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

+ 0 - 51
src/lib/dhcpsrv/parsers/duid_config_parser.h

@@ -33,57 +33,6 @@ public:
     ///
     /// @throw DhcpConfigError If the configuration is invalid.
     void parse(const CfgDUIDPtr& cfg, isc::data::ConstElementPtr duid_configuration);
-private:
-
-    /// @brief Validate and set DUID type.
-    ///
-    /// @param cfg parsed information will be stored here
-    /// @param duid_type DUID type in textual format.
-    void setType(const CfgDUIDPtr& cfg, const std::string& duid_type) const;
-
-    /// @brief Validate and set identifier.
-    ///
-    /// @param cfg parsed information will be stored here
-    /// @param identifier Identifier.
-    void setIdentifier(const CfgDUIDPtr& cfg, const std::string& identifier) const;
-
-    /// @brief Validate and set hardware type.
-    ///
-    /// @param cfg parsed information will be stored here
-    /// @param htype Hardware type.
-    void setHType(const CfgDUIDPtr& cfg, const int64_t htype) const;
-
-    /// @brief Validate and set time value.
-    ///
-    /// @param cfg parsed information will be stored here
-    /// @param new_time Time value to be used for DUID.
-    void setTime(const CfgDUIDPtr& cfg, const int64_t new_time) const;
-
-    /// @brief Validate and set enterprise id.
-    ///
-    /// @param cfg parsed information will be stored here
-    /// @param enterprise_id Enterprise id.
-    void setEnterpriseId(const CfgDUIDPtr& cfg, const int64_t enterprise_id) const;
-
-    /// @brief Set persistence flag.
-    ///
-    /// @param cfg parsed information will be stored here
-    /// @param persist A boolean value indicating if the server
-    /// identifier should be stored on the disk (if true) or
-    /// not (if false).
-    void setPersist(const CfgDUIDPtr& cfg, const bool persist);
-
-    /// @brief Verifies if the specified parameter is in range.
-    ///
-    /// Each numeric value must be in range of [0 .. max_value], where
-    /// max_value is a maximum value for the numeric type used for this
-    /// parameter.
-    ///
-    /// @param parameter_name Parameter name.
-    /// @tparam Numeric type of the specified parameter.
-    template<typename NumericType>
-    void checkRange(const std::string& parameter_name,
-                    const int64_t parameter_value) const;
 };
 
 }