dbaccess_parser.cc 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. // Copyright (C) 2012-2013 Internet Systems Consortium, Inc. ("ISC")
  2. //
  3. // Permission to use, copy, modify, and/or distribute this software for any
  4. // purpose with or without fee is hereby granted, provided that the above
  5. // copyright notice and this permission notice appear in all copies.
  6. //
  7. // THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
  8. // REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
  9. // AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
  10. // INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
  11. // LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
  12. // OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  13. // PERFORMANCE OF THIS SOFTWARE.
  14. #include <dhcpsrv/dbaccess_parser.h>
  15. #include <dhcpsrv/dhcpsrv_log.h>
  16. #include <dhcpsrv/lease_mgr_factory.h>
  17. #include <boost/foreach.hpp>
  18. #include <map>
  19. #include <string>
  20. #include <utility>
  21. using namespace std;
  22. using namespace isc::data;
  23. namespace isc {
  24. namespace dhcp {
  25. // Factory function to build the parser
  26. DbAccessParser::DbAccessParser(const std::string& param_name) : values_()
  27. {
  28. if (param_name != "lease-database") {
  29. LOG_WARN(dhcpsrv_logger, DHCPSRV_UNEXPECTED_NAME).arg(param_name);
  30. }
  31. }
  32. // Parse the configuration and check that the various keywords are consistent.
  33. void
  34. DbAccessParser::build(isc::data::ConstElementPtr config_value) {
  35. // To cope with incremental updates, the strategy is:
  36. // 1. Take a copy of the stored keyword/value pairs.
  37. // 2. Update the copy with the passed keywords.
  38. // 3. Perform validation checks on the updated keyword/value pairs.
  39. // 4. If all is OK, update the stored keyword/value pairs.
  40. // 1. Take a copy of the stored keyword/value pairs.
  41. std::map<string, string> values_copy = values_;
  42. // 2. Update the copy with the passed keywords.
  43. BOOST_FOREACH(ConfigPair param, config_value->mapValue()) {
  44. values_copy[param.first] = param.second->stringValue();
  45. }
  46. // 3. Perform validation checks on the updated set of keyword/values.
  47. //
  48. // a. Check if the "type" keyword exists and thrown an exception if not.
  49. StringPairMap::const_iterator type_ptr = values_copy.find("type");
  50. if (type_ptr == values_copy.end()) {
  51. isc_throw(TypeKeywordMissing, "lease database access parameters must "
  52. "include the keyword 'type' to determine type of database "
  53. "to be accessed");
  54. }
  55. // b. Check if the 'type; keyword known and throw an exception if not.
  56. string dbtype = type_ptr->second;
  57. if ((dbtype != "memfile") && (dbtype != "mysql")) {
  58. isc_throw(BadValue, "unknown backend database type: " << dbtype);
  59. }
  60. // 4. If all is OK, update the stored keyword/value pairs. We do this by
  61. // swapping contents - values_copy is destroyed immediately after the
  62. // operation (when the method exits), so we are not interested in its new
  63. // value.
  64. values_.swap(values_copy);
  65. }
  66. // Create the database access string
  67. std::string
  68. DbAccessParser::getDbAccessString() const {
  69. // Construct the database access string from all keywords and values in the
  70. // parameter map where the value is not null.
  71. string dbaccess;
  72. BOOST_FOREACH(StringPair keyval, values_) {
  73. if (!keyval.second.empty()) {
  74. // Separate keyword/value pair from predecessor (if there is one).
  75. if (!dbaccess.empty()) {
  76. dbaccess += std::string(" ");
  77. }
  78. // Add the keyword/value pair to the access string.
  79. dbaccess += (keyval.first + std::string("=") + keyval.second);
  80. }
  81. }
  82. return (dbaccess);
  83. }
  84. // Commit the changes - reopen the database with the new parameters
  85. void
  86. DbAccessParser::commit() {
  87. // Close current lease manager database.
  88. LeaseMgrFactory::destroy();
  89. // ... and open the new database using the access string.
  90. LeaseMgrFactory::create(getDbAccessString());
  91. }
  92. }; // namespace dhcp
  93. }; // namespace isc