d_cfg_mgr.cc 11 KB

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