26 #include <boost/algorithm/string/split.hpp>
27 #include <boost/algorithm/string/classification.hpp>
28 #include <boost/array.hpp>
29 #include <boost/pointer_cast.hpp>
30 #include <boost/static_assert.hpp>
56 const uint8_t MAX_IDENTIFIER_TYPE =
static_cast<uint8_t
>(Host::LAST_IDENTIFIER_TYPE);
59 const size_t DHCP_IDENTIFIER_MAX_LEN = 128;
94 static const int HOST_ID_COL = 0;
95 static const int DHCP_IDENTIFIER_COL = 1;
96 static const int DHCP_IDENTIFIER_TYPE_COL = 2;
97 static const int DHCP4_SUBNET_ID_COL = 3;
98 static const int DHCP6_SUBNET_ID_COL = 4;
99 static const int IPV4_ADDRESS_COL = 5;
100 static const int HOSTNAME_COL = 6;
101 static const int DHCP4_CLIENT_CLASSES_COL = 7;
102 static const int DHCP6_CLIENT_CLASSES_COL = 8;
103 static const int USER_CONTEXT_COL = 9;
104 static const int DHCP4_NEXT_SERVER_COL = 10;
105 static const int DHCP4_SERVER_HOSTNAME_COL = 11;
106 static const int DHCP4_BOOT_FILE_NAME_COL = 12;
107 static const int AUTH_KEY_COL = 13;
109 static const size_t HOST_COLUMNS = 14;
119 PgSqlHostExchange(
const size_t additional_columns_num = 0)
125 columns_[HOST_ID_COL] =
"host_id";
126 columns_[DHCP_IDENTIFIER_COL] =
"dhcp_identifier";
127 columns_[DHCP_IDENTIFIER_TYPE_COL] =
"dhcp_identifier_type";
128 columns_[DHCP4_SUBNET_ID_COL] =
"dhcp4_subnet_id";
129 columns_[DHCP6_SUBNET_ID_COL] =
"dhcp6_subnet_id";
130 columns_[IPV4_ADDRESS_COL] =
"ipv4_address";
131 columns_[HOSTNAME_COL] =
"hostname";
132 columns_[DHCP4_CLIENT_CLASSES_COL] =
"dhcp4_client_classes";
133 columns_[DHCP6_CLIENT_CLASSES_COL] =
"dhcp6_client_classes";
134 columns_[USER_CONTEXT_COL] =
"user_context";
135 columns_[DHCP4_NEXT_SERVER_COL] =
"dhcp4_next_server";
136 columns_[DHCP4_SERVER_HOSTNAME_COL] =
"dhcp4_server_hostname";
137 columns_[DHCP4_BOOT_FILE_NAME_COL] =
"dhcp4_boot_file_name";
138 columns_[AUTH_KEY_COL] =
"auth_key";
140 BOOST_STATIC_ASSERT(12 < HOST_COLUMNS);
144 virtual ~PgSqlHostExchange() {
152 virtual void clear() {
170 size_t findAvailColumn()
const {
171 std::vector<std::string>::const_iterator empty_column =
172 std::find(columns_.begin(), columns_.end(), std::string());
173 return (std::distance(columns_.begin(), empty_column));
182 getColumnValue(r, row, HOST_ID_COL, host_id);
215 bind_array->add(host->getIdentifier());
218 bind_array->add(host->getIdentifierType());
221 if (host->getIPv4SubnetID() == SUBNET_ID_UNUSED) {
222 bind_array->addNull();
225 bind_array->add(host->getIPv4SubnetID());
229 if (host->getIPv6SubnetID() == SUBNET_ID_UNUSED) {
230 bind_array->addNull();
233 bind_array->add(host->getIPv6SubnetID());
237 bind_array->add((host->getIPv4Reservation()));
240 bind_array->add(host->getHostname());
244 bind_array->addTempString(host->getClientClasses4().toText(
","));
247 bind_array->addTempString(host->getClientClasses6().toText(
","));
252 std::string user_context_ = ctx->str();
253 bind_array->addTempString(user_context_);
255 bind_array->addNull();
259 bind_array->add((host->getNextServer()));
262 bind_array->add(host->getServerHostname());
265 bind_array->add(host->getBootFileName());
268 std::string key = host->getKey().toText();
270 bind_array->addNull();
272 bind_array->add(key);
279 bind_array->add(host->getIPv4Reservation());
280 bind_array->add(host->getIPv4SubnetID());
284 }
catch (
const std::exception& ex) {
287 "Could not create bind array from Host: "
288 << host->getHostname() <<
", reason: " << ex.
what());
312 HostID row_host_id = getHostId(r, row);
317 if (hosts.empty() || row_host_id != hosts.back()->getHostId()) {
318 HostPtr host = retrieveHost(r, row, row_host_id);
319 hosts.push_back(host);
335 const HostID& peeked_host_id = 0) {
339 HostID host_id = (peeked_host_id ? peeked_host_id : getHostId(r,row));
342 uint8_t identifier_value[DHCP_IDENTIFIER_MAX_LEN];
343 size_t identifier_len;
344 convertFromBytea(r, row, DHCP_IDENTIFIER_COL, identifier_value,
345 sizeof(identifier_value), identifier_len);
349 getColumnValue(r, row, DHCP_IDENTIFIER_TYPE_COL, type);
350 if (type > MAX_IDENTIFIER_TYPE) {
352 << static_cast<int>(type));
359 uint32_t subnet_id(SUBNET_ID_UNUSED);
360 if (!isColumnNull(r, row, DHCP4_SUBNET_ID_COL)) {
361 getColumnValue(r, row, DHCP4_SUBNET_ID_COL, subnet_id);
366 subnet_id = SUBNET_ID_UNUSED;
367 if (!isColumnNull(r, row, DHCP6_SUBNET_ID_COL)) {
368 getColumnValue(r, row, DHCP6_SUBNET_ID_COL, subnet_id);
374 if (!isColumnNull(r, row, IPV4_ADDRESS_COL)) {
375 getColumnValue(r, row, IPV4_ADDRESS_COL, addr4);
380 std::string hostname;
381 if (!isColumnNull(r, row, HOSTNAME_COL)) {
382 getColumnValue(r, row, HOSTNAME_COL, hostname);
386 std::string dhcp4_client_classes;
387 if (!isColumnNull(r, row, DHCP4_CLIENT_CLASSES_COL)) {
388 getColumnValue(r, row, DHCP4_CLIENT_CLASSES_COL, dhcp4_client_classes);
392 std::string dhcp6_client_classes;
393 if (!isColumnNull(r, row, DHCP6_CLIENT_CLASSES_COL)) {
394 getColumnValue(r, row, DHCP6_CLIENT_CLASSES_COL, dhcp6_client_classes);
398 std::string user_context;
399 if (!isColumnNull(r, row, USER_CONTEXT_COL)) {
400 getColumnValue(r, row, USER_CONTEXT_COL, user_context);
404 uint32_t dhcp4_next_server_as_uint32(0);
405 if (!isColumnNull(r, row, DHCP4_NEXT_SERVER_COL)) {
406 getColumnValue(r, row, DHCP4_NEXT_SERVER_COL, dhcp4_next_server_as_uint32);
411 std::string dhcp4_server_hostname;
412 if (!isColumnNull(r, row, DHCP4_SERVER_HOSTNAME_COL)) {
413 getColumnValue(r, row, DHCP4_SERVER_HOSTNAME_COL, dhcp4_server_hostname);
417 std::string dhcp4_boot_file_name;
418 if (!isColumnNull(r, row, DHCP4_BOOT_FILE_NAME_COL)) {
419 getColumnValue(r, row, DHCP4_BOOT_FILE_NAME_COL, dhcp4_boot_file_name);
423 std::string auth_key;
424 if (!isColumnNull(r, row, AUTH_KEY_COL)) {
425 getColumnValue(r, row, AUTH_KEY_COL, auth_key);
431 host.reset(
new Host(identifier_value, identifier_len,
432 identifier_type, dhcp4_subnet_id,
433 dhcp6_subnet_id, ipv4_reservation, hostname,
434 dhcp4_client_classes, dhcp6_client_classes,
435 dhcp4_next_server, dhcp4_server_hostname,
436 dhcp4_boot_file_name,
AuthKey(auth_key)));
439 if (!user_context.empty()) {
442 if (!ctx || (ctx->getType() != Element::map)) {
444 <<
"' is not a JSON map");
446 host->setContext(ctx);
449 <<
"' is invalid JSON: " << ex.
what());
453 host->setHostId(host_id);
476 class PgSqlHostWithOptionsExchange :
public PgSqlHostExchange {
480 static const size_t OPTION_COLUMNS = 7;
496 class OptionProcessor {
506 const size_t start_column)
507 : universe_(universe), start_column_(start_column),
508 option_id_index_(start_column), code_index_(start_column_ + 1),
509 value_index_(start_column_ + 2),
510 formatted_value_index_(start_column_ + 3),
511 space_index_(start_column_ + 4),
512 persistent_index_(start_column_ + 5),
513 user_context_index_(start_column_ + 6),
514 most_recent_option_id_(0) {
522 most_recent_option_id_ = 0;
561 if (PgSqlExchange::isColumnNull(r, row, option_id_index_)) {
567 PgSqlExchange::getColumnValue(r, row, option_id_index_, option_id);
572 if (most_recent_option_id_ >= option_id) {
578 most_recent_option_id_ = option_id;
582 PgSqlExchange::getColumnValue(r, row, code_index_, code);
587 if (!isColumnNull(r, row, value_index_)) {
588 PgSqlExchange::convertFromBytea(r, row, value_index_, value,
589 sizeof(value), value_len);
593 std::string formatted_value;
594 if (!isColumnNull(r, row, formatted_value_index_)) {
595 PgSqlExchange::getColumnValue(r, row, formatted_value_index_,
601 if (!isColumnNull(r, row, space_index_)) {
602 PgSqlExchange::getColumnValue(r, row, space_index_, space);
607 space = (universe_ == Option::V4 ?
613 PgSqlExchange::getColumnValue(r, row, persistent_index_,
617 std::string user_context;
618 if (!isColumnNull(r, row, user_context_index_)) {
619 PgSqlExchange::getColumnValue(r, row, user_context_index_,
638 uint32_t vendor_id = LibDHCP::optionSpaceToVendorId(space);
640 def = LibDHCP::getVendorOptionDef(universe_, vendor_id,
648 def = LibDHCP::getRuntimeOptionDef(space, code);
656 option.reset(
new Option(universe_, code, buf.begin(),
663 if (formatted_value.empty()) {
665 option = def->optionFactory(universe_, code, buf.begin(),
670 std::vector<std::string> split_vec;
671 boost::split(split_vec, formatted_value,
672 boost::is_any_of(
","));
673 option = def->optionFactory(universe_, code, split_vec);
680 if (!user_context.empty()) {
683 if (!ctx || (ctx->getType() != Element::map)) {
685 <<
"' is no a JSON map");
687 desc.setContext(ctx);
690 <<
"' is invalid JSON: " << ex.
what());
694 cfg->add(desc, space);
701 void setColumnNames(std::vector<std::string>& columns) {
702 columns[option_id_index_] =
"option_id";
703 columns[code_index_] =
"code";
704 columns[value_index_] =
"value";
705 columns[formatted_value_index_] =
"formatted_value";
706 columns[space_index_] =
"space";
707 columns[persistent_index_] =
"persistent";
708 columns[user_context_index_] =
"user_context";
716 size_t start_column_;
722 size_t option_id_index_;
732 size_t formatted_value_index_;
738 size_t persistent_index_;
742 size_t user_context_index_;
745 uint64_t most_recent_option_id_;
749 typedef boost::shared_ptr<OptionProcessor> OptionProcessorPtr;
759 enum FetchedOptions {
773 PgSqlHostWithOptionsExchange(
const FetchedOptions& fetched_options,
774 const size_t additional_columns_num = 0)
775 : PgSqlHostExchange(getRequiredColumnsNum(fetched_options)
776 + additional_columns_num),
777 opt_proc4_(), opt_proc6_() {
780 if ((fetched_options == DHCP4_ONLY) ||
781 (fetched_options == DHCP4_AND_DHCP6)) {
782 opt_proc4_.reset(
new OptionProcessor(Option::V4,
784 opt_proc4_->setColumnNames(columns_);
788 if ((fetched_options == DHCP6_ONLY) ||
789 (fetched_options == DHCP4_AND_DHCP6)) {
790 opt_proc6_.reset(
new OptionProcessor(Option::V6,
792 opt_proc6_->setColumnNames(columns_);
801 virtual void clear() {
802 PgSqlHostExchange::clear();
826 current_host = retrieveHost(r, row);
827 hosts.push_back(current_host);
832 HostID row_host_id = getHostId(r, row);
833 current_host = boost::const_pointer_cast<
Host>(hosts.back());
837 if (row_host_id > current_host->getHostId()) {
838 current_host = retrieveHost(r, row, row_host_id);
839 hosts.push_back(current_host);
846 opt_proc4_->retrieveOption(cfg, r, row);
852 opt_proc6_->retrieveOption(cfg, r, row);
869 static size_t getRequiredColumnsNum(
const FetchedOptions& fetched_options) {
870 return (fetched_options == DHCP4_AND_DHCP6 ? 2 * OPTION_COLUMNS :
877 OptionProcessorPtr opt_proc4_;
882 OptionProcessorPtr opt_proc6_;
897 class PgSqlHostIPv6Exchange :
public PgSqlHostWithOptionsExchange {
901 static const size_t RESERVATION_COLUMNS = 5;
909 PgSqlHostIPv6Exchange(
const FetchedOptions& fetched_options)
910 : PgSqlHostWithOptionsExchange(fetched_options, RESERVATION_COLUMNS),
911 reservation_id_index_(findAvailColumn()),
912 address_index_(reservation_id_index_ + 1),
913 prefix_len_index_(reservation_id_index_ + 2),
914 type_index_(reservation_id_index_ + 3),
915 iaid_index_(reservation_id_index_ + 4),
916 most_recent_reservation_id_(0) {
919 columns_[reservation_id_index_] =
"reservation_id";
920 columns_[address_index_] =
"address";
921 columns_[prefix_len_index_] =
"prefix_len";
922 columns_[type_index_] =
"type";
923 columns_[iaid_index_] =
"dhcp6_iaid";
925 BOOST_STATIC_ASSERT(4 < RESERVATION_COLUMNS);
934 PgSqlHostWithOptionsExchange::clear();
935 most_recent_reservation_id_ = 0;
941 uint64_t getReservationId(
const PgSqlResult& r,
int row)
const {
942 uint64_t resv_id = 0;
943 if (!isColumnNull(r, row, reservation_id_index_)) {
944 getColumnValue(r, row, reservation_id_index_, resv_id);
958 getColumnValue(r, row, type_index_, tmp);
964 resv_type = IPv6Resrv::TYPE_NA;
968 resv_type = IPv6Resrv::TYPE_PD;
973 "invalid IPv6 reservation type returned: "
974 << tmp <<
". Only 0 or 2 are allowed.");
982 getColumnValue(r, row, prefix_len_index_, prefix_len);
991 return (reservation);
1018 PgSqlHostWithOptionsExchange::processRowData(hosts, r, row);
1021 if (hosts.empty()) {
1023 " IPv6 reservation");
1028 uint64_t reservation_id = getReservationId(r, row);
1029 if (reservation_id && (reservation_id > most_recent_reservation_id_)) {
1030 HostPtr host = boost::const_pointer_cast<
Host>(hosts.back());
1032 most_recent_reservation_id_ = reservation_id;
1039 size_t reservation_id_index_;
1043 size_t address_index_;
1046 size_t prefix_len_index_;
1057 uint64_t most_recent_reservation_id_;
1074 static const size_t RESRV_COLUMNS = 6;
1081 PgSqlIPv6ReservationExchange()
1085 columns_[0] =
"host_id";
1086 columns_[1] =
"address";
1087 columns_[2] =
"prefix_len";
1088 columns_[3] =
"type";
1089 columns_[4] =
"dhcp6_iaid";
1090 BOOST_STATIC_ASSERT(5 < RESRV_COLUMNS);
1109 const bool unique_ip) {
1126 uint16_t type = resv.
getType() == IPv6Resrv::TYPE_NA ? 0 : 2;
1127 bind_array->add(type);
1131 bind_array->addNull();
1134 bind_array->add(host_id);
1143 }
catch (
const std::exception& ex) {
1145 "Could not create bind array from IPv6 Reservation: "
1146 << resv_.toText() <<
", reason: " << ex.
what());
1149 return (bind_array);
1163 static const int OPTION_ID_COL = 0;
1164 static const int CODE_COL = 1;
1165 static const int VALUE_COL = 2;
1166 static const int FORMATTED_VALUE_COL = 3;
1167 static const int SPACE_COL = 4;
1168 static const int PERSISTENT_COL = 5;
1169 static const int USER_CONTEXT_COL = 6;
1170 static const int DHCP_CLIENT_CLASS_COL = 7;
1171 static const int DHCP_SUBNET_ID_COL = 8;
1172 static const int HOST_ID_COL = 9;
1173 static const int SCOPE_ID_COL = 10;
1176 static const size_t OPTION_COLUMNS = 11;
1181 PgSqlOptionExchange()
1183 value_len_(0), option_() {
1184 columns_[OPTION_ID_COL] =
"option_id";
1185 columns_[CODE_COL] =
"code";
1186 columns_[VALUE_COL] =
"value";
1187 columns_[FORMATTED_VALUE_COL] =
"formatted_value";
1188 columns_[SPACE_COL] =
"space";
1189 columns_[PERSISTENT_COL] =
"persistent";
1190 columns_[USER_CONTEXT_COL] =
"user_context";
1191 columns_[DHCP_CLIENT_CLASS_COL] =
"dhcp_client_class";
1192 columns_[DHCP_SUBNET_ID_COL] =
"dhcp_subnet_id";
1193 columns_[HOST_ID_COL] =
"host_id";
1194 columns_[SCOPE_ID_COL] =
"scope_id";
1196 BOOST_STATIC_ASSERT(10 < OPTION_COLUMNS);
1208 const std::string& opt_space,
1221 bind_array->add(option_->getType());
1231 const char* buf_ptr =
static_cast<const char*
>(buf.getData());
1232 value_.assign(buf_ptr + opt_desc.
option_->getHeaderLen(),
1233 buf_ptr + buf.getLength());
1234 value_len_ = value_.size();
1235 bind_array->add(value_);
1239 bind_array->addNull(PsqlBindArray::BINARY_FMT);
1246 bind_array->addNull();
1250 if (!opt_space.empty()) {
1251 bind_array->addTempString(opt_space);
1253 bind_array->addNull();
1262 std::string user_context_ = ctx->str();
1263 bind_array->addTempString(user_context_);
1265 bind_array->addNull();
1272 bind_array->add(host_id);
1274 }
catch (
const std::exception& ex) {
1276 "Could not create bind array for inserting DHCP "
1277 "host option: " << option_->toText() <<
", reason: "
1281 return (bind_array);
1287 std::vector<uint8_t> value_;
1487 const bool return_last_id =
false);
1521 const std::string& opt_space,
1536 const uint64_t host_id);
1559 boost::shared_ptr<PgSqlHostExchange> exchange,
1583 const uint8_t* identifier_begin,
1584 const size_t identifier_len,
1586 boost::shared_ptr<PgSqlHostExchange> exchange)
const;
1607 std::pair<uint32_t, uint32_t> getVersion()
const;
1630 typedef boost::array<PgSqlTaggedStatement, PgSqlHostDataSourceImpl::NUM_STATEMENTS>
1631 TaggedStatementArray;
1635 TaggedStatementArray tagged_statements = { {
1644 "SELECT h.host_id, h.dhcp_identifier, h.dhcp_identifier_type, "
1645 " h.dhcp4_subnet_id, h.dhcp6_subnet_id, h.ipv4_address, "
1646 " h.hostname, h.dhcp4_client_classes, h.dhcp6_client_classes, "
1648 " h.dhcp4_next_server, h.dhcp4_server_hostname, "
1649 " h.dhcp4_boot_file_name, h.auth_key, "
1650 " o4.option_id, o4.code, o4.value, o4.formatted_value, o4.space, "
1651 " o4.persistent, o4.user_context, "
1652 " o6.option_id, o6.code, o6.value, o6.formatted_value, o6.space, "
1653 " o6.persistent, o6.user_context, "
1654 " r.reservation_id, r.address, r.prefix_len, r.type, r.dhcp6_iaid "
1656 "LEFT JOIN dhcp4_options AS o4 ON h.host_id = o4.host_id "
1657 "LEFT JOIN dhcp6_options AS o6 ON h.host_id = o6.host_id "
1658 "LEFT JOIN ipv6_reservations AS r ON h.host_id = r.host_id "
1659 "WHERE dhcp_identifier = $1 AND dhcp_identifier_type = $2 "
1660 "ORDER BY h.host_id, o4.option_id, o6.option_id, r.reservation_id"
1670 "SELECT h.host_id, h.dhcp_identifier, h.dhcp_identifier_type, "
1671 " h.dhcp4_subnet_id, h.dhcp6_subnet_id, h.ipv4_address, h.hostname, "
1672 " h.dhcp4_client_classes, h.dhcp6_client_classes, h.user_context, "
1673 " h.dhcp4_next_server, h.dhcp4_server_hostname, "
1674 " h.dhcp4_boot_file_name, h.auth_key, "
1675 " o.option_id, o.code, o.value, o.formatted_value, o.space, "
1676 " o.persistent, o.user_context "
1678 "LEFT JOIN dhcp4_options AS o ON h.host_id = o.host_id "
1679 "WHERE ipv4_address = $1 "
1680 "ORDER BY h.host_id, o.option_id"
1689 "get_host_subid4_dhcpid",
1690 "SELECT h.host_id, h.dhcp_identifier, h.dhcp_identifier_type, "
1691 " h.dhcp4_subnet_id, h.dhcp6_subnet_id, h.ipv4_address, h.hostname, "
1692 " h.dhcp4_client_classes, h.dhcp6_client_classes, h.user_context, "
1693 " h.dhcp4_next_server, h.dhcp4_server_hostname, "
1694 " h.dhcp4_boot_file_name, h.auth_key, "
1695 " o.option_id, o.code, o.value, o.formatted_value, o.space, "
1696 " o.persistent, o.user_context "
1698 "LEFT JOIN dhcp4_options AS o ON h.host_id = o.host_id "
1699 "WHERE h.dhcp4_subnet_id = $1 AND h.dhcp_identifier_type = $2 "
1700 " AND h.dhcp_identifier = $3 "
1701 "ORDER BY h.host_id, o.option_id"
1710 "get_host_subid6_dhcpid",
1711 "SELECT h.host_id, h.dhcp_identifier, "
1712 " h.dhcp_identifier_type, h.dhcp4_subnet_id, "
1713 " h.dhcp6_subnet_id, h.ipv4_address, h.hostname, "
1714 " h.dhcp4_client_classes, h.dhcp6_client_classes, h.user_context, "
1715 " h.dhcp4_next_server, h.dhcp4_server_hostname, "
1716 " h.dhcp4_boot_file_name, h.auth_key, "
1717 " o.option_id, o.code, o.value, o.formatted_value, o.space, "
1718 " o.persistent, o.user_context, "
1719 " r.reservation_id, r.address, r.prefix_len, r.type, r.dhcp6_iaid "
1721 "LEFT JOIN dhcp6_options AS o ON h.host_id = o.host_id "
1722 "LEFT JOIN ipv6_reservations AS r ON h.host_id = r.host_id "
1723 "WHERE h.dhcp6_subnet_id = $1 AND h.dhcp_identifier_type = $2 "
1724 " AND h.dhcp_identifier = $3 "
1725 "ORDER BY h.host_id, o.option_id, r.reservation_id"
1735 "get_host_subid_addr",
1736 "SELECT h.host_id, h.dhcp_identifier, h.dhcp_identifier_type, "
1737 " h.dhcp4_subnet_id, h.dhcp6_subnet_id, h.ipv4_address, h.hostname, "
1738 " h.dhcp4_client_classes, h.dhcp6_client_classes, h.user_context, "
1739 " h.dhcp4_next_server, h.dhcp4_server_hostname, "
1740 " h.dhcp4_boot_file_name, h.auth_key, "
1741 " o.option_id, o.code, o.value, o.formatted_value, o.space, "
1742 " o.persistent, o.user_context "
1744 "LEFT JOIN dhcp4_options AS o ON h.host_id = o.host_id "
1745 "WHERE h.dhcp4_subnet_id = $1 AND h.ipv4_address = $2 "
1746 "ORDER BY h.host_id, o.option_id"
1759 "SELECT h.host_id, h.dhcp_identifier, "
1760 " h.dhcp_identifier_type, h.dhcp4_subnet_id, "
1761 " h.dhcp6_subnet_id, h.ipv4_address, h.hostname, "
1762 " h.dhcp4_client_classes, h.dhcp6_client_classes, h.user_context, "
1763 " h.dhcp4_next_server, h.dhcp4_server_hostname, "
1764 " h.dhcp4_boot_file_name, h.auth_key, "
1765 " o.option_id, o.code, o.value, o.formatted_value, o.space, "
1766 " o.persistent, o.user_context, "
1767 " r.reservation_id, r.address, r.prefix_len, r.type, "
1770 "LEFT JOIN dhcp6_options AS o ON h.host_id = o.host_id "
1771 "LEFT JOIN ipv6_reservations AS r ON h.host_id = r.host_id "
1772 "WHERE h.host_id = "
1773 " (SELECT host_id FROM ipv6_reservations "
1774 " WHERE address = $1 AND prefix_len = $2) "
1775 "ORDER BY h.host_id, o.option_id, r.reservation_id"
1787 "get_host_subid6_addr",
1788 "SELECT h.host_id, h.dhcp_identifier, "
1789 " h.dhcp_identifier_type, h.dhcp4_subnet_id, "
1790 " h.dhcp6_subnet_id, h.ipv4_address, h.hostname, "
1791 " h.dhcp4_client_classes, h.dhcp6_client_classes, h.user_context, "
1792 " h.dhcp4_next_server, h.dhcp4_server_hostname, "
1793 " h.dhcp4_boot_file_name, h.auth_key, "
1794 " o.option_id, o.code, o.value, o.formatted_value, o.space, "
1795 " o.persistent, o.user_context, "
1796 " r.reservation_id, r.address, r.prefix_len, r.type, "
1799 "LEFT JOIN dhcp6_options AS o ON h.host_id = o.host_id "
1800 "LEFT JOIN ipv6_reservations AS r ON h.host_id = r.host_id "
1801 "WHERE h.dhcp6_subnet_id = $1 AND r.address = $2 "
1802 "ORDER BY h.host_id, o.option_id, r.reservation_id"
1814 "SELECT h.host_id, h.dhcp_identifier, h.dhcp_identifier_type, "
1815 " h.dhcp4_subnet_id, h.dhcp6_subnet_id, h.ipv4_address, h.hostname, "
1816 " h.dhcp4_client_classes, h.dhcp6_client_classes, h.user_context, "
1817 " h.dhcp4_next_server, h.dhcp4_server_hostname, "
1818 " h.dhcp4_boot_file_name, h.auth_key, "
1819 " o.option_id, o.code, o.value, o.formatted_value, o.space, "
1820 " o.persistent, o.user_context "
1822 "LEFT JOIN dhcp4_options AS o ON h.host_id = o.host_id "
1823 "WHERE h.dhcp4_subnet_id = $1 "
1824 "ORDER BY h.host_id, o.option_id"
1840 "SELECT h.host_id, h.dhcp_identifier, "
1841 " h.dhcp_identifier_type, h.dhcp4_subnet_id, "
1842 " h.dhcp6_subnet_id, h.ipv4_address, h.hostname, "
1843 " h.dhcp4_client_classes, h.dhcp6_client_classes, h.user_context, "
1844 " h.dhcp4_next_server, h.dhcp4_server_hostname, "
1845 " h.dhcp4_boot_file_name, h.auth_key, "
1846 " o.option_id, o.code, o.value, o.formatted_value, o.space, "
1847 " o.persistent, o.user_context, "
1848 " r.reservation_id, r.address, r.prefix_len, r.type, r.dhcp6_iaid "
1850 "LEFT JOIN dhcp6_options AS o ON h.host_id = o.host_id "
1851 "LEFT JOIN ipv6_reservations AS r ON h.host_id = r.host_id "
1852 "WHERE h.dhcp6_subnet_id = $1 "
1853 "ORDER BY h.host_id, o.option_id, r.reservation_id"
1864 "get_host_hostname",
1865 "SELECT h.host_id, h.dhcp_identifier, h.dhcp_identifier_type, "
1866 " h.dhcp4_subnet_id, h.dhcp6_subnet_id, h.ipv4_address, "
1867 " h.hostname, h.dhcp4_client_classes, h.dhcp6_client_classes, "
1869 " h.dhcp4_next_server, h.dhcp4_server_hostname, "
1870 " h.dhcp4_boot_file_name, h.auth_key, "
1871 " o4.option_id, o4.code, o4.value, o4.formatted_value, o4.space, "
1872 " o4.persistent, o4.user_context, "
1873 " o6.option_id, o6.code, o6.value, o6.formatted_value, o6.space, "
1874 " o6.persistent, o6.user_context, "
1875 " r.reservation_id, r.address, r.prefix_len, r.type, r.dhcp6_iaid "
1877 "LEFT JOIN dhcp4_options AS o4 ON h.host_id = o4.host_id "
1878 "LEFT JOIN dhcp6_options AS o6 ON h.host_id = o6.host_id "
1879 "LEFT JOIN ipv6_reservations AS r ON h.host_id = r.host_id "
1880 "WHERE lower(h.hostname) = $1 "
1881 "ORDER BY h.host_id, o4.option_id, o6.option_id, r.reservation_id"
1891 "get_host_hostname_subid4",
1892 "SELECT h.host_id, h.dhcp_identifier, h.dhcp_identifier_type, "
1893 " h.dhcp4_subnet_id, h.dhcp6_subnet_id, h.ipv4_address, h.hostname, "
1894 " h.dhcp4_client_classes, h.dhcp6_client_classes, h.user_context, "
1895 " h.dhcp4_next_server, h.dhcp4_server_hostname, "
1896 " h.dhcp4_boot_file_name, h.auth_key, "
1897 " o.option_id, o.code, o.value, o.formatted_value, o.space, "
1898 " o.persistent, o.user_context "
1900 "LEFT JOIN dhcp4_options AS o ON h.host_id = o.host_id "
1901 "WHERE lower(h.hostname) = $1 AND h.dhcp4_subnet_id = $2 "
1902 "ORDER BY h.host_id, o.option_id"
1914 "get_host_hostname_subid6",
1915 "SELECT h.host_id, h.dhcp_identifier, "
1916 " h.dhcp_identifier_type, h.dhcp4_subnet_id, "
1917 " h.dhcp6_subnet_id, h.ipv4_address, h.hostname, "
1918 " h.dhcp4_client_classes, h.dhcp6_client_classes, h.user_context, "
1919 " h.dhcp4_next_server, h.dhcp4_server_hostname, "
1920 " h.dhcp4_boot_file_name, h.auth_key, "
1921 " o.option_id, o.code, o.value, o.formatted_value, o.space, "
1922 " o.persistent, o.user_context, "
1923 " r.reservation_id, r.address, r.prefix_len, r.type, r.dhcp6_iaid "
1925 "LEFT JOIN dhcp6_options AS o ON h.host_id = o.host_id "
1926 "LEFT JOIN ipv6_reservations AS r ON h.host_id = r.host_id "
1927 "WHERE lower(h.hostname) = $1 AND h.dhcp6_subnet_id = $2 "
1928 "ORDER BY h.host_id, o.option_id, r.reservation_id"
1938 "get_host_subid4_page",
1939 "SELECT h.host_id, h.dhcp_identifier, h.dhcp_identifier_type, "
1940 " h.dhcp4_subnet_id, h.dhcp6_subnet_id, h.ipv4_address, h.hostname, "
1941 " h.dhcp4_client_classes, h.dhcp6_client_classes, h.user_context, "
1942 " h.dhcp4_next_server, h.dhcp4_server_hostname, "
1943 " h.dhcp4_boot_file_name, h.auth_key, "
1944 " o.option_id, o.code, o.value, o.formatted_value, o.space, "
1945 " o.persistent, o.user_context "
1946 "FROM ( SELECT * FROM hosts AS h "
1947 " WHERE h.dhcp4_subnet_id = $1 AND h.host_id > $2 "
1948 " ORDER BY h.host_id "
1950 "LEFT JOIN dhcp4_options AS o ON h.host_id = o.host_id "
1951 "ORDER BY h.host_id, o.option_id"
1963 "get_host_subid6_page",
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.user_context, "
1972 " r.reservation_id, r.address, r.prefix_len, r.type, r.dhcp6_iaid "
1973 "FROM ( SELECT * FROM hosts AS h "
1974 " WHERE h.dhcp6_subnet_id = $1 AND h.host_id > $2 "
1975 " ORDER BY h.host_id "
1977 "LEFT JOIN dhcp6_options AS o ON h.host_id = o.host_id "
1978 "LEFT JOIN ipv6_reservations AS r ON h.host_id = r.host_id "
1979 "ORDER BY h.host_id, o.option_id, r.reservation_id"
1990 "SELECT h.host_id, h.dhcp_identifier, h.dhcp_identifier_type, "
1991 " h.dhcp4_subnet_id, h.dhcp6_subnet_id, h.ipv4_address, h.hostname, "
1992 " h.dhcp4_client_classes, h.dhcp6_client_classes, h.user_context, "
1993 " h.dhcp4_next_server, h.dhcp4_server_hostname, "
1994 " h.dhcp4_boot_file_name, h.auth_key, "
1995 " o.option_id, o.code, o.value, o.formatted_value, o.space, "
1996 " o.persistent, o.user_context "
1997 "FROM ( SELECT * FROM hosts AS h "
1998 " WHERE h.host_id > $1 "
1999 " ORDER BY h.host_id "
2001 "LEFT JOIN dhcp4_options AS o ON h.host_id = o.host_id "
2002 "ORDER BY h.host_id, o.option_id"
2015 "SELECT h.host_id, h.dhcp_identifier, "
2016 " h.dhcp_identifier_type, h.dhcp4_subnet_id, "
2017 " h.dhcp6_subnet_id, h.ipv4_address, h.hostname, "
2018 " h.dhcp4_client_classes, h.dhcp6_client_classes, h.user_context, "
2019 " h.dhcp4_next_server, h.dhcp4_server_hostname, "
2020 " h.dhcp4_boot_file_name, h.auth_key, "
2021 " o.option_id, o.code, o.value, o.formatted_value, o.space, "
2022 " o.persistent, o.user_context, "
2023 " r.reservation_id, r.address, r.prefix_len, r.type, r.dhcp6_iaid "
2024 "FROM ( SELECT * FROM hosts AS h "
2025 " WHERE h.host_id > $1 "
2026 " ORDER BY h.host_id "
2028 "LEFT JOIN dhcp6_options AS o ON h.host_id = o.host_id "
2029 "LEFT JOIN ipv6_reservations AS r ON h.host_id = r.host_id "
2030 "ORDER BY h.host_id, o.option_id, r.reservation_id"
2041 "insert_host_non_unique_ip",
2042 "INSERT INTO hosts(dhcp_identifier, dhcp_identifier_type, "
2043 " dhcp4_subnet_id, dhcp6_subnet_id, ipv4_address, hostname, "
2044 " dhcp4_client_classes, dhcp6_client_classes, user_context, "
2045 " dhcp4_next_server, dhcp4_server_hostname, dhcp4_boot_file_name, auth_key)"
2046 "VALUES ( $1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13 ) "
2067 "insert_host_unique_ip",
2068 "INSERT INTO hosts(dhcp_identifier, dhcp_identifier_type, "
2069 " dhcp4_subnet_id, dhcp6_subnet_id, ipv4_address, hostname, "
2070 " dhcp4_client_classes, dhcp6_client_classes, user_context, "
2071 " dhcp4_next_server, dhcp4_server_hostname, dhcp4_boot_file_name, auth_key)"
2072 " SELECT $1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13"
2073 " WHERE NOT EXISTS ("
2074 " SELECT 1 FROM hosts WHERE ipv4_address = $14 AND dhcp4_subnet_id = $15"
2085 "insert_v6_resrv_non_unique",
2086 "INSERT INTO ipv6_reservations(address, prefix_len, type, "
2087 " dhcp6_iaid, host_id) "
2088 "VALUES ($1, $2, $3, $4, $5)"
2096 "insert_v6_resrv_unique",
2097 "INSERT INTO ipv6_reservations(address, prefix_len, type, "
2098 " dhcp6_iaid, host_id) "
2099 "SELECT $1, $2, $3, $4, $5 "
2100 " WHERE NOT EXISTS ("
2101 " SELECT 1 FROM ipv6_reservations"
2102 " WHERE address = $6 AND prefix_len = $7"
2113 "insert_v4_host_option",
2114 "INSERT INTO dhcp4_options(code, value, formatted_value, space, "
2115 " persistent, user_context, host_id, scope_id) "
2116 "VALUES ($1, $2, $3, $4, $5, $6, $7, 3)"
2125 "insert_v6_host_option",
2126 "INSERT INTO dhcp6_options(code, value, formatted_value, space, "
2127 " persistent, user_context, host_id, scope_id) "
2128 "VALUES ($1, $2, $3, $4, $5, $6, $7, 3)"
2136 "DELETE FROM hosts WHERE dhcp4_subnet_id = $1 AND ipv4_address = $2"
2144 "DELETE FROM hosts USING ipv6_reservations "
2145 " WHERE dhcp6_subnet_id = $1 AND ipv6_reservations.address = $2"
2152 "del_host_subid4_id",
2153 "DELETE FROM hosts WHERE dhcp4_subnet_id = $1 "
2154 "AND dhcp_identifier_type = $2 "
2155 "AND dhcp_identifier = $3"
2162 "del_host_subid6_id",
2163 "DELETE FROM hosts WHERE dhcp6_subnet_id = $1 "
2164 "AND dhcp_identifier_type = $2 "
2165 "AND dhcp_identifier = $3"
2177 : conn_(parameters, io_service_accessor, db_reconnect_callback),
2178 is_readonly_(true) {
2186 if (MultiThreadingMgr::instance().getMode()) {
2190 lock_guard<mutex> lock(mgr_.
pool_->mutex_);
2191 if (!mgr_.
pool_->pool_.empty()) {
2193 mgr_.
pool_->pool_.pop_back();
2201 if (mgr_.
pool_->pool_.empty()) {
2209 if (MultiThreadingMgr::instance().getMode()) {
2211 lock_guard<mutex> lock(mgr_.pool_->mutex_);
2212 mgr_.pool_->pool_.push_back(ctx_);
2213 if (ctx_->conn_.isUnusable()) {
2214 mgr_.unusable_ =
true;
2216 }
else if (ctx_->conn_.isUnusable()) {
2217 mgr_.unusable_ =
true;
2222 : parameters_(parameters), ip_reservations_unique_(true), unusable_(false),
2227 timer_name_ += boost::lexical_cast<std::string>(
reinterpret_cast<uint64_t
>(
this));
2233 std::pair<uint32_t, uint32_t> db_version =
getVersion();
2234 if (code_version != db_version) {
2236 "PostgreSQL schema version mismatch: need version: "
2237 << code_version.first <<
"." << code_version.second
2238 <<
" found version: " << db_version.first <<
"."
2239 << db_version.second);
2256 ctx->conn_.openDatabase();
2259 ctx->conn_.prepareStatements(tagged_statements.begin(),
2264 ctx->is_readonly_ = ctx->conn_.configuredReadOnly();
2268 if (!ctx->is_readonly_) {
2270 tagged_statements.end());
2275 ctx->host_ipv4_exchange_.reset(
new PgSqlHostWithOptionsExchange(PgSqlHostWithOptionsExchange::DHCP4_ONLY));
2276 ctx->host_ipv6_exchange_.reset(
new PgSqlHostIPv6Exchange(PgSqlHostWithOptionsExchange::DHCP6_ONLY));
2277 ctx->host_ipv46_exchange_.reset(
new PgSqlHostIPv6Exchange(PgSqlHostWithOptionsExchange::DHCP4_AND_DHCP6));
2278 ctx->host_ipv6_reservation_exchange_.reset(
new PgSqlIPv6ReservationExchange());
2279 ctx->host_option_exchange_.reset(
new PgSqlOptionExchange());
2299 bool reopened =
false;
2301 const std::string timer_name = db_reconnect_ctl->timerName();
2306 std::list<std::string> host_db_access_list = cfg_db->getHostDbAccessStringList();
2307 for (std::string& hds : host_db_access_list) {
2314 }
catch (
const std::exception& ex) {
2330 if (!db_reconnect_ctl->checkRetries()) {
2333 .arg(db_reconnect_ctl->maxRetries());
2346 .arg(db_reconnect_ctl->maxRetries() - db_reconnect_ctl->retriesLeft() + 1)
2347 .arg(db_reconnect_ctl->maxRetries())
2348 .arg(db_reconnect_ctl->retryInterval());
2354 db_reconnect_ctl->retryInterval(),
2367 const bool return_last_id) {
2368 uint64_t last_id = 0;
2369 PgSqlResult r(PQexecPrepared(ctx->conn_, tagged_statements[stindex].name,
2370 tagged_statements[stindex].nbparams,
2371 &bind_array->values_[0],
2372 &bind_array->lengths_[0],
2373 &bind_array->formats_[0], 0));
2375 int s = PQresultStatus(r);
2377 if (s != PGRES_COMMAND_OK) {
2385 ctx->conn_.checkStatementError(r, tagged_statements[stindex]);
2389 char* rows_affected = PQcmdTuples(r);
2390 if (!rows_affected) {
2392 "Could not retrieve the number of affected rows.");
2400 if (rows_affected[0] ==
'0') {
2404 if (return_last_id) {
2415 PgSqlResult r(PQexecPrepared(ctx->conn_, tagged_statements[stindex].name,
2416 tagged_statements[stindex].nbparams,
2417 &bind_array->values_[0],
2418 &bind_array->lengths_[0],
2419 &bind_array->formats_[0], 0));
2421 int s = PQresultStatus(r);
2423 if (s != PGRES_COMMAND_OK) {
2426 ctx->conn_.checkStatementError(r, tagged_statements[stindex]);
2431 char* rows_deleted = PQcmdTuples(r);
2432 if (!rows_deleted) {
2434 "Could not retrieve the number of deleted rows.");
2436 return (rows_deleted[0] !=
'0');
2455 const std::string& opt_space,
2458 PsqlBindArrayPtr bind_array = ctx->host_option_exchange_->createBindForSend(opt_desc, opt_space,
id);
2467 const uint64_t host_id) {
2470 std::list<std::string> option_spaces = options_cfg->getOptionSpaceNames();
2471 std::list<std::string> vendor_spaces = options_cfg->getVendorIdsSpaceNames();
2472 option_spaces.insert(option_spaces.end(), vendor_spaces.begin(),
2473 vendor_spaces.end());
2477 for (
auto space = option_spaces.begin(); space != option_spaces.end(); ++space) {
2479 if (options && !options->empty()) {
2480 for (
auto opt = options->begin(); opt != options->end(); ++opt) {
2491 boost::shared_ptr<PgSqlHostExchange> exchange,
2493 bool single)
const {
2496 PgSqlResult r(PQexecPrepared(ctx->conn_, tagged_statements[stindex].name,
2497 tagged_statements[stindex].nbparams,
2498 &bind_array->values_[0],
2499 &bind_array->lengths_[0],
2500 &bind_array->formats_[0], 0));
2502 ctx->conn_.checkStatementError(r, tagged_statements[stindex]);
2505 for (
int row = 0; row < rows; ++row) {
2506 exchange->processRowData(result, r, row);
2508 if (single && result.size() > 1) {
2510 "database where only one was expected for query "
2511 << tagged_statements[stindex].name);
2520 const uint8_t* identifier_begin,
2521 const size_t identifier_len,
2523 boost::shared_ptr<PgSqlHostExchange> exchange)
const {
2529 bind_array->add(subnet_id);
2532 bind_array->add(static_cast<uint8_t>(identifier_type));
2535 bind_array->add(identifier_begin, identifier_len);
2542 if (!collection.empty()) {
2543 result = *collection.begin();
2549 std::pair<uint32_t, uint32_t>
2558 if (ctx->is_readonly_) {
2560 " to operate in read only mode");
2575 return impl_->parameters_;
2585 impl_->checkReadOnly(ctx);
2597 bool unique_ip = impl_->ip_reservations_unique_ && !host->getIPv4Reservation().isV4Zero()
2598 && host->getIPv4SubnetID() != SUBNET_ID_UNUSED;
2601 PsqlBindArrayPtr bind_array = ctx->host_ipv4_exchange_->createBindForSend(host, unique_ip);
2604 uint32_t host_id = impl_->addStatement(ctx,
2613 cfg_option4, host_id);
2620 cfg_option6, host_id);
2625 if (std::distance(v6resv.first, v6resv.second) > 0) {
2628 impl_->addResv(ctx, resv->second, host_id);
2644 impl_->checkReadOnly(ctx);
2647 bind_array->add(subnet_id);
2651 bind_array->add(addr);
2657 bind_array->add(addr.
toText());
2666 const uint8_t* identifier_begin,
2667 const size_t identifier_len) {
2673 impl_->checkReadOnly(ctx);
2678 bind_array->add(subnet_id);
2681 bind_array->add(static_cast<uint8_t>(identifier_type));
2684 bind_array->add(identifier_begin, identifier_len);
2693 const uint8_t* identifier_begin,
2694 const size_t identifier_len) {
2700 impl_->checkReadOnly(ctx);
2705 bind_array->add(subnet_id);
2708 bind_array->add(static_cast<uint8_t>(identifier_type));
2711 bind_array->add(identifier_begin, identifier_len);
2719 const uint8_t* identifier_begin,
2720 const size_t identifier_len)
const {
2729 bind_array->add(identifier_begin, identifier_len);
2732 bind_array->add(static_cast<uint8_t>(identifier_type));
2736 bind_array, ctx->host_ipv46_exchange_, result,
false);
2751 bind_array->add(subnet_id);
2755 bind_array, ctx->host_ipv4_exchange_, result,
false);
2770 bind_array->add(subnet_id);
2774 bind_array, ctx->host_ipv6_exchange_, result,
false);
2789 bind_array->add(hostname);
2793 bind_array, ctx->host_ipv46_exchange_, result,
false);
2809 bind_array->add(hostname);
2812 bind_array->add(subnet_id);
2816 bind_array, ctx->host_ipv4_exchange_, result,
false);
2832 bind_array->add(hostname);
2835 bind_array->add(subnet_id);
2839 bind_array, ctx->host_ipv6_exchange_, result,
false);
2847 uint64_t lower_host_id,
2857 bind_array->add(subnet_id);
2860 bind_array->add(lower_host_id);
2863 string page_size_data =
2864 boost::lexical_cast<std::string>(page_size.
page_size_);
2865 bind_array->add(page_size_data);
2869 bind_array, ctx->host_ipv4_exchange_, result,
false);
2877 uint64_t lower_host_id,
2887 bind_array->add(subnet_id);
2890 bind_array->add(lower_host_id);
2893 string page_size_data =
2894 boost::lexical_cast<std::string>(page_size.
page_size_);
2895 bind_array->add(page_size_data);
2899 bind_array, ctx->host_ipv6_exchange_, result,
false);
2906 uint64_t lower_host_id,
2916 bind_array->add(lower_host_id);
2919 string page_size_data =
2920 boost::lexical_cast<std::string>(page_size.
page_size_);
2921 bind_array->add(page_size_data);
2925 bind_array, ctx->host_ipv4_exchange_, result,
false);
2932 uint64_t lower_host_id,
2942 bind_array->add(lower_host_id);
2945 string page_size_data =
2946 boost::lexical_cast<std::string>(page_size.
page_size_);
2947 bind_array->add(page_size_data);
2951 bind_array, ctx->host_ipv6_exchange_, result,
false);
2966 bind_array->add(address);
2970 bind_array, ctx->host_ipv4_exchange_, result,
false);
2978 const uint8_t* identifier_begin,
2979 const size_t identifier_len)
const {
2984 return (impl_->getHost(ctx, subnet_id, identifier_type, identifier_begin, identifier_len,
2986 ctx->host_ipv4_exchange_));
2996 if (!address.
isV4()) {
2998 " wrong address type, address supplied is an IPv6 address");
3005 bind_array->add(subnet_id);
3008 bind_array->add(address);
3012 bind_array, ctx->host_ipv4_exchange_, collection,
true);
3016 if (!collection.empty()) {
3017 result = *collection.begin();
3030 if (!address.
isV4()) {
3032 " wrong address type, address supplied is an IPv6 address");
3039 bind_array->add(subnet_id);
3042 bind_array->add(address);
3046 bind_array, ctx->host_ipv4_exchange_, collection,
false);
3047 return (collection);
3053 const uint8_t* identifier_begin,
3054 const size_t identifier_len)
const {
3059 return (impl_->getHost(ctx, subnet_id, identifier_type, identifier_begin, identifier_len,
3061 ctx->host_ipv6_exchange_));
3066 const uint8_t prefix_len)
const {
3067 if (!prefix.
isV6()) {
3069 "wrong address type, address supplied is an IPv4 address");
3080 bind_array->add(prefix);
3083 bind_array->add(prefix_len);
3087 bind_array, ctx->host_ipv6_exchange_, collection,
true);
3091 if (!collection.empty()) {
3092 result = *collection.begin();
3101 if (!address.
isV6()) {
3103 "wrong address type, address supplied is an IPv4 address");
3114 bind_array->add(subnet_id);
3117 bind_array->add(address);
3121 bind_array, ctx->host_ipv6_exchange_, collection,
true);
3125 if (!collection.empty()) {
3126 result = *collection.begin();
3135 if (!address.
isV6()) {
3137 "wrong address type, address supplied is an IPv4 address");
3148 bind_array->add(subnet_id);
3151 bind_array->add(address);
3155 bind_array, ctx->host_ipv6_exchange_, collection,
false);
3156 return (collection);
3164 std::string name =
"";
3170 name = ctx->conn_.getParameter(
"name");
3179 return (std::string(
"Host data source that stores host information"
3180 "in PostgreSQL database"));
3183 std::pair<uint32_t, uint32_t>
3185 return(impl_->getVersion());
3195 impl_->checkReadOnly(ctx);
3196 ctx->conn_.commit();
3206 impl_->checkReadOnly(ctx);
3207 ctx->conn_.rollback();
3212 impl_->ip_reservations_unique_ = unique;
3218 return (impl_->unusable_);
std::string timer_name_
Timer name used to register database reconnect timer.
RAII class creating a critical section.
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.
RAII wrapper for PostgreSQL Result sets.
const isc::log::MessageID DHCPSRV_PGSQL_HOST_DB_RECONNECT_ATTEMPT_FAILED
virtual ConstHostCollection getAllbyHostname4(const std::string &hostname, const SubnetID &subnet_id) const
Return all hosts with a hostname in a DHCPv4 subnet.
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...
Wraps value holding size of the page with host reservations.
boost::shared_ptr< CfgOption > CfgOptionPtr
Non-const pointer.
void commit()
Commits transaction.
virtual void add(const HostPtr &host)
Adds a new host to the collection.
#define LOG_INFO(LOGGER, MESSAGE)
Macro to conveniently test info output and log it.
std::pair< uint32_t, uint32_t > getVersion() const
Returns PostgreSQL schema version of the open database.
void addResv(PgSqlHostContextPtr &ctx, const IPv6Resrv &resv, const HostID &id)
Inserts IPv6 Reservation into ipv6_reservation table.
A standard Data module exception that is thrown if a parse error is encountered when constructing an ...
virtual ConstHostCollection getAll4(const SubnetID &subnet_id) const
Return all hosts in a DHCPv4 subnet.
boost::shared_ptr< Host > HostPtr
Pointer to the Host object.
StatementIndex
Statement Tags.
const asiolink::IOAddress & getPrefix() const
Returns prefix for the reservation.
boost::shared_ptr< const CfgOption > ConstCfgOptionPtr
Const pointer.
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...
static CfgMgr & instance()
returns a single instance of Configuration Manager
~PgSqlHostContextAlloc()
Destructor.
const size_t page_size_
Holds page size.
static bool dbReconnect(ReconnectCtlPtr db_reconnect_ctl)
Attempts to reconnect the server to the host DB backend manager.
static isc::asiolink::IOServicePtr & getIOService()
Returns pointer to the IO service.
boost::shared_ptr< Option > OptionPtr
const isc::log::MessageID DHCPSRV_PGSQL_HOST_DB_RECONNECT_FAILED
static const char DUPLICATE_KEY[]
Define the PgSql error state for a duplicate key error.
PgSqlConnection conn_
PostgreSQL connection.
Universe
defines option universe DHCPv4 or DHCPv6
#define LOG_ERROR(LOGGER, MESSAGE)
Macro to conveniently test error output and log it.
const isc::log::MessageID DHCPSRV_PGSQL_HOST_DB_GET_VERSION
SrvConfigPtr getCurrentCfg()
Returns a pointer to the current configuration.
virtual bool setIPReservationsUnique(const bool unique)
Controls whether IP reservations are unique or non-unique.
PgSqlHostDataSource(const db::DatabaseConnection::ParameterMap ¶meters)
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)
Base class for marshalling data to and from PostgreSQL.
static ParameterMap parse(const std::string &dbaccess)
Parse database access string.
IPv6 reservation for a host.
int getRows() const
Returns the number of rows in the result set.
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 ...
virtual ConstHostCollection getAll6(const SubnetID &subnet_id) const
Return all hosts in a DHCPv6 subnet.
bool isV4() const
Convenience function to check for an IPv4 address.
std::vector< uint8_t > OptionBuffer
buffer types used in DHCP code.
Exception thrown on failure to open database.
PgSqlHostDataSourceImpl(const DatabaseConnection::ParameterMap ¶meters)
Constructor.
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.
Multiple lease records found where one expected.
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
void addOptions(PgSqlHostContextPtr &ctx, const StatementIndex &stindex, const ConstCfgOptionPtr &options_cfg, const uint64_t host_id)
Inserts multiple options into the database.
A generic exception that is thrown if a parameter given to a method is considered invalid in that con...
std::string formatted_value_
Option value in textual (CSV) format.
~PgSqlHostDataSourceImpl()
Destructor.
std::pair< IPv6ResrvIterator, IPv6ResrvIterator > IPv6ResrvRange
const uint32_t PG_SCHEMA_VERSION_MINOR
boost::shared_ptr< PsqlBindArray > PsqlBindArrayPtr
Defines a smart pointer to PsqlBindArray.
static bool delBackend(const std::string &db_type)
Delete an alternate host backend (aka host data source).
const int DHCPSRV_DBG_TRACE_DETAIL
Additional information.
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.
boost::shared_ptr< CfgDbAccess > CfgDbAccessPtr
A pointer to the CfgDbAccess.
std::vector< ConstHostPtr > ConstHostCollection
Collection of the const Host objects.
void checkReadOnly(PgSqlHostContextPtr &ctx) const
Throws exception if database is read only.
virtual std::string getName() const
Returns the name of the open database.
bool persistent_
Persistence flag.
virtual ConstHostCollection getAllbyHostname(const std::string &hostname) const
Return all hosts with a hostname.
A generic exception that is thrown when an unexpected error condition occurs.
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.
PgSqlHostContextPtr ctx_
The context.
virtual std::pair< uint32_t, uint32_t > getVersion() const
Returns backend version.
boost::shared_ptr< const Element > ConstElementPtr
boost::shared_ptr< const Host > ConstHostPtr
Const pointer to the Host object.
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)
static void getColumnValue(const PgSqlResult &r, const int row, const size_t col, std::string &value)
Fetches text column value as a string.
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.
data::ConstElementPtr getContext() const
Returns const pointer to the user context.
Common PgSql Connector Pool.
virtual ~PgSqlHostDataSource()
Virtual destructor.
bool ip_reservations_unique_
Holds the setting whether the IP reservations must be unique or may be non-unique.
Represents a device with IPv4 and/or IPv6 reservations.
Type
Type of the reservation.
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.
boost::shared_ptr< PgSqlHostContext > PgSqlHostContextPtr
Type of pointers to contexts.
IPv6ResrvCollection::const_iterator IPv6ResrvIterator
virtual const char * what() const
Returns a C-style character string of the cause of the exception.
The OutputBuffer class is a buffer abstraction for manipulating mutable data.
std::vector< PgSqlHostContextPtr > pool_
The vector of available contexts.
OptionPtr option_
Option instance.
bool delStatement(PgSqlHostContextPtr &ctx, PgSqlHostDataSourceImpl::StatementIndex stindex, PsqlBindArrayPtr &bind)
Executes statements that delete records.
const isc::log::MessageID DHCPSRV_PGSQL_HOST_DB_RECONNECT_ATTEMPT_SCHEDULE
static bool invokeDbFailedCallback(const ReconnectCtlPtr &db_reconnect_ctl)
Invokes the connection's restore failed connectivity callback.
This is a base class for exceptions thrown from the DNS library module.
Defines the logger used by the top-level component of kea-dhcp-ddns.
std::function< bool(ReconnectCtlPtr db_reconnect_ctl)> DbCallback
Defines a callback prototype for propagating events upward.
virtual std::string getDescription() const
Returns description of the backend.
PgSqlHostContextPoolPtr pool_
The pool of contexts.
boost::shared_ptr< PgSqlHostContextPool > PgSqlHostContextPoolPtr
Type of pointers to context pools.
static bool invokeDbRecoveredCallback(const ReconnectCtlPtr &db_reconnect_ctl)
Invokes the connection's restored connectivity callback.
Implementation of the PgSqlHostDataSource.
bool is_readonly_
Indicates if the database is opened in read only mode.
PgSqlHostContextAlloc(PgSqlHostDataSourceImpl &mgr)
Constructor.
virtual bool isUnusable()
Flag which indicates if the host manager has at least one unusable connection.
void addReservation(const IPv6Resrv &reservation)
Adds new IPv6 reservation.
A wrapper interface for the ASIO library.
Type getType() const
Returns reservation type.
static bool invokeDbLostCallback(const ReconnectCtlPtr &db_reconnect_ctl)
Invokes the connection's lost connectivity callback.
#define DHCP6_OPTION_SPACE
virtual void rollback()
Rollback Transactions.
#define DHCP4_OPTION_SPACE
global std option spaces
static void addBackend(const std::string &access)
Add an alternate host backend (aka host data source).
boost::shared_ptr< OptionContainer > OptionContainerPtr
Pointer to the OptionContainer object.
#define LOG_DEBUG(LOGGER, LEVEL, MESSAGE)
Macro to conveniently test debug output and log it.
std::string toText() const
Convert the address to a string.
virtual isc::db::DatabaseConnection::ParameterMap getParameters() const
Return backend parameters.
Attempt to modify data in read-only database.
DatabaseConnection::ParameterMap parameters_
The parameters.
boost::shared_ptr< OptionDefinition > OptionDefinitionPtr
Pointer to option definition object.
bool isV6() const
Convenience function to check for an IPv6 address.
isc::log::Logger dhcpsrv_logger("dhcpsrv")
DHCP server library Logger.
static std::pair< uint32_t, uint32_t > getVersion(const ParameterMap ¶meters)
Get the schema version.
virtual void commit()
Commit Transactions.
PgSqlHostContextPtr createContext() const
Create a new context.
IdentifierType
Type of the host identifier.
std::function< isc::asiolink::IOServicePtr()> IOServiceAccessor
Function which returns the IOService that can be used to recover the connection.
std::map< std::string, std::string > ParameterMap
Database configuration parameter map.
uint8_t getPrefixLen() const
Returns prefix length.
static const TimerMgrPtr & instance()
Returns pointer to the sole instance of the TimerMgr.
The IOAddress class represents an IP addresses (version agnostic)
boost::shared_ptr< PgSqlHostIPv6Exchange > host_ipv6_exchange_
Pointer to an object representing an exchange which can be used to retrieve hosts, DHCPv6 options and IPv6 reservations.
boost::shared_ptr< PgSqlHostIPv6Exchange > host_ipv46_exchange_
Pointer to an object representing an exchange which can be used to retrieve hosts, DHCPv4 and DHCPv6 options, and IPv6 reservations using a single query.
std::mutex mutex_
The mutex to protect pool access.
virtual ConstHostCollection getAllbyHostname6(const std::string &hostname, const SubnetID &subnet_id) const
Return all hosts with a hostname in a DHCPv6 subnet.
boost::shared_ptr< ReconnectCtl > ReconnectCtlPtr
Pointer to an instance of ReconnectCtl.
static const StatementIndex WRITE_STMTS_BEGIN
Index of first statement performing write to the database.
const uint32_t PG_SCHEMA_VERSION_MAJOR
Define PostgreSQL backend version: 6.2.
const size_t OPTION_VALUE_MAX_LEN
Maximum length of option value.
uint64_t HostID
HostID (used only when storing in MySQL, PostgreSQL or Cassandra)
A template representing an optional value.
const isc::log::MessageID DHCPSRV_PGSQL_HOST_DB_READONLY
boost::shared_ptr< PgSqlIPv6ReservationExchange > host_ipv6_reservation_exchange_
Pointer to an object representing an exchange which can be used to insert new IPv6 reservation...
Exception thrown on failure to execute a database function.
boost::shared_ptr< IOServiceAccessor > IOServiceAccessorPtr
Pointer to an instance of IOServiceAccessor.
PostgreSQL Host Context Pool.
Database duplicate entry error.
uint32_t SubnetID
Unique identifier for a subnet (both v4 and v6)
RAII object representing a PostgreSQL transaction.
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.
boost::shared_ptr< PgSqlHostWithOptionsExchange > host_ipv4_exchange_
The exchange objects are used for transfer of data to/from the database.
bool unusable_
Indicates if there is at least one connection that can no longer be used for normal operations...