module_spec.py 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  1. # Copyright (C) 2009 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. """Module Specifications
  16. A module specification holds the information about what configuration
  17. a module can have, and what commands it understands. It provides
  18. functions to read it from a .spec file, and to validate a given
  19. set of data against the specification
  20. """
  21. import json
  22. import sys
  23. import isc.cc.data
  24. # file objects are passed around as _io.TextIOWrapper objects
  25. # import that so we can check those types
  26. class ModuleSpecError(Exception):
  27. """This exception is raised it the ModuleSpec fails to initialize
  28. or if there is a failure or parse error reading the specification
  29. file"""
  30. pass
  31. def module_spec_from_file(spec_file, check = True):
  32. """Returns a ModuleSpec object defined by the file at spec_file.
  33. If check is True, the contents are verified. If there is an error
  34. in those contents, a ModuleSpecError is raised.
  35. A ModuleSpecError is also raised if the file cannot be read, or
  36. if it is not valid JSON."""
  37. module_spec = None
  38. try:
  39. if hasattr(spec_file, 'read'):
  40. json_str = spec_file.read()
  41. module_spec = json.loads(json_str)
  42. elif type(spec_file) == str:
  43. file = open(spec_file)
  44. json_str = file.read()
  45. module_spec = json.loads(json_str)
  46. file.close()
  47. else:
  48. raise ModuleSpecError("spec_file not a str or file-like object")
  49. except ValueError as ve:
  50. raise ModuleSpecError("JSON parse error: " + str(ve))
  51. except IOError as ioe:
  52. raise ModuleSpecError("JSON read error: " + str(ioe))
  53. if 'module_spec' not in module_spec:
  54. raise ModuleSpecError("Data definition has no module_spec element")
  55. result = ModuleSpec(module_spec['module_spec'], check)
  56. return result
  57. class ModuleSpec:
  58. def __init__(self, module_spec, check = True):
  59. """Initializes a ModuleSpec object from the specification in
  60. the given module_spec (which must be a dict). If check is
  61. True, the contents are verified. Raises a ModuleSpec error
  62. if there is something wrong with the contents of the dict"""
  63. if type(module_spec) != dict:
  64. raise ModuleSpecError("module_spec is of type " + str(type(module_spec)) + ", not dict")
  65. if check:
  66. _check(module_spec)
  67. self._module_spec = module_spec
  68. def validate_config(self, full, data, errors = None):
  69. """Check whether the given piece of data conforms to this
  70. data definition. If so, it returns True. If not, it will
  71. return false. If errors is given, and is an array, a string
  72. describing the error will be appended to it. The current
  73. version stops as soon as there is one error so this list
  74. will not be exhaustive. If 'full' is true, it also errors on
  75. non-optional missing values. Set this to False if you want to
  76. validate only a part of a configuration tree (like a list of
  77. non-default values)"""
  78. data_def = self.get_config_spec()
  79. if data_def:
  80. return _validate_spec_list(data_def, full, data, errors)
  81. else:
  82. # no spec, always bad
  83. if errors != None:
  84. errors.append("No config_data specification")
  85. return False
  86. def validate_command(self, cmd_name, cmd_params, errors = None):
  87. '''Check whether the given piece of command conforms to this
  88. command definition. If so, it reutrns True. If not, it will
  89. return False. If errors is given, and is an array, a string
  90. describing the error will be appended to it. The current version
  91. stops as soon as there is one error.
  92. cmd_name is command name to be validated, cmd_params includes
  93. command's parameters needs to be validated. cmd_params must
  94. be a map, with the format like:
  95. {param1_name: param1_value, param2_name: param2_value}
  96. '''
  97. cmd_spec = self.get_commands_spec()
  98. if not cmd_spec:
  99. return False
  100. for cmd in cmd_spec:
  101. if cmd['command_name'] != cmd_name:
  102. continue
  103. return _validate_spec_list(cmd['command_args'], True, cmd_params, errors)
  104. return False
  105. def get_module_name(self):
  106. """Returns a string containing the name of the module as
  107. specified by the specification given at __init__()"""
  108. return self._module_spec['module_name']
  109. def get_module_description(self):
  110. """Returns a string containing the description of the module as
  111. specified by the specification given at __init__().
  112. Returns an empty string if there is no description.
  113. """
  114. if 'module_description' in self._module_spec:
  115. return self._module_spec['module_description']
  116. else:
  117. return ""
  118. def get_full_spec(self):
  119. """Returns a dict representation of the full module specification"""
  120. return self._module_spec
  121. def get_config_spec(self):
  122. """Returns a dict representation of the configuration data part
  123. of the specification, or None if there is none."""
  124. if 'config_data' in self._module_spec:
  125. return self._module_spec['config_data']
  126. else:
  127. return None
  128. def get_commands_spec(self):
  129. """Returns a dict representation of the commands part of the
  130. specification, or None if there is none."""
  131. if 'commands' in self._module_spec:
  132. return self._module_spec['commands']
  133. else:
  134. return None
  135. def __str__(self):
  136. """Returns a string representation of the full specification"""
  137. return self._module_spec.__str__()
  138. def _check(module_spec):
  139. """Checks the full specification. This is a dict that contains the
  140. element "module_spec", which is in itself a dict that
  141. must contain at least a "module_name" (string) and optionally
  142. a "config_data" and a "commands" element, both of which are lists
  143. of dicts. Raises a ModuleSpecError if there is a problem."""
  144. if type(module_spec) != dict:
  145. raise ModuleSpecError("data specification not a dict")
  146. if "module_name" not in module_spec:
  147. raise ModuleSpecError("no module_name in module_spec")
  148. if "module_description" in module_spec and \
  149. type(module_spec["module_description"]) != str:
  150. raise ModuleSpecError("module_description is not a string")
  151. if "config_data" in module_spec:
  152. _check_config_spec(module_spec["config_data"])
  153. if "commands" in module_spec:
  154. _check_command_spec(module_spec["commands"])
  155. def _check_config_spec(config_data):
  156. # config data is a list of items represented by dicts that contain
  157. # things like "item_name", depending on the type they can have
  158. # specific subitems
  159. """Checks a list that contains the configuration part of the
  160. specification. Raises a ModuleSpecError if there is a
  161. problem."""
  162. if type(config_data) != list:
  163. raise ModuleSpecError("config_data is of type " + str(type(config_data)) + ", not a list of items")
  164. for config_item in config_data:
  165. _check_item_spec(config_item)
  166. def _check_command_spec(commands):
  167. """Checks the list that contains a set of commands. Raises a
  168. ModuleSpecError is there is an error"""
  169. if type(commands) != list:
  170. raise ModuleSpecError("commands is not a list of commands")
  171. for command in commands:
  172. if type(command) != dict:
  173. raise ModuleSpecError("command in commands list is not a dict")
  174. if "command_name" not in command:
  175. raise ModuleSpecError("no command_name in command item")
  176. command_name = command["command_name"]
  177. if type(command_name) != str:
  178. raise ModuleSpecError("command_name not a string: " + str(type(command_name)))
  179. if "command_description" in command:
  180. if type(command["command_description"]) != str:
  181. raise ModuleSpecError("command_description not a string in " + command_name)
  182. if "command_args" in command:
  183. if type(command["command_args"]) != list:
  184. raise ModuleSpecError("command_args is not a list in " + command_name)
  185. for command_arg in command["command_args"]:
  186. if type(command_arg) != dict:
  187. raise ModuleSpecError("command argument not a dict in " + command_name)
  188. _check_item_spec(command_arg)
  189. else:
  190. raise ModuleSpecError("command_args missing in " + command_name)
  191. pass
  192. def _check_item_spec(config_item):
  193. """Checks the dict that defines one config item
  194. (i.e. containing "item_name", "item_type", etc.
  195. Raises a ModuleSpecError if there is an error"""
  196. if type(config_item) != dict:
  197. raise ModuleSpecError("item spec not a dict")
  198. if "item_name" not in config_item:
  199. raise ModuleSpecError("no item_name in config item")
  200. if type(config_item["item_name"]) != str:
  201. raise ModuleSpecError("item_name is not a string: " + str(config_item["item_name"]))
  202. item_name = config_item["item_name"]
  203. if "item_type" not in config_item:
  204. raise ModuleSpecError("no item_type in config item")
  205. item_type = config_item["item_type"]
  206. if type(item_type) != str:
  207. raise ModuleSpecError("item_type in " + item_name + " is not a string: " + str(type(item_type)))
  208. if item_type not in ["integer", "real", "boolean", "string", "list", "map", "any"]:
  209. raise ModuleSpecError("unknown item_type in " + item_name + ": " + item_type)
  210. if "item_optional" in config_item:
  211. if type(config_item["item_optional"]) != bool:
  212. raise ModuleSpecError("item_default in " + item_name + " is not a boolean")
  213. if not config_item["item_optional"] and "item_default" not in config_item:
  214. raise ModuleSpecError("no default value for non-optional item " + item_name)
  215. else:
  216. raise ModuleSpecError("item_optional not in item " + item_name)
  217. if "item_default" in config_item:
  218. item_default = config_item["item_default"]
  219. if (item_type == "integer" and type(item_default) != int) or \
  220. (item_type == "real" and type(item_default) != float) or \
  221. (item_type == "boolean" and type(item_default) != bool) or \
  222. (item_type == "string" and type(item_default) != str) or \
  223. (item_type == "list" and type(item_default) != list) or \
  224. (item_type == "map" and type(item_default) != dict):
  225. raise ModuleSpecError("Wrong type for item_default in " + item_name)
  226. # TODO: once we have check_type, run the item default through that with the list|map_item_spec
  227. if item_type == "list":
  228. if "list_item_spec" not in config_item:
  229. raise ModuleSpecError("no list_item_spec in list item " + item_name)
  230. if type(config_item["list_item_spec"]) != dict:
  231. raise ModuleSpecError("list_item_spec in " + item_name + " is not a dict")
  232. _check_item_spec(config_item["list_item_spec"])
  233. if item_type == "map":
  234. if "map_item_spec" not in config_item:
  235. raise ModuleSpecError("no map_item_sepc in map item " + item_name)
  236. if type(config_item["map_item_spec"]) != list:
  237. raise ModuleSpecError("map_item_spec in " + item_name + " is not a list")
  238. for map_item in config_item["map_item_spec"]:
  239. if type(map_item) != dict:
  240. raise ModuleSpecError("map_item_spec element is not a dict")
  241. _check_item_spec(map_item)
  242. def _validate_type(spec, value, errors):
  243. """Returns true if the value is of the correct type given the
  244. specification"""
  245. data_type = spec['item_type']
  246. if data_type == "integer" and type(value) != int:
  247. if errors != None:
  248. errors.append(str(value) + " should be an integer")
  249. return False
  250. elif data_type == "real" and type(value) != float:
  251. if errors != None:
  252. errors.append(str(value) + " should be a real")
  253. return False
  254. elif data_type == "boolean" and type(value) != bool:
  255. if errors != None:
  256. errors.append(str(value) + " should be a boolean")
  257. return False
  258. elif data_type == "string" and type(value) != str:
  259. if errors != None:
  260. errors.append(str(value) + " should be a string")
  261. return False
  262. elif data_type == "list" and type(value) != list:
  263. if errors != None:
  264. errors.append(str(value) + " should be a list")
  265. return False
  266. elif data_type == "map" and type(value) != dict:
  267. if errors != None:
  268. errors.append(str(value) + " should be a map")
  269. return False
  270. else:
  271. return True
  272. def _validate_item(spec, full, data, errors):
  273. if not _validate_type(spec, data, errors):
  274. return False
  275. elif type(data) == list:
  276. list_spec = spec['list_item_spec']
  277. for data_el in data:
  278. if not _validate_type(list_spec, data_el, errors):
  279. return False
  280. if list_spec['item_type'] == "map":
  281. if not _validate_item(list_spec, full, data_el, errors):
  282. return False
  283. elif type(data) == dict:
  284. if not _validate_spec_list(spec['map_item_spec'], full, data, errors):
  285. return False
  286. return True
  287. def _validate_spec(spec, full, data, errors):
  288. item_name = spec['item_name']
  289. item_optional = spec['item_optional']
  290. if not data and item_optional:
  291. return True
  292. elif item_name in data:
  293. return _validate_item(spec, full, data[item_name], errors)
  294. elif full and not item_optional:
  295. if errors != None:
  296. errors.append("non-optional item " + item_name + " missing")
  297. return False
  298. else:
  299. return True
  300. def _validate_spec_list(module_spec, full, data, errors):
  301. # we do not return immediately, there may be more errors
  302. # so we keep a boolean to keep track if we found errors
  303. validated = True
  304. # check if the known items are correct
  305. for spec_item in module_spec:
  306. if not _validate_spec(spec_item, full, data, errors):
  307. validated = False
  308. # check if there are items in our data that are not in the
  309. # specification
  310. if data is not None:
  311. for item_name in data:
  312. found = False
  313. for spec_item in module_spec:
  314. if spec_item["item_name"] == item_name:
  315. found = True
  316. if not found:
  317. if errors != None:
  318. errors.append("unknown item " + item_name)
  319. validated = False
  320. return validated