Kea: backend_selector.cc Source File

| Kea 3.2.0-git

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
9#include <database/backend_selector.h>
10#include <exceptions/exceptions.h>
11#include <limits>
12#include <sstream>
13
14using namespace isc::data;
15
16namespace isc {
17namespace db {
18
19BackendSelector::BackendSelector()
20 : backend_type_(BackendSelector::Type::UNSPEC),
21 host_(), port_(0) {
22}
23
24 BackendSelector::BackendSelector(const Type& backend_type)
25 : backend_type_(backend_type),
26 host_(), port_(0) {
27}
28
29 BackendSelector::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
36 BackendSelector::BackendSelector(const data::ConstElementPtr& access_map)
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&
74BackendSelector::UNSPEC() {
75 static BackendSelector selector;
76 return (selector);
77}
78
79bool
80BackendSelector::amUnspecified() const {
81 return ((backend_type_ == BackendSelector::Type::UNSPEC) &&
82 (host_.empty()) &&
83 (port_ == 0));
84}
85
86std::string
87BackendSelector::toText() const {
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
114 ElementPtr
115BackendSelector::toElement() const {
116 if (backend_type_ == BackendSelector::Type::UNSPEC) {
117 isc_throw(BadValue, "toElement: backend selector type is unspecified");
118 }
119 ElementPtr result = Element::createMap();
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
130 BackendSelector::Type
131BackendSelector::stringToBackendType(const std::string& type) {
132 if (type == "mysql") {
133 return (BackendSelector::Type::MYSQL);
134
135 } else if (type == "postgresql") {
136 return (BackendSelector::Type::POSTGRESQL);
137
138 } else {
139 isc_throw(BadValue, "unsupported configuration backend type '" << type << "'");
140 }
141}
142
143std::string
144BackendSelector::backendTypeToString(const BackendSelector::Type& type) {
145 switch (type) {
146 case BackendSelector::Type::MYSQL:
147 return ("mysql");
148 case BackendSelector::Type::POSTGRESQL:
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
backend_selector.h
Element::create
static ElementPtr create(const Position &pos=ZERO_POSITION())
Create a NullElement.
Definition data.cc:299
Element::map
@ map
Definition data.h:160
Element::integer
@ integer
Definition data.h:153
Element::string
@ string
Definition data.h:157
Element::createMap
static ElementPtr createMap(const Position &pos=ZERO_POSITION())
Creates an empty MapElement type ElementPtr.
Definition data.cc:354
isc::BadValue
A generic exception that is thrown if a parameter given to a method is considered invalid in that con...
Definition exceptions/exceptions.h:132
isc::db::BackendSelector
Config Backend selector.
Definition backend_selector.h:55
isc::db::BackendSelector::backendTypeToString
static std::string backendTypeToString(const Type &type)
Converts backend type to string.
Definition backend_selector.cc:144
isc::db::BackendSelector::toText
std::string toText() const
Returns selections as text.
Definition backend_selector.cc:87
isc::db::BackendSelector::stringToBackendType
static Type stringToBackendType(const std::string &type)
Converts string to backend type.
Definition backend_selector.cc:131
isc::db::BackendSelector::Type
Type
Supported database types.
Definition backend_selector.h:62
isc::db::BackendSelector::Type::MYSQL
@ MYSQL
Definition backend_selector.h:63
isc::db::BackendSelector::Type::UNSPEC
@ UNSPEC
Definition backend_selector.h:65
isc::db::BackendSelector::Type::POSTGRESQL
@ POSTGRESQL
Definition backend_selector.h:64
isc::db::BackendSelector::amUnspecified
bool amUnspecified() const
Checks if selector is "unspecified".
Definition backend_selector.cc:80
isc::db::BackendSelector::toElement
virtual data::ElementPtr toElement() const
Unparse a backend selector object.
Definition backend_selector.cc:115
isc::db::BackendSelector::BackendSelector
BackendSelector()
Default constructor.
Definition backend_selector.cc:19
isc::db::BackendSelector::UNSPEC
static const BackendSelector & UNSPEC()
Returns instance of the "unspecified" backend selector.
Definition backend_selector.cc:74
if
if(!(yy_init))
Definition d2_lexer.cc:1515
exceptions.h
isc_throw
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
Definition exceptions/exceptions.h:210
isc::data
Definition base_stamped_element.cc:12
isc::data::ConstElementPtr
boost::shared_ptr< const Element > ConstElementPtr
Definition data.h:30
isc::data::ElementPtr
boost::shared_ptr< Element > ElementPtr
Definition data.h:29
isc::db
Definition audit_entry.cc:14
isc
Defines the logger used by the top-level component of kea-lfc.
Definition check_exists_add.cc:18