lease_mgr_factory.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. // Copyright (C) 2012, 2015 Internet Systems Consortium, Inc. ("ISC")
  2. //
  3. // Permission to use, copy, modify, and/or distribute this software for any
  4. // purpose with or without fee is hereby granted, provided that the above
  5. // copyright notice and this permission notice appear in all copies.
  6. //
  7. // THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
  8. // REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
  9. // AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
  10. // INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
  11. // LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
  12. // OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  13. // PERFORMANCE OF THIS SOFTWARE.
  14. #ifndef LEASE_MGR_FACTORY_H
  15. #define LEASE_MGR_FACTORY_H
  16. #include <dhcpsrv/lease_mgr.h>
  17. #include <dhcpsrv/data_source.h>
  18. #include <exceptions/exceptions.h>
  19. #include <string>
  20. namespace isc {
  21. namespace dhcp {
  22. /// @brief Invalid type exception
  23. ///
  24. /// Thrown when the factory doesn't recognize the type of the backend.
  25. class InvalidType : public Exception {
  26. public:
  27. InvalidType(const char* file, size_t line, const char* what) :
  28. isc::Exception(file, line, what) {}
  29. };
  30. /// @brief No lease manager exception
  31. ///
  32. /// Thrown if an attempt is made to get a reference to the current lease
  33. /// manager and none is currently available.
  34. class NoLeaseManager : public Exception {
  35. public:
  36. NoLeaseManager(const char* file, size_t line, const char* what) :
  37. isc::Exception(file, line, what) {}
  38. };
  39. /// @brief Lease Manager Factory
  40. ///
  41. /// This class comprises nothing but static methods used to create a lease
  42. /// manager. It analyzes the database information passed to the creation
  43. /// function and instantiates an appropriate lease manager based on the type
  44. /// requested.
  45. ///
  46. /// Strictly speaking these functions could be stand-alone functions. However,
  47. /// it is convenient to encapsulate them in a class for naming purposes.
  48. ///
  49. /// @todo: Will need to develop some form of registration mechanism for
  50. /// user-supplied backends (so that there is no need to modify the code).
  51. class LeaseMgrFactory {
  52. public:
  53. /// @brief Create an instance of a lease manager.
  54. ///
  55. /// Each database backend has its own lease manager type. This static
  56. /// method sets the "current" lease manager to be a manager of the
  57. /// appropriate type. The actual lease manager is returned by the
  58. /// "instance" method.
  59. ///
  60. /// @note When called, the current lease manager is <b>always</b> destroyed
  61. /// and a new one created - even if the parameters are the same.
  62. ///
  63. /// dbaccess is a generic way of passing parameters. Parameters are passed
  64. /// in the "name=value" format, separated by spaces. The data MUST include
  65. /// a keyword/value pair of the form "type=dbtype" giving the database
  66. /// type, e.q. "mysql" or "sqlite3".
  67. ///
  68. /// @param dbaccess Database access parameters. These are in the form of
  69. /// "keyword=value" pairs, separated by spaces. They are backend-
  70. /// -end specific, although must include the "type" keyword which
  71. /// gives the backend in use.
  72. ///
  73. /// @throw isc::InvalidParameter dbaccess string does not contain the "type"
  74. /// keyword.
  75. /// @throw isc::dhcp::InvalidType The "type" keyword in dbaccess does not
  76. /// identify a supported backend.
  77. static void create(const std::string& dbaccess);
  78. /// @brief Destroy lease manager
  79. ///
  80. /// Destroys the current lease manager object. This should have the effect
  81. /// of closing the database connection. The method is a no-op if no
  82. /// lease manager is available.
  83. static void destroy();
  84. /// @brief Return current lease manager
  85. ///
  86. /// Returns an instance of the "current" lease manager. An exception
  87. /// will be thrown if none is available.
  88. ///
  89. /// @throw isc::dhcp::NoLeaseManager No lease manager is available: use
  90. /// create() to create one before calling this method.
  91. static LeaseMgr& instance();
  92. /// @brief Parse database access string
  93. ///
  94. /// Parses the string of "keyword=value" pairs and separates them
  95. /// out into the map.
  96. ///
  97. /// @param dbaccess Database access string.
  98. ///
  99. /// @return std::map<std::string, std::string> Map of keyword/value pairs.
  100. static DataSource::ParameterMap parse(const std::string& dbaccess);
  101. /// @brief Redact database access string
  102. ///
  103. /// Takes the database parameters and returns a database access string
  104. /// passwords replaced by asterisks. This string is used in log messages.
  105. ///
  106. /// @param parameters Database access parameters (output of "parse").
  107. ///
  108. /// @return Redacted database access string.
  109. static std::string redactedAccessString(
  110. const DataSource::ParameterMap& parameters);
  111. private:
  112. /// @brief Hold pointer to lease manager
  113. ///
  114. /// Holds a pointer to the singleton lease manager. The singleton
  115. /// is encapsulated in this method to avoid a "static initialization
  116. /// fiasco" if defined in an external static variable.
  117. static boost::scoped_ptr<LeaseMgr>& getLeaseMgrPtr();
  118. };
  119. }; // end of isc::dhcp namespace
  120. }; // end of isc namespace
  121. #endif // LEASE_MGR_FACTORY_H