module_spec.h 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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. /// TODO: use jinmei's exception class as a base and not c_str in
  25. /// what() there
  26. class ModuleSpecError : public std::exception {
  27. public:
  28. ModuleSpecError(std::string m = "Module specification is invalid") : msg(m) {}
  29. ~ModuleSpecError() throw() {}
  30. const char* what() const throw() { return (msg.c_str()); }
  31. private:
  32. std::string msg;
  33. };
  34. ///
  35. /// The \c ModuleSpec class holds a data specification.
  36. /// Each module should have a .spec file containing the specification
  37. /// for configuration and commands for that module.
  38. /// This class holds that specification, and provides a function to
  39. /// validate a set of data, to see whether it conforms to the given
  40. /// specification
  41. ///
  42. /// The form of the specification is described in doc/ (TODO)
  43. ///
  44. class ModuleSpec {
  45. public:
  46. ModuleSpec() {};
  47. /// Create a \c ModuleSpec instance with the given data as
  48. /// the specification
  49. /// \param e The Element containing the data specification
  50. /// \param check If false, the module specification in the file
  51. /// is not checked to be of the correct form.
  52. explicit ModuleSpec(isc::data::ConstElementPtr e,
  53. const bool check = true)
  54. throw(ModuleSpecError);
  55. /// Returns the commands part of the specification as an
  56. /// ElementPtr, returns an empty ElementPtr if there is none
  57. /// \return ElementPtr Shared pointer to the commands
  58. /// part of the specification
  59. isc::data::ConstElementPtr getCommandsSpec() const;
  60. /// Returns the configuration part of the specification as an
  61. /// ElementPtr
  62. /// \return ElementPtr Shared pointer to the configuration
  63. /// part of the specification
  64. isc::data::ConstElementPtr getConfigSpec() const;
  65. /// Returns the statistics part of the specification as an
  66. /// ElementPtr
  67. /// \return ElementPtr Shared pointer to the statistics
  68. /// part of the specification
  69. isc::data::ConstElementPtr getStatisticsSpec() const;
  70. /// Returns the full module specification as an ElementPtr
  71. /// \return ElementPtr Shared pointer to the specification
  72. isc::data::ConstElementPtr getFullSpec() const {
  73. return module_specification;
  74. }
  75. /// Returns the module name as specified by the specification
  76. const std::string getModuleName() const;
  77. /// Returns the module description as specified by the specification
  78. /// returns an empty string if there is no description
  79. const std::string getModuleDescription() const;
  80. // returns true if the given element conforms to this data
  81. // configuration specification
  82. /// Validates the given configuration data for this specification.
  83. /// \param data The base \c Element of the data to check
  84. /// \param full If true, all non-optional configuration parameters
  85. /// must be specified.
  86. /// \return true if the data conforms to the specification,
  87. /// false otherwise.
  88. bool validateConfig(isc::data::ConstElementPtr data,
  89. const bool full = false) const;
  90. // returns true if the given element conforms to this data
  91. // statistics specification
  92. /// Validates the given statistics data for this specification.
  93. /// \param data The base \c Element of the data to check
  94. /// \param full If true, all non-optional statistics parameters
  95. /// must be specified.
  96. /// \return true if the data conforms to the specification,
  97. /// false otherwise.
  98. bool validateStatistics(isc::data::ConstElementPtr data,
  99. const bool full = false) const;
  100. /// Validates the arguments for the given command
  101. ///
  102. /// This checks the command and argument against the
  103. /// specification in the module's .spec file.
  104. ///
  105. /// A command is considered valid if:
  106. /// - it is known (the 'command' string must have an entry in
  107. /// the specification)
  108. /// - the args is a MapElement
  109. /// - args contains all mandatory arguments
  110. /// - args does not contain unknown arguments
  111. /// - all arguments in args match their specification
  112. /// If all of these are true, this function returns \c true
  113. /// If not, this method returns \c false
  114. ///
  115. /// Example usage:
  116. /// \code
  117. /// ElementPtr errors = Element::createList();
  118. /// if (module_specification_.validateCommand(cmd_str,
  119. /// arg,
  120. /// errors)) {
  121. /// std::cout << "Command is valid" << std::endl;
  122. /// } else {
  123. /// std::cout << "Command is invalid: " << std::endl;
  124. /// BOOST_FOREACH(ConstElementPtr error,
  125. /// errors->listValue()) {
  126. /// std::cout << error->stringValue() << std::endl;
  127. /// }
  128. /// }
  129. /// \endcode
  130. ///
  131. /// \param command The command to validate the arguments for
  132. /// \param args A dict containing the command parameters
  133. /// \param errors An ElementPtr pointing to a ListElement. Any
  134. /// errors that are found are added as
  135. /// StringElements to this list
  136. /// \return true if the command is known and the parameters are correct
  137. /// false otherwise
  138. bool validateCommand(const std::string& command,
  139. isc::data::ConstElementPtr args,
  140. isc::data::ElementPtr errors) const;
  141. /// errors must be of type ListElement
  142. bool validateConfig(isc::data::ConstElementPtr data, const bool full,
  143. isc::data::ElementPtr errors) const;
  144. /// errors must be of type ListElement
  145. bool validateStatistics(isc::data::ConstElementPtr data, const bool full,
  146. isc::data::ElementPtr errors) const;
  147. private:
  148. bool validateItem(isc::data::ConstElementPtr spec,
  149. isc::data::ConstElementPtr data,
  150. const bool full,
  151. isc::data::ElementPtr errors) const;
  152. bool validateSpec(isc::data::ConstElementPtr spec,
  153. isc::data::ConstElementPtr data,
  154. const bool full,
  155. isc::data::ElementPtr errors) const;
  156. bool validateSpecList(isc::data::ConstElementPtr spec,
  157. isc::data::ConstElementPtr data,
  158. const bool full,
  159. isc::data::ElementPtr errors) const;
  160. isc::data::ConstElementPtr module_specification;
  161. };
  162. /// Creates a \c ModuleSpec instance from the contents
  163. /// of the file given by file_name.
  164. /// If check is true, and the module specification is not of
  165. /// the correct form, a ModuleSpecError is thrown. If the file
  166. /// could not be parse, a ParseError is thrown.
  167. /// \param file_name The file to be opened and parsed
  168. /// \param check If true, the module specification in the file
  169. /// is checked to be of the correct form
  170. ModuleSpec
  171. moduleSpecFromFile(const std::string& file_name, const bool check = true)
  172. throw(isc::data::JSONError, ModuleSpecError);
  173. /// Creates a \c ModuleSpec instance from the given input
  174. /// stream that contains the contents of a .spec file.
  175. /// If check is true, and the module specification is not of
  176. /// the correct form, a ModuleSpecError is thrown. If the
  177. /// file could not be parsed, a ParseError is thrown.
  178. /// \param in The std::istream containing the .spec file data
  179. /// \param check If true, the module specification is checked
  180. /// to be of the correct form
  181. ModuleSpec
  182. moduleSpecFromFile(std::ifstream& in, const bool check = true)
  183. throw(isc::data::JSONError, ModuleSpecError);
  184. } }
  185. #endif // _DATA_DEF_H
  186. // Local Variables:
  187. // mode: c++
  188. // End: