|
5 | 5 | // file, You can obtain one at http://mozilla.org/MPL/2.0/. |
6 | 6 |
|
7 | 7 | #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; |
13 | 8 |
|
14 | 9 | namespace isc { |
15 | 10 | namespace cb { |
16 | 11 |
|
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 | | -} |
150 | 12 |
|
151 | 13 |
|
152 | 14 | } // end of namespace isc::cb |
|
0 commit comments