res_sorcery_realtime.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 2013, Digium, Inc.
  5. *
  6. * Joshua Colp <jcolp@digium.com>
  7. *
  8. * See http://www.asterisk.org for more information about
  9. * the Asterisk project. Please do not directly contact
  10. * any of the maintainers of this project for assistance;
  11. * the project provides a web site, mailing lists and IRC
  12. * channels for your use.
  13. *
  14. * This program is free software, distributed under the terms of
  15. * the GNU General Public License Version 2. See the LICENSE file
  16. * at the top of the source tree.
  17. */
  18. /*!
  19. * \file
  20. *
  21. * \brief Sorcery Realtime Object Wizard
  22. *
  23. * \author Joshua Colp <jcolp@digium.com>
  24. */
  25. /*** MODULEINFO
  26. <support_level>core</support_level>
  27. ***/
  28. #include "asterisk.h"
  29. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  30. #include <regex.h>
  31. #include "asterisk/module.h"
  32. #include "asterisk/sorcery.h"
  33. /*! \brief They key field used to store the unique identifier for the object */
  34. #define UUID_FIELD "id"
  35. static void *sorcery_realtime_open(const char *data);
  36. static int sorcery_realtime_create(const struct ast_sorcery *sorcery, void *data, void *object);
  37. static void *sorcery_realtime_retrieve_id(const struct ast_sorcery *sorcery, void *data, const char *type, const char *id);
  38. static void *sorcery_realtime_retrieve_fields(const struct ast_sorcery *sorcery, void *data, const char *type, const struct ast_variable *fields);
  39. static void sorcery_realtime_retrieve_multiple(const struct ast_sorcery *sorcery, void *data, const char *type, struct ao2_container *objects,
  40. const struct ast_variable *fields);
  41. static void sorcery_realtime_retrieve_regex(const struct ast_sorcery *sorcery, void *data, const char *type, struct ao2_container *objects, const char *regex);
  42. static int sorcery_realtime_update(const struct ast_sorcery *sorcery, void *data, void *object);
  43. static int sorcery_realtime_delete(const struct ast_sorcery *sorcery, void *data, void *object);
  44. static void sorcery_realtime_close(void *data);
  45. static struct ast_sorcery_wizard realtime_object_wizard = {
  46. .name = "realtime",
  47. .open = sorcery_realtime_open,
  48. .create = sorcery_realtime_create,
  49. .retrieve_id = sorcery_realtime_retrieve_id,
  50. .retrieve_fields = sorcery_realtime_retrieve_fields,
  51. .retrieve_multiple = sorcery_realtime_retrieve_multiple,
  52. .retrieve_regex = sorcery_realtime_retrieve_regex,
  53. .update = sorcery_realtime_update,
  54. .delete = sorcery_realtime_delete,
  55. .close = sorcery_realtime_close,
  56. };
  57. static int sorcery_realtime_create(const struct ast_sorcery *sorcery, void *data, void *object)
  58. {
  59. const char *family = data;
  60. RAII_VAR(struct ast_variable *, fields, ast_sorcery_objectset_create(sorcery, object), ast_variables_destroy);
  61. struct ast_variable *id = ast_variable_new(UUID_FIELD, ast_sorcery_object_get_id(object), "");
  62. if (!fields || !id) {
  63. ast_variables_destroy(id);
  64. return -1;
  65. }
  66. /* Place the identifier at the front for sanity sake */
  67. id->next = fields;
  68. fields = id;
  69. return (ast_store_realtime_fields(family, fields) <= 0) ? -1 : 0;
  70. }
  71. /*! \brief Internal helper function which returns a filtered objectset.
  72. *
  73. * The following are filtered out of the objectset:
  74. * \li The id field. This is returned to the caller in an out parameter.
  75. * \li Fields that are not registered with sorcery.
  76. *
  77. * \param objectset Objectset to filter.
  78. * \param[out] id The ID of the sorcery object, as found in the objectset.
  79. * \param sorcery The sorcery instance that is requesting an objectset.
  80. * \param type The object type
  81. *
  82. * \return The filtered objectset
  83. */
  84. static struct ast_variable *sorcery_realtime_filter_objectset(struct ast_variable *objectset, struct ast_variable **id,
  85. const struct ast_sorcery *sorcery, const char *type)
  86. {
  87. struct ast_variable *previous = NULL, *field = objectset;
  88. struct ast_sorcery_object_type *object_type;
  89. object_type = ast_sorcery_get_object_type(sorcery, type);
  90. if (!object_type) {
  91. ast_log(LOG_WARNING, "Unknown sorcery object type %s. Expect errors\n", type);
  92. /* Continue since we still want to filter out the id */
  93. }
  94. while (field) {
  95. int remove_field = 0;
  96. int delete_field = 0;
  97. if (!strcmp(field->name, UUID_FIELD)) {
  98. *id = field;
  99. remove_field = 1;
  100. } else if (object_type &&
  101. !ast_sorcery_is_object_field_registered(object_type, field->name)) {
  102. ast_debug(1, "Filtering out realtime field '%s' from retrieval\n", field->name);
  103. remove_field = 1;
  104. delete_field = 1;
  105. }
  106. if (remove_field) {
  107. struct ast_variable *removed;
  108. if (previous) {
  109. previous->next = field->next;
  110. } else {
  111. objectset = field->next;
  112. }
  113. removed = field;
  114. field = field->next;
  115. removed->next = NULL;
  116. if (delete_field) {
  117. ast_variables_destroy(removed);
  118. }
  119. } else {
  120. previous = field;
  121. field = field->next;
  122. }
  123. }
  124. return objectset;
  125. }
  126. static void *sorcery_realtime_retrieve_fields(const struct ast_sorcery *sorcery, void *data, const char *type, const struct ast_variable *fields)
  127. {
  128. const char *family = data;
  129. RAII_VAR(struct ast_variable *, objectset, NULL, ast_variables_destroy);
  130. RAII_VAR(struct ast_variable *, id, NULL, ast_variables_destroy);
  131. void *object = NULL;
  132. if (!(objectset = ast_load_realtime_fields(family, fields))) {
  133. return NULL;
  134. }
  135. objectset = sorcery_realtime_filter_objectset(objectset, &id, sorcery, type);
  136. if (!id || !(object = ast_sorcery_alloc(sorcery, type, id->value)) || ast_sorcery_objectset_apply(sorcery, object, objectset)) {
  137. return NULL;
  138. }
  139. return object;
  140. }
  141. static void *sorcery_realtime_retrieve_id(const struct ast_sorcery *sorcery, void *data, const char *type, const char *id)
  142. {
  143. RAII_VAR(struct ast_variable *, fields, ast_variable_new(UUID_FIELD, id, ""), ast_variables_destroy);
  144. return sorcery_realtime_retrieve_fields(sorcery, data, type, fields);
  145. }
  146. static void sorcery_realtime_retrieve_multiple(const struct ast_sorcery *sorcery, void *data, const char *type, struct ao2_container *objects, const struct ast_variable *fields)
  147. {
  148. const char *family = data;
  149. RAII_VAR(struct ast_config *, rows, NULL, ast_config_destroy);
  150. RAII_VAR(struct ast_variable *, all, NULL, ast_variables_destroy);
  151. struct ast_category *row = NULL;
  152. if (!fields) {
  153. char field[strlen(UUID_FIELD) + 6], value[2];
  154. /* If no fields have been specified we want all rows, so trick realtime into doing it */
  155. snprintf(field, sizeof(field), "%s LIKE", UUID_FIELD);
  156. snprintf(value, sizeof(value), "%%");
  157. if (!(all = ast_variable_new(field, value, ""))) {
  158. return;
  159. }
  160. fields = all;
  161. }
  162. if (!(rows = ast_load_realtime_multientry_fields(family, fields))) {
  163. return;
  164. }
  165. while ((row = ast_category_browse_filtered(rows, NULL, row, NULL))) {
  166. struct ast_variable *objectset = ast_category_detach_variables(row);
  167. RAII_VAR(struct ast_variable *, id, NULL, ast_variables_destroy);
  168. RAII_VAR(void *, object, NULL, ao2_cleanup);
  169. objectset = sorcery_realtime_filter_objectset(objectset, &id, sorcery, type);
  170. if (id && (object = ast_sorcery_alloc(sorcery, type, id->value)) && !ast_sorcery_objectset_apply(sorcery, object, objectset)) {
  171. ao2_link(objects, object);
  172. }
  173. ast_variables_destroy(objectset);
  174. }
  175. }
  176. static void sorcery_realtime_retrieve_regex(const struct ast_sorcery *sorcery, void *data, const char *type, struct ao2_container *objects, const char *regex)
  177. {
  178. char field[strlen(UUID_FIELD) + 6], value[strlen(regex) + 2];
  179. RAII_VAR(struct ast_variable *, fields, NULL, ast_variables_destroy);
  180. /* The realtime API provides no direct ability to do regex so for now we support a limited subset using pattern matching */
  181. if (regex[0] != '^') {
  182. return;
  183. }
  184. snprintf(field, sizeof(field), "%s LIKE", UUID_FIELD);
  185. snprintf(value, sizeof(value), "%s%%", regex + 1);
  186. if (!(fields = ast_variable_new(field, value, ""))) {
  187. return;
  188. }
  189. sorcery_realtime_retrieve_multiple(sorcery, data, type, objects, fields);
  190. }
  191. static int sorcery_realtime_update(const struct ast_sorcery *sorcery, void *data, void *object)
  192. {
  193. const char *family = data;
  194. RAII_VAR(struct ast_variable *, fields, ast_sorcery_objectset_create(sorcery, object), ast_variables_destroy);
  195. if (!fields) {
  196. return -1;
  197. }
  198. return (ast_update_realtime_fields(family, UUID_FIELD, ast_sorcery_object_get_id(object), fields) <= 0) ? -1 : 0;
  199. }
  200. static int sorcery_realtime_delete(const struct ast_sorcery *sorcery, void *data, void *object)
  201. {
  202. const char *family = data;
  203. return (ast_destroy_realtime_fields(family, UUID_FIELD, ast_sorcery_object_get_id(object), NULL) <= 0) ? -1 : 0;
  204. }
  205. static void *sorcery_realtime_open(const char *data)
  206. {
  207. /* We require a prefix for family string generation, or else stuff could mix together */
  208. if (ast_strlen_zero(data) || !ast_realtime_is_mapping_defined(data)) {
  209. return NULL;
  210. }
  211. return ast_strdup(data);
  212. }
  213. static void sorcery_realtime_close(void *data)
  214. {
  215. ast_free(data);
  216. }
  217. static int load_module(void)
  218. {
  219. if (ast_sorcery_wizard_register(&realtime_object_wizard)) {
  220. return AST_MODULE_LOAD_DECLINE;
  221. }
  222. return AST_MODULE_LOAD_SUCCESS;
  223. }
  224. static int unload_module(void)
  225. {
  226. ast_sorcery_wizard_unregister(&realtime_object_wizard);
  227. return 0;
  228. }
  229. AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_GLOBAL_SYMBOLS | AST_MODFLAG_LOAD_ORDER, "Sorcery Realtime Object Wizard",
  230. .support_level = AST_MODULE_SUPPORT_CORE,
  231. .load = load_module,
  232. .unload = unload_module,
  233. .load_pri = AST_MODPRI_REALTIME_DRIVER,
  234. );