sorcery, bucket: Change observer remove calls to take const callbacks struct.
[asterisk/asterisk.git] / main / sorcery.c
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 2012 - 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 Data Access Layer API
22  *
23  * \author Joshua Colp <jcolp@digium.com>
24  */
25
26 /*** MODULEINFO
27         <support_level>core</support_level>
28  ***/
29
30 #include "asterisk.h"
31
32 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
33
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"
44
45 /* To prevent DEBUG_FD_LEAKS from interfering with things we undef open and close */
46 #undef open
47 #undef close
48
49 /*! \brief Number of buckets for wizards (should be prime for performance reasons) */
50 #define WIZARD_BUCKETS 7
51
52 /*! \brief Number of buckets for types (should be prime for performance reasons) */
53 #define TYPE_BUCKETS 53
54
55 /*! \brief Thread pool for observers */
56 static struct ast_threadpool *threadpool;
57
58 /*! \brief Structure for internal sorcery object information */
59 struct ast_sorcery_object {
60         /*! \brief Unique identifier of this object */
61         char *id;
62
63         /*! \brief Type of object */
64         char type[MAX_OBJECT_TYPE];
65
66         /*! \brief Optional object destructor */
67         ao2_destructor_fn destructor;
68
69         /*! \brief Extended object fields */
70         struct ast_variable *extended;
71 };
72
73 /*! \brief Structure for registered object type */
74 struct ast_sorcery_object_type {
75         /*! \brief Unique name of the object type */
76         char name[MAX_OBJECT_TYPE];
77
78         /*! \brief Optional transformation callback */
79         sorcery_transform_handler transform;
80
81         /*! \brief Optional object set apply callback */
82         sorcery_apply_handler apply;
83
84         /*! \brief Optional object copy callback */
85         sorcery_copy_handler copy;
86
87         /*! \brief Optional object diff callback */
88         sorcery_diff_handler diff;
89
90         /*! \brief Wizard instances */
91         struct ao2_container *wizards;
92
93         /*! \brief Object fields */
94         struct ao2_container *fields;
95
96         /*! \brief Configuration framework general information */
97         struct aco_info *info;
98
99         /*! \brief Configuration framework file information */
100         struct aco_file *file;
101
102         /*! \brief Type details */
103         struct aco_type type;
104
105         /*! \brief Observers */
106         struct ao2_container *observers;
107
108         /*! \brief Serializer for observers */
109         struct ast_taskprocessor *serializer;
110
111         /*! \brief Specifies if object type is reloadable or not */
112         unsigned int reloadable:1;
113 };
114
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;
119 };
120
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;
125
126         /*! \brief Pointer to the object */
127         void *object;
128 };
129
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];
134
135         /*! \brief Callback function for translation of a single value */
136         sorcery_field_handler handler;
137
138         /*! \brief Callback function for translation of multiple values */
139         sorcery_fields_handler multiple_handler;
140
141         /*! \brief Position of the field */
142         intptr_t args[];
143 };
144
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;
149
150         /*! \brief Unique data for the wizard */
151         void *data;
152
153         /*! \brief Wizard is acting as an object cache */
154         unsigned int caching:1;
155 };
156
157 /*! \brief Full structure for sorcery */
158 struct ast_sorcery {
159         /*! \brief Container for known object types */
160         struct ao2_container *types;
161 };
162
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;
167
168         /*! \brief Type of object being loaded */
169         const char *type;
170
171         /*! \brief Whether this is a reload or not */
172         unsigned int reload:1;
173 };
174
175 /*! \brief Registered sorcery wizards */
176 struct ao2_container *wizards;
177
178 static int int_handler_fn(const void *obj, const intptr_t *args, char **buf)
179 {
180         int *field = (int *)(obj + args[0]);
181         return (ast_asprintf(buf, "%d", *field) < 0) ? -1 : 0;
182 }
183
184 static int uint_handler_fn(const void *obj, const intptr_t *args, char **buf)
185 {
186         unsigned int *field = (unsigned int *)(obj + args[0]);
187         return (ast_asprintf(buf, "%u", *field) < 0) ? -1 : 0;
188 }
189
190 static int double_handler_fn(const void *obj, const intptr_t *args, char **buf)
191 {
192         double *field = (double *)(obj + args[0]);
193         return (ast_asprintf(buf, "%f", *field) < 0) ? -1 : 0;
194 }
195
196 static int stringfield_handler_fn(const void *obj, const intptr_t *args, char **buf)
197 {
198         ast_string_field *field = (const char **)(obj + args[0]);
199         return !(*buf = ast_strdup(*field)) ? -1 : 0;
200 }
201
202 static int bool_handler_fn(const void *obj, const intptr_t *args, char **buf)
203 {
204         unsigned int *field = (unsigned int *)(obj + args[0]);
205         return !(*buf = ast_strdup(*field ? "true" : "false")) ? -1 : 0;
206 }
207
208 static int sockaddr_handler_fn(const void *obj, const intptr_t *args, char **buf)
209 {
210         struct ast_sockaddr *field = (struct ast_sockaddr *)(obj + args[0]);
211         return !(*buf = ast_strdup(ast_sockaddr_stringify(field))) ? -1 : 0;
212 }
213
214 static int chararray_handler_fn(const void *obj, const intptr_t *args, char **buf)
215 {
216         char *field = (char *)(obj + args[0]);
217         return !(*buf = ast_strdup(field)) ? -1 : 0;
218 }
219
220 static sorcery_field_handler sorcery_field_default_handler(enum aco_option_type type)
221 {
222         switch(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;
230
231         default:
232         case OPT_CUSTOM_T: return NULL;
233         }
234
235         return NULL;
236 }
237
238 /*! \brief Hashing function for sorcery wizards */
239 static int sorcery_wizard_hash(const void *obj, const int flags)
240 {
241         const struct ast_sorcery_wizard *wizard = obj;
242         const char *name = obj;
243
244         return ast_str_hash(flags & OBJ_KEY ? name : wizard->name);
245 }
246
247 /*! \brief Comparator function for sorcery wizards */
248 static int sorcery_wizard_cmp(void *obj, void *arg, int flags)
249 {
250         struct ast_sorcery_wizard *wizard1 = obj, *wizard2 = arg;
251         const char *name = arg;
252
253         return !strcmp(wizard1->name, flags & OBJ_KEY ? name : wizard2->name) ? CMP_MATCH | CMP_STOP : 0;
254 }
255
256 /*! \brief Cleanup function */
257 static void sorcery_exit(void)
258 {
259         ast_threadpool_shutdown(threadpool);
260         threadpool = NULL;
261 }
262
263 /*! \brief Cleanup function for graceful shutdowns */
264 static void sorcery_cleanup(void)
265 {
266         ao2_cleanup(wizards);
267 }
268
269 int ast_sorcery_init(void)
270 {
271         struct ast_threadpool_options options = {
272                 .version = AST_THREADPOOL_OPTIONS_VERSION,
273                 .auto_increment = 1,
274                 .max_size = 0,
275                 .idle_timeout = 60,
276                 .initial_size = 0,
277         };
278         ast_assert(wizards == NULL);
279
280         if (!(threadpool = ast_threadpool_create("Sorcery", NULL, &options))) {
281                 threadpool = NULL;
282                 return -1;
283         }
284
285         if (!(wizards = ao2_container_alloc(WIZARD_BUCKETS, sorcery_wizard_hash, sorcery_wizard_cmp))) {
286                 ast_threadpool_shutdown(threadpool);
287                 return -1;
288         }
289
290         ast_register_cleanup(sorcery_cleanup);
291         ast_register_atexit(sorcery_exit);
292
293         return 0;
294 }
295
296 int __ast_sorcery_wizard_register(const struct ast_sorcery_wizard *interface, struct ast_module *module)
297 {
298         struct ast_sorcery_wizard *wizard;
299         int res = -1;
300
301         ast_assert(!ast_strlen_zero(interface->name));
302
303         ao2_lock(wizards);
304
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",
307                         interface->name);
308                 goto done;
309         }
310
311         if (!(wizard = ao2_alloc(sizeof(*wizard), NULL))) {
312                 goto done;
313         }
314
315         *wizard = *interface;
316         wizard->module = module;
317
318         ao2_link_flags(wizards, wizard, OBJ_NOLOCK);
319         res = 0;
320
321         ast_verb(2, "Sorcery registered wizard '%s'\n", interface->name);
322
323 done:
324         ao2_cleanup(wizard);
325         ao2_unlock(wizards);
326
327         return res;
328 }
329
330 int ast_sorcery_wizard_unregister(const struct ast_sorcery_wizard *interface)
331 {
332         RAII_VAR(struct ast_sorcery_wizard *, wizard, ao2_find(wizards, interface->name, OBJ_KEY | OBJ_UNLINK), ao2_cleanup);
333
334         if (wizard) {
335                 ast_verb(2, "Sorcery unregistered wizard '%s'\n", interface->name);
336                 return 0;
337         } else {
338                 return -1;
339         }
340 }
341
342 /*! \brief Destructor called when sorcery structure is destroyed */
343 static void sorcery_destructor(void *obj)
344 {
345         struct ast_sorcery *sorcery = obj;
346
347         ao2_cleanup(sorcery->types);
348 }
349
350 /*! \brief Hashing function for sorcery types */
351 static int sorcery_type_hash(const void *obj, const int flags)
352 {
353         const struct ast_sorcery_object_type *type = obj;
354         const char *name = obj;
355
356         return ast_str_hash(flags & OBJ_KEY ? name : type->name);
357 }
358
359 /*! \brief Comparator function for sorcery types */
360 static int sorcery_type_cmp(void *obj, void *arg, int flags)
361 {
362         struct ast_sorcery_object_type *type1 = obj, *type2 = arg;
363         const char *name = arg;
364
365         return !strcmp(type1->name, flags & OBJ_KEY ? name : type2->name) ? CMP_MATCH | CMP_STOP : 0;
366 }
367
368 struct ast_sorcery *ast_sorcery_open(void)
369 {
370         struct ast_sorcery *sorcery;
371
372         if (!(sorcery = ao2_alloc(sizeof(*sorcery), sorcery_destructor))) {
373                 return NULL;
374         }
375
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);
378                 sorcery = NULL;
379         }
380
381         return sorcery;
382 }
383
384 /*! \brief Destructor function for object types */
385 static void sorcery_object_type_destructor(void *obj)
386 {
387         struct ast_sorcery_object_type *object_type = obj;
388
389         ao2_cleanup(object_type->wizards);
390         ao2_cleanup(object_type->fields);
391         ao2_cleanup(object_type->observers);
392
393         if (object_type->info) {
394                 aco_info_destroy(object_type->info);
395                 ast_free(object_type->info);
396         }
397
398         ast_free(object_type->file);
399
400         ast_taskprocessor_unreference(object_type->serializer);
401 }
402
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)
405 {
406         struct ast_sorcery_object_type *object_type;
407         char uuid[AST_UUID_STR_LEN];
408
409         if (!(object_type = ao2_alloc(sizeof(*object_type), sorcery_object_type_destructor))) {
410                 return NULL;
411         }
412
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);
416                 return NULL;
417         }
418
419         if (!(object_type->fields = ao2_container_alloc_options(AO2_ALLOC_OPT_LOCK_NOLOCK, 1, NULL, NULL))) {
420                 ao2_ref(object_type, -1);
421                 return NULL;
422         }
423
424         if (!(object_type->observers = ao2_container_alloc_options(AO2_ALLOC_OPT_LOCK_RWLOCK, 1, NULL, NULL))) {
425                 ao2_ref(object_type, -1);
426                 return NULL;
427         }
428
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);
431                 return NULL;
432         }
433
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);
436                 return NULL;
437         }
438
439         if (!ast_uuid_generate_str(uuid, sizeof(uuid))) {
440                 ao2_ref(object_type, -1);
441                 return NULL;
442         }
443
444         if (!(object_type->serializer = ast_threadpool_serializer(uuid, threadpool))) {
445                 ao2_ref(object_type, -1);
446                 return NULL;
447         }
448
449         object_type->info->files[0] = object_type->file;
450         object_type->info->files[1] = NULL;
451         object_type->info->module = module;
452
453         ast_copy_string(object_type->name, type, sizeof(object_type->name));
454
455         return object_type;
456 }
457
458 /*! \brief Object wizard destructor */
459 static void sorcery_object_wizard_destructor(void *obj)
460 {
461         struct ast_sorcery_object_wizard *object_wizard = obj;
462
463         if (object_wizard->data) {
464                 object_wizard->wizard->close(object_wizard->data);
465         }
466
467         if (object_wizard->wizard) {
468                 ast_module_unref(object_wizard->wizard->module);
469         }
470 }
471
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)
474 {
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);
478         int created = 0;
479
480         if (!wizard || !object_wizard) {
481                 return -1;
482         }
483
484         if (!object_type) {
485                 if (!(object_type = sorcery_object_type_alloc(type, module))) {
486                         return -1;
487                 }
488                 created = 1;
489         }
490
491         if (wizard->open && !(object_wizard->data = wizard->open(data))) {
492                 return -1;
493         }
494
495         ast_module_ref(wizard->module);
496
497         object_wizard->wizard = wizard;
498         object_wizard->caching = caching;
499
500         ao2_link(object_type->wizards, object_wizard);
501
502         if (created) {
503                 ao2_link(sorcery->types, object_type);
504         }
505
506         return 0;
507 }
508
509 int __ast_sorcery_apply_config(struct ast_sorcery *sorcery, const char *name, const char *module)
510 {
511         struct ast_flags flags = { 0 };
512         struct ast_config *config = ast_config_load2("sorcery.conf", "sorcery", flags);
513         struct ast_variable *mapping;
514         int res = 0;
515
516         if (!config || (config == CONFIG_STATUS_FILEMISSING) || (config == CONFIG_STATUS_FILEINVALID)) {
517                 return -1;
518         }
519
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;
526
527                 /* If no wizard exists just skip, nothing we can do */
528                 if (ast_strlen_zero(wizard)) {
529                         continue;
530                 }
531
532                 /* If the wizard is configured as a cache treat it as such */
533                 if (!ast_strlen_zero(options) && strstr(options, "cache")) {
534                         caching = 1;
535                 }
536
537                 /* Any error immediately causes us to stop */
538                 if ((res = sorcery_apply_wizard_mapping(sorcery, name, module, wizard, data, caching))) {
539                         break;
540                 }
541         }
542
543         ast_config_destroy(config);
544
545         return res;
546 }
547
548 int __ast_sorcery_apply_default(struct ast_sorcery *sorcery, const char *type, const char *module, const char *name, const char *data)
549 {
550         RAII_VAR(struct ast_sorcery_object_type *, object_type,  ao2_find(sorcery->types, type, OBJ_KEY), ao2_cleanup);
551
552         /* Defaults can not be added if any existing mapping exists */
553         if (object_type) {
554                 return -1;
555         }
556
557         return sorcery_apply_wizard_mapping(sorcery, type, module, name, data, 0);
558 }
559
560 static int sorcery_extended_config_handler(const struct aco_option *opt, struct ast_variable *var, void *obj)
561 {
562         return ast_sorcery_object_set_extended(obj, var->name, var->value);
563 }
564
565 static int sorcery_extended_fields_handler(const void *obj, struct ast_variable **fields)
566 {
567         const struct ast_sorcery_object_details *details = obj;
568
569         if (details->object->extended) {
570                 *fields = ast_variables_dup(details->object->extended);
571         } else {
572                 *fields = NULL;
573         }
574
575         return 0;
576 }
577
578 int __ast_sorcery_object_register(struct ast_sorcery *sorcery, const char *type, unsigned int hidden, unsigned int reloadable, aco_type_item_alloc alloc, sorcery_transform_handler transform, sorcery_apply_handler apply)
579 {
580         RAII_VAR(struct ast_sorcery_object_type *, object_type, ao2_find(sorcery->types, type, OBJ_KEY), ao2_cleanup);
581
582         if (!object_type || object_type->type.item_alloc) {
583                 return -1;
584         }
585
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;
591
592         object_type->reloadable = reloadable;
593         object_type->transform = transform;
594         object_type->apply = apply;
595         object_type->file->types[0] = &object_type->type;
596         object_type->file->types[1] = NULL;
597
598         if (aco_info_init(object_type->info)) {
599                 return -1;
600         }
601
602         if (ast_sorcery_object_fields_register(sorcery, type, "^@", sorcery_extended_config_handler, sorcery_extended_fields_handler)) {
603                 return -1;
604         }
605
606         return 0;
607 }
608
609 void ast_sorcery_object_set_copy_handler(struct ast_sorcery *sorcery, const char *type, sorcery_copy_handler copy)
610 {
611         RAII_VAR(struct ast_sorcery_object_type *, object_type, ao2_find(sorcery->types, type, OBJ_KEY), ao2_cleanup);
612
613         if (!object_type) {
614                 return;
615         }
616
617         object_type->copy = copy;
618 }
619
620 void ast_sorcery_object_set_diff_handler(struct ast_sorcery *sorcery, const char *type, sorcery_diff_handler diff)
621 {
622         RAII_VAR(struct ast_sorcery_object_type *, object_type, ao2_find(sorcery->types, type, OBJ_KEY), ao2_cleanup);
623
624         if (!object_type) {
625                 return;
626         }
627
628         object_type->diff = diff;
629 }
630
631 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 {
633         RAII_VAR(struct ast_sorcery_object_type *, object_type, ao2_find(sorcery->types, type, OBJ_KEY), ao2_cleanup);
634         RAII_VAR(struct ast_sorcery_object_field *, object_field, NULL, ao2_cleanup);
635
636         if (!object_type || !object_type->type.item_alloc || !config_handler || !(object_field = ao2_alloc(sizeof(*object_field), NULL))) {
637                 return -1;
638         }
639
640         ast_copy_string(object_field->name, regex, sizeof(object_field->name));
641         object_field->multiple_handler = sorcery_handler;
642
643         ao2_link(object_type->fields, object_field);
644         __aco_option_register(object_type->info, regex, ACO_REGEX, object_type->file->types, "", OPT_CUSTOM_T, config_handler, 0, 1, 0);
645
646         return 0;
647 }
648
649 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,
650                                         aco_option_handler config_handler, sorcery_field_handler sorcery_handler, unsigned int flags, unsigned int no_doc, size_t argc, ...)
651 {
652         RAII_VAR(struct ast_sorcery_object_type *, object_type, ao2_find(sorcery->types, type, OBJ_KEY), ao2_cleanup);
653         RAII_VAR(struct ast_sorcery_object_field *, object_field, NULL, ao2_cleanup);
654         int pos;
655         va_list args;
656
657         if (!strcmp(type, "id") || !object_type || !object_type->type.item_alloc) {
658                 return -1;
659         }
660
661         if (!sorcery_handler) {
662                 sorcery_handler = sorcery_field_default_handler(opt_type);
663         }
664
665         if (!(object_field = ao2_alloc(sizeof(*object_field) + argc * sizeof(object_field->args[0]), NULL))) {
666                 return -1;
667         }
668
669         ast_copy_string(object_field->name, name, sizeof(object_field->name));
670         object_field->handler = sorcery_handler;
671
672         va_start(args, argc);
673         for (pos = 0; pos < argc; pos++) {
674                 object_field->args[pos] = va_arg(args, size_t);
675         }
676         va_end(args);
677
678         ao2_link(object_type->fields, object_field);
679
680         /* TODO: Improve this hack */
681         if (!argc) {
682                 __aco_option_register(object_type->info, name, ACO_EXACT, object_type->file->types, default_val, opt_type, config_handler, flags, no_doc, argc);
683         } else if (argc == 1) {
684                 __aco_option_register(object_type->info, name, ACO_EXACT, object_type->file->types, default_val, opt_type, config_handler, flags, no_doc, argc,
685                                       object_field->args[0]);
686         } else if (argc == 2) {
687                 __aco_option_register(object_type->info, name, ACO_EXACT, object_type->file->types, default_val, opt_type, config_handler, flags, no_doc, argc,
688                                       object_field->args[0], object_field->args[1]);
689         } else if (argc == 3) {
690                 __aco_option_register(object_type->info, name, ACO_EXACT, object_type->file->types, default_val, opt_type, config_handler, flags, no_doc, argc,
691                                       object_field->args[0], object_field->args[1], object_field->args[2]);
692         } else {
693                 ast_assert(0); /* The hack... she does us no good for this */
694         }
695
696         return 0;
697 }
698
699 /*! \brief Retrieves whether or not the type is reloadable */
700 static int sorcery_reloadable(const struct ast_sorcery *sorcery, const char *type)
701 {
702         RAII_VAR(struct ast_sorcery_object_type *, object_type,
703                  ao2_find(sorcery->types, type, OBJ_KEY), ao2_cleanup);
704         return object_type && object_type->reloadable;
705 }
706
707 static int sorcery_wizard_load(void *obj, void *arg, int flags)
708 {
709         struct ast_sorcery_object_wizard *wizard = obj;
710         struct sorcery_load_details *details = arg;
711         void (*load)(void *data, const struct ast_sorcery *sorcery, const char *type);
712
713         if (details->reload && !sorcery_reloadable(details->sorcery, details->type)) {
714                 ast_log(LOG_NOTICE, "Type '%s' is not reloadable, "
715                         "maintaining previous values\n", details->type);
716                 return 0;
717         }
718
719         load = !details->reload ? wizard->wizard->load : wizard->wizard->reload;
720
721         if (load) {
722                 load(wizard->data, details->sorcery, details->type);
723         }
724
725         return 0;
726 }
727
728 /*! \brief Destructor for observer invocation */
729 static void sorcery_observer_invocation_destroy(void *obj)
730 {
731         struct sorcery_observer_invocation *invocation = obj;
732
733         ao2_cleanup(invocation->object_type);
734         ao2_cleanup(invocation->object);
735 }
736
737 /*! \brief Allocator function for observer invocation */
738 static struct sorcery_observer_invocation *sorcery_observer_invocation_alloc(struct ast_sorcery_object_type *object_type, void *object)
739 {
740         struct sorcery_observer_invocation *invocation = ao2_alloc(sizeof(*invocation), sorcery_observer_invocation_destroy);
741
742         if (!invocation) {
743                 return NULL;
744         }
745
746         ao2_ref(object_type, +1);
747         invocation->object_type = object_type;
748
749         if (object) {
750                 ao2_ref(object, +1);
751                 invocation->object = object;
752         }
753
754         return invocation;
755 }
756
757 /*! \brief Internal callback function which notifies an individual observer that an object type has been loaded */
758 static int sorcery_observer_notify_loaded(void *obj, void *arg, int flags)
759 {
760         const struct ast_sorcery_object_type_observer *observer = obj;
761
762         if (observer->callbacks->loaded) {
763                 observer->callbacks->loaded(arg);
764         }
765
766         return 0;
767 }
768
769 /*! \brief Internal callback function which notifies observers that an object type has been loaded */
770 static int sorcery_observers_notify_loaded(void *data)
771 {
772         struct sorcery_observer_invocation *invocation = data;
773
774         ao2_callback(invocation->object_type->observers, OBJ_NODATA, sorcery_observer_notify_loaded, invocation->object_type->name);
775         ao2_cleanup(invocation);
776
777         return 0;
778 }
779
780 static int sorcery_object_load(void *obj, void *arg, int flags)
781 {
782         struct ast_sorcery_object_type *type = obj;
783         struct sorcery_load_details *details = arg;
784
785         details->type = type->name;
786         ao2_callback(type->wizards, OBJ_NODATA, sorcery_wizard_load, details);
787
788         if (ao2_container_count(type->observers)) {
789                 struct sorcery_observer_invocation *invocation = sorcery_observer_invocation_alloc(type, NULL);
790
791                 if (invocation && ast_taskprocessor_push(type->serializer, sorcery_observers_notify_loaded, invocation)) {
792                         ao2_cleanup(invocation);
793                 }
794         }
795
796         return 0;
797 }
798
799 void ast_sorcery_load(const struct ast_sorcery *sorcery)
800 {
801         struct sorcery_load_details details = {
802                 .sorcery = sorcery,
803                 .reload = 0,
804         };
805
806         ao2_callback(sorcery->types, OBJ_NODATA, sorcery_object_load, &details);
807 }
808
809 void ast_sorcery_load_object(const struct ast_sorcery *sorcery, const char *type)
810 {
811         RAII_VAR(struct ast_sorcery_object_type *, object_type, ao2_find(sorcery->types, type, OBJ_KEY), ao2_cleanup);
812         struct sorcery_load_details details = {
813                 .sorcery = sorcery,
814                 .reload = 0,
815         };
816
817         if (!object_type) {
818                 return;
819         }
820
821         sorcery_object_load(object_type, &details, 0);
822 }
823
824 void ast_sorcery_reload(const struct ast_sorcery *sorcery)
825 {
826         struct sorcery_load_details details = {
827                 .sorcery = sorcery,
828                 .reload = 1,
829         };
830
831         ao2_callback(sorcery->types, OBJ_NODATA, sorcery_object_load, &details);
832 }
833
834 void ast_sorcery_reload_object(const struct ast_sorcery *sorcery, const char *type)
835 {
836         RAII_VAR(struct ast_sorcery_object_type *, object_type, ao2_find(sorcery->types, type, OBJ_KEY), ao2_cleanup);
837         struct sorcery_load_details details = {
838                 .sorcery = sorcery,
839                 .reload = 1,
840         };
841
842         if (!object_type) {
843                 return;
844         }
845
846         sorcery_object_load(object_type, &details, 0);
847 }
848
849 void ast_sorcery_ref(struct ast_sorcery *sorcery)
850 {
851         ao2_ref(sorcery, +1);
852 }
853
854 struct ast_variable *ast_sorcery_objectset_create(const struct ast_sorcery *sorcery, const void *object)
855 {
856         const struct ast_sorcery_object_details *details = object;
857         RAII_VAR(struct ast_sorcery_object_type *, object_type, ao2_find(sorcery->types, details->object->type, OBJ_KEY), ao2_cleanup);
858         struct ao2_iterator i;
859         struct ast_sorcery_object_field *object_field;
860         struct ast_variable *fields = NULL;
861         int res = 0;
862
863         if (!object_type) {
864                 return NULL;
865         }
866
867         i = ao2_iterator_init(object_type->fields, 0);
868
869         for (; (object_field = ao2_iterator_next(&i)) && !res; ao2_ref(object_field, -1)) {
870                 struct ast_variable *tmp = NULL;
871
872                 if (object_field->multiple_handler) {
873                         if ((res = object_field->multiple_handler(object, &tmp))) {
874                                 ast_variables_destroy(tmp);
875                         }
876                 } else if (object_field->handler) {
877                         char *buf = NULL;
878
879                         if ((res = object_field->handler(object, object_field->args, &buf)) ||
880                                 !(tmp = ast_variable_new(object_field->name, S_OR(buf, ""), ""))) {
881                                 res = -1;
882                         }
883
884                         ast_free(buf);
885                 } else {
886                         continue;
887                 }
888
889                 if (!res && tmp) {
890                         tmp->next = fields;
891                         fields = tmp;
892                 }
893         }
894
895         ao2_iterator_destroy(&i);
896
897         /* If any error occurs we destroy all fields handled before so a partial objectset is not returned */
898         if (res) {
899                 ast_variables_destroy(fields);
900                 fields = NULL;
901         }
902
903         return fields;
904 }
905
906 struct ast_json *ast_sorcery_objectset_json_create(const struct ast_sorcery *sorcery, const void *object)
907 {
908         const struct ast_sorcery_object_details *details = object;
909         RAII_VAR(struct ast_sorcery_object_type *, object_type, ao2_find(sorcery->types, details->object->type, OBJ_KEY), ao2_cleanup);
910         struct ao2_iterator i;
911         struct ast_sorcery_object_field *object_field;
912         struct ast_json *json = ast_json_object_create();
913         int res = 0;
914
915         if (!object_type || !json) {
916                 return NULL;
917         }
918
919         i = ao2_iterator_init(object_type->fields, 0);
920
921         for (; (object_field = ao2_iterator_next(&i)) && !res; ao2_ref(object_field, -1)) {
922                 if (object_field->multiple_handler) {
923                         struct ast_variable *tmp = NULL;
924                         struct ast_variable *field;
925
926                         if ((res = object_field->multiple_handler(object, &tmp))) {
927                                 ao2_ref(object_field, -1);
928                                 break;
929                         }
930
931                         for (field = tmp; field; field = field->next) {
932                                 struct ast_json *value = ast_json_string_create(field->value);
933
934                                 if (value && ast_json_object_set(json, field->name, value)) {
935                                         ast_json_unref(value);
936                                         res = -1;
937                                 }
938                         }
939
940                         ast_variables_destroy(tmp);
941                 } else if (object_field->handler) {
942                         char *buf = NULL;
943                         struct ast_json *value = NULL;
944
945                         if ((res = object_field->handler(object, object_field->args, &buf)) ||
946                                 !(value = ast_json_string_create(buf)) ||
947                                 ast_json_object_set(json, object_field->name, value)) {
948                                 ast_json_unref(value);
949                                 res = -1;
950                         }
951
952                         ast_free(buf);
953                 } else {
954                         continue;
955                 }
956         }
957
958         ao2_iterator_destroy(&i);
959
960         /* If any error occurs we destroy the JSON object so a partial objectset is not returned */
961         if (res) {
962                 ast_json_unref(json);
963                 json = NULL;
964         }
965
966         return json;
967 }
968
969 int ast_sorcery_objectset_apply(const struct ast_sorcery *sorcery, void *object, struct ast_variable *objectset)
970 {
971         const struct ast_sorcery_object_details *details = object;
972         RAII_VAR(struct ast_sorcery_object_type *, object_type, ao2_find(sorcery->types, details->object->type, OBJ_KEY), ao2_cleanup);
973         RAII_VAR(struct ast_variable *, transformed, NULL, ast_variables_destroy);
974         struct ast_variable *field;
975         int res = 0;
976
977         if (!object_type) {
978                 return -1;
979         }
980
981         if (object_type->transform && (transformed = object_type->transform(objectset))) {
982                 field = transformed;
983         } else {
984                 field = objectset;
985         }
986
987         for (; field; field = field->next) {
988                 if ((res = aco_process_var(&object_type->type, details->object->id, field, object))) {
989                         break;
990                 }
991         }
992
993         if (!res && object_type->apply) {
994                 res = object_type->apply(sorcery, object);
995         }
996
997         return res;
998 }
999
1000 static const struct ast_variable *sorcery_find_field(const struct ast_variable *fields, const char *name)
1001 {
1002         const struct ast_variable *field;
1003
1004         /* Search the linked list of fields to find the correct one */
1005         for (field = fields; field; field = field->next) {
1006                 if (!strcmp(field->name, name)) {
1007                         return field;
1008                 }
1009         }
1010
1011         return NULL;
1012 }
1013
1014 int ast_sorcery_changeset_create(const struct ast_variable *original, const struct ast_variable *modified, struct ast_variable **changes)
1015 {
1016         const struct ast_variable *field;
1017         int res = 0;
1018
1019         *changes = NULL;
1020
1021         /* Unless the ast_variable list changes when examined... it can't differ from itself */
1022         if (original == modified) {
1023                 return 0;
1024         }
1025
1026         for (field = modified; field; field = field->next) {
1027                 const struct ast_variable *old = sorcery_find_field(original, field->name);
1028
1029                 if (!old || strcmp(old->value, field->value)) {
1030                         struct ast_variable *tmp;
1031
1032                         if (!(tmp = ast_variable_new(field->name, field->value, ""))) {
1033                                 res = -1;
1034                                 break;
1035                         }
1036
1037                         tmp->next = *changes;
1038                         *changes = tmp;
1039                 }
1040         }
1041
1042         /* If an error occurred do not return a partial changeset */
1043         if (res) {
1044                 ast_variables_destroy(*changes);
1045                 *changes = NULL;
1046         }
1047
1048         return res;
1049 }
1050
1051 static void sorcery_object_destructor(void *object)
1052 {
1053         struct ast_sorcery_object_details *details = object;
1054
1055         if (details->object->destructor) {
1056                 details->object->destructor(object);
1057         }
1058
1059         ast_variables_destroy(details->object->extended);
1060         ast_free(details->object->id);
1061 }
1062
1063 void *ast_sorcery_generic_alloc(size_t size, ao2_destructor_fn destructor)
1064 {
1065         void *object = ao2_alloc_options(size + sizeof(struct ast_sorcery_object), sorcery_object_destructor, AO2_ALLOC_OPT_LOCK_NOLOCK);
1066         struct ast_sorcery_object_details *details = object;
1067
1068         if (!object) {
1069                 return NULL;
1070         }
1071
1072         details->object = object + size;
1073         details->object->destructor = destructor;
1074
1075         return object;
1076 }
1077
1078 void *ast_sorcery_alloc(const struct ast_sorcery *sorcery, const char *type, const char *id)
1079 {
1080         RAII_VAR(struct ast_sorcery_object_type *, object_type, ao2_find(sorcery->types, type, OBJ_KEY), ao2_cleanup);
1081         struct ast_sorcery_object_details *details;
1082
1083         if (!object_type || !object_type->type.item_alloc ||
1084             !(details = object_type->type.item_alloc(id))) {
1085                 return NULL;
1086         }
1087
1088         if (ast_strlen_zero(id)) {
1089                 char uuid[AST_UUID_STR_LEN];
1090
1091                 ast_uuid_generate_str(uuid, sizeof(uuid));
1092                 details->object->id = ast_strdup(uuid);
1093         } else {
1094                 details->object->id = ast_strdup(id);
1095         }
1096
1097         ast_copy_string(details->object->type, type, sizeof(details->object->type));
1098
1099         if (aco_set_defaults(&object_type->type, id, details)) {
1100                 ao2_ref(details, -1);
1101                 return NULL;
1102         }
1103
1104         return details;
1105 }
1106
1107 void *ast_sorcery_copy(const struct ast_sorcery *sorcery, const void *object)
1108 {
1109         const struct ast_sorcery_object_details *details = object;
1110         RAII_VAR(struct ast_sorcery_object_type *, object_type, ao2_find(sorcery->types, details->object->type, OBJ_KEY), ao2_cleanup);
1111         struct ast_sorcery_object_details *copy = ast_sorcery_alloc(sorcery, details->object->type, details->object->id);
1112         RAII_VAR(struct ast_variable *, objectset, NULL, ast_variables_destroy);
1113         int res = 0;
1114
1115         if (!copy) {
1116                 return NULL;
1117         } else if (object_type->copy) {
1118                 res = object_type->copy(object, copy);
1119         } else if ((objectset = ast_sorcery_objectset_create(sorcery, object))) {
1120                 res = ast_sorcery_objectset_apply(sorcery, copy, objectset);
1121         } else {
1122                 /* No native copy available and could not create an objectset, this copy has failed */
1123                 res = -1;
1124         }
1125
1126         if (res) {
1127                 ao2_cleanup(copy);
1128                 copy = NULL;
1129         }
1130
1131         return copy;
1132 }
1133
1134 int ast_sorcery_diff(const struct ast_sorcery *sorcery, const void *original, const void *modified, struct ast_variable **changes)
1135 {
1136         RAII_VAR(struct ast_sorcery_object_type *, object_type, ao2_find(sorcery->types, ast_sorcery_object_get_type(original), OBJ_KEY), ao2_cleanup);
1137
1138         *changes = NULL;
1139
1140         if (strcmp(ast_sorcery_object_get_type(original), ast_sorcery_object_get_type(modified))) {
1141                 return -1;
1142         }
1143
1144         if (original == modified) {
1145                 return 0;
1146         } else if (!object_type->diff) {
1147                 RAII_VAR(struct ast_variable *, objectset1, NULL, ast_variables_destroy);
1148                 RAII_VAR(struct ast_variable *, objectset2, NULL, ast_variables_destroy);
1149
1150                 objectset1 = ast_sorcery_objectset_create(sorcery, original);
1151                 objectset2 = ast_sorcery_objectset_create(sorcery, modified);
1152
1153                 return ast_sorcery_changeset_create(objectset1, objectset2, changes);
1154         } else {
1155                 return object_type->diff(original, modified, changes);
1156         }
1157 }
1158
1159 /*! \brief Structure used when calling create, update, or delete */
1160 struct sorcery_details {
1161         /*! \brief Pointer to the sorcery instance */
1162         const struct ast_sorcery *sorcery;
1163         /*! \brief Pointer to the object itself */
1164         void *obj;
1165 };
1166
1167 /*! \brief Internal function used to create an object in caching wizards */
1168 static int sorcery_cache_create(void *obj, void *arg, int flags)
1169 {
1170         const struct ast_sorcery_object_wizard *object_wizard = obj;
1171         const struct sorcery_details *details = arg;
1172
1173         if (!object_wizard->caching || !object_wizard->wizard->create) {
1174                 return 0;
1175         }
1176
1177         object_wizard->wizard->create(details->sorcery, object_wizard->data, details->obj);
1178
1179         return 0;
1180 }
1181
1182 void *ast_sorcery_retrieve_by_id(const struct ast_sorcery *sorcery, const char *type, const char *id)
1183 {
1184         RAII_VAR(struct ast_sorcery_object_type *, object_type, ao2_find(sorcery->types, type, OBJ_KEY), ao2_cleanup);
1185         void *object = NULL;
1186         struct ao2_iterator i;
1187         struct ast_sorcery_object_wizard *wizard;
1188         unsigned int cached = 0;
1189
1190         if (!object_type || ast_strlen_zero(id)) {
1191                 return NULL;
1192         }
1193
1194         i = ao2_iterator_init(object_type->wizards, 0);
1195         for (; (wizard = ao2_iterator_next(&i)); ao2_ref(wizard, -1)) {
1196                 if (wizard->wizard->retrieve_id &&
1197                     !(object = wizard->wizard->retrieve_id(sorcery, wizard->data, object_type->name, id))) {
1198                         continue;
1199                 }
1200
1201                 cached = wizard->caching;
1202
1203                 ao2_ref(wizard, -1);
1204                 break;
1205         }
1206         ao2_iterator_destroy(&i);
1207
1208         if (!cached && object) {
1209                 ao2_callback(object_type->wizards, 0, sorcery_cache_create, object);
1210         }
1211
1212         return object;
1213 }
1214
1215 void *ast_sorcery_retrieve_by_fields(const struct ast_sorcery *sorcery, const char *type, unsigned int flags, struct ast_variable *fields)
1216 {
1217         RAII_VAR(struct ast_sorcery_object_type *, object_type, ao2_find(sorcery->types, type, OBJ_KEY), ao2_cleanup);
1218         void *object = NULL;
1219         struct ao2_iterator i;
1220         struct ast_sorcery_object_wizard *wizard;
1221         unsigned int cached = 0;
1222
1223         if (!object_type) {
1224                 return NULL;
1225         }
1226
1227         /* If returning multiple objects create a container to store them in */
1228         if ((flags & AST_RETRIEVE_FLAG_MULTIPLE)) {
1229                 if (!(object = ao2_container_alloc_options(AO2_ALLOC_OPT_LOCK_NOLOCK, 1, NULL, NULL))) {
1230                         return NULL;
1231                 }
1232         }
1233
1234         /* Inquire with the available wizards for retrieval */
1235         i = ao2_iterator_init(object_type->wizards, 0);
1236         for (; (wizard = ao2_iterator_next(&i)); ao2_ref(wizard, -1)) {
1237                 if ((flags & AST_RETRIEVE_FLAG_MULTIPLE)) {
1238                         if (wizard->wizard->retrieve_multiple) {
1239                                 wizard->wizard->retrieve_multiple(sorcery, wizard->data, object_type->name, object, fields);
1240                         }
1241                 } else if (fields && wizard->wizard->retrieve_fields) {
1242                         if (wizard->wizard->retrieve_fields) {
1243                                 object = wizard->wizard->retrieve_fields(sorcery, wizard->data, object_type->name, fields);
1244                         }
1245                 }
1246
1247                 if ((flags & AST_RETRIEVE_FLAG_MULTIPLE) || !object) {
1248                         continue;
1249                 }
1250
1251                 cached = wizard->caching;
1252
1253                 ao2_ref(wizard, -1);
1254                 break;
1255         }
1256         ao2_iterator_destroy(&i);
1257
1258         /* If we are returning a single object and it came from a non-cache source create it in any caches */
1259         if (!(flags & AST_RETRIEVE_FLAG_MULTIPLE) && !cached && object) {
1260                 ao2_callback(object_type->wizards, 0, sorcery_cache_create, object);
1261         }
1262
1263         return object;
1264 }
1265
1266 struct ao2_container *ast_sorcery_retrieve_by_regex(const struct ast_sorcery *sorcery, const char *type, const char *regex)
1267 {
1268         RAII_VAR(struct ast_sorcery_object_type *, object_type, ao2_find(sorcery->types, type, OBJ_KEY), ao2_cleanup);
1269         struct ao2_container *objects;
1270         struct ao2_iterator i;
1271         struct ast_sorcery_object_wizard *wizard;
1272
1273         if (!object_type || !(objects = ao2_container_alloc_options(AO2_ALLOC_OPT_LOCK_NOLOCK, 1, NULL, NULL))) {
1274                 return NULL;
1275         }
1276
1277         i = ao2_iterator_init(object_type->wizards, 0);
1278         for (; (wizard = ao2_iterator_next(&i)); ao2_ref(wizard, -1)) {
1279                 if (!wizard->wizard->retrieve_regex) {
1280                         continue;
1281                 }
1282
1283                 wizard->wizard->retrieve_regex(sorcery, wizard->data, object_type->name, objects, regex);
1284         }
1285         ao2_iterator_destroy(&i);
1286
1287         return objects;
1288 }
1289
1290 /*! \brief Internal function which returns if the wizard has created the object */
1291 static int sorcery_wizard_create(void *obj, void *arg, int flags)
1292 {
1293         const struct ast_sorcery_object_wizard *object_wizard = obj;
1294         const struct sorcery_details *details = arg;
1295
1296         return (!object_wizard->caching && !object_wizard->wizard->create(details->sorcery, object_wizard->data, details->obj)) ? CMP_MATCH | CMP_STOP : 0;
1297 }
1298
1299 /*! \brief Internal callback function which notifies an individual observer that an object has been created */
1300 static int sorcery_observer_notify_create(void *obj, void *arg, int flags)
1301 {
1302         const struct ast_sorcery_object_type_observer *observer = obj;
1303
1304         if (observer->callbacks->created) {
1305                 observer->callbacks->created(arg);
1306         }
1307
1308         return 0;
1309 }
1310
1311 /*! \brief Internal callback function which notifies observers that an object has been created */
1312 static int sorcery_observers_notify_create(void *data)
1313 {
1314         struct sorcery_observer_invocation *invocation = data;
1315
1316         ao2_callback(invocation->object_type->observers, OBJ_NODATA, sorcery_observer_notify_create, invocation->object);
1317         ao2_cleanup(invocation);
1318
1319         return 0;
1320 }
1321
1322 int ast_sorcery_create(const struct ast_sorcery *sorcery, void *object)
1323 {
1324         const struct ast_sorcery_object_details *details = object;
1325         RAII_VAR(struct ast_sorcery_object_type *, object_type, ao2_find(sorcery->types, details->object->type, OBJ_KEY), ao2_cleanup);
1326         RAII_VAR(struct ast_sorcery_object_wizard *, object_wizard, NULL, ao2_cleanup);
1327         struct sorcery_details sdetails = {
1328                 .sorcery = sorcery,
1329                 .obj = object,
1330         };
1331
1332         if (!object_type) {
1333                 return -1;
1334         }
1335
1336         if ((object_wizard = ao2_callback(object_type->wizards, 0, sorcery_wizard_create, &sdetails)) &&
1337                 ao2_container_count(object_type->observers)) {
1338                 struct sorcery_observer_invocation *invocation = sorcery_observer_invocation_alloc(object_type, object);
1339
1340                 if (invocation && ast_taskprocessor_push(object_type->serializer, sorcery_observers_notify_create, invocation)) {
1341                         ao2_cleanup(invocation);
1342                 }
1343         }
1344
1345         return object_wizard ? 0 : -1;
1346 }
1347
1348 /*! \brief Internal callback function which notifies an individual observer that an object has been updated */
1349 static int sorcery_observer_notify_update(void *obj, void *arg, int flags)
1350 {
1351         const struct ast_sorcery_object_type_observer *observer = obj;
1352
1353         if (observer->callbacks->updated) {
1354                 observer->callbacks->updated(arg);
1355         }
1356
1357         return 0;
1358 }
1359
1360 /*! \brief Internal callback function which notifies observers that an object has been updated */
1361 static int sorcery_observers_notify_update(void *data)
1362 {
1363         struct sorcery_observer_invocation *invocation = data;
1364
1365         ao2_callback(invocation->object_type->observers, OBJ_NODATA, sorcery_observer_notify_update, invocation->object);
1366         ao2_cleanup(invocation);
1367
1368         return 0;
1369 }
1370
1371 /*! \brief Internal function which returns if a wizard has updated the object */
1372 static int sorcery_wizard_update(void *obj, void *arg, int flags)
1373 {
1374         const struct ast_sorcery_object_wizard *object_wizard = obj;
1375         const struct sorcery_details *details = arg;
1376
1377         return (object_wizard->wizard->update && !object_wizard->wizard->update(details->sorcery, object_wizard->data, details->obj) &&
1378                 !object_wizard->caching) ? CMP_MATCH | CMP_STOP : 0;
1379 }
1380
1381 int ast_sorcery_update(const struct ast_sorcery *sorcery, void *object)
1382 {
1383         const struct ast_sorcery_object_details *details = object;
1384         RAII_VAR(struct ast_sorcery_object_type *, object_type, ao2_find(sorcery->types, details->object->type, OBJ_KEY), ao2_cleanup);
1385         RAII_VAR(struct ast_sorcery_object_wizard *, object_wizard, NULL, ao2_cleanup);
1386         struct sorcery_details sdetails = {
1387                 .sorcery = sorcery,
1388                 .obj = object,
1389         };
1390
1391         if (!object_type) {
1392                 return -1;
1393         }
1394
1395         if ((object_wizard = ao2_callback(object_type->wizards, 0, sorcery_wizard_update, &sdetails)) &&
1396                 ao2_container_count(object_type->observers)) {
1397                 struct sorcery_observer_invocation *invocation = sorcery_observer_invocation_alloc(object_type, object);
1398
1399                 if (invocation && ast_taskprocessor_push(object_type->serializer, sorcery_observers_notify_update, invocation)) {
1400                         ao2_cleanup(invocation);
1401                 }
1402         }
1403
1404         return object_wizard ? 0 : -1;
1405 }
1406
1407 /*! \brief Internal callback function which notifies an individual observer that an object has been deleted */
1408 static int sorcery_observer_notify_delete(void *obj, void *arg, int flags)
1409 {
1410         const struct ast_sorcery_object_type_observer *observer = obj;
1411
1412         if (observer->callbacks->deleted) {
1413                 observer->callbacks->deleted(arg);
1414         }
1415
1416         return 0;
1417 }
1418
1419 /*! \brief Internal callback function which notifies observers that an object has been deleted */
1420 static int sorcery_observers_notify_delete(void *data)
1421 {
1422         struct sorcery_observer_invocation *invocation = data;
1423
1424         ao2_callback(invocation->object_type->observers, OBJ_NODATA, sorcery_observer_notify_delete, invocation->object);
1425         ao2_cleanup(invocation);
1426
1427         return 0;
1428 }
1429
1430 /*! \brief Internal function which returns if a wizard has deleted the object */
1431 static int sorcery_wizard_delete(void *obj, void *arg, int flags)
1432 {
1433         const struct ast_sorcery_object_wizard *object_wizard = obj;
1434         const struct sorcery_details *details = arg;
1435
1436         return (object_wizard->wizard->delete && !object_wizard->wizard->delete(details->sorcery, object_wizard->data, details->obj) &&
1437                 !object_wizard->caching) ? CMP_MATCH | CMP_STOP : 0;
1438 }
1439
1440 int ast_sorcery_delete(const struct ast_sorcery *sorcery, void *object)
1441 {
1442         const struct ast_sorcery_object_details *details = object;
1443         RAII_VAR(struct ast_sorcery_object_type *, object_type, ao2_find(sorcery->types, details->object->type, OBJ_KEY), ao2_cleanup);
1444         RAII_VAR(struct ast_sorcery_object_wizard *, object_wizard, NULL, ao2_cleanup);
1445         struct sorcery_details sdetails = {
1446                 .sorcery = sorcery,
1447                 .obj = object,
1448         };
1449
1450         if (!object_type) {
1451                 return -1;
1452         }
1453
1454         if ((object_wizard = ao2_callback(object_type->wizards, 0, sorcery_wizard_delete, &sdetails)) &&
1455                 ao2_container_count(object_type->observers)) {
1456                 struct sorcery_observer_invocation *invocation = sorcery_observer_invocation_alloc(object_type, object);
1457
1458                 if (invocation && ast_taskprocessor_push(object_type->serializer, sorcery_observers_notify_delete, invocation)) {
1459                         ao2_cleanup(invocation);
1460                 }
1461         }
1462
1463         return object_wizard ? 0 : -1;
1464 }
1465
1466 void ast_sorcery_unref(struct ast_sorcery *sorcery)
1467 {
1468         ao2_cleanup(sorcery);
1469 }
1470
1471 const char *ast_sorcery_object_get_id(const void *object)
1472 {
1473         const struct ast_sorcery_object_details *details = object;
1474         return details->object->id;
1475 }
1476
1477 const char *ast_sorcery_object_get_type(const void *object)
1478 {
1479         const struct ast_sorcery_object_details *details = object;
1480         return details->object->type;
1481 }
1482
1483 const char *ast_sorcery_object_get_extended(const void *object, const char *name)
1484 {
1485         const struct ast_sorcery_object_details *details = object;
1486         struct ast_variable *field;
1487
1488         for (field = details->object->extended; field; field = field->next) {
1489                 if (!strcmp(field->name + 1, name)) {
1490                         return field->value;
1491                 }
1492         }
1493
1494         return NULL;
1495 }
1496
1497 int ast_sorcery_object_set_extended(const void *object, const char *name, const char *value)
1498 {
1499         RAII_VAR(struct ast_variable *, field, NULL, ast_variables_destroy);
1500         struct ast_variable *extended = ast_variable_new(name, value, ""), *previous = NULL;
1501         const struct ast_sorcery_object_details *details = object;
1502
1503         if (!extended) {
1504                 return -1;
1505         }
1506
1507         for (field = details->object->extended; field; previous = field, field = field->next) {
1508                 if (!strcmp(field->name, name)) {
1509                         if (previous) {
1510                                 previous->next = field->next;
1511                         } else {
1512                                 details->object->extended = field->next;
1513                         }
1514                         field->next = NULL;
1515                         break;
1516                 }
1517         }
1518
1519         extended->next = details->object->extended;
1520         details->object->extended = extended;
1521
1522         return 0;
1523 }
1524
1525 int ast_sorcery_observer_add(const struct ast_sorcery *sorcery, const char *type, const struct ast_sorcery_observer *callbacks)
1526 {
1527         RAII_VAR(struct ast_sorcery_object_type *, object_type, ao2_find(sorcery->types, type, OBJ_KEY), ao2_cleanup);
1528         struct ast_sorcery_object_type_observer *observer;
1529         int res;
1530
1531         if (!object_type || !callbacks) {
1532                 return -1;
1533         }
1534
1535         if (!(observer = ao2_alloc(sizeof(*observer), NULL))) {
1536                 return -1;
1537         }
1538
1539         observer->callbacks = callbacks;
1540         res = 0;
1541         if (!ao2_link(object_type->observers, observer)) {
1542                 res = -1;
1543         }
1544         ao2_ref(observer, -1);
1545
1546         return res;
1547 }
1548
1549 /*! \brief Internal callback function for removing an observer */
1550 static int sorcery_observer_remove(void *obj, void *arg, int flags)
1551 {
1552         const struct ast_sorcery_object_type_observer *observer = obj;
1553
1554         return (observer->callbacks == arg) ? CMP_MATCH | CMP_STOP : 0;
1555 }
1556
1557 void ast_sorcery_observer_remove(const struct ast_sorcery *sorcery, const char *type, const struct ast_sorcery_observer *callbacks)
1558 {
1559         RAII_VAR(struct ast_sorcery_object_type *, object_type, NULL, ao2_cleanup);
1560         struct ast_sorcery_observer *cbs = (struct ast_sorcery_observer *) callbacks;/* Remove const for traversal. */
1561
1562         if (!sorcery) {
1563                 return;
1564         }
1565         object_type = ao2_find(sorcery->types, type, OBJ_KEY);
1566         if (!object_type) {
1567                 return;
1568         }
1569
1570         ao2_callback(object_type->observers, OBJ_NODATA | OBJ_UNLINK,
1571                 sorcery_observer_remove, cbs);
1572 }