simple_parser.cc 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. // Copyright (C) 2017 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 <agent/simple_parser.h>
  7. #include <cc/data.h>
  8. #include <cc/dhcp_config_error.h>
  9. #include <hooks/hooks_parser.h>
  10. #include <boost/foreach.hpp>
  11. using namespace isc::data;
  12. namespace isc {
  13. namespace agent {
  14. /// @brief This sets of arrays define the default values in various scopes
  15. /// of the Control Agent Configuration.
  16. ///
  17. /// Each of those is documented in @file agent/simple_parser.cc. This
  18. /// is different than most other comments in Kea code. The reason
  19. /// for placing those in .cc rather than .h file is that it
  20. /// is expected to be one centralized place to look at for
  21. /// the default values. This is expected to be looked at also by
  22. /// people who are not skilled in C or C++, so they may be
  23. /// confused with the differences between declaration and definition.
  24. /// As such, there's one file to look at that hopefully is readable
  25. /// without any C or C++ skills.
  26. ///
  27. /// @{
  28. /// @brief This table defines default values for global options.
  29. ///
  30. /// These are global Control Agent parameters.
  31. const SimpleDefaults AgentSimpleParser::AGENT_DEFAULTS = {
  32. { "http-host", Element::string, "127.0.0.1"},
  33. { "http-port", Element::integer, "8000"}
  34. };
  35. /// @brief This table defines default values for control sockets.
  36. ///
  37. const SimpleDefaults AgentSimpleParser::SOCKET_DEFAULTS = {
  38. { "socket-type", Element::string, "unix"}
  39. };
  40. /// @}
  41. /// ---------------------------------------------------------------------------
  42. /// --- end of default values -------------------------------------------------
  43. /// ---------------------------------------------------------------------------
  44. size_t AgentSimpleParser::setAllDefaults(const isc::data::ElementPtr& global) {
  45. size_t cnt = 0;
  46. // Set global defaults first.
  47. cnt = setDefaults(global, AGENT_DEFAULTS);
  48. // Now set the defaults for control-sockets, if any.
  49. ConstElementPtr sockets = global->get("control-sockets");
  50. if (sockets) {
  51. ElementPtr d2 = boost::const_pointer_cast<Element>(sockets->get("d2-server"));
  52. if (d2) {
  53. cnt += SimpleParser::setDefaults(d2, SOCKET_DEFAULTS);
  54. }
  55. ElementPtr d4 = boost::const_pointer_cast<Element>(sockets->get("dhcp4-server"));
  56. if (d4) {
  57. cnt += SimpleParser::setDefaults(d4, SOCKET_DEFAULTS);
  58. }
  59. ElementPtr d6 = boost::const_pointer_cast<Element>(sockets->get("dhcp6-server"));
  60. if (d6) {
  61. cnt += SimpleParser::setDefaults(d6, SOCKET_DEFAULTS);
  62. }
  63. }
  64. return (cnt);
  65. }
  66. void
  67. AgentSimpleParser::parse(const CtrlAgentCfgContextPtr& ctx,
  68. const isc::data::ConstElementPtr& config,
  69. bool check_only) {
  70. // Let's get the HTTP parameters first.
  71. ctx->setHttpHost(SimpleParser::getString(config, "http-host"));
  72. ctx->setHttpPort(SimpleParser::getIntType<uint16_t>(config, "http-port"));
  73. // Control sockets are second.
  74. ConstElementPtr ctrl_sockets = config->get("control-sockets");
  75. if (ctrl_sockets) {
  76. ConstElementPtr d2_socket = ctrl_sockets->get("d2-server");
  77. ConstElementPtr d4_socket = ctrl_sockets->get("dhcp4-server");
  78. ConstElementPtr d6_socket = ctrl_sockets->get("dhcp6-server");
  79. if (d2_socket) {
  80. ctx->setControlSocketInfo(d2_socket, CtrlAgentCfgContext::TYPE_D2);
  81. }
  82. if (d4_socket) {
  83. ctx->setControlSocketInfo(d4_socket, CtrlAgentCfgContext::TYPE_DHCP4);
  84. }
  85. if (d6_socket) {
  86. ctx->setControlSocketInfo(d6_socket, CtrlAgentCfgContext::TYPE_DHCP6);
  87. }
  88. }
  89. // Finally, let's get the hook libs!
  90. using namespace isc::hooks;
  91. HooksConfig& libraries = ctx->getHooksConfig();
  92. ConstElementPtr hooks = config->get("hooks-libraries");
  93. if (hooks) {
  94. HooksLibrariesParser hooks_parser;
  95. hooks_parser.parse(libraries, hooks);
  96. libraries.verifyLibraries(hooks->getPosition());
  97. }
  98. if (!check_only) {
  99. // This occurs last as if it succeeds, there is no easy way
  100. // revert it. As a result, the failure to commit a subsequent
  101. // change causes problems when trying to roll back.
  102. libraries.loadLibraries();
  103. }
  104. }
  105. };
  106. };