Browse Source

[5044] DUID parser migrated to SimpleParser

Tomek Mrugalski 8 years ago
parent
commit
c8a0e1c07a

+ 8 - 2
src/bin/dhcp6/json_config_parser.cc

@@ -725,8 +725,7 @@ DhcpConfigParser* createGlobal6DhcpConfigParser(const std::string& config_id,
         parser = new ExpirationConfigParser();
     } else if (config_id.compare("client-classes") == 0) {
         parser = new ClientClassDefListParser(config_id, globalContext());
-    } else if (config_id.compare("server-id") == 0) {
-        parser = new DUIDConfigParser();
+    // server-id has been migrated to SimpleParser
     } else if (config_id.compare("host-reservation-identifiers") == 0) {
         parser = new HostReservationIdsParser6();
     } else {
@@ -921,6 +920,13 @@ configureDhcp6Server(Dhcpv6Srv&, isc::data::ConstElementPtr config_set) {
                 continue;
             }
 
+            if (config_pair.first == "server-id") {
+                DUIDConfigParser parser;
+                const CfgDUIDPtr& cfg = CfgMgr::instance().getStagingCfg()->getCfgDUID();
+                parser.parse(cfg, config_pair.second);
+                continue;
+            }
+
             ParserPtr parser(createGlobal6DhcpConfigParser(config_pair.first,
                                                            config_pair.second));
             LOG_DEBUG(dhcp6_logger, DBG_DHCP6_DETAIL, DHCP6_PARSER_CREATED)

+ 19 - 25
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,28 +23,28 @@ using namespace isc::data;
 namespace isc {
 namespace dhcp {
 
-DUIDConfigParser::DUIDConfigParser()
-    : DhcpConfigParser() {
-}
-
 void
-DUIDConfigParser::build(isc::data::ConstElementPtr duid_configuration) {
+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(element.second->stringValue());
+                setType(cfg, element.second->stringValue());
             } else if (element.first == "identifier") {
-                setIdentifier(element.second->stringValue());
+                setIdentifier(cfg, element.second->stringValue());
             } else if (element.first == "htype") {
-                setHType(element.second->intValue());
+                setHType(cfg, element.second->intValue());
             } else if (element.first == "time") {
-                setTime(element.second->intValue());
+                setTime(cfg, element.second->intValue());
             } else if (element.first == "enterprise-id") {
-                setEnterpriseId(element.second->intValue());
+                setEnterpriseId(cfg, element.second->intValue());
             } else if (element.first == "persist") {
-                setPersist(element.second->boolValue());
+                setPersist(cfg, element.second->boolValue());
             } else {
                 isc_throw(DhcpConfigError, "unsupported configuration "
                           "parameter '" << element.first << "'");
@@ -66,7 +67,7 @@ DUIDConfigParser::build(isc::data::ConstElementPtr duid_configuration) {
 }
 
 void
-DUIDConfigParser::setType(const std::string& duid_type) const {
+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") {
@@ -80,41 +81,34 @@ DUIDConfigParser::setType(const std::string& duid_type) const {
                   << 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();
+DUIDConfigParser::setIdentifier(const CfgDUIDPtr& cfg, const std::string& identifier) const {
     cfg->setIdentifier(identifier);
 }
 
 void
-DUIDConfigParser::setHType(const int64_t htype) const {
-    const CfgDUIDPtr& cfg = CfgMgr::instance().getStagingCfg()->getCfgDUID();
+DUIDConfigParser::setHType(const CfgDUIDPtr& cfg, const int64_t htype) const {
     checkRange<uint16_t>("htype", htype);
     cfg->setHType(static_cast<uint16_t>(htype));
-
 }
 
 void
-DUIDConfigParser::setTime(const int64_t new_time) const {
-    const CfgDUIDPtr& cfg = CfgMgr::instance().getStagingCfg()->getCfgDUID();
+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));
 }
 
 void
-DUIDConfigParser::setEnterpriseId(const int64_t enterprise_id) const {
-    const CfgDUIDPtr& cfg = CfgMgr::instance().getStagingCfg()->getCfgDUID();
+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));
 }
 
 void
-DUIDConfigParser::setPersist(const bool persist) {
-    const CfgDUIDPtr& cfg = CfgMgr::instance().getStagingCfg()->getCfgDUID();
+DUIDConfigParser::setPersist(const CfgDUIDPtr& cfg, const bool persist) {
     cfg->setPersist(persist);
 }
 

+ 17 - 18
src/lib/dhcpsrv/parsers/duid_config_parser.h

@@ -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
@@ -8,7 +8,7 @@
 #define DUID_CONFIG_PARSER_H
 
 #include <cc/data.h>
-#include <dhcpsrv/parsers/dhcp_config_parser.h>
+#include <cc/simple_parser.h>
 #include <stdint.h>
 #include <string>
 
@@ -23,56 +23,55 @@ namespace dhcp {
 /// - DUID-LL
 ///
 /// @todo Add support for DUID-UUID in the parser.
-class DUIDConfigParser : public DhcpConfigParser {
+class DUIDConfigParser : public isc::data::SimpleParser {
 public:
-
-    /// @brief Constructor.
-    DUIDConfigParser();
-
     /// @brief Parses DUID configuration.
     ///
+    /// @param cfg parsed DUID configuration will be stored here
     /// @param duid_configuration Data element holding a map representing
     /// DUID configuration.
     ///
     /// @throw DhcpConfigError If the configuration is invalid.
-    virtual void build(isc::data::ConstElementPtr duid_configuration);
-
-    /// @brief Commit, unused.
-    virtual void commit() { }
-
+    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 textfual format.
-    void setType(const std::string& duid_type) const;
+    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 std::string& identifier) const;
+    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 int64_t htype) const;
+    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 int64_t new_time) const;
+    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 int64_t enterprise_id) const;
+    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 bool persist);
+    void setPersist(const CfgDUIDPtr& cfg, const bool persist);
 
     /// @brief Verifies if the specified parameter is in range.
     ///

+ 27 - 17
src/lib/dhcpsrv/tests/duid_config_parser_unittest.cc

@@ -1,4 +1,4 @@
-// Copyright (C) 2015-2016 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
@@ -9,6 +9,7 @@
 #include <dhcpsrv/cfgmgr.h>
 #include <dhcpsrv/cfg_duid.h>
 #include <dhcpsrv/parsers/duid_config_parser.h>
+#include <dhcpsrv/parsers/dhcp_parsers.h>
 #include <dhcpsrv/testutils/config_result_check.h>
 #include <util/encode/hex.h>
 #include <gtest/gtest.h>
@@ -26,6 +27,13 @@ namespace {
 class DUIDConfigParserTest : public ::testing::Test {
 public:
 
+    /// @brief constructor
+    ///
+    /// Initializes cfg_duid_ to a new empty object
+    DUIDConfigParserTest()
+        :cfg_duid_(new CfgDUID()){
+    }
+
     /// @brief Creates simple configuration with DUID type only.
     ///
     /// @param duid_type DUID type in the textual format.
@@ -82,9 +90,12 @@ public:
     /// @param vec Input vector.
     /// @return String of hexadecimal digits converted from vector.
     std::string toString(const std::vector<uint8_t>& vec) const;
+
+    /// Config DUID pointer
+    CfgDUIDPtr cfg_duid_;
 };
 
-std::string 
+std::string
 DUIDConfigParserTest::createConfigWithType(const std::string& duid_type) const {
     std::ostringstream s;
     s << "{ \"type\": \"" << duid_type << "\" }";
@@ -103,10 +114,9 @@ void
 DUIDConfigParserTest::build(const std::string& config) const {
     ElementPtr config_element = Element::fromJSON(config);
     DUIDConfigParser parser;
-    parser.build(config_element);
+    parser.parse(cfg_duid_, config_element);
 }
 
-
 void
 DUIDConfigParserTest::testTypeOnly(const DUID::DUIDType& duid_type,
                                    const std::string& duid_type_text) const {
@@ -115,12 +125,12 @@ DUIDConfigParserTest::testTypeOnly(const DUID::DUIDType& duid_type,
 
     // Make sure that the type is correct and that other parameters are set
     // to their defaults.
-    CfgDUIDPtr cfg_duid = CfgMgr::instance().getStagingCfg()->getCfgDUID();
-    EXPECT_EQ(duid_type, cfg_duid->getType());
-    EXPECT_TRUE(cfg_duid->getIdentifier().empty());
-    EXPECT_EQ(0, cfg_duid->getHType());
-    EXPECT_EQ(0, cfg_duid->getTime());
-    EXPECT_EQ(0, cfg_duid->getEnterpriseId());
+    ASSERT_TRUE(cfg_duid_);
+    EXPECT_EQ(duid_type, cfg_duid_->getType());
+    EXPECT_TRUE(cfg_duid_->getIdentifier().empty());
+    EXPECT_EQ(0, cfg_duid_->getHType());
+    EXPECT_EQ(0, cfg_duid_->getTime());
+    EXPECT_EQ(0, cfg_duid_->getEnterpriseId());
 }
 
 void
@@ -181,13 +191,13 @@ TEST_F(DUIDConfigParserTest, allParameters) {
                           "}"));
 
     // Verify that parameters have been set correctly.
-    CfgDUIDPtr cfg_duid = CfgMgr::instance().getStagingCfg()->getCfgDUID();
-    EXPECT_EQ(DUID::DUID_EN, cfg_duid->getType());
-    EXPECT_EQ("ABCDEF", toString(cfg_duid->getIdentifier()));
-    EXPECT_EQ(8, cfg_duid->getHType());
-    EXPECT_EQ(100, cfg_duid->getTime());
-    EXPECT_EQ(2024, cfg_duid->getEnterpriseId());
-    EXPECT_FALSE(cfg_duid->persist());
+    ASSERT_TRUE(cfg_duid_);
+    EXPECT_EQ(DUID::DUID_EN, cfg_duid_->getType());
+    EXPECT_EQ("ABCDEF", toString(cfg_duid_->getIdentifier()));
+    EXPECT_EQ(8, cfg_duid_->getHType());
+    EXPECT_EQ(100, cfg_duid_->getTime());
+    EXPECT_EQ(2024, cfg_duid_->getEnterpriseId());
+    EXPECT_FALSE(cfg_duid_->persist());
 }
 
 // Test out of range values for time.