26#include <boost/algorithm/string/split.hpp>
27#include <boost/algorithm/string/classification.hpp>
28#include <boost/array.hpp>
29#include <boost/foreach.hpp>
30#include <boost/pointer_cast.hpp>
31#include <boost/static_assert.hpp>
95 static const size_t HOST_ID_COL = 0;
96 static const size_t DHCP_IDENTIFIER_COL = 1;
97 static const size_t DHCP_IDENTIFIER_TYPE_COL = 2;
98 static const size_t DHCP4_SUBNET_ID_COL = 3;
99 static const size_t DHCP6_SUBNET_ID_COL = 4;
100 static const size_t IPV4_ADDRESS_COL = 5;
101 static const size_t HOSTNAME_COL = 6;
102 static const size_t DHCP4_CLIENT_CLASSES_COL = 7;
103 static const size_t DHCP6_CLIENT_CLASSES_COL = 8;
104 static const size_t USER_CONTEXT_COL = 9;
105 static const size_t DHCP4_NEXT_SERVER_COL = 10;
106 static const size_t DHCP4_SERVER_HOSTNAME_COL = 11;
107 static const size_t DHCP4_BOOT_FILE_NAME_COL = 12;
108 static const size_t AUTH_KEY_COL = 13;
110 static const size_t HOST_COLUMNS = 14;
120 PgSqlHostExchange(
const size_t additional_columns_num = 0)
126 columns_[HOST_ID_COL] =
"host_id";
127 columns_[DHCP_IDENTIFIER_COL] =
"dhcp_identifier";
128 columns_[DHCP_IDENTIFIER_TYPE_COL] =
"dhcp_identifier_type";
129 columns_[DHCP4_SUBNET_ID_COL] =
"dhcp4_subnet_id";
130 columns_[DHCP6_SUBNET_ID_COL] =
"dhcp6_subnet_id";
131 columns_[IPV4_ADDRESS_COL] =
"ipv4_address";
132 columns_[HOSTNAME_COL] =
"hostname";
133 columns_[DHCP4_CLIENT_CLASSES_COL] =
"dhcp4_client_classes";
134 columns_[DHCP6_CLIENT_CLASSES_COL] =
"dhcp6_client_classes";
135 columns_[USER_CONTEXT_COL] =
"user_context";
136 columns_[DHCP4_NEXT_SERVER_COL] =
"dhcp4_next_server";
137 columns_[DHCP4_SERVER_HOSTNAME_COL] =
"dhcp4_server_hostname";
138 columns_[DHCP4_BOOT_FILE_NAME_COL] =
"dhcp4_boot_file_name";
139 columns_[AUTH_KEY_COL] =
"auth_key";
141 BOOST_STATIC_ASSERT(12 < HOST_COLUMNS);
145 virtual ~PgSqlHostExchange() {
153 virtual void clear() {
171 size_t findAvailColumn()
const {
172 std::vector<std::string>::const_iterator empty_column =
173 std::find(columns_.begin(), columns_.end(), std::string());
174 return (std::distance(columns_.begin(), empty_column));
216 bind_array->add(host->getIdentifier());
219 bind_array->add(host->getIdentifierType());
222 if (host->getIPv4SubnetID() == SUBNET_ID_UNUSED) {
223 bind_array->addNull();
225 bind_array->add(host->getIPv4SubnetID());
229 if (host->getIPv6SubnetID() == SUBNET_ID_UNUSED) {
230 bind_array->addNull();
232 bind_array->add(host->getIPv6SubnetID());
236 bind_array->add((host->getIPv4Reservation()));
239 bind_array->add(host->getHostname());
243 bind_array->addTempString(host->getClientClasses4().toText(
","));
246 bind_array->addTempString(host->getClientClasses6().toText(
","));
251 std::string user_context_ = ctx->str();
252 bind_array->addTempString(user_context_);
254 bind_array->addNull();
258 bind_array->add((host->getNextServer()));
261 bind_array->add(host->getServerHostname());
264 bind_array->add(host->getBootFileName());
267 std::string key = host->getKey().toText();
269 bind_array->addNull();
271 bind_array->addTempString(key);
278 bind_array->add(host->getIPv4Reservation());
279 bind_array->add(host->getIPv4SubnetID());
282 }
catch (
const std::exception& ex) {
285 "Could not create bind array from Host: "
286 << host->getHostname() <<
", reason: " << ex.
what());
310 HostID row_host_id = getHostId(r, row);
315 if (hosts.empty() || row_host_id != hosts.back()->getHostId()) {
316 HostPtr host = retrieveHost(r, row, row_host_id);
317 hosts.push_back(host);
333 const HostID& peeked_host_id = 0) {
337 HostID host_id = (peeked_host_id ? peeked_host_id : getHostId(r,row));
340 uint8_t identifier_value[DHCP_IDENTIFIER_MAX_LEN];
341 size_t identifier_len;
343 sizeof(identifier_value), identifier_len);
348 if (type > MAX_IDENTIFIER_TYPE) {
350 <<
static_cast<int>(type));
357 uint32_t subnet_id(SUBNET_ID_UNUSED);
364 subnet_id = SUBNET_ID_UNUSED;
378 std::string hostname;
384 std::string dhcp4_client_classes;
386 getColumnValue(r, row, DHCP4_CLIENT_CLASSES_COL, dhcp4_client_classes);
390 std::string dhcp6_client_classes;
392 getColumnValue(r, row, DHCP6_CLIENT_CLASSES_COL, dhcp6_client_classes);
396 std::string user_context;
402 uint32_t dhcp4_next_server_as_uint32(0);
404 getColumnValue(r, row, DHCP4_NEXT_SERVER_COL, dhcp4_next_server_as_uint32);
409 std::string dhcp4_server_hostname;
411 getColumnValue(r, row, DHCP4_SERVER_HOSTNAME_COL, dhcp4_server_hostname);
415 std::string dhcp4_boot_file_name;
417 getColumnValue(r, row, DHCP4_BOOT_FILE_NAME_COL, dhcp4_boot_file_name);
421 std::string auth_key;
429 host.reset(
new Host(identifier_value, identifier_len,
430 identifier_type, dhcp4_subnet_id,
431 dhcp6_subnet_id, ipv4_reservation, hostname,
432 dhcp4_client_classes, dhcp6_client_classes,
433 dhcp4_next_server, dhcp4_server_hostname,
434 dhcp4_boot_file_name,
AuthKey(auth_key)));
437 if (!user_context.empty()) {
442 <<
"' is not a JSON map");
444 host->setContext(ctx);
447 <<
"' is invalid JSON: " << ex.
what());
451 host->setHostId(host_id);
474class PgSqlHostWithOptionsExchange :
public PgSqlHostExchange {
478 static const size_t OPTION_COLUMNS = 8;
494 class OptionProcessor {
504 const size_t start_column)
505 : universe_(universe), start_column_(start_column),
506 option_id_index_(start_column), code_index_(start_column_ + 1),
507 value_index_(start_column_ + 2),
508 formatted_value_index_(start_column_ + 3),
509 space_index_(start_column_ + 4),
510 persistent_index_(start_column_ + 5),
511 cancelled_index_(start_column_ + 6),
512 user_context_index_(start_column_ + 7),
513 most_recent_option_id_(0) {
521 most_recent_option_id_ = 0;
571 if (most_recent_option_id_ >= option_id) {
577 most_recent_option_id_ = option_id;
586 if (!isColumnNull(r, row, value_index_)) {
588 sizeof(value), value_len);
592 std::string formatted_value;
593 if (!isColumnNull(r, row, formatted_value_index_)) {
600 if (!isColumnNull(r, row, space_index_)) {
621 std::string user_context;
622 if (!isColumnNull(r, row, user_context_index_)) {
665 option.reset(
new Option(universe_, code, buf.begin(),
672 if (formatted_value.empty()) {
674 option = def->optionFactory(universe_, code, buf.begin(),
679 std::vector<std::string> split_vec;
680 boost::split(split_vec, formatted_value,
681 boost::is_any_of(
","));
682 option = def->optionFactory(universe_, code, split_vec);
690 if (!user_context.empty()) {
695 <<
"' is no a JSON map");
697 desc.setContext(ctx);
700 <<
"' is invalid JSON: " << ex.
what());
704 cfg->add(desc, space);
711 void setColumnNames(std::vector<std::string>& columns) {
712 columns[option_id_index_] =
"option_id";
713 columns[code_index_] =
"code";
714 columns[value_index_] =
"value";
715 columns[formatted_value_index_] =
"formatted_value";
716 columns[space_index_] =
"space";
717 columns[persistent_index_] =
"persistent";
718 columns[cancelled_index_] =
"cancelled";
719 columns[user_context_index_] =
"user_context";
727 size_t start_column_;
734 size_t option_id_index_;
743 size_t formatted_value_index_;
749 size_t persistent_index_;
752 size_t cancelled_index_;
756 size_t user_context_index_;
759 uint64_t most_recent_option_id_;
763 typedef boost::shared_ptr<OptionProcessor> OptionProcessorPtr;
773 enum FetchedOptions {
787 PgSqlHostWithOptionsExchange(
const FetchedOptions& fetched_options,
788 const size_t additional_columns_num = 0)
789 : PgSqlHostExchange(getRequiredColumnsNum(fetched_options)
790 + additional_columns_num),
791 opt_proc4_(), opt_proc6_() {
794 if ((fetched_options == DHCP4_ONLY) ||
795 (fetched_options == DHCP4_AND_DHCP6)) {
796 opt_proc4_.reset(
new OptionProcessor(
Option::V4,
798 opt_proc4_->setColumnNames(columns_);
802 if ((fetched_options == DHCP6_ONLY) ||
803 (fetched_options == DHCP4_AND_DHCP6)) {
804 opt_proc6_.reset(
new OptionProcessor(
Option::V6,
806 opt_proc6_->setColumnNames(columns_);
815 virtual void clear() {
816 PgSqlHostExchange::clear();
840 current_host = retrieveHost(r, row);
841 hosts.push_back(current_host);
846 HostID row_host_id = getHostId(r, row);
847 current_host = boost::const_pointer_cast<Host>(hosts.back());
851 if (row_host_id > current_host->getHostId()) {
852 current_host = retrieveHost(r, row, row_host_id);
853 hosts.push_back(current_host);
860 opt_proc4_->retrieveOption(cfg, r, row);
866 opt_proc6_->retrieveOption(cfg, r, row);
883 static size_t getRequiredColumnsNum(
const FetchedOptions& fetched_options) {
884 return (fetched_options == DHCP4_AND_DHCP6 ? 2 * OPTION_COLUMNS :
891 OptionProcessorPtr opt_proc4_;
896 OptionProcessorPtr opt_proc6_;
911class PgSqlHostIPv6Exchange :
public PgSqlHostWithOptionsExchange {
915 static const size_t RESERVATION_COLUMNS = 5;
923 PgSqlHostIPv6Exchange(
const FetchedOptions& fetched_options)
924 : PgSqlHostWithOptionsExchange(fetched_options, RESERVATION_COLUMNS),
925 reservation_id_index_(findAvailColumn()),
926 address_index_(reservation_id_index_ + 1),
927 prefix_len_index_(reservation_id_index_ + 2),
928 type_index_(reservation_id_index_ + 3),
929 iaid_index_(reservation_id_index_ + 4),
930 most_recent_reservation_id_(0) {
933 columns_[reservation_id_index_] =
"reservation_id";
934 columns_[address_index_] =
"address";
935 columns_[prefix_len_index_] =
"prefix_len";
936 columns_[type_index_] =
"type";
937 columns_[iaid_index_] =
"dhcp6_iaid";
939 BOOST_STATIC_ASSERT(4 < RESERVATION_COLUMNS);
948 PgSqlHostWithOptionsExchange::clear();
949 most_recent_reservation_id_ = 0;
955 uint64_t getReservationId(
const PgSqlResult& r,
int row)
const {
956 uint64_t resv_id = 0;
957 if (!isColumnNull(r, row, reservation_id_index_)) {
958 getColumnValue(r, row, reservation_id_index_, resv_id);
972 getColumnValue(r, row, type_index_, tmp);
987 "invalid IPv6 reservation type returned: "
988 << tmp <<
". Only 0 or 2 are allowed.");
996 getColumnValue(r, row, prefix_len_index_, prefix_len);
1005 return (reservation);
1032 PgSqlHostWithOptionsExchange::processRowData(hosts, r, row);
1035 if (hosts.empty()) {
1037 " IPv6 reservation");
1042 uint64_t reservation_id = getReservationId(r, row);
1043 if (reservation_id && (reservation_id > most_recent_reservation_id_)) {
1044 HostPtr host = boost::const_pointer_cast<Host>(hosts.back());
1045 host->addReservation(retrieveReservation(r, row));
1046 most_recent_reservation_id_ = reservation_id;
1054 size_t reservation_id_index_;
1057 size_t address_index_;
1060 size_t prefix_len_index_;
1071 uint64_t most_recent_reservation_id_;
1088 static const size_t RESRV_COLUMNS = 6;
1095 PgSqlIPv6ReservationExchange()
1099 columns_[0] =
"host_id";
1100 columns_[1] =
"address";
1101 columns_[2] =
"prefix_len";
1102 columns_[3] =
"type";
1103 columns_[4] =
"dhcp6_iaid";
1105 BOOST_STATIC_ASSERT(5 < RESRV_COLUMNS);
1124 const bool unique_ip) {
1142 bind_array->add(type);
1146 bind_array->addNull();
1149 bind_array->add(host_id);
1158 }
catch (
const std::exception& ex) {
1160 "Could not create bind array from IPv6 Reservation: "
1161 << resv_.toText() <<
", reason: " << ex.
what());
1164 return (bind_array);
1178 static const size_t OPTION_ID_COL = 0;
1179 static const size_t CODE_COL = 1;
1180 static const size_t VALUE_COL = 2;
1181 static const size_t FORMATTED_VALUE_COL = 3;
1182 static const size_t SPACE_COL = 4;
1183 static const size_t PERSISTENT_COL = 5;
1184 static const size_t CANCELLED_COL = 6;
1185 static const size_t USER_CONTEXT_COL = 7;
1186 static const size_t DHCP_SUBNET_ID_COL = 8;
1187 static const size_t HOST_ID_COL = 9;
1189 static const size_t OPTION_COLUMNS = 10;
1194 PgSqlOptionExchange()
1196 value_len_(0), option_() {
1197 columns_[OPTION_ID_COL] =
"option_id";
1198 columns_[CODE_COL] =
"code";
1199 columns_[VALUE_COL] =
"value";
1200 columns_[FORMATTED_VALUE_COL] =
"formatted_value";
1201 columns_[SPACE_COL] =
"space";
1202 columns_[PERSISTENT_COL] =
"persistent";
1203 columns_[CANCELLED_COL] =
"cancelled";
1204 columns_[USER_CONTEXT_COL] =
"user_context";
1205 columns_[DHCP_SUBNET_ID_COL] =
"dhcp_subnet_id";
1206 columns_[HOST_ID_COL] =
"host_id";
1208 BOOST_STATIC_ASSERT(10 <= OPTION_COLUMNS);
1220 const std::string& opt_space,
1233 bind_array->add(option_->getType());
1243 const uint8_t* buf_ptr = buf.getData();
1244 value_.assign(buf_ptr + opt_desc.
option_->getHeaderLen(),
1245 buf_ptr + buf.getLength());
1246 value_len_ = value_.size();
1247 bind_array->add(value_);
1258 bind_array->addNull();
1262 if (!opt_space.empty()) {
1263 bind_array->addTempString(opt_space);
1265 bind_array->addNull();
1277 std::string user_context_ = ctx->str();
1278 bind_array->addTempString(user_context_);
1280 bind_array->addNull();
1287 bind_array->add(host_id);
1289 }
catch (
const std::exception& ex) {
1291 "Could not create bind array for inserting DHCP "
1292 "host option: " << option_->toText() <<
", reason: "
1296 return (bind_array);
1302 std::vector<uint8_t> value_;
1503 const bool return_last_id =
false);
1537 const std::string& opt_space,
1552 const uint64_t host_id);
1575 boost::shared_ptr<PgSqlHostExchange> exchange,
1599 const uint8_t* identifier_begin,
1600 const size_t identifier_len,
1602 boost::shared_ptr<PgSqlHostExchange> exchange)
const;
1624 std::pair<uint32_t, uint32_t>
getVersion(
const std::string& timer_name = std::string())
const;
1647typedef boost::array<PgSqlTaggedStatement, PgSqlHostDataSourceImpl::NUM_STATEMENTS>
1648TaggedStatementArray;
1652TaggedStatementArray tagged_statements = { {
1661 "SELECT h.host_id, h.dhcp_identifier, h.dhcp_identifier_type, "
1662 " h.dhcp4_subnet_id, h.dhcp6_subnet_id, h.ipv4_address, "
1663 " h.hostname, h.dhcp4_client_classes, h.dhcp6_client_classes, "
1665 " h.dhcp4_next_server, h.dhcp4_server_hostname, "
1666 " h.dhcp4_boot_file_name, h.auth_key, "
1667 " o4.option_id, o4.code, o4.value, o4.formatted_value, o4.space, "
1668 " o4.persistent, o4.cancelled, o4.user_context, "
1669 " o6.option_id, o6.code, o6.value, o6.formatted_value, o6.space, "
1670 " o6.persistent, o6.cancelled, o6.user_context, "
1671 " r.reservation_id, host(r.address), r.prefix_len, r.type, r.dhcp6_iaid "
1673 "LEFT JOIN dhcp4_options AS o4 ON h.host_id = o4.host_id "
1674 "LEFT JOIN dhcp6_options AS o6 ON h.host_id = o6.host_id "
1675 "LEFT JOIN ipv6_reservations AS r ON h.host_id = r.host_id "
1676 "WHERE dhcp_identifier = $1 AND dhcp_identifier_type = $2 "
1677 "ORDER BY h.host_id, o4.option_id, o6.option_id, r.reservation_id"
1687 "SELECT h.host_id, h.dhcp_identifier, h.dhcp_identifier_type, "
1688 " h.dhcp4_subnet_id, h.dhcp6_subnet_id, h.ipv4_address, h.hostname, "
1689 " h.dhcp4_client_classes, h.dhcp6_client_classes, h.user_context, "
1690 " h.dhcp4_next_server, h.dhcp4_server_hostname, "
1691 " h.dhcp4_boot_file_name, h.auth_key, "
1692 " o.option_id, o.code, o.value, o.formatted_value, o.space, "
1693 " o.persistent, o.cancelled, o.user_context "
1695 "LEFT JOIN dhcp4_options AS o ON h.host_id = o.host_id "
1696 "WHERE ipv4_address = $1 "
1697 "ORDER BY h.host_id, o.option_id"
1706 "get_host_subid4_dhcpid",
1707 "SELECT h.host_id, h.dhcp_identifier, h.dhcp_identifier_type, "
1708 " h.dhcp4_subnet_id, h.dhcp6_subnet_id, h.ipv4_address, h.hostname, "
1709 " h.dhcp4_client_classes, h.dhcp6_client_classes, h.user_context, "
1710 " h.dhcp4_next_server, h.dhcp4_server_hostname, "
1711 " h.dhcp4_boot_file_name, h.auth_key, "
1712 " o.option_id, o.code, o.value, o.formatted_value, o.space, "
1713 " o.persistent, o.cancelled, o.user_context "
1715 "LEFT JOIN dhcp4_options AS o ON h.host_id = o.host_id "
1716 "WHERE h.dhcp4_subnet_id = $1 AND h.dhcp_identifier_type = $2 "
1717 " AND h.dhcp_identifier = $3 "
1718 "ORDER BY h.host_id, o.option_id"
1727 "get_host_subid6_dhcpid",
1728 "SELECT h.host_id, h.dhcp_identifier, "
1729 " h.dhcp_identifier_type, h.dhcp4_subnet_id, "
1730 " h.dhcp6_subnet_id, h.ipv4_address, h.hostname, "
1731 " h.dhcp4_client_classes, h.dhcp6_client_classes, h.user_context, "
1732 " h.dhcp4_next_server, h.dhcp4_server_hostname, "
1733 " h.dhcp4_boot_file_name, h.auth_key, "
1734 " o.option_id, o.code, o.value, o.formatted_value, o.space, "
1735 " o.persistent, o.cancelled, o.user_context, "
1736 " r.reservation_id, host(r.address), r.prefix_len, r.type, r.dhcp6_iaid "
1738 "LEFT JOIN dhcp6_options AS o ON h.host_id = o.host_id "
1739 "LEFT JOIN ipv6_reservations AS r ON h.host_id = r.host_id "
1740 "WHERE h.dhcp6_subnet_id = $1 AND h.dhcp_identifier_type = $2 "
1741 " AND h.dhcp_identifier = $3 "
1742 "ORDER BY h.host_id, o.option_id, r.reservation_id"
1752 "get_host_subid_addr",
1753 "SELECT h.host_id, h.dhcp_identifier, h.dhcp_identifier_type, "
1754 " h.dhcp4_subnet_id, h.dhcp6_subnet_id, h.ipv4_address, h.hostname, "
1755 " h.dhcp4_client_classes, h.dhcp6_client_classes, h.user_context, "
1756 " h.dhcp4_next_server, h.dhcp4_server_hostname, "
1757 " h.dhcp4_boot_file_name, h.auth_key, "
1758 " o.option_id, o.code, o.value, o.formatted_value, o.space, "
1759 " o.persistent, o.cancelled, o.user_context "
1761 "LEFT JOIN dhcp4_options AS o ON h.host_id = o.host_id "
1762 "WHERE h.dhcp4_subnet_id = $1 AND h.ipv4_address = $2 "
1763 "ORDER BY h.host_id, o.option_id"
1776 "SELECT h.host_id, h.dhcp_identifier, "
1777 " h.dhcp_identifier_type, h.dhcp4_subnet_id, "
1778 " h.dhcp6_subnet_id, h.ipv4_address, h.hostname, "
1779 " h.dhcp4_client_classes, h.dhcp6_client_classes, h.user_context, "
1780 " h.dhcp4_next_server, h.dhcp4_server_hostname, "
1781 " h.dhcp4_boot_file_name, h.auth_key, "
1782 " o.option_id, o.code, o.value, o.formatted_value, o.space, "
1783 " o.persistent, o.cancelled, o.user_context, "
1784 " r.reservation_id, host(r.address), r.prefix_len, r.type, "
1787 "LEFT JOIN dhcp6_options AS o ON h.host_id = o.host_id "
1788 "LEFT JOIN ipv6_reservations AS r ON h.host_id = r.host_id "
1789 "WHERE h.host_id = "
1790 " (SELECT host_id FROM ipv6_reservations "
1791 " WHERE address = cast($1 as inet) AND prefix_len = $2) "
1792 "ORDER BY h.host_id, o.option_id, r.reservation_id"
1804 "get_host_subid6_addr",
1805 "SELECT h.host_id, h.dhcp_identifier, "
1806 " h.dhcp_identifier_type, h.dhcp4_subnet_id, "
1807 " h.dhcp6_subnet_id, h.ipv4_address, h.hostname, "
1808 " h.dhcp4_client_classes, h.dhcp6_client_classes, h.user_context, "
1809 " h.dhcp4_next_server, h.dhcp4_server_hostname, "
1810 " h.dhcp4_boot_file_name, h.auth_key, "
1811 " o.option_id, o.code, o.value, o.formatted_value, o.space, "
1812 " o.persistent, o.cancelled, o.user_context, "
1813 " r.reservation_id, host(r.address), r.prefix_len, r.type, "
1816 "LEFT JOIN dhcp6_options AS o ON h.host_id = o.host_id "
1817 "LEFT JOIN ipv6_reservations AS r ON h.host_id = r.host_id "
1818 "WHERE h.dhcp6_subnet_id = $1 AND h.host_id IN "
1819 " (SELECT host_id FROM ipv6_reservations "
1820 " WHERE address = cast($2 as inet)) "
1821 "ORDER BY h.host_id, o.option_id, r.reservation_id"
1835 "SELECT h.host_id, h.dhcp_identifier, "
1836 " h.dhcp_identifier_type, h.dhcp4_subnet_id, "
1837 " h.dhcp6_subnet_id, h.ipv4_address, h.hostname, "
1838 " h.dhcp4_client_classes, h.dhcp6_client_classes, h.user_context, "
1839 " h.dhcp4_next_server, h.dhcp4_server_hostname, "
1840 " h.dhcp4_boot_file_name, h.auth_key, "
1841 " o.option_id, o.code, o.value, o.formatted_value, o.space, "
1842 " o.persistent, o.cancelled, o.user_context, "
1843 " r.reservation_id, r.address, r.prefix_len, r.type, "
1846 "LEFT JOIN dhcp6_options AS o ON h.host_id = o.host_id "
1847 "LEFT JOIN ipv6_reservations AS r ON h.host_id = r.host_id "
1848 "WHERE h.host_id IN "
1849 " (SELECT host_id FROM ipv6_reservations "
1850 " WHERE address = cast($1 as inet)) "
1851 "ORDER BY h.host_id, o.option_id, r.reservation_id"
1863 "SELECT h.host_id, h.dhcp_identifier, h.dhcp_identifier_type, "
1864 " h.dhcp4_subnet_id, h.dhcp6_subnet_id, h.ipv4_address, h.hostname, "
1865 " h.dhcp4_client_classes, h.dhcp6_client_classes, h.user_context, "
1866 " h.dhcp4_next_server, h.dhcp4_server_hostname, "
1867 " h.dhcp4_boot_file_name, h.auth_key, "
1868 " o.option_id, o.code, o.value, o.formatted_value, o.space, "
1869 " o.persistent, o.cancelled, o.user_context "
1871 "LEFT JOIN dhcp4_options AS o ON h.host_id = o.host_id "
1872 "WHERE h.dhcp4_subnet_id = $1 "
1873 "ORDER BY h.host_id, o.option_id"
1889 "SELECT h.host_id, h.dhcp_identifier, "
1890 " h.dhcp_identifier_type, h.dhcp4_subnet_id, "
1891 " h.dhcp6_subnet_id, h.ipv4_address, h.hostname, "
1892 " h.dhcp4_client_classes, h.dhcp6_client_classes, h.user_context, "
1893 " h.dhcp4_next_server, h.dhcp4_server_hostname, "
1894 " h.dhcp4_boot_file_name, h.auth_key, "
1895 " o.option_id, o.code, o.value, o.formatted_value, o.space, "
1896 " o.persistent, o.cancelled, o.user_context, "
1897 " r.reservation_id, host(r.address), r.prefix_len, r.type, r.dhcp6_iaid "
1899 "LEFT JOIN dhcp6_options AS o ON h.host_id = o.host_id "
1900 "LEFT JOIN ipv6_reservations AS r ON h.host_id = r.host_id "
1901 "WHERE h.dhcp6_subnet_id = $1 "
1902 "ORDER BY h.host_id, o.option_id, r.reservation_id"
1913 "get_host_hostname",
1914 "SELECT h.host_id, h.dhcp_identifier, h.dhcp_identifier_type, "
1915 " h.dhcp4_subnet_id, h.dhcp6_subnet_id, h.ipv4_address, "
1916 " h.hostname, h.dhcp4_client_classes, h.dhcp6_client_classes, "
1918 " h.dhcp4_next_server, h.dhcp4_server_hostname, "
1919 " h.dhcp4_boot_file_name, h.auth_key, "
1920 " o4.option_id, o4.code, o4.value, o4.formatted_value, o4.space, "
1921 " o4.persistent, o4.cancelled, o4.user_context, "
1922 " o6.option_id, o6.code, o6.value, o6.formatted_value, o6.space, "
1923 " o6.persistent, o6.cancelled, o6.user_context, "
1924 " r.reservation_id, host(r.address), r.prefix_len, r.type, r.dhcp6_iaid "
1926 "LEFT JOIN dhcp4_options AS o4 ON h.host_id = o4.host_id "
1927 "LEFT JOIN dhcp6_options AS o6 ON h.host_id = o6.host_id "
1928 "LEFT JOIN ipv6_reservations AS r ON h.host_id = r.host_id "
1929 "WHERE lower(h.hostname) = $1 "
1930 "ORDER BY h.host_id, o4.option_id, o6.option_id, r.reservation_id"
1940 "get_host_hostname_subid4",
1941 "SELECT h.host_id, h.dhcp_identifier, h.dhcp_identifier_type, "
1942 " h.dhcp4_subnet_id, h.dhcp6_subnet_id, h.ipv4_address, h.hostname, "
1943 " h.dhcp4_client_classes, h.dhcp6_client_classes, h.user_context, "
1944 " h.dhcp4_next_server, h.dhcp4_server_hostname, "
1945 " h.dhcp4_boot_file_name, h.auth_key, "
1946 " o.option_id, o.code, o.value, o.formatted_value, o.space, "
1947 " o.persistent, o.cancelled, o.user_context "
1949 "LEFT JOIN dhcp4_options AS o ON h.host_id = o.host_id "
1950 "WHERE lower(h.hostname) = $1 AND h.dhcp4_subnet_id = $2 "
1951 "ORDER BY h.host_id, o.option_id"
1963 "get_host_hostname_subid6",
1964 "SELECT h.host_id, h.dhcp_identifier, "
1965 " h.dhcp_identifier_type, h.dhcp4_subnet_id, "
1966 " h.dhcp6_subnet_id, h.ipv4_address, h.hostname, "
1967 " h.dhcp4_client_classes, h.dhcp6_client_classes, h.user_context, "
1968 " h.dhcp4_next_server, h.dhcp4_server_hostname, "
1969 " h.dhcp4_boot_file_name, h.auth_key, "
1970 " o.option_id, o.code, o.value, o.formatted_value, o.space, "
1971 " o.persistent, o.cancelled, o.user_context, "
1972 " r.reservation_id, host(r.address), r.prefix_len, r.type, r.dhcp6_iaid "
1974 "LEFT JOIN dhcp6_options AS o ON h.host_id = o.host_id "
1975 "LEFT JOIN ipv6_reservations AS r ON h.host_id = r.host_id "
1976 "WHERE lower(h.hostname) = $1 AND h.dhcp6_subnet_id = $2 "
1977 "ORDER BY h.host_id, o.option_id, r.reservation_id"
1987 "get_host_subid4_page",
1988 "SELECT h.host_id, h.dhcp_identifier, h.dhcp_identifier_type, "
1989 " h.dhcp4_subnet_id, h.dhcp6_subnet_id, h.ipv4_address, h.hostname, "
1990 " h.dhcp4_client_classes, h.dhcp6_client_classes, h.user_context, "
1991 " h.dhcp4_next_server, h.dhcp4_server_hostname, "
1992 " h.dhcp4_boot_file_name, h.auth_key, "
1993 " o.option_id, o.code, o.value, o.formatted_value, o.space, "
1994 " o.persistent, o.cancelled, o.user_context "
1995 "FROM ( SELECT * FROM hosts AS h "
1996 " WHERE h.dhcp4_subnet_id = $1 AND h.host_id > $2 "
1997 " ORDER BY h.host_id "
1999 "LEFT JOIN dhcp4_options AS o ON h.host_id = o.host_id "
2000 "ORDER BY h.host_id, o.option_id"
2012 "get_host_subid6_page",
2013 "SELECT h.host_id, h.dhcp_identifier, "
2014 " h.dhcp_identifier_type, h.dhcp4_subnet_id, "
2015 " h.dhcp6_subnet_id, h.ipv4_address, h.hostname, "
2016 " h.dhcp4_client_classes, h.dhcp6_client_classes, h.user_context, "
2017 " h.dhcp4_next_server, h.dhcp4_server_hostname, "
2018 " h.dhcp4_boot_file_name, h.auth_key, "
2019 " o.option_id, o.code, o.value, o.formatted_value, o.space, "
2020 " o.persistent, o.cancelled, o.user_context, "
2021 " r.reservation_id, host(r.address), r.prefix_len, r.type, r.dhcp6_iaid "
2022 "FROM ( SELECT * FROM hosts AS h "
2023 " WHERE h.dhcp6_subnet_id = $1 AND h.host_id > $2 "
2024 " ORDER BY h.host_id "
2026 "LEFT JOIN dhcp6_options AS o ON h.host_id = o.host_id "
2027 "LEFT JOIN ipv6_reservations AS r ON h.host_id = r.host_id "
2028 "ORDER BY h.host_id, o.option_id, r.reservation_id"
2039 "SELECT h.host_id, h.dhcp_identifier, h.dhcp_identifier_type, "
2040 " h.dhcp4_subnet_id, h.dhcp6_subnet_id, h.ipv4_address, h.hostname, "
2041 " h.dhcp4_client_classes, h.dhcp6_client_classes, h.user_context, "
2042 " h.dhcp4_next_server, h.dhcp4_server_hostname, "
2043 " h.dhcp4_boot_file_name, h.auth_key, "
2044 " o.option_id, o.code, o.value, o.formatted_value, o.space, "
2045 " o.persistent, o.cancelled, o.user_context "
2046 "FROM ( SELECT * FROM hosts AS h "
2047 " WHERE h.host_id > $1 "
2048 " ORDER BY h.host_id "
2050 "LEFT JOIN dhcp4_options AS o ON h.host_id = o.host_id "
2051 "ORDER BY h.host_id, o.option_id"
2064 "SELECT h.host_id, h.dhcp_identifier, "
2065 " h.dhcp_identifier_type, h.dhcp4_subnet_id, "
2066 " h.dhcp6_subnet_id, h.ipv4_address, h.hostname, "
2067 " h.dhcp4_client_classes, h.dhcp6_client_classes, h.user_context, "
2068 " h.dhcp4_next_server, h.dhcp4_server_hostname, "
2069 " h.dhcp4_boot_file_name, h.auth_key, "
2070 " o.option_id, o.code, o.value, o.formatted_value, o.space, "
2071 " o.persistent, o.cancelled, o.user_context, "
2072 " r.reservation_id, host(r.address), r.prefix_len, r.type, r.dhcp6_iaid "
2073 "FROM ( SELECT * FROM hosts AS h "
2074 " WHERE h.host_id > $1 "
2075 " ORDER BY h.host_id "
2077 "LEFT JOIN dhcp6_options AS o ON h.host_id = o.host_id "
2078 "LEFT JOIN ipv6_reservations AS r ON h.host_id = r.host_id "
2079 "ORDER BY h.host_id, o.option_id, r.reservation_id"
2090 "insert_host_non_unique_ip",
2091 "INSERT INTO hosts(dhcp_identifier, dhcp_identifier_type, "
2092 " dhcp4_subnet_id, dhcp6_subnet_id, ipv4_address, hostname, "
2093 " dhcp4_client_classes, dhcp6_client_classes, user_context, "
2094 " dhcp4_next_server, dhcp4_server_hostname, dhcp4_boot_file_name, auth_key)"
2095 "VALUES ( $1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13 ) "
2116 "insert_host_unique_ip",
2117 "INSERT INTO hosts(dhcp_identifier, dhcp_identifier_type, "
2118 " dhcp4_subnet_id, dhcp6_subnet_id, ipv4_address, hostname, "
2119 " dhcp4_client_classes, dhcp6_client_classes, user_context, "
2120 " dhcp4_next_server, dhcp4_server_hostname, dhcp4_boot_file_name, auth_key)"
2121 " SELECT $1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13"
2122 " WHERE NOT EXISTS ("
2123 " SELECT 1 FROM hosts WHERE ipv4_address = $14 AND dhcp4_subnet_id = $15"
2134 "insert_v6_resrv_non_unique",
2135 "INSERT INTO ipv6_reservations(address, prefix_len, type, "
2136 " dhcp6_iaid, host_id) "
2137 "VALUES (cast($1 as inet), $2, $3, $4, $5)"
2145 "insert_v6_resrv_unique",
2146 "INSERT INTO ipv6_reservations(address, prefix_len, type, "
2147 " dhcp6_iaid, host_id) "
2148 "SELECT cast($1 as inet), $2, $3, $4, $5 "
2149 " WHERE NOT EXISTS ("
2150 " SELECT 1 FROM ipv6_reservations"
2151 " WHERE address = cast($6 as inet) AND prefix_len = $7"
2162 "insert_v4_host_option",
2163 "INSERT INTO dhcp4_options(code, value, formatted_value, space, "
2164 " persistent, cancelled, user_context, host_id, scope_id) "
2165 "VALUES ($1, $2, $3, $4, $5, $6, $7, $8, 3)"
2174 "insert_v6_host_option",
2175 "INSERT INTO dhcp6_options(code, value, formatted_value, space, "
2176 " persistent, cancelled, user_context, host_id, scope_id) "
2177 "VALUES ($1, $2, $3, $4, $5, $6, $7, $8, 3)"
2185 "DELETE FROM hosts WHERE dhcp4_subnet_id = $1 AND ipv4_address = $2"
2193 "DELETE FROM hosts USING ipv6_reservations "
2194 " WHERE dhcp6_subnet_id = $1 AND ipv6_reservations.address = cast($2 as inet)"
2201 "del_host_subid4_id",
2202 "DELETE FROM hosts WHERE dhcp4_subnet_id = $1 "
2203 "AND dhcp_identifier_type = $2 "
2204 "AND dhcp_identifier = $3"
2211 "del_host_subid6_id",
2212 "DELETE FROM hosts WHERE dhcp6_subnet_id = $1 "
2213 "AND dhcp_identifier_type = $2 "
2214 "AND dhcp_identifier = $3"
2226 : conn_(parameters, io_service_accessor, db_reconnect_callback),
2227 is_readonly_(true) {
2239 lock_guard<mutex> lock(mgr_.
pool_->mutex_);
2240 if (!mgr_.
pool_->pool_.empty()) {
2242 mgr_.
pool_->pool_.pop_back();
2250 if (mgr_.
pool_->pool_.empty()) {
2260 lock_guard<mutex> lock(mgr_.pool_->mutex_);
2261 mgr_.pool_->pool_.push_back(ctx_);
2262 if (ctx_->conn_.isUnusable()) {
2263 mgr_.unusable_ =
true;
2265 }
else if (ctx_->conn_.isUnusable()) {
2266 mgr_.unusable_ =
true;
2271 : parameters_(parameters), ip_reservations_unique_(true), unusable_(false) {
2275 tls += parameters.count(
"trust-anchor");
2276 tls += parameters.count(
"cert-file");
2277 tls += parameters.count(
"key-file");
2278 tls += parameters.count(
"cipher-list");
2279#ifdef HAVE_PGSQL_SSL
2291 <<
"backend (built with this feature disabled)");
2297 timer_name_ += boost::lexical_cast<std::string>(
reinterpret_cast<uint64_t
>(
this));
2318 ctx->conn_.openDatabase();
2321 ctx->conn_.prepareStatements(tagged_statements.begin(),
2326 ctx->is_readonly_ = ctx->conn_.configuredReadOnly();
2330 if (!ctx->is_readonly_) {
2332 tagged_statements.end());
2337 ctx->host_ipv4_exchange_.reset(
new PgSqlHostWithOptionsExchange(PgSqlHostWithOptionsExchange::DHCP4_ONLY));
2338 ctx->host_ipv6_exchange_.reset(
new PgSqlHostIPv6Exchange(PgSqlHostWithOptionsExchange::DHCP6_ONLY));
2339 ctx->host_ipv46_exchange_.reset(
new PgSqlHostIPv6Exchange(PgSqlHostWithOptionsExchange::DHCP4_AND_DHCP6));
2340 ctx->host_ipv6_reservation_exchange_.reset(
new PgSqlIPv6ReservationExchange());
2341 ctx->host_option_exchange_.reset(
new PgSqlOptionExchange());
2361 bool reopened =
false;
2363 const std::string timer_name = db_reconnect_ctl->timerName();
2368 std::list<std::string> host_db_access_list = cfg_db->getHostDbAccessStringList();
2369 for (std::string& hds : host_db_access_list) {
2376 }
catch (
const std::exception& ex) {
2392 if (!db_reconnect_ctl->checkRetries()) {
2395 .arg(db_reconnect_ctl->maxRetries());
2408 .arg(db_reconnect_ctl->maxRetries() - db_reconnect_ctl->retriesLeft() + 1)
2409 .arg(db_reconnect_ctl->maxRetries())
2410 .arg(db_reconnect_ctl->retryInterval());
2416 db_reconnect_ctl->retryInterval(),
2429 const bool return_last_id) {
2430 uint64_t last_id = 0;
2431 PgSqlResult r(PQexecPrepared(ctx->conn_, tagged_statements[stindex].name,
2432 tagged_statements[stindex].nbparams,
2433 &bind_array->values_[0],
2434 &bind_array->lengths_[0],
2435 &bind_array->formats_[0], 0));
2437 int s = PQresultStatus(r);
2439 if (s != PGRES_COMMAND_OK) {
2447 ctx->conn_.checkStatementError(r, tagged_statements[stindex]);
2451 char* rows_affected = PQcmdTuples(r);
2452 if (!rows_affected) {
2454 "Could not retrieve the number of affected rows.");
2462 if (rows_affected[0] ==
'0') {
2466 if (return_last_id) {
2477 PgSqlResult r(PQexecPrepared(ctx->conn_, tagged_statements[stindex].name,
2478 tagged_statements[stindex].nbparams,
2479 &bind_array->values_[0],
2480 &bind_array->lengths_[0],
2481 &bind_array->formats_[0], 0));
2483 int s = PQresultStatus(r);
2485 if (s != PGRES_COMMAND_OK) {
2488 ctx->conn_.checkStatementError(r, tagged_statements[stindex]);
2493 char* rows_deleted = PQcmdTuples(r);
2494 if (!rows_deleted) {
2496 "Could not retrieve the number of deleted rows.");
2498 return (rows_deleted[0] !=
'0');
2517 const std::string& opt_space,
2520 PsqlBindArrayPtr bind_array = ctx->host_option_exchange_->createBindForSend(opt_desc, opt_space,
id);
2529 const uint64_t host_id) {
2532 std::list<std::string> option_spaces = options_cfg->getOptionSpaceNames();
2533 std::list<std::string> vendor_spaces = options_cfg->getVendorIdsSpaceNames();
2534 option_spaces.insert(option_spaces.end(), vendor_spaces.begin(),
2535 vendor_spaces.end());
2539 for (
auto const& space : option_spaces) {
2541 if (options && !options->empty()) {
2542 for (
auto const& opt : *options) {
2553 boost::shared_ptr<PgSqlHostExchange> exchange,
2555 bool single)
const {
2558 PgSqlResult r(PQexecPrepared(ctx->conn_, tagged_statements[stindex].name,
2559 tagged_statements[stindex].nbparams,
2560 &bind_array->values_[0],
2561 &bind_array->lengths_[0],
2562 &bind_array->formats_[0], 0));
2564 ctx->conn_.checkStatementError(r, tagged_statements[stindex]);
2567 for (
int row = 0; row < rows; ++row) {
2568 exchange->processRowData(result, r, row);
2570 if (single && result.size() > 1) {
2572 "database where only one was expected for query "
2573 << tagged_statements[stindex].name);
2582 const uint8_t* identifier_begin,
2583 const size_t identifier_len,
2585 boost::shared_ptr<PgSqlHostExchange> exchange)
const {
2591 bind_array->add(subnet_id);
2594 bind_array->add(
static_cast<uint8_t
>(identifier_type));
2597 bind_array->add(identifier_begin, identifier_len);
2604 if (!collection.empty()) {
2605 result = *collection.begin();
2611std::pair<uint32_t, uint32_t>
2623 if (ctx->is_readonly_) {
2625 " to operate in read only mode");
2640 return (impl_->parameters_);
2650 impl_->checkReadOnly(ctx);
2662 bool unique_ip = impl_->ip_reservations_unique_ && !host->getIPv4Reservation().isV4Zero()
2663 && host->getIPv4SubnetID() != SUBNET_ID_UNUSED;
2666 PsqlBindArrayPtr bind_array = ctx->host_ipv4_exchange_->createBindForSend(host, unique_ip);
2669 uint32_t host_id = impl_->addStatement(ctx,
2678 cfg_option4, host_id);
2685 cfg_option6, host_id);
2690 if (std::distance(v6resv.first, v6resv.second) > 0) {
2691 BOOST_FOREACH(
auto const& resv, v6resv) {
2692 impl_->addResv(ctx, resv.second, host_id);
2708 impl_->checkReadOnly(ctx);
2711 bind_array->add(subnet_id);
2715 bind_array->add(addr);
2721 bind_array->addTempString(addr.
toText());
2730 const uint8_t* identifier_begin,
2731 const size_t identifier_len) {
2737 impl_->checkReadOnly(ctx);
2742 bind_array->add(subnet_id);
2745 bind_array->add(
static_cast<uint8_t
>(identifier_type));
2748 bind_array->add(identifier_begin, identifier_len);
2757 const uint8_t* identifier_begin,
2758 const size_t identifier_len) {
2764 impl_->checkReadOnly(ctx);
2769 bind_array->add(subnet_id);
2772 bind_array->add(
static_cast<uint8_t
>(identifier_type));
2775 bind_array->add(identifier_begin, identifier_len);
2783 const uint8_t* identifier_begin,
2784 const size_t identifier_len)
const {
2793 bind_array->add(identifier_begin, identifier_len);
2796 bind_array->add(
static_cast<uint8_t
>(identifier_type));
2800 bind_array, ctx->host_ipv46_exchange_, result,
false);
2815 bind_array->add(subnet_id);
2819 bind_array, ctx->host_ipv4_exchange_, result,
false);
2834 bind_array->add(subnet_id);
2838 bind_array, ctx->host_ipv6_exchange_, result,
false);
2853 bind_array->add(hostname);
2857 bind_array, ctx->host_ipv46_exchange_, result,
false);
2873 bind_array->add(hostname);
2876 bind_array->add(subnet_id);
2880 bind_array, ctx->host_ipv4_exchange_, result,
false);
2896 bind_array->add(hostname);
2899 bind_array->add(subnet_id);
2903 bind_array, ctx->host_ipv6_exchange_, result,
false);
2911 uint64_t lower_host_id,
2921 bind_array->add(subnet_id);
2924 bind_array->add(lower_host_id);
2927 string page_size_data =
2928 boost::lexical_cast<std::string>(page_size.
page_size_);
2929 bind_array->add(page_size_data);
2933 bind_array, ctx->host_ipv4_exchange_, result,
false);
2941 uint64_t lower_host_id,
2951 bind_array->add(subnet_id);
2954 bind_array->add(lower_host_id);
2957 string page_size_data =
2958 boost::lexical_cast<std::string>(page_size.
page_size_);
2959 bind_array->add(page_size_data);
2963 bind_array, ctx->host_ipv6_exchange_, result,
false);
2970 uint64_t lower_host_id,
2980 bind_array->add(lower_host_id);
2983 string page_size_data =
2984 boost::lexical_cast<std::string>(page_size.
page_size_);
2985 bind_array->add(page_size_data);
2989 bind_array, ctx->host_ipv4_exchange_, result,
false);
2996 uint64_t lower_host_id,
3006 bind_array->add(lower_host_id);
3009 string page_size_data =
3010 boost::lexical_cast<std::string>(page_size.
page_size_);
3011 bind_array->add(page_size_data);
3015 bind_array, ctx->host_ipv6_exchange_, result,
false);
3030 bind_array->add(address);
3034 bind_array, ctx->host_ipv4_exchange_, result,
false);
3042 const uint8_t* identifier_begin,
3043 const size_t identifier_len)
const {
3048 return (impl_->getHost(ctx, subnet_id, identifier_type, identifier_begin, identifier_len,
3050 ctx->host_ipv4_exchange_));
3060 if (!address.
isV4()) {
3062 " wrong address type, address supplied is an IPv6 address");
3069 bind_array->add(subnet_id);
3072 bind_array->add(address);
3076 bind_array, ctx->host_ipv4_exchange_, collection,
true);
3080 if (!collection.empty()) {
3081 result = *collection.begin();
3094 if (!address.
isV4()) {
3096 " wrong address type, address supplied is an IPv6 address");
3103 bind_array->add(subnet_id);
3106 bind_array->add(address);
3110 bind_array, ctx->host_ipv4_exchange_, collection,
false);
3111 return (collection);
3117 const uint8_t* identifier_begin,
3118 const size_t identifier_len)
const {
3123 return (impl_->getHost(ctx, subnet_id, identifier_type, identifier_begin, identifier_len,
3125 ctx->host_ipv6_exchange_));
3130 const uint8_t prefix_len)
const {
3131 if (!prefix.
isV6()) {
3133 "wrong address type, address supplied is an IPv4 address");
3144 bind_array->add(prefix);
3147 bind_array->add(prefix_len);
3151 bind_array, ctx->host_ipv6_exchange_, collection,
true);
3155 if (!collection.empty()) {
3156 result = *collection.begin();
3165 if (!address.
isV6()) {
3167 "wrong address type, address supplied is an IPv4 address");
3178 bind_array->add(subnet_id);
3181 bind_array->add(address);
3185 bind_array, ctx->host_ipv6_exchange_, collection,
true);
3189 if (!collection.empty()) {
3190 result = *collection.begin();
3199 if (!address.
isV6()) {
3201 "wrong address type, address supplied is an IPv4 address");
3212 bind_array->add(subnet_id);
3215 bind_array->add(address);
3219 bind_array, ctx->host_ipv6_exchange_, collection,
false);
3220 return (collection);
3225 if (!address.
isV6()) {
3227 "wrong address type, address supplied is an IPv4 address");
3238 bind_array->add(address);
3242 bind_array, ctx->host_ipv6_exchange_, collection,
false);
3243 return (collection);
3253 impl_->checkReadOnly(ctx);
3276 std::string name =
"";
3282 name = ctx->conn_.getParameter(
"name");
3291 return (std::string(
"Host data source that stores host information"
3292 "in PostgreSQL database"));
3295std::pair<uint32_t, uint32_t>
3297 return(impl_->getVersion(timer_name));
3307 impl_->checkReadOnly(ctx);
3308 ctx->conn_.commit();
3318 impl_->checkReadOnly(ctx);
3319 ctx->conn_.rollback();
3324 impl_->ip_reservations_unique_ = unique;
3330 return (impl_->unusable_);
A generic exception that is thrown if a parameter given to a method is considered invalid in that con...
This is a base class for exceptions thrown from the DNS library module.
virtual const char * what() const
Returns a C-style character string of the cause of the exception.
A generic exception that is thrown when an unexpected error condition occurs.
The IOAddress class represents an IP addresses (version agnostic)
std::string toText() const
Convert the address to a string.
bool isV6() const
Convenience function to check for an IPv6 address.
bool isV4() const
Convenience function to check for an IPv4 address.
static ElementPtr fromJSON(const std::string &in, bool preproc=false)
These functions will parse the given string (JSON) representation of a compound element.
A standard Data module exception that is thrown if a parse error is encountered when constructing an ...
static bool invokeDbLostCallback(const util::ReconnectCtlPtr &db_reconnect_ctl)
Invokes the connection's lost connectivity callback.
static std::string redactedAccessString(const ParameterMap ¶meters)
Redact database access string.
static bool invokeDbFailedCallback(const util::ReconnectCtlPtr &db_reconnect_ctl)
Invokes the connection's restore failed connectivity callback.
static ParameterMap parse(const std::string &dbaccess)
Parse database access string.
static bool invokeDbRecoveredCallback(const util::ReconnectCtlPtr &db_reconnect_ctl)
Invokes the connection's restored connectivity callback.
static isc::asiolink::IOServicePtr & getIOService()
Returns pointer to the IO service.
std::map< std::string, std::string > ParameterMap
Database configuration parameter map.
Exception thrown on failure to open database.
Exception thrown on failure to execute a database function.
Database duplicate entry error.
Multiple lease records found where one expected.
Common PgSql Connector Pool.
static bool warned_about_tls
Emit the TLS support warning only once.
static const char DUPLICATE_KEY[]
Define the PgSql error state for a duplicate key error.
static void ensureSchemaVersion(const ParameterMap ¶meters, const DbCallback &cb=DbCallback(), const std::string &timer_name=std::string())
Retrieve schema version, validate it against the hardcoded version, and attempt to initialize the sch...
static std::pair< uint32_t, uint32_t > getVersion(const ParameterMap ¶meters, const IOServiceAccessorPtr &ac=IOServiceAccessorPtr(), const DbCallback &cb=DbCallback(), const std::string &timer_name=std::string())
Get the schema version.
Base class for marshalling data to and from PostgreSQL.
static void convertFromBytea(const PgSqlResult &r, const int row, const size_t col, uint8_t *buffer, const size_t buffer_size, size_t &bytes_converted)
Converts a column in a row in a result set to a binary bytes.
static bool isColumnNull(const PgSqlResult &r, const int row, const size_t col)
Returns true if a column within a row is null.
static void getColumnValue(const PgSqlResult &r, const int row, const size_t col, std::string &value)
Fetches text column value as a string.
RAII wrapper for PostgreSQL Result sets.
int getRows() const
Returns the number of rows in the result set.
RAII object representing a PostgreSQL transaction.
void commit()
Commits transaction.
Attempt to modify data in read-only database.
virtual void update(HostPtr const &host)
Attempts to update an existing host entry.
static CfgMgr & instance()
returns a single instance of Configuration Manager
SrvConfigPtr getCurrentCfg()
Returns a pointer to the current configuration.
static constexpr size_t MAX_CLIENT_ID_LEN
Maximum size of a client ID.
static bool delBackend(const std::string &db_type)
Delete an alternate host backend (aka host data source).
static void addBackend(const std::string &access)
Add an alternate host backend (aka host data source).
Wraps value holding size of the page with host reservations.
const size_t page_size_
Holds page size.
Represents a device with IPv4 and/or IPv6 reservations.
IdentifierType
Type of the host identifier.
static const IdentifierType LAST_IDENTIFIER_TYPE
Constant pointing to the last identifier of the IdentifierType enumeration.
IPv6 reservation for a host.
const asiolink::IOAddress & getPrefix() const
Returns prefix for the reservation.
Type getType() const
Returns reservation type.
Type
Type of the reservation.
uint8_t getPrefixLen() const
Returns prefix length.
static OptionDefinitionPtr getOptionDef(const std::string &space, const uint16_t code)
Return the first option definition matching a particular option code.
static OptionDefinitionPtr getVendorOptionDef(const Option::Universe u, const uint32_t vendor_id, const uint16_t code)
Returns vendor option definition for a given vendor-id and code.
static uint32_t optionSpaceToVendorId(const std::string &option_space)
Converts option space name to vendor id.
static OptionDefinitionPtr getRuntimeOptionDef(const std::string &space, const uint16_t code)
Returns runtime (non-standard) option definition by space and option code.
static OptionDefinitionPtr getLastResortOptionDef(const std::string &space, const uint16_t code)
Returns last resort option definition by space and option code.
OptionPtr option_
Option instance.
bool cancelled_
Cancelled flag.
std::string formatted_value_
Option value in textual (CSV) format.
bool persistent_
Persistence flag.
Universe
defines option universe DHCPv4 or DHCPv6
PostgreSQL Host Context Pool.
std::mutex mutex_
The mutex to protect pool access.
std::vector< PgSqlHostContextPtr > pool_
The vector of available contexts.
boost::shared_ptr< PgSqlHostIPv6Exchange > host_ipv46_exchange_
Pointer to an object representing an exchange which can be used to retrieve hosts,...
boost::shared_ptr< PgSqlIPv6ReservationExchange > host_ipv6_reservation_exchange_
Pointer to an object representing an exchange which can be used to insert new IPv6 reservation.
PgSqlHostContext(const DatabaseConnection::ParameterMap ¶meters, IOServiceAccessorPtr io_service_accessor, db::DbCallback db_reconnect_callback)
Constructor.
PgSqlConnection conn_
PostgreSQL connection.
boost::shared_ptr< PgSqlHostWithOptionsExchange > host_ipv4_exchange_
The exchange objects are used for transfer of data to/from the database.
bool is_readonly_
Indicates if the database is opened in read only mode.
boost::shared_ptr< PgSqlHostIPv6Exchange > host_ipv6_exchange_
Pointer to an object representing an exchange which can be used to retrieve hosts,...
boost::shared_ptr< PgSqlOptionExchange > host_option_exchange_
Pointer to an object representing an exchange which can be used to insert DHCPv4 or DHCPv6 option int...
Implementation of the PgSqlHostDataSource.
bool ip_reservations_unique_
Holds the setting whether the IP reservations must be unique or may be non-unique.
uint64_t addStatement(PgSqlHostContextPtr &ctx, PgSqlHostDataSourceImpl::StatementIndex stindex, PsqlBindArrayPtr &bind, const bool return_last_id=false)
Executes statements which insert a row into one of the tables.
static bool dbReconnect(ReconnectCtlPtr db_reconnect_ctl)
Attempts to reconnect the server to the host DB backend manager.
std::pair< uint32_t, uint32_t > getVersion(const std::string &timer_name=std::string()) const
Returns PostgreSQL schema version of the open database.
void checkReadOnly(PgSqlHostContextPtr &ctx) const
Throws exception if database is read only.
StatementIndex
Statement Tags.
@ GET_HOST_HOSTNAME_SUBID6
@ INSERT_HOST_NON_UNIQUE_IP
@ INSERT_V6_RESRV_NON_UNIQUE
@ GET_HOST_HOSTNAME_SUBID4
DatabaseConnection::ParameterMap parameters_
The parameters.
PgSqlHostContextPoolPtr pool_
The pool of contexts.
void addOption(PgSqlHostContextPtr &ctx, const PgSqlHostDataSourceImpl::StatementIndex &stindex, const OptionDescriptor &opt_desc, const std::string &opt_space, const Optional< SubnetID > &subnet_id, const HostID &host_id)
Inserts a single DHCP option into the database.
void addOptions(PgSqlHostContextPtr &ctx, const StatementIndex &stindex, const ConstCfgOptionPtr &options_cfg, const uint64_t host_id)
Inserts multiple options into the database.
bool delStatement(PgSqlHostContextPtr &ctx, PgSqlHostDataSourceImpl::StatementIndex stindex, PsqlBindArrayPtr &bind)
Executes statements that delete records.
static const StatementIndex WRITE_STMTS_BEGIN
Index of first statement performing write to the database.
~PgSqlHostDataSourceImpl()
Destructor.
void addResv(PgSqlHostContextPtr &ctx, const IPv6Resrv &resv, const HostID &id)
Inserts IPv6 Reservation into ipv6_reservation table.
std::string timer_name_
Timer name used to register database reconnect timer.
ConstHostPtr getHost(PgSqlHostContextPtr &ctx, const SubnetID &subnet_id, const Host::IdentifierType &identifier_type, const uint8_t *identifier_begin, const size_t identifier_len, StatementIndex stindex, boost::shared_ptr< PgSqlHostExchange > exchange) const
Retrieves a host by subnet and client's unique identifier.
PgSqlHostContextPtr createContext() const
Create a new context.
void getHostCollection(PgSqlHostContextPtr &ctx, StatementIndex stindex, PsqlBindArrayPtr bind, boost::shared_ptr< PgSqlHostExchange > exchange, ConstHostCollection &result, bool single) const
Creates collection of Host objects with associated information such as IPv6 reservations and/or DHCP ...
PgSqlHostDataSourceImpl(const DatabaseConnection::ParameterMap ¶meters)
Constructor.
bool unusable_
Indicates if there is at least one connection that can no longer be used for normal operations.
PgSqlHostContextPtr ctx_
The context.
~PgSqlHostContextAlloc()
Destructor.
PgSqlHostContextAlloc(PgSqlHostDataSourceImpl &mgr)
Constructor.
virtual bool del6(const SubnetID &subnet_id, const Host::IdentifierType &identifier_type, const uint8_t *identifier_begin, const size_t identifier_len)
Attempts to delete a host by (subnet6-id, identifier type, identifier)
virtual ConstHostPtr get4(const SubnetID &subnet_id, const Host::IdentifierType &identifier_type, const uint8_t *identifier_begin, const size_t identifier_len) const
Returns a host connected to the IPv4 subnet.
virtual bool isUnusable()
Flag which indicates if the host manager has at least one unusable connection.
virtual std::string getName() const
Returns the name of the open database.
virtual ConstHostCollection getAll(const Host::IdentifierType &identifier_type, const uint8_t *identifier_begin, const size_t identifier_len) const
Return all hosts connected to any subnet for which reservations have been made using a specified iden...
virtual std::string getDescription() const
Returns description of the backend.
virtual ConstHostCollection getAllbyHostname4(const std::string &hostname, const SubnetID &subnet_id) const
Return all hosts with a hostname in a DHCPv4 subnet.
virtual ConstHostCollection getPage6(const SubnetID &subnet_id, size_t &source_index, uint64_t lower_host_id, const HostPageSize &page_size) const
Returns range of hosts in a DHCPv6 subnet.
virtual bool del(const SubnetID &subnet_id, const asiolink::IOAddress &addr)
Attempts to delete hosts by (subnet-id, address)
virtual ConstHostCollection getPage4(const SubnetID &subnet_id, size_t &source_index, uint64_t lower_host_id, const HostPageSize &page_size) const
Returns range of hosts in a DHCPv4 subnet.
virtual void rollback()
Rollback Transactions.
virtual bool del4(const SubnetID &subnet_id, const Host::IdentifierType &identifier_type, const uint8_t *identifier_begin, const size_t identifier_len)
Attempts to delete a host by (subnet4-id, identifier type, identifier)
virtual ConstHostCollection getAll6(const SubnetID &subnet_id) const
Return all hosts in a DHCPv6 subnet.
virtual ConstHostCollection getAllbyHostname6(const std::string &hostname, const SubnetID &subnet_id) const
Return all hosts with a hostname in a DHCPv6 subnet.
virtual void commit()
Commit Transactions.
virtual bool setIPReservationsUnique(const bool unique)
Controls whether IP reservations are unique or non-unique.
virtual ConstHostCollection getAll4(const SubnetID &subnet_id) const
Return all hosts in a DHCPv4 subnet.
void update(HostPtr const &host)
Implements BaseHostDataSource::update() for PostgreSQL.
virtual isc::db::DatabaseConnection::ParameterMap getParameters() const
Return backend parameters.
PgSqlHostDataSource(const db::DatabaseConnection::ParameterMap ¶meters)
Constructor.
virtual void add(const HostPtr &host)
Adds a new host to the collection.
virtual ConstHostPtr get6(const SubnetID &subnet_id, const Host::IdentifierType &identifier_type, const uint8_t *identifier_begin, const size_t identifier_len) const
Returns a host connected to the IPv6 subnet.
virtual ~PgSqlHostDataSource()
Virtual destructor.
virtual ConstHostCollection getAllbyHostname(const std::string &hostname) const
Return all hosts with a hostname.
virtual std::pair< uint32_t, uint32_t > getVersion(const std::string &timer_name=std::string()) const
Returns backend version.
static const TimerMgrPtr & instance()
Returns pointer to the sole instance of the TimerMgr.
RAII class creating a critical section.
static MultiThreadingMgr & instance()
Returns a single instance of Multi Threading Manager.
A template representing an optional value.
The OutputBuffer class is a buffer abstraction for manipulating mutable data.
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
#define LOG_ERROR(LOGGER, MESSAGE)
Macro to conveniently test error output and log it.
#define LOG_INFO(LOGGER, MESSAGE)
Macro to conveniently test info output and log it.
#define LOG_DEBUG(LOGGER, LEVEL, MESSAGE)
Macro to conveniently test debug output and log it.
A wrapper interface for the ASIO library.
boost::shared_ptr< const Element > ConstElementPtr
boost::shared_ptr< IOServiceAccessor > IOServiceAccessorPtr
Pointer to an instance of IOServiceAccessor.
boost::shared_ptr< PsqlBindArray > PsqlBindArrayPtr
Defines a smart pointer to PsqlBindArray.
std::function< bool(util::ReconnectCtlPtr db_reconnect_ctl)> DbCallback
Defines a callback prototype for propagating events upward.
std::function< isc::asiolink::IOServicePtr()> IOServiceAccessor
Function which returns the IOService that can be used to recover the connection.
isc::log::Logger dhcpsrv_logger("dhcpsrv")
DHCP server library Logger.
boost::shared_ptr< PgSqlHostContext > PgSqlHostContextPtr
Type of pointers to contexts.
boost::shared_ptr< CfgOption > CfgOptionPtr
Non-const pointer.
boost::shared_ptr< Host > HostPtr
Pointer to the Host object.
std::vector< ConstHostPtr > ConstHostCollection
Collection of the const Host objects.
boost::shared_ptr< CfgDbAccess > CfgDbAccessPtr
A pointer to the CfgDbAccess.
const isc::log::MessageID DHCPSRV_PGSQL_HOST_DB_GET_VERSION
std::pair< IPv6ResrvIterator, IPv6ResrvIterator > IPv6ResrvRange
const int DHCPSRV_DBG_TRACE_DETAIL
Additional information.
const isc::log::MessageID DHCPSRV_PGSQL_NO_TLS_SUPPORT
boost::shared_ptr< OptionDefinition > OptionDefinitionPtr
Pointer to option definition object.
const isc::log::MessageID DHCPSRV_PGSQL_HOST_DB_RECONNECT_ATTEMPT_FAILED
const isc::log::MessageID DHCPSRV_PGSQL_TLS_SUPPORT
uint32_t SubnetID
Defines unique IPv4 or IPv6 subnet identifier.
uint64_t HostID
HostID (used only when storing in MySQL or PostgreSQL backends)
boost::shared_ptr< OptionContainer > OptionContainerPtr
Pointer to the OptionContainer object.
const isc::log::MessageID DHCPSRV_PGSQL_HOST_DB_RECONNECT_ATTEMPT_SCHEDULE
boost::shared_ptr< const Host > ConstHostPtr
Const pointer to the Host object.
const isc::log::MessageID DHCPSRV_PGSQL_HOST_DB_RECONNECT_FAILED
const size_t OPTION_VALUE_MAX_LEN
Maximum length of option value.
std::vector< uint8_t > OptionBuffer
buffer types used in DHCP code.
boost::shared_ptr< PgSqlHostContextPool > PgSqlHostContextPoolPtr
Type of pointers to context pools.
const isc::log::MessageID DHCPSRV_PGSQL_HOST_DB_READONLY
boost::shared_ptr< Option > OptionPtr
boost::shared_ptr< const CfgOption > ConstCfgOptionPtr
Const pointer.
boost::shared_ptr< ReconnectCtl > ReconnectCtlPtr
Pointer to an instance of ReconnectCtl.
Defines the logger used by the top-level component of kea-lfc.
#define DHCP4_OPTION_SPACE
global std option spaces
#define DHCP6_OPTION_SPACE
data::ConstElementPtr getContext() const
Returns const pointer to the user context.
static const int BINARY_FMT
Format value for binary data.