module_spec.h 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. // Copyright (C) 2010, 2011 Internet Systems Consortium.
  2. //
  3. // Permission to use, copy, modify, and 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 INTERNET SYSTEMS CONSORTIUM
  8. // DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL
  9. // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL
  10. // INTERNET SYSTEMS CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT,
  11. // INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING
  12. // FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
  13. // NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
  14. // WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  15. #ifndef MODULE_SPEC_H
  16. #define MODULE_SPEC_H 1
  17. #include <cc/data.h>
  18. #include <sstream>
  19. namespace isc { namespace config {
  20. ///
  21. /// A standard ModuleSpec exception that is thrown when a
  22. /// specification is not in the correct form.
  23. ///
  24. class ModuleSpecError : public isc::Exception {
  25. public:
  26. ModuleSpecError(const char* file, size_t line,
  27. const char* what = "Module specification is invalid") :
  28. isc::Exception(file, line, what) {}
  29. };
  30. ///
  31. /// The \c ModuleSpec class holds a data specification.
  32. /// Each module should have a .spec file containing the specification
  33. /// for configuration and commands for that module.
  34. /// This class holds that specification, and provides a function to
  35. /// validate a set of data, to see whether it conforms to the given
  36. /// specification
  37. ///
  38. /// The form of the specification is described in doc/ (TODO)
  39. ///
  40. class ModuleSpec {
  41. public:
  42. ModuleSpec() {};
  43. /// Create a \c ModuleSpec instance with the given data as
  44. /// the specification
  45. /// \param e The Element containing the data specification
  46. /// \param check If false, the module specification in the file
  47. /// is not checked to be of the correct form.
  48. explicit ModuleSpec(isc::data::ConstElementPtr e,
  49. const bool check = true)
  50. throw(ModuleSpecError);
  51. /// Returns the commands part of the specification as an
  52. /// ElementPtr, returns an empty ElementPtr if there is none
  53. /// \return ElementPtr Shared pointer to the commands
  54. /// part of the specification
  55. isc::data::ConstElementPtr getCommandsSpec() const;
  56. /// Returns the configuration part of the specification as an
  57. /// ElementPtr
  58. /// \return ElementPtr Shared pointer to the configuration
  59. /// part of the specification
  60. isc::data::ConstElementPtr getConfigSpec() const;
  61. /// Returns the statistics part of the specification as an
  62. /// ElementPtr
  63. /// \return ElementPtr Shared pointer to the statistics
  64. /// part of the specification
  65. isc::data::ConstElementPtr getStatisticsSpec() const;
  66. /// Returns the full module specification as an ElementPtr
  67. /// \return ElementPtr Shared pointer to the specification
  68. isc::data::ConstElementPtr getFullSpec() const {
  69. return module_specification;
  70. }
  71. /// Returns the module name as specified by the specification
  72. const std::string getModuleName() const;
  73. /// Returns the module description as specified by the specification
  74. /// returns an empty string if there is no description
  75. const std::string getModuleDescription() const;
  76. // returns true if the given element conforms to this data
  77. // configuration specification
  78. /// Validates the given configuration data for this specification.
  79. /// \param data The base \c Element of the data to check
  80. /// \param full If true, all non-optional configuration parameters
  81. /// must be specified.
  82. /// \return true if the data conforms to the specification,
  83. /// false otherwise.
  84. bool validateConfig(isc::data::ConstElementPtr data,
  85. const bool full = false) const;
  86. // returns true if the given element conforms to this data
  87. // statistics specification
  88. /// Validates the given statistics data for this specification.
  89. /// \param data The base \c Element of the data to check
  90. /// \param full If true, all non-optional statistics parameters
  91. /// must be specified.
  92. /// \return true if the data conforms to the specification,
  93. /// false otherwise.
  94. bool validateStatistics(isc::data::ConstElementPtr data,
  95. const bool full = false) const;
  96. /// Validates the arguments for the given command
  97. ///
  98. /// This checks the command and argument against the
  99. /// specification in the module's .spec file.
  100. ///
  101. /// A command is considered valid if:
  102. /// - it is known (the 'command' string must have an entry in
  103. /// the specification)
  104. /// - the args is a MapElement
  105. /// - args contains all mandatory arguments
  106. /// - args does not contain unknown arguments
  107. /// - all arguments in args match their specification
  108. /// If all of these are true, this function returns \c true
  109. /// If not, this method returns \c false
  110. ///
  111. /// Example usage:
  112. /// \code
  113. /// ElementPtr errors = Element::createList();
  114. /// if (module_specification_.validateCommand(cmd_str,
  115. /// arg,
  116. /// errors)) {
  117. /// std::cout << "Command is valid" << std::endl;
  118. /// } else {
  119. /// std::cout << "Command is invalid: " << std::endl;
  120. /// BOOST_FOREACH(ConstElementPtr error,
  121. /// errors->listValue()) {
  122. /// std::cout << error->stringValue() << std::endl;
  123. /// }
  124. /// }
  125. /// \endcode
  126. ///
  127. /// \param command The command to validate the arguments for
  128. /// \param args A dict containing the command parameters
  129. /// \param errors An ElementPtr pointing to a ListElement. Any
  130. /// errors that are found are added as
  131. /// StringElements to this list
  132. /// \return true if the command is known and the parameters are correct
  133. /// false otherwise
  134. bool validateCommand(const std::string& command,
  135. isc::data::ConstElementPtr args,
  136. isc::data::ElementPtr errors) const;
  137. /// errors must be of type ListElement
  138. bool validateConfig(isc::data::ConstElementPtr data, const bool full,
  139. isc::data::ElementPtr errors) const;
  140. /// errors must be of type ListElement
  141. bool validateStatistics(isc::data::ConstElementPtr data, const bool full,
  142. isc::data::ElementPtr errors) const;
  143. private:
  144. bool validateItem(isc::data::ConstElementPtr spec,
  145. isc::data::ConstElementPtr data,
  146. const bool full,
  147. isc::data::ElementPtr errors) const;
  148. bool validateSpec(isc::data::ConstElementPtr spec,
  149. isc::data::ConstElementPtr data,
  150. const bool full,
  151. isc::data::ElementPtr errors) const;
  152. bool validateSpecList(isc::data::ConstElementPtr spec,
  153. isc::data::ConstElementPtr data,
  154. const bool full,
  155. isc::data::ElementPtr errors) const;
  156. isc::data::ConstElementPtr module_specification;
  157. };
  158. /// Creates a \c ModuleSpec instance from the contents
  159. /// of the file given by file_name.
  160. /// If check is true, and the module specification is not of
  161. /// the correct form, a ModuleSpecError is thrown. If the file
  162. /// could not be parse, a ParseError is thrown.
  163. /// \param file_name The file to be opened and parsed
  164. /// \param check If true, the module specification in the file
  165. /// is checked to be of the correct form
  166. ModuleSpec
  167. moduleSpecFromFile(const std::string& file_name, const bool check = true)
  168. throw(isc::data::JSONError, ModuleSpecError);
  169. /// Creates a \c ModuleSpec instance from the given input
  170. /// stream that contains the contents of a .spec file.
  171. /// If check is true, and the module specification is not of
  172. /// the correct form, a ModuleSpecError is thrown. If the
  173. /// file could not be parsed, a ParseError is thrown.
  174. /// \param in The std::istream containing the .spec file data
  175. /// \param check If true, the module specification is checked
  176. /// to be of the correct form
  177. ModuleSpec
  178. moduleSpecFromFile(std::ifstream& in, const bool check = true)
  179. throw(isc::data::JSONError, ModuleSpecError);
  180. } }
  181. #endif // _DATA_DEF_H
  182. // Local Variables:
  183. // mode: c++
  184. // End: