host_data_source_factory.cc 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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. HostDataSourcePtr&
  34. HostDataSourceFactory::getHostDataSourcePtr() {
  35. static HostDataSourcePtr hostDataSourcePtr;
  36. return (hostDataSourcePtr);
  37. }
  38. void
  39. HostDataSourceFactory::create(const std::string& dbaccess) {
  40. // Parse the access string and create a redacted string for logging.
  41. DatabaseConnection::ParameterMap parameters =
  42. DatabaseConnection::parse(dbaccess);
  43. // Get the databaase type and open the corresponding database
  44. DatabaseConnection::ParameterMap::iterator it = parameters.find("type");
  45. if (it == parameters.end()) {
  46. isc_throw(InvalidParameter, "Host database configuration does not "
  47. "contain the 'type' keyword");
  48. }
  49. std::string db_type = it->second;
  50. #ifdef HAVE_MYSQL
  51. if (db_type == "mysql") {
  52. LOG_INFO(dhcpsrv_logger, DHCPSRV_MYSQL_HOST_DB)
  53. .arg(DatabaseConnection::redactedAccessString(parameters));
  54. getHostDataSourcePtr().reset(new MySqlHostDataSource(parameters));
  55. return;
  56. }
  57. #endif
  58. #ifdef HAVE_PGSQL
  59. if (db_type == "postgresql") {
  60. isc_throw(NotImplemented, "Sorry, PostgreSQL backend for host reservations "
  61. "is not implemented yet.");
  62. }
  63. #endif
  64. // Get here on no match.
  65. isc_throw(InvalidType, "Hosts database access parameter 'type': " <<
  66. db_type << " is invalid");
  67. }
  68. void
  69. HostDataSourceFactory::destroy() {
  70. // Destroy current host data source instance. This is a no-op if no host
  71. // data source is available.
  72. if (getHostDataSourcePtr()) {
  73. LOG_DEBUG(dhcpsrv_logger, DHCPSRV_DBG_TRACE, HOSTS_CFG_CLOSE_HOST_DATA_SOURCE)
  74. .arg(getHostDataSourcePtr()->getType());
  75. }
  76. getHostDataSourcePtr().reset();
  77. }
  78. #if 0
  79. BaseHostDataSource&
  80. HostDataSourceFactory::instance() {
  81. BaseHostDataSource* hdsptr = getHostDataSourcePtr().get();
  82. if (hdsptr == NULL) {
  83. isc_throw(NoHostDataSourceManager,
  84. "no current host data source instance is available");
  85. }
  86. return (*hdsptr);
  87. }
  88. #endif
  89. }; // namespace dhcp
  90. }; // namespace isc