Skip to content

Commit 3bc730d

Browse files
committed
[#28,!20] Moved backend and server selector classes to database library.
1 parent e45157f commit 3bc730d

File tree

11 files changed

+530
-454
lines changed

11 files changed

+530
-454
lines changed

src/lib/config_backend/base_config_backend.h

Lines changed: 0 additions & 106 deletions
Original file line numberDiff line numberDiff line change
@@ -15,112 +15,6 @@
1515
namespace isc {
1616
namespace cb {
1717

18-
/// @brief Server selector for associating objects in a database with
19-
/// specific servers.
20-
///
21-
/// Configuration information stored in the configuration backends can be
22-
/// associated with selected servers, all servers or no particular server.
23-
/// For example: a particular subnet definition in the database may be
24-
/// associated with one server or can be shared by multiple servers.
25-
/// In the latter case, a subnet may be associated with a subset of
26-
/// servers or all servers. An administrator may also add the
27-
/// configuration data into the database and do not associate this data
28-
/// with any patrticular server.
29-
///
30-
/// When fetching the configuration data from a databse or when storing
31-
/// data in the database there is a need to specify which servers this
32-
/// data is associated with. The @c ServerSelector class represents
33-
/// such associations.
34-
///
35-
/// It includes three modes of selection: UNASSIGNED, ALL and SUBSET and
36-
/// several factory functions making associations described above.
37-
///
38-
/// The @c ServerSelector class should be used in objects derived from
39-
/// @c BaseConfigBackendPool and in objects derived from
40-
/// @c BaseConfigBackend to indicate which servers the specific calls
41-
/// exposed by these objects refer to.
42-
class ServerSelector {
43-
public:
44-
45-
/// @brief Type of the server selection.
46-
enum class Type {
47-
UNASSIGNED,
48-
ALL,
49-
SUBSET
50-
};
51-
52-
/// @brief Factory returning "unassigned" server selector.
53-
static ServerSelector& UNASSIGNED() {
54-
static ServerSelector selector(Type::UNASSIGNED);
55-
return (selector);
56-
}
57-
58-
/// @brief Factory returning "all servers" selector.
59-
static ServerSelector& ALL() {
60-
static ServerSelector selector(Type::ALL);
61-
return (selector);
62-
}
63-
64-
/// @brief Factory returning selector of one server.
65-
///
66-
/// @param server_tag tag of the single server to be selected.
67-
static ServerSelector& ONE(const std::string& server_tag) {
68-
static ServerSelector selector(server_tag);
69-
return (selector);
70-
}
71-
72-
/// @brief Factory returning "multiple servers" selector.
73-
///
74-
/// @param server_tags set of server tags to be selected.
75-
static ServerSelector& MULTIPLE(const std::set<std::string>& server_tags) {
76-
static ServerSelector selector(server_tags);
77-
return (selector);
78-
}
79-
80-
/// @brief Returns type of the selector.
81-
Type getType() const {
82-
return (type_);
83-
}
84-
85-
/// @brief Returns tags associated with the selector.
86-
///
87-
/// @return server tags for mutliple selections and for one server,
88-
/// empty set for all servers and and unassigned.
89-
std::set<std::string> getTags() const {
90-
return (tags_);
91-
}
92-
93-
private:
94-
95-
/// @brief Constructor used for "unassigned" and "all" slection types.
96-
///
97-
/// @param type selector type.
98-
explicit ServerSelector(const Type& type)
99-
: type_(type), tags_() {
100-
}
101-
102-
/// @brief Constructor used for selecting a single server.
103-
///
104-
/// @param server_tag tag of the server to be selected.
105-
explicit ServerSelector(const std::string& server_tag)
106-
: type_(Type::SUBSET), tags_() {
107-
tags_.insert(server_tag);
108-
}
109-
110-
/// @brief Constructor used for selecting multiple servers.
111-
///
112-
/// @param server_tags set of server tags.
113-
explicit ServerSelector(const std::set<std::string>& server_tags)
114-
: type_(Type::SUBSET), tags_(server_tags) {
115-
}
116-
117-
/// @brief Selection type used.
118-
Type type_;
119-
120-
/// @brief Holds tags of explicitly selected servers.
121-
std::set<std::string> tags_;
122-
};
123-
12418
/// @brief Base class for server specific configuration backends.
12519
class BaseConfigBackend {
12620
public:

src/lib/config_backend/base_config_backend_pool.cc

Lines changed: 0 additions & 138 deletions
Original file line numberDiff line numberDiff line change
@@ -5,148 +5,10 @@
55
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
66

77
#include <config_backend/base_config_backend_pool.h>
8-
#include <exceptions/exceptions.h>
9-
#include <climits>
10-
#include <sstream>
11-
12-
using namespace isc::data;
138

149
namespace isc {
1510
namespace cb {
1611

17-
BackendSelector::BackendSelector()
18-
: backend_type_(BackendSelector::Type::UNSPEC),
19-
host_(), port_(0) {
20-
}
21-
22-
BackendSelector::BackendSelector(const Type& backend_type)
23-
: backend_type_(backend_type),
24-
host_(), port_(0) {
25-
}
26-
27-
BackendSelector::BackendSelector(const std::string& host,
28-
const uint16_t port)
29-
: backend_type_(BackendSelector::Type::UNSPEC),
30-
host_(host), port_(port) {
31-
validate();
32-
}
33-
34-
BackendSelector::BackendSelector(const data::ConstElementPtr& access_map)
35-
: backend_type_(BackendSelector::Type::UNSPEC),
36-
host_(), port_(0) {
37-
if (access_map->getType() != Element::map) {
38-
isc_throw(BadValue, "database access information must be a map");
39-
}
40-
41-
ConstElementPtr t = access_map->get("type");
42-
if (t) {
43-
if (t->getType() != Element::string) {
44-
isc_throw(BadValue, "'type' parameter must be a string");
45-
}
46-
backend_type_ = stringToBackendType(t->stringValue());
47-
}
48-
49-
ConstElementPtr h = access_map->get("host");
50-
if (h) {
51-
if (h->getType() != Element::string) {
52-
isc_throw(BadValue, "'host' parameter must be a string");
53-
}
54-
host_ = h->stringValue();
55-
}
56-
57-
ConstElementPtr p = access_map->get("port");
58-
if (p) {
59-
if ((p->getType() != Element::integer) ||
60-
(p->intValue() < 0) ||
61-
(p->intValue() > std::numeric_limits<uint16_t>::max())) {
62-
isc_throw(BadValue, "'port' parameter must be a number in range from 0 "
63-
"to " << std::numeric_limits<uint16_t>::max());
64-
}
65-
port_ = static_cast<uint16_t>(p->intValue());
66-
}
67-
68-
validate();
69-
}
70-
71-
const BackendSelector&
72-
BackendSelector::BackendSelector::UNSPEC() {
73-
static BackendSelector selector;
74-
return (selector);
75-
}
76-
77-
bool
78-
BackendSelector::amUnspecified() const {
79-
return ((backend_type_ == BackendSelector::Type::UNSPEC) &&
80-
(host_.empty()) &&
81-
(port_ == 0));
82-
}
83-
84-
std::string
85-
BackendSelector::toText() const {
86-
std::ostringstream s;
87-
if (amUnspecified()) {
88-
s << "unspecified";
89-
90-
} else {
91-
if (backend_type_ != BackendSelector::Type::UNSPEC) {
92-
s << "type=" << backendTypeToString(backend_type_) << ",";
93-
}
94-
95-
if (!host_.empty()) {
96-
s << "host=" << host_ << ",";
97-
98-
if (port_ > 0) {
99-
s << "port=" << port_ << ",";
100-
}
101-
}
102-
}
103-
104-
std::string text = s.str();
105-
if ((!text.empty() && (text.back() == ','))) {
106-
text.pop_back();
107-
}
108-
109-
return (text);
110-
}
111-
112-
BackendSelector::Type
113-
BackendSelector::stringToBackendType(const std::string& type) {
114-
if (type == "mysql") {
115-
return (BackendSelector::Type::MYSQL);
116-
117-
} else if (type == "pgsql") {
118-
return (BackendSelector::Type::PGSQL);
119-
120-
} else if (type == "cql") {
121-
return (BackendSelector::Type::CQL);
122-
123-
} else {
124-
isc_throw(BadValue, "unsupported configuration backend type '" << type << "'");
125-
}
126-
}
127-
128-
std::string
129-
BackendSelector::backendTypeToString(const BackendSelector::Type& type) {
130-
switch (type) {
131-
case BackendSelector::Type::MYSQL:
132-
return ("mysql");
133-
case BackendSelector::Type::PGSQL:
134-
return ("pgsql");
135-
case BackendSelector::Type::CQL:
136-
return ("cql");
137-
default:
138-
;
139-
}
140-
141-
return (std::string());
142-
}
143-
144-
void
145-
BackendSelector::validate() const {
146-
if ((port_ != 0) && (host_.empty())) {
147-
isc_throw(BadValue, "'host' must be specified along with 'port' parameter");
148-
}
149-
}
15012

15113

15214
} // end of namespace isc::cb

0 commit comments

Comments
 (0)