lease_mgr_factory.h 5.23 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14
// Copyright (C) 2012 Internet Systems Consortium, Inc. ("ISC")
//
// Permission to use, copy, modify, and/or distribute this software for any
// purpose with or without fee is hereby granted, provided that the above
// copyright notice and this permission notice appear in all copies.
//
// THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
// AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
// LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
// OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
// PERFORMANCE OF THIS SOFTWARE.

15 16
#ifndef LEASE_MGR_FACTORY_H
#define LEASE_MGR_FACTORY_H
17

18
#include <dhcpsrv/lease_mgr.h>
19
#include <exceptions/exceptions.h>
20

21 22
#include <string>

23 24 25
namespace isc {
namespace dhcp {

26
/// @brief Invalid type exception
27
///
Francis Dupont's avatar
Francis Dupont committed
28
/// Thrown when the factory doesn't recognize the type of the backend.
29
class InvalidType : public Exception {
30
public:
31 32 33 34
    InvalidType(const char* file, size_t line, const char* what) :
        isc::Exception(file, line, what) {}
};

35 36 37 38 39 40 41 42 43 44
/// @brief No lease manager exception
///
/// Thrown if an attempt is made to get a reference to the current lease
/// manager and none is currently available.
class NoLeaseManager : public Exception {
public:
    NoLeaseManager(const char* file, size_t line, const char* what) :
        isc::Exception(file, line, what) {}
};

45 46 47 48 49 50 51 52 53
/// @brief Lease Manager Factory
///
/// This class comprises nothing but static methods used to create a lease
/// manager.  It analyzes the database information passed to the creation
/// function and instantiates an appropriate lease manager based on the type
/// requested.
///
/// Strictly speaking these functions could be stand-alone functions.  However,
/// it is convenient to encapsulate them in a class for naming purposes.
54
///
55
/// @todo: Will need to develop some form of registration mechanism for
56
///        user-supplied backends (so that there is no need to modify the code).
57 58 59 60 61
class LeaseMgrFactory {
public:
    /// @brief Create an instance of a lease manager.
    ///
    /// Each database backend has its own lease manager type.  This static
62 63 64 65
    /// method sets the "current" lease manager to be a manager of the
    /// appropriate type.  The actual lease manager is returned by the
    /// "instance" method.
    ///
66 67
    /// @note When called, the current lease manager is <b>always</b> destroyed
    ///       and a new one created - even if the parameters are the same.
68
    ///
69
    /// dbaccess is a generic way of passing parameters. Parameters are passed
70 71 72 73
    /// in the "name=value" format, separated by spaces.  The data MUST include
    /// a keyword/value pair of the form "type=dbtype" giving the database
    /// type, e.q. "mysql" or "sqlite3".
    ///
74 75 76 77
    /// @param dbaccess Database access parameters.  These are in the form of
    ///        "keyword=value" pairs, separated by spaces. They are backend-
    ///        -end specific, although must include the "type" keyword which
    ///        gives the backend in use.
78
    ///
79
    /// @throw isc::InvalidParameter dbaccess string does not contain the "type"
80
    ///        keyword.
81
    /// @throw isc::dhcp::InvalidType The "type" keyword in dbaccess does not
82
    ///        identify a supported backend.
83
    static void create(const std::string& dbaccess);
84 85 86 87 88 89 90 91

    /// @brief Destroy lease manager
    ///
    /// Destroys the current lease manager object.  This should have the effect
    /// of closing the database connection.  The method is a no-op if no
    /// lease manager is available.
    static void destroy();

92
    /// @brief Return current lease manager
93 94 95 96
    ///
    /// Returns an instance of the "current" lease manager.  An exception
    /// will be thrown if none is available.
    ///
97 98
    /// @throw isc::dhcp::NoLeaseManager No lease manager is available: use
    ///        create() to create one before calling this method.
99
    static LeaseMgr& instance();
100

101
    /// @brief Parse database access string
102 103 104 105
    ///
    /// Parses the string of "keyword=value" pairs and separates them
    /// out into the map.
    ///
106
    /// @param dbaccess Database access string.
107
    ///
108
    /// @return std::map<std::string, std::string> Map of keyword/value pairs.
109 110 111 112 113 114 115
    static LeaseMgr::ParameterMap parse(const std::string& dbaccess);

    /// @brief Redact database access string
    ///
    /// Takes the database parameters and returns a database access string
    /// passwords replaced by asterisks. This string is used in log messages.
    ///
116
    /// @param parameters Database access parameters (output of "parse").
117 118 119 120
    ///
    /// @return Redacted database access string.
    static std::string redactedAccessString(
            const LeaseMgr::ParameterMap& parameters);
121 122 123 124 125 126 127 128 129

private:
    /// @brief Hold pointer to lease manager
    ///
    /// Holds a pointer to the singleton lease manager.  The singleton
    /// is encapsulated in this method to avoid a "static initialization
    /// fiasco" if defined in an external static variable.
    static boost::scoped_ptr<LeaseMgr>& getLeaseMgrPtr();

130 131 132 133
};

}; // end of isc::dhcp namespace
}; // end of isc namespace
134

135
#endif // LEASE_MGR_FACTORY_H