d_cfg_mgr.cc 11 KB

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