]> git.ipfire.org Git - thirdparty/kea.git/commitdiff
[94-cb-implement-mysqlconfigbackenddhcpv6-prepare] Shared getOption*
authorFrancis Dupont <fdupont@isc.org>
Mon, 11 Feb 2019 12:59:21 +0000 (13:59 +0100)
committerFrancis Dupont <fdupont@isc.org>
Fri, 22 Feb 2019 22:59:25 +0000 (17:59 -0500)
src/hooks/dhcp/mysql_cb/mysql_cb_dhcp4.cc
src/hooks/dhcp/mysql_cb/mysql_cb_impl.cc
src/hooks/dhcp/mysql_cb/mysql_cb_impl.h

index 0a35a9d354f2d0c8578e01f3a834a37d6915784b..590f874192ad7fa0bcfd3781d37f26b1a2733f42 100644 (file)
@@ -1588,23 +1588,8 @@ public:
     OptionDescriptorPtr
     getOption4(const ServerSelector& server_selector, const uint16_t code,
                const std::string& space) {
-
-        if (server_selector.amUnassigned()) {
-            isc_throw(NotImplemented, "managing configuration for no particular server"
-                      " (unassigned) is unsupported at the moment");
-        }
-
-        auto tag = getServerTag(server_selector, "fetching global option");
-
-        OptionContainer options;
-        MySqlBindingCollection in_bindings = {
-            MySqlBinding::createString(tag),
-            MySqlBinding::createInteger<uint8_t>(static_cast<uint8_t>(code)),
-            MySqlBinding::createString(space)
-        };
-        getOptions(GET_OPTION4_CODE_SPACE, in_bindings, Option::V4, options);
-        return (options.empty() ? OptionDescriptorPtr() :
-                OptionDescriptorPtr(new OptionDescriptor(*options.begin())));
+        return (getOption(GET_OPTION4_CODE_SPACE, Option::V4,
+                          server_selector, code, space));;
     }
 
     /// @brief Sends query to retrieve all global options.
@@ -1613,18 +1598,8 @@ public:
     /// @return Container holding returned options.
     OptionContainer
     getAllOptions4(const ServerSelector& server_selector) {
-        OptionContainer options;
-
-        auto tags = getServerTags(server_selector);
-        for (auto tag : tags) {
-            MySqlBindingCollection in_bindings = {
-                MySqlBinding::createString(tag)
-            };
-            getOptions(MySqlConfigBackendDHCPv4Impl::GET_ALL_OPTIONS4,
-                       in_bindings, Option::V4, options);
-        }
-
-        return (options);
+        return (getAllOptions(MySqlConfigBackendDHCPv4Impl::GET_ALL_OPTIONS4,
+                              Option::V4, server_selector));
     }
 
     /// @brief Sends query to retrieve global options with modification
@@ -1635,23 +1610,12 @@ public:
     OptionContainer
     getModifiedOptions4(const ServerSelector& server_selector,
                         const boost::posix_time::ptime& modification_time) {
-        OptionContainer options;
-
-        auto tags = getServerTags(server_selector);
-        for (auto tag : tags) {
-            MySqlBindingCollection in_bindings = {
-                MySqlBinding::createString(tag),
-                MySqlBinding::createTimestamp(modification_time)
-            };
-            getOptions(MySqlConfigBackendDHCPv4Impl::GET_MODIFIED_OPTIONS4,
-                       in_bindings, Option::V4, options);
-        }
-
-        return (options);
+        return (getModifiedOptions(MySqlConfigBackendDHCPv4Impl::GET_MODIFIED_OPTIONS4,
+                                   Option::V4, server_selector, modification_time));
     }
 
     /// @brief Sends query to retrieve single option by code and option space
-    /// for a giben subnet id.
+    /// for a given subnet id.
     ///
     /// @param server_selector Server selector.
     /// @param subnet_id Subnet identifier.
@@ -1664,25 +1628,8 @@ public:
                                    const SubnetID& subnet_id,
                                    const uint16_t code,
                                    const std::string& space) {
-
-        if (server_selector.amUnassigned()) {
-            isc_throw(NotImplemented, "managing configuration for no particular server"
-                      " (unassigned) is unsupported at the moment");
-        }
-
-        auto tag = getServerTag(server_selector, "fetching subnet level option");
-
-        OptionContainer options;
-        MySqlBindingCollection in_bindings = {
-            MySqlBinding::createString(tag),
-            MySqlBinding::createInteger<uint32_t>(static_cast<uint32_t>(subnet_id)),
-            MySqlBinding::createInteger<uint8_t>(static_cast<uint8_t>(code)),
-            MySqlBinding::createString(space)
-        };
-        getOptions(GET_OPTION4_SUBNET_ID_CODE_SPACE, in_bindings, Option::V4,
-                   options);
-        return (options.empty() ? OptionDescriptorPtr() :
-                OptionDescriptorPtr(new OptionDescriptor(*options.begin())));
+        return (getOption(GET_OPTION4_SUBNET_ID_CODE_SPACE, Option::V4,
+                          server_selector, subnet_id, code, space));
     }
 
     /// @brief Sends query to retrieve single option by code and option space
