2 * Asterisk -- An open source telephony toolkit.
4 * Copyright (C) 2012 - 2013, Digium, Inc.
6 * Joshua Colp <jcolp@digium.com>
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.
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.
21 * \brief Sorcery Data Access Layer API
23 * \author Joshua Colp <jcolp@digium.com>
27 <support_level>core</support_level>
32 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
34 #include "asterisk/logger.h"
35 #include "asterisk/sorcery.h"
36 #include "asterisk/astobj2.h"
37 #include "asterisk/strings.h"
38 #include "asterisk/config_options.h"
39 #include "asterisk/netsock2.h"
40 #include "asterisk/module.h"
41 #include "asterisk/taskprocessor.h"
42 #include "asterisk/threadpool.h"
43 #include "asterisk/json.h"
45 /* To prevent DEBUG_FD_LEAKS from interfering with things we undef open and close */
49 /*! \brief Number of buckets for wizards (should be prime for performance reasons) */
50 #define WIZARD_BUCKETS 7
52 /*! \brief Number of buckets for types (should be prime for performance reasons) */
53 #define TYPE_BUCKETS 53
55 /*! \brief Maximum length of an object field name */
56 #define MAX_OBJECT_FIELD 128
58 /*! \brief Thread pool for observers */
59 static struct ast_threadpool *threadpool;
61 /*! \brief Structure for internal sorcery object information */
62 struct ast_sorcery_object {
63 /*! \brief Unique identifier of this object */
66 /*! \brief Type of object */
67 char type[MAX_OBJECT_TYPE];
69 /*! \brief Optional object destructor */
70 ao2_destructor_fn destructor;
72 /*! \brief Extended object fields */
73 struct ast_variable *extended;
76 /*! \brief Structure for registered object type */
77 struct ast_sorcery_object_type {
78 /*! \brief Unique name of the object type */
79 char name[MAX_OBJECT_TYPE];
81 /*! \brief Optional transformation callback */
82 sorcery_transform_handler transform;
84 /*! \brief Optional object set apply callback */
85 sorcery_apply_handler apply;
87 /*! \brief Optional object copy callback */
88 sorcery_copy_handler copy;
90 /*! \brief Optional object diff callback */
91 sorcery_diff_handler diff;
93 /*! \brief Wizard instances */
94 struct ao2_container *wizards;
96 /*! \brief Object fields */
97 struct ao2_container *fields;
99 /*! \brief Configuration framework general information */
100 struct aco_info *info;
102 /*! \brief Configuration framework file information */
103 struct aco_file *file;
105 /*! \brief Type details */
106 struct aco_type type;
108 /*! \brief Observers */
109 struct ao2_container *observers;
111 /*! \brief Serializer for observers */
112 struct ast_taskprocessor *serializer;
115 /*! \brief Structure for registered object type observer */
116 struct ast_sorcery_object_type_observer {
117 /*! \brief Pointer to the observer implementation */
118 const struct ast_sorcery_observer *callbacks;
121 /*! \brief Structure used for observer invocations */
122 struct sorcery_observer_invocation {
123 /*! \brief Pointer to the object type */
124 struct ast_sorcery_object_type *object_type;
126 /*! \brief Pointer to the object */
130 /*! \brief Structure for registered object field */
131 struct ast_sorcery_object_field {
132 /*! \brief Name of the field */
133 char name[MAX_OBJECT_FIELD];
135 /*! \brief Callback function for translation of a single value */
136 sorcery_field_handler handler;
138 /*! \brief Callback function for translation of multiple values */
139 sorcery_fields_handler multiple_handler;
141 /*! \brief Position of the field */
145 /*! \brief Structure for a wizard instance which operates on objects */
146 struct ast_sorcery_object_wizard {
147 /*! \brief Wizard interface itself */
148 struct ast_sorcery_wizard *wizard;
150 /*! \brief Unique data for the wizard */
153 /*! \brief Wizard is acting as an object cache */
154 unsigned int caching:1;
157 /*! \brief Full structure for sorcery */
159 /*! \brief Container for known object types */
160 struct ao2_container *types;
163 /*! \brief Structure for passing load/reload details */
164 struct sorcery_load_details {
165 /*! \brief Sorcery structure in use */
166 const struct ast_sorcery *sorcery;
168 /*! \brief Type of object being loaded */
171 /*! \brief Whether this is a reload or not */
172 unsigned int reload:1;
175 /*! \brief Registered sorcery wizards */
176 struct ao2_container *wizards;
178 static int int_handler_fn(const void *obj, const intptr_t *args, char **buf)
180 int *field = (int *)(obj + args[0]);
181 return (ast_asprintf(buf, "%d", *field) < 0) ? -1 : 0;
184 static int uint_handler_fn(const void *obj, const intptr_t *args, char **buf)
186 unsigned int *field = (unsigned int *)(obj + args[0]);
187 return (ast_asprintf(buf, "%u", *field) < 0) ? -1 : 0;
190 static int double_handler_fn(const void *obj, const intptr_t *args, char **buf)
192 double *field = (double *)(obj + args[0]);
193 return (ast_asprintf(buf, "%f", *field) < 0) ? -1 : 0;
196 static int stringfield_handler_fn(const void *obj, const intptr_t *args, char **buf)
198 ast_string_field *field = (const char **)(obj + args[0]);
199 return !(*buf = ast_strdup(*field)) ? -1 : 0;
202 static int bool_handler_fn(const void *obj, const intptr_t *args, char **buf)
204 unsigned int *field = (unsigned int *)(obj + args[0]);
205 return !(*buf = ast_strdup(*field ? "true" : "false")) ? -1 : 0;
208 static int sockaddr_handler_fn(const void *obj, const intptr_t *args, char **buf)
210 struct ast_sockaddr *field = (struct ast_sockaddr *)(obj + args[0]);
211 return !(*buf = ast_strdup(ast_sockaddr_stringify(field))) ? -1 : 0;
214 static int chararray_handler_fn(const void *obj, const intptr_t *args, char **buf)
216 char *field = (char *)(obj + args[0]);
217 return !(*buf = ast_strdup(field)) ? -1 : 0;
220 static sorcery_field_handler sorcery_field_default_handler(enum aco_option_type type)
223 case OPT_BOOL_T: return bool_handler_fn;
224 case OPT_CHAR_ARRAY_T: return chararray_handler_fn;
225 case OPT_DOUBLE_T: return double_handler_fn;
226 case OPT_INT_T: return int_handler_fn;
227 case OPT_SOCKADDR_T: return sockaddr_handler_fn;
228 case OPT_STRINGFIELD_T: return stringfield_handler_fn;
229 case OPT_UINT_T: return uint_handler_fn;
232 case OPT_CUSTOM_T: return NULL;
238 /*! \brief Hashing function for sorcery wizards */
239 static int sorcery_wizard_hash(const void *obj, const int flags)
241 const struct ast_sorcery_wizard *wizard = obj;
242 const char *name = obj;
244 return ast_str_hash(flags & OBJ_KEY ? name : wizard->name);
247 /*! \brief Comparator function for sorcery wizards */
248 static int sorcery_wizard_cmp(void *obj, void *arg, int flags)
250 struct ast_sorcery_wizard *wizard1 = obj, *wizard2 = arg;
251 const char *name = arg;
253 return !strcmp(wizard1->name, flags & OBJ_KEY ? name : wizard2->name) ? CMP_MATCH | CMP_STOP : 0;
256 /*! \brief Cleanup function */
257 static void sorcery_exit(void)
259 ast_threadpool_shutdown(threadpool);
263 /*! \brief Cleanup function for graceful shutdowns */
264 static void sorcery_cleanup(void)
266 ao2_cleanup(wizards);
269 int ast_sorcery_init(void)
271 struct ast_threadpool_options options = {
272 .version = AST_THREADPOOL_OPTIONS_VERSION,
278 ast_assert(wizards == NULL);
280 if (!(threadpool = ast_threadpool_create("Sorcery", NULL, &options))) {
285 if (!(wizards = ao2_container_alloc(WIZARD_BUCKETS, sorcery_wizard_hash, sorcery_wizard_cmp))) {
286 ast_threadpool_shutdown(threadpool);
290 ast_register_cleanup(sorcery_cleanup);
291 ast_register_atexit(sorcery_exit);
296 int __ast_sorcery_wizard_register(const struct ast_sorcery_wizard *interface, struct ast_module *module)
298 struct ast_sorcery_wizard *wizard;
301 ast_assert(!ast_strlen_zero(interface->name));
305 if ((wizard = ao2_find(wizards, interface->name, OBJ_KEY | OBJ_NOLOCK))) {
306 ast_log(LOG_WARNING, "Attempted to register sorcery wizard '%s' twice\n",
311 if (!(wizard = ao2_alloc(sizeof(*wizard), NULL))) {
315 *wizard = *interface;
316 wizard->module = module;
318 ao2_link_flags(wizards, wizard, OBJ_NOLOCK);
321 ast_verb(2, "Sorcery registered wizard '%s'\n", interface->name);
330 int ast_sorcery_wizard_unregister(const struct ast_sorcery_wizard *interface)
332 RAII_VAR(struct ast_sorcery_wizard *, wizard, ao2_find(wizards, interface->name, OBJ_KEY | OBJ_UNLINK), ao2_cleanup);
335 ast_verb(2, "Sorcery unregistered wizard '%s'\n", interface->name);
342 /*! \brief Destructor called when sorcery structure is destroyed */
343 static void sorcery_destructor(void *obj)
345 struct ast_sorcery *sorcery = obj;
347 ao2_cleanup(sorcery->types);
350 /*! \brief Hashing function for sorcery types */
351 static int sorcery_type_hash(const void *obj, const int flags)
353 const struct ast_sorcery_object_type *type = obj;
354 const char *name = obj;
356 return ast_str_hash(flags & OBJ_KEY ? name : type->name);
359 /*! \brief Comparator function for sorcery types */
360 static int sorcery_type_cmp(void *obj, void *arg, int flags)
362 struct ast_sorcery_object_type *type1 = obj, *type2 = arg;
363 const char *name = arg;
365 return !strcmp(type1->name, flags & OBJ_KEY ? name : type2->name) ? CMP_MATCH | CMP_STOP : 0;
368 struct ast_sorcery *ast_sorcery_open(void)
370 struct ast_sorcery *sorcery;
372 if (!(sorcery = ao2_alloc(sizeof(*sorcery), sorcery_destructor))) {
376 if (!(sorcery->types = ao2_container_alloc_options(AO2_ALLOC_OPT_LOCK_RWLOCK, TYPE_BUCKETS, sorcery_type_hash, sorcery_type_cmp))) {
377 ao2_ref(sorcery, -1);
384 /*! \brief Destructor function for object types */
385 static void sorcery_object_type_destructor(void *obj)
387 struct ast_sorcery_object_type *object_type = obj;
389 ao2_cleanup(object_type->wizards);
390 ao2_cleanup(object_type->fields);
391 ao2_cleanup(object_type->observers);
393 if (object_type->info) {
394 aco_info_destroy(object_type->info);
395 ast_free(object_type->info);
398 ast_free(object_type->file);
400 ast_taskprocessor_unreference(object_type->serializer);
403 /*! \brief Internal function which allocates an object type structure */
404 static struct ast_sorcery_object_type *sorcery_object_type_alloc(const char *type, const char *module)
406 struct ast_sorcery_object_type *object_type;
407 char uuid[AST_UUID_STR_LEN];
409 if (!(object_type = ao2_alloc(sizeof(*object_type), sorcery_object_type_destructor))) {
413 /* Order matters for object wizards */
414 if (!(object_type->wizards = ao2_container_alloc_options(AO2_ALLOC_OPT_LOCK_NOLOCK, 1, NULL, NULL))) {
415 ao2_ref(object_type, -1);
419 if (!(object_type->fields = ao2_container_alloc_options(AO2_ALLOC_OPT_LOCK_NOLOCK, 1, NULL, NULL))) {
420 ao2_ref(object_type, -1);
424 if (!(object_type->observers = ao2_container_alloc_options(AO2_ALLOC_OPT_LOCK_RWLOCK, 1, NULL, NULL))) {
425 ao2_ref(object_type, -1);
429 if (!(object_type->info = ast_calloc(1, sizeof(*object_type->info) + 2 * sizeof(object_type->info->files[0])))) {
430 ao2_ref(object_type, -1);
434 if (!(object_type->file = ast_calloc(1, sizeof(*object_type->file) + 2 * sizeof(object_type->file->types[0])))) {
435 ao2_ref(object_type, -1);
439 if (!ast_uuid_generate_str(uuid, sizeof(uuid))) {
440 ao2_ref(object_type, -1);
444 if (!(object_type->serializer = ast_threadpool_serializer(uuid, threadpool))) {
445 ao2_ref(object_type, -1);
449 object_type->info->files[0] = object_type->file;
450 object_type->info->files[1] = NULL;
451 object_type->info->module = module;
453 ast_copy_string(object_type->name, type, sizeof(object_type->name));
458 /*! \brief Object wizard destructor */
459 static void sorcery_object_wizard_destructor(void *obj)
461 struct ast_sorcery_object_wizard *object_wizard = obj;
463 if (object_wizard->data) {
464 object_wizard->wizard->close(object_wizard->data);
467 if (object_wizard->wizard) {
468 ast_module_unref(object_wizard->wizard->module);
472 /*! \brief Internal function which creates an object type and adds a wizard mapping */
473 static int sorcery_apply_wizard_mapping(struct ast_sorcery *sorcery, const char *type, const char *module, const char *name, const char *data, unsigned int caching)
475 RAII_VAR(struct ast_sorcery_object_type *, object_type, ao2_find(sorcery->types, type, OBJ_KEY), ao2_cleanup);
476 RAII_VAR(struct ast_sorcery_wizard *, wizard, ao2_find(wizards, name, OBJ_KEY), ao2_cleanup);
477 RAII_VAR(struct ast_sorcery_object_wizard *, object_wizard, ao2_alloc(sizeof(*object_wizard), sorcery_object_wizard_destructor), ao2_cleanup);
480 if (!wizard || !object_wizard) {
485 if (!(object_type = sorcery_object_type_alloc(type, module))) {
491 if (wizard->open && !(object_wizard->data = wizard->open(data))) {
495 ast_module_ref(wizard->module);
497 object_wizard->wizard = wizard;
498 object_wizard->caching = caching;
500 ao2_link(object_type->wizards, object_wizard);
503 ao2_link(sorcery->types, object_type);
509 int __ast_sorcery_apply_config(struct ast_sorcery *sorcery, const char *name, const char *module)
511 struct ast_flags flags = { 0 };
512 struct ast_config *config = ast_config_load2("sorcery.conf", "sorcery", flags);
513 struct ast_variable *mapping;
516 if (!config || (config == CONFIG_STATUS_FILEMISSING) || (config == CONFIG_STATUS_FILEINVALID)) {
520 for (mapping = ast_variable_browse(config, name); mapping; mapping = mapping->next) {
521 RAII_VAR(char *, mapping_name, ast_strdup(mapping->name), ast_free);
522 RAII_VAR(char *, mapping_value, ast_strdup(mapping->value), ast_free);
523 char *options = mapping_name, *name = strsep(&options, "/");
524 char *data = mapping_value, *wizard = strsep(&data, ",");
525 unsigned int caching = 0;
527 /* If no wizard exists just skip, nothing we can do */
528 if (ast_strlen_zero(wizard)) {
532 /* If the wizard is configured as a cache treat it as such */
533 if (!ast_strlen_zero(options) && strstr(options, "cache")) {
537 /* Any error immediately causes us to stop */
538 if ((res = sorcery_apply_wizard_mapping(sorcery, name, module, wizard, data, caching))) {
543 ast_config_destroy(config);
548 int __ast_sorcery_apply_default(struct ast_sorcery *sorcery, const char *type, const char *module, const char *name, const char *data)
550 RAII_VAR(struct ast_sorcery_object_type *, object_type, ao2_find(sorcery->types, type, OBJ_KEY), ao2_cleanup);
552 /* Defaults can not be added if any existing mapping exists */
557 return sorcery_apply_wizard_mapping(sorcery, type, module, name, data, 0);
560 static int sorcery_extended_config_handler(const struct aco_option *opt, struct ast_variable *var, void *obj)
562 return ast_sorcery_object_set_extended(obj, var->name, var->value);
565 static int sorcery_extended_fields_handler(const void *obj, struct ast_variable **fields)
567 const struct ast_sorcery_object_details *details = obj;
569 if (details->object->extended) {
570 *fields = ast_variables_dup(details->object->extended);
578 int __ast_sorcery_object_register(struct ast_sorcery *sorcery, const char *type, unsigned int hidden, aco_type_item_alloc alloc, sorcery_transform_handler transform, sorcery_apply_handler apply)
580 RAII_VAR(struct ast_sorcery_object_type *, object_type, ao2_find(sorcery->types, type, OBJ_KEY), ao2_cleanup);
582 if (!object_type || object_type->type.item_alloc) {
586 object_type->type.name = object_type->name;
587 object_type->type.type = ACO_ITEM;
588 object_type->type.category = ".?";
589 object_type->type.item_alloc = alloc;
590 object_type->type.hidden = hidden;
592 object_type->transform = transform;
593 object_type->apply = apply;
594 object_type->file->types[0] = &object_type->type;
595 object_type->file->types[1] = NULL;
597 if (aco_info_init(object_type->info)) {
601 if (ast_sorcery_object_fields_register(sorcery, type, "^@", sorcery_extended_config_handler, sorcery_extended_fields_handler)) {
608 void ast_sorcery_object_set_copy_handler(struct ast_sorcery *sorcery, const char *type, sorcery_copy_handler copy)
610 RAII_VAR(struct ast_sorcery_object_type *, object_type, ao2_find(sorcery->types, type, OBJ_KEY), ao2_cleanup);
616 object_type->copy = copy;
619 void ast_sorcery_object_set_diff_handler(struct ast_sorcery *sorcery, const char *type, sorcery_diff_handler diff)
621 RAII_VAR(struct ast_sorcery_object_type *, object_type, ao2_find(sorcery->types, type, OBJ_KEY), ao2_cleanup);
627 object_type->diff = diff;
630 int ast_sorcery_object_fields_register(struct ast_sorcery *sorcery, const char *type, const char *regex, aco_option_handler config_handler, sorcery_fields_handler sorcery_handler)
632 RAII_VAR(struct ast_sorcery_object_type *, object_type, ao2_find(sorcery->types, type, OBJ_KEY), ao2_cleanup);
633 RAII_VAR(struct ast_sorcery_object_field *, object_field, NULL, ao2_cleanup);
635 if (!object_type || !object_type->type.item_alloc || !config_handler || !(object_field = ao2_alloc(sizeof(*object_field), NULL))) {
639 ast_copy_string(object_field->name, regex, sizeof(object_field->name));
640 object_field->multiple_handler = sorcery_handler;
642 ao2_link(object_type->fields, object_field);
643 __aco_option_register(object_type->info, regex, ACO_REGEX, object_type->file->types, "", OPT_CUSTOM_T, config_handler, 0, 1, 0);
648 int __ast_sorcery_object_field_register(struct ast_sorcery *sorcery, const char *type, const char *name, const char *default_val, enum aco_option_type opt_type,
649 aco_option_handler config_handler, sorcery_field_handler sorcery_handler, unsigned int flags, unsigned int no_doc, size_t argc, ...)
651 RAII_VAR(struct ast_sorcery_object_type *, object_type, ao2_find(sorcery->types, type, OBJ_KEY), ao2_cleanup);
652 RAII_VAR(struct ast_sorcery_object_field *, object_field, NULL, ao2_cleanup);
656 if (!strcmp(type, "id") || !object_type || !object_type->type.item_alloc) {
660 if (!sorcery_handler) {
661 sorcery_handler = sorcery_field_default_handler(opt_type);
664 if (!(object_field = ao2_alloc(sizeof(*object_field) + argc * sizeof(object_field->args[0]), NULL))) {
668 ast_copy_string(object_field->name, name, sizeof(object_field->name));
669 object_field->handler = sorcery_handler;
671 va_start(args, argc);
672 for (pos = 0; pos < argc; pos++) {
673 object_field->args[pos] = va_arg(args, size_t);
677 ao2_link(object_type->fields, object_field);
679 /* TODO: Improve this hack */
681 __aco_option_register(object_type->info, name, ACO_EXACT, object_type->file->types, default_val, opt_type, config_handler, flags, no_doc, argc);
682 } else if (argc == 1) {
683 __aco_option_register(object_type->info, name, ACO_EXACT, object_type->file->types, default_val, opt_type, config_handler, flags, no_doc, argc,
684 object_field->args[0]);
685 } else if (argc == 2) {
686 __aco_option_register(object_type->info, name, ACO_EXACT, object_type->file->types, default_val, opt_type, config_handler, flags, no_doc, argc,
687 object_field->args[0], object_field->args[1]);
688 } else if (argc == 3) {
689 __aco_option_register(object_type->info, name, ACO_EXACT, object_type->file->types, default_val, opt_type, config_handler, flags, no_doc, argc,
690 object_field->args[0], object_field->args[1], object_field->args[2]);
692 ast_assert(0); /* The hack... she does us no good for this */
698 static int sorcery_wizard_load(void *obj, void *arg, int flags)
700 struct ast_sorcery_object_wizard *wizard = obj;
701 struct sorcery_load_details *details = arg;
702 void (*load)(void *data, const struct ast_sorcery *sorcery, const char *type);
704 load = !details->reload ? wizard->wizard->load : wizard->wizard->reload;
707 load(wizard->data, details->sorcery, details->type);
713 /*! \brief Destructor for observer invocation */
714 static void sorcery_observer_invocation_destroy(void *obj)
716 struct sorcery_observer_invocation *invocation = obj;
718 ao2_cleanup(invocation->object_type);
719 ao2_cleanup(invocation->object);
722 /*! \brief Allocator function for observer invocation */
723 static struct sorcery_observer_invocation *sorcery_observer_invocation_alloc(struct ast_sorcery_object_type *object_type, void *object)
725 struct sorcery_observer_invocation *invocation = ao2_alloc(sizeof(*invocation), sorcery_observer_invocation_destroy);
731 ao2_ref(object_type, +1);
732 invocation->object_type = object_type;
736 invocation->object = object;
742 /*! \brief Internal callback function which notifies an individual observer that an object type has been loaded */
743 static int sorcery_observer_notify_loaded(void *obj, void *arg, int flags)
745 const struct ast_sorcery_object_type_observer *observer = obj;
747 if (observer->callbacks->loaded) {
748 observer->callbacks->loaded(arg);
754 /*! \brief Internal callback function which notifies observers that an object type has been loaded */
755 static int sorcery_observers_notify_loaded(void *data)
757 struct sorcery_observer_invocation *invocation = data;
759 ao2_callback(invocation->object_type->observers, OBJ_NODATA, sorcery_observer_notify_loaded, invocation->object_type->name);
760 ao2_cleanup(invocation);
765 static int sorcery_object_load(void *obj, void *arg, int flags)
767 struct ast_sorcery_object_type *type = obj;
768 struct sorcery_load_details *details = arg;
770 details->type = type->name;
771 ao2_callback(type->wizards, OBJ_NODATA, sorcery_wizard_load, details);
773 if (ao2_container_count(type->observers)) {
774 struct sorcery_observer_invocation *invocation = sorcery_observer_invocation_alloc(type, NULL);
776 if (invocation && ast_taskprocessor_push(type->serializer, sorcery_observers_notify_loaded, invocation)) {
777 ao2_cleanup(invocation);
784 void ast_sorcery_load(const struct ast_sorcery *sorcery)
786 struct sorcery_load_details details = {
791 ao2_callback(sorcery->types, OBJ_NODATA, sorcery_object_load, &details);
794 void ast_sorcery_load_object(const struct ast_sorcery *sorcery, const char *type)
796 RAII_VAR(struct ast_sorcery_object_type *, object_type, ao2_find(sorcery->types, type, OBJ_KEY), ao2_cleanup);
797 struct sorcery_load_details details = {
806 sorcery_object_load(object_type, &details, 0);
809 void ast_sorcery_reload(const struct ast_sorcery *sorcery)
811 struct sorcery_load_details details = {
816 ao2_callback(sorcery->types, OBJ_NODATA, sorcery_object_load, &details);
819 void ast_sorcery_reload_object(const struct ast_sorcery *sorcery, const char *type)
821 RAII_VAR(struct ast_sorcery_object_type *, object_type, ao2_find(sorcery->types, type, OBJ_KEY), ao2_cleanup);
822 struct sorcery_load_details details = {
831 sorcery_object_load(object_type, &details, 0);
834 void ast_sorcery_ref(struct ast_sorcery *sorcery)
836 ao2_ref(sorcery, +1);
839 struct ast_variable *ast_sorcery_objectset_create(const struct ast_sorcery *sorcery, const void *object)
841 const struct ast_sorcery_object_details *details = object;
842 RAII_VAR(struct ast_sorcery_object_type *, object_type, ao2_find(sorcery->types, details->object->type, OBJ_KEY), ao2_cleanup);
843 struct ao2_iterator i;
844 struct ast_sorcery_object_field *object_field;
845 struct ast_variable *fields = NULL;
852 i = ao2_iterator_init(object_type->fields, 0);
854 for (; (object_field = ao2_iterator_next(&i)) && !res; ao2_ref(object_field, -1)) {
855 struct ast_variable *tmp = NULL;
857 if (object_field->multiple_handler) {
858 if ((res = object_field->multiple_handler(object, &tmp))) {
859 ast_variables_destroy(tmp);
861 } else if (object_field->handler) {
864 if ((res = object_field->handler(object, object_field->args, &buf)) ||
865 !(tmp = ast_variable_new(object_field->name, S_OR(buf, ""), ""))) {
880 ao2_iterator_destroy(&i);
882 /* If any error occurs we destroy all fields handled before so a partial objectset is not returned */
884 ast_variables_destroy(fields);
891 struct ast_json *ast_sorcery_objectset_json_create(const struct ast_sorcery *sorcery, const void *object)
893 const struct ast_sorcery_object_details *details = object;
894 RAII_VAR(struct ast_sorcery_object_type *, object_type, ao2_find(sorcery->types, details->object->type, OBJ_KEY), ao2_cleanup);
895 struct ao2_iterator i;
896 struct ast_sorcery_object_field *object_field;
897 struct ast_json *json = ast_json_object_create();
900 if (!object_type || !json) {
904 i = ao2_iterator_init(object_type->fields, 0);
906 for (; (object_field = ao2_iterator_next(&i)) && !res; ao2_ref(object_field, -1)) {
907 if (object_field->multiple_handler) {
908 struct ast_variable *tmp = NULL;
909 struct ast_variable *field;
911 if ((res = object_field->multiple_handler(object, &tmp))) {
915 for (field = tmp; field; field = field->next) {
916 struct ast_json *value = ast_json_string_create(field->value);
918 if (value && ast_json_object_set(json, field->name, value)) {
919 ast_json_unref(value);
924 ast_variables_destroy(tmp);
925 } else if (object_field->handler) {
927 struct ast_json *value = NULL;
929 if ((res = object_field->handler(object, object_field->args, &buf)) ||
930 !(value = ast_json_string_create(buf)) ||
931 ast_json_object_set(json, object_field->name, value)) {
932 ast_json_unref(value);
942 ao2_iterator_destroy(&i);
944 /* If any error occurs we destroy the JSON object so a partial objectset is not returned */
946 ast_json_unref(json);
953 int ast_sorcery_objectset_apply(const struct ast_sorcery *sorcery, void *object, struct ast_variable *objectset)
955 const struct ast_sorcery_object_details *details = object;
956 RAII_VAR(struct ast_sorcery_object_type *, object_type, ao2_find(sorcery->types, details->object->type, OBJ_KEY), ao2_cleanup);
957 RAII_VAR(struct ast_variable *, transformed, NULL, ast_variables_destroy);
958 struct ast_variable *field;
965 if (object_type->transform && (transformed = object_type->transform(objectset))) {
971 for (; field; field = field->next) {
972 if ((res = aco_process_var(&object_type->type, details->object->id, field, object))) {
977 if (!res && object_type->apply) {
978 res = object_type->apply(sorcery, object);
984 static const struct ast_variable *sorcery_find_field(const struct ast_variable *fields, const char *name)
986 const struct ast_variable *field;
988 /* Search the linked list of fields to find the correct one */
989 for (field = fields; field; field = field->next) {
990 if (!strcmp(field->name, name)) {
998 int ast_sorcery_changeset_create(const struct ast_variable *original, const struct ast_variable *modified, struct ast_variable **changes)
1000 const struct ast_variable *field;
1005 /* Unless the ast_variable list changes when examined... it can't differ from itself */
1006 if (original == modified) {
1010 for (field = modified; field; field = field->next) {
1011 const struct ast_variable *old = sorcery_find_field(original, field->name);
1013 if (!old || strcmp(old->value, field->value)) {
1014 struct ast_variable *tmp;
1016 if (!(tmp = ast_variable_new(field->name, field->value, ""))) {
1021 tmp->next = *changes;
1026 /* If an error occurred do not return a partial changeset */
1028 ast_variables_destroy(*changes);
1035 static void sorcery_object_destructor(void *object)
1037 struct ast_sorcery_object_details *details = object;
1039 if (details->object->destructor) {
1040 details->object->destructor(object);
1043 ast_variables_destroy(details->object->extended);
1044 ast_free(details->object->id);
1047 void *ast_sorcery_generic_alloc(size_t size, ao2_destructor_fn destructor)
1049 void *object = ao2_alloc_options(size + sizeof(struct ast_sorcery_object), sorcery_object_destructor, AO2_ALLOC_OPT_LOCK_NOLOCK);
1050 struct ast_sorcery_object_details *details = object;
1056 details->object = object + size;
1057 details->object->destructor = destructor;
1062 void *ast_sorcery_alloc(const struct ast_sorcery *sorcery, const char *type, const char *id)
1064 RAII_VAR(struct ast_sorcery_object_type *, object_type, ao2_find(sorcery->types, type, OBJ_KEY), ao2_cleanup);
1065 struct ast_sorcery_object_details *details;
1067 if (!object_type || !object_type->type.item_alloc ||
1068 !(details = object_type->type.item_alloc(id))) {
1072 if (ast_strlen_zero(id)) {
1073 char uuid[AST_UUID_STR_LEN];
1075 ast_uuid_generate_str(uuid, sizeof(uuid));
1076 details->object->id = ast_strdup(uuid);
1078 details->object->id = ast_strdup(id);
1081 ast_copy_string(details->object->type, type, sizeof(details->object->type));
1083 if (aco_set_defaults(&object_type->type, id, details)) {
1084 ao2_ref(details, -1);
1091 void *ast_sorcery_copy(const struct ast_sorcery *sorcery, const void *object)
1093 const struct ast_sorcery_object_details *details = object;
1094 RAII_VAR(struct ast_sorcery_object_type *, object_type, ao2_find(sorcery->types, details->object->type, OBJ_KEY), ao2_cleanup);
1095 struct ast_sorcery_object_details *copy = ast_sorcery_alloc(sorcery, details->object->type, details->object->id);
1096 RAII_VAR(struct ast_variable *, objectset, NULL, ast_variables_destroy);
1101 } else if (object_type->copy) {
1102 res = object_type->copy(object, copy);
1103 } else if ((objectset = ast_sorcery_objectset_create(sorcery, object))) {
1104 res = ast_sorcery_objectset_apply(sorcery, copy, objectset);
1106 /* No native copy available and could not create an objectset, this copy has failed */
1118 int ast_sorcery_diff(const struct ast_sorcery *sorcery, const void *original, const void *modified, struct ast_variable **changes)
1120 RAII_VAR(struct ast_sorcery_object_type *, object_type, ao2_find(sorcery->types, ast_sorcery_object_get_type(original), OBJ_KEY), ao2_cleanup);
1124 if (strcmp(ast_sorcery_object_get_type(original), ast_sorcery_object_get_type(modified))) {
1128 if (original == modified) {
1130 } else if (!object_type->diff) {
1131 RAII_VAR(struct ast_variable *, objectset1, NULL, ast_variables_destroy);
1132 RAII_VAR(struct ast_variable *, objectset2, NULL, ast_variables_destroy);
1134 objectset1 = ast_sorcery_objectset_create(sorcery, original);
1135 objectset2 = ast_sorcery_objectset_create(sorcery, modified);
1137 return ast_sorcery_changeset_create(objectset1, objectset2, changes);
1139 return object_type->diff(original, modified, changes);
1143 /*! \brief Structure used when calling create, update, or delete */
1144 struct sorcery_details {
1145 /*! \brief Pointer to the sorcery instance */
1146 const struct ast_sorcery *sorcery;
1147 /*! \brief Pointer to the object itself */
1151 /*! \brief Internal function used to create an object in caching wizards */
1152 static int sorcery_cache_create(void *obj, void *arg, int flags)
1154 const struct ast_sorcery_object_wizard *object_wizard = obj;
1155 const struct sorcery_details *details = arg;
1157 if (!object_wizard->caching || !object_wizard->wizard->create) {
1161 object_wizard->wizard->create(details->sorcery, object_wizard->data, details->obj);
1166 void *ast_sorcery_retrieve_by_id(const struct ast_sorcery *sorcery, const char *type, const char *id)
1168 RAII_VAR(struct ast_sorcery_object_type *, object_type, ao2_find(sorcery->types, type, OBJ_KEY), ao2_cleanup);
1169 void *object = NULL;
1170 struct ao2_iterator i;
1171 struct ast_sorcery_object_wizard *wizard;
1172 unsigned int cached = 0;
1174 if (!object_type || ast_strlen_zero(id)) {
1178 i = ao2_iterator_init(object_type->wizards, 0);
1179 for (; (wizard = ao2_iterator_next(&i)); ao2_ref(wizard, -1)) {
1180 if (wizard->wizard->retrieve_id &&
1181 !(object = wizard->wizard->retrieve_id(sorcery, wizard->data, object_type->name, id))) {
1185 cached = wizard->caching;
1187 ao2_ref(wizard, -1);
1190 ao2_iterator_destroy(&i);
1192 if (!cached && object) {
1193 ao2_callback(object_type->wizards, 0, sorcery_cache_create, object);
1199 void *ast_sorcery_retrieve_by_fields(const struct ast_sorcery *sorcery, const char *type, unsigned int flags, struct ast_variable *fields)
1201 RAII_VAR(struct ast_sorcery_object_type *, object_type, ao2_find(sorcery->types, type, OBJ_KEY), ao2_cleanup);
1202 void *object = NULL;
1203 struct ao2_iterator i;
1204 struct ast_sorcery_object_wizard *wizard;
1205 unsigned int cached = 0;
1211 /* If returning multiple objects create a container to store them in */
1212 if ((flags & AST_RETRIEVE_FLAG_MULTIPLE)) {
1213 if (!(object = ao2_container_alloc_options(AO2_ALLOC_OPT_LOCK_NOLOCK, 1, NULL, NULL))) {
1218 /* Inquire with the available wizards for retrieval */
1219 i = ao2_iterator_init(object_type->wizards, 0);
1220 for (; (wizard = ao2_iterator_next(&i)); ao2_ref(wizard, -1)) {
1221 if ((flags & AST_RETRIEVE_FLAG_MULTIPLE)) {
1222 if (wizard->wizard->retrieve_multiple) {
1223 wizard->wizard->retrieve_multiple(sorcery, wizard->data, object_type->name, object, fields);
1225 } else if (fields && wizard->wizard->retrieve_fields) {
1226 if (wizard->wizard->retrieve_fields) {
1227 object = wizard->wizard->retrieve_fields(sorcery, wizard->data, object_type->name, fields);
1231 if ((flags & AST_RETRIEVE_FLAG_MULTIPLE) || !object) {
1235 cached = wizard->caching;
1237 ao2_ref(wizard, -1);
1240 ao2_iterator_destroy(&i);
1242 /* If we are returning a single object and it came from a non-cache source create it in any caches */
1243 if (!(flags & AST_RETRIEVE_FLAG_MULTIPLE) && !cached && object) {
1244 ao2_callback(object_type->wizards, 0, sorcery_cache_create, object);
1250 struct ao2_container *ast_sorcery_retrieve_by_regex(const struct ast_sorcery *sorcery, const char *type, const char *regex)
1252 RAII_VAR(struct ast_sorcery_object_type *, object_type, ao2_find(sorcery->types, type, OBJ_KEY), ao2_cleanup);
1253 struct ao2_container *objects;
1254 struct ao2_iterator i;
1255 struct ast_sorcery_object_wizard *wizard;
1257 if (!object_type || !(objects = ao2_container_alloc_options(AO2_ALLOC_OPT_LOCK_NOLOCK, 1, NULL, NULL))) {
1261 i = ao2_iterator_init(object_type->wizards, 0);
1262 for (; (wizard = ao2_iterator_next(&i)); ao2_ref(wizard, -1)) {
1263 if (!wizard->wizard->retrieve_regex) {
1267 wizard->wizard->retrieve_regex(sorcery, wizard->data, object_type->name, objects, regex);
1269 ao2_iterator_destroy(&i);
1274 /*! \brief Internal function which returns if the wizard has created the object */
1275 static int sorcery_wizard_create(void *obj, void *arg, int flags)
1277 const struct ast_sorcery_object_wizard *object_wizard = obj;
1278 const struct sorcery_details *details = arg;
1280 return (!object_wizard->caching && !object_wizard->wizard->create(details->sorcery, object_wizard->data, details->obj)) ? CMP_MATCH | CMP_STOP : 0;
1283 /*! \brief Internal callback function which notifies an individual observer that an object has been created */
1284 static int sorcery_observer_notify_create(void *obj, void *arg, int flags)
1286 const struct ast_sorcery_object_type_observer *observer = obj;
1288 if (observer->callbacks->created) {
1289 observer->callbacks->created(arg);
1295 /*! \brief Internal callback function which notifies observers that an object has been created */
1296 static int sorcery_observers_notify_create(void *data)
1298 struct sorcery_observer_invocation *invocation = data;
1300 ao2_callback(invocation->object_type->observers, OBJ_NODATA, sorcery_observer_notify_create, invocation->object);
1301 ao2_cleanup(invocation);
1306 int ast_sorcery_create(const struct ast_sorcery *sorcery, void *object)
1308 const struct ast_sorcery_object_details *details = object;
1309 RAII_VAR(struct ast_sorcery_object_type *, object_type, ao2_find(sorcery->types, details->object->type, OBJ_KEY), ao2_cleanup);
1310 RAII_VAR(struct ast_sorcery_object_wizard *, object_wizard, NULL, ao2_cleanup);
1311 struct sorcery_details sdetails = {
1320 if ((object_wizard = ao2_callback(object_type->wizards, 0, sorcery_wizard_create, &sdetails)) &&
1321 ao2_container_count(object_type->observers)) {
1322 struct sorcery_observer_invocation *invocation = sorcery_observer_invocation_alloc(object_type, object);
1324 if (invocation && ast_taskprocessor_push(object_type->serializer, sorcery_observers_notify_create, invocation)) {
1325 ao2_cleanup(invocation);
1329 return object_wizard ? 0 : -1;
1332 /*! \brief Internal callback function which notifies an individual observer that an object has been updated */
1333 static int sorcery_observer_notify_update(void *obj, void *arg, int flags)
1335 const struct ast_sorcery_object_type_observer *observer = obj;
1337 if (observer->callbacks->updated) {
1338 observer->callbacks->updated(arg);
1344 /*! \brief Internal callback function which notifies observers that an object has been updated */
1345 static int sorcery_observers_notify_update(void *data)
1347 struct sorcery_observer_invocation *invocation = data;
1349 ao2_callback(invocation->object_type->observers, OBJ_NODATA, sorcery_observer_notify_update, invocation->object);
1350 ao2_cleanup(invocation);
1355 /*! \brief Internal function which returns if a wizard has updated the object */
1356 static int sorcery_wizard_update(void *obj, void *arg, int flags)
1358 const struct ast_sorcery_object_wizard *object_wizard = obj;
1359 const struct sorcery_details *details = arg;
1361 return (object_wizard->wizard->update && !object_wizard->wizard->update(details->sorcery, object_wizard->data, details->obj) &&
1362 !object_wizard->caching) ? CMP_MATCH | CMP_STOP : 0;
1365 int ast_sorcery_update(const struct ast_sorcery *sorcery, void *object)
1367 const struct ast_sorcery_object_details *details = object;
1368 RAII_VAR(struct ast_sorcery_object_type *, object_type, ao2_find(sorcery->types, details->object->type, OBJ_KEY), ao2_cleanup);
1369 RAII_VAR(struct ast_sorcery_object_wizard *, object_wizard, NULL, ao2_cleanup);
1370 struct sorcery_details sdetails = {
1379 if ((object_wizard = ao2_callback(object_type->wizards, 0, sorcery_wizard_update, &sdetails)) &&
1380 ao2_container_count(object_type->observers)) {
1381 struct sorcery_observer_invocation *invocation = sorcery_observer_invocation_alloc(object_type, object);
1383 if (invocation && ast_taskprocessor_push(object_type->serializer, sorcery_observers_notify_update, invocation)) {
1384 ao2_cleanup(invocation);
1388 return object_wizard ? 0 : -1;
1391 /*! \brief Internal callback function which notifies an individual observer that an object has been deleted */
1392 static int sorcery_observer_notify_delete(void *obj, void *arg, int flags)
1394 const struct ast_sorcery_object_type_observer *observer = obj;
1396 if (observer->callbacks->deleted) {
1397 observer->callbacks->deleted(arg);
1403 /*! \brief Internal callback function which notifies observers that an object has been deleted */
1404 static int sorcery_observers_notify_delete(void *data)
1406 struct sorcery_observer_invocation *invocation = data;
1408 ao2_callback(invocation->object_type->observers, OBJ_NODATA, sorcery_observer_notify_delete, invocation->object);
1409 ao2_cleanup(invocation);
1414 /*! \brief Internal function which returns if a wizard has deleted the object */
1415 static int sorcery_wizard_delete(void *obj, void *arg, int flags)
1417 const struct ast_sorcery_object_wizard *object_wizard = obj;
1418 const struct sorcery_details *details = arg;
1420 return (object_wizard->wizard->delete && !object_wizard->wizard->delete(details->sorcery, object_wizard->data, details->obj) &&
1421 !object_wizard->caching) ? CMP_MATCH | CMP_STOP : 0;
1424 int ast_sorcery_delete(const struct ast_sorcery *sorcery, void *object)
1426 const struct ast_sorcery_object_details *details = object;
1427 RAII_VAR(struct ast_sorcery_object_type *, object_type, ao2_find(sorcery->types, details->object->type, OBJ_KEY), ao2_cleanup);
1428 RAII_VAR(struct ast_sorcery_object_wizard *, object_wizard, NULL, ao2_cleanup);
1429 struct sorcery_details sdetails = {
1438 if ((object_wizard = ao2_callback(object_type->wizards, 0, sorcery_wizard_delete, &sdetails)) &&
1439 ao2_container_count(object_type->observers)) {
1440 struct sorcery_observer_invocation *invocation = sorcery_observer_invocation_alloc(object_type, object);
1442 if (invocation && ast_taskprocessor_push(object_type->serializer, sorcery_observers_notify_delete, invocation)) {
1443 ao2_cleanup(invocation);
1447 return object_wizard ? 0 : -1;
1450 void ast_sorcery_unref(struct ast_sorcery *sorcery)
1452 ao2_cleanup(sorcery);
1455 const char *ast_sorcery_object_get_id(const void *object)
1457 const struct ast_sorcery_object_details *details = object;
1458 return details->object->id;
1461 const char *ast_sorcery_object_get_type(const void *object)
1463 const struct ast_sorcery_object_details *details = object;
1464 return details->object->type;
1467 const char *ast_sorcery_object_get_extended(const void *object, const char *name)
1469 const struct ast_sorcery_object_details *details = object;
1470 struct ast_variable *field;
1472 for (field = details->object->extended; field; field = field->next) {
1473 if (!strcmp(field->name + 1, name)) {
1474 return field->value;
1481 int ast_sorcery_object_set_extended(const void *object, const char *name, const char *value)
1483 RAII_VAR(struct ast_variable *, field, NULL, ast_variables_destroy);
1484 struct ast_variable *extended = ast_variable_new(name, value, ""), *previous = NULL;
1485 const struct ast_sorcery_object_details *details = object;
1491 for (field = details->object->extended; field; previous = field, field = field->next) {
1492 if (!strcmp(field->name, name)) {
1494 previous->next = field->next;
1496 details->object->extended = field->next;
1503 extended->next = details->object->extended;
1504 details->object->extended = extended;
1509 int ast_sorcery_observer_add(const struct ast_sorcery *sorcery, const char *type, const struct ast_sorcery_observer *callbacks)
1511 RAII_VAR(struct ast_sorcery_object_type *, object_type, ao2_find(sorcery->types, type, OBJ_KEY), ao2_cleanup);
1512 struct ast_sorcery_object_type_observer *observer;
1514 if (!object_type || !callbacks) {
1518 if (!(observer = ao2_alloc(sizeof(*observer), NULL))) {
1522 observer->callbacks = callbacks;
1523 ao2_link(object_type->observers, observer);
1524 ao2_ref(observer, -1);
1529 /*! \brief Internal callback function for removing an observer */
1530 static int sorcery_observer_remove(void *obj, void *arg, int flags)
1532 const struct ast_sorcery_object_type_observer *observer = obj;
1534 return (observer->callbacks == arg) ? CMP_MATCH | CMP_STOP : 0;
1537 void ast_sorcery_observer_remove(const struct ast_sorcery *sorcery, const char *type, struct ast_sorcery_observer *callbacks)
1539 RAII_VAR(struct ast_sorcery_object_type *, object_type, ao2_find(sorcery->types, type, OBJ_KEY), ao2_cleanup);
1545 ao2_callback(object_type->observers, OBJ_NODATA | OBJ_UNLINK, sorcery_observer_remove, callbacks);