config_parser.h 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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. Dhcp6ConfigError(const char* file, size_t line, const char* what) :
  27. isc::Exception(file, line, what) {}
  28. };
  29. class Dhcp6ConfigParser {
  30. ///
  31. /// \name Constructors and Destructor
  32. ///
  33. /// Note: The copy constructor and the assignment operator are
  34. /// intentionally defined as private to make it explicit that this is a
  35. /// pure base class.
  36. //@{
  37. private:
  38. Dhcp6ConfigParser(const Dhcp6ConfigParser& source);
  39. Dhcp6ConfigParser& operator=(const Dhcp6ConfigParser& source);
  40. protected:
  41. /// \brief The default constructor.
  42. ///
  43. /// This is intentionally defined as \c protected as this base class should
  44. /// never be instantiated (except as part of a derived class).
  45. Dhcp6ConfigParser() {}
  46. public:
  47. /// The destructor.
  48. virtual ~Dhcp6ConfigParser() {}
  49. //@}
  50. /// Prepare configuration value.
  51. ///
  52. /// This method parses the "value part" of the configuration identifier
  53. /// that corresponds to this derived class and prepares a new value to
  54. /// apply to the server.
  55. /// In the above example, the derived class for the identifier "param1"
  56. /// would be passed an data \c Element storing an integer whose value
  57. /// is 10, and would record that value internally;
  58. /// the derived class for the identifier "param2" would be passed a
  59. /// map element and (after parsing) convert it into some internal
  60. /// data structure.
  61. ///
  62. /// This method must validate the given value both in terms of syntax
  63. /// and semantics of the configuration, so that the server will be
  64. /// validly configured at the time of \c commit(). Note: the given
  65. /// configuration value is normally syntactically validated, but the
  66. /// \c build() implementation must also expect invalid input. If it
  67. /// detects an error it may throw an exception of a derived class
  68. /// of \c isc::Exception.
  69. ///
  70. /// Preparing a configuration value will often require resource
  71. /// allocation. If it fails, it may throw a corresponding standard
  72. /// exception.
  73. ///
  74. /// This method is not expected to be called more than once. Although
  75. /// multiple calls are not prohibited by the interface, the behavior
  76. /// is undefined.
  77. ///
  78. /// \param config_value The configuration value for the identifier
  79. /// corresponding to the derived class.
  80. virtual void build(isc::data::ConstElementPtr config_value) = 0;
  81. /// Apply the prepared configuration value to the server.
  82. ///
  83. /// This method is expected to be exception free, and, as a consequence,
  84. /// it should normally not involve resource allocation.
  85. /// Typically it would simply perform exception free assignment or swap
  86. /// operation on the value prepared in \c build().
  87. /// In some cases, however, it may be very difficult to meet this
  88. /// condition in a realistic way, while the failure case should really
  89. /// be very rare. In such a case it may throw, and, if the parser is
  90. /// called via \c configureDhcp6Server(), the caller will convert the
  91. /// exception as a fatal error.
  92. ///
  93. /// This method is expected to be called after \c build(), and only once.
  94. /// The result is undefined otherwise.
  95. virtual void commit() = 0;
  96. };
  97. /// Configure an \c Dhcpv6Srv object with a set of configuration values.
  98. ///
  99. /// This function parses configuration information stored in \c config_set
  100. /// and configures the \c server by applying the configuration to it.
  101. /// It provides the strong exception guarantee as long as the underlying
  102. /// derived class implementations of \c Dhcp6ConfigParser meet the assumption,
  103. /// that is, it ensures that either configuration is fully applied or the
  104. /// state of the server is intact.
  105. ///
  106. /// If a syntax or semantics level error happens during the configuration
  107. /// (such as malformed configuration or invalid configuration parameter),
  108. /// this function throws an exception of class \c Dhcp6ConfigError.
  109. /// If the given configuration requires resource allocation and it fails,
  110. /// a corresponding standard exception will be thrown.
  111. /// Other exceptions may also be thrown, depending on the implementation of
  112. /// the underlying derived class of \c Dhcp6ConfigError.
  113. /// In any case the strong guarantee is provided as described above except
  114. /// in the very rare cases where the \c commit() method of a parser throws
  115. /// an exception. If that happens this function converts the exception
  116. /// into a \c FatalError exception and rethrows it. This exception is
  117. /// expected to be caught at the highest level of the application to terminate
  118. /// the program gracefully.
  119. ///
  120. /// \param server The \c Dhcpv6Srv object to be configured.
  121. /// \param config_set A JSON style configuration to apply to \c server.
  122. void configureDhcp6Server(Dhcpv6Srv& server,
  123. isc::data::ConstElementPtr config_set);
  124. /// Create a new \c Dhcp6ConfigParser object for a given configuration
  125. /// identifier.
  126. ///
  127. /// It internally identifies an appropriate derived class for the given
  128. /// identifier and creates a new instance of that class. The caller can
  129. /// then configure the \c server regarding the identifier by calling
  130. /// the \c build() and \c commit() methods of the returned object.
  131. ///
  132. /// In practice, this function is only expected to be used as a backend of
  133. /// \c configureDhcp6Server() and is not supposed to be called directly
  134. /// by applications. It is publicly available mainly for testing purposes.
  135. /// When called directly, the created object must be deleted by the caller.
  136. /// Note: this means if this module and the caller use incompatible sets of
  137. /// new/delete, it may cause unexpected strange failure. We could avoid that
  138. /// by providing a separate deallocation function or by using a smart pointer,
  139. /// but since the expected usage of this function is very limited (i.e. for
  140. /// our own testing purposes) it would be an overkilling. We therefore prefer
  141. /// simplicity and keeping the interface intuitive.
  142. ///
  143. /// If the resource allocation for the new object fails, a corresponding
  144. /// standard exception will be thrown. Otherwise this function is not
  145. /// expected to throw an exception, unless the constructor of the underlying
  146. /// derived class implementation (unexpectedly) throws.
  147. ///
  148. /// \param server The \c Dhcpv6Srv object to be configured.
  149. /// \param config_id The configuration identifier for which a parser object
  150. /// is to be created.
  151. /// \return A pointer to an \c Dhcp6ConfigParser object.
  152. Dhcp6ConfigParser* createDhcp6ConfigParser(Dhcpv6Srv& server,
  153. const std::string& config_id);
  154. }; // end of isc::dhcp namespace
  155. }; // end of isc namespace
  156. #endif // DHCP6_CONFIG_PARSER_H