@@ -1699,25 +1646,8 @@ public:
                                    const uint64_t pool_id,
                                    const uint16_t code,
                                    const std::string& space) {
-
-        if (server_selector.amUnassigned()) {
-            isc_throw(NotImplemented, "managing configuration for no particular server"
-                      " (unassigned) is unsupported at the moment");
-        }
-
-        auto tag = getServerTag(server_selector, "fetching pool level option");
-
-        OptionContainer options;
-        MySqlBindingCollection in_bindings = {
-            MySqlBinding::createString(tag),
-            MySqlBinding::createInteger<uint64_t>(pool_id),
-            MySqlBinding::createInteger<uint8_t>(static_cast<uint8_t>(code)),
-            MySqlBinding::createString(space)
-        };
-        getOptions(GET_OPTION4_POOL_ID_CODE_SPACE, in_bindings, Option::V4,
-                   options);
-        return (options.empty() ? OptionDescriptorPtr() :
-                OptionDescriptorPtr(new OptionDescriptor(*options.begin())));
+        return (getOption(GET_OPTION4_POOL_ID_CODE_SPACE, Option::V4,
+                          server_selector, pool_id, code, space));
     }
 
     /// @brief Sends query to retrieve single option by code and option space
@@ -1734,26 +1664,8 @@ public:
                                    const std::string& shared_network_name,
                                    const uint16_t code,
                                    const std::string& space) {
-
-        if (server_selector.amUnassigned()) {
-            isc_throw(NotImplemented, "managing configuration for no particular server"
-                      " (unassigned) is unsupported at the moment");
-        }
-
-        auto tag = getServerTag(server_selector, "fetching shared network"
-                                " level option");
-
-        OptionContainer options;
-        MySqlBindingCollection in_bindings = {
-            MySqlBinding::createString(tag),
-            MySqlBinding::createString(shared_network_name),
-            MySqlBinding::createInteger<uint8_t>(static_cast<uint8_t>(code)),
-            MySqlBinding::createString(space)
-        };
-        getOptions(GET_OPTION4_SHARED_NETWORK_CODE_SPACE, in_bindings, Option::V4,
-                   options);
-        return (options.empty() ? OptionDescriptorPtr() :
-                OptionDescriptorPtr(new OptionDescriptor(*options.begin())));
+        return (getOption(GET_OPTION4_SHARED_NETWORK_CODE_SPACE, Option::V4,
+                          server_selector, shared_network_name, code, space));
     }
 
     /// @brief Sends query to insert or update option definition.
index 9f3b4aa23cd5bd5509c39d6233531d417b95b0d4..a90b99332690313cc7e4898db89ee5819ea15552 100644 (file)
@@ -365,6 +365,161 @@ MySqlConfigBackendImpl::getOptionDefs(const int index,
     });
 }
 
