Parcourir la source

[3358] D2ConfigParser supplies its own default values

Added a method to ValueType<> template, getOptionalParam.
Defined constants for dhcp-ddns default values in D2ClientConfig.
D2ClientConfigParser uses these in conjuction with calls to
ValueType<>getOptionalParam() to ensure all the parameters needed
have values.
Modifed unit tests accordingly.
Thomas Markwalder il y a 11 ans
Parent
commit
a8560aefbf

+ 2 - 1
src/bin/dhcp4/tests/config_parser_unittest.cc

@@ -2810,7 +2810,7 @@ TEST_F(Dhcp4ParserTest, invalidD2ClientConfig) {
     ConstElementPtr status;
 
     // Configuration string with an invalid D2 client config,
-    // "server-ip" is missing.
+    // "server-ip" is invalid.
     string config_str = "{ \"interfaces\": [ \"*\" ],"
         "\"rebind-timer\": 2000, "
         "\"renew-timer\": 1000, "
@@ -2819,6 +2819,7 @@ TEST_F(Dhcp4ParserTest, invalidD2ClientConfig) {
         "    \"subnet\": \"192.0.2.0/24\" } ],"
         " \"dhcp-ddns\" : {"
         "     \"enable-updates\" : true, "
+        "     \"server-ip\" : \"bogus-value\", "
         "     \"server-port\" : 5301, "
         "     \"ncr-protocol\" : \"UDP\", "
         "     \"ncr-format\" : \"JSON\", "

+ 2 - 1
src/bin/dhcp6/tests/config_parser_unittest.cc

@@ -3162,7 +3162,7 @@ TEST_F(Dhcp6ParserTest, invalidD2ClientConfig) {
     ConstElementPtr status;
 
     // Configuration string with an invalid D2 client config,
-    // "server-ip" is missing.
+    // "server-ip" is invalid.
     string config_str = "{ \"interfaces\": [ \"*\" ],"
         "\"rebind-timer\": 2000, "
         "\"renew-timer\": 1000, "
@@ -3171,6 +3171,7 @@ TEST_F(Dhcp6ParserTest, invalidD2ClientConfig) {
         "    \"subnet\": \"2001:db8:1::/64\" } ], "
         " \"dhcp-ddns\" : {"
         "     \"enable-updates\" : true, "
+        "     \"server-ip\" : \"bogus-value\", "
         "     \"server-port\" : 5301, "
         "     \"ncr-protocol\" : \"UDP\", "
         "     \"ncr-format\" : \"JSON\", "

+ 10 - 0
src/lib/dhcpsrv/d2_client_cfg.cc

@@ -23,6 +23,16 @@ using namespace std;
 namespace isc {
 namespace dhcp {
 
+const char *D2ClientConfig::DFT_SERVER_IP = "127.0.0.1";
+const size_t D2ClientConfig::DFT_SERVER_PORT = 53001;
+const char *D2ClientConfig::DFT_NCR_PROTOCOL = "UDP";
+const char *D2ClientConfig::DFT_NCR_FORMAT = "JSON";
+const bool D2ClientConfig::DFT_OVERRIDE_NO_UPDATE = false;
+const bool D2ClientConfig::DFT_OVERRIDE_CLIENT_UPDATE = false;
+const bool D2ClientConfig::DFT_REPLACE_CLIENT_NAME = false;
+const char *D2ClientConfig::DFT_GENERATED_PREFIX = "myhost";
+const char *D2ClientConfig::DFT_QUALIFYING_SUFFIX = "example.com";
+
 D2ClientConfig::D2ClientConfig(const  bool enable_updates,
                                const isc::asiolink::IOAddress& server_ip,
                                const size_t server_port,

+ 15 - 0
src/lib/dhcpsrv/d2_client_cfg.h

@@ -55,6 +55,21 @@ public:
 ///
 class D2ClientConfig {
 public:
+
+
+    /// @brief Default configuration constants.
+    /// @todo For now these are hard-coded as configuraiton layer cannot
+    /// readily provide them (see Trac #3358).
+    static const char *DFT_SERVER_IP;
+    static const size_t DFT_SERVER_PORT;
+    static const char *DFT_NCR_PROTOCOL;
+    static const char *DFT_NCR_FORMAT;
+    static const bool DFT_OVERRIDE_NO_UPDATE;
+    static const bool DFT_OVERRIDE_CLIENT_UPDATE;
+    static const bool DFT_REPLACE_CLIENT_NAME;
+    static const char *DFT_GENERATED_PREFIX;
+    static const char *DFT_QUALIFYING_SUFFIX;
+
     /// @brief Constructor
     ///
     /// @param enable_updates Enables DHCP-DDNS updates

+ 39 - 17
src/lib/dhcpsrv/dhcp_parsers.cc

@@ -1273,27 +1273,49 @@ D2ClientConfigParser::build(isc::data::ConstElementPtr client_config) {
     }
 
     // Get all parameters that are needed to create the D2ClientConfig.
-    asiolink::IOAddress server_ip(string_values_->getParam("server-ip"));
-
-    uint32_t server_port = uint32_values_->getParam("server-port");
-
-    dhcp_ddns::NameChangeProtocol
-    ncr_protocol = dhcp_ddns:: stringToNcrProtocol(string_values_->
-                                                   getParam("ncr-protocol"));
+    asiolink::IOAddress server_ip(string_values_->
+                                  getOptionalParam("server-ip",
+                                                   D2ClientConfig::
+                                                   DFT_SERVER_IP));
+
+    uint32_t server_port = uint32_values_->getOptionalParam("server-port",
+                                                             D2ClientConfig::
+                                                             DFT_SERVER_PORT);
+    dhcp_ddns::NameChangeProtocol ncr_protocol
+        = dhcp_ddns::stringToNcrProtocol(string_values_->
+                                         getOptionalParam("ncr-protocol",
+                                                          D2ClientConfig::
+                                                          DFT_NCR_PROTOCOL));
+    dhcp_ddns::NameChangeFormat ncr_format
+        = dhcp_ddns::stringToNcrFormat(string_values_->
+                                       getOptionalParam("ncr-format",
+                                                          D2ClientConfig::
+                                                          DFT_NCR_FORMAT));
+    std::string generated_prefix = string_values_->
+                                   getOptionalParam("generated-prefix",
+                                                    D2ClientConfig::
+                                                    DFT_GENERATED_PREFIX);
+    std::string qualifying_suffix = string_values_->
+                                    getOptionalParam("qualifying-suffix",
+                                                     D2ClientConfig::
+                                                     DFT_QUALIFYING_SUFFIX);
 
-    dhcp_ddns::NameChangeFormat
-    ncr_format = dhcp_ddns::stringToNcrFormat(string_values_->
-                                              getParam("ncr-format"));
+    bool always_include_fqdn = boolean_values_->
+                               getOptionalParam("always-include-fqdn", false);
 
-    std::string generated_prefix = string_values_->getParam("generated-prefix");
-    std::string qualifying_suffix = string_values_->
-                                    getParam("qualifying-suffix");
+    bool override_no_update = boolean_values_->
+                              getOptionalParam("override-no-update",
+                                               D2ClientConfig::
+                                               DFT_OVERRIDE_NO_UPDATE);
 
-    bool always_include_fqdn = boolean_values_->getParam("always-include-fqdn");
-    bool override_no_update = boolean_values_->getParam("override-no-update");
     bool override_client_update = boolean_values_->
-                                  getParam("override-client-update");
-    bool replace_client_name = boolean_values_->getParam("replace-client-name");
+                                  getOptionalParam("override-client-update",
+                                                   D2ClientConfig::
+                                                   DFT_OVERRIDE_CLIENT_UPDATE);
+    bool replace_client_name = boolean_values_->
+                               getOptionalParam("replace-client-name",
+                                                D2ClientConfig::
+                                                DFT_REPLACE_CLIENT_NAME);
 
     // Attempt to create the new client config.
     local_client_config_.reset(new D2ClientConfig(enable_updates, server_ip,

+ 25 - 0
src/lib/dhcpsrv/dhcp_parsers.h

@@ -90,6 +90,28 @@ class ValueStorage {
             return (param->second);
         }
 
+        /// @brief Returns the data value for an optional parameter.
+        ///
+        /// Finds and returns the data value for the given parameter or
+        /// a supplied default value if it is not found.
+        ///
+        /// @param name is the name of the parameter for which the data
+        /// value is desired.
+        /// @param default_value value to use the default
+        ///
+        /// @return The paramater's data value of type @c ValueType.
+        ValueType getOptionalParam(const std::string& name,
+                                   const ValueType& default_value) const {
+            typename std::map<std::string, ValueType>::const_iterator param
+                = values_.find(name);
+
+            if (param == values_.end()) {
+                return (default_value);
+            }
+
+            return (param->second);
+        }
+
         /// @brief  Remove the parameter from the store.
         ///
         /// Deletes the entry for the given parameter from the store if it
@@ -957,6 +979,9 @@ public:
     ///
     /// The results of the parsing are retained internally for use during
     /// commit.
+    /// @todo This parser supplies hard-coded default values for all
+    /// optional parameters.  This should be changed once a new plan
+    /// for configuration is determined.
     ///
     /// @param client_config is the "dhcp-ddns" configuration to parse
     virtual void build(isc::data::ConstElementPtr client_config);

+ 47 - 19
src/lib/dhcpsrv/tests/dhcp_parsers_unittest.cc

@@ -821,29 +821,57 @@ TEST_F(ParseConfigTest, validDisabledD2Config) {
     EXPECT_FALSE(d2_client_config->getEnableUpdates());
 }
 
-/// @brief Check various invalid D2 client configurations.
-TEST_F(ParseConfigTest, invalidD2Config) {
-    std::string invalid_configs[] = {
-        // only the enable flag of true
+/// @brief Checks that given a partial configuration, parser supplies
+/// default values
+TEST_F(ParseConfigTest, parserDefaultsD2Config) {
+
+    // Configuration string.  This contains a set of valid libraries.
+    std::string config_str =
         "{ \"dhcp-ddns\" :"
         "    {"
         "     \"enable-updates\" : true"
         "    }"
-        "}",
-        // Missing server ip value
+        "}";
+
+    // Verify that the configuration string parses.
+    int rcode = parseConfiguration(config_str);
+    ASSERT_TRUE(rcode == 0) << error_text_;
+
+    // Verify that DHCP-DDNS is enabled.
+    EXPECT_TRUE(CfgMgr::instance().ddnsEnabled());
+
+    // Make sure fetched config is correct.
+    D2ClientConfigPtr d2_client_config;
+    ASSERT_NO_THROW(d2_client_config = CfgMgr::instance().getD2ClientConfig());
+    EXPECT_TRUE(d2_client_config);
+    EXPECT_TRUE(d2_client_config->getEnableUpdates());
+    EXPECT_EQ(D2ClientConfig::DFT_SERVER_IP,
+              d2_client_config->getServerIp().toText());
+    EXPECT_EQ(D2ClientConfig::DFT_SERVER_PORT,
+              d2_client_config->getServerPort());
+    EXPECT_EQ(dhcp_ddns::stringToNcrProtocol(D2ClientConfig::DFT_NCR_PROTOCOL),
+              d2_client_config->getNcrProtocol());
+    EXPECT_EQ(dhcp_ddns::stringToNcrFormat(D2ClientConfig::DFT_NCR_FORMAT),
+              d2_client_config->getNcrFormat());
+    EXPECT_EQ(D2ClientConfig::DFT_OVERRIDE_NO_UPDATE,
+              d2_client_config->getOverrideNoUpdate());
+    EXPECT_EQ(D2ClientConfig::DFT_OVERRIDE_CLIENT_UPDATE,
+              d2_client_config->getOverrideClientUpdate());
+    EXPECT_EQ(D2ClientConfig::DFT_REPLACE_CLIENT_NAME,
+              d2_client_config->getReplaceClientName());
+    EXPECT_EQ(D2ClientConfig::DFT_GENERATED_PREFIX,
+              d2_client_config->getGeneratedPrefix());
+    EXPECT_EQ(D2ClientConfig::DFT_QUALIFYING_SUFFIX,
+              d2_client_config->getQualifyingSuffix());
+}
+
+
+/// @brief Check various invalid D2 client configurations.
+TEST_F(ParseConfigTest, invalidD2Config) {
+    std::string invalid_configs[] = {
+        // Must supply at lease enable-updates
         "{ \"dhcp-ddns\" :"
         "    {"
-        "     \"enable-updates\" : true, "
-        //"     \"server-ip\" : \"192.0.2.0\", "
-        "     \"server-port\" : 53001, "
-        "     \"ncr-protocol\" : \"UDP\", "
-        "     \"ncr-format\" : \"JSON\", "
-        "     \"always-include-fqdn\" : true, "
-        "     \"override-no-update\" : true, "
-        "     \"override-client-update\" : true, "
-        "     \"replace-client-name\" : true, "
-        "     \"generated-prefix\" : \"test.prefix\", "
-        "     \"qualifying-suffix\" : \"test.suffix.\" "
         "    }"
         "}",
         // Invalid server ip value
@@ -910,12 +938,12 @@ TEST_F(ParseConfigTest, invalidD2Config) {
         "     \"qualifying-suffix\" : \"test.suffix.\" "
         "    }"
         "}",
-        // Missig Port
+        // Invalid Port
         "{ \"dhcp-ddns\" :"
         "    {"
         "     \"enable-updates\" : true, "
         "     \"server-ip\" : \"192.0.2.0\", "
-        // "     \"server-port\" : 53001, "
+        "     \"server-port\" : \"bogus\", "
         "     \"ncr-protocol\" : \"UDP\", "
         "     \"ncr-format\" : \"JSON\", "
         "     \"always-include-fqdn\" : true, "