Fix stuck channel in ARI through the introduction of synchronous bridge actions.
[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/format.h"
38 #include "asterisk/format_cap.h"
39 #include "asterisk/strings.h"
40 #include "asterisk/config_options.h"
41 #include "asterisk/netsock2.h"
42 #include "asterisk/module.h"
43 #include "asterisk/taskprocessor.h"
44 #include "asterisk/threadpool.h"
45 #include "asterisk/json.h"
46 #include "asterisk/format_pref.h"
47
48 /* To prevent DEBUG_FD_LEAKS from interfering with things we undef open and close */
49 #undef open
50 #undef close
51
52 /*! \brief Number of buckets for wizards (should be prime for performance reasons) */
53 #define WIZARD_BUCKETS 7
54
55 /*! \brief Number of buckets for types (should be prime for performance reasons) */
56 #define TYPE_BUCKETS 53
57
58 /*! \brief Number of buckets for instances (should be prime for performance reasons) */
59 #define INSTANCE_BUCKETS 17
60
61 /*! \brief Number of buckets for object fields (should be prime for performance reasons) */
62 #define OBJECT_FIELD_BUCKETS 29
63
64 /*! \brief Thread pool for observers */
65 static struct ast_threadpool *threadpool;
66
67 /*! \brief Structure for internal sorcery object information */
68 struct ast_sorcery_object {
69         /*! \brief Unique identifier of this object */
70         char *id;
71
72         /*! \brief Type of object */
73         char type[MAX_OBJECT_TYPE];
74
75         /*! \brief Optional object destructor */
76         ao2_destructor_fn destructor;
77
78         /*! \brief Extended object fields */
79         struct ast_variable *extended;
80 };
81
82 /*! \brief Structure for registered object type */
83 struct ast_sorcery_object_type {
84         /*! \brief Unique name of the object type */
85         char name[MAX_OBJECT_TYPE];
86
87         /*! \brief Optional transformation callback */
88         sorcery_transform_handler transform;
89
90         /*! \brief Optional object set apply callback */
91         sorcery_apply_handler apply;
92
93         /*! \brief Optional object copy callback */
94         sorcery_copy_handler copy;
95
96         /*! \brief Optional object diff callback */
97         sorcery_diff_handler diff;
98
99         /*! \brief Wizard instances */
100         struct ao2_container *wizards;
101
102         /*! \brief Object fields */
103         struct ao2_container *fields;
104
105         /*! \brief Configuration framework general information */
106         struct aco_info *info;
107
108         /*! \brief Configuration framework file information */
109         struct aco_file *file;
110
111         /*! \brief Type details */
112         struct aco_type type;
113
114         /*! \brief Observers */
115         struct ao2_container *observers;
116
117         /*! \brief Serializer for observers */
118         struct ast_taskprocessor *serializer;
119
120         /*! \brief Specifies if object type is reloadable or not */
121         unsigned int reloadable:1;
122 };
123
124 /*! \brief Structure for registered object type observer */
125 struct ast_sorcery_object_type_observer {
126         /*! \brief Pointer to the observer implementation */
127         const struct ast_sorcery_observer *callbacks;
128 };
129
130 /*! \brief Structure used for observer invocations */
131 struct sorcery_observer_invocation {
132         /*! \brief Pointer to the object type */
133         struct ast_sorcery_object_type *object_type;
134
135         /*! \brief Pointer to the object */
136         void *object;
137 };
138
139 /*! \brief Structure for registered object field */
140 struct ast_sorcery_object_field {
141         /*! \brief Name of the field */
142         char name[MAX_OBJECT_FIELD];
143
144         /*! \brief Callback function for translation of a single value */
145         sorcery_field_handler handler;
146
147         /*! \brief Callback function for translation of multiple values */
148         sorcery_fields_handler multiple_handler;
149
150         /*! \brief Position of the field */
151         intptr_t args[];
152 };
153
154 /*! \brief Structure for a wizard instance which operates on objects */
155 struct ast_sorcery_object_wizard {
156         /*! \brief Wizard interface itself */
157         struct ast_sorcery_wizard *wizard;
158
159         /*! \brief Unique data for the wizard */
160         void *data;
161
162         /*! \brief Wizard is acting as an object cache */
163         unsigned int caching:1;
164 };
165
166 /*! \brief Full structure for sorcery */
167 struct ast_sorcery {
168         /*! \brief Container for known object types */
169         struct ao2_container *types;
170         /*! \brief The name of the module owning this sorcery instance */
171         char module_name[0];
172 };
173
174 /*! \brief Structure for passing load/reload details */
175 struct sorcery_load_details {
176         /*! \brief Sorcery structure in use */
177         const struct ast_sorcery *sorcery;
178
179         /*! \brief Type of object being loaded */
180         const char *type;
181
182         /*! \brief Whether this is a reload or not */
183         unsigned int reload:1;
184 };
185
186 /*! \brief Registered sorcery wizards */
187 static struct ao2_container *wizards;
188
189 /*! \brief Registered sorcery instances */
190 static struct ao2_container *instances;
191
192 static int int_handler_fn(const void *obj, const intptr_t *args, char **buf)
193 {
194         int *field = (int *)(obj + args[0]);
195         return (ast_asprintf(buf, "%d", *field) < 0) ? -1 : 0;
196 }
197
198 static int uint_handler_fn(const void *obj, const intptr_t *args, char **buf)
199 {
200         unsigned int *field = (unsigned int *)(obj + args[0]);
201         return (ast_asprintf(buf, "%u", *field) < 0) ? -1 : 0;
202 }
203
204 static int double_handler_fn(const void *obj, const intptr_t *args, char **buf)
205 {
206         double *field = (double *)(obj + args[0]);
207         return (ast_asprintf(buf, "%f", *field) < 0) ? -1 : 0;
208 }
209
210 static int stringfield_handler_fn(const void *obj, const intptr_t *args, char **buf)
211 {
212         ast_string_field *field = (const char **)(obj + args[0]);
213         return !(*buf = ast_strdup(*field)) ? -1 : 0;
214 }
215
216 static int bool_handler_fn(const void *obj, const intptr_t *args, char **buf)
217 {
218         unsigned int *field = (unsigned int *)(obj + args[0]);
219         return !(*buf = ast_strdup(*field ? "true" : "false")) ? -1 : 0;
220 }
221
222 static int sockaddr_handler_fn(const void *obj, const intptr_t *args, char **buf)
223 {
224         struct ast_sockaddr *field = (struct ast_sockaddr *)(obj + args[0]);
225         return !(*buf = ast_strdup(ast_sockaddr_stringify(field))) ? -1 : 0;
226 }
227
228 static int chararray_handler_fn(const void *obj, const intptr_t *args, char **buf)
229 {
230         char *field = (char *)(obj + args[0]);
231         return !(*buf = ast_strdup(field)) ? -1 : 0;
232 }
233
234 static int codec_handler_fn(const void *obj, const intptr_t *args, char **buf)
235 {
236         char tmp_buf[256];
237         struct ast_codec_pref *pref = (struct ast_codec_pref *)(obj + args[0]);
238         ast_codec_pref_string(pref, tmp_buf, sizeof(tmp_buf));
239         return !(*buf = ast_strdup(tmp_buf));
240 }
241
242 static sorcery_field_handler sorcery_field_default_handler(enum aco_option_type type)
243 {
244         switch(type) {
245         case OPT_BOOL_T: return bool_handler_fn;
246         case OPT_CHAR_ARRAY_T: return chararray_handler_fn;
247         case OPT_CODEC_T: return codec_handler_fn;
248         case OPT_DOUBLE_T: return double_handler_fn;
249         case OPT_INT_T: return int_handler_fn;
250         case OPT_SOCKADDR_T: return sockaddr_handler_fn;
251         case OPT_STRINGFIELD_T: return stringfield_handler_fn;
252         case OPT_UINT_T: return uint_handler_fn;
253
254         default:
255         case OPT_CUSTOM_T: return NULL;
256         }
257
258         return NULL;
259 }
260
261 /*! \brief Hashing function for sorcery wizards */
262 static int sorcery_wizard_hash(const void *obj, const int flags)
263 {
264         const struct ast_sorcery_wizard *object;
265         const char *key;
266
267         switch (flags & OBJ_SEARCH_MASK) {
268         case OBJ_SEARCH_KEY:
269                 key = obj;
270                 break;
271         case OBJ_SEARCH_OBJECT:
272                 object = obj;
273                 key = object->name;
274                 break;
275         default:
276                 ast_assert(0);
277                 return 0;
278         }
279         return ast_str_hash(key);
280 }
281
282 /*! \brief Comparator function for sorcery wizards */
283 static int sorcery_wizard_cmp(void *obj, void *arg, int flags)
284 {
285         const struct ast_sorcery_wizard *object_left = obj;
286         const struct ast_sorcery_wizard *object_right = arg;
287         const char *right_key = arg;
288         int cmp;
289
290         switch (flags & OBJ_SEARCH_MASK) {
291         case OBJ_SEARCH_OBJECT:
292                 right_key = object_right->name;
293                 /* Fall through */
294         case OBJ_SEARCH_KEY:
295                 cmp = strcmp(object_left->name, right_key);
296                 break;
297         case OBJ_SEARCH_PARTIAL_KEY:
298                 cmp = strncmp(object_left->name, right_key, strlen(right_key));
299                 break;
300         default:
301                 cmp = 0;
302                 break;
303         }
304         if (cmp) {
305                 return 0;
306         }
307         return CMP_MATCH;
308 }
309
310 /*! \brief Hashing function for sorcery wizards */
311 static int object_type_field_hash(const void *obj, const int flags)
312 {
313         const struct ast_sorcery_object_field *object_field;
314         const char *key;
315
316         switch (flags & OBJ_SEARCH_MASK) {
317         case OBJ_SEARCH_KEY:
318                 key = obj;
319                 break;
320         case OBJ_SEARCH_OBJECT:
321                 object_field = obj;
322                 key = object_field->name;
323                 break;
324         default:
325                 ast_assert(0);
326                 return 0;
327         }
328         return ast_str_hash(key);
329 }
330
331 static int object_type_field_cmp(void *obj, void *arg, int flags)
332 {
333         const struct ast_sorcery_object_field *field_left = obj;
334         const struct ast_sorcery_object_field *field_right = arg;
335         const char *right_key = arg;
336         int cmp;
337
338         switch (flags & OBJ_SEARCH_MASK) {
339         case OBJ_SEARCH_OBJECT:
340                 right_key = field_right->name;
341                 /* Fall through */
342         case OBJ_SEARCH_KEY:
343                 cmp = strcmp(field_left->name, right_key);
344                 break;
345         case OBJ_SEARCH_PARTIAL_KEY:
346                 cmp = strncmp(field_left->name, right_key, strlen(right_key));
347                 break;
348         default:
349                 cmp = 0;
350                 break;
351         }
352         if (cmp) {
353                 return 0;
354         }
355         return CMP_MATCH;
356 }
357
358 /*! \brief Cleanup function */
359 static void sorcery_exit(void)
360 {
361         ast_threadpool_shutdown(threadpool);
362         threadpool = NULL;
363 }
364
365 /*! \brief Cleanup function for graceful shutdowns */
366 static void sorcery_cleanup(void)
367 {
368         ao2_cleanup(wizards);
369         wizards = NULL;
370         ao2_cleanup(instances);
371         instances = NULL;
372 }
373
374 /*! \brief Compare function for sorcery instances */
375 static int sorcery_instance_cmp(void *obj, void *arg, int flags)
376 {
377         const struct ast_sorcery *object_left = obj;
378         const struct ast_sorcery *object_right = arg;
379         const char *right_key = arg;
380         int cmp;
381
382         switch (flags & OBJ_SEARCH_MASK) {
383         case OBJ_SEARCH_OBJECT:
384                 right_key = object_right->module_name;
385                 /* Fall through */
386         case OBJ_SEARCH_KEY:
387                 cmp = strcmp(object_left->module_name, right_key);
388                 break;
389         case OBJ_SEARCH_PARTIAL_KEY:
390                 cmp = strncmp(object_left->module_name, right_key, strlen(right_key));
391                 break;
392         default:
393                 cmp = 0;
394                 break;
395         }
396         if (cmp) {
397                 return 0;
398         }
399         return CMP_MATCH;
400 }
401
402 /*! \brief Hashing function for sorcery instances */
403 static int sorcery_instance_hash(const void *obj, const int flags)
404 {
405         const struct ast_sorcery *object;
406         const char *key;
407
408         switch (flags & OBJ_SEARCH_MASK) {
409         case OBJ_SEARCH_KEY:
410                 key = obj;
411                 break;
412         case OBJ_SEARCH_OBJECT:
413                 object = obj;
414                 key = object->module_name;
415                 break;
416         default:
417                 ast_assert(0);
418                 return 0;
419         }
420         return ast_str_hash(key);
421 }
422
423 int ast_sorcery_init(void)
424 {
425         struct ast_threadpool_options options = {
426                 .version = AST_THREADPOOL_OPTIONS_VERSION,
427                 .auto_increment = 1,
428                 .max_size = 0,
429                 .idle_timeout = 60,
430                 .initial_size = 0,
431         };
432         ast_assert(wizards == NULL);
433
434         if (!(threadpool = ast_threadpool_create("Sorcery", NULL, &options))) {
435                 threadpool = NULL;
436                 return -1;
437         }
438
439         if (!(wizards = ao2_container_alloc(WIZARD_BUCKETS, sorcery_wizard_hash, sorcery_wizard_cmp))) {
440                 ast_threadpool_shutdown(threadpool);
441                 return -1;
442         }
443
444         instances = ao2_container_alloc_options(AO2_ALLOC_OPT_LOCK_RWLOCK, INSTANCE_BUCKETS,
445                 sorcery_instance_hash, sorcery_instance_cmp);
446         if (!instances) {
447                 sorcery_cleanup();
448                 sorcery_exit();
449                 return -1;
450         }
451
452         ast_register_cleanup(sorcery_cleanup);
453         ast_register_atexit(sorcery_exit);
454
455         return 0;
456 }
457
458 int __ast_sorcery_wizard_register(const struct ast_sorcery_wizard *interface, struct ast_module *module)
459 {
460         struct ast_sorcery_wizard *wizard;
461         int res = -1;
462
463         ast_assert(!ast_strlen_zero(interface->name));
464
465         ao2_lock(wizards);
466
467         if ((wizard = ao2_find(wizards, interface->name, OBJ_KEY | OBJ_NOLOCK))) {
468                 ast_log(LOG_WARNING, "Attempted to register sorcery wizard '%s' twice\n",
469                         interface->name);
470                 goto done;
471         }
472
473         if (!(wizard = ao2_alloc(sizeof(*wizard), NULL))) {
474                 goto done;
475         }
476
477         *wizard = *interface;
478         wizard->module = module;
479
480         ao2_link_flags(wizards, wizard, OBJ_NOLOCK);
481         res = 0;
482
483         ast_verb(2, "Sorcery registered wizard '%s'\n", interface->name);
484
485 done:
486         ao2_cleanup(wizard);
487         ao2_unlock(wizards);
488
489         return res;
490 }
491
492 int ast_sorcery_wizard_unregister(const struct ast_sorcery_wizard *interface)
493 {
494         RAII_VAR(struct ast_sorcery_wizard *, wizard, ao2_find(wizards, interface->name, OBJ_KEY | OBJ_UNLINK), ao2_cleanup);
495
496         if (wizard) {
497                 ast_verb(2, "Sorcery unregistered wizard '%s'\n", interface->name);
498                 return 0;
499         } else {
500                 return -1;
501         }
502 }
503
504 /*! \brief Destructor called when sorcery structure is destroyed */
505 static void sorcery_destructor(void *obj)
506 {
507         struct ast_sorcery *sorcery = obj;
508
509         ao2_cleanup(sorcery->types);
510 }
511
512 /*! \brief Hashing function for sorcery types */
513 static int sorcery_type_hash(const void *obj, const int flags)
514 {
515         const struct ast_sorcery_object_type *object;
516         const char *key;
517
518         switch (flags & OBJ_SEARCH_MASK) {
519         case OBJ_SEARCH_KEY:
520                 key = obj;
521                 break;
522         case OBJ_SEARCH_OBJECT:
523                 object = obj;
524                 key = object->name;
525                 break;
526         default:
527                 ast_assert(0);
528                 return 0;
529         }
530         return ast_str_hash(key);
531 }
532
533 /*! \brief Comparator function for sorcery types */
534 static int sorcery_type_cmp(void *obj, void *arg, int flags)
535 {
536         const struct ast_sorcery_object_type *object_left = obj;
537         const struct ast_sorcery_object_type *object_right = arg;
538         const char *right_key = arg;
539         int cmp;
540
541         switch (flags & OBJ_SEARCH_MASK) {
542         case OBJ_SEARCH_OBJECT:
543                 right_key = object_right->name;
544                 /* Fall through */
545         case OBJ_SEARCH_KEY:
546                 cmp = strcmp(object_left->name, right_key);
547                 break;
548         case OBJ_SEARCH_PARTIAL_KEY:
549                 cmp = strncmp(object_left->name, right_key, strlen(right_key));
550                 break;
551         default:
552                 cmp = 0;
553                 break;
554         }
555         if (cmp) {
556                 return 0;
557         }
558         return CMP_MATCH;
559 }
560
561 struct ast_sorcery *__ast_sorcery_open(const char *module_name)
562 {
563         struct ast_sorcery *sorcery;
564
565         ast_assert(module_name != NULL);
566
567         ao2_wrlock(instances);
568         if ((sorcery = ao2_find(instances, module_name, OBJ_SEARCH_KEY | OBJ_NOLOCK))) {
569                 goto done;
570         }
571
572         if (!(sorcery = ao2_alloc(sizeof(*sorcery) + strlen(module_name) + 1, sorcery_destructor))) {
573                 goto done;
574         }
575
576         if (!(sorcery->types = ao2_container_alloc_options(AO2_ALLOC_OPT_LOCK_RWLOCK, TYPE_BUCKETS, sorcery_type_hash, sorcery_type_cmp))) {
577                 ao2_ref(sorcery, -1);
578                 sorcery = NULL;
579                 goto done;
580         }
581
582         strcpy(sorcery->module_name, module_name); /* Safe */
583
584         if (__ast_sorcery_apply_config(sorcery, module_name, module_name) == AST_SORCERY_APPLY_FAIL) {
585                 ast_log(LOG_ERROR, "Error attempting to apply configuration %s to sorcery.", module_name);
586                 ao2_cleanup(sorcery);
587                 sorcery = NULL;
588                 goto done;
589         }
590
591         ao2_link_flags(instances, sorcery, OBJ_NOLOCK);
592
593 done:
594         ao2_unlock(instances);
595         return sorcery;
596 }
597
598 /*! \brief Search function for sorcery instances */
599 struct ast_sorcery *ast_sorcery_retrieve_by_module_name(const char *module_name)
600 {
601         return ao2_find(instances, module_name, OBJ_SEARCH_KEY);
602 }
603
604 /*! \brief Destructor function for object types */
605 static void sorcery_object_type_destructor(void *obj)
606 {
607         struct ast_sorcery_object_type *object_type = obj;
608
609         ao2_cleanup(object_type->wizards);
610         ao2_cleanup(object_type->fields);
611         ao2_cleanup(object_type->observers);
612
613         if (object_type->info) {
614                 aco_info_destroy(object_type->info);
615                 ast_free(object_type->info);
616         }
617
618         ast_free(object_type->file);
619
620         ast_taskprocessor_unreference(object_type->serializer);
621 }
622
623 /*! \brief Internal function which allocates an object type structure */
624 static struct ast_sorcery_object_type *sorcery_object_type_alloc(const char *type, const char *module)
625 {
626         struct ast_sorcery_object_type *object_type;
627         char uuid[AST_UUID_STR_LEN];
628
629         if (!(object_type = ao2_alloc(sizeof(*object_type), sorcery_object_type_destructor))) {
630                 return NULL;
631         }
632
633         /* Order matters for object wizards */
634         if (!(object_type->wizards = ao2_container_alloc_options(AO2_ALLOC_OPT_LOCK_NOLOCK, 1, NULL, sorcery_wizard_cmp))) {
635                 ao2_ref(object_type, -1);
636                 return NULL;
637         }
638
639         if (!(object_type->fields = ao2_container_alloc_options(AO2_ALLOC_OPT_LOCK_NOLOCK, OBJECT_FIELD_BUCKETS,
640                                         object_type_field_hash, object_type_field_cmp))) {
641                 ao2_ref(object_type, -1);
642                 return NULL;
643         }
644
645         if (!(object_type->observers = ao2_container_alloc_options(AO2_ALLOC_OPT_LOCK_RWLOCK, 1, NULL, NULL))) {
646                 ao2_ref(object_type, -1);
647                 return NULL;
648         }
649
650         if (!(object_type->info = ast_calloc(1, sizeof(*object_type->info) + 2 * sizeof(object_type->info->files[0])))) {
651                 ao2_ref(object_type, -1);
652                 return NULL;
653         }
654
655         if (!(object_type->file = ast_calloc(1, sizeof(*object_type->file) + 2 * sizeof(object_type->file->types[0])))) {
656                 ao2_ref(object_type, -1);
657                 return NULL;
658         }
659
660         if (!ast_uuid_generate_str(uuid, sizeof(uuid))) {
661                 ao2_ref(object_type, -1);
662                 return NULL;
663         }
664
665         if (!(object_type->serializer = ast_threadpool_serializer(uuid, threadpool))) {
666                 ao2_ref(object_type, -1);
667                 return NULL;
668         }
669
670         object_type->info->files[0] = object_type->file;
671         object_type->info->files[1] = NULL;
672         object_type->info->module = module;
673
674         ast_copy_string(object_type->name, type, sizeof(object_type->name));
675
676         return object_type;
677 }
678
679 /*! \brief Object wizard destructor */
680 static void sorcery_object_wizard_destructor(void *obj)
681 {
682         struct ast_sorcery_object_wizard *object_wizard = obj;
683
684         if (object_wizard->data) {
685                 object_wizard->wizard->close(object_wizard->data);
686         }
687
688         if (object_wizard->wizard) {
689                 ast_module_unref(object_wizard->wizard->module);
690         }
691 }
692
693 /*! \brief Internal function which creates an object type and adds a wizard mapping */
694 static enum ast_sorcery_apply_result sorcery_apply_wizard_mapping(struct ast_sorcery *sorcery,
695                 const char *type, const char *module, const char *name, const char *data, unsigned int caching)
696 {
697         RAII_VAR(struct ast_sorcery_object_type *, object_type, ao2_find(sorcery->types, type, OBJ_KEY), ao2_cleanup);
698         RAII_VAR(struct ast_sorcery_wizard *, wizard, ao2_find(wizards, name, OBJ_KEY), ao2_cleanup);
699         RAII_VAR(struct ast_sorcery_object_wizard *, object_wizard, ao2_alloc(sizeof(*object_wizard), sorcery_object_wizard_destructor), ao2_cleanup);
700         int created = 0;
701
702         if (!wizard || !object_wizard) {
703                 return AST_SORCERY_APPLY_FAIL;
704         }
705
706         if (!object_type) {
707                 if (!(object_type = sorcery_object_type_alloc(type, module))) {
708                         return AST_SORCERY_APPLY_FAIL;
709                 }
710                 created = 1;
711         }
712
713         if (!created) {
714                 struct ast_sorcery_wizard *found;
715
716                 found = ao2_find(object_type->wizards, wizard, OBJ_SEARCH_OBJECT);
717                 if (found) {
718                         ast_debug(1, "Wizard %s already applied to object type %s\n",
719                                         wizard->name, object_type->name);
720                         ao2_cleanup(found);
721                         return AST_SORCERY_APPLY_DUPLICATE;
722                 }
723         }
724
725         if (wizard->open && !(object_wizard->data = wizard->open(data))) {
726                 return AST_SORCERY_APPLY_FAIL;
727         }
728
729         ast_module_ref(wizard->module);
730
731         object_wizard->wizard = wizard;
732         object_wizard->caching = caching;
733
734         ao2_link(object_type->wizards, object_wizard);
735
736         if (created) {
737                 ao2_link(sorcery->types, object_type);
738         }
739
740         return AST_SORCERY_APPLY_SUCCESS;
741 }
742
743 enum ast_sorcery_apply_result  __ast_sorcery_apply_config(struct ast_sorcery *sorcery, const char *name, const char *module)
744 {
745         struct ast_flags flags = { 0 };
746         struct ast_config *config = ast_config_load2("sorcery.conf", "sorcery", flags);
747         struct ast_variable *mapping;
748         int res = AST_SORCERY_APPLY_SUCCESS;
749
750         if (!config || config == CONFIG_STATUS_FILEINVALID) {
751                 return AST_SORCERY_APPLY_FAIL;
752         }
753
754         for (mapping = ast_variable_browse(config, name); mapping; mapping = mapping->next) {
755                 RAII_VAR(char *, mapping_name, ast_strdup(mapping->name), ast_free);
756                 RAII_VAR(char *, mapping_value, ast_strdup(mapping->value), ast_free);
757                 char *options = mapping_name;
758                 char *type = strsep(&options, "/");
759                 char *data = mapping_value;
760                 char *wizard = strsep(&data, ",");
761                 unsigned int caching = 0;
762
763                 /* If no object type or wizard exists just skip, nothing we can do */
764                 if (ast_strlen_zero(type) || ast_strlen_zero(wizard)) {
765                         continue;
766                 }
767
768                 /* If the wizard is configured as a cache treat it as such */
769                 if (!ast_strlen_zero(options) && strstr(options, "cache")) {
770                         caching = 1;
771                 }
772
773                 /* Any error immediately causes us to stop */
774                 if (sorcery_apply_wizard_mapping(sorcery, type, module, wizard, data, caching) == AST_SORCERY_APPLY_FAIL) {
775                         res = AST_SORCERY_APPLY_FAIL;
776                         break;
777                 }
778         }
779
780         ast_config_destroy(config);
781
782         return res;
783 }
784
785 enum ast_sorcery_apply_result __ast_sorcery_apply_default(struct ast_sorcery *sorcery, const char *type, const char *module, const char *name, const char *data)
786 {
787         RAII_VAR(struct ast_sorcery_object_type *, object_type, ao2_find(sorcery->types, type, OBJ_KEY), ao2_cleanup);
788
789         /* Defaults can not be added if any existing mapping exists */
790         if (object_type) {
791                 return AST_SORCERY_APPLY_DEFAULT_UNNECESSARY;
792         }
793
794         return sorcery_apply_wizard_mapping(sorcery, type, module, name, data, 0);
795 }
796
797 static int sorcery_extended_config_handler(const struct aco_option *opt, struct ast_variable *var, void *obj)
798 {
799         return ast_sorcery_object_set_extended(obj, var->name, var->value);
800 }
801
802 static int sorcery_extended_fields_handler(const void *obj, struct ast_variable **fields)
803 {
804         const struct ast_sorcery_object_details *details = obj;
805
806         if (details->object->extended) {
807                 *fields = ast_variables_dup(details->object->extended);
808         } else {
809                 *fields = NULL;
810         }
811
812         return 0;
813 }
814
815 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)
816 {
817         RAII_VAR(struct ast_sorcery_object_type *, object_type, ao2_find(sorcery->types, type, OBJ_KEY), ao2_cleanup);
818
819         if (!object_type || object_type->type.item_alloc) {
820                 return -1;
821         }
822
823         object_type->type.name = object_type->name;
824         object_type->type.type = ACO_ITEM;
825         object_type->type.category = ".?";
826         object_type->type.item_alloc = alloc;
827         object_type->type.hidden = hidden;
828
829         object_type->reloadable = reloadable;
830         object_type->transform = transform;
831         object_type->apply = apply;
832         object_type->file->types[0] = &object_type->type;
833         object_type->file->types[1] = NULL;
834
835         if (aco_info_init(object_type->info)) {
836                 return -1;
837         }
838
839         if (ast_sorcery_object_fields_register(sorcery, type, "^@", sorcery_extended_config_handler, sorcery_extended_fields_handler)) {
840                 return -1;
841         }
842
843         return 0;
844 }
845
846 void ast_sorcery_object_set_copy_handler(struct ast_sorcery *sorcery, const char *type, sorcery_copy_handler copy)
847 {
848         RAII_VAR(struct ast_sorcery_object_type *, object_type, ao2_find(sorcery->types, type, OBJ_KEY), ao2_cleanup);
849
850         if (!object_type) {
851                 return;
852         }
853
854         object_type->copy = copy;
855 }
856
857 void ast_sorcery_object_set_diff_handler(struct ast_sorcery *sorcery, const char *type, sorcery_diff_handler diff)
858 {
859         RAII_VAR(struct ast_sorcery_object_type *, object_type, ao2_find(sorcery->types, type, OBJ_KEY), ao2_cleanup);
860
861         if (!object_type) {
862                 return;
863         }
864
865         object_type->diff = diff;
866 }
867
868 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)
869 {
870         RAII_VAR(struct ast_sorcery_object_type *, object_type, ao2_find(sorcery->types, type, OBJ_KEY), ao2_cleanup);
871         RAII_VAR(struct ast_sorcery_object_field *, object_field, NULL, ao2_cleanup);
872
873         if (!object_type || !object_type->type.item_alloc || !config_handler || !(object_field = ao2_alloc(sizeof(*object_field), NULL))) {
874                 return -1;
875         }
876
877         ast_copy_string(object_field->name, regex, sizeof(object_field->name));
878         object_field->multiple_handler = sorcery_handler;
879
880         ao2_link(object_type->fields, object_field);
881         __aco_option_register(object_type->info, regex, ACO_REGEX, object_type->file->types, "", OPT_CUSTOM_T, config_handler, 0, 1, 0);
882
883         return 0;
884 }
885
886 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,
887                                         aco_option_handler config_handler, sorcery_field_handler sorcery_handler, sorcery_fields_handler multiple_handler, unsigned int flags, unsigned int no_doc, unsigned int alias, size_t argc, ...)
888 {
889         RAII_VAR(struct ast_sorcery_object_type *, object_type, ao2_find(sorcery->types, type, OBJ_KEY), ao2_cleanup);
890         RAII_VAR(struct ast_sorcery_object_field *, object_field, NULL, ao2_cleanup);
891         int pos;
892         va_list args;
893
894         if (!strcmp(type, "id") || !object_type || !object_type->type.item_alloc) {
895                 return -1;
896         }
897
898         if (!sorcery_handler) {
899                 sorcery_handler = sorcery_field_default_handler(opt_type);
900         }
901
902         if (!(object_field = ao2_alloc(sizeof(*object_field) + argc * sizeof(object_field->args[0]), NULL))) {
903                 return -1;
904         }
905
906         ast_copy_string(object_field->name, name, sizeof(object_field->name));
907         object_field->handler = sorcery_handler;
908         object_field->multiple_handler = multiple_handler;
909
910         va_start(args, argc);
911         for (pos = 0; pos < argc; pos++) {
912                 object_field->args[pos] = va_arg(args, size_t);
913         }
914         va_end(args);
915
916         if (!alias) {
917                 ao2_link(object_type->fields, object_field);
918         }
919
920         /* TODO: Improve this hack */
921         if (!argc) {
922                 __aco_option_register(object_type->info, name, ACO_EXACT, object_type->file->types, default_val, opt_type, config_handler, flags, no_doc, argc);
923         } else if (argc == 1) {
924                 __aco_option_register(object_type->info, name, ACO_EXACT, object_type->file->types, default_val, opt_type, config_handler, flags, no_doc, argc,
925                         object_field->args[0]);
926         } else if (argc == 2) {
927                 __aco_option_register(object_type->info, name, ACO_EXACT, object_type->file->types, default_val, opt_type, config_handler, flags, no_doc, argc,
928                         object_field->args[0], object_field->args[1]);
929         } else if (argc == 3) {
930                 __aco_option_register(object_type->info, name, ACO_EXACT, object_type->file->types, default_val, opt_type, config_handler, flags, no_doc, argc,
931                         object_field->args[0], object_field->args[1], object_field->args[2]);
932         } else {
933                 ast_assert(0); /* The hack... she does us no good for this */
934         }
935
936         return 0;
937 }
938
939 /*! \brief Retrieves whether or not the type is reloadable */
940 static int sorcery_reloadable(const struct ast_sorcery *sorcery, const char *type)
941 {
942         RAII_VAR(struct ast_sorcery_object_type *, object_type,
943                  ao2_find(sorcery->types, type, OBJ_KEY), ao2_cleanup);
944         return object_type && object_type->reloadable;
945 }
946
947 static int sorcery_wizard_load(void *obj, void *arg, int flags)
948 {
949         struct ast_sorcery_object_wizard *wizard = obj;
950         struct sorcery_load_details *details = arg;
951         void (*load)(void *data, const struct ast_sorcery *sorcery, const char *type);
952
953         if (details->reload && !sorcery_reloadable(details->sorcery, details->type)) {
954                 ast_log(LOG_NOTICE, "Type '%s' is not reloadable, "
955                         "maintaining previous values\n", details->type);
956                 return 0;
957         }
958
959         load = !details->reload ? wizard->wizard->load : wizard->wizard->reload;
960
961         if (load) {
962                 load(wizard->data, details->sorcery, details->type);
963         }
964
965         return 0;
966 }
967
968 /*! \brief Destructor for observer invocation */
969 static void sorcery_observer_invocation_destroy(void *obj)
970 {
971         struct sorcery_observer_invocation *invocation = obj;
972
973         ao2_cleanup(invocation->object_type);
974         ao2_cleanup(invocation->object);
975 }
976
977 /*! \brief Allocator function for observer invocation */
978 static struct sorcery_observer_invocation *sorcery_observer_invocation_alloc(struct ast_sorcery_object_type *object_type, void *object)
979 {
980         struct sorcery_observer_invocation *invocation = ao2_alloc(sizeof(*invocation), sorcery_observer_invocation_destroy);
981
982         if (!invocation) {
983                 return NULL;
984         }
985
986         ao2_ref(object_type, +1);
987         invocation->object_type = object_type;
988
989         if (object) {
990                 ao2_ref(object, +1);
991                 invocation->object = object;
992         }
993
994         return invocation;
995 }
996
997 /*! \brief Internal callback function which notifies an individual observer that an object type has been loaded */
998 static int sorcery_observer_notify_loaded(void *obj, void *arg, int flags)
999 {
1000         const struct ast_sorcery_object_type_observer *observer = obj;
1001
1002         if (observer->callbacks->loaded) {
1003                 observer->callbacks->loaded(arg);
1004         }
1005
1006         return 0;
1007 }
1008
1009 /*! \brief Internal callback function which notifies observers that an object type has been loaded */
1010 static int sorcery_observers_notify_loaded(void *data)
1011 {
1012         struct sorcery_observer_invocation *invocation = data;
1013
1014         ao2_callback(invocation->object_type->observers, OBJ_NODATA, sorcery_observer_notify_loaded, invocation->object_type->name);
1015         ao2_cleanup(invocation);
1016
1017         return 0;
1018 }
1019
1020 static int sorcery_object_load(void *obj, void *arg, int flags)
1021 {
1022         struct ast_sorcery_object_type *type = obj;
1023         struct sorcery_load_details *details = arg;
1024
1025         details->type = type->name;
1026         ao2_callback(type->wizards, OBJ_NODATA, sorcery_wizard_load, details);
1027
1028         if (ao2_container_count(type->observers)) {
1029                 struct sorcery_observer_invocation *invocation = sorcery_observer_invocation_alloc(type, NULL);
1030
1031                 if (invocation && ast_taskprocessor_push(type->serializer, sorcery_observers_notify_loaded, invocation)) {
1032                         ao2_cleanup(invocation);
1033                 }
1034         }
1035
1036         return 0;
1037 }
1038
1039 void ast_sorcery_load(const struct ast_sorcery *sorcery)
1040 {
1041         struct sorcery_load_details details = {
1042                 .sorcery = sorcery,
1043                 .reload = 0,
1044         };
1045
1046         ao2_callback(sorcery->types, OBJ_NODATA, sorcery_object_load, &details);
1047 }
1048
1049 void ast_sorcery_load_object(const struct ast_sorcery *sorcery, const char *type)
1050 {
1051         RAII_VAR(struct ast_sorcery_object_type *, object_type, ao2_find(sorcery->types, type, OBJ_KEY), ao2_cleanup);
1052         struct sorcery_load_details details = {
1053                 .sorcery = sorcery,
1054                 .reload = 0,
1055         };
1056
1057         if (!object_type) {
1058                 return;
1059         }
1060
1061         sorcery_object_load(object_type, &details, 0);
1062 }
1063
1064 void ast_sorcery_reload(const struct ast_sorcery *sorcery)
1065 {
1066         struct sorcery_load_details details = {
1067                 .sorcery = sorcery,
1068                 .reload = 1,
1069         };
1070
1071         ao2_callback(sorcery->types, OBJ_NODATA, sorcery_object_load, &details);
1072 }
1073
1074 void ast_sorcery_reload_object(const struct ast_sorcery *sorcery, const char *type)
1075 {
1076         RAII_VAR(struct ast_sorcery_object_type *, object_type, ao2_find(sorcery->types, type, OBJ_KEY), ao2_cleanup);
1077         struct sorcery_load_details details = {
1078                 .sorcery = sorcery,
1079                 .reload = 1,
1080         };
1081
1082         if (!object_type) {
1083                 return;
1084         }
1085
1086         sorcery_object_load(object_type, &details, 0);
1087 }
1088
1089 void ast_sorcery_ref(struct ast_sorcery *sorcery)
1090 {
1091         ao2_ref(sorcery, +1);
1092 }
1093
1094 static struct ast_variable *get_single_field_as_var_list(const void *object, struct ast_sorcery_object_field *object_field)
1095 {
1096         struct ast_variable *tmp = NULL;
1097         char *buf = NULL;
1098
1099         if (!object_field->handler) {
1100                 return NULL;
1101         }
1102
1103         if (!(object_field->handler(object, object_field->args, &buf))) {
1104                 tmp = ast_variable_new(object_field->name, S_OR(buf, ""), "");
1105         }
1106         ast_free(buf);
1107
1108         return tmp;
1109 }
1110
1111 static struct ast_variable *get_multiple_fields_as_var_list(const void *object, struct ast_sorcery_object_field *object_field)
1112 {
1113         struct ast_variable *tmp = NULL;
1114
1115         if (!object_field->multiple_handler) {
1116                 return NULL;
1117         }
1118
1119         if (object_field->multiple_handler(object, &tmp)) {
1120                 ast_variables_destroy(tmp);
1121                 tmp = NULL;
1122         }
1123
1124         return tmp;
1125 }
1126
1127 struct ast_variable *ast_sorcery_objectset_create2(const struct ast_sorcery *sorcery,
1128         const void *object,     enum ast_sorcery_field_handler_flags flags)
1129 {
1130         const struct ast_sorcery_object_details *details = object;
1131         RAII_VAR(struct ast_sorcery_object_type *, object_type, ao2_find(sorcery->types, details->object->type, OBJ_KEY), ao2_cleanup);
1132         struct ao2_iterator i;
1133         struct ast_sorcery_object_field *object_field;
1134         struct ast_variable *head = NULL;
1135         struct ast_variable *tail = NULL;
1136
1137         if (!object_type) {
1138                 return NULL;
1139         }
1140
1141         i = ao2_iterator_init(object_type->fields, 0);
1142
1143         for (; (object_field = ao2_iterator_next(&i)); ao2_ref(object_field, -1)) {
1144                 struct ast_variable *tmp;
1145
1146                 switch (flags) {
1147                 case AST_HANDLER_PREFER_LIST:
1148                         if ((tmp = get_multiple_fields_as_var_list(object, object_field)) ||
1149                                 (tmp = get_single_field_as_var_list(object, object_field))) {
1150                                 break;
1151                         }
1152                         continue;
1153                 case AST_HANDLER_PREFER_STRING:
1154                         if ((tmp = get_single_field_as_var_list(object, object_field)) ||
1155                                 (tmp = get_multiple_fields_as_var_list(object, object_field))) {
1156                                 break;
1157                         }
1158                         continue;
1159                 case AST_HANDLER_ONLY_LIST:
1160                         if ((tmp = get_multiple_fields_as_var_list(object, object_field))) {
1161                                 break;
1162                         }
1163                         continue;
1164                 case AST_HANDLER_ONLY_STRING:
1165                         if ((tmp = get_single_field_as_var_list(object, object_field))) {
1166                                 break;
1167                         }
1168                         continue;
1169                 default:
1170                         continue;
1171                 }
1172
1173                 tail = ast_variable_list_append_hint(&head, tail, tmp);
1174         }
1175
1176         ao2_iterator_destroy(&i);
1177
1178         return head;
1179 }
1180
1181 struct ast_json *ast_sorcery_objectset_json_create(const struct ast_sorcery *sorcery, const void *object)
1182 {
1183         const struct ast_sorcery_object_details *details = object;
1184         RAII_VAR(struct ast_sorcery_object_type *, object_type, ao2_find(sorcery->types, details->object->type, OBJ_KEY), ao2_cleanup);
1185         struct ao2_iterator i;
1186         struct ast_sorcery_object_field *object_field;
1187         struct ast_json *json = ast_json_object_create();
1188         int res = 0;
1189
1190         if (!object_type || !json) {
1191                 return NULL;
1192         }
1193
1194         i = ao2_iterator_init(object_type->fields, 0);
1195
1196         for (; !res && (object_field = ao2_iterator_next(&i)); ao2_ref(object_field, -1)) {
1197                 if (object_field->multiple_handler) {
1198                         struct ast_variable *tmp = NULL;
1199                         struct ast_variable *field;
1200
1201                         if ((res = object_field->multiple_handler(object, &tmp))) {
1202                                 ast_variables_destroy(tmp);
1203                                 ao2_ref(object_field, -1);
1204                                 break;
1205                         }
1206
1207                         for (field = tmp; field; field = field->next) {
1208                                 struct ast_json *value = ast_json_string_create(field->value);
1209
1210                                 if (!value || ast_json_object_set(json, field->name, value)) {
1211                                         res = -1;
1212                                         break;
1213                                 }
1214                         }
1215
1216                         ast_variables_destroy(tmp);
1217                 } else if (object_field->handler) {
1218                         char *buf = NULL;
1219                         struct ast_json *value = NULL;
1220
1221                         if ((res = object_field->handler(object, object_field->args, &buf))
1222                                 || !(value = ast_json_string_create(buf))
1223                                 || ast_json_object_set(json, object_field->name, value)) {
1224                                 res = -1;
1225                         }
1226
1227                         ast_free(buf);
1228                 } else {
1229                         continue;
1230                 }
1231         }
1232
1233         ao2_iterator_destroy(&i);
1234
1235         /* If any error occurs we destroy the JSON object so a partial objectset is not returned */
1236         if (res) {
1237                 ast_json_unref(json);
1238                 json = NULL;
1239         }
1240
1241         return json;
1242 }
1243
1244 int ast_sorcery_objectset_apply(const struct ast_sorcery *sorcery, void *object, struct ast_variable *objectset)
1245 {
1246         const struct ast_sorcery_object_details *details = object;
1247         RAII_VAR(struct ast_sorcery_object_type *, object_type, ao2_find(sorcery->types, details->object->type, OBJ_KEY), ao2_cleanup);
1248         RAII_VAR(struct ast_variable *, transformed, NULL, ast_variables_destroy);
1249         struct ast_variable *field;
1250         int res = 0;
1251
1252         if (!object_type) {
1253                 return -1;
1254         }
1255
1256         if (object_type->transform && (transformed = object_type->transform(objectset))) {
1257                 field = transformed;
1258         } else {
1259                 field = objectset;
1260         }
1261
1262         for (; field; field = field->next) {
1263                 if ((res = aco_process_var(&object_type->type, details->object->id, field, object))) {
1264                         break;
1265                 }
1266         }
1267
1268         if (!res && object_type->apply) {
1269                 res = object_type->apply(sorcery, object);
1270         }
1271
1272         return res;
1273 }
1274
1275 static const struct ast_variable *sorcery_find_field(const struct ast_variable *fields, const char *name)
1276 {
1277         const struct ast_variable *field;
1278
1279         /* Search the linked list of fields to find the correct one */
1280         for (field = fields; field; field = field->next) {
1281                 if (!strcmp(field->name, name)) {
1282                         return field;
1283                 }
1284         }
1285
1286         return NULL;
1287 }
1288
1289 int ast_sorcery_changeset_create(const struct ast_variable *original, const struct ast_variable *modified, struct ast_variable **changes)
1290 {
1291         const struct ast_variable *field;
1292         int res = 0;
1293
1294         *changes = NULL;
1295
1296         /* Unless the ast_variable list changes when examined... it can't differ from itself */
1297         if (original == modified) {
1298                 return 0;
1299         }
1300
1301         for (field = modified; field; field = field->next) {
1302                 const struct ast_variable *old = sorcery_find_field(original, field->name);
1303
1304                 if (!old || strcmp(old->value, field->value)) {
1305                         struct ast_variable *tmp;
1306
1307                         if (!(tmp = ast_variable_new(field->name, field->value, ""))) {
1308                                 res = -1;
1309                                 break;
1310                         }
1311
1312                         tmp->next = *changes;
1313                         *changes = tmp;
1314                 }
1315         }
1316
1317         /* If an error occurred do not return a partial changeset */
1318         if (res) {
1319                 ast_variables_destroy(*changes);
1320                 *changes = NULL;
1321         }
1322
1323         return res;
1324 }
1325
1326 static void sorcery_object_destructor(void *object)
1327 {
1328         struct ast_sorcery_object_details *details = object;
1329
1330         if (details->object->destructor) {
1331                 details->object->destructor(object);
1332         }
1333
1334         ast_variables_destroy(details->object->extended);
1335         ast_free(details->object->id);
1336 }
1337
1338 void *ast_sorcery_generic_alloc(size_t size, ao2_destructor_fn destructor)
1339 {
1340         void *object = ao2_alloc_options(size + sizeof(struct ast_sorcery_object), sorcery_object_destructor, AO2_ALLOC_OPT_LOCK_NOLOCK);
1341         struct ast_sorcery_object_details *details = object;
1342
1343         if (!object) {
1344                 return NULL;
1345         }
1346
1347         details->object = object + size;
1348         details->object->destructor = destructor;
1349
1350         return object;
1351 }
1352
1353 void *ast_sorcery_alloc(const struct ast_sorcery *sorcery, const char *type, const char *id)
1354 {
1355         RAII_VAR(struct ast_sorcery_object_type *, object_type, ao2_find(sorcery->types, type, OBJ_KEY), ao2_cleanup);
1356         struct ast_sorcery_object_details *details;
1357
1358         if (!object_type || !object_type->type.item_alloc ||
1359                 !(details = object_type->type.item_alloc(id))) {
1360                 return NULL;
1361         }
1362
1363         if (ast_strlen_zero(id)) {
1364                 char uuid[AST_UUID_STR_LEN];
1365
1366                 ast_uuid_generate_str(uuid, sizeof(uuid));
1367                 details->object->id = ast_strdup(uuid);
1368         } else {
1369                 details->object->id = ast_strdup(id);
1370         }
1371
1372         ast_copy_string(details->object->type, type, sizeof(details->object->type));
1373
1374         if (aco_set_defaults(&object_type->type, id, details)) {
1375                 ao2_ref(details, -1);
1376                 return NULL;
1377         }
1378
1379         return details;
1380 }
1381
1382 void *ast_sorcery_copy(const struct ast_sorcery *sorcery, const void *object)
1383 {
1384         const struct ast_sorcery_object_details *details = object;
1385         RAII_VAR(struct ast_sorcery_object_type *, object_type, ao2_find(sorcery->types, details->object->type, OBJ_KEY), ao2_cleanup);
1386         struct ast_sorcery_object_details *copy = ast_sorcery_alloc(sorcery, details->object->type, details->object->id);
1387         RAII_VAR(struct ast_variable *, objectset, NULL, ast_variables_destroy);
1388         int res = 0;
1389
1390         if (!copy) {
1391                 return NULL;
1392         } else if (object_type->copy) {
1393                 res = object_type->copy(object, copy);
1394         } else if ((objectset = ast_sorcery_objectset_create(sorcery, object))) {
1395                 res = ast_sorcery_objectset_apply(sorcery, copy, objectset);
1396         } else {
1397                 /* No native copy available and could not create an objectset, this copy has failed */
1398                 res = -1;
1399         }
1400
1401         if (res) {
1402                 ao2_cleanup(copy);
1403                 copy = NULL;
1404         }
1405
1406         return copy;
1407 }
1408
1409 int ast_sorcery_diff(const struct ast_sorcery *sorcery, const void *original, const void *modified, struct ast_variable **changes)
1410 {
1411         RAII_VAR(struct ast_sorcery_object_type *, object_type, ao2_find(sorcery->types, ast_sorcery_object_get_type(original), OBJ_KEY), ao2_cleanup);
1412
1413         *changes = NULL;
1414
1415         if (strcmp(ast_sorcery_object_get_type(original), ast_sorcery_object_get_type(modified))) {
1416                 return -1;
1417         }
1418
1419         if (original == modified) {
1420                 return 0;
1421         } else if (!object_type->diff) {
1422                 RAII_VAR(struct ast_variable *, objectset1, NULL, ast_variables_destroy);
1423                 RAII_VAR(struct ast_variable *, objectset2, NULL, ast_variables_destroy);
1424
1425                 objectset1 = ast_sorcery_objectset_create(sorcery, original);
1426                 objectset2 = ast_sorcery_objectset_create(sorcery, modified);
1427
1428                 return ast_sorcery_changeset_create(objectset1, objectset2, changes);
1429         } else {
1430                 return object_type->diff(original, modified, changes);
1431         }
1432 }
1433
1434 /*! \brief Structure used when calling create, update, or delete */
1435 struct sorcery_details {
1436         /*! \brief Pointer to the sorcery instance */
1437         const struct ast_sorcery *sorcery;
1438         /*! \brief Pointer to the object itself */
1439         void *obj;
1440 };
1441
1442 /*! \brief Internal function used to create an object in caching wizards */
1443 static int sorcery_cache_create(void *obj, void *arg, int flags)
1444 {
1445         const struct ast_sorcery_object_wizard *object_wizard = obj;
1446         const struct sorcery_details *details = arg;
1447
1448         if (!object_wizard->caching || !object_wizard->wizard->create) {
1449                 return 0;
1450         }
1451
1452         object_wizard->wizard->create(details->sorcery, object_wizard->data, details->obj);
1453
1454         return 0;
1455 }
1456
1457 void *ast_sorcery_retrieve_by_id(const struct ast_sorcery *sorcery, const char *type, const char *id)
1458 {
1459         RAII_VAR(struct ast_sorcery_object_type *, object_type, ao2_find(sorcery->types, type, OBJ_KEY), ao2_cleanup);
1460         void *object = NULL;
1461         struct ao2_iterator i;
1462         struct ast_sorcery_object_wizard *wizard;
1463         unsigned int cached = 0;
1464
1465         if (!object_type || ast_strlen_zero(id)) {
1466                 return NULL;
1467         }
1468
1469         i = ao2_iterator_init(object_type->wizards, 0);
1470         for (; (wizard = ao2_iterator_next(&i)); ao2_ref(wizard, -1)) {
1471                 if (wizard->wizard->retrieve_id &&
1472                         !(object = wizard->wizard->retrieve_id(sorcery, wizard->data, object_type->name, id))) {
1473                         continue;
1474                 }
1475
1476                 cached = wizard->caching;
1477
1478                 ao2_ref(wizard, -1);
1479                 break;
1480         }
1481         ao2_iterator_destroy(&i);
1482
1483         if (!cached && object) {
1484                 ao2_callback(object_type->wizards, 0, sorcery_cache_create, object);
1485         }
1486
1487         return object;
1488 }
1489
1490 void *ast_sorcery_retrieve_by_fields(const struct ast_sorcery *sorcery, const char *type, unsigned int flags, struct ast_variable *fields)
1491 {
1492         RAII_VAR(struct ast_sorcery_object_type *, object_type, ao2_find(sorcery->types, type, OBJ_KEY), ao2_cleanup);
1493         void *object = NULL;
1494         struct ao2_iterator i;
1495         struct ast_sorcery_object_wizard *wizard;
1496         unsigned int cached = 0;
1497
1498         if (!object_type) {
1499                 return NULL;
1500         }
1501
1502         /* If returning multiple objects create a container to store them in */
1503         if ((flags & AST_RETRIEVE_FLAG_MULTIPLE)) {
1504                 if (!(object = ao2_container_alloc_options(AO2_ALLOC_OPT_LOCK_NOLOCK, 1, NULL, NULL))) {
1505                         return NULL;
1506                 }
1507         }
1508
1509         /* Inquire with the available wizards for retrieval */
1510         i = ao2_iterator_init(object_type->wizards, 0);
1511         for (; (wizard = ao2_iterator_next(&i)); ao2_ref(wizard, -1)) {
1512                 if ((flags & AST_RETRIEVE_FLAG_MULTIPLE)) {
1513                         if (wizard->wizard->retrieve_multiple) {
1514                                 wizard->wizard->retrieve_multiple(sorcery, wizard->data, object_type->name, object, fields);
1515                         }
1516                 } else if (fields && wizard->wizard->retrieve_fields) {
1517                         if (wizard->wizard->retrieve_fields) {
1518                                 object = wizard->wizard->retrieve_fields(sorcery, wizard->data, object_type->name, fields);
1519                         }
1520                 }
1521
1522                 if ((flags & AST_RETRIEVE_FLAG_MULTIPLE) || !object) {
1523                         continue;
1524                 }
1525
1526                 cached = wizard->caching;
1527
1528                 ao2_ref(wizard, -1);
1529                 break;
1530         }
1531         ao2_iterator_destroy(&i);
1532
1533         /* If we are returning a single object and it came from a non-cache source create it in any caches */
1534         if (!(flags & AST_RETRIEVE_FLAG_MULTIPLE) && !cached && object) {
1535                 ao2_callback(object_type->wizards, 0, sorcery_cache_create, object);
1536         }
1537
1538         return object;
1539 }
1540
1541 struct ao2_container *ast_sorcery_retrieve_by_regex(const struct ast_sorcery *sorcery, const char *type, const char *regex)
1542 {
1543         RAII_VAR(struct ast_sorcery_object_type *, object_type, ao2_find(sorcery->types, type, OBJ_KEY), ao2_cleanup);
1544         struct ao2_container *objects;
1545         struct ao2_iterator i;
1546         struct ast_sorcery_object_wizard *wizard;
1547
1548         if (!object_type || !(objects = ao2_container_alloc_options(AO2_ALLOC_OPT_LOCK_NOLOCK, 1, NULL, NULL))) {
1549                 return NULL;
1550         }
1551
1552         i = ao2_iterator_init(object_type->wizards, 0);
1553         for (; (wizard = ao2_iterator_next(&i)); ao2_ref(wizard, -1)) {
1554                 if (!wizard->wizard->retrieve_regex) {
1555                         continue;
1556                 }
1557
1558                 wizard->wizard->retrieve_regex(sorcery, wizard->data, object_type->name, objects, regex);
1559         }
1560         ao2_iterator_destroy(&i);
1561
1562         return objects;
1563 }
1564
1565 /*! \brief Internal function which returns if the wizard has created the object */
1566 static int sorcery_wizard_create(void *obj, void *arg, int flags)
1567 {
1568         const struct ast_sorcery_object_wizard *object_wizard = obj;
1569         const struct sorcery_details *details = arg;
1570
1571         return (!object_wizard->caching && !object_wizard->wizard->create(details->sorcery, object_wizard->data, details->obj)) ? CMP_MATCH | CMP_STOP : 0;
1572 }
1573
1574 /*! \brief Internal callback function which notifies an individual observer that an object has been created */
1575 static int sorcery_observer_notify_create(void *obj, void *arg, int flags)
1576 {
1577         const struct ast_sorcery_object_type_observer *observer = obj;
1578
1579         if (observer->callbacks->created) {
1580                 observer->callbacks->created(arg);
1581         }
1582
1583         return 0;
1584 }
1585
1586 /*! \brief Internal callback function which notifies observers that an object has been created */
1587 static int sorcery_observers_notify_create(void *data)
1588 {
1589         struct sorcery_observer_invocation *invocation = data;
1590
1591         ao2_callback(invocation->object_type->observers, OBJ_NODATA, sorcery_observer_notify_create, invocation->object);
1592         ao2_cleanup(invocation);
1593
1594         return 0;
1595 }
1596
1597 int ast_sorcery_create(const struct ast_sorcery *sorcery, void *object)
1598 {
1599         const struct ast_sorcery_object_details *details = object;
1600         RAII_VAR(struct ast_sorcery_object_type *, object_type, ao2_find(sorcery->types, details->object->type, OBJ_KEY), ao2_cleanup);
1601         RAII_VAR(struct ast_sorcery_object_wizard *, object_wizard, NULL, ao2_cleanup);
1602         struct sorcery_details sdetails = {
1603                 .sorcery = sorcery,
1604                 .obj = object,
1605         };
1606
1607         if (!object_type) {
1608                 return -1;
1609         }
1610
1611         if ((object_wizard = ao2_callback(object_type->wizards, 0, sorcery_wizard_create, &sdetails)) &&
1612                 ao2_container_count(object_type->observers)) {
1613                 struct sorcery_observer_invocation *invocation = sorcery_observer_invocation_alloc(object_type, object);
1614
1615                 if (invocation && ast_taskprocessor_push(object_type->serializer, sorcery_observers_notify_create, invocation)) {
1616                         ao2_cleanup(invocation);
1617                 }
1618         }
1619
1620         return object_wizard ? 0 : -1;
1621 }
1622
1623 /*! \brief Internal callback function which notifies an individual observer that an object has been updated */
1624 static int sorcery_observer_notify_update(void *obj, void *arg, int flags)
1625 {
1626         const struct ast_sorcery_object_type_observer *observer = obj;
1627
1628         if (observer->callbacks->updated) {
1629                 observer->callbacks->updated(arg);
1630         }
1631
1632         return 0;
1633 }
1634
1635 /*! \brief Internal callback function which notifies observers that an object has been updated */
1636 static int sorcery_observers_notify_update(void *data)
1637 {
1638         struct sorcery_observer_invocation *invocation = data;
1639
1640         ao2_callback(invocation->object_type->observers, OBJ_NODATA, sorcery_observer_notify_update, invocation->object);
1641         ao2_cleanup(invocation);
1642
1643         return 0;
1644 }
1645
1646 /*! \brief Internal function which returns if a wizard has updated the object */
1647 static int sorcery_wizard_update(void *obj, void *arg, int flags)
1648 {
1649         const struct ast_sorcery_object_wizard *object_wizard = obj;
1650         const struct sorcery_details *details = arg;
1651
1652         return (object_wizard->wizard->update && !object_wizard->wizard->update(details->sorcery, object_wizard->data, details->obj) &&
1653                 !object_wizard->caching) ? CMP_MATCH | CMP_STOP : 0;
1654 }
1655
1656 int ast_sorcery_update(const struct ast_sorcery *sorcery, void *object)
1657 {
1658         const struct ast_sorcery_object_details *details = object;
1659         RAII_VAR(struct ast_sorcery_object_type *, object_type, ao2_find(sorcery->types, details->object->type, OBJ_KEY), ao2_cleanup);
1660         RAII_VAR(struct ast_sorcery_object_wizard *, object_wizard, NULL, ao2_cleanup);
1661         struct sorcery_details sdetails = {
1662                 .sorcery = sorcery,
1663                 .obj = object,
1664         };
1665
1666         if (!object_type) {
1667                 return -1;
1668         }
1669
1670         if ((object_wizard = ao2_callback(object_type->wizards, 0, sorcery_wizard_update, &sdetails)) &&
1671                 ao2_container_count(object_type->observers)) {
1672                 struct sorcery_observer_invocation *invocation = sorcery_observer_invocation_alloc(object_type, object);
1673
1674                 if (invocation && ast_taskprocessor_push(object_type->serializer, sorcery_observers_notify_update, invocation)) {
1675                         ao2_cleanup(invocation);
1676                 }
1677         }
1678
1679         return object_wizard ? 0 : -1;
1680 }
1681
1682 /*! \brief Internal callback function which notifies an individual observer that an object has been deleted */
1683 static int sorcery_observer_notify_delete(void *obj, void *arg, int flags)
1684 {
1685         const struct ast_sorcery_object_type_observer *observer = obj;
1686
1687         if (observer->callbacks->deleted) {
1688                 observer->callbacks->deleted(arg);
1689         }
1690
1691         return 0;
1692 }
1693
1694 /*! \brief Internal callback function which notifies observers that an object has been deleted */
1695 static int sorcery_observers_notify_delete(void *data)
1696 {
1697         struct sorcery_observer_invocation *invocation = data;
1698
1699         ao2_callback(invocation->object_type->observers, OBJ_NODATA, sorcery_observer_notify_delete, invocation->object);
1700         ao2_cleanup(invocation);
1701
1702         return 0;
1703 }
1704
1705 /*! \brief Internal function which returns if a wizard has deleted the object */
1706 static int sorcery_wizard_delete(void *obj, void *arg, int flags)
1707 {
1708         const struct ast_sorcery_object_wizard *object_wizard = obj;
1709         const struct sorcery_details *details = arg;
1710
1711         return (object_wizard->wizard->delete && !object_wizard->wizard->delete(details->sorcery, object_wizard->data, details->obj) &&
1712                 !object_wizard->caching) ? CMP_MATCH | CMP_STOP : 0;
1713 }
1714
1715 int ast_sorcery_delete(const struct ast_sorcery *sorcery, void *object)
1716 {
1717         const struct ast_sorcery_object_details *details = object;
1718         RAII_VAR(struct ast_sorcery_object_type *, object_type, ao2_find(sorcery->types, details->object->type, OBJ_KEY), ao2_cleanup);
1719         RAII_VAR(struct ast_sorcery_object_wizard *, object_wizard, NULL, ao2_cleanup);
1720         struct sorcery_details sdetails = {
1721                 .sorcery = sorcery,
1722                 .obj = object,
1723         };
1724
1725         if (!object_type) {
1726                 return -1;
1727         }
1728
1729         if ((object_wizard = ao2_callback(object_type->wizards, 0, sorcery_wizard_delete, &sdetails)) &&
1730                 ao2_container_count(object_type->observers)) {
1731                 struct sorcery_observer_invocation *invocation = sorcery_observer_invocation_alloc(object_type, object);
1732
1733                 if (invocation && ast_taskprocessor_push(object_type->serializer, sorcery_observers_notify_delete, invocation)) {
1734                         ao2_cleanup(invocation);
1735                 }
1736         }
1737
1738         return object_wizard ? 0 : -1;
1739 }
1740
1741 void ast_sorcery_unref(struct ast_sorcery *sorcery)
1742 {
1743         if (sorcery) {
1744                 /* One ref for what we just released, the other for the instances container. */
1745                 ao2_wrlock(instances);
1746                 if (ao2_ref(sorcery, -1) == 2) {
1747                         ao2_unlink_flags(instances, sorcery, OBJ_NOLOCK);
1748                 }
1749                 ao2_unlock(instances);
1750         }
1751 }
1752
1753 const char *ast_sorcery_object_get_id(const void *object)
1754 {
1755         const struct ast_sorcery_object_details *details = object;
1756         return details->object->id;
1757 }
1758
1759 const char *ast_sorcery_object_get_type(const void *object)
1760 {
1761         const struct ast_sorcery_object_details *details = object;
1762         return details->object->type;
1763 }
1764
1765 const char *ast_sorcery_object_get_extended(const void *object, const char *name)
1766 {
1767         const struct ast_sorcery_object_details *details = object;
1768         struct ast_variable *field;
1769
1770         for (field = details->object->extended; field; field = field->next) {
1771                 if (!strcmp(field->name + 1, name)) {
1772                         return field->value;
1773                 }
1774         }
1775
1776         return NULL;
1777 }
1778
1779 int ast_sorcery_object_set_extended(const void *object, const char *name, const char *value)
1780 {
1781         RAII_VAR(struct ast_variable *, field, NULL, ast_variables_destroy);
1782         struct ast_variable *extended = ast_variable_new(name, value, ""), *previous = NULL;
1783         const struct ast_sorcery_object_details *details = object;
1784
1785         if (!extended) {
1786                 return -1;
1787         }
1788
1789         for (field = details->object->extended; field; previous = field, field = field->next) {
1790                 if (!strcmp(field->name, name)) {
1791                         if (previous) {
1792                                 previous->next = field->next;
1793                         } else {
1794                                 details->object->extended = field->next;
1795                         }
1796                         field->next = NULL;
1797                         break;
1798                 }
1799         }
1800
1801         extended->next = details->object->extended;
1802         details->object->extended = extended;
1803
1804         return 0;
1805 }
1806
1807 int ast_sorcery_observer_add(const struct ast_sorcery *sorcery, const char *type, const struct ast_sorcery_observer *callbacks)
1808 {
1809         RAII_VAR(struct ast_sorcery_object_type *, object_type, ao2_find(sorcery->types, type, OBJ_KEY), ao2_cleanup);
1810         struct ast_sorcery_object_type_observer *observer;
1811         int res;
1812
1813         if (!object_type || !callbacks) {
1814                 return -1;
1815         }
1816
1817         if (!(observer = ao2_alloc(sizeof(*observer), NULL))) {
1818                 return -1;
1819         }
1820
1821         observer->callbacks = callbacks;
1822         res = 0;
1823         if (!ao2_link(object_type->observers, observer)) {
1824                 res = -1;
1825         }
1826         ao2_ref(observer, -1);
1827
1828         return res;
1829 }
1830
1831 /*! \brief Internal callback function for removing an observer */
1832 static int sorcery_observer_remove(void *obj, void *arg, int flags)
1833 {
1834         const struct ast_sorcery_object_type_observer *observer = obj;
1835
1836         return (observer->callbacks == arg) ? CMP_MATCH | CMP_STOP : 0;
1837 }
1838
1839 void ast_sorcery_observer_remove(const struct ast_sorcery *sorcery, const char *type, const struct ast_sorcery_observer *callbacks)
1840 {
1841         RAII_VAR(struct ast_sorcery_object_type *, object_type, NULL, ao2_cleanup);
1842         struct ast_sorcery_observer *cbs = (struct ast_sorcery_observer *) callbacks;/* Remove const for traversal. */
1843
1844         if (!sorcery) {
1845                 return;
1846         }
1847         object_type = ao2_find(sorcery->types, type, OBJ_KEY);
1848         if (!object_type) {
1849                 return;
1850         }
1851
1852         ao2_callback(object_type->observers, OBJ_NODATA | OBJ_UNLINK,
1853                 sorcery_observer_remove, cbs);
1854 }
1855
1856 int ast_sorcery_object_id_sort(const void *obj, const void *arg, int flags)
1857 {
1858         const char *right_key = arg;
1859         int cmp;
1860
1861         switch (flags & OBJ_SEARCH_MASK) {
1862         case OBJ_SEARCH_OBJECT:
1863                 right_key = ast_sorcery_object_get_id(arg);
1864                 /* Fall through */
1865         case OBJ_SEARCH_KEY:
1866                 cmp = strcmp(ast_sorcery_object_get_id(obj), right_key);
1867                 break;
1868         case OBJ_SEARCH_PARTIAL_KEY:
1869                 cmp = strncmp(ast_sorcery_object_get_id(obj), right_key, strlen(right_key));
1870                 break;
1871         default:
1872                 cmp = 0;
1873                 break;
1874         }
1875         return cmp;
1876 }
1877
1878 int ast_sorcery_object_id_compare(void *obj, void *arg, int flags)
1879 {
1880         const char *right_key = arg;
1881         int cmp = 0;
1882
1883         switch (flags & OBJ_SEARCH_MASK) {
1884         case OBJ_SEARCH_OBJECT:
1885                 right_key = ast_sorcery_object_get_id(arg);
1886                 /* Fall through */
1887         case OBJ_SEARCH_KEY:
1888                 if (strcmp(ast_sorcery_object_get_id(obj), right_key) == 0) {
1889                         cmp = CMP_MATCH | CMP_STOP;
1890                 }
1891                 break;
1892         case OBJ_SEARCH_PARTIAL_KEY:
1893                 if (strncmp(ast_sorcery_object_get_id(obj), right_key, strlen(right_key)) == 0) {
1894                         cmp = CMP_MATCH;
1895                 }
1896                 break;
1897         default:
1898                 cmp = 0;
1899                 break;
1900         }
1901         return cmp;
1902 }
1903
1904 int ast_sorcery_object_id_hash(const void *obj, int flags) {
1905         if (flags & OBJ_SEARCH_OBJECT) {
1906                 return ast_str_hash(ast_sorcery_object_get_id(obj));
1907         } else if (flags & OBJ_SEARCH_KEY) {
1908                 return ast_str_hash(obj);
1909         }
1910         return -1;
1911 }
1912
1913 struct ast_sorcery_object_type *ast_sorcery_get_object_type(const struct ast_sorcery *sorcery,
1914                 const char *type)
1915 {
1916         return ao2_find(sorcery->types, type, OBJ_SEARCH_KEY);
1917 }
1918
1919 int ast_sorcery_is_object_field_registered(const struct ast_sorcery_object_type *object_type,
1920                 const char *field_name)
1921 {
1922         struct ast_sorcery_object_field *object_field;
1923         int res = 1;
1924
1925         ast_assert(object_type != NULL);
1926
1927         object_field = ao2_find(object_type->fields, field_name, OBJ_SEARCH_KEY);
1928         if (!object_field) {
1929                 res = 0;
1930         }
1931
1932         ao2_cleanup(object_field);
1933         return res;
1934 }