dhcp_config_parser.h 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. // Copyright (C) 2013 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. #ifndef DHCP_CONFIG_PARSER_H
  15. #define DHCP_CONFIG_PARSER_H
  16. #include <exceptions/exceptions.h>
  17. #include <cc/data.h>
  18. #include <stdint.h>
  19. #include <string>
  20. #include <map>
  21. namespace isc {
  22. namespace dhcp {
  23. /// An exception that is thrown if an error occurs while configuring
  24. /// DHCP server.
  25. class DhcpConfigError : public isc::Exception {
  26. public:
  27. /// @brief constructor
  28. ///
  29. /// @param file name of the file, where exception occurred
  30. /// @param line line of the file, where exception occurred
  31. /// @param what text description of the issue that caused exception
  32. DhcpConfigError(const char* file, size_t line, const char* what)
  33. : isc::Exception(file, line, what) {}
  34. };
  35. /// @brief Forward declaration to DhcpConfigParser class.
  36. ///
  37. /// It is only needed here to define types that are
  38. /// based on this class before the class definition.
  39. class DhcpConfigParser;
  40. /// @brief a pointer to configuration parser
  41. typedef boost::shared_ptr<DhcpConfigParser> ParserPtr;
  42. /// @brief Collection of parsers.
  43. ///
  44. /// This container is used to store pointer to parsers for a given scope.
  45. typedef std::vector<ParserPtr> ParserCollection;
  46. /// @brief Combination of parameter name and configuration contents
  47. typedef std::pair<std::string, isc::data::ConstElementPtr> ConfigPair;
  48. /// @brief Base abstract class for all DHCP parsers
  49. ///
  50. /// Each instance of a class derived from this class parses one specific config
  51. /// element. Sometimes elements are simple (e.g. a string) and sometimes quite
  52. /// complex (e.g. a subnet). In such case, it is likely that a parser will
  53. /// spawn child parsers to parse child elements in the configuration.
  54. class DhcpConfigParser {
  55. ///
  56. /// @name Constructors and Destructor
  57. ///
  58. /// Note: The copy constructor and the assignment operator are
  59. /// intentionally defined as private to make it explicit that this is a
  60. /// pure base class.
  61. //@{
  62. private:
  63. // Private constructor and assignment operator assures that nobody
  64. // will be able to copy or assign a parser. There are no defined
  65. // bodies for them.
  66. DhcpConfigParser(const DhcpConfigParser& source);
  67. DhcpConfigParser& operator=(const DhcpConfigParser& source);
  68. protected:
  69. /// @brief The default constructor.
  70. ///
  71. /// This is intentionally defined as @c protected as this base class should
  72. /// never be instantiated (except as part of a derived class).
  73. DhcpConfigParser() {}
  74. public:
  75. /// The destructor.
  76. virtual ~DhcpConfigParser() {}
  77. //@}
  78. /// @brief Prepare configuration value.
  79. ///
  80. /// This method parses the "value part" of the configuration identifier
  81. /// that corresponds to this derived class and prepares a new value to
  82. /// apply to the server.
  83. ///
  84. /// This method must validate the given value both in terms of syntax
  85. /// and semantics of the configuration, so that the server will be
  86. /// validly configured at the time of @c commit(). Note: the given
  87. /// configuration value is normally syntactically validated, but the
  88. /// @c build() implementation must also expect invalid input. If it
  89. /// detects an error it may throw an exception of a derived class
  90. /// of @c isc::Exception.
  91. ///
  92. /// Preparing a configuration value will often require resource
  93. /// allocation. If it fails, it may throw a corresponding standard
  94. /// exception.
  95. ///
  96. /// This method is not expected to be called more than once in the
  97. /// life of the object. Although multiple calls are not prohibited
  98. /// by the interface, the behavior is undefined.
  99. ///
  100. /// @param config_value The configuration value for the identifier
  101. /// corresponding to the derived class.
  102. virtual void build(isc::data::ConstElementPtr config_value) = 0;
  103. /// @brief Apply the prepared configuration value to the server.
  104. ///
  105. /// This method is expected to be exception free, and, as a consequence,
  106. /// it should normally not involve resource allocation.
  107. /// Typically it would simply perform exception free assignment or swap
  108. /// operation on the value prepared in @c build().
  109. /// In some cases, however, it may be very difficult to meet this
  110. /// condition in a realistic way, while the failure case should really
  111. /// be very rare. In such a case it may throw, and, if the parser is
  112. /// called via @c configureDhcp4Server(), the caller will convert the
  113. /// exception as a fatal error.
  114. ///
  115. /// This method is expected to be called after @c build(), and only once.
  116. /// The result is undefined otherwise.
  117. virtual void commit() = 0;
  118. };
  119. /// @brief A template class that stores named elements of a given data type.
  120. ///
  121. /// This template class is provides data value storage for configuration parameters
  122. /// of a given data type. The values are stored by parmater name and as instances
  123. /// of type "ValueType".
  124. ///
  125. /// @tparam ValueType is the data type of the elements to store.
  126. template<typename ValueType>
  127. class ValueStorage {
  128. public:
  129. /// @brief Stores the the parameter and its value in the store.
  130. ///
  131. /// If the parmater does not exist in the store, then it will be added,
  132. /// otherwise its data value will be updated with the given value.
  133. ///
  134. /// @param name is the name of the paramater to store.
  135. /// @param value is the data value to store.
  136. void setParam(const std::string name, const ValueType value) {
  137. values_[name] = value;
  138. }
  139. /// @brief Returns the data value for the given parameter.
  140. ///
  141. /// Finds and returns the data value for the given parameter.
  142. /// @param name is the name of the parameter for which the data
  143. /// value is desired.
  144. ///
  145. /// @return The paramater's data value of type <ValueType>.
  146. /// @throw DhcpConfigError if the parameter is not found.
  147. ValueType getParam(const std::string& name) const {
  148. typename std::map<std::string, ValueType>::const_iterator param
  149. = values_.find(name);
  150. if (param == values_.end()) {
  151. isc_throw(DhcpConfigError, "missing parameter '"
  152. << name << "'");
  153. }
  154. ValueType value = param->second;
  155. return (value);
  156. }
  157. /// @brief Remove the parameter from the store.
  158. ///
  159. /// Deletes the entry for the given parameter from the store if it
  160. /// exists.
  161. ///
  162. /// @param name is the name of the paramater to delete.
  163. void delParam(const std::string name) {
  164. values_.erase(name);
  165. }
  166. /// @brief Deletes all of the entries from the store.
  167. ///
  168. void clear() {
  169. values_.clear();
  170. }
  171. private:
  172. /// @brief An std::map of the data values, keyed by parameter names.
  173. std::map<std::string, ValueType> values_;
  174. };
  175. /// @brief a collection of elements that store uint32 values (e.g. renew-timer = 900)
  176. typedef ValueStorage<uint32_t> Uint32Storage;
  177. /// @brief a collection of elements that store string values
  178. typedef ValueStorage<std::string> StringStorage;
  179. /// @brief Storage for parsed boolean values.
  180. typedef ValueStorage<bool> BooleanStorage;
  181. }; // end of isc::dhcp namespace
  182. }; // end of isc namespace
  183. #endif // DHCP_CONFIG_PARSER_H