dbaccess_parser.cc 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. // Copyright (C) 2012-2014 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 <dhcp/option.h>
  15. #include <dhcpsrv/dbaccess_parser.h>
  16. #include <dhcpsrv/dhcpsrv_log.h>
  17. #include <dhcpsrv/lease_mgr_factory.h>
  18. #include <boost/foreach.hpp>
  19. #include <map>
  20. #include <string>
  21. #include <utility>
  22. using namespace std;
  23. using namespace isc::data;
  24. namespace isc {
  25. namespace dhcp {
  26. // Factory function to build the parser
  27. DbAccessParser::DbAccessParser(const std::string&, const ParserContext& ctx)
  28. : values_(), ctx_(ctx)
  29. {
  30. }
  31. // Parse the configuration and check that the various keywords are consistent.
  32. void
  33. DbAccessParser::build(isc::data::ConstElementPtr config_value) {
  34. // To cope with incremental updates, the strategy is:
  35. // 1. Take a copy of the stored keyword/value pairs.
  36. // 2. Inject the universe parameter.
  37. // 3. Update the copy with the passed keywords.
  38. // 4. Perform validation checks on the updated keyword/value pairs.
  39. // 5. 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. Inject the parameter which defines whether we are configuring
  43. // DHCPv4 or DHCPv6. Some database backends (e.g. Memfile make
  44. // use of it).
  45. values_copy["universe"] = ctx_.universe_ == Option::V4 ? "4" : "6";
  46. // 3. Update the copy with the passed keywords.
  47. BOOST_FOREACH(ConfigPair param, config_value->mapValue()) {
  48. // The persist parameter is the only boolean parameter at the
  49. // moment. It needs special handling.
  50. if (param.first != "persist") {
  51. values_copy[param.first] = param.second->stringValue();
  52. } else {
  53. values_copy[param.first] = (param.second->boolValue() ?
  54. "true" : "false");
  55. }
  56. }
  57. // 4. Perform validation checks on the updated set of keyword/values.
  58. //
  59. // a. Check if the "type" keyword exists and thrown an exception if not.
  60. StringPairMap::const_iterator type_ptr = values_copy.find("type");
  61. if (type_ptr == values_copy.end()) {
  62. isc_throw(TypeKeywordMissing, "lease database access parameters must "
  63. "include the keyword 'type' to determine type of database "
  64. "to be accessed");
  65. }
  66. // b. Check if the 'type; keyword known and throw an exception if not.
  67. string dbtype = type_ptr->second;
  68. if ((dbtype != "memfile") && (dbtype != "mysql")) {
  69. isc_throw(BadValue, "unknown backend database type: " << dbtype);
  70. }
  71. // 5. If all is OK, update the stored keyword/value pairs. We do this by
  72. // swapping contents - values_copy is destroyed immediately after the
  73. // operation (when the method exits), so we are not interested in its new
  74. // value.
  75. values_.swap(values_copy);
  76. }
  77. // Create the database access string
  78. std::string
  79. DbAccessParser::getDbAccessString() const {
  80. // Construct the database access string from all keywords and values in the
  81. // parameter map where the value is not null.
  82. string dbaccess;
  83. BOOST_FOREACH(StringPair keyval, values_) {
  84. if (!keyval.second.empty()) {
  85. // Separate keyword/value pair from predecessor (if there is one).
  86. if (!dbaccess.empty()) {
  87. dbaccess += std::string(" ");
  88. }
  89. // Add the keyword/value pair to the access string.
  90. dbaccess += (keyval.first + std::string("=") + keyval.second);
  91. }
  92. }
  93. return (dbaccess);
  94. }
  95. // Commit the changes - reopen the database with the new parameters
  96. void
  97. DbAccessParser::commit() {
  98. // Close current lease manager database.
  99. LeaseMgrFactory::destroy();
  100. // ... and open the new database using the access string.
  101. LeaseMgrFactory::create(getDbAccessString());
  102. }
  103. }; // namespace dhcp
  104. }; // namespace isc