d_cfg_mgr.cc 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. // Copyright (C) 2013-2016 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 <config.h>
  7. #include <cc/command_interpreter.h>
  8. #include <dhcp/libdhcp++.h>
  9. #include <dhcpsrv/parsers/dhcp_parsers.h>
  10. #include <process/d_log.h>
  11. #include <process/d_cfg_mgr.h>
  12. #include <util/encode/hex.h>
  13. #include <util/strutil.h>
  14. #include <boost/foreach.hpp>
  15. #include <boost/lexical_cast.hpp>
  16. #include <boost/algorithm/string.hpp>
  17. #include <limits>
  18. #include <iostream>
  19. #include <vector>
  20. #include <map>
  21. using namespace std;
  22. using namespace isc;
  23. using namespace isc::dhcp;
  24. using namespace isc::data;
  25. using namespace isc::asiolink;
  26. namespace isc {
  27. namespace d2 {
  28. // *********************** DCfgContextBase *************************
  29. DCfgContextBase::DCfgContextBase():
  30. boolean_values_(new BooleanStorage()),
  31. uint32_values_(new Uint32Storage()),
  32. string_values_(new StringStorage()) {
  33. }
  34. DCfgContextBase::DCfgContextBase(const DCfgContextBase& rhs):
  35. boolean_values_(new BooleanStorage(*(rhs.boolean_values_))),
  36. uint32_values_(new Uint32Storage(*(rhs.uint32_values_))),
  37. string_values_(new StringStorage(*(rhs.string_values_))) {
  38. }
  39. const data::Element::Position&
  40. DCfgContextBase::getParam(const std::string& name, bool& value, bool optional) {
  41. try {
  42. value = boolean_values_->getParam(name);
  43. return (boolean_values_->getPosition(name));
  44. } catch (DhcpConfigError& ex) {
  45. // If the parameter is not optional, re-throw the exception.
  46. if (!optional) {
  47. throw;
  48. }
  49. }
  50. return (data::Element::ZERO_POSITION());
  51. }
  52. const data::Element::Position&
  53. DCfgContextBase::getParam(const std::string& name, uint32_t& value,
  54. bool optional) {
  55. try {
  56. value = uint32_values_->getParam(name);
  57. return (uint32_values_->getPosition(name));
  58. } catch (DhcpConfigError& ex) {
  59. // If the parameter is not optional, re-throw the exception.
  60. if (!optional) {
  61. throw;
  62. }
  63. }
  64. return (data::Element::ZERO_POSITION());
  65. }
  66. const data::Element::Position&
  67. DCfgContextBase::getParam(const std::string& name, std::string& value,
  68. bool optional) {
  69. try {
  70. value = string_values_->getParam(name);
  71. return (string_values_->getPosition(name));
  72. } catch (DhcpConfigError& ex) {
  73. // If the parameter is not optional, re-throw the exception.
  74. if (!optional) {
  75. throw;
  76. }
  77. }
  78. return (data::Element::ZERO_POSITION());
  79. }
  80. DCfgContextBase::~DCfgContextBase() {
  81. }
  82. // *********************** DCfgMgrBase *************************
  83. DCfgMgrBase::DCfgMgrBase(DCfgContextBasePtr context)
  84. : parse_order_() {
  85. setContext(context);
  86. }
  87. DCfgMgrBase::~DCfgMgrBase() {
  88. }
  89. void
  90. DCfgMgrBase::resetContext() {
  91. DCfgContextBasePtr context = createNewContext();
  92. setContext(context);
  93. }
  94. void
  95. DCfgMgrBase::setContext(DCfgContextBasePtr& context) {
  96. if (!context) {
  97. isc_throw(DCfgMgrBaseError, "DCfgMgrBase: context cannot be NULL");
  98. }
  99. context_ = context;
  100. }
  101. isc::data::ConstElementPtr
  102. DCfgMgrBase::parseConfig(isc::data::ConstElementPtr config_set) {
  103. LOG_DEBUG(dctl_logger, DBGLVL_COMMAND,
  104. DCTL_CONFIG_START).arg(config_set->str());
  105. if (!config_set) {
  106. return (isc::config::createAnswer(1,
  107. std::string("Can't parse NULL config")));
  108. }
  109. // The parsers implement data inheritance by directly accessing
  110. // configuration context. For this reason the data parsers must store
  111. // the parsed data into context immediately. This may cause data
  112. // inconsistency if the parsing operation fails after the context has been
  113. // modified. We need to preserve the original context here
  114. // so as we can rollback changes when an error occurs.
  115. DCfgContextBasePtr original_context = context_;
  116. resetContext();
  117. // Answer will hold the result returned to the caller.
  118. ConstElementPtr answer;
  119. // Holds the name of the element being parsed.
  120. std::string element_id;
  121. try {
  122. // Split the configuration into two maps. The first containing only
  123. // top-level scalar parameters (i.e. globals), the second containing
  124. // non-scalar or object elements (maps, lists, etc...). This allows
  125. // us to parse and validate all of the global values before we do
  126. // objects which may depend on them.
  127. ElementMap params_map;
  128. ElementMap objects_map;
  129. isc::dhcp::ConfigPair config_pair;
  130. BOOST_FOREACH(config_pair, config_set->mapValue()) {
  131. std::string element_id = config_pair.first;
  132. isc::data::ConstElementPtr element = config_pair.second;
  133. switch (element->getType()) {
  134. case isc::data::Element::integer:
  135. case isc::data::Element::real:
  136. case isc::data::Element::boolean:
  137. case isc::data::Element::string:
  138. params_map[element_id] = element;
  139. break;
  140. default:
  141. objects_map[element_id] = element;
  142. break;
  143. }
  144. }
  145. // Parse the global, scalar parameters. These are "committed" to
  146. // the context to make them available during object parsing.
  147. boost::shared_ptr<MapElement> params_config(new MapElement());
  148. params_config->setValue(params_map);
  149. buildParams(params_config);
  150. // Now parse the configuration objects.
  151. // Use a pre-ordered list of element ids to parse the elements in a
  152. // specific order if the list (parser_order_) is not empty; otherwise
  153. // elements are parsed in the order the value_map presents them.
  154. if (!parse_order_.empty()) {
  155. // For each element_id in the parse order list, look for it in the
  156. // value map. If the element exists in the map, pass it and it's
  157. // associated data in for parsing.
  158. // If there is no matching entry in the value map an error is
  159. // thrown. Note, that elements tagged as "optional" from the user
  160. // perspective must still have default or empty entries in the
  161. // configuration set to be parsed.
  162. std::map<std::string, ConstElementPtr>::iterator it;
  163. BOOST_FOREACH(element_id, parse_order_) {
  164. it = objects_map.find(element_id);
  165. if (it != objects_map.end()) {
  166. buildAndCommit(element_id, it->second);
  167. // We parsed it, take it out of the list.
  168. objects_map.erase(it);
  169. }
  170. else {
  171. isc_throw(DCfgMgrBaseError,
  172. "Element required by parsing order is missing: "
  173. << element_id << " ("
  174. << config_set->getPosition() << ")");
  175. }
  176. }
  177. // NOTE: When using ordered parsing, the parse order list MUST
  178. // include every possible element id that the value_map may contain.
  179. // Entries in the map that are not in the parse order, will not be
  180. // parsed. For now we will flag this as a programmatic error. One
  181. // could attempt to adjust for this, by identifying such entries
  182. // and parsing them either first or last but which would be correct?
  183. // Better to hold the engineer accountable. So, if there are any
  184. // left in the objects_map then they were not in the parse order.
  185. if (!objects_map.empty()) {
  186. std::ostringstream stream;
  187. bool add_comma = false;
  188. ConfigPair config_pair;
  189. BOOST_FOREACH(config_pair, objects_map) {
  190. stream << ( add_comma ? ", " : "") << config_pair.first
  191. << " (" << config_pair.second->getPosition() << ")";
  192. add_comma = true;
  193. }
  194. isc_throw(DCfgMgrBaseError,
  195. "Configuration contains elements not in parse order: "
  196. << stream.str());
  197. }
  198. } else {
  199. // Order doesn't matter so iterate over the value map directly.
  200. // Pass each element and it's associated data in to be parsed.
  201. ConfigPair config_pair;
  202. BOOST_FOREACH(config_pair, objects_map) {
  203. element_id = config_pair.first;
  204. buildAndCommit(element_id, config_pair.second);
  205. }
  206. }
  207. // Everything was fine. Configuration set processed successfully.
  208. LOG_INFO(dctl_logger, DCTL_CONFIG_COMPLETE).arg(getConfigSummary(0));
  209. answer = isc::config::createAnswer(0, "Configuration committed.");
  210. } catch (const std::exception& ex) {
  211. LOG_ERROR(dctl_logger, DCTL_PARSER_FAIL).arg(ex.what());
  212. answer = isc::config::createAnswer(1, ex.what());
  213. // An error occurred, so make sure that we restore original context.
  214. context_ = original_context;
  215. return (answer);
  216. }
  217. return (answer);
  218. }
  219. void
  220. DCfgMgrBase::buildParams(isc::data::ConstElementPtr params_config) {
  221. // Loop through scalars parsing them and committing them to storage.
  222. BOOST_FOREACH(dhcp::ConfigPair param, params_config->mapValue()) {
  223. // Call derivation's method to create the proper parser.
  224. dhcp::ParserPtr parser(createConfigParser(param.first,
  225. param.second->getPosition()));
  226. parser->build(param.second);
  227. parser->commit();
  228. }
  229. }
  230. void DCfgMgrBase::buildAndCommit(std::string& element_id,
  231. isc::data::ConstElementPtr value) {
  232. // Call derivation's implementation to create the appropriate parser
  233. // based on the element id.
  234. ParserPtr parser = createConfigParser(element_id, value->getPosition());
  235. if (!parser) {
  236. isc_throw(DCfgMgrBaseError, "Could not create parser");
  237. }
  238. // Invoke the parser's build method passing in the value. This will
  239. // "convert" the Element form of value into the actual data item(s)
  240. // and store them in parser's local storage.
  241. parser->build(value);
  242. // Invoke the parser's commit method. This "writes" the data
  243. // item(s) stored locally by the parser into the context. (Note that
  244. // parsers are free to do more than update the context, but that is an
  245. // nothing something we are concerned with here.)
  246. parser->commit();
  247. }
  248. }; // end of isc::dhcp namespace
  249. }; // end of isc namespace