host_data_source_factory.cc 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. // Copyright (C) 2015-2016 Internet Systems Consortium, Inc. ("ISC")
  2. //
  3. // This Source Code Form is subject to the terms of the Mozilla Public
  4. // License, v. 2.0. If a copy of the MPL was not distributed with this
  5. // file, You can obtain one at http://mozilla.org/MPL/2.0/.
  6. #include "config.h"
  7. #include <dhcpsrv/dhcpsrv_log.h>
  8. #include <dhcpsrv/host_data_source_factory.h>
  9. #include <dhcpsrv/hosts_log.h>
  10. #ifdef HAVE_MYSQL
  11. #include <dhcpsrv/mysql_host_data_source.h>
  12. #endif
  13. #ifdef HAVE_PGSQL
  14. #include <dhcpsrv/pgsql_host_data_source.h>
  15. #endif
  16. #include <boost/algorithm/string.hpp>
  17. #include <boost/foreach.hpp>
  18. #include <boost/scoped_ptr.hpp>
  19. #include <algorithm>
  20. #include <iostream>
  21. #include <iterator>
  22. #include <map>
  23. #include <sstream>
  24. #include <utility>
  25. using namespace std;
  26. namespace isc {
  27. namespace dhcp {
  28. HostDataSourcePtr&
  29. HostDataSourceFactory::getHostDataSourcePtr() {
  30. static HostDataSourcePtr hostDataSourcePtr;
  31. return (hostDataSourcePtr);
  32. }
  33. void
  34. HostDataSourceFactory::create(const std::string& dbaccess) {
  35. // Parse the access string and create a redacted string for logging.
  36. DatabaseConnection::ParameterMap parameters =
  37. DatabaseConnection::parse(dbaccess);
  38. // Get the databaase type and open the corresponding database
  39. DatabaseConnection::ParameterMap::iterator it = parameters.find("type");
  40. if (it == parameters.end()) {
  41. isc_throw(InvalidParameter, "Host database configuration does not "
  42. "contain the 'type' keyword");
  43. }
  44. std::string db_type = it->second;
  45. #ifdef HAVE_MYSQL
  46. if (db_type == "mysql") {
  47. LOG_INFO(dhcpsrv_logger, DHCPSRV_MYSQL_HOST_DB)
  48. .arg(DatabaseConnection::redactedAccessString(parameters));
  49. getHostDataSourcePtr().reset(new MySqlHostDataSource(parameters));
  50. return;
  51. }
  52. #endif
  53. #ifdef HAVE_PGSQL
  54. if (db_type == "postgresql") {
  55. LOG_INFO(dhcpsrv_logger, DHCPSRV_PGSQL_HOST_DB)
  56. .arg(DatabaseConnection::redactedAccessString(parameters));
  57. getHostDataSourcePtr().reset(new PgSqlHostDataSource(parameters));
  58. return;
  59. }
  60. #endif
  61. // Get here on no match.
  62. isc_throw(InvalidType, "Hosts database access parameter 'type': " <<
  63. db_type << " is invalid");
  64. }
  65. void
  66. HostDataSourceFactory::destroy() {
  67. // Destroy current host data source instance. This is a no-op if no host
  68. // data source is available.
  69. if (getHostDataSourcePtr()) {
  70. LOG_DEBUG(dhcpsrv_logger, DHCPSRV_DBG_TRACE, HOSTS_CFG_CLOSE_HOST_DATA_SOURCE)
  71. .arg(getHostDataSourcePtr()->getType());
  72. }
  73. getHostDataSourcePtr().reset();
  74. }
  75. #if 0
  76. BaseHostDataSource&
  77. HostDataSourceFactory::instance() {
  78. BaseHostDataSource* hdsptr = getHostDataSourcePtr().get();
  79. if (hdsptr == NULL) {
  80. isc_throw(NoHostDataSourceManager,
  81. "no current host data source instance is available");
  82. }
  83. return (*hdsptr);
  84. }
  85. #endif
  86. }; // namespace dhcp
  87. }; // namespace isc