lease_mgr_factory.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. // Copyright (C) 2012 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 <exceptions/exceptions.h>
  18. #include <string>
  19. namespace isc {
  20. namespace dhcp {
  21. /// @brief Invalid type exception
  22. ///
  23. /// Thrown when the factory doesn't recognise the type of the backend.
  24. class InvalidType : public Exception {
  25. public:
  26. InvalidType(const char* file, size_t line, const char* what) :
  27. isc::Exception(file, line, what) {}
  28. };
  29. /// @brief No lease manager exception
  30. ///
  31. /// Thrown if an attempt is made to get a reference to the current lease
  32. /// manager and none is currently available.
  33. class NoLeaseManager : public Exception {
  34. public:
  35. NoLeaseManager(const char* file, size_t line, const char* what) :
  36. isc::Exception(file, line, what) {}
  37. };
  38. /// @brief Lease Manager Factory
  39. ///
  40. /// This class comprises nothing but static methods used to create a lease
  41. /// manager. It analyzes the database information passed to the creation
  42. /// function and instantiates an appropriate lease manager based on the type
  43. /// requested.
  44. ///
  45. /// Strictly speaking these functions could be stand-alone functions. However,
  46. /// it is convenient to encapsulate them in a class for naming purposes.
  47. ///
  48. /// @todo: Will need to develop some form of registration mechanism for
  49. /// user-supplied backends (so that there is no need to modify the code).
  50. class LeaseMgrFactory {
  51. public:
  52. /// @brief Create an instance of a lease manager.
  53. ///
  54. /// Each database backend has its own lease manager type. This static
  55. /// method sets the "current" lease manager to be a manager of the
  56. /// appropriate type. The actual lease manager is returned by the
  57. /// "instance" method.
  58. ///
  59. /// @note When called, the current lease manager is <b>always</b> destroyed
  60. /// and a new one created - even if the parameters are the same.
  61. ///
  62. /// dbaccess is a generic way of passing parameters. Parameters are passed
  63. /// in the "name=value" format, separated by spaces. The data MUST include
  64. /// a keyword/value pair of the form "type=dbtype" giving the database
  65. /// type, e.q. "mysql" or "sqlite3".
  66. ///
  67. /// @param dbaccess Database access parameters. These are in the form of
  68. /// "keyword=value" pairs, separated by spaces. They are backend-
  69. /// -end specific, although must include the "type" keyword which
  70. /// gives the backend in use.
  71. ///
  72. /// @throw isc::InvalidParameter dbaccess string does not contain the "type"
  73. /// keyword.
  74. /// @throw isc::dhcp::InvalidType The "type" keyword in dbaccess does not
  75. /// identify a supported backend.
  76. static void create(const std::string& dbaccess);
  77. /// @brief Destroy lease manager
  78. ///
  79. /// Destroys the current lease manager object. This should have the effect
  80. /// of closing the database connection. The method is a no-op if no
  81. /// lease manager is available.
  82. static void destroy();
  83. /// @brief Return current lease manager
  84. ///
  85. /// Returns an instance of the "current" lease manager. An exception
  86. /// will be thrown if none is available.
  87. ///
  88. /// @throw isc::dhcp::NoLeaseManager No lease manager is available: use
  89. /// create() to create one before calling this method.
  90. static LeaseMgr& instance();
  91. /// @brief Parse database access string
  92. ///
  93. /// Parses the string of "keyword=value" pairs and separates them
  94. /// out into the map.
  95. ///
  96. /// @param dbaccess Database access string.
  97. ///
  98. /// @return std::map<std::string, std::string> Map of keyword/value pairs.
  99. static LeaseMgr::ParameterMap parse(const std::string& dbaccess);
  100. /// @brief Redact database access string
  101. ///
  102. /// Takes the database parameters and returns a database access string
  103. /// passwords replaced by asterisks. This string is used in log messages.
  104. ///
  105. /// @param parameters Database access parameters (output of "parse").
  106. ///
  107. /// @return Redacted database access string.
  108. static std::string redactedAccessString(
  109. const LeaseMgr::ParameterMap& parameters);
  110. private:
  111. /// @brief Hold pointer to lease manager
  112. ///
  113. /// Holds a pointer to the singleton lease manager. The singleton
  114. /// is encapsulated in this method to avoid a "static initialization
  115. /// fiasco" if defined in an external static variable.
  116. static boost::scoped_ptr<LeaseMgr>& getLeaseMgrPtr();
  117. };
  118. }; // end of isc::dhcp namespace
  119. }; // end of isc namespace
  120. #endif // LEASE_MGR_FACTORY_H