Kea  1.5.0
backend_selector.cc
Go to the documentation of this file.
1 // Copyright (C) 2018 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 
9 #include <climits>
10 #include <sstream>
11 
12 using namespace isc::data;
13 
14 namespace isc {
15 namespace db {
16 
17 BackendSelector::BackendSelector()
18  : backend_type_(BackendSelector::Type::UNSPEC),
19  host_(), port_(0) {
20 }
21 
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 
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&
73  static BackendSelector selector;
74  return (selector);
75 }
76 
77 bool
79  return ((backend_type_ == BackendSelector::Type::UNSPEC) &&
80  (host_.empty()) &&
81  (port_ == 0));
82 }
83 
84 std::string
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 
113 BackendSelector::stringToBackendType(const std::string& type) {
114  if (type == "mysql") {
116 
117  } else if (type == "pgsql") {
119 
120  } else if (type == "cql") {
122 
123  } else {
124  isc_throw(BadValue, "unsupported configuration backend type '" << type << "'");
125  }
126 }
127 
128 std::string
130  switch (type) {
132  return ("mysql");
134  return ("pgsql");
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 
151 } // end of namespace isc::db
152 } // end of namespace isc
static const BackendSelector & UNSPEC()
Returns instance of the "unspecified" backend selector.
std::string toText() const
Returns selections as text.
static Type stringToBackendType(const std::string &type)
Converts string to backend type.
static std::string backendTypeToString(const Type &type)
Converts backend type to string.
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
A generic exception that is thrown if a parameter given to a method is considered invalid in that con...
boost::shared_ptr< const Element > ConstElementPtr
Definition: data.h:23
BackendSelector()
Default constructor.
Defines the logger used by the top-level component of kea-dhcp-ddns.
Type
Supported database types.
Config Backend selector.
bool amUnspecified() const
Checks if selector is "unspecified".