module_spec.h 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. // Copyright (C) 2010 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 full module specification as an ElementPtr
  66. /// \return ElementPtr Shared pointer to the specification
  67. isc::data::ConstElementPtr getFullSpec() const {
  68. return module_specification;
  69. }
  70. /// Returns the module name as specified by the specification
  71. const std::string getModuleName() const;
  72. /// Returns the module description as specified by the specification
  73. /// returns an empty string if there is no description
  74. const std::string getModuleDescription() const;
  75. // returns true if the given element conforms to this data
  76. // configuration specification
  77. /// Validates the given configuration data for this specification.
  78. /// \param data The base \c Element of the data to check
  79. /// \param full If true, all non-optional configuration parameters
  80. /// must be specified.
  81. /// \return true if the data conforms to the specification,
  82. /// false otherwise.
  83. bool validateConfig(isc::data::ConstElementPtr data,
  84. const bool full = false) const;
  85. /// Validates the arguments for the given command
  86. ///
  87. /// This checks the command and argument against the
  88. /// specification in the module's .spec file.
  89. ///
  90. /// A command is considered valid if:
  91. /// - it is known (the 'command' string must have an entry in
  92. /// the specification)
  93. /// - the args is a MapElement
  94. /// - args contains all mandatory arguments
  95. /// - args does not contain unknown arguments
  96. /// - all arguments in args match their specification
  97. /// If all of these are true, this function returns \c true
  98. /// If not, this method returns \c false
  99. ///
  100. /// Example usage:
  101. /// \code
  102. /// ElementPtr errors = Element::createList();
  103. /// if (module_specification_.validateCommand(cmd_str,
  104. /// arg,
  105. /// errors)) {
  106. /// std::cout << "Command is valid" << std::endl;
  107. /// } else {
  108. /// std::cout << "Command is invalid: " << std::endl;
  109. /// BOOST_FOREACH(ConstElementPtr error,
  110. /// errors->listValue()) {
  111. /// std::cout << error->stringValue() << std::endl;
  112. /// }
  113. /// }
  114. /// \endcode
  115. ///
  116. /// \param command The command to validate the arguments for
  117. /// \param args A dict containing the command parameters
  118. /// \param errors An ElementPtr pointing to a ListElement. Any
  119. /// errors that are found are added as
  120. /// StringElements to this list
  121. /// \return true if the command is known and the parameters are correct
  122. /// false otherwise
  123. bool validateCommand(const std::string& command,
  124. isc::data::ConstElementPtr args,
  125. isc::data::ElementPtr errors) const;
  126. /// errors must be of type ListElement
  127. bool validateConfig(isc::data::ConstElementPtr data, const bool full,
  128. isc::data::ElementPtr errors) const;
  129. private:
  130. bool validateItem(isc::data::ConstElementPtr spec,
  131. isc::data::ConstElementPtr data,
  132. const bool full,
  133. isc::data::ElementPtr errors) const;
  134. bool validateSpec(isc::data::ConstElementPtr spec,
  135. isc::data::ConstElementPtr data,
  136. const bool full,
  137. isc::data::ElementPtr errors) const;
  138. bool validateSpecList(isc::data::ConstElementPtr spec,
  139. isc::data::ConstElementPtr data,
  140. const bool full,
  141. isc::data::ElementPtr errors) const;
  142. isc::data::ConstElementPtr module_specification;
  143. };
  144. /// Creates a \c ModuleSpec instance from the contents
  145. /// of the file given by file_name.
  146. /// If check is true, and the module specification is not of
  147. /// the correct form, a ModuleSpecError is thrown. If the file
  148. /// could not be parse, a ParseError is thrown.
  149. /// \param file_name The file to be opened and parsed
  150. /// \param check If true, the module specification in the file
  151. /// is checked to be of the correct form
  152. ModuleSpec
  153. moduleSpecFromFile(const std::string& file_name, const bool check = true)
  154. throw(isc::data::JSONError, ModuleSpecError);
  155. /// Creates a \c ModuleSpec instance from the given input
  156. /// stream that contains the contents of a .spec file.
  157. /// If check is true, and the module specification is not of
  158. /// the correct form, a ModuleSpecError is thrown. If the
  159. /// file could not be parsed, a ParseError is thrown.
  160. /// \param in The std::istream containing the .spec file data
  161. /// \param check If true, the module specification is checked
  162. /// to be of the correct form
  163. ModuleSpec
  164. moduleSpecFromFile(std::ifstream& in, const bool check = true)
  165. throw(isc::data::JSONError, ModuleSpecError);
  166. } }
  167. #endif // _DATA_DEF_H
  168. // Local Variables:
  169. // mode: c++
  170. // End: