occi-core 4.3.6 → 5.0.0.alpha.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (359) hide show
  1. checksums.yaml +4 -4
  2. data/.fasterer.yml +3 -0
  3. data/.gitignore +9 -16
  4. data/.rspec +1 -1
  5. data/.rubocop.yml +69 -0
  6. data/.travis.yml +27 -0
  7. data/AUTHORS +8 -6
  8. data/CHANGELOG.md +0 -0
  9. data/Gemfile +1 -1
  10. data/LICENSE +1 -1
  11. data/README.md +25 -170
  12. data/Rakefile +25 -17
  13. data/examples/initialize_model.rb +10 -0
  14. data/examples/parse_action_instance_json.rb +15 -0
  15. data/examples/parse_action_instance_text.rb +15 -0
  16. data/examples/parse_categories_json.rb +15 -0
  17. data/examples/parse_categories_text.rb +15 -0
  18. data/examples/parse_instance_json.rb +15 -0
  19. data/examples/parse_instance_text.rb +15 -0
  20. data/examples/parse_model_json.rb +9 -0
  21. data/examples/parse_model_text.rb +9 -0
  22. data/examples/render_action_instance_json.rb +17 -0
  23. data/examples/render_action_instance_text.rb +17 -0
  24. data/examples/render_instance_json.rb +20 -0
  25. data/examples/render_instance_text.rb +20 -0
  26. data/examples/render_model_json.rb +12 -0
  27. data/examples/render_model_text.rb +12 -0
  28. data/examples/rendering/action_instance.json +6 -0
  29. data/examples/rendering/action_instance.txt +2 -0
  30. data/examples/rendering/categories.json +78 -0
  31. data/examples/rendering/categories.txt +4 -0
  32. data/examples/rendering/instance.json +70 -0
  33. data/examples/rendering/instance.txt +12 -0
  34. data/examples/rendering/model.alt.json +1716 -0
  35. data/examples/rendering/model.json +785 -0
  36. data/examples/rendering/model.txt +68 -0
  37. data/lib/occi/core/action.rb +12 -26
  38. data/lib/occi/core/action_instance.rb +109 -65
  39. data/lib/occi/core/attribute.rb +136 -0
  40. data/lib/occi/core/attribute_definition.rb +269 -0
  41. data/lib/occi/core/category.rb +110 -83
  42. data/lib/occi/core/collection.rb +255 -0
  43. data/lib/occi/core/constants.rb +14 -0
  44. data/lib/occi/core/entity.rb +301 -195
  45. data/lib/occi/core/errors/.gitkeep +0 -0
  46. data/lib/occi/core/errors/attribute_definition_error.rb +11 -0
  47. data/lib/occi/core/errors/attribute_validation_error.rb +13 -0
  48. data/lib/occi/core/errors/category_validation_error.rb +13 -0
  49. data/lib/occi/core/errors/collection_lookup_error.rb +11 -0
  50. data/lib/occi/core/errors/instance_validation_error.rb +13 -0
  51. data/lib/occi/core/errors/mandatory_argument_error.rb +11 -0
  52. data/lib/occi/core/errors/model_lookup_error.rb +11 -0
  53. data/lib/occi/core/errors/parser_error.rb +11 -0
  54. data/lib/occi/core/errors/parsing_error.rb +11 -0
  55. data/lib/occi/core/errors/renderer_error.rb +11 -0
  56. data/lib/occi/core/errors/rendering_error.rb +11 -0
  57. data/lib/occi/core/errors/validation_error.rb +11 -0
  58. data/lib/occi/core/errors.rb +9 -0
  59. data/lib/occi/core/helpers/.gitkeep +0 -0
  60. data/lib/occi/core/helpers/argument_validator.rb +27 -0
  61. data/lib/occi/core/helpers/attributes_accessor.rb +22 -0
  62. data/lib/occi/core/helpers/error_handler.rb +29 -0
  63. data/lib/occi/core/helpers/hash_dereferencer.rb +149 -0
  64. data/lib/occi/core/helpers/identifier_validator.rb +135 -0
  65. data/lib/occi/core/helpers/instance_attribute_resetter.rb +131 -0
  66. data/lib/occi/core/helpers/instance_attributes_accessor.rb +30 -0
  67. data/lib/occi/core/helpers/locatable.rb +40 -0
  68. data/lib/occi/core/helpers/parser_dereferencer.rb +96 -0
  69. data/lib/occi/core/helpers/raw_json_parser.rb +18 -0
  70. data/lib/occi/core/helpers/renderable.rb +93 -0
  71. data/lib/occi/core/helpers/yaml_summoner.rb +44 -0
  72. data/lib/occi/core/helpers.rb +13 -0
  73. data/lib/occi/core/instance_builder.rb +162 -0
  74. data/lib/occi/core/kind.rb +100 -132
  75. data/lib/occi/core/link.rb +47 -72
  76. data/lib/occi/core/mixin.rb +76 -61
  77. data/lib/occi/core/model.rb +341 -0
  78. data/lib/occi/core/parsers/.gitkeep +0 -0
  79. data/lib/occi/core/parsers/base_parser.rb +168 -0
  80. data/lib/occi/core/parsers/json/.gitkeep +0 -0
  81. data/lib/occi/core/parsers/json/action_instance.rb +36 -0
  82. data/lib/occi/core/parsers/json/category.rb +113 -0
  83. data/lib/occi/core/parsers/json/entity.rb +156 -0
  84. data/lib/occi/core/parsers/json/validator/.gitkeep +0 -0
  85. data/lib/occi/core/parsers/json/validator/action-instance.json +3 -0
  86. data/lib/occi/core/parsers/json/validator/entity-collection.json +3 -0
  87. data/lib/occi/core/parsers/json/validator/link-collection.json +3 -0
  88. data/lib/occi/core/parsers/json/validator/link.json +3 -0
  89. data/lib/occi/core/parsers/json/validator/locations.json +3 -0
  90. data/lib/occi/core/parsers/json/validator/mixin-collection.json +3 -0
  91. data/lib/occi/core/parsers/json/validator/model.json +3 -0
  92. data/lib/occi/core/parsers/json/validator/occi-schema.json +277 -0
  93. data/lib/occi/core/parsers/json/validator/resource-collection.json +3 -0
  94. data/lib/occi/core/parsers/json/validator/resource.json +3 -0
  95. data/lib/occi/core/parsers/json/validator.rb +87 -0
  96. data/lib/occi/core/parsers/json_parser.rb +128 -0
  97. data/lib/occi/core/parsers/text/.gitkeep +0 -0
  98. data/lib/occi/core/parsers/text/category.rb +183 -0
  99. data/lib/occi/core/parsers/text/constants.rb +89 -0
  100. data/lib/occi/core/parsers/text/entity.rb +257 -0
  101. data/lib/occi/core/parsers/text/location.rb +48 -0
  102. data/lib/occi/core/parsers/text_parser.rb +257 -0
  103. data/lib/occi/core/parsers.rb +18 -0
  104. data/lib/occi/core/renderer_factory.rb +213 -0
  105. data/lib/occi/core/renderers/.gitkeep +0 -0
  106. data/lib/occi/core/renderers/base_renderer.rb +81 -0
  107. data/lib/occi/core/renderers/json/.gitkeep +0 -0
  108. data/lib/occi/core/renderers/json/action_instance.rb +24 -0
  109. data/lib/occi/core/renderers/json/attributes.rb +50 -0
  110. data/lib/occi/core/renderers/json/base.rb +46 -0
  111. data/lib/occi/core/renderers/json/category.rb +85 -0
  112. data/lib/occi/core/renderers/json/collection.rb +52 -0
  113. data/lib/occi/core/renderers/json/instance.rb +39 -0
  114. data/lib/occi/core/renderers/json/link.rb +48 -0
  115. data/lib/occi/core/renderers/json/model.rb +26 -0
  116. data/lib/occi/core/renderers/json/resource.rb +27 -0
  117. data/lib/occi/core/renderers/json_renderer.rb +72 -0
  118. data/lib/occi/core/renderers/text/.gitkeep +0 -0
  119. data/lib/occi/core/renderers/text/action_instance.rb +36 -0
  120. data/lib/occi/core/renderers/text/attributes.rb +87 -0
  121. data/lib/occi/core/renderers/text/base.rb +82 -0
  122. data/lib/occi/core/renderers/text/category.rb +183 -0
  123. data/lib/occi/core/renderers/text/collection.rb +65 -0
  124. data/lib/occi/core/renderers/text/instance.rb +44 -0
  125. data/lib/occi/core/renderers/text/link.rb +40 -0
  126. data/lib/occi/core/renderers/text/model.rb +41 -0
  127. data/lib/occi/core/renderers/text/resource.rb +109 -0
  128. data/lib/occi/core/renderers/text_renderer.rb +72 -0
  129. data/lib/occi/core/renderers.rb +20 -0
  130. data/lib/occi/core/resource.rb +93 -57
  131. data/lib/occi/core/utils/.gitkeep +0 -0
  132. data/lib/occi/core/version.rb +17 -0
  133. data/lib/occi/core/warehouse/actions/.gitkeep +0 -0
  134. data/lib/occi/core/warehouse/actions/attributes/.gitkeep +0 -0
  135. data/lib/occi/core/warehouse/kinds/.gitkeep +0 -0
  136. data/lib/occi/core/warehouse/kinds/attributes/.gitkeep +0 -0
  137. data/lib/occi/core/warehouse/kinds/attributes/occi.core.id.yml +7 -0
  138. data/lib/occi/core/warehouse/kinds/attributes/occi.core.source.yml +7 -0
  139. data/lib/occi/core/warehouse/kinds/attributes/occi.core.summary.yml +7 -0
  140. data/lib/occi/core/warehouse/kinds/attributes/occi.core.target.yml +7 -0
  141. data/lib/occi/core/warehouse/kinds/attributes/occi.core.title.yml +7 -0
  142. data/lib/occi/core/warehouse/kinds/entity.yml +10 -0
  143. data/lib/occi/core/warehouse/kinds/link.yml +10 -0
  144. data/lib/occi/core/warehouse/kinds/resource.yml +9 -0
  145. data/lib/occi/core/warehouse/mixins/.gitkeep +0 -0
  146. data/lib/occi/core/warehouse/mixins/attributes/.gitkeep +0 -0
  147. data/lib/occi/core/warehouse.rb +115 -0
  148. data/lib/occi/core.rb +59 -23
  149. data/lib/occi/infrastructure/compute.rb +5 -122
  150. data/lib/occi/infrastructure/constants.rb +25 -0
  151. data/lib/occi/infrastructure/instance_builder.rb +24 -0
  152. data/lib/occi/infrastructure/mixins/.gitkeep +0 -0
  153. data/lib/occi/infrastructure/mixins/os_tpl.rb +19 -0
  154. data/lib/occi/infrastructure/mixins/resource_tpl.rb +19 -0
  155. data/lib/occi/infrastructure/mixins.rb +12 -0
  156. data/lib/occi/infrastructure/model.rb +27 -0
  157. data/lib/occi/infrastructure/network.rb +5 -104
  158. data/lib/occi/infrastructure/networkinterface.rb +5 -100
  159. data/lib/occi/infrastructure/storage.rb +5 -59
  160. data/lib/occi/infrastructure/storagelink.rb +5 -54
  161. data/lib/occi/infrastructure/version.rb +5 -0
  162. data/lib/occi/infrastructure/warehouse/actions/.gitkeep +0 -0
  163. data/lib/occi/infrastructure/warehouse/actions/attributes/.gitkeep +0 -0
  164. data/lib/occi/infrastructure/warehouse/actions/compute_restart.yml +12 -0
  165. data/lib/occi/infrastructure/warehouse/actions/compute_save.yml +19 -0
  166. data/lib/occi/infrastructure/warehouse/actions/compute_start.yml +4 -0
  167. data/lib/occi/infrastructure/warehouse/actions/compute_stop.yml +12 -0
  168. data/lib/occi/infrastructure/warehouse/actions/compute_suspend.yml +12 -0
  169. data/lib/occi/infrastructure/warehouse/actions/network_down.yml +4 -0
  170. data/lib/occi/infrastructure/warehouse/actions/network_up.yml +4 -0
  171. data/lib/occi/infrastructure/warehouse/actions/networkinterface_down.yml +4 -0
  172. data/lib/occi/infrastructure/warehouse/actions/networkinterface_up.yml +4 -0
  173. data/lib/occi/infrastructure/warehouse/actions/storage_backup.yml +4 -0
  174. data/lib/occi/infrastructure/warehouse/actions/storage_offline.yml +4 -0
  175. data/lib/occi/infrastructure/warehouse/actions/storage_online.yml +4 -0
  176. data/lib/occi/infrastructure/warehouse/actions/storage_resize.yml +12 -0
  177. data/lib/occi/infrastructure/warehouse/actions/storage_snapshot.yml +4 -0
  178. data/lib/occi/infrastructure/warehouse/actions/storagelink_offline.yml +4 -0
  179. data/lib/occi/infrastructure/warehouse/actions/storagelink_online.yml +4 -0
  180. data/lib/occi/infrastructure/warehouse/kinds/.gitkeep +0 -0
  181. data/lib/occi/infrastructure/warehouse/kinds/attributes/.gitkeep +0 -0
  182. data/lib/occi/infrastructure/warehouse/kinds/attributes/occi.compute.architecture.yml +7 -0
  183. data/lib/occi/infrastructure/warehouse/kinds/attributes/occi.compute.cores.yml +7 -0
  184. data/lib/occi/infrastructure/warehouse/kinds/attributes/occi.compute.hostname.yml +7 -0
  185. data/lib/occi/infrastructure/warehouse/kinds/attributes/occi.compute.memory.yml +7 -0
  186. data/lib/occi/infrastructure/warehouse/kinds/attributes/occi.compute.speed.yml +7 -0
  187. data/lib/occi/infrastructure/warehouse/kinds/attributes/occi.compute.state.yml +7 -0
  188. data/lib/occi/infrastructure/warehouse/kinds/attributes/occi.network.label.yml +7 -0
  189. data/lib/occi/infrastructure/warehouse/kinds/attributes/occi.network.state.yml +7 -0
  190. data/lib/occi/infrastructure/warehouse/kinds/attributes/occi.network.vlan.yml +7 -0
  191. data/lib/occi/infrastructure/warehouse/kinds/attributes/occi.networkinterface.interface.yml +7 -0
  192. data/lib/occi/infrastructure/warehouse/kinds/attributes/occi.networkinterface.mac.yml +7 -0
  193. data/lib/occi/infrastructure/warehouse/kinds/attributes/occi.networkinterface.state.yml +7 -0
  194. data/lib/occi/infrastructure/warehouse/kinds/attributes/occi.storage.size.yml +7 -0
  195. data/lib/occi/infrastructure/warehouse/kinds/attributes/occi.storage.state.yml +7 -0
  196. data/lib/occi/infrastructure/warehouse/kinds/attributes/occi.storagelink.deviceid.yml +7 -0
  197. data/lib/occi/infrastructure/warehouse/kinds/attributes/occi.storagelink.mountpoint.yml +7 -0
  198. data/lib/occi/infrastructure/warehouse/kinds/attributes/occi.storagelink.state.yml +7 -0
  199. data/lib/occi/infrastructure/warehouse/kinds/compute.yml +19 -0
  200. data/lib/occi/infrastructure/warehouse/kinds/network.yml +13 -0
  201. data/lib/occi/infrastructure/warehouse/kinds/networkinterface.yml +13 -0
  202. data/lib/occi/infrastructure/warehouse/kinds/storage.yml +15 -0
  203. data/lib/occi/infrastructure/warehouse/kinds/storagelink.yml +13 -0
  204. data/lib/occi/infrastructure/warehouse/mixins/.gitkeep +0 -0
  205. data/lib/occi/infrastructure/warehouse/mixins/attributes/.gitkeep +0 -0
  206. data/lib/occi/infrastructure/warehouse/mixins/attributes/occi.network.address.yml +7 -0
  207. data/lib/occi/infrastructure/warehouse/mixins/attributes/occi.network.allocation.yml +7 -0
  208. data/lib/occi/infrastructure/warehouse/mixins/attributes/occi.network.gateway.yml +7 -0
  209. data/lib/occi/infrastructure/warehouse/mixins/attributes/occi.networkinterface.address.yml +7 -0
  210. data/lib/occi/infrastructure/warehouse/mixins/attributes/occi.networkinterface.allocation.yml +7 -0
  211. data/lib/occi/infrastructure/warehouse/mixins/attributes/occi.networkinterface.gateway.yml +7 -0
  212. data/lib/occi/infrastructure/warehouse/mixins/ipnetwork.yml +11 -0
  213. data/lib/occi/infrastructure/warehouse/mixins/ipnetworkinterface.yml +11 -0
  214. data/lib/occi/infrastructure/warehouse.rb +20 -0
  215. data/lib/occi/infrastructure-ext.rb +35 -0
  216. data/lib/occi/infrastructure.rb +34 -19
  217. data/lib/occi/infrastructure_ext/constants.rb +20 -0
  218. data/lib/occi/infrastructure_ext/instance_builder.rb +22 -0
  219. data/lib/occi/infrastructure_ext/ipreservation.rb +9 -0
  220. data/lib/occi/infrastructure_ext/mixins/.gitkeep +0 -0
  221. data/lib/occi/infrastructure_ext/mixins/availability_zone.rb +19 -0
  222. data/lib/occi/infrastructure_ext/mixins.rb +12 -0
  223. data/lib/occi/infrastructure_ext/model.rb +26 -0
  224. data/lib/occi/infrastructure_ext/securitygroup.rb +9 -0
  225. data/lib/occi/infrastructure_ext/securitygrouplink.rb +9 -0
  226. data/lib/occi/infrastructure_ext/version.rb +5 -0
  227. data/lib/occi/infrastructure_ext/warehouse/actions/.gitkeep +0 -0
  228. data/lib/occi/infrastructure_ext/warehouse/actions/attributes/.gitkeep +0 -0
  229. data/lib/occi/infrastructure_ext/warehouse/actions/ipreservation_down.yml +4 -0
  230. data/lib/occi/infrastructure_ext/warehouse/actions/ipreservation_up.yml +4 -0
  231. data/lib/occi/infrastructure_ext/warehouse/kinds/.gitkeep +0 -0
  232. data/lib/occi/infrastructure_ext/warehouse/kinds/attributes/.gitkeep +0 -0
  233. data/lib/occi/infrastructure_ext/warehouse/kinds/attributes/occi.ipreservation.address.yml +7 -0
  234. data/lib/occi/infrastructure_ext/warehouse/kinds/attributes/occi.ipreservation.state.yml +7 -0
  235. data/lib/occi/infrastructure_ext/warehouse/kinds/attributes/occi.ipreservation.used.yml +7 -0
  236. data/lib/occi/infrastructure_ext/warehouse/kinds/attributes/occi.securitygroup.rules.yml +7 -0
  237. data/lib/occi/infrastructure_ext/warehouse/kinds/attributes/occi.securitygroup.state.yml +7 -0
  238. data/lib/occi/infrastructure_ext/warehouse/kinds/attributes/occi.securitygrouplink.state.yml +7 -0
  239. data/lib/occi/infrastructure_ext/warehouse/kinds/ipreservation.yml +13 -0
  240. data/lib/occi/infrastructure_ext/warehouse/kinds/securitygroup.yml +9 -0
  241. data/lib/occi/infrastructure_ext/warehouse/kinds/securitygrouplink.yml +8 -0
  242. data/lib/occi/infrastructure_ext/warehouse/mixins/.gitkeep +0 -0
  243. data/lib/occi/infrastructure_ext/warehouse/mixins/attributes/.gitkeep +0 -0
  244. data/lib/occi/infrastructure_ext/warehouse.rb +20 -0
  245. data/lib/occi/monkey_island/boolean.rb +6 -0
  246. data/lib/occi/monkey_island/hash.rb +4 -0
  247. data/occi-core.gemspec +26 -26
  248. metadata +312 -243
  249. data/.yardopts +0 -1
  250. data/config/occi.yml +0 -4
  251. data/lib/occi/collection.rb +0 -265
  252. data/lib/occi/core/actions.rb +0 -21
  253. data/lib/occi/core/attributes.rb +0 -411
  254. data/lib/occi/core/categories.rb +0 -64
  255. data/lib/occi/core/dependencies.rb +0 -7
  256. data/lib/occi/core/entities.rb +0 -48
  257. data/lib/occi/core/kinds.rb +0 -22
  258. data/lib/occi/core/links.rb +0 -37
  259. data/lib/occi/core/mixins.rb +0 -37
  260. data/lib/occi/core/properties.rb +0 -108
  261. data/lib/occi/core/resources.rb +0 -14
  262. data/lib/occi/errors/attribute_definitions_converted_error.rb +0 -5
  263. data/lib/occi/errors/attribute_missing_error.rb +0 -5
  264. data/lib/occi/errors/attribute_name_invalid_error.rb +0 -5
  265. data/lib/occi/errors/attribute_not_defined_error.rb +0 -5
  266. data/lib/occi/errors/attribute_property_type_error.rb +0 -5
  267. data/lib/occi/errors/attribute_type_error.rb +0 -5
  268. data/lib/occi/errors/category_not_defined_error.rb +0 -5
  269. data/lib/occi/errors/kind_not_defined_error.rb +0 -5
  270. data/lib/occi/errors/parser_input_error.rb +0 -5
  271. data/lib/occi/errors/parser_type_error.rb +0 -5
  272. data/lib/occi/errors.rb +0 -1
  273. data/lib/occi/extensions/hashie.rb +0 -25
  274. data/lib/occi/helpers/comparators/action_instance.rb +0 -22
  275. data/lib/occi/helpers/comparators/attributes.rb +0 -22
  276. data/lib/occi/helpers/comparators/categories.rb +0 -22
  277. data/lib/occi/helpers/comparators/category.rb +0 -22
  278. data/lib/occi/helpers/comparators/collection.rb +0 -40
  279. data/lib/occi/helpers/comparators/entities.rb +0 -22
  280. data/lib/occi/helpers/comparators/entity.rb +0 -22
  281. data/lib/occi/helpers/comparators/properties.rb +0 -26
  282. data/lib/occi/helpers/comparators.rb +0 -1
  283. data/lib/occi/helpers/inspect.rb +0 -12
  284. data/lib/occi/infrastructure/network/ipnetwork.rb +0 -28
  285. data/lib/occi/infrastructure/networkinterface/ipnetworkinterface.rb +0 -28
  286. data/lib/occi/infrastructure/os_tpl.rb +0 -20
  287. data/lib/occi/infrastructure/resource_tpl.rb +0 -20
  288. data/lib/occi/log.rb +0 -71
  289. data/lib/occi/model.rb +0 -87
  290. data/lib/occi/parser/json.rb +0 -38
  291. data/lib/occi/parser/text/constants.rb +0 -89
  292. data/lib/occi/parser/text.rb +0 -280
  293. data/lib/occi/parser.rb +0 -144
  294. data/lib/occi/settings.rb +0 -10
  295. data/lib/occi/version.rb +0 -3
  296. data/lib/occi-core.rb +0 -53
  297. data/spec/helpers/to_plain.rb +0 -39
  298. data/spec/helpers/yaml_hash.rb +0 -32
  299. data/spec/occi/collection_samples/collection1.json +0 -1
  300. data/spec/occi/collection_samples/directory2/collection2.json +0 -1
  301. data/spec/occi/collection_spec.rb +0 -1003
  302. data/spec/occi/core/action_instance_spec.rb +0 -317
  303. data/spec/occi/core/action_spec.rb +0 -71
  304. data/spec/occi/core/attributes_spec.rb +0 -665
  305. data/spec/occi/core/categories_spec.rb +0 -28
  306. data/spec/occi/core/category_spec.rb +0 -216
  307. data/spec/occi/core/entities_spec.rb +0 -160
  308. data/spec/occi/core/entity_spec.rb +0 -374
  309. data/spec/occi/core/kind_spec.rb +0 -182
  310. data/spec/occi/core/link_spec.rb +0 -52
  311. data/spec/occi/core/links_spec.rb +0 -132
  312. data/spec/occi/core/mixin_spec.rb +0 -38
  313. data/spec/occi/core/mixins_spec.rb +0 -107
  314. data/spec/occi/core/properties_spec.rb +0 -171
  315. data/spec/occi/core/resource_spec.rb +0 -42
  316. data/spec/occi/core_spec.rb +0 -12
  317. data/spec/occi/infrastructure/compute_spec.rb +0 -229
  318. data/spec/occi/infrastructure/network_spec.rb +0 -96
  319. data/spec/occi/infrastructure/networkinterface_spec.rb +0 -96
  320. data/spec/occi/infrastructure/storage_spec.rb +0 -33
  321. data/spec/occi/infrastructure/storagelink_spec.rb +0 -45
  322. data/spec/occi/log_spec.rb +0 -117
  323. data/spec/occi/model_spec.rb +0 -256
  324. data/spec/occi/parser/json_samples/test.json +0 -49
  325. data/spec/occi/parser/text_samples/occi_categories.body_plain.expected +0 -2
  326. data/spec/occi/parser/text_samples/occi_categories.expected +0 -2
  327. data/spec/occi/parser/text_samples/occi_categories.parse_headers.expected +0 -2
  328. data/spec/occi/parser/text_samples/occi_categories.text +0 -2
  329. data/spec/occi/parser/text_samples/occi_compute_rocci_server.expected +0 -11
  330. data/spec/occi/parser/text_samples/occi_compute_rocci_server.text +0 -10
  331. data/spec/occi/parser/text_samples/occi_link_resource_instance.body_plain.expected +0 -7
  332. data/spec/occi/parser/text_samples/occi_link_resource_instance.expected +0 -7
  333. data/spec/occi/parser/text_samples/occi_link_resource_instance.parse.expected +0 -7
  334. data/spec/occi/parser/text_samples/occi_link_resource_instance.text +0 -7
  335. data/spec/occi/parser/text_samples/occi_link_simple.expected +0 -7
  336. data/spec/occi/parser/text_samples/occi_link_simple.text +0 -1
  337. data/spec/occi/parser/text_samples/occi_link_w_attributes.expected +0 -7
  338. data/spec/occi/parser/text_samples/occi_link_w_attributes.text +0 -7
  339. data/spec/occi/parser/text_samples/occi_link_w_category.expected +0 -7
  340. data/spec/occi/parser/text_samples/occi_link_w_category.text +0 -3
  341. data/spec/occi/parser/text_samples/occi_model_categories.expected +0 -3
  342. data/spec/occi/parser/text_samples/occi_model_rocci_server.text +0 -51
  343. data/spec/occi/parser/text_samples/occi_network_rocci_server.body_plain.expected +0 -11
  344. data/spec/occi/parser/text_samples/occi_network_rocci_server.expected +0 -11
  345. data/spec/occi/parser/text_samples/occi_network_rocci_server.resource.header.expected +0 -9
  346. data/spec/occi/parser/text_samples/occi_network_rocci_server.resource.rack.expected +0 -9
  347. data/spec/occi/parser/text_samples/occi_network_rocci_server.text +0 -11
  348. data/spec/occi/parser/text_samples/occi_resource_custom_class_w_attributes.parse.expected +0 -15
  349. data/spec/occi/parser/text_samples/occi_resource_custom_class_w_attributes.text +0 -15
  350. data/spec/occi/parser/text_samples/occi_resource_w_attributes.text +0 -11
  351. data/spec/occi/parser/text_samples/occi_resource_w_inline_links.expected +0 -1
  352. data/spec/occi/parser/text_samples/occi_resource_w_inline_links.text +0 -16
  353. data/spec/occi/parser/text_samples/occi_resource_w_inline_links_only.text +0 -13
  354. data/spec/occi/parser/text_samples/occi_storage_rocci_server.expected +0 -9
  355. data/spec/occi/parser/text_samples/occi_storage_rocci_server.text +0 -9
  356. data/spec/occi/parser/text_spec.rb +0 -320
  357. data/spec/occi/parser_spec.rb +0 -259
  358. data/spec/occi-core_spec.rb +0 -31
  359. data/spec/spec_helper.rb +0 -18
@@ -0,0 +1,128 @@
1
+ module Occi
2
+ module Core
3
+ module Parsers
4
+ # Contains all JSON-related classes and modules. This
5
+ # module houses functionality transforming various internal
6
+ # instances from basic JSON rendering.
7
+ #
8
+ # @author Boris Parak <parak@cesnet.cz>
9
+ module Json; end
10
+ end
11
+ end
12
+ end
13
+
14
+ # Load class-specific parsing primitives
15
+ Dir[File.join(File.dirname(__FILE__), 'json', '*.rb')].each { |file| require file.gsub('.rb', '') }
16
+
17
+ module Occi
18
+ module Core
19
+ module Parsers
20
+ # Implementes components necessary to parse all required instance types
21
+ # from `JSON` or `JSON`-like format.
22
+ #
23
+ # @author Boris Parak <parak@cesnet.cz>
24
+ class JsonParser < BaseParser
25
+ # Media type constants
26
+ MEDIA_TYPES = %w[application/occi+json application/json].freeze
27
+
28
+ # Parses entities from the given body/headers. Only kinds, mixins, and actions already declared
29
+ # in the model are allowed.
30
+ #
31
+ # @param body [String] raw `String`-like body as provided by the transport protocol
32
+ # @param headers [Hash] raw headers as provided by the transport protocol
33
+ # @param expectation [Class] expected class of the returned instance(s)
34
+ # @return [Set] set of instances
35
+ def entities(body, _headers = nil, expectation = nil)
36
+ expectation ||= Occi::Core::Entity
37
+ type = validate_entities! body
38
+
39
+ entity_parser = Json::Entity.new(model: model)
40
+ entities = entity_parser.json body, type
41
+ entities.each do |entity|
42
+ unless entity.is_a?(expectation)
43
+ raise Occi::Core::Errors::ParsingError, "#{self.class} -> Given entity isn't #{expectation}"
44
+ end
45
+ end
46
+
47
+ entities
48
+ end
49
+
50
+ # Parses action instances from the given body/headers. Only actions already declared in the model are
51
+ # allowed.
52
+ #
53
+ # @param body [String] raw `String`-like body as provided by the transport protocol
54
+ # @param headers [Hash] raw headers as provided by the transport protocol
55
+ # @return [Set] set of parsed instances
56
+ def action_instances(body, _headers = nil)
57
+ Json::Validator.validate_action_instance! body
58
+ Set.new [Json::ActionInstance.json(body, model)]
59
+ end
60
+
61
+ # Parses categories from the given body/headers and returns corresponding instances
62
+ # from the known model.
63
+ #
64
+ # @param body [String] raw `String`-like body as provided by the transport protocol
65
+ # @param headers [Hash] raw headers as provided by the transport protocol
66
+ # @param expectation [Class] expected class of the returned instance(s)
67
+ # @return [Set] set of instances
68
+ def categories(body, _headers = nil, expectation = nil)
69
+ expectation ||= Occi::Core::Category
70
+ Json::Validator.validate_model! body
71
+ Json::Category.json(body, Set.new, false).map { |c| lookup c.identifier, expectation }.to_set
72
+ end
73
+
74
+ # :nodoc:
75
+ def validate_entities!(body)
76
+ found = nil
77
+
78
+ %i[link resource entity-collection].each do |type|
79
+ begin
80
+ Json::Validator.validate! body, type
81
+ found = type
82
+ rescue
83
+ logger.debug "#{self.class}: Body isn't #{type}"
84
+ end
85
+ end
86
+ raise Occi::Core::Errors::ParsingError, "#{self.class} -> No entity sub-type instance found" unless found
87
+
88
+ found
89
+ end
90
+ private :validate_entities!
91
+
92
+ class << self
93
+ # Extracts categories from body and headers. For details, see `Occi::Core::Parsers::Json::Category`.
94
+ #
95
+ # @param body [String] raw `String`-like body as provided by the transport protocol
96
+ # @param headers [Hash] raw headers as provided by the transport protocol
97
+ # @param media_type [String] media type string as provided by the transport protocol
98
+ # @param model [Occi::Core::Model] `Model`-like instance to be populated (may contain existing categories)
99
+ # @return [Occi::Core::Model] model instance filled with parsed categories
100
+ def model(body, _headers, media_type, model)
101
+ unless media_types.include?(media_type)
102
+ raise Occi::Core::Errors::ParsingError,
103
+ "#{self} -> model cannot be parsed from #{media_type.inspect}"
104
+ end
105
+ Json::Validator.validate_model! body
106
+ Json::Category.json body, model
107
+ model
108
+ end
109
+
110
+ # Extracts URI-like locations from body and headers. For details, see `Occi::Core::Parsers::Json::Location`.
111
+ #
112
+ # @param body [String] raw `String`-like body as provided by the transport protocol
113
+ # @param headers [Hash] raw headers as provided by the transport protocol
114
+ # @param media_type [String] media type string as provided by the transport protocol
115
+ # @return [Array] list of extracted URIs
116
+ def locations(body, _headers, media_type)
117
+ unless media_types.include?(media_type)
118
+ raise Occi::Core::Errors::ParsingError,
119
+ "#{self} -> locations cannot be parsed from #{media_type.inspect}"
120
+ end
121
+ Json::Validator.validate_locations! body
122
+ handle(Occi::Core::Errors::ParsingError) { JSON.parse(body).map { |i| URI.parse(i) } }
123
+ end
124
+ end
125
+ end
126
+ end
127
+ end
128
+ end
File without changes
@@ -0,0 +1,183 @@
1
+ module Occi
2
+ module Core
3
+ module Parsers
4
+ module Text
5
+ # Static parsing class responsible for extracting categories from plain text.
6
+ # Class supports 'text/plain' via `plain`. No other formats are supported.
7
+ #
8
+ # @author Boris Parak <parak@cesnet.cz>
9
+ class Category
10
+ include Yell::Loggable
11
+ extend Helpers::ParserDereferencer
12
+
13
+ # Regexp constants
14
+ CATEGORY_REGEXP = /#{Constants::REGEXP_CATEGORY}/
15
+ ATTRIBUTE_REGEXP = /^#{Constants::REGEXP_ATTRIBUTE_DEF}$/
16
+
17
+ # Hash constants for ParserDereferencer
18
+ PARENT_KEY = :rel
19
+ APPLIES_KEY = :rel
20
+ DEPENDS_KEY = :rel
21
+
22
+ class << self
23
+ # Parses category lines into instances of subtypes of `Occi::Core::Category`. Internal references
24
+ # between objects are converted from strings to actual objects. Categories provided in the model
25
+ # will be reused but have to be declared in the parsed model as well. This mechanism can be used to
26
+ # introduce properly typed attribute definitions to 'plain/text'-based models.
27
+ #
28
+ # @param lines [Array] list of single-category lines
29
+ # @param model [Occi::Core::Model] model with existing categories
30
+ # @return [Occi::Core::Model] model with all known category instances
31
+ def plain(lines, model)
32
+ raw_categories = []
33
+
34
+ lines.each do |line|
35
+ raw_categories << plain_category(line)
36
+ model << construct_instance(raw_categories.last)
37
+ end
38
+ dereference_identifiers! model.categories, raw_categories
39
+
40
+ model
41
+ end
42
+
43
+ # Parses a single-category line into a raw category hash containing all the necessary
44
+ # information for creating an instance.
45
+ #
46
+ # @param line [String] single-category line containing the definition
47
+ # @param full [TrueClass, FalseClass] parse full definition, defaults to `true`
48
+ # @return [Hash] raw category hash for further processing
49
+ def plain_category(line, full = true)
50
+ matched = line.match(CATEGORY_REGEXP)
51
+ unless matched
52
+ raise Occi::Core::Errors::ParsingError,
53
+ "#{self} -> #{line.inspect} does not match expectations for Category"
54
+ end
55
+
56
+ cat = matchdata_to_hash(matched)
57
+ return cat unless full
58
+
59
+ cat[:attributes] = plain_attributes(cat[:attributes]) if cat[:attributes]
60
+ cat[:rel] = plain_identifiers(cat[:rel]) if cat[:rel]
61
+ cat[:actions] = plain_identifiers(cat[:actions]) if cat[:actions]
62
+
63
+ cat
64
+ end
65
+
66
+ # Parses a multi-attribute line into a multi-attribute hash. The resulting hash has
67
+ # attribute names pointing to attribute definitions.
68
+ #
69
+ # @example
70
+ # plain_attributes 'occi.core.id{required immutable} occi.storage.size' # => {...}
71
+ #
72
+ # @param line [String] multi-attribute line from a category definition
73
+ # @return [Hash] hash with attribute names pointing to attribute definitions
74
+ def plain_attributes(line)
75
+ # TODO: find a better approach to fixing split
76
+ line.gsub!(/\{(immutable|required)\s+(required|immutable)\}/, '{\1_\2}')
77
+
78
+ attributes = {}
79
+ line.split.each { |attribute| attributes.merge! plain_attribute(attribute) }
80
+
81
+ attributes
82
+ end
83
+
84
+ # Parses an attribute line into an attribute hash. The resulting hash has
85
+ # the attribute name pointing to the attribute definition.
86
+ #
87
+ # @example
88
+ # plain_attribute 'occi.core.id{required immutable}' # => {...}
89
+ #
90
+ # @param line [String] single-attribute line from a category definition
91
+ # @return [Hash] hash with attribute name pointing to attribute definition
92
+ def plain_attribute(line)
93
+ # TODO: find a better approach to fixing split
94
+ line.gsub!(/\{(immutable|required)_(required|immutable)\}/, '{\1 \2}')
95
+
96
+ matched = line.match(ATTRIBUTE_REGEXP)
97
+ unless matched && matched[1]
98
+ raise Occi::Core::Errors::ParsingError,
99
+ "#{self} -> #{line.inspect} does not match expectations for Attribute"
100
+ end
101
+
102
+ { matched[1] => plain_attribute_definition(matched[-2]) }
103
+ end
104
+
105
+ # Parses a line with attribute definitions into an `Occi::Core::AttributeDefinition` instance.
106
+ #
107
+ # @example
108
+ # plain_attribute_definition '{required immutable}'
109
+ # # => Occi::Core::AttributeDefinition
110
+ #
111
+ # @param line [String] line with plain text attribute definition(s)
112
+ # @return [Occi::Core::AttributeDefinition] constructed instance
113
+ def plain_attribute_definition(line)
114
+ attr_def = Occi::Core::AttributeDefinition.new
115
+ return attr_def if line.blank?
116
+
117
+ attr_def.required! if line.include?('required')
118
+ attr_def.immutable! if line.include?('immutable')
119
+
120
+ attr_def
121
+ end
122
+
123
+ # Separates category identifiers from a single-line notation to an
124
+ # array.
125
+ #
126
+ # @example
127
+ # plain_identifiers 'http://l/b/c#a http://a/a/b#r' # => [...]
128
+ #
129
+ # @param line [String] line with space-separated category identifiers
130
+ # @return [Array] list of separated category identifiers
131
+ def plain_identifiers(line)
132
+ line.split.map(&:strip)
133
+ end
134
+
135
+ # Constructs an instance of `parsed[:class]` with given arguments.
136
+ # All classes must be located in the `Occi::Core` namespace.
137
+ #
138
+ # @param parsed [Hash] arguments for instance construction
139
+ # @return [Object] constructed instance
140
+ def construct_instance(parsed)
141
+ all = {
142
+ term: parsed[:term], schema: parsed[:scheme], title: parsed[:title],
143
+ attributes: parsed[:attributes] ? parsed[:attributes] : {}
144
+ }
145
+
146
+ klass = parsed[:class].capitalize
147
+ klass = Occi::Core.const_get(klass)
148
+ all[:location] = parsed[:location] if klass.instance_methods.include?(:location)
149
+
150
+ klass.new(all)
151
+ end
152
+
153
+ # @param mixin [Occi::Core::Mixin] mixin instance needing applicability dereferencing
154
+ # @param derefd [Array] list of all available category instances
155
+ # @param parsed_rel [Array] textual representations of needed applicability targets
156
+ def lookup_applies_references!(mixin, derefd, parsed_rel)
157
+ return if parsed_rel.blank? || parsed_rel.count == 1 # only depends here
158
+ parsed_rel.drop(1).each { |kind| mixin.applies << first_or_die(derefd, kind) }
159
+ end
160
+
161
+ # @param mixin [Occi::Core::Mixin] mixin instance needing dependency dereferencing
162
+ # @param derefd [Array] list of all available category instances
163
+ # @param parsed_rel [Array] textual representations of needed dependencies
164
+ def lookup_depends_references!(mixin, derefd, parsed_rel)
165
+ return if parsed_rel.blank?
166
+ mixin.depends << first_or_die(derefd, parsed_rel.first)
167
+ end
168
+
169
+ # @param md [MatchData] `MatchData` instance to be converted
170
+ # @return [Hash] converted hash
171
+ def matchdata_to_hash(md)
172
+ hash = {}
173
+ md.names.each { |group| md[group] && hash[group.to_sym] = md[group] }
174
+ hash
175
+ end
176
+
177
+ private :lookup_applies_references!, :lookup_depends_references!
178
+ end
179
+ end
180
+ end
181
+ end
182
+ end
183
+ end
@@ -0,0 +1,89 @@
1
+ module Occi
2
+ module Core
3
+ module Parsers
4
+ module Text
5
+ module Constants
6
+ # Base regular expressions
7
+ REGEXP_QUOTED_STRING = /([^"\\]|\\.)*/
8
+ REGEXP_LOALPHA = /[a-z]/
9
+ REGEXP_ALPHA = /[a-zA-Z]/
10
+ REGEXP_DIGIT = /[0-9]/
11
+ REGEXP_INT = /#{REGEXP_DIGIT}+/
12
+ REGEXP_FLOAT = /#{REGEXP_INT}\.#{REGEXP_INT}/
13
+ REGEXP_NUMBER = /#{REGEXP_FLOAT}|#{REGEXP_INT}/
14
+ REGEXP_BOOL = /\b(?<!\|)true(?!\|)\b|\b(?<!\|)false(?!\|)\b/
15
+
16
+ # Regular expressions for OCCI
17
+ REGEXP_TERM = /(#{REGEXP_ALPHA}|#{REGEXP_DIGIT})(#{REGEXP_ALPHA}|#{REGEXP_DIGIT}|-|_)*/ # Compatibility with terms starting with a number
18
+ REGEXP_TERM_STRICT = /#{REGEXP_LOALPHA}(#{REGEXP_LOALPHA}|#{REGEXP_DIGIT}|-|_)*/
19
+ REGEXP_SCHEME = /#{URI::ABS_URI_REF}#/
20
+ REGEXP_TYPE_IDENTIFIER = /#{REGEXP_SCHEME}#{REGEXP_TERM}/
21
+ REGEXP_TYPE_IDENTIFIER_STRICT = /#{REGEXP_SCHEME}#{REGEXP_TERM_STRICT}/
22
+ REGEXP_CLASS = /\b(?<!\|)action(?!\|)\b|\b(?<!\|)mixin(?!\|)\b|\b(?<!\|)kind(?!\|)\b/
23
+ REGEXP_TYPE_IDENTIFIER_LIST = /#{REGEXP_TYPE_IDENTIFIER}(\s+#{REGEXP_TYPE_IDENTIFIER})*/
24
+ REGEXP_TYPE_IDENTIFIER_LIST_STRICT = /#{REGEXP_TYPE_IDENTIFIER_STRICT}(\s+#{REGEXP_TYPE_IDENTIFIER_STRICT})*/
25
+
26
+ REGEXP_ATTR_COMPONENT = /#{REGEXP_LOALPHA}(#{REGEXP_LOALPHA}|#{REGEXP_DIGIT}|-|_)*/
27
+ REGEXP_ATTRIBUTE_NAME = /#{REGEXP_ATTR_COMPONENT}(\.#{REGEXP_ATTR_COMPONENT})*/
28
+ REGEXP_ATTRIBUTE_PROPERTY = /\b(?<!\|)immutable(?!\|)\b|\b(?<!\|)required(?!\|)\b/
29
+ REGEXP_ATTRIBUTE_DEF = /(#{REGEXP_ATTRIBUTE_NAME})(\{#{REGEXP_ATTRIBUTE_PROPERTY}(\s+#{REGEXP_ATTRIBUTE_PROPERTY})*\})?/
30
+ REGEXP_ATTRIBUTE_LIST = /#{REGEXP_ATTRIBUTE_DEF}(\s+#{REGEXP_ATTRIBUTE_DEF})*/
31
+ REGEXP_ATTRIBUTE_REPR = /#{REGEXP_ATTRIBUTE_NAME}=("#{REGEXP_QUOTED_STRING}"|#{REGEXP_NUMBER}|#{REGEXP_BOOL})/
32
+
33
+ REGEXP_ACTION = /#{REGEXP_TYPE_IDENTIFIER}/
34
+ REGEXP_ACTION_STRICT = /#{REGEXP_TYPE_IDENTIFIER_STRICT}/
35
+ REGEXP_ACTION_LIST = /#{REGEXP_ACTION}(\s+#{REGEXP_ACTION})*/
36
+ REGEXP_ACTION_LIST_STRICT = /#{REGEXP_ACTION_STRICT}(\s+#{REGEXP_ACTION_STRICT})*/
37
+
38
+ REGEXP_RESOURCE_TYPE = /#{REGEXP_TYPE_IDENTIFIER}(\s+#{REGEXP_TYPE_IDENTIFIER})*/
39
+ REGEXP_RESOURCE_TYPE_STRICT = /#{REGEXP_TYPE_IDENTIFIER_STRICT}(\s+#{REGEXP_TYPE_IDENTIFIER_STRICT})*/
40
+ REGEXP_LINK_INSTANCE = /#{URI::URI_REF}/
41
+ REGEXP_LINK_TYPE = /#{REGEXP_TYPE_IDENTIFIER}(\s+#{REGEXP_TYPE_IDENTIFIER})*/
42
+ REGEXP_LINK_TYPE_STRICT = /#{REGEXP_TYPE_IDENTIFIER_STRICT}(\s+#{REGEXP_TYPE_IDENTIFIER_STRICT})*/
43
+
44
+ # Regular expression for OCCI Categories
45
+ REGEXP_CATEGORY = "^Category:\\s*(?<term>#{REGEXP_TERM})" << # term (mandatory)
46
+ ";\\s*scheme=\"(?<scheme>#{REGEXP_SCHEME})#{REGEXP_TERM}?\"" << # scheme (mandatory)
47
+ ";\\s*class=\"?(?<class>#{REGEXP_CLASS})\"?" << # class (mandatory)
48
+ "(;\\s*title=\"(?<title>#{REGEXP_QUOTED_STRING})\")?" << # title (optional)
49
+ "(;\\s*rel=\"(?<rel>#{REGEXP_TYPE_IDENTIFIER_LIST})\")?" << # rel (optional)
50
+ "(;\\s*location=\"(?<location>#{URI::URI_REF})\")?" << # location (optional)
51
+ "(;\\s*attributes=\"(?<attributes>#{REGEXP_ATTRIBUTE_LIST})\")?" << # attributes (optional)
52
+ "(;\\s*actions=\"(?<actions>#{REGEXP_ACTION_LIST})\")?" << # actions (optional)
53
+ ';?$' # additional semicolon at the end (not specified, for interoperability)
54
+ REGEXP_CATEGORY_STRICT = "^Category:\\s*(?<term>#{REGEXP_TERM_STRICT})" << # term (mandatory)
55
+ ";\\s*scheme=\"(?<scheme>#{REGEXP_SCHEME})\"" << # scheme (mandatory)
56
+ ";\\s*class=\"(?<class>#{REGEXP_CLASS})\"" << # class (mandatory)
57
+ "(;\\s*title=\"(?<title>#{REGEXP_QUOTED_STRING})\")?" << # title (optional)
58
+ "(;\\s*rel=\"(?<rel>#{REGEXP_TYPE_IDENTIFIER_LIST_STRICT})\")?" << # rel (optional)
59
+ "(;\\s*location=\"(?<location>#{URI::URI_REF})\")?" << # location (optional)
60
+ "(;\\s*attributes=\"(?<attributes>#{REGEXP_ATTRIBUTE_LIST})\")?" << # attributes (optional)
61
+ "(;\\s*actions=\"(?<actions>#{REGEXP_ACTION_LIST_STRICT})\")?" << # actions (optional)
62
+ ';?$' # additional semicolon at the end (not specified, for interoperability)
63
+
64
+ # Regular expression for OCCI Link Instance References
65
+ REGEXP_LINK = "^Link:\\s*\\<(?<uri>#{URI::URI_REF})\\>" << # uri (mandatory)
66
+ ";\\s*rel=\"(?<rel>#{REGEXP_RESOURCE_TYPE})\"" << # rel (mandatory)
67
+ "(;\\s*self=\"(?<self>#{REGEXP_LINK_INSTANCE})\")?" << # self (optional)
68
+ "(;\\s*category=\"(?<category>(;?\\s*(#{REGEXP_LINK_TYPE}))+)\")?" << # category (optional)
69
+ "(?<attributes>(;?\\s*(#{REGEXP_ATTRIBUTE_REPR}))*)" << # attributes (optional)
70
+ ';?$' # additional semicolon at the end (not specified, for interoperability)
71
+ REGEXP_LINK_STRICT = "^Link:\\s*\\<(?<uri>#{URI::URI_REF})\\>" << # uri (mandatory)
72
+ ";\\s*rel=\"(?<rel>#{REGEXP_RESOURCE_TYPE_STRICT})\"" << # rel (mandatory)
73
+ "(;\\s*self=\"(?<self>#{REGEXP_LINK_INSTANCE})\")?" << # self (optional)
74
+ "(;\\s*category=\"(?<category>(;?\\s*(#{REGEXP_LINK_TYPE_STRICT}))+)\")?" << # category (optional)
75
+ "(?<attributes>(;\\s*(#{REGEXP_ATTRIBUTE_REPR}))*)" << # attributes (optional)
76
+ ';?$' # additional semicolon at the end (not specified, for interoperability)
77
+
78
+ # Regular expression for OCCI Entity Attributes
79
+ REGEXP_ATTRIBUTE = "^X-OCCI-Attribute:\\s*(?<name>#{REGEXP_ATTRIBUTE_NAME})=(\"(?<string>#{REGEXP_QUOTED_STRING})\"|(?<number>#{REGEXP_NUMBER})|(?<bool>#{REGEXP_BOOL}))" \
80
+ ';?$'.freeze # additional semicolon at the end (not specified, for interoperability)
81
+
82
+ # Regular expression for OCCI Location
83
+ REGEXP_LOCATION = "^X-OCCI-Location:\\s*(?<location>#{URI::URI_REF})" \
84
+ ';?$'.freeze # additional semicolon at the end (not specified, for interoperability)
85
+ end
86
+ end
87
+ end
88
+ end
89
+ end
@@ -0,0 +1,257 @@
1
+ module Occi
2
+ module Core
3
+ module Parsers
4
+ module Text
5
+ # Static parsing class responsible for extracting entities from plain text.
6
+ # Class supports 'text/plain' via `plain`. No other formats are supported.
7
+ #
8
+ # @attr model [Occi::Core::Model, Occi::Infrastructure::Model] model to use as a primary reference point
9
+ #
10
+ # @author Boris Parak <parak@cesnet.cz>
11
+ class Entity
12
+ include Yell::Loggable
13
+ include Helpers::ArgumentValidator
14
+ include Helpers::ErrorHandler
15
+
16
+ # Regexp constants
17
+ ATTRIBUTE_REGEXP = /#{Constants::REGEXP_ATTRIBUTE}/
18
+ LINK_REGEXP = /#{Constants::REGEXP_LINK}/
19
+
20
+ # Typecasting lambdas
21
+ DEFAULT_LAMBDA = ->(val) { raise "#{self} -> Cannot typecast #{val.inspect} to an unknown type" }
22
+
23
+ FLOAT_LAMBDA = ->(val) { val.to_f }
24
+ JSON_LAMBDA = ->(val) { JSON.parse(val) }
25
+
26
+ TYPECASTER_HASH = {
27
+ IPAddr => ->(val) { IPAddr.new val },
28
+ URI => ->(val) { URI.parse val },
29
+ String => ->(val) { val },
30
+ Float => FLOAT_LAMBDA,
31
+ Numeric => FLOAT_LAMBDA,
32
+ Integer => ->(val) { val.to_i },
33
+ Boolean => ->(val) { val.casecmp('true') || val.casecmp('yes') },
34
+ Array => JSON_LAMBDA,
35
+ Hash => JSON_LAMBDA
36
+ }.freeze
37
+
38
+ attr_reader :model
39
+
40
+ # Constructs an instance of the entity parser. Only entities (their kinds) defined by the model are allowed.
41
+ #
42
+ # @param args [Hash] constructor arguments in a Hash
43
+ # @option args [Occi::Core::Model] :model model to use as a primary reference point
44
+ def initialize(args = {})
45
+ pre_initialize(args)
46
+ default_args! args
47
+
48
+ @model = args.fetch(:model)
49
+
50
+ post_initialize(args)
51
+ end
52
+
53
+ # Builds an entity instances from the lines provided as input.
54
+ #
55
+ # @param lines [Array] list of lines containing a single entity rendering
56
+ # @return [Occi::Core::Entity] constructed instance
57
+ def plain(lines)
58
+ cats = plain_categories(lines)
59
+ kind = cats.detect { |c| c.is_a?(Occi::Core::Kind) }
60
+ raise Occi::Core::Errors::ParsingError, "#{self.class} -> Entity does not specify its kind" unless kind
61
+
62
+ entity = @_ib.build(kind.identifier)
63
+ cats.each { |cat| cat.is_a?(Occi::Core::Mixin) && entity << cat }
64
+
65
+ plain_attributes! lines, entity.attributes
66
+ plain_links! lines, entity
67
+
68
+ entity
69
+ end
70
+
71
+ # Parses categories from entity lines. Every category is looked up in the model.
72
+ #
73
+ # @param lines [Array] list of lines containing a single entity rendering
74
+ # @return [Array] list of identified category instances
75
+ def plain_categories(lines)
76
+ lines.map do |line|
77
+ next unless line.start_with?(TextParser::CATEGORY_KEYS.first)
78
+ cat = Category.plain_category(line, false)
79
+ handle(Occi::Core::Errors::ParsingError) { model.find_by_identifier!("#{cat[:scheme]}#{cat[:term]}") }
80
+ end.compact
81
+ end
82
+
83
+ # Parses attributes from entity lines. Every attribute value is typed according to the attribute
84
+ # specification provided by the model (in the defined kind).
85
+ #
86
+ # @param lines [Array] list of lines containing a single entity rendering
87
+ # @param attributes [Hash] defined attributes
88
+ # @return [Hash] parsed and typed attributes
89
+ def plain_attributes!(lines, attributes)
90
+ lines.each do |line|
91
+ next unless line.start_with?(TextParser::ATTRIBUTE_KEYS.first)
92
+ name, value = raw_attribute(line)
93
+ unless attributes[name]
94
+ raise Occi::Core::Errors::ParsingError,
95
+ "#{self.class} -> attribute #{name.inspect} is not allowed for this entity"
96
+ end
97
+ attributes[name].value = handle(Occi::Core::Errors::ParsingError) do
98
+ typecast value, attributes[name].attribute_definition.type
99
+ end
100
+ end
101
+ attributes
102
+ end
103
+
104
+ # Parses a single attribute line to identify name and value.
105
+ #
106
+ # @param line [String] line containing a single entity attribute
107
+ # @return [Array] two-element array with name and value of the attribute
108
+ def raw_attribute(line)
109
+ matched = line.match(ATTRIBUTE_REGEXP)
110
+ unless matched
111
+ raise Occi::Core::Errors::ParsingError,
112
+ "#{self.class} -> #{line.inspect} does not match expectations for Attribute"
113
+ end
114
+ [matched[:name], matched[:string] || matched[:number] || matched[:bool]]
115
+ end
116
+
117
+ # Parses links attached to the entity instance. This includes both action "links" and ordinary
118
+ # OCCI links between resources.
119
+ #
120
+ # @param lines [Array] list of lines containing a single entity rendering
121
+ # @param entity [Occi::Core::Entity] partially constructed entity instance to be updated
122
+ # @return [Occi::Core::Entity] updated entity instance
123
+ def plain_links!(lines, entity)
124
+ lines.each do |line|
125
+ next unless line.start_with?(TextParser::LINK_KEYS.first)
126
+ matched = line.match(LINK_REGEXP)
127
+ unless matched
128
+ raise Occi::Core::Errors::ParsingError,
129
+ "#{self.class} -> #{line.inspect} does not match expectations for Link"
130
+ end
131
+ plain_link! matched, entity
132
+ end
133
+ entity
134
+ end
135
+
136
+ # Constructs a single link instance. This includes both action "links" and ordinary OCCI links.
137
+ #
138
+ # @param md [MatchData] Hash-like structure with matched parts of the link
139
+ # @param entity [Occi::Core::Entity] partially constructed entity instance to be updated
140
+ def plain_link!(md, entity)
141
+ md[:uri].include?('?action=') ? plain_action!(md, entity) : plain_oglink!(md, entity)
142
+ end
143
+
144
+ # Looks up the action mentioned in the given action "link" and assigns it to the given partially
145
+ # constructed entity instance.
146
+ #
147
+ # @param md [MatchData] Hash-like structure with matched parts of the link
148
+ # @param entity [Occi::Core::Entity] partially constructed entity instance to be updated
149
+ def plain_action!(md, entity)
150
+ entity << handle(Occi::Core::Errors::ParsingError) { model.find_by_identifier!(md[:rel]) }
151
+ end
152
+
153
+ # Constructs a single link instance. Supports only ordinary OCCI links between resources.
154
+ #
155
+ # @param md [MatchData] Hash-like structure with matched parts of the link
156
+ # @param entity [Occi::Core::Entity] partially constructed entity instance to be updated
157
+ def plain_oglink!(md, entity)
158
+ unless entity.respond_to?(:links)
159
+ raise Occi::Core::Errors::ParsingError,
160
+ "#{self.class} -> Cannot assign links to entity #{entity.id} which does not support them"
161
+ end
162
+
163
+ link = plain_oglink_instance(md)
164
+ link.location = handle(Occi::Core::Errors::ParsingError) { URI.parse md[:self] }
165
+ entity.links << link
166
+
167
+ plain_oglink_attributes! md, link
168
+
169
+ entity
170
+ end
171
+
172
+ # Constructs a single link instance based on the provided data. The returned instance does include contain
173
+ # action instance attributes!
174
+ #
175
+ # @param md [MatchData] Hash-like structure with matched parts of the link
176
+ # @return [Occi::Core::Link] constructed link instance
177
+ def plain_oglink_instance(md)
178
+ if md[:category].blank? || md[:self].blank?
179
+ raise Occi::Core::Errors::ParsingError,
180
+ "#{self.class} -> Link #{md[:uri].inspect} is missing type and location information"
181
+ end
182
+
183
+ categories = md[:category].split
184
+ target_kind = handle(Occi::Core::Errors::ParsingError) { model.find_by_identifier!(md[:rel]) }
185
+ link = @_ib.build(categories.shift, target_kind: target_kind)
186
+ categories.each do |mxn|
187
+ link << handle(Occi::Core::Errors::ParsingError) { model.find_by_identifier!(mxn) }
188
+ end
189
+
190
+ link
191
+ end
192
+
193
+ # Attaches attributes to an existing link instance.
194
+ #
195
+ # @param md [MatchData] Hash-like structure with matched parts of the link
196
+ # @param link [Occi::Core::Link] partially constructed link instance to be updated
197
+ def plain_oglink_attributes!(md, link)
198
+ if md[:attributes].blank?
199
+ raise Occi::Core::Errors::ParsingError,
200
+ "#{self.class} -> Link #{link.id} is missing attribute information"
201
+ end
202
+
203
+ line = md[:attributes].strip.gsub(/^;\s*/, '')
204
+ attrs = line.split(';').map { |attrb| "#{TextParser::ATTRIBUTE_KEYS.first}: #{attrb}" }
205
+ plain_attributes! attrs, link.attributes
206
+
207
+ link
208
+ end
209
+
210
+ # Typecasts attribute values from String to the desired type.
211
+ #
212
+ # @param value [String] attribute value
213
+ # @param type [Class,Module] desired attribute type
214
+ # @return [Object] typecasted value
215
+ def typecast(value, type)
216
+ if value.nil? || type.nil?
217
+ raise Occi::Core::Errors::ParsingError,
218
+ "#{self.class} -> Cannot typecast (un)set value to (un)set type"
219
+ end
220
+
221
+ self.class.typecaster[type].call(value)
222
+ end
223
+
224
+ class << self
225
+ # Constructs a map pointing from expected attribute types to conversion lambdas.
226
+ #
227
+ # @return [Hash] typecaster hash with conversion lambdas
228
+ def typecaster
229
+ Hash.new(DEFAULT_LAMBDA).merge(TYPECASTER_HASH)
230
+ end
231
+ end
232
+
233
+ protected
234
+
235
+ # :nodoc:
236
+ def sufficient_args!(args)
237
+ return if args[:model]
238
+ raise Occi::Core::Errors::MandatoryArgumentError, "Model is a mandatory argument for #{self.class}"
239
+ end
240
+
241
+ # :nodoc:
242
+ def defaults
243
+ { model: nil }
244
+ end
245
+
246
+ # :nodoc:
247
+ def pre_initialize(args); end
248
+
249
+ # :nodoc:
250
+ def post_initialize(_args)
251
+ @_ib = model.instance_builder
252
+ end
253
+ end
254
+ end
255
+ end
256
+ end
257
+ end