config_parser.h 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. // Copyright (C) 2012 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 <exceptions/exceptions.h>
  15. #include <cc/data.h>
  16. #include <stdint.h>
  17. #include <string>
  18. #ifndef DHCP4_CONFIG_PARSER_H
  19. #define DHCP4_CONFIG_PARSER_H
  20. /// @todo: This header file and its .cc counterpart are very similar between
  21. /// DHCPv4 and DHCPv6. They should be merged. A ticket #2355.
  22. namespace isc {
  23. namespace dhcp {
  24. class Dhcpv4Srv;
  25. /// @brief a collection of elements that store uint32 values (e.g. renew-timer = 900)
  26. typedef std::map<std::string, uint32_t> Uint32Storage;
  27. /// @brief a collection of elements that store string values
  28. typedef std::map<std::string, std::string> StringStorage;
  29. /// An exception that is thrown if an error occurs while configuring an
  30. /// \c Dhcpv4Srv object.
  31. class Dhcp4ConfigError : public isc::Exception {
  32. public:
  33. /// @brief constructor
  34. ///
  35. /// @param file name of the file, where exception occurred
  36. /// @param line line of the file, where exception occurred
  37. /// @param what text description of the issue that caused exception
  38. Dhcp4ConfigError(const char* file, size_t line, const char* what)
  39. : isc::Exception(file, line, what) {}
  40. };
  41. /// @brief Base abstract class for all DHCPv4 parsers
  42. ///
  43. /// Each instance of a class derived from this class parses one specific config
  44. /// element. Sometimes elements are simple (e.g. a string) and sometimes quite
  45. /// complex (e.g. a subnet). In such case, it is likely that a parser will
  46. /// spawn child parsers to parse child elements in the configuration.
  47. /// @todo: Merge this class with DhcpConfigParser in src/bin/dhcp6
  48. class Dhcp4ConfigParser {
  49. ///
  50. /// \name Constructors and Destructor
  51. ///
  52. /// Note: The copy constructor and the assignment operator are
  53. /// intentionally defined as private to make it explicit that this is a
  54. /// pure base class.
  55. //@{
  56. private:
  57. // Private construtor and assignment operator assures that nobody
  58. // will be able to copy or assign a parser. There are no defined
  59. // bodies for them.
  60. Dhcp4ConfigParser(const Dhcp4ConfigParser& source);
  61. Dhcp4ConfigParser& operator=(const Dhcp4ConfigParser& source);
  62. protected:
  63. /// \brief The default constructor.
  64. ///
  65. /// This is intentionally defined as \c protected as this base class should
  66. /// never be instantiated (except as part of a derived class).
  67. Dhcp4ConfigParser() {}
  68. public:
  69. /// The destructor.
  70. virtual ~Dhcp4ConfigParser() {}
  71. //@}
  72. /// \brief Prepare configuration value.
  73. ///
  74. /// This method parses the "value part" of the configuration identifier
  75. /// that corresponds to this derived class and prepares a new value to
  76. /// apply to the server.
  77. ///
  78. /// This method must validate the given value both in terms of syntax
  79. /// and semantics of the configuration, so that the server will be
  80. /// validly configured at the time of \c commit(). Note: the given
  81. /// configuration value is normally syntactically validated, but the
  82. /// \c build() implementation must also expect invalid input. If it
  83. /// detects an error it may throw an exception of a derived class
  84. /// of \c isc::Exception.
  85. ///
  86. /// Preparing a configuration value will often require resource
  87. /// allocation. If it fails, it may throw a corresponding standard
  88. /// exception.
  89. ///
  90. /// This method is not expected to be called more than once in the
  91. /// life of the object. Although multiple calls are not prohibited
  92. /// by the interface, the behavior is undefined.
  93. ///
  94. /// \param config_value The configuration value for the identifier
  95. /// corresponding to the derived class.
  96. virtual void build(isc::data::ConstElementPtr config_value) = 0;
  97. /// \brief Apply the prepared configuration value to the server.
  98. ///
  99. /// This method is expected to be exception free, and, as a consequence,
  100. /// it should normally not involve resource allocation.
  101. /// Typically it would simply perform exception free assignment or swap
  102. /// operation on the value prepared in \c build().
  103. /// In some cases, however, it may be very difficult to meet this
  104. /// condition in a realistic way, while the failure case should really
  105. /// be very rare. In such a case it may throw, and, if the parser is
  106. /// called via \c configureDhcp4Server(), the caller will convert the
  107. /// exception as a fatal error.
  108. ///
  109. /// This method is expected to be called after \c build(), and only once.
  110. /// The result is undefined otherwise.
  111. virtual void commit() = 0;
  112. };
  113. /// @brief a pointer to configuration parser
  114. typedef boost::shared_ptr<Dhcp4ConfigParser> ParserPtr;
  115. /// @brief a collection of parsers
  116. ///
  117. /// This container is used to store pointer to parsers for a given scope.
  118. typedef std::vector<ParserPtr> ParserCollection;
  119. /// \brief Configure DHCPv4 server (\c Dhcpv4Srv) with a set of configuration values.
  120. ///
  121. /// This function parses configuration information stored in \c config_set
  122. /// and configures the \c server by applying the configuration to it.
  123. /// It provides the strong exception guarantee as long as the underlying
  124. /// derived class implementations of \c DhcpConfigParser meet the assumption,
  125. /// that is, it ensures that either configuration is fully applied or the
  126. /// state of the server is intact.
  127. ///
  128. /// If a syntax or semantics level error happens during the configuration
  129. /// (such as malformed configuration or invalid configuration parameter),
  130. /// this function returns appropriate error code.
  131. ///
  132. /// This function is called every time a new configuration is received. The extra
  133. /// parameter is a reference to DHCPv4 server component. It is currently not used
  134. /// and CfgMgr::instance() is accessed instead.
  135. ///
  136. /// This method does not throw. It catches all exceptions and returns them as
  137. /// reconfiguration statuses. It may return the following response codes:
  138. /// 0 - configuration successful
  139. /// 1 - malformed configuration (parsing failed)
  140. /// 2 - logical error (parsing was successful, but the values are invalid)
  141. ///
  142. /// @param config_set a new configuration (JSON) for DHCPv4 server
  143. /// @return answer that contains result of reconfiguration
  144. isc::data::ConstElementPtr
  145. configureDhcp4Server(Dhcpv4Srv&,
  146. isc::data::ConstElementPtr config_set);
  147. }; // end of isc::dhcp namespace
  148. }; // end of isc namespace
  149. #endif // DHCP4_CONFIG_PARSER_H