cfg_expiration.cc 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. // Copyright (C) 2015,2017 Internet Systems Consortium, Inc. ("ISC")
  2. //
  3. // This Source Code Form is subject to the terms of the Mozilla Public
  4. // License, v. 2.0. If a copy of the MPL was not distributed with this
  5. // file, You can obtain one at http://mozilla.org/MPL/2.0/.
  6. #include <dhcpsrv/cfg_expiration.h>
  7. #include <exceptions/exceptions.h>
  8. #include <limits>
  9. using namespace isc::data;
  10. namespace isc {
  11. namespace dhcp {
  12. // Default values
  13. const uint16_t CfgExpiration::DEFAULT_RECLAIM_TIMER_WAIT_TIME = 10;
  14. const uint16_t CfgExpiration::DEFAULT_FLUSH_RECLAIMED_TIMER_WAIT_TIME = 25;
  15. const uint32_t CfgExpiration::DEFAULT_HOLD_RECLAIMED_TIME = 3600;
  16. const uint32_t CfgExpiration::DEFAULT_MAX_RECLAIM_LEASES = 100;
  17. const uint16_t CfgExpiration::DEFAULT_MAX_RECLAIM_TIME = 250;
  18. const uint16_t CfgExpiration::DEFAULT_UNWARNED_RECLAIM_CYCLES = 5;
  19. // Maximum values.
  20. const uint16_t CfgExpiration::LIMIT_RECLAIM_TIMER_WAIT_TIME =
  21. std::numeric_limits<uint16_t>::max();
  22. const uint16_t CfgExpiration::LIMIT_FLUSH_RECLAIMED_TIMER_WAIT_TIME =
  23. std::numeric_limits<uint16_t>::max();
  24. const uint32_t CfgExpiration::LIMIT_HOLD_RECLAIMED_TIME =
  25. std::numeric_limits<uint32_t>::max();
  26. const uint32_t CfgExpiration::LIMIT_MAX_RECLAIM_LEASES =
  27. std::numeric_limits<uint32_t>::max();
  28. const uint16_t CfgExpiration::LIMIT_MAX_RECLAIM_TIME = 10000;
  29. const uint16_t CfgExpiration::LIMIT_UNWARNED_RECLAIM_CYCLES =
  30. std::numeric_limits<uint16_t>::max();
  31. // Timers' names
  32. const std::string CfgExpiration::RECLAIM_EXPIRED_TIMER_NAME =
  33. "reclaim-expired-leases";
  34. const std::string CfgExpiration::FLUSH_RECLAIMED_TIMER_NAME =
  35. "flush-reclaimed-leases";
  36. CfgExpiration::CfgExpiration(const bool test_mode)
  37. : reclaim_timer_wait_time_(DEFAULT_RECLAIM_TIMER_WAIT_TIME),
  38. flush_reclaimed_timer_wait_time_(DEFAULT_FLUSH_RECLAIMED_TIMER_WAIT_TIME),
  39. hold_reclaimed_time_(DEFAULT_HOLD_RECLAIMED_TIME),
  40. max_reclaim_leases_(DEFAULT_MAX_RECLAIM_LEASES),
  41. max_reclaim_time_(DEFAULT_MAX_RECLAIM_TIME),
  42. unwarned_reclaim_cycles_(DEFAULT_UNWARNED_RECLAIM_CYCLES),
  43. timer_mgr_(TimerMgr::instance()),
  44. test_mode_(test_mode) {
  45. }
  46. void
  47. CfgExpiration::setReclaimTimerWaitTime(const int64_t reclaim_timer_wait_time) {
  48. rangeCheck(reclaim_timer_wait_time, LIMIT_RECLAIM_TIMER_WAIT_TIME,
  49. "reclaim-timer-wait-time");
  50. reclaim_timer_wait_time_ = reclaim_timer_wait_time;
  51. }
  52. void
  53. CfgExpiration::setFlushReclaimedTimerWaitTime(const int64_t flush_reclaimed_wait_time) {
  54. rangeCheck(flush_reclaimed_wait_time, LIMIT_FLUSH_RECLAIMED_TIMER_WAIT_TIME,
  55. "flush-reclaimed-timer-wait-time");
  56. flush_reclaimed_timer_wait_time_ = flush_reclaimed_wait_time;
  57. }
  58. void
  59. CfgExpiration::setHoldReclaimedTime(const int64_t hold_reclaimed_time) {
  60. rangeCheck(hold_reclaimed_time, LIMIT_HOLD_RECLAIMED_TIME, "hold-reclaimed-time");
  61. hold_reclaimed_time_ = hold_reclaimed_time;
  62. }
  63. void
  64. CfgExpiration::setMaxReclaimLeases(const int64_t max_reclaim_leases) {
  65. rangeCheck(max_reclaim_leases, LIMIT_MAX_RECLAIM_LEASES, "max-reclaim-leases");
  66. max_reclaim_leases_ = max_reclaim_leases;
  67. }
  68. void
  69. CfgExpiration::setMaxReclaimTime(const int64_t max_reclaim_time) {
  70. rangeCheck(max_reclaim_time, LIMIT_MAX_RECLAIM_TIME, "max-reclaim-time");
  71. max_reclaim_time_ = max_reclaim_time;
  72. }
  73. void
  74. CfgExpiration::setUnwarnedReclaimCycles(const int64_t unwarned_reclaim_cycles) {
  75. rangeCheck(unwarned_reclaim_cycles, LIMIT_UNWARNED_RECLAIM_CYCLES,
  76. "unwarned-reclaim-cycles");
  77. unwarned_reclaim_cycles_ = unwarned_reclaim_cycles;
  78. }
  79. void
  80. CfgExpiration::rangeCheck(const int64_t value, const uint64_t max_value,
  81. const std::string& config_parameter_name) const {
  82. if (value < 0) {
  83. isc_throw(OutOfRange, "value for configuration parameter '"
  84. << config_parameter_name << "' must not be negative");
  85. } else if (value > max_value) {
  86. isc_throw(OutOfRange, "out range value '" << value << "' for configuration"
  87. " parameter '" << config_parameter_name << "', expected maximum"
  88. " value of '" << max_value << "'");
  89. }
  90. }
  91. ElementPtr
  92. CfgExpiration::toElement() const {
  93. ElementPtr result = Element::createMap();
  94. // Set reclaim-timer-wait-time
  95. result->set("reclaim-timer-wait-time",
  96. Element::create(static_cast<long long>
  97. (reclaim_timer_wait_time_)));
  98. // Set flush-reclaimed-timer-wait-time
  99. result->set("flush-reclaimed-timer-wait-time",
  100. Element::create(static_cast<long long>
  101. (flush_reclaimed_timer_wait_time_)));
  102. // Set hold-reclaimed-time
  103. result->set("hold-reclaimed-time",
  104. Element::create(static_cast<long long>
  105. (hold_reclaimed_time_)));
  106. // Set max-reclaim-leases
  107. result->set("max-reclaim-leases",
  108. Element::create(static_cast<long long>
  109. (max_reclaim_leases_)));
  110. // Set max-reclaim-time
  111. result->set("max-reclaim-time",
  112. Element::create(static_cast<long long>
  113. (max_reclaim_time_)));
  114. // Set unwarned-reclaim-cycles
  115. result->set("unwarned-reclaim-cycles",
  116. Element::create(static_cast<long long>
  117. (unwarned_reclaim_cycles_)));
  118. return (result);
  119. }
  120. }
  121. }