dhcp_config_parser.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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. }; // end of isc::dhcp namespace
  120. }; // end of isc namespace
  121. #endif // DHCP_CONFIG_PARSER_H