Kea  2.1.7-git
base_config_backend_mgr.h
Go to the documentation of this file.
1 // Copyright (C) 2018-2021 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 
7 #ifndef BASE_CONFIG_BACKEND_MGR_H
8 #define BASE_CONFIG_BACKEND_MGR_H
9 
13 #include <exceptions/exceptions.h>
14 #include <boost/shared_ptr.hpp>
15 #include <functional>
16 #include <map>
17 #include <string>
18 
19 namespace isc {
20 namespace cb {
21 
58 template<typename ConfigBackendPoolType>
60 public:
61 
63  typedef boost::shared_ptr<ConfigBackendPoolType> ConfigBackendPoolPtr;
64 
69  typedef std::function<typename ConfigBackendPoolType::ConfigBackendTypePtr
71 
74  : factories_(), pool_(new ConfigBackendPoolType()) {
75  }
76 
96  bool registerBackendFactory(const std::string& db_type,
97  const Factory& factory) {
98  // Check if this backend has been already registered.
99  if (factories_.count(db_type)) {
100  return (false);
101  }
102 
103  // Register the new backend.
104  factories_.insert(std::make_pair(db_type, factory));
105  return (true);
106  }
107 
119  bool unregisterBackendFactory(const std::string& db_type) {
120  // Look for it.
121  auto index = factories_.find(db_type);
122 
123  // If it's there remove it
124  if (index != factories_.end()) {
125  factories_.erase(index);
126  pool_->delAllBackends(db_type);
127  return (true);
128 
129  }
130 
131  return (false);
132  }
133 
148  void addBackend(const std::string& dbaccess) {
149  // Parse the access string into a map of parameters.
152 
153  // Get the database type to locate a factory function.
154  db::DatabaseConnection::ParameterMap::iterator it = parameters.find("type");
155  if (it == parameters.end()) {
156  isc_throw(InvalidParameter, "Config backend specification lacks the "
157  "'type' keyword");
158  }
159 
160  std::string db_type = it->second;
161  auto index = factories_.find(db_type);
162 
163  // No match?
164  if (index == factories_.end()) {
165  isc_throw(db::InvalidType, "The type of the configuration backend: '" <<
166  db_type << "' is not supported");
167  }
168 
169  // Call the factory and push the pointer on sources.
170  auto backend = index->second(parameters);
171  if (!backend) {
172  isc_throw(Unexpected, "Config database " << db_type <<
173  " factory returned NULL");
174  }
175 
176  // Backend instance created successfully.
177  pool_->addBackend(backend);
178  }
179 
181  void delAllBackends() {
182  pool_->delAllBackends();
183  }
184 
198  bool delBackend(const std::string& db_type, const std::string& dbaccess,
199  bool if_unusable) {
200  return (pool_->del(db_type, dbaccess, if_unusable));
201  }
202 
204  ConfigBackendPoolPtr getPool() const {
205  return (pool_);
206  }
207 
208 protected:
209 
211  std::map<std::string, Factory> factories_;
212 
214  ConfigBackendPoolPtr pool_;
215 };
216 
217 } // end of namespace isc::cb
218 } // end of namespace isc
219 
220 #endif // BASE_CONFIG_BACKEND_MGR_H
ConfigBackendPoolPtr pool_
Pointer to the configuration backends pool.
A generic exception that is thrown if a parameter given to a method or function is considered invalid...
boost::shared_ptr< ConfigBackendPoolType > ConfigBackendPoolPtr
Pointer to the configuration backend pool.
static ParameterMap parse(const std::string &dbaccess)
Parse database access string.
Invalid type exception.
Base class for Configuration Backend Managers (CBM).
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
std::function< typename ConfigBackendPoolType::ConfigBackendTypePtr(const db::DatabaseConnection::ParameterMap &)> Factory
Type of the backend factory function.
A generic exception that is thrown when an unexpected error condition occurs.
bool unregisterBackendFactory(const std::string &db_type)
Unregisters the backend factory function for a given backend type.
bool registerBackendFactory(const std::string &db_type, const Factory &factory)
Registers new backend factory function for a given backend type.
ConfigBackendPoolPtr getPool() const
Returns underlying config backend pool.
Defines the logger used by the top-level component of kea-lfc.
std::map< std::string, Factory > factories_
A map holding registered backend factory functions.
void delAllBackends()
Removes all backends from the pool.
bool delBackend(const std::string &db_type, const std::string &dbaccess, bool if_unusable)
Delete a config backend manager.
std::map< std::string, std::string > ParameterMap
Database configuration parameter map.
void addBackend(const std::string &dbaccess)
Create an instance of a configuration backend.