config_parser.h 6.1 KB

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