Kea 2.5.8
backend_selector.cc
Go to the documentation of this file.
1// Copyright (C) 2018-2022 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#include <config.h>
8
11#include <limits>
12#include <sstream>
13
14using namespace isc::data;
15
16namespace isc {
17namespace db {
18
20 : backend_type_(BackendSelector::Type::UNSPEC),
21 host_(), port_(0) {
22}
23
25 : backend_type_(backend_type),
26 host_(), port_(0) {
27}
28
29BackendSelector::BackendSelector(const std::string& host,
30 const uint16_t port)
31 : backend_type_(BackendSelector::Type::UNSPEC),
32 host_(host), port_(port) {
33 validate();
34}
35
37 : backend_type_(BackendSelector::Type::UNSPEC),
38 host_(), port_(0) {
39 if (access_map->getType() != Element::map) {
40 isc_throw(BadValue, "database access information must be a map");
41 }
42
43 ConstElementPtr t = access_map->get("type");
44 if (t) {
45 if (t->getType() != Element::string) {
46 isc_throw(BadValue, "'type' parameter must be a string");
47 }
48 backend_type_ = stringToBackendType(t->stringValue());
49 }
50
51 ConstElementPtr h = access_map->get("host");
52 if (h) {
53 if (h->getType() != Element::string) {
54 isc_throw(BadValue, "'host' parameter must be a string");
55 }
56 host_ = h->stringValue();
57 }
58
59 ConstElementPtr p = access_map->get("port");
60 if (p) {
61 if ((p->getType() != Element::integer) ||
62 (p->intValue() < 0) ||
63 (p->intValue() > std::numeric_limits<uint16_t>::max())) {
64 isc_throw(BadValue, "'port' parameter must be a number in range from 0 "
65 "to " << std::numeric_limits<uint16_t>::max());
66 }
67 port_ = static_cast<uint16_t>(p->intValue());
68 }
69
70 validate();
71}
72
73const BackendSelector&
75 static BackendSelector selector;
76 return (selector);
77}
78
79bool
81 return ((backend_type_ == BackendSelector::Type::UNSPEC) &&
82 (host_.empty()) &&
83 (port_ == 0));
84}
85
86std::string
88 std::ostringstream s;
89 if (amUnspecified()) {
90 s << "unspecified";
91
92 } else {
93 if (backend_type_ != BackendSelector::Type::UNSPEC) {
94 s << "type=" << backendTypeToString(backend_type_) << ",";
95 }
96
97 if (!host_.empty()) {
98 s << "host=" << host_ << ",";
99
100 if (port_ > 0) {
101 s << "port=" << port_ << ",";
102 }
103 }
104 }
105
106 std::string text = s.str();
107 if ((!text.empty() && (text.back() == ','))) {
108 text.pop_back();
109 }
110
111 return (text);
112}
113
116 if (backend_type_ == BackendSelector::Type::UNSPEC) {
117 isc_throw(BadValue, "toElement: backend selector type is unspecified");
118 }
120 result->set("type", Element::create(backendTypeToString(backend_type_)));
121 if (!host_.empty()) {
122 result->set("host", Element::create(host_));
123 if (port_ > 0) {
124 result->set("port", Element::create(static_cast<long int>(port_)));
125 }
126 }
127 return (result);
128}
129
131BackendSelector::stringToBackendType(const std::string& type) {
132 if (type == "mysql") {
134
135 } else if (type == "postgresql") {
137
138 } else {
139 isc_throw(BadValue, "unsupported configuration backend type '" << type << "'");
140 }
141}
142
143std::string
145 switch (type) {
147 return ("mysql");
149 return ("postgresql");
150 default:
151 ;
152 }
153
154 return (std::string());
155}
156
157void
158BackendSelector::validate() const {
159 if ((port_ != 0) && (host_.empty())) {
160 isc_throw(BadValue, "'host' must be specified along with 'port' parameter");
161 }
162}
163
164} // end of namespace isc::db
165} // end of namespace isc
A generic exception that is thrown if a parameter given to a method is considered invalid in that con...
static ElementPtr create(const Position &pos=ZERO_POSITION())
Definition: data.cc:249
static ElementPtr createMap(const Position &pos=ZERO_POSITION())
Creates an empty MapElement type ElementPtr.
Definition: data.cc:304
Config Backend selector.
static std::string backendTypeToString(const Type &type)
Converts backend type to string.
std::string toText() const
Returns selections as text.
static Type stringToBackendType(const std::string &type)
Converts string to backend type.
Type
Supported database types.
bool amUnspecified() const
Checks if selector is "unspecified".
virtual data::ElementPtr toElement() const
Unparse a backend selector object.
BackendSelector()
Default constructor.
static const BackendSelector & UNSPEC()
Returns instance of the "unspecified" backend selector.
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
boost::shared_ptr< const Element > ConstElementPtr
Definition: data.h:29
boost::shared_ptr< Element > ElementPtr
Definition: data.h:28
Defines the logger used by the top-level component of kea-lfc.