module_spec.h 8.0 KB

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