dhcp_config_parser.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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. namespace isc {
  17. namespace dhcp {
  18. /// An exception that is thrown if an error occurs while configuring
  19. /// DHCP server.
  20. class DhcpConfigError : public isc::Exception {
  21. public:
  22. /// @brief constructor
  23. ///
  24. /// @param file name of the file, where exception occurred
  25. /// @param line line of the file, where exception occurred
  26. /// @param what text description of the issue that caused exception
  27. DhcpConfigError(const char* file, size_t line, const char* what)
  28. : isc::Exception(file, line, what) {}
  29. };
  30. /// @brief Forward declaration to DhcpConfigParser class.
  31. ///
  32. /// It is only needed here to define types that are
  33. /// based on this class before the class definition.
  34. class DhcpConfigParser;
  35. /// @brief a pointer to configuration parser
  36. typedef boost::shared_ptr<DhcpConfigParser> ParserPtr;
  37. /// @brief Collection of parsers.
  38. ///
  39. /// This container is used to store pointer to parsers for a given scope.
  40. typedef std::vector<ParserPtr> ParserCollection;
  41. /// @brief Base abstract class for all DHCP 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. class DhcpConfigParser {
  48. ///
  49. /// @name Constructors and Destructor
  50. ///
  51. /// Note: The copy constructor and the assignment operator are
  52. /// intentionally defined as private to make it explicit that this is a
  53. /// pure base class.
  54. //@{
  55. private:
  56. // Private construtor and assignment operator assures that nobody
  57. // will be able to copy or assign a parser. There are no defined
  58. // bodies for them.
  59. DhcpConfigParser(const DhcpConfigParser& source);
  60. DhcpConfigParser& operator=(const DhcpConfigParser& source);
  61. protected:
  62. /// @brief The default constructor.
  63. ///
  64. /// This is intentionally defined as @c protected as this base class should
  65. /// never be instantiated (except as part of a derived class).
  66. DhcpConfigParser() {}
  67. public:
  68. /// The destructor.
  69. virtual ~DhcpConfigParser() {}
  70. //@}
  71. /// @brief Prepare configuration value.
  72. ///
  73. /// This method parses the "value part" of the configuration identifier
  74. /// that corresponds to this derived class and prepares a new value to
  75. /// apply to the server.
  76. ///
  77. /// This method must validate the given value both in terms of syntax
  78. /// and semantics of the configuration, so that the server will be
  79. /// validly configured at the time of @c commit(). Note: the given
  80. /// configuration value is normally syntactically validated, but the
  81. /// @c build() implementation must also expect invalid input. If it
  82. /// detects an error it may throw an exception of a derived class
  83. /// of @c isc::Exception.
  84. ///
  85. /// Preparing a configuration value will often require resource
  86. /// allocation. If it fails, it may throw a corresponding standard
  87. /// exception.
  88. ///
  89. /// This method is not expected to be called more than once in the
  90. /// life of the object. Although multiple calls are not prohibited
  91. /// by the interface, the behavior is undefined.
  92. ///
  93. /// @param config_value The configuration value for the identifier
  94. /// corresponding to the derived class.
  95. virtual void build(isc::data::ConstElementPtr config_value) = 0;
  96. /// @brief Apply the prepared configuration value to the server.
  97. ///
  98. /// This method is expected to be exception free, and, as a consequence,
  99. /// it should normally not involve resource allocation.
  100. /// Typically it would simply perform exception free assignment or swap
  101. /// operation on the value prepared in @c build().
  102. /// In some cases, however, it may be very difficult to meet this
  103. /// condition in a realistic way, while the failure case should really
  104. /// be very rare. In such a case it may throw, and, if the parser is
  105. /// called via @c configureDhcp4Server(), the caller will convert the
  106. /// exception as a fatal error.
  107. ///
  108. /// This method is expected to be called after @c build(), and only once.
  109. /// The result is undefined otherwise.
  110. virtual void commit() = 0;
  111. protected:
  112. /// @brief Return the parsed entry from the provided storage.
  113. ///
  114. /// This method returns the parsed entry from the provided
  115. /// storage. If the entry is not found, then exception is
  116. /// thrown.
  117. ///
  118. /// @param param_id name of the configuration entry.
  119. /// @param storage storage where the entry should be searched.
  120. /// @tparam ReturnType type of the returned value.
  121. /// @tparam StorageType type of the storage.
  122. ///
  123. /// @throw DhcpConfigError if the entry has not been found
  124. /// in the storage.
  125. template<typename ReturnType, typename StorageType>
  126. static ReturnType getParam(const std::string& param_id,
  127. const StorageType& storage) {
  128. typename StorageType::const_iterator param = storage.find(param_id);
  129. if (param == storage.end()) {
  130. isc_throw(DhcpConfigError, "missing parameter '"
  131. << param_id << "'");
  132. }
  133. ReturnType value = param->second;
  134. return (value);
  135. }
  136. };
  137. } // end of isc::dhcp namespace
  138. } // end of isc namespace
  139. #endif // DHCP_CONFIG_PARSER_H