21 #include <boost/make_shared.hpp> 22 #include <boost/static_assert.hpp> 47 "DELETE FROM lease4 WHERE address = $1 AND expire = $2"},
51 "delete_lease4_state_expired",
53 "WHERE state = $1 AND expire < $2"},
58 "DELETE FROM lease6 WHERE address = $1 AND expire = $2"},
62 "delete_lease6_state_expired",
64 "WHERE state = $1 AND expire < $2"},
69 "SELECT address, hwaddr, client_id, " 70 "valid_lifetime, extract(epoch from expire)::bigint, subnet_id, " 71 "fqdn_fwd, fqdn_rev, hostname, " 72 "state, user_context " 78 "SELECT address, hwaddr, client_id, " 79 "valid_lifetime, extract(epoch from expire)::bigint, subnet_id, " 80 "fqdn_fwd, fqdn_rev, hostname, " 81 "state, user_context " 83 "WHERE address = $1"},
87 "get_lease4_clientid",
88 "SELECT address, hwaddr, client_id, " 89 "valid_lifetime, extract(epoch from expire)::bigint, subnet_id, " 90 "fqdn_fwd, fqdn_rev, hostname, " 91 "state, user_context " 93 "WHERE client_id = $1"},
97 "get_lease4_clientid_subid",
98 "SELECT address, hwaddr, client_id, " 99 "valid_lifetime, extract(epoch from expire)::bigint, subnet_id, " 100 "fqdn_fwd, fqdn_rev, hostname, " 101 "state, user_context " 103 "WHERE client_id = $1 AND subnet_id = $2"},
108 "SELECT address, hwaddr, client_id, " 109 "valid_lifetime, extract(epoch from expire)::bigint, subnet_id, " 110 "fqdn_fwd, fqdn_rev, hostname, " 111 "state, user_context " 113 "WHERE hwaddr = $1"},
117 "get_lease4_hwaddr_subid",
118 "SELECT address, hwaddr, client_id, " 119 "valid_lifetime, extract(epoch from expire)::bigint, subnet_id, " 120 "fqdn_fwd, fqdn_rev, hostname, " 121 "state, user_context " 123 "WHERE hwaddr = $1 AND subnet_id = $2"},
128 "SELECT address, hwaddr, client_id, " 129 "valid_lifetime, extract(epoch from expire)::bigint, subnet_id, " 130 "fqdn_fwd, fqdn_rev, hostname, " 131 "state, user_context " 133 "WHERE address > $1 " 140 "SELECT address, hwaddr, client_id, " 141 "valid_lifetime, extract(epoch from expire)::bigint, subnet_id, " 142 "fqdn_fwd, fqdn_rev, hostname, " 143 "state, user_context " 145 "WHERE subnet_id = $1"},
149 "get_lease4_hostname",
150 "SELECT address, hwaddr, client_id, " 151 "valid_lifetime, extract(epoch from expire)::bigint, subnet_id, " 152 "fqdn_fwd, fqdn_rev, hostname, " 153 "state, user_context " 155 "WHERE lower(hostname) = $1"},
160 "SELECT address, hwaddr, client_id, " 161 "valid_lifetime, extract(epoch from expire)::bigint, subnet_id, " 162 "fqdn_fwd, fqdn_rev, hostname, " 163 "state, user_context " 165 "WHERE state != $1 AND valid_lifetime != 4294967295 AND expire < $2 " 172 "SELECT address, duid, valid_lifetime, " 173 "extract(epoch from expire)::bigint, subnet_id, pref_lifetime, " 174 "lease_type, iaid, prefix_len, fqdn_fwd, fqdn_rev, hostname, " 175 "hwaddr, hwtype, hwaddr_source, " 176 "state, user_context " 182 "SELECT address, duid, valid_lifetime, " 183 "extract(epoch from expire)::bigint, subnet_id, pref_lifetime, " 184 "lease_type, iaid, prefix_len, fqdn_fwd, fqdn_rev, hostname, " 185 "hwaddr, hwtype, hwaddr_source, " 186 "state, user_context " 188 "WHERE address = $1 AND lease_type = $2"},
192 "get_lease6_duid_iaid",
193 "SELECT address, duid, valid_lifetime, " 194 "extract(epoch from expire)::bigint, subnet_id, pref_lifetime, " 195 "lease_type, iaid, prefix_len, fqdn_fwd, fqdn_rev, hostname, " 196 "hwaddr, hwtype, hwaddr_source, " 197 "state, user_context " 199 "WHERE duid = $1 AND iaid = $2 AND lease_type = $3"},
203 "get_lease6_duid_iaid_subid",
204 "SELECT address, duid, valid_lifetime, " 205 "extract(epoch from expire)::bigint, subnet_id, pref_lifetime, " 206 "lease_type, iaid, prefix_len, fqdn_fwd, fqdn_rev, hostname, " 207 "hwaddr, hwtype, hwaddr_source, " 208 "state, user_context " 210 "WHERE lease_type = $1 " 211 "AND duid = $2 AND iaid = $3 AND subnet_id = $4"},
216 "SELECT address, duid, valid_lifetime, " 217 "extract(epoch from expire)::bigint, subnet_id, pref_lifetime, " 218 "lease_type, iaid, prefix_len, fqdn_fwd, fqdn_rev, hostname, " 219 "hwaddr, hwtype, hwaddr_source, " 220 "state, user_context " 222 "WHERE address > $1 " 229 "SELECT address, duid, valid_lifetime, " 230 "extract(epoch from expire)::bigint, subnet_id, pref_lifetime, " 231 "lease_type, iaid, prefix_len, fqdn_fwd, fqdn_rev, hostname, " 232 "hwaddr, hwtype, hwaddr_source, " 233 "state, user_context " 235 "WHERE subnet_id = $1"},
240 "SELECT address, duid, valid_lifetime, " 241 "extract(epoch from expire)::bigint, subnet_id, pref_lifetime, " 242 "lease_type, iaid, prefix_len, fqdn_fwd, fqdn_rev, hostname, " 243 "hwaddr, hwtype, hwaddr_source, " 244 "state, user_context " 250 "get_lease6_hostname",
251 "SELECT address, duid, valid_lifetime, " 252 "extract(epoch from expire)::bigint, subnet_id, pref_lifetime, " 253 "lease_type, iaid, prefix_len, fqdn_fwd, fqdn_rev, hostname, " 254 "hwaddr, hwtype, hwaddr_source, " 255 "state, user_context " 257 "WHERE lower(hostname) = $1"},
262 "SELECT address, duid, valid_lifetime, " 263 "extract(epoch from expire)::bigint, subnet_id, pref_lifetime, " 264 "lease_type, iaid, prefix_len, " 265 "fqdn_fwd, fqdn_rev, hostname, " 266 "hwaddr, hwtype, hwaddr_source, " 267 "state, user_context " 269 "WHERE state != $1 AND valid_lifetime != 4294967295 AND expire < $2 " 277 "INSERT INTO lease4(address, hwaddr, client_id, " 278 "valid_lifetime, expire, subnet_id, fqdn_fwd, fqdn_rev, hostname, " 279 "state, user_context) " 280 "VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11)"},
287 "INSERT INTO lease6(address, duid, valid_lifetime, " 288 "expire, subnet_id, pref_lifetime, " 289 "lease_type, iaid, prefix_len, fqdn_fwd, fqdn_rev, hostname, " 290 "hwaddr, hwtype, hwaddr_source, " 291 "state, user_context) " 292 "VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15, $16, $17)"},
298 "UPDATE lease4 SET address = $1, hwaddr = $2, " 299 "client_id = $3, valid_lifetime = $4, expire = $5, " 300 "subnet_id = $6, fqdn_fwd = $7, fqdn_rev = $8, hostname = $9, " 301 "state = $10, user_context = $11 " 302 "WHERE address = $12 AND expire = $13"},
310 "UPDATE lease6 SET address = $1, duid = $2, " 311 "valid_lifetime = $3, expire = $4, subnet_id = $5, " 312 "pref_lifetime = $6, lease_type = $7, iaid = $8, " 313 "prefix_len = $9, fqdn_fwd = $10, fqdn_rev = $11, hostname = $12, " 314 "hwaddr = $13, hwtype = $14, hwaddr_source = $15, " 315 "state = $16, user_context = $17 " 316 "WHERE address = $18 AND expire = $19"},
321 "SELECT subnet_id, state, leases as state_count" 322 " FROM lease4_stat ORDER BY subnet_id, state"},
326 "subnet_lease4_stats",
327 "SELECT subnet_id, state, leases as state_count" 329 " WHERE subnet_id = $1 " 334 "subnet_range_lease4_stats",
335 "SELECT subnet_id, state, leases as state_count" 337 " WHERE subnet_id >= $1 and subnet_id <= $2 " 338 " ORDER BY subnet_id, state"},
343 "SELECT subnet_id, lease_type, state, leases as state_count" 344 " FROM lease6_stat ORDER BY subnet_id, lease_type, state" },
348 "subnet_lease6_stats",
349 "SELECT subnet_id, lease_type, state, leases as state_count" 351 " WHERE subnet_id = $1 " 352 " ORDER BY lease_type, state" },
356 "subnet_range_lease6_stats",
357 "SELECT subnet_id, lease_type, state, leases as state_count" 359 " WHERE subnet_id >= $1 and subnet_id <= $2 " 360 " ORDER BY subnet_id, lease_type, state" },
366 "check_lease4_limits",
367 "SELECT 'checkLease4Limits($1)'" },
371 "check_lease6_limits",
372 "SELECT 'checkLease6Limits($1)'" },
375 { 0, { 0 }, NULL, NULL}
392 : addr_str_(
""), hwaddr_length_(0), hwaddr_(hwaddr_length_),
393 valid_lifetime_(0), valid_lifetime_str_(
""), expire_(0),
394 expire_str_(
""), subnet_id_(0), subnet_id_str_(
""), cltt_(0),
395 fqdn_fwd_(false), fqdn_rev_(false), hostname_(
""), state_str_(
""),
408 uint8_t hwaddr_buffer_[HWAddr::MAX_HWADDR_LEN];
432 static const size_t ADDRESS_COL = 0;
433 static const size_t HWADDR_COL = 1;
434 static const size_t CLIENT_ID_COL = 2;
435 static const size_t VALID_LIFETIME_COL = 3;
436 static const size_t EXPIRE_COL = 4;
437 static const size_t SUBNET_ID_COL = 5;
438 static const size_t FQDN_FWD_COL = 6;
439 static const size_t FQDN_REV_COL = 7;
440 static const size_t HOSTNAME_COL = 8;
441 static const size_t STATE_COL = 9;
442 static const size_t USER_CONTEXT_COL = 10;
444 static const size_t LEASE_COLUMNS = 11;
450 : lease_(), addr4_(0), client_id_length_(0) {
452 BOOST_STATIC_ASSERT(9 < LEASE_COLUMNS);
454 memset(hwaddr_buffer_, 0,
sizeof(hwaddr_buffer_));
455 memset(client_id_buffer_, 0,
sizeof(client_id_buffer_));
458 columns_.push_back(
"address");
459 columns_.push_back(
"hwaddr");
460 columns_.push_back(
"client_id");
461 columns_.push_back(
"valid_lifetime");
462 columns_.push_back(
"expire");
463 columns_.push_back(
"subnet_id");
464 columns_.push_back(
"fqdn_fwd");
465 columns_.push_back(
"fqdn_rev");
466 columns_.push_back(
"hostname");
467 columns_.push_back(
"state");
468 columns_.push_back(
"user_context");
492 addr_str_ = boost::lexical_cast<std::string>(lease->addr_.toUint32());
493 bind_array.
add(addr_str_);
495 if (lease->hwaddr_ && !lease->hwaddr_->hwaddr_.empty()) {
498 if (lease->hwaddr_->hwaddr_.size() > HWAddr::MAX_HWADDR_LEN) {
500 << lease_->hwaddr_->hwaddr_.size()
501 <<
" exceeds maximum allowed of: " 502 << HWAddr::MAX_HWADDR_LEN);
504 bind_array.
add(lease->hwaddr_->hwaddr_);
509 if (lease->client_id_) {
510 bind_array.
add(lease->client_id_->getClientId());
515 valid_lifetime_str_ = boost::lexical_cast<std::string>(lease->valid_lft_);
516 bind_array.
add(valid_lifetime_str_);
524 if (lease_->valid_lft_ == Lease::INFINITY_LFT) {
525 expire_str_ = convertToDatabaseTime(lease->cltt_, 0);
527 expire_str_ = convertToDatabaseTime(lease->cltt_,
530 bind_array.
add(expire_str_);
532 subnet_id_str_ = boost::lexical_cast<std::string>(lease->subnet_id_);
533 bind_array.
add(subnet_id_str_);
535 bind_array.
add(lease->fqdn_fwd_);
537 bind_array.
add(lease->fqdn_rev_);
539 bind_array.
add(lease->hostname_);
541 state_str_ = boost::lexical_cast<std::string>(lease->state_);
542 bind_array.
add(state_str_);
546 user_context_ = ctx->str();
550 bind_array.
add(user_context_);
551 }
catch (
const std::exception& ex) {
553 "Could not create bind array from Lease4: " 554 << lease_->addr_.toText() <<
", reason: " << ex.
what());
568 getColumnValue(r, row, ADDRESS_COL, addr4_);
570 convertFromBytea(r, row, HWADDR_COL, hwaddr_buffer_,
571 sizeof(hwaddr_buffer_), hwaddr_length_);
573 convertFromBytea(r, row, CLIENT_ID_COL, client_id_buffer_,
574 sizeof(client_id_buffer_), client_id_length_);
576 getColumnValue(r, row, VALID_LIFETIME_COL, valid_lifetime_);
578 expire_ = convertFromDatabaseTime(getRawColumnValue(r, row,
581 getColumnValue(r, row , SUBNET_ID_COL, subnet_id_);
584 if (valid_lifetime_ == Lease::INFINITY_LFT) {
587 cltt_ = expire_ - valid_lifetime_;
590 getColumnValue(r, row, FQDN_FWD_COL, fqdn_fwd_);
592 getColumnValue(r, row, FQDN_REV_COL, fqdn_rev_);
594 hostname_ = getRawColumnValue(r, row, HOSTNAME_COL);
597 getColumnValue(r, row , STATE_COL, state);
602 user_context_ = getRawColumnValue(r, row, USER_CONTEXT_COL);
604 if (!user_context_.empty()) {
605 ctx = Element::fromJSON(user_context_);
606 if (!ctx || (ctx->getType() != Element::map)) {
608 <<
"' is not a JSON map");
612 Lease4Ptr result(boost::make_shared<Lease4>(addr4_, hwaddr,
615 valid_lifetime_, cltt_,
616 subnet_id_, fqdn_fwd_,
617 fqdn_rev_, hostname_));
619 result->state_ = state;
622 result->setContext(ctx);
626 }
catch (
const std::exception& ex) {
628 "Could not convert data to Lease4, reason: " 642 size_t client_id_length_;
643 uint8_t client_id_buffer_[ClientId::MAX_CLIENT_ID_LEN];
655 static const int ADDRESS_COL = 0;
656 static const int DUID_COL = 1;
657 static const int VALID_LIFETIME_COL = 2;
658 static const int EXPIRE_COL = 3;
659 static const int SUBNET_ID_COL = 4;
660 static const int PREF_LIFETIME_COL = 5;
661 static const int LEASE_TYPE_COL = 6;
662 static const int IAID_COL = 7;
663 static const int PREFIX_LEN_COL = 8;
664 static const int FQDN_FWD_COL = 9;
665 static const int FQDN_REV_COL = 10;
666 static const int HOSTNAME_COL = 11;
667 static const int HWADDR_COL = 12;
668 static const int HWTYPE_COL = 13;
669 static const int HWADDR_SOURCE_COL = 14;
670 static const int STATE_COL = 15;
671 static const size_t USER_CONTEXT_COL = 16;
673 static const size_t LEASE_COLUMNS = 17;
695 return (boost::lexical_cast<std::string>(ival_));
703 : lease_(), duid_length_(0), duid_(duid_length_), iaid_u_(0),
704 iaid_str_(
""), lease_type_(
Lease6::TYPE_NA), lease_type_str_(
""),
705 prefix_len_(0), prefix_len_str_(
""), pref_lifetime_(0),
706 preferred_lifetime_str_(
""), hwtype_(0), hwtype_str_(
""),
707 hwaddr_source_(0), hwaddr_source_str_(
"") {
709 BOOST_STATIC_ASSERT(15 < LEASE_COLUMNS);
711 memset(duid_buffer_, 0,
sizeof(duid_buffer_));
714 columns_.push_back(
"address");
715 columns_.push_back(
"duid");
716 columns_.push_back(
"valid_lifetime");
717 columns_.push_back(
"expire");
718 columns_.push_back(
"subnet_id");
719 columns_.push_back(
"pref_lifetime");
720 columns_.push_back(
"lease_type");
721 columns_.push_back(
"iaid");
722 columns_.push_back(
"prefix_len");
723 columns_.push_back(
"fqdn_fwd");
724 columns_.push_back(
"fqdn_rev");
725 columns_.push_back(
"hostname");
726 columns_.push_back(
"hwaddr");
727 columns_.push_back(
"hwtype");
728 columns_.push_back(
"hwaddr_source");
729 columns_.push_back(
"state");
730 columns_.push_back(
"user_context");
753 addr_str_ = lease_->addr_.toText();
754 bind_array.
add(addr_str_);
757 bind_array.
add(lease_->duid_->getDuid());
762 valid_lifetime_str_ = boost::lexical_cast<std::string>(lease->valid_lft_);
763 bind_array.
add(valid_lifetime_str_);
771 if (lease_->valid_lft_ == Lease::INFINITY_LFT) {
772 expire_str_ = convertToDatabaseTime(lease->cltt_, 0);
774 expire_str_ = convertToDatabaseTime(lease->cltt_,
777 bind_array.
add(expire_str_);
779 subnet_id_str_ = boost::lexical_cast<std::string>(lease->subnet_id_);
780 bind_array.
add(subnet_id_str_);
782 preferred_lifetime_str_ = boost::lexical_cast<std::string>(lease_->preferred_lft_);
783 bind_array.
add(preferred_lifetime_str_);
785 lease_type_str_ = boost::lexical_cast<std::string>(lease_->type_);
786 bind_array.
add(lease_type_str_);
791 iaid_u_.uval_ = lease_->iaid_;
792 iaid_str_ = iaid_u_.dbInputString();
793 bind_array.
add(iaid_str_);
795 prefix_len_str_ = boost::lexical_cast<std::string>
796 (
static_cast<unsigned int>(lease_->prefixlen_));
797 bind_array.
add(prefix_len_str_);
799 bind_array.
add(lease->fqdn_fwd_);
801 bind_array.
add(lease->fqdn_rev_);
803 bind_array.
add(lease->hostname_);
805 if (lease->hwaddr_ && !lease->hwaddr_->hwaddr_.empty()) {
808 if (lease->hwaddr_->hwaddr_.size() > HWAddr::MAX_HWADDR_LEN) {
810 << lease_->hwaddr_->hwaddr_.size()
811 <<
" exceeds maximum allowed of: " 812 << HWAddr::MAX_HWADDR_LEN);
814 bind_array.
add(lease->hwaddr_->hwaddr_);
819 if (lease->hwaddr_) {
820 hwtype_str_ = boost::lexical_cast<std::string>
821 (
static_cast<unsigned int>(lease_->hwaddr_->htype_));
822 hwaddr_source_str_ = boost::lexical_cast<std::string>
823 (
static_cast<unsigned int>(lease_->hwaddr_->source_));
825 hwtype_str_ = boost::lexical_cast<std::string>
827 hwaddr_source_str_ = boost::lexical_cast<std::string>
828 (
static_cast<unsigned int>(HWAddr::HWADDR_SOURCE_UNKNOWN));
831 bind_array.
add(hwtype_str_);
833 bind_array.
add(hwaddr_source_str_);
835 state_str_ = boost::lexical_cast<std::string>(lease->state_);
836 bind_array.
add(state_str_);
840 user_context_ = ctx->str();
844 bind_array.
add(user_context_);
845 }
catch (
const std::exception& ex) {
847 "Could not create bind array from Lease6: " 848 << lease_->addr_.toText() <<
", reason: " << ex.
what());
871 IOAddress addr(getIPv6Value(r, row, ADDRESS_COL));
873 convertFromBytea(r, row, DUID_COL, duid_buffer_,
sizeof(duid_buffer_), duid_length_);
874 DuidPtr duid_ptr(
new DUID(duid_buffer_, duid_length_));
876 getColumnValue(r, row, VALID_LIFETIME_COL, valid_lifetime_);
878 expire_ = convertFromDatabaseTime(getRawColumnValue(r, row,
882 if (valid_lifetime_ == Lease::INFINITY_LFT) {
885 cltt_ = expire_ - valid_lifetime_;
888 getColumnValue(r, row , SUBNET_ID_COL, subnet_id_);
890 getColumnValue(r, row , PREF_LIFETIME_COL, pref_lifetime_);
892 getLeaseTypeColumnValue(r, row, LEASE_TYPE_COL, lease_type_);
894 getColumnValue(r, row , IAID_COL, iaid_u_.ival_);
896 getColumnValue(r, row , PREFIX_LEN_COL, prefix_len_);
898 getColumnValue(r, row, FQDN_FWD_COL, fqdn_fwd_);
900 getColumnValue(r, row, FQDN_REV_COL, fqdn_rev_);
902 hostname_ = getRawColumnValue(r, row, HOSTNAME_COL);
904 convertFromBytea(r, row, HWADDR_COL, hwaddr_buffer_,
905 sizeof(hwaddr_buffer_), hwaddr_length_);
907 getColumnValue(r, row , HWTYPE_COL, hwtype_);
909 getColumnValue(r, row , HWADDR_SOURCE_COL, hwaddr_source_);
913 if (hwaddr_length_) {
914 hwaddr.reset(
new HWAddr(hwaddr_buffer_, hwaddr_length_,
917 hwaddr->source_ = hwaddr_source_;
921 getColumnValue(r, row , STATE_COL, state);
923 user_context_ = getRawColumnValue(r, row, USER_CONTEXT_COL);
925 if (!user_context_.empty()) {
926 ctx = Element::fromJSON(user_context_);
927 if (!ctx || (ctx->getType() != Element::map)) {
929 <<
"' is not a JSON map");
933 Lease6Ptr result(boost::make_shared<Lease6>(lease_type_, addr,
938 subnet_id_, fqdn_fwd_,
939 fqdn_rev_, hostname_,
940 hwaddr, prefix_len_));
942 result->cltt_ = cltt_;
943 result->current_cltt_ = cltt_;
945 result->state_ = state;
948 result->setContext(ctx);
952 }
catch (
const std::exception& ex) {
954 "Could not convert data to Lease6, reason: " 973 uint32_t raw_value = 0;
974 getColumnValue(r, row , col, raw_value);
976 case Lease6::TYPE_NA:
977 case Lease6::TYPE_TA:
978 case Lease6::TYPE_PD:
984 <<
" for: " << getColumnLabel(r, col) <<
" row:" << row);
997 std::vector<uint8_t> duid_;
998 uint8_t duid_buffer_[DUID::MAX_DUID_LEN];
1000 std::string iaid_str_;
1002 std::string lease_type_str_;
1003 uint8_t prefix_len_;
1004 std::string prefix_len_str_;
1005 uint32_t pref_lifetime_;
1006 std::string preferred_lifetime_str_;
1008 std::string hwtype_str_;
1009 uint32_t hwaddr_source_;
1010 std::string hwaddr_source_str_;
1031 const bool fetch_type)
1032 : conn_(conn), statement_(statement), result_set_(), next_row_(0),
1033 fetch_type_(fetch_type) {
1045 const bool fetch_type,
const SubnetID& subnet_id)
1046 :
LeaseStatsQuery(subnet_id), conn_(conn), statement_(statement), result_set_(),
1047 next_row_(0), fetch_type_(fetch_type) {
1061 const bool fetch_type,
const SubnetID& first_subnet_id,
1063 :
LeaseStatsQuery(first_subnet_id, last_subnet_id), conn_(conn), statement_(statement),
1064 result_set_(), next_row_(0), fetch_type_(fetch_type) {
1081 if (getSelectMode() == ALL_SUBNETS) {
1083 result_set_.reset(
new PgSqlResult(PQexecPrepared(conn_, statement_.name,
1090 parms.
addTempString(boost::lexical_cast<std::string>(getFirstSubnetID()));
1093 if (getSelectMode() == SUBNET_RANGE) {
1095 parms.
addTempString(boost::lexical_cast<std::string>(getLastSubnetID()));
1099 result_set_.reset(
new PgSqlResult(PQexecPrepared(conn_, statement_.name,
1104 conn_.checkStatementError(*result_set_, statement_);
1123 if (next_row_ >= result_set_->getRows()) {
1130 PgSqlExchange::getColumnValue(*result_set_, next_row_, col, subnet_id);
1136 uint32_t lease_type;
1137 PgSqlExchange::getColumnValue(*result_set_, next_row_ , col,
1146 PgSqlExchange::getColumnValue(*result_set_, next_row_ , col,
1151 PgSqlExchange::getColumnValue(*result_set_, next_row_, col,
1157 if (!negative_count_) {
1158 negative_count_ =
true;
1190 bool PgSqlLeaseStatsQuery::negative_count_ =
false;
1197 : conn_(parameters, io_service_accessor, db_reconnect_callback) {
1202 PgSqlLeaseMgr::PgSqlLeaseContextAlloc::PgSqlLeaseContextAlloc(
1205 if (MultiThreadingMgr::instance().getMode()) {
1209 lock_guard<mutex> lock(mgr_.pool_->mutex_);
1210 if (!mgr_.pool_->pool_.empty()) {
1211 ctx_ = mgr_.pool_->pool_.back();
1212 mgr_.pool_->pool_.pop_back();
1216 ctx_ = mgr_.createContext();
1220 if (mgr_.pool_->pool_.empty()) {
1223 ctx_ = mgr_.pool_->pool_.back();
1227 PgSqlLeaseMgr::PgSqlLeaseContextAlloc::~PgSqlLeaseContextAlloc() {
1228 if (MultiThreadingMgr::instance().getMode()) {
1230 lock_guard<mutex> lock(mgr_.pool_->mutex_);
1231 mgr_.pool_->pool_.push_back(ctx_);
1239 : parameters_(parameters), timer_name_(
"") {
1242 timer_name_ =
"PgSqlLeaseMgr[";
1243 timer_name_ += boost::lexical_cast<std::string>(
reinterpret_cast<uint64_t
>(
this));
1244 timer_name_ +=
"]DbReconnectTimer";
1248 tls += parameters.count(
"trust-anchor");
1249 tls += parameters.count(
"cert-file");
1250 tls += parameters.count(
"key-file");
1251 tls += parameters.count(
"cipher-list");
1252 #ifdef HAVE_PGSQL_SSL 1264 <<
"backend (built with this feature disabled)");
1271 std::pair<uint32_t, uint32_t> db_version =
getVersion();
1272 if (code_version != db_version) {
1274 "PostgreSQL schema version mismatch: need version: " 1275 << code_version.first <<
"." << code_version.second
1276 <<
" found version: " << db_version.first <<
"." 1277 << db_version.second);
1297 bool reopened =
false;
1299 const std::string timer_name = db_reconnect_ctl->timerName();
1307 }
catch (
const std::exception& ex) {
1323 if (!db_reconnect_ctl->checkRetries()) {
1326 .arg(db_reconnect_ctl->maxRetries());
1339 .arg(db_reconnect_ctl->maxRetries() - db_reconnect_ctl->retriesLeft() + 1)
1340 .arg(db_reconnect_ctl->maxRetries())
1341 .arg(db_reconnect_ctl->retryInterval());
1347 db_reconnect_ctl->retryInterval(),
1365 ctx->conn_.openDatabase();
1369 for (; tagged_statements[i].text != NULL; ++i) {
1370 ctx->conn_.prepareStatement(tagged_statements[i]);
1385 ctx->conn_.makeReconnectCtl(timer_name_);
1392 std::stringstream tmp;
1395 tmp <<
", library " << PQlibVersion();
1403 PgSqlResult r(PQexecPrepared(ctx->conn_, tagged_statements[stindex].name,
1404 tagged_statements[stindex].nbparams,
1409 int s = PQresultStatus(r);
1411 if (s != PGRES_COMMAND_OK) {
1418 ctx->conn_.checkStatementError(r, tagged_statements[stindex]);
1427 .arg(lease->addr_.toText());
1430 PgSqlLeaseContextAlloc get_context(*
this);
1434 ctx->exchange4_->createBindForSend(lease, bind_array);
1435 auto result = addLeaseCommon(ctx,
INSERT_LEASE4, bind_array);
1439 lease->updateCurrentExpirationTime();
1447 .arg(lease->addr_.toText())
1451 PgSqlLeaseContextAlloc get_context(*
this);
1455 ctx->exchange6_->createBindForSend(lease, bind_array);
1457 auto result = addLeaseCommon(ctx,
INSERT_LEASE6, bind_array);
1461 lease->updateCurrentExpirationTime();
1466 template <
typename Exchange,
typename LeaseCollection>
1472 LeaseCollection& result,
1473 bool single)
const {
1474 const int n = tagged_statements[stindex].nbparams;
1476 tagged_statements[stindex].name, n,
1477 n > 0 ? &bind_array.
values_[0] : NULL,
1478 n > 0 ? &bind_array.
lengths_[0] : NULL,
1479 n > 0 ? &bind_array.
formats_[0] : NULL, 0));
1481 ctx->conn_.checkStatementError(r, tagged_statements[stindex]);
1483 int rows = PQntuples(r);
1484 if (single && rows > 1) {
1486 "database where only one was expected for query " 1487 << tagged_statements[stindex].name);
1490 for(
int i = 0; i < rows; ++ i) {
1491 result.push_back(exchange->convertFromDatabase(r, i));
1505 getLeaseCollection(ctx, stindex, bind_array, ctx->exchange4_,
1509 if (collection.empty()) {
1512 result = *collection.begin();
1526 getLeaseCollection(ctx, stindex, bind_array, ctx->exchange6_,
1530 if (collection.empty()) {
1533 result = *collection.begin();
1546 std::string addr_str = boost::lexical_cast<std::string>(addr.
toUint32());
1547 bind_array.
add(addr_str);
1553 PgSqlLeaseContextAlloc get_context(*
this);
1570 if (!hwaddr.
hwaddr_.empty()) {
1580 PgSqlLeaseContextAlloc get_context(*
this);
1598 if (!hwaddr.
hwaddr_.empty()) {
1605 std::string subnet_id_str = boost::lexical_cast<std::string>(subnet_id);
1606 bind_array.
add(subnet_id_str);
1612 PgSqlLeaseContextAlloc get_context(*
this);
1635 PgSqlLeaseContextAlloc get_context(*
this);
1656 std::string subnet_id_str = boost::lexical_cast<std::string>(subnet_id);
1657 bind_array.
add(subnet_id_str);
1663 PgSqlLeaseContextAlloc get_context(*
this);
1680 std::string subnet_id_str = boost::lexical_cast<std::string>(subnet_id);
1681 bind_array.
add(subnet_id_str);
1687 PgSqlLeaseContextAlloc get_context(*
this);
1704 bind_array.
add(hostname);
1710 PgSqlLeaseContextAlloc get_context(*
this);
1728 PgSqlLeaseContextAlloc get_context(*
this);
1731 getLeaseCollection(ctx,
GET_LEASE4, bind_array, result);
1740 if (!lower_bound_address.
isV4()) {
1742 "retrieving leases from the lease database, got " 1743 << lower_bound_address);
1748 .arg(lower_bound_address.
toText());
1754 std::string lb_address_data = boost::lexical_cast<std::string>(lower_bound_address.
toUint32());
1755 bind_array.
add(lb_address_data);
1758 std::string page_size_data = boost::lexical_cast<std::string>(page_size.
page_size_);
1759 bind_array.
add(page_size_data);
1765 PgSqlLeaseContextAlloc get_context(*
this);
1784 std::string addr_str = addr.
toText();
1785 bind_array.
add(addr_str);
1788 std::string type_str_ = boost::lexical_cast<std::string>(lease_type);
1789 bind_array.
add(type_str_);
1795 PgSqlLeaseContextAlloc get_context(*
this);
1805 uint32_t iaid)
const {
1819 bind_array.
add(iaid_str);
1822 std::string lease_type_str = boost::lexical_cast<std::string>(lease_type);
1823 bind_array.
add(lease_type_str);
1829 PgSqlLeaseContextAlloc get_context(*
this);
1839 uint32_t iaid,
SubnetID subnet_id)
const {
1850 std::string lease_type_str = boost::lexical_cast<std::string>(lease_type);
1851 bind_array.
add(lease_type_str);
1858 bind_array.
add(iaid_str);
1861 std::string subnet_id_str = boost::lexical_cast<std::string>(subnet_id);
1862 bind_array.
add(subnet_id_str);
1868 PgSqlLeaseContextAlloc get_context(*
this);
1885 std::string subnet_id_str = boost::lexical_cast<std::string>(subnet_id);
1886 bind_array.
add(subnet_id_str);
1892 PgSqlLeaseContextAlloc get_context(*
this);
1913 PgSqlLeaseContextAlloc get_context(*
this);
1931 bind_array.
add(hostname);
1937 PgSqlLeaseContextAlloc get_context(*
this);
1955 PgSqlLeaseContextAlloc get_context(*
this);
1958 getLeaseCollection(ctx,
GET_LEASE6, bind_array, result);
1967 if (!lower_bound_address.
isV6()) {
1969 "retrieving leases from the lease database, got " 1970 << lower_bound_address);
1975 .arg(lower_bound_address.
toText());
1983 std::string lb_address_data =
"0";
1984 if (!lower_bound_address.
isV6Zero()) {
1985 lb_address_data = lower_bound_address.
toText();
1989 bind_array.
add(lb_address_data);
1992 std::string page_size_data = boost::lexical_cast<std::string>(page_size.
page_size_);
1993 bind_array.
add(page_size_data);
1999 PgSqlLeaseContextAlloc get_context(*
this);
2009 const size_t max_leases)
const {
2017 const size_t max_leases)
const {
2023 template<
typename LeaseCollection>
2025 PgSqlLeaseMgr::getExpiredLeasesCommon(LeaseCollection& expired_leases,
2026 const size_t max_leases,
2032 bind_array.
add(state_str);
2036 bind_array.
add(timestamp_str);
2040 uint32_t limit = max_leases > 0 ?
static_cast<uint32_t
>(max_leases) :
2041 std::numeric_limits<uint32_t>::max();
2042 std::string limit_str = boost::lexical_cast<std::string>(limit);
2043 bind_array.
add(limit_str);
2046 PgSqlLeaseContextAlloc get_context(*
this);
2050 getLeaseCollection(ctx, statement_index, bind_array, expired_leases);
2053 template<
typename LeasePtr>
2059 PgSqlResult r(PQexecPrepared(ctx->conn_, tagged_statements[stindex].name,
2060 tagged_statements[stindex].nbparams,
2065 ctx->conn_.checkStatementError(r, tagged_statements[stindex]);
2067 int affected_rows = boost::lexical_cast<
int>(PQcmdTuples(r));
2070 if (affected_rows == 1) {
2075 if (affected_rows == 0) {
2077 lease->addr_.toText() <<
" as it does not exist");
2083 "that had the address " << lease->addr_.toText());
2091 .arg(lease->addr_.toText());
2094 PgSqlLeaseContextAlloc get_context(*
this);
2099 ctx->exchange4_->createBindForSend(lease, bind_array);
2102 std::string addr4_str = boost::lexical_cast<std::string>(lease->addr_.toUint32());
2103 bind_array.
add(addr4_str);
2105 std::string expire_str;
2111 lease->current_valid_lft_);
2113 bind_array.
add(expire_str);
2116 updateLeaseCommon(ctx, stindex, bind_array, lease);
2119 lease->updateCurrentExpirationTime();
2127 .arg(lease->addr_.toText())
2131 PgSqlLeaseContextAlloc get_context(*
this);
2136 ctx->exchange6_->createBindForSend(lease, bind_array);
2139 std::string addr_str = lease->addr_.
toText();
2140 bind_array.
add(addr_str);
2142 std::string expire_str;
2148 lease->current_valid_lft_);
2150 bind_array.
add(expire_str);
2153 updateLeaseCommon(ctx, stindex, bind_array, lease);
2156 lease->updateCurrentExpirationTime();
2163 PgSqlLeaseContextAlloc get_context(*
this);
2166 PgSqlResult r(PQexecPrepared(ctx->conn_, tagged_statements[stindex].name,
2167 tagged_statements[stindex].nbparams,
2172 ctx->conn_.checkStatementError(r, tagged_statements[stindex]);
2173 int affected_rows = boost::lexical_cast<
int>(PQcmdTuples(r));
2175 return (affected_rows);
2187 std::string addr4_str = boost::lexical_cast<std::string>(addr.
toUint32());
2188 bind_array.
add(addr4_str);
2190 std::string expire_str;
2196 lease->current_valid_lft_);
2198 bind_array.
add(expire_str);
2200 auto affected_rows = deleteLeaseCommon(
DELETE_LEASE4, bind_array);
2203 if (affected_rows == 1) {
2208 if (affected_rows == 0) {
2215 "that had the address " << lease->addr_.toText());
2228 std::string addr6_str = addr.
toText();
2229 bind_array.
add(addr6_str);
2231 std::string expire_str;
2237 lease->current_valid_lft_);
2239 bind_array.
add(expire_str);
2241 auto affected_rows = deleteLeaseCommon(
DELETE_LEASE6, bind_array);
2244 if (affected_rows == 1) {
2249 if (affected_rows == 0) {
2256 "that had the address " << lease->addr_.toText());
2274 PgSqlLeaseMgr::deleteExpiredReclaimedLeasesCommon(
const uint32_t secs,
2280 bind_array.
add(state_str);
2284 static_cast<time_t>(secs));
2285 bind_array.
add(expiration_str);
2288 return (deleteLeaseCommon(statement_index, bind_array));
2295 std::string
const user_context_str(user_context->str());
2296 bind_array.
add(user_context_str);
2299 PgSqlLeaseContextAlloc get_context(*
this);
2303 tagged_statements[stindex].name,
2304 tagged_statements[stindex].nbparams,
2309 ctx->conn_.checkStatementError(r, tagged_statements[stindex]);
2311 int rows = PQntuples(r);
2314 "database where only one was expected for query " 2315 << tagged_statements[stindex].name);
2323 PgSqlLeaseMgr::checkLimits4(
ConstElementPtr const& user_context)
const {
2328 PgSqlLeaseMgr::checkLimits6(
ConstElementPtr const& user_context)
const {
2335 PgSqlLeaseContextAlloc get_context(*
this);
2348 PgSqlLeaseContextAlloc get_context(*
this);
2363 PgSqlLeaseContextAlloc get_context(*
this);
2378 PgSqlLeaseContextAlloc get_context(*
this);
2391 PgSqlLeaseContextAlloc get_context(*
this);
2406 PgSqlLeaseContextAlloc get_context(*
this);
2436 PgSqlLeaseContextAlloc get_context(*
this);
2439 std::string name =
"";
2441 name = ctx->conn_.getParameter(
"name");
2450 return (std::string(
"PostgreSQL Database"));
2453 std::pair<uint32_t, uint32_t>
const isc::log::MessageID DHCPSRV_PGSQL_GET_SUBID6
virtual ~PgSqlLeaseMgr()
Destructor (closes database)
PgSqlLeaseStatsQuery(PgSqlConnection &conn, PgSqlTaggedStatement &statement, const bool fetch_type, const SubnetID &subnet_id)
Constructor to query for a single subnet's stats.
RAII class creating a critical section.
bool isJsonSupported() const override
Checks if JSON support is enabled in the database.
#define LOG_WARN(LOGGER, MESSAGE)
Macro to conveniently test warn output and log it.
std::function< bool(util::ReconnectCtlPtr db_reconnect_ctl)> DbCallback
Defines a callback prototype for propagating events upward.
boost::shared_ptr< DUID > DuidPtr
std::string toText() const
Dumps the contents of the array to a string.
const isc::log::MessageID DHCPSRV_PGSQL_LEASE_DB_RECONNECT_FAILED
RAII wrapper for PostgreSQL Result sets.
const isc::log::MessageID DHCPSRV_PGSQL_ROLLBACK
boost::shared_ptr< LeaseStatsQuery > LeaseStatsQueryPtr
Defines a pointer to a LeaseStatsQuery.
void createBindForSend(const Lease6Ptr &lease, PsqlBindArray &bind_array)
Creates the bind array for sending Lease6 data to the database.
PostgreSQL Lease Context.
const isc::log::MessageID DHCPSRV_PGSQL_GET_SUBID4
A generic exception that is thrown when a function is not implemented.
static const uint32_t STATE_EXPIRED_RECLAIMED
Expired and reclaimed lease.
static bool negative_count_
Received negative state count showing a problem.
const std::vector< uint8_t > & getClientId() const
Returns reference to the client-id data.
static std::string convertToDatabaseTime(const time_t input_time)
Converts UTC time_t value to a text representation in local time.
std::string valid_lifetime_str_
const isc::log::MessageID DHCPSRV_PGSQL_GET_HOSTNAME4
void getLeaseTypeColumnValue(const PgSqlResult &r, const int row, const size_t col, Lease6::Type &value) const
Fetches an integer text column as a Lease6::Type.
#define LOG_INFO(LOGGER, MESSAGE)
Macro to conveniently test info output and log it.
Uiaid(uint32_t val)
Constructor.
boost::shared_ptr< HWAddr > HWAddrPtr
Shared pointer to a hardware address structure.
std::vector< int > formats_
Vector of "format" for each value.
boost::shared_ptr< PgSqlResult > result_set_
The result set returned by Postgres.
PostgreSQL Lease Manager.
uint32_t next_row_
Index of the next row to fetch.
boost::shared_ptr< ReconnectCtl > ReconnectCtlPtr
Pointer to an instance of ReconnectCtl.
const isc::log::MessageID DHCPSRV_PGSQL_LEASE_DB_RECONNECT_ATTEMPT_SCHEDULE
static void destroy()
Destroy lease manager.
virtual LeaseStatsQueryPtr startSubnetLeaseStatsQuery6(const SubnetID &subnet_id)
Creates and runs the IPv6 lease stats query for a single subnet.
const isc::log::MessageID DHCPSRV_PGSQL_NO_TLS_SUPPORT
bool getNextRow(LeaseStatsRow &row)
Fetches the next row in the result set.
Base class for marshalling leases to and from PostgreSQL.
const isc::log::MessageID DHCPSRV_PGSQL_GET_ADDR4
const isc::log::MessageID DHCPSRV_PGSQL_GET_EXPIRED4
static CfgMgr & instance()
returns a single instance of Configuration Manager
std::vector< int > lengths_
Vector of data lengths for each value.
std::string user_context_
Attempt to update lease that was not there.
Union for marshalling IAID into and out of the database IAID is defined in the RFC as 4 octets...
std::vector< Lease4Ptr > Lease4Collection
A collection of IPv4 leases.
virtual uint64_t deleteExpiredReclaimedLeases6(const uint32_t secs)
Deletes all expired-reclaimed DHCPv6 leases.
const isc::log::MessageID DHCPSRV_PGSQL_GET_VERSION
static const char DUPLICATE_KEY[]
Define the PgSql error state for a duplicate key error.
#define LOG_ERROR(LOGGER, MESSAGE)
Macro to conveniently test error output and log it.
SrvConfigPtr getCurrentCfg()
Returns a pointer to the current configuration.
std::vector< uint8_t > hwaddr_
Base class for fulfilling a statistical lease data query.
Base class for marshalling data to and from PostgreSQL.
virtual LeaseStatsQueryPtr startSubnetRangeLeaseStatsQuery6(const SubnetID &first_subnet_id, const SubnetID &last_subnet_id)
Creates and runs the IPv6 lease stats query for a single subnet.
const isc::log::MessageID DHCPSRV_PGSQL_DELETE_EXPIRED_RECLAIMED6
PgSqlLease4Exchange()
Constructor.
void createBindForSend(const Lease4Ptr &lease, PsqlBindArray &bind_array)
Creates the bind array for sending Lease4 data to the database.
const isc::log::MessageID DHCPSRV_PGSQL_GET_SUBID_HWADDR
std::vector< uint8_t > hwaddr_
boost::shared_ptr< Lease > LeasePtr
Pointer to the lease object.
virtual const char * what() const
Returns a C-style character string of the cause of the exception.
Holds DUID (DHCPv6 Unique Identifier)
const isc::log::MessageID DHCPSRV_PGSQL_GET_EXPIRED6
Exception thrown on failure to open database.
const isc::log::MessageID DHCPSRV_PGSQL_GET_HOSTNAME6
static bool dbReconnect(util::ReconnectCtlPtr db_reconnect_ctl)
Attempts to reconnect the server to the lease DB backend manager.
not specified or undefined
static isc::asiolink::IOServicePtr & getIOService()
Returns pointer to the IO service.
virtual LeaseStatsQueryPtr startSubnetRangeLeaseStatsQuery4(const SubnetID &first_subnet_id, const SubnetID &last_subnet_id)
Creates and runs the IPv4 lease stats query for a single subnet.
std::vector< const char * > values_
Vector of pointers to the data values.
virtual LeaseStatsQueryPtr startSubnetLeaseStatsQuery4(const SubnetID &subnet_id)
Creates and runs the IPv4 lease stats query for a single subnet.
Multiple lease records found where one expected.
Lease6Ptr convertFromDatabase(const PgSqlResult &r, int row)
Creates a Lease6 object from a given row in a result set.
virtual void getExpiredLeases6(Lease6Collection &expired_leases, const size_t max_leases) const
Returns a collection of expired DHCPv6 leases.
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
A generic exception that is thrown if a parameter given to a method is considered invalid in that con...
void addTempString(const std::string &str)
Binds the given string to the bind array.
PostgreSQL Lease Context Pool.
void add(const char *value)
Adds a char array to bind array based.
virtual void updateLease4(const Lease4Ptr &lease4)
Updates IPv4 lease.
const size_t page_size_
Holds page size.
PgSqlLeaseContextPtr createContext() const
Create a new context.
const isc::log::MessageID DHCPSRV_PGSQL_GET4
boost::shared_ptr< Lease4 > Lease4Ptr
Pointer to a Lease4 structure.
const int DHCPSRV_DBG_TRACE_DETAIL
Additional information.
boost::shared_ptr< CfgDbAccess > CfgDbAccessPtr
A pointer to the CfgDbAccess.
const isc::log::MessageID DHCPSRV_PGSQL_GET_ADDR6
std::string subnet_id_str_
static bool warned_about_tls
Emit the TLS support warning only once.
uint32_t toUint32() const
Converts IPv4 address to uint32_t.
const isc::log::MessageID DHCPSRV_PGSQL_GET_CLIENTID
bool isV6Zero() const
Convenience function to check if it is an IPv4 zero address.
virtual std::string getDescription() const
Returns description of the backend.
const isc::log::MessageID DHCPSRV_PGSQL_LEASE_DB_RECONNECT_ATTEMPT_FAILED
SubnetID subnet_id_
The subnet ID to which this data applies.
static std::string getDBVersion()
Local version of getDBVersion() class method.
A generic exception that is thrown when an unexpected error condition occurs.
virtual void updateLease6(const Lease6Ptr &lease6)
Updates IPv6 lease.
virtual uint64_t deleteExpiredReclaimedLeases4(const uint32_t secs)
Deletes all expired-reclaimed DHCPv4 leases.
const isc::log::MessageID DHCPSRV_PGSQL_GET_IAID_SUBID_DUID
boost::shared_ptr< const Element > ConstElementPtr
Structure that holds a lease for IPv6 address and/or prefix.
const isc::log::MessageID DHCPSRV_PGSQL_GET_PAGE6
PgSqlTaggedStatement & statement_
The query's prepared statement.
const isc::log::MessageID DHCPSRV_PGSQL_COMMIT
virtual size_t wipeLeases6(const SubnetID &subnet_id)
Removed specified IPv6 leases.
virtual LeaseStatsQueryPtr startLeaseStatsQuery4()
Creates and runs the IPv4 lease stats query.
const isc::log::MessageID DHCPSRV_PGSQL_GET_HWADDR
virtual void rollback()
Rollback Transactions.
std::string addr_str_
Common Instance members used for binding and conversion.
Common PgSql Connector Pool.
virtual ~PgSqlLeaseExchange()
static bool invokeDbRecoveredCallback(const util::ReconnectCtlPtr &db_reconnect_ctl)
Invokes the connection's restored connectivity callback.
std::vector< Lease6Ptr > Lease6Collection
A collection of IPv6 leases.
bool isV6() const
Convenience function to check for an IPv6 address.
virtual std::pair< uint32_t, uint32_t > getVersion() const
Returns backend version.
bool isV4() const
Convenience function to check for an IPv4 address.
const std::vector< uint8_t > & getDuid() const
Returns a const reference to the actual DUID value.
virtual void getExpiredLeases4(Lease4Collection &expired_leases, const size_t max_leases) const
Returns a collection of expired DHCPv4 leases.
Invalid address family used as input to Lease Manager.
Defines the logger used by the top-level component of kea-lfc.
const isc::log::MessageID DHCPSRV_PGSQL_GET6
Lease4Ptr convertFromDatabase(const PgSqlResult &r, int row)
Creates a Lease4 object from a given row in a result set.
Define a PostgreSQL statement.
void start()
Creates the lease statistical data result set.
static bool invokeDbFailedCallback(const util::ReconnectCtlPtr &db_reconnect_ctl)
Invokes the connection's restore failed connectivity callback.
size_t size() const
Fetches the number of entries in the array.
std::string toText() const
Convert the address to a string.
const isc::log::MessageID DHCPSRV_PGSQL_DELETE_ADDR
const isc::log::MessageID DHCPSRV_PGSQL_ADD_ADDR6
PgSqlConnection & conn_
Database connection to use to execute the query.
boost::shared_ptr< PgSqlLeaseContext > PgSqlLeaseContextPtr
Type of pointers to contexts.
static const uint32_t INFINITY_LFT
Infinity (means static, i.e. never expire)
virtual Lease6Collection getLeases6() const
Returns all IPv6 leases.
const isc::log::MessageID DHCPSRV_PGSQL_DELETE_EXPIRED_RECLAIMED4
const isc::log::MessageID DHCPSRV_PGSQL_UPDATE_ADDR6
const isc::log::MessageID DHCPSRV_PGSQL_GET_PAGE4
uint32_t lease_state_
The lease_state to which the count applies.
Type
Type of lease or pool.
Holds Client identifier or client IPv4 address.
static void create(const std::string &dbaccess)
Create an instance of a lease manager.
virtual Lease4Ptr getLease4(const isc::asiolink::IOAddress &addr) const
Returns an IPv4 lease for specified IPv4 address.
#define LOG_DEBUG(LOGGER, LEVEL, MESSAGE)
Macro to conveniently test debug output and log it.
const isc::log::MessageID DHCPSRV_PGSQL_GET_DUID
Uiaid(int32_t val)
Constructor.
StatementIndex
Statement Tags.
Hardware type that represents information from DHCPv4 packet.
PgSqlLeaseStatsQuery(PgSqlConnection &conn, PgSqlTaggedStatement &statement, const bool fetch_type, const SubnetID &first_subnet_id, const SubnetID &last_subnet_id)
Constructor to query for the stats for a range of subnets.
virtual bool addLease(const Lease4Ptr &lease)
Adds an IPv4 lease.
int64_t state_count_
state_count The count of leases in the lease state
isc::log::Logger dhcpsrv_logger("dhcpsrv")
DHCP server library Logger.
Contains a single row of lease statistical data.
const uint32_t PGSQL_SCHEMA_VERSION_MAJOR
Define PostgreSQL backend version: 12.0.
std::string toText() const
Returns textual representation of a DUID (e.g. 00:01:02:03:ff)
Wraps value holding size of the page with leases.
static std::pair< uint32_t, uint32_t > getVersion(const ParameterMap ¶meters)
Get the schema version.
const size_t OID_NONE
Constants for PostgreSQL data types These are defined by PostgreSQL in <catalog/pg_type.h>, but including this file is extraordinarily convoluted, so we'll use these to fill-in.
Lease::Type lease_type_
The lease_type to which the count applies.
virtual void commit()
Commit Transactions.
virtual bool deleteLease(const Lease4Ptr &lease)
Deletes an IPv4 lease.
virtual std::string getName() const
Returns backend name.
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.
virtual Lease6Ptr getLease6(Lease::Type type, const isc::asiolink::IOAddress &addr) const
Returns existing IPv6 lease for a given IPv6 address.
static const TimerMgrPtr & instance()
Returns pointer to the sole instance of the TimerMgr.
The IOAddress class represents an IP addresses (version agnostic)
virtual size_t wipeLeases4(const SubnetID &subnet_id)
Removes specified IPv4 leases.
static bool invokeDbLostCallback(const util::ReconnectCtlPtr &db_reconnect_ctl)
Invokes the connection's lost connectivity callback.
static const char * getRawColumnValue(const PgSqlResult &r, const int row, const size_t col)
Gets a pointer to the raw column value in a result set row.
const isc::log::MessageID DHCPSRV_PGSQL_UPDATE_ADDR4
virtual ~PgSqlLeaseStatsQuery()
Destructor.
Base PgSql derivation of the statistical lease data query.
boost::shared_ptr< Lease6 > Lease6Ptr
Pointer to a Lease6 structure.
static std::string redactedAccessString(const ParameterMap ¶meters)
Redact database access string.
virtual Lease4Collection getLeases4() const
Returns all IPv4 leases.
std::string dbInputString()
Return a string representing the signed 32-bit value.
PgSqlLeaseStatsQuery(PgSqlConnection &conn, PgSqlTaggedStatement &statement, const bool fetch_type)
Constructor to query for all subnets' stats.
const size_t OID_TIMESTAMP
virtual LeaseStatsQueryPtr startLeaseStatsQuery6()
Creates and runs the IPv6 lease stats query.
const isc::log::MessageID DHCPSRV_PGSQL_TLS_SUPPORT
Supports exchanging IPv6 leases with PostgreSQL.
std::string toText() const
Returns textual representation of a DUID (e.g. 00:01:02:03:ff)
Exception thrown on failure to execute a database function.
Supports exchanging IPv4 leases with PostgreSQL.
const isc::log::MessageID DHCPSRV_PGSQL_NEGATIVE_LEASES_STAT
boost::shared_ptr< IOServiceAccessor > IOServiceAccessorPtr
Pointer to an instance of IOServiceAccessor.
const isc::log::MessageID DHCPSRV_PGSQL_GET_IAID_DUID
const uint32_t PGSQL_SCHEMA_VERSION_MINOR
std::string toText(bool include_htype=true) const
Returns textual representation of a hardware address (e.g.
const isc::log::MessageID DHCPSRV_PGSQL_ADD_ADDR4
uint32_t SubnetID
Defines unique IPv4 or IPv6 subnet identifier.
bool fetch_type_
Indicates if query supplies lease type.
const isc::log::MessageID DHCPSRV_PGSQL_GET_SUBID_CLIENTID
PgSqlLeaseMgr(const db::DatabaseConnection::ParameterMap ¶meters)
Constructor.