host_data_source_factory.cc 3.25 KB
Newer Older
1
// Copyright (C) 2015-2016 Internet Systems Consortium, Inc. ("ISC")
2
//
3 4 5
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
6

Andrei Pavel's avatar
Andrei Pavel committed
7
#include <config.h>
8 9 10 11 12 13 14 15 16

#include <dhcpsrv/dhcpsrv_log.h>
#include <dhcpsrv/host_data_source_factory.h>
#include <dhcpsrv/hosts_log.h>

#ifdef HAVE_MYSQL
#include <dhcpsrv/mysql_host_data_source.h>
#endif

17 18 19 20
#ifdef HAVE_PGSQL
#include <dhcpsrv/pgsql_host_data_source.h>
#endif

Andrei Pavel's avatar
Andrei Pavel committed
21 22 23 24
#ifdef HAVE_CQL
#include <dhcpsrv/cql_host_data_source.h>
#endif

25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
#include <boost/algorithm/string.hpp>
#include <boost/foreach.hpp>
#include <boost/scoped_ptr.hpp>

#include <algorithm>
#include <iostream>
#include <iterator>
#include <map>
#include <sstream>
#include <utility>

using namespace std;

namespace isc {
namespace dhcp {

41
HostDataSourcePtr&
42
HostDataSourceFactory::getHostDataSourcePtr() {
43
    static HostDataSourcePtr hostDataSourcePtr;
44 45 46 47 48 49 50 51 52
    return (hostDataSourcePtr);
}

void
HostDataSourceFactory::create(const std::string& dbaccess) {
    // Parse the access string and create a redacted string for logging.
    DatabaseConnection::ParameterMap parameters =
            DatabaseConnection::parse(dbaccess);

Josh Soref's avatar
Josh Soref committed
53
    // Get the database type and open the corresponding database
54 55
    DatabaseConnection::ParameterMap::iterator it = parameters.find("type");
    if (it == parameters.end()) {
56
        isc_throw(InvalidParameter, "Host database configuration does not "
57 58 59
                  "contain the 'type' keyword");
    }

60
    std::string db_type = it->second;
61 62

#ifdef HAVE_MYSQL
63 64 65
    if (db_type == "mysql") {
        LOG_INFO(dhcpsrv_logger, DHCPSRV_MYSQL_HOST_DB)
            .arg(DatabaseConnection::redactedAccessString(parameters));
66 67 68 69 70 71
        getHostDataSourcePtr().reset(new MySqlHostDataSource(parameters));
        return;
    }
#endif

#ifdef HAVE_PGSQL
72
    if (db_type == "postgresql") {
73 74 75 76
        LOG_INFO(dhcpsrv_logger, DHCPSRV_PGSQL_HOST_DB)
            .arg(DatabaseConnection::redactedAccessString(parameters));
        getHostDataSourcePtr().reset(new PgSqlHostDataSource(parameters));
        return;
77 78 79
    }
#endif

Tomek Mrugalski's avatar
Tomek Mrugalski committed
80 81
#ifdef HAVE_CQL
    if (db_type == "cql") {
Andrei Pavel's avatar
Andrei Pavel committed
82 83 84 85
        LOG_INFO(dhcpsrv_logger, DHCPSRV_CQL_HOST_DB)
            .arg(DatabaseConnection::redactedAccessString(parameters));
        getHostDataSourcePtr().reset(new CqlHostDataSource(parameters));
        return;
86 87 88
    }
#endif

89
    // Get here on no match.
90 91
    isc_throw(InvalidType, "Hosts database access parameter 'type': " <<
                           db_type << " is invalid");
92 93 94 95 96 97 98 99 100 101 102 103 104
}

void
HostDataSourceFactory::destroy() {
    // Destroy current host data source instance.  This is a no-op if no host
    // data source is available.
    if (getHostDataSourcePtr()) {
        LOG_DEBUG(dhcpsrv_logger, DHCPSRV_DBG_TRACE, HOSTS_CFG_CLOSE_HOST_DATA_SOURCE)
            .arg(getHostDataSourcePtr()->getType());
    }
    getHostDataSourcePtr().reset();
}

105
#if 0
106 107 108 109 110 111 112 113 114
BaseHostDataSource&
HostDataSourceFactory::instance() {
    BaseHostDataSource* hdsptr = getHostDataSourcePtr().get();
    if (hdsptr == NULL) {
        isc_throw(NoHostDataSourceManager,
                "no current host data source instance is available");
    }
    return (*hdsptr);
}
115
#endif
116

Andrei Pavel's avatar
Andrei Pavel committed
117 118
}  // namespace dhcp
}  // namespace isc