config_parser.h 6.7 KB

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