+OptionDescriptorPtr
+MySqlConfigBackendImpl::getOption(const int index,
+                                  const Option::Universe& universe,
+                                  const ServerSelector& server_selector,
+                                  const uint16_t code,
+                                  const std::string& space) {
+
+    if (server_selector.amUnassigned()) {
+        isc_throw(NotImplemented, "managing configuration for no particular server"
+                  " (unassigned) is unsupported at the moment");
+    }
+
+    auto tag = getServerTag(server_selector, "fetching global option");
+
+    OptionContainer options;
+    MySqlBindingCollection in_bindings;
+    in_bindings.push_back(MySqlBinding::createString(tag));
+    if (universe == Option::V4) {
+        in_bindings.push_back(MySqlBinding::createInteger<uint8_t>(static_cast<uint8_t>(code)));
+    } else {
+        in_bindings.push_back(MySqlBinding::createInteger<uint16_t>(code));
+    }
+    in_bindings.push_back(MySqlBinding::createString(space));
+    getOptions(index, in_bindings, universe, options);
+    return (options.empty() ? OptionDescriptorPtr() :
+            OptionDescriptorPtr(new OptionDescriptor(*options.begin())));
+}
+
+OptionContainer
+MySqlConfigBackendImpl::getAllOptions(const int index,
+                                      const Option::Universe& universe,
+                                      const ServerSelector& server_selector) {
+    OptionContainer options;
+
+    auto tags = getServerTags(server_selector);
+    for (auto tag : tags) {
+        MySqlBindingCollection in_bindings = {
+            MySqlBinding::createString(tag)
+        };
+        getOptions(index, in_bindings, universe, options);
+    }
+
+    return (options);
+}
+
+OptionContainer
+MySqlConfigBackendImpl::getModifiedOptions(const int index,
+                                           const Option::Universe& universe,
+                                           const ServerSelector& server_selector,
+                                           const boost::posix_time::ptime& modification_time) {
+    OptionContainer options;
+
+    auto tags = getServerTags(server_selector);
+    for (auto tag : tags) {
+        MySqlBindingCollection in_bindings = {
+            MySqlBinding::createString(tag),
+            MySqlBinding::createTimestamp(modification_time)
+        };
+        getOptions(index, in_bindings, universe, options);
+    }
+
+    return (options);
+}
+
+OptionDescriptorPtr
+MySqlConfigBackendImpl::getOption(const int index,
+                                  const Option::Universe& universe,
+                                  const ServerSelector& server_selector,
+                                  const SubnetID& subnet_id,
+                                  const uint16_t code,
+                                  const std::string& space) {
+
+    if (server_selector.amUnassigned()) {
+        isc_throw(NotImplemented, "managing configuration for no particular server"
+                  " (unassigned) is unsupported at the moment");
+    }
+
+    auto tag = getServerTag(server_selector, "fetching subnet level option");
+
+    OptionContainer options;
+    MySqlBindingCollection in_bindings;
+    in_bindings.push_back(MySqlBinding::createString(tag));
+    uint32_t id = static_cast<uint32_t>(subnet_id);
+    in_bindings.push_back(MySqlBinding::createInteger<uint32_t>(id));
+    if (universe == Option::V4) {
+        in_bindings.push_back(MySqlBinding::createInteger<uint8_t>(static_cast<uint8_t>(code)));
+    } else {
+        in_bindings.push_back(MySqlBinding::createInteger<uint16_t>(code));
+    }
+    in_bindings.push_back(MySqlBinding::createString(space));
+    getOptions(index, in_bindings, universe, options);
+    return (options.empty() ? OptionDescriptorPtr() :
+            OptionDescriptorPtr(new OptionDescriptor(*options.begin())));
+}
+
+OptionDescriptorPtr
+MySqlConfigBackendImpl::getOption(const int index,
+                                  const Option::Universe& universe,
+                                  const ServerSelector& server_selector,
+                                  const uint64_t pool_id,
+                                  const uint16_t code,
+                                  const std::string& space) {
+
+    if (server_selector.amUnassigned()) {
+        isc_throw(NotImplemented, "managing configuration for no particular server"
+                  " (unassigned) is unsupported at the moment");
+    }
+
+    auto tag = getServerTag(server_selector, "fetching [pd] pool level option");
+
+    OptionContainer options;
+    MySqlBindingCollection in_bindings;
+    in_bindings.push_back(MySqlBinding::createString(tag));
+    in_bindings.push_back(MySqlBinding::createInteger<uint64_t>(pool_id));
+    if (universe == Option::V4) {
+        in_bindings.push_back(MySqlBinding::createInteger<uint8_t>(static_cast<uint8_t>(code)));
+    } else {
+        in_bindings.push_back(MySqlBinding::createInteger<uint16_t>(code));
+    }
+    in_bindings.push_back(MySqlBinding::createString(space));
+    getOptions(index, in_bindings, universe, options);
+    return (options.empty() ? OptionDescriptorPtr() :
+            OptionDescriptorPtr(new OptionDescriptor(*options.begin())));
+}
+
+OptionDescriptorPtr
+MySqlConfigBackendImpl::getOption(const int index,
+                                  const Option::Universe& universe,
+                                  const ServerSelector& server_selector,
+                                  const std::string& shared_network_name,
+                                  const uint16_t code,
+                                  const std::string& space) {
+
+    if (server_selector.amUnassigned()) {
+        isc_throw(NotImplemented, "managing configuration for no particular server"
+                  " (unassigned) is unsupported at the moment");
+    }
+
+    auto tag = getServerTag(server_selector, "fetching shared network level option");
+
+    OptionContainer options;
+    MySqlBindingCollection in_bindings;
+    in_bindings.push_back(MySqlBinding::createString(tag));
+    in_bindings.push_back(MySqlBinding::createString(shared_network_name));
+    if (universe == Option::V4) {
+        in_bindings.push_back(MySqlBinding::createInteger<uint8_t>(static_cast<uint8_t>(code)));
+    } else {
+        in_bindings.push_back(MySqlBinding::createInteger<uint16_t>(code));
+    }
+    in_bindings.push_back(MySqlBinding::createString(space));
+    getOptions(index, in_bindings, universe, options);
+    return (options.empty() ? OptionDescriptorPtr() :
+            OptionDescriptorPtr(new OptionDescriptor(*options.begin())));
+}
+
 void
 MySqlConfigBackendImpl::getOptions(const int index,
                                    const db::MySqlBindingCollection& in_bindings,
@@ -578,7 +733,5 @@ MySqlConfigBackendImpl::getPort() const {
     return (0);
 }
 
-
-
 } // end of namespace isc::dhcp
 } // end of namespace isc
index c97868dc7809cf68e9b79deb93c905f7a456ae63..2644faa6be161c29474fe40d3d693c461bcc3857 100644 (file)
@@ -15,6 +15,7 @@
 #include <dhcp/option_definition.h>
 #include <dhcpsrv/cfg_option.h>
 #include <dhcpsrv/network.h>
+#include <dhcpsrv/subnet_id.h>
 #include <exceptions/exceptions.h>
 #include <mysql/mysql_binding.h>
 #include <mysql/mysql_connection.h>
@@ -364,6 +365,101 @@ public:
                        const db::MySqlBindingCollection& in_bindings,
                        OptionDefContainer& option_defs);
 
+    /// @brief Sends query to retrieve single global option by code and
+    /// option space.
+    ///
+    /// @param index Index of the query to be used.
+    /// @param universe Option universe, i.e. V4 or V6.
+    /// @param server_selector Server selector.
+    /// @param code Option code.
+    /// @param space Option space name.
+    ///
+    /// @return Pointer to the returned option or NULL if such option
+    /// doesn't exist.
+    OptionDescriptorPtr
+    getOption(const int index, const Option::Universe& universe,
+              const db::ServerSelector& server_selector, const uint16_t code,
+              const std::string& space);
+
+    /// @brief Sends query to retrieve all global options.
+    ///
+    /// @param index Index of the query to be used.
+    /// @param universe Option universe, i.e. V4 or V6.
+    /// @param server_selector Server selector.
+    /// @return Container holding returned options.
+    OptionContainer
+    getAllOptions(const int index, const Option::Universe& universe,
+                  const db::ServerSelector& server_selector);
+
+    /// @brief Sends query to retrieve global options with modification
+    /// time later than specified timestamp.
+    ///
+    /// @param index Index of the query to be used.
+    /// @param universe Option universe, i.e. V4 or V6.
+    /// @param server_selector Server selector.
+    /// @return Container holding returned options.
+    OptionContainer
+    getModifiedOptions(const int index, const Option::Universe& universe,
+                       const db::ServerSelector& server_selector,
+                       const boost::posix_time::ptime& modification_time);
+
+    /// @brief Sends query to retrieve single option by code and option space
+    /// for a given subnet id.
+    ///
+    /// @param index Index of the query to be used.
+    /// @param universe Option universe, i.e. V4 or V6.
+    /// @param server_selector Server selector.
+    /// @param subnet_id Subnet identifier.
+    /// @param code Option code.
+    /// @param space Option space name.
+    ///
+    /// @return Pointer to the returned option descriptor or NULL if such
+    /// option doesn't exist.
+    OptionDescriptorPtr getOption(const int index,
+                                  const Option::Universe& universe,
+                                  const db::ServerSelector& server_selector,
+                                  const dhcp::SubnetID& subnet_id,
+                                  const uint16_t code,
+                                  const std::string& space);
+
+    /// @brief Sends query to retrieve single option by code and option space
+    /// for a given [pd] pool id.
+    ///
+    /// @param index Index of the query to be used.
+    /// @param universe Option universe, i.e. V4 or V6.
+    /// @param server_selector Server selector.
+    /// @param pool_id Pool identifier in the database.
+    /// @param code Option code.
+    /// @param space Option space name.
+    ///
+    /// @return Pointer to the returned option descriptor or NULL if such
+    /// option doesn't exist.
+    OptionDescriptorPtr getOption(const int index,
+                                  const Option::Universe& universe,
+                                  const db::ServerSelector& server_selector,
+                                  const uint64_t pool_id,
+                                  const uint16_t code,
+                                  const std::string& space);
+
+    /// @brief Sends query to retrieve single option by code and option space
+    /// for a given shared network.
+    ///
+    /// @param index Index of the query to be used.
+    /// @param universe Option universe, i.e. V4 or V6.
+    /// @param server_selector Server selector.
+    /// @param shared_network_name Shared network name.
+    /// @param code Option code.
+    /// @param space Option space name.
+    ///
+    /// @return Pointer to the returned option descriptor or NULL if such
+    /// option doesn't exist.
+    OptionDescriptorPtr getOption(const int index,
+                                  const Option::Universe& universe,
+                                  const db::ServerSelector& server_selector,
+                                  const std::string& shared_network_name,
+                                  const uint16_t code,
+                                  const std::string& space);
+
     /// @brief Sends query to the database to retrieve multiple options.
     ///
     /// Query should order by option_id.