host_data_source_factory.cc 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. // Copyright (C) 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. #include "config.h"
  15. #include <dhcpsrv/dhcpsrv_log.h>
  16. #include <dhcpsrv/host_data_source_factory.h>
  17. #include <dhcpsrv/hosts_log.h>
  18. #ifdef HAVE_MYSQL
  19. #include <dhcpsrv/mysql_host_data_source.h>
  20. #endif
  21. #include <boost/algorithm/string.hpp>
  22. #include <boost/foreach.hpp>
  23. #include <boost/scoped_ptr.hpp>
  24. #include <algorithm>
  25. #include <iostream>
  26. #include <iterator>
  27. #include <map>
  28. #include <sstream>
  29. #include <utility>
  30. using namespace std;
  31. namespace isc {
  32. namespace dhcp {
  33. boost::scoped_ptr<BaseHostDataSource>&
  34. HostDataSourceFactory::getHostDataSourcePtr() {
  35. static boost::scoped_ptr<BaseHostDataSource> hostDataSourcePtr;
  36. return (hostDataSourcePtr);
  37. }
  38. void
  39. HostDataSourceFactory::create(const std::string& dbaccess) {
  40. const std::string type = "type";
  41. // Parse the access string and create a redacted string for logging.
  42. DatabaseConnection::ParameterMap parameters =
  43. DatabaseConnection::parse(dbaccess);
  44. std::string redacted =
  45. DatabaseConnection::redactedAccessString(parameters);
  46. // Is "type" present?
  47. if (parameters.find(type) == parameters.end()) {
  48. LOG_ERROR(dhcpsrv_logger, DHCPSRV_NOTYPE_DB).arg(dbaccess);
  49. isc_throw(InvalidParameter, "Database configuration parameters do not "
  50. "contain the 'type' keyword");
  51. }
  52. // Yes, check what it is.
  53. #ifdef HAVE_MYSQL
  54. if (parameters[type] == string("mysql")) {
  55. LOG_INFO(dhcpsrv_logger, DHCPSRV_MYSQL_DB).arg(redacted);
  56. getHostDataSourcePtr().reset(new MySqlHostDataSource(parameters));
  57. return;
  58. }
  59. #endif
  60. #ifdef HAVE_PGSQL
  61. if (parameters[type] == string("postgresql")) {
  62. LOG_INFO(dhcpsrv_logger, DHCPSRV_PGSQL_DB).arg(redacted);
  63. isc_throw(NotImplemented, "Sorry, Postgres backend for host reservations "
  64. "is not implemented yet.");
  65. // Set pgsql data source here, when it will be implemented.
  66. return;
  67. }
  68. #endif
  69. // Get here on no match.
  70. LOG_ERROR(dhcpsrv_logger, DHCPSRV_UNKNOWN_DB).arg(parameters[type]);
  71. isc_throw(InvalidType, "Database access parameter 'type' does "
  72. "not specify a supported database backend");
  73. }
  74. void
  75. HostDataSourceFactory::destroy() {
  76. // Destroy current host data source instance. This is a no-op if no host
  77. // data source is available.
  78. if (getHostDataSourcePtr()) {
  79. LOG_DEBUG(dhcpsrv_logger, DHCPSRV_DBG_TRACE, HOSTS_CFG_CLOSE_HOST_DATA_SOURCE)
  80. .arg(getHostDataSourcePtr()->getType());
  81. }
  82. getHostDataSourcePtr().reset();
  83. }
  84. BaseHostDataSource&
  85. HostDataSourceFactory::instance() {
  86. BaseHostDataSource* hdsptr = getHostDataSourcePtr().get();
  87. if (hdsptr == NULL) {
  88. isc_throw(NoHostDataSourceManager,
  89. "no current host data source instance is available");
  90. }
  91. return (*hdsptr);
  92. }
  93. }; // namespace dhcp
  94. }; // namespace isc