ARI: Add ability to raise arbitrary User Events
[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.\n", 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         ao2_cleanup(object_wizard->wizard);
693 }
694
695 /*! \brief Internal function which creates an object type and adds a wizard mapping */
696 static enum ast_sorcery_apply_result sorcery_apply_wizard_mapping(struct ast_sorcery *sorcery,
697                 const char *type, const char *module, const char *name, const char *data, unsigned int caching)
698 {
699         RAII_VAR(struct ast_sorcery_object_type *, object_type, ao2_find(sorcery->types, type, OBJ_KEY), ao2_cleanup);
700         RAII_VAR(struct ast_sorcery_wizard *, wizard, ao2_find(wizards, name, OBJ_KEY), ao2_cleanup);
701         RAII_VAR(struct ast_sorcery_object_wizard *, object_wizard, ao2_alloc(sizeof(*object_wizard), sorcery_object_wizard_destructor), ao2_cleanup);
702         int created = 0;
703
704         if (!wizard || !object_wizard) {
705                 return AST_SORCERY_APPLY_FAIL;
706         }
707
708         if (!object_type) {
709                 if (!(object_type = sorcery_object_type_alloc(type, module))) {
710                         return AST_SORCERY_APPLY_FAIL;
711                 }
712                 created = 1;
713         }
714
715         if (!created) {
716                 struct ast_sorcery_wizard *found;
717
718                 found = ao2_find(object_type->wizards, wizard, OBJ_SEARCH_OBJECT);
719                 if (found) {
720                         ast_debug(1, "Wizard %s already applied to object type %s\n",
721                                         wizard->name, object_type->name);
722                         ao2_cleanup(found);
723                         return AST_SORCERY_APPLY_DUPLICATE;
724                 }
725         }
726
727         if (wizard->open && !(object_wizard->data = wizard->open(data))) {
728                 return AST_SORCERY_APPLY_FAIL;
729         }
730
731         ast_module_ref(wizard->module);
732
733         object_wizard->wizard = ao2_bump(wizard);
734         object_wizard->caching = caching;
735
736         ao2_link(object_type->wizards, object_wizard);
737
738         if (created) {
739                 ao2_link(sorcery->types, object_type);
740         }
741
742         return AST_SORCERY_APPLY_SUCCESS;
743 }
744
745 enum ast_sorcery_apply_result  __ast_sorcery_apply_config(struct ast_sorcery *sorcery, const char *name, const char *module)
746 {
747         struct ast_flags flags = { 0 };
748         struct ast_config *config = ast_config_load2("sorcery.conf", "sorcery", flags);
749         struct ast_variable *mapping;
750         int res = AST_SORCERY_APPLY_SUCCESS;
751
752         if (!config) {
753                 return AST_SORCERY_APPLY_NO_CONFIGURATION;
754         }
755
756         if (config == CONFIG_STATUS_FILEINVALID) {
757                 return AST_SORCERY_APPLY_FAIL;
758         }
759
760         for (mapping = ast_variable_browse(config, name); mapping; mapping = mapping->next) {
761                 RAII_VAR(char *, mapping_name, ast_strdup(mapping->name), ast_free);
762                 RAII_VAR(char *, mapping_value, ast_strdup(mapping->value), ast_free);
763                 char *options = mapping_name;
764                 char *type = strsep(&options, "/");
765                 char *data = mapping_value;
766                 char *wizard = strsep(&data, ",");
767                 unsigned int caching = 0;
768
769                 /* If no object type or wizard exists just skip, nothing we can do */
770                 if (ast_strlen_zero(type) || ast_strlen_zero(wizard)) {
771                         continue;
772                 }
773
774                 /* If the wizard is configured as a cache treat it as such */
775                 if (!ast_strlen_zero(options) && strstr(options, "cache")) {
776                         caching = 1;
777                 }
778
779                 /* Any error immediately causes us to stop */
780                 if (sorcery_apply_wizard_mapping(sorcery, type, module, wizard, data, caching) == AST_SORCERY_APPLY_FAIL) {
781                         res = AST_SORCERY_APPLY_FAIL;
782                         break;
783                 }
784         }
785
786         ast_config_destroy(config);
787
788         return res;
789 }
790
791 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)
792 {
793         RAII_VAR(struct ast_sorcery_object_type *, object_type, ao2_find(sorcery->types, type, OBJ_KEY), ao2_cleanup);
794
795         /* Defaults can not be added if any existing mapping exists */
796         if (object_type) {
797                 return AST_SORCERY_APPLY_DEFAULT_UNNECESSARY;
798         }
799
800         return sorcery_apply_wizard_mapping(sorcery, type, module, name, data, 0);
801 }
802
803 static int sorcery_extended_config_handler(const struct aco_option *opt, struct ast_variable *var, void *obj)
804 {
805         return ast_sorcery_object_set_extended(obj, var->name, var->value);
806 }
807
808 static int sorcery_extended_fields_handler(const void *obj, struct ast_variable **fields)
809 {
810         const struct ast_sorcery_object_details *details = obj;
811
812         if (details->object->extended) {
813                 *fields = ast_variables_dup(details->object->extended);
814         } else {
815                 *fields = NULL;
816         }
817
818         return 0;
819 }
820
821 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)
822 {
823         RAII_VAR(struct ast_sorcery_object_type *, object_type, ao2_find(sorcery->types, type, OBJ_KEY), ao2_cleanup);
824
825         if (!object_type || object_type->type.item_alloc) {
826                 return -1;
827         }
828
829         object_type->type.name = object_type->name;
830         object_type->type.type = ACO_ITEM;
831         object_type->type.category = ".?";
832         object_type->type.item_alloc = alloc;
833         object_type->type.hidden = hidden;
834
835         object_type->reloadable = reloadable;
836         object_type->transform = transform;
837         object_type->apply = apply;
838         object_type->file->types[0] = &object_type->type;
839         object_type->file->types[1] = NULL;
840
841         if (aco_info_init(object_type->info)) {
842                 return -1;
843         }
844
845         if (ast_sorcery_object_fields_register(sorcery, type, "^@", sorcery_extended_config_handler, sorcery_extended_fields_handler)) {
846                 return -1;
847         }
848
849         return 0;
850 }
851
852 void ast_sorcery_object_set_copy_handler(struct ast_sorcery *sorcery, const char *type, sorcery_copy_handler copy)
853 {
854         RAII_VAR(struct ast_sorcery_object_type *, object_type, ao2_find(sorcery->types, type, OBJ_KEY), ao2_cleanup);
855
856         if (!object_type) {
857                 return;
858         }
859
860         object_type->copy = copy;
861 }
862
863 void ast_sorcery_object_set_diff_handler(struct ast_sorcery *sorcery, const char *type, sorcery_diff_handler diff)
864 {
865         RAII_VAR(struct ast_sorcery_object_type *, object_type, ao2_find(sorcery->types, type, OBJ_KEY), ao2_cleanup);
866
867         if (!object_type) {
868                 return;
869         }
870
871         object_type->diff = diff;
872 }
873
874 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)
875 {
876         RAII_VAR(struct ast_sorcery_object_type *, object_type, ao2_find(sorcery->types, type, OBJ_KEY), ao2_cleanup);
877         RAII_VAR(struct ast_sorcery_object_field *, object_field, NULL, ao2_cleanup);
878
879         if (!object_type || !object_type->type.item_alloc || !config_handler || !(object_field = ao2_alloc(sizeof(*object_field), NULL))) {
880                 return -1;
881         }
882
883         ast_copy_string(object_field->name, regex, sizeof(object_field->name));
884         object_field->multiple_handler = sorcery_handler;
885
886         ao2_link(object_type->fields, object_field);
887         __aco_option_register(object_type->info, regex, ACO_REGEX, object_type->file->types, "", OPT_CUSTOM_T, config_handler, 0, 1, 0);
888
889         return 0;
890 }
891
892 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,
893                                         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, ...)
894 {
895         RAII_VAR(struct ast_sorcery_object_type *, object_type, ao2_find(sorcery->types, type, OBJ_KEY), ao2_cleanup);
896         RAII_VAR(struct ast_sorcery_object_field *, object_field, NULL, ao2_cleanup);
897         int pos;
898         va_list args;
899
900         if (!strcmp(type, "id") || !object_type || !object_type->type.item_alloc) {
901                 return -1;
902         }
903
904         if (!sorcery_handler) {
905                 sorcery_handler = sorcery_field_default_handler(opt_type);
906         }
907
908         if (!(object_field = ao2_alloc(sizeof(*object_field) + argc * sizeof(object_field->args[0]), NULL))) {
909                 return -1;
910         }
911
912         ast_copy_string(object_field->name, name, sizeof(object_field->name));
913         object_field->handler = sorcery_handler;
914         object_field->multiple_handler = multiple_handler;
915
916         va_start(args, argc);
917         for (pos = 0; pos < argc; pos++) {
918                 object_field->args[pos] = va_arg(args, size_t);
919         }
920         va_end(args);
921
922         if (!alias) {
923                 ao2_link(object_type->fields, object_field);
924         }
925
926         /* TODO: Improve this hack */
927         if (!argc) {
928                 __aco_option_register(object_type->info, name, ACO_EXACT, object_type->file->types, default_val, opt_type, config_handler, flags, no_doc, argc);
929         } else if (argc == 1) {
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]);
932         } else if (argc == 2) {
933                 __aco_option_register(object_type->info, name, ACO_EXACT, object_type->file->types, default_val, opt_type, config_handler, flags, no_doc, argc,
934                         object_field->args[0], object_field->args[1]);
935         } else if (argc == 3) {
936                 __aco_option_register(object_type->info, name, ACO_EXACT, object_type->file->types, default_val, opt_type, config_handler, flags, no_doc, argc,
937                         object_field->args[0], object_field->args[1], object_field->args[2]);
938         } else {
939                 ast_assert(0); /* The hack... she does us no good for this */
940         }
941
942         return 0;
943 }
944
945 /*! \brief Retrieves whether or not the type is reloadable */
946 static int sorcery_reloadable(const struct ast_sorcery *sorcery, const char *type)
947 {
948         RAII_VAR(struct ast_sorcery_object_type *, object_type,
949                  ao2_find(sorcery->types, type, OBJ_KEY), ao2_cleanup);
950         return object_type && object_type->reloadable;
951 }
952
953 static int sorcery_wizard_load(void *obj, void *arg, int flags)
954 {
955         struct ast_sorcery_object_wizard *wizard = obj;
956         struct sorcery_load_details *details = arg;
957         void (*load)(void *data, const struct ast_sorcery *sorcery, const char *type);
958
959         if (details->reload && !sorcery_reloadable(details->sorcery, details->type)) {
960                 ast_log(LOG_NOTICE, "Type '%s' is not reloadable, "
961                         "maintaining previous values\n", details->type);
962                 return 0;
963         }
964
965         load = !details->reload ? wizard->wizard->load : wizard->wizard->reload;
966
967         if (load) {
968                 load(wizard->data, details->sorcery, details->type);
969         }
970
971         return 0;
972 }
973
974 /*! \brief Destructor for observer invocation */
975 static void sorcery_observer_invocation_destroy(void *obj)
976 {
977         struct sorcery_observer_invocation *invocation = obj;
978
979         ao2_cleanup(invocation->object_type);
980         ao2_cleanup(invocation->object);
981 }
982
983 /*! \brief Allocator function for observer invocation */
984 static struct sorcery_observer_invocation *sorcery_observer_invocation_alloc(struct ast_sorcery_object_type *object_type, void *object)
985 {
986         struct sorcery_observer_invocation *invocation = ao2_alloc(sizeof(*invocation), sorcery_observer_invocation_destroy);
987
988         if (!invocation) {
989                 return NULL;
990         }
991
992         ao2_ref(object_type, +1);
993         invocation->object_type = object_type;
994
995         if (object) {
996                 ao2_ref(object, +1);
997                 invocation->object = object;
998         }
999
1000         return invocation;
1001 }
1002
1003 /*! \brief Internal callback function which notifies an individual observer that an object type has been loaded */
1004 static int sorcery_observer_notify_loaded(void *obj, void *arg, int flags)
1005 {
1006         const struct ast_sorcery_object_type_observer *observer = obj;
1007
1008         if (observer->callbacks->loaded) {
1009                 observer->callbacks->loaded(arg);
1010         }
1011
1012         return 0;
1013 }
1014
1015 /*! \brief Internal callback function which notifies observers that an object type has been loaded */
1016 static int sorcery_observers_notify_loaded(void *data)
1017 {
1018         struct sorcery_observer_invocation *invocation = data;
1019
1020         ao2_callback(invocation->object_type->observers, OBJ_NODATA, sorcery_observer_notify_loaded, invocation->object_type->name);
1021         ao2_cleanup(invocation);
1022
1023         return 0;
1024 }
1025
1026 static int sorcery_object_load(void *obj, void *arg, int flags)
1027 {
1028         struct ast_sorcery_object_type *type = obj;
1029         struct sorcery_load_details *details = arg;
1030
1031         details->type = type->name;
1032         ao2_callback(type->wizards, OBJ_NODATA, sorcery_wizard_load, details);
1033
1034         if (ao2_container_count(type->observers)) {
1035                 struct sorcery_observer_invocation *invocation = sorcery_observer_invocation_alloc(type, NULL);
1036
1037                 if (invocation && ast_taskprocessor_push(type->serializer, sorcery_observers_notify_loaded, invocation)) {
1038                         ao2_cleanup(invocation);
1039                 }
1040         }
1041
1042         return 0;
1043 }
1044
1045 void ast_sorcery_load(const struct ast_sorcery *sorcery)
1046 {
1047         struct sorcery_load_details details = {
1048                 .sorcery = sorcery,
1049                 .reload = 0,
1050         };
1051
1052         ao2_callback(sorcery->types, OBJ_NODATA, sorcery_object_load, &details);
1053 }
1054
1055 void ast_sorcery_load_object(const struct ast_sorcery *sorcery, const char *type)
1056 {
1057         RAII_VAR(struct ast_sorcery_object_type *, object_type, ao2_find(sorcery->types, type, OBJ_KEY), ao2_cleanup);
1058         struct sorcery_load_details details = {
1059                 .sorcery = sorcery,
1060                 .reload = 0,
1061         };
1062
1063         if (!object_type) {
1064                 return;
1065         }
1066
1067         sorcery_object_load(object_type, &details, 0);
1068 }
1069
1070 void ast_sorcery_reload(const struct ast_sorcery *sorcery)
1071 {
1072         struct sorcery_load_details details = {
1073                 .sorcery = sorcery,
1074                 .reload = 1,
1075         };
1076
1077         ao2_callback(sorcery->types, OBJ_NODATA, sorcery_object_load, &details);
1078 }
1079
1080 void ast_sorcery_reload_object(const struct ast_sorcery *sorcery, const char *type)
1081 {
1082         RAII_VAR(struct ast_sorcery_object_type *, object_type, ao2_find(sorcery->types, type, OBJ_KEY), ao2_cleanup);
1083         struct sorcery_load_details details = {
1084                 .sorcery = sorcery,
1085                 .reload = 1,
1086         };
1087
1088         if (!object_type) {
1089                 return;
1090         }
1091
1092         sorcery_object_load(object_type, &details, 0);
1093 }
1094
1095 void ast_sorcery_ref(struct ast_sorcery *sorcery)
1096 {
1097         ao2_ref(sorcery, +1);
1098 }
1099
1100 static struct ast_variable *get_single_field_as_var_list(const void *object, struct ast_sorcery_object_field *object_field)
1101 {
1102         struct ast_variable *tmp = NULL;
1103         char *buf = NULL;
1104
1105         if (!object_field->handler) {
1106                 return NULL;
1107         }
1108
1109         if (!(object_field->handler(object, object_field->args, &buf))) {
1110                 tmp = ast_variable_new(object_field->name, S_OR(buf, ""), "");
1111         }
1112         ast_free(buf);
1113
1114         return tmp;
1115 }
1116
1117 static struct ast_variable *get_multiple_fields_as_var_list(const void *object, struct ast_sorcery_object_field *object_field)
1118 {
1119         struct ast_variable *tmp = NULL;
1120
1121         if (!object_field->multiple_handler) {
1122                 return NULL;
1123         }
1124
1125         if (object_field->multiple_handler(object, &tmp)) {
1126                 ast_variables_destroy(tmp);
1127                 tmp = NULL;
1128         }
1129
1130         return tmp;
1131 }
1132
1133 struct ast_variable *ast_sorcery_objectset_create2(const struct ast_sorcery *sorcery,
1134         const void *object,     enum ast_sorcery_field_handler_flags flags)
1135 {
1136         const struct ast_sorcery_object_details *details = object;
1137         RAII_VAR(struct ast_sorcery_object_type *, object_type, ao2_find(sorcery->types, details->object->type, OBJ_KEY), ao2_cleanup);
1138         struct ao2_iterator i;
1139         struct ast_sorcery_object_field *object_field;
1140         struct ast_variable *head = NULL;
1141         struct ast_variable *tail = NULL;
1142
1143         if (!object_type) {
1144                 return NULL;
1145         }
1146
1147         i = ao2_iterator_init(object_type->fields, 0);
1148
1149         for (; (object_field = ao2_iterator_next(&i)); ao2_ref(object_field, -1)) {
1150                 struct ast_variable *tmp;
1151
1152                 switch (flags) {
1153                 case AST_HANDLER_PREFER_LIST:
1154                         if ((tmp = get_multiple_fields_as_var_list(object, object_field)) ||
1155                                 (tmp = get_single_field_as_var_list(object, object_field))) {
1156                                 break;
1157                         }
1158                         continue;
1159                 case AST_HANDLER_PREFER_STRING:
1160                         if ((tmp = get_single_field_as_var_list(object, object_field)) ||
1161                                 (tmp = get_multiple_fields_as_var_list(object, object_field))) {
1162                                 break;
1163                         }
1164                         continue;
1165                 case AST_HANDLER_ONLY_LIST:
1166                         if ((tmp = get_multiple_fields_as_var_list(object, object_field))) {
1167                                 break;
1168                         }
1169                         continue;
1170                 case AST_HANDLER_ONLY_STRING:
1171                         if ((tmp = get_single_field_as_var_list(object, object_field))) {
1172                                 break;
1173                         }
1174                         continue;
1175                 default:
1176                         continue;
1177                 }
1178
1179                 tail = ast_variable_list_append_hint(&head, tail, tmp);
1180         }
1181
1182         ao2_iterator_destroy(&i);
1183
1184         return head;
1185 }
1186
1187 struct ast_json *ast_sorcery_objectset_json_create(const struct ast_sorcery *sorcery, const void *object)
1188 {
1189         const struct ast_sorcery_object_details *details = object;
1190         RAII_VAR(struct ast_sorcery_object_type *, object_type, ao2_find(sorcery->types, details->object->type, OBJ_KEY), ao2_cleanup);
1191         struct ao2_iterator i;
1192         struct ast_sorcery_object_field *object_field;
1193         struct ast_json *json = ast_json_object_create();
1194         int res = 0;
1195
1196         if (!object_type || !json) {
1197                 return NULL;
1198         }
1199
1200         i = ao2_iterator_init(object_type->fields, 0);
1201
1202         for (; !res && (object_field = ao2_iterator_next(&i)); ao2_ref(object_field, -1)) {
1203                 if (object_field->multiple_handler) {
1204                         struct ast_variable *tmp = NULL;
1205                         struct ast_variable *field;
1206
1207                         if ((res = object_field->multiple_handler(object, &tmp))) {
1208                                 ast_variables_destroy(tmp);
1209                                 ao2_ref(object_field, -1);
1210                                 break;
1211                         }
1212
1213                         for (field = tmp; field; field = field->next) {
1214                                 struct ast_json *value = ast_json_string_create(field->value);
1215
1216                                 if (!value || ast_json_object_set(json, field->name, value)) {
1217                                         res = -1;
1218                                         break;
1219                                 }
1220                         }
1221
1222                         ast_variables_destroy(tmp);
1223                 } else if (object_field->handler) {
1224                         char *buf = NULL;
1225                         struct ast_json *value = NULL;
1226
1227                         if ((res = object_field->handler(object, object_field->args, &buf))
1228                                 || !(value = ast_json_string_create(buf))
1229                                 || ast_json_object_set(json, object_field->name, value)) {
1230                                 res = -1;
1231                         }
1232
1233                         ast_free(buf);
1234                 } else {
1235                         continue;
1236                 }
1237         }
1238
1239         ao2_iterator_destroy(&i);
1240
1241         /* If any error occurs we destroy the JSON object so a partial objectset is not returned */
1242         if (res) {
1243                 ast_json_unref(json);
1244                 json = NULL;
1245         }
1246
1247         return json;
1248 }
1249
1250 int ast_sorcery_objectset_apply(const struct ast_sorcery *sorcery, void *object, struct ast_variable *objectset)
1251 {
1252         const struct ast_sorcery_object_details *details = object;
1253         RAII_VAR(struct ast_sorcery_object_type *, object_type, ao2_find(sorcery->types, details->object->type, OBJ_KEY), ao2_cleanup);
1254         RAII_VAR(struct ast_variable *, transformed, NULL, ast_variables_destroy);
1255         struct ast_variable *field;
1256         int res = 0;
1257
1258         if (!object_type) {
1259                 return -1;
1260         }
1261
1262         if (object_type->transform && (transformed = object_type->transform(objectset))) {
1263                 field = transformed;
1264         } else {
1265                 field = objectset;
1266         }
1267
1268         for (; field; field = field->next) {
1269                 if ((res = aco_process_var(&object_type->type, details->object->id, field, object))) {
1270                         break;
1271                 }
1272         }
1273
1274         if (!res && object_type->apply) {
1275                 res = object_type->apply(sorcery, object);
1276         }
1277
1278         return res;
1279 }
1280
1281 static const struct ast_variable *sorcery_find_field(const struct ast_variable *fields, const char *name)
1282 {
1283         const struct ast_variable *field;
1284
1285         /* Search the linked list of fields to find the correct one */
1286         for (field = fields; field; field = field->next) {
1287                 if (!strcmp(field->name, name)) {
1288                         return field;
1289                 }
1290         }
1291
1292         return NULL;
1293 }
1294
1295 int ast_sorcery_changeset_create(const struct ast_variable *original, const struct ast_variable *modified, struct ast_variable **changes)
1296 {
1297         const struct ast_variable *field;
1298         int res = 0;
1299
1300         *changes = NULL;
1301
1302         /* Unless the ast_variable list changes when examined... it can't differ from itself */
1303         if (original == modified) {
1304                 return 0;
1305         }
1306
1307         for (field = modified; field; field = field->next) {
1308                 const struct ast_variable *old = sorcery_find_field(original, field->name);
1309
1310                 if (!old || strcmp(old->value, field->value)) {
1311                         struct ast_variable *tmp;
1312
1313                         if (!(tmp = ast_variable_new(field->name, field->value, ""))) {
1314                                 res = -1;
1315                                 break;
1316                         }
1317
1318                         tmp->next = *changes;
1319                         *changes = tmp;
1320                 }
1321         }
1322
1323         /* If an error occurred do not return a partial changeset */
1324         if (res) {
1325                 ast_variables_destroy(*changes);
1326                 *changes = NULL;
1327         }
1328
1329         return res;
1330 }
1331
1332 static void sorcery_object_destructor(void *object)
1333 {
1334         struct ast_sorcery_object_details *details = object;
1335
1336         if (details->object->destructor) {
1337                 details->object->destructor(object);
1338         }
1339
1340         ast_variables_destroy(details->object->extended);
1341         ast_free(details->object->id);
1342 }
1343
1344 void *ast_sorcery_generic_alloc(size_t size, ao2_destructor_fn destructor)
1345 {
1346         void *object = ao2_alloc_options(size + sizeof(struct ast_sorcery_object), sorcery_object_destructor, AO2_ALLOC_OPT_LOCK_NOLOCK);
1347         struct ast_sorcery_object_details *details = object;
1348
1349         if (!object) {
1350                 return NULL;
1351         }
1352
1353         details->object = object + size;
1354         details->object->destructor = destructor;
1355
1356         return object;
1357 }
1358
1359 void *ast_sorcery_alloc(const struct ast_sorcery *sorcery, const char *type, const char *id)
1360 {
1361         RAII_VAR(struct ast_sorcery_object_type *, object_type, ao2_find(sorcery->types, type, OBJ_KEY), ao2_cleanup);
1362         struct ast_sorcery_object_details *details;
1363
1364         if (!object_type || !object_type->type.item_alloc ||
1365                 !(details = object_type->type.item_alloc(id))) {
1366                 return NULL;
1367         }
1368
1369         if (ast_strlen_zero(id)) {
1370                 char uuid[AST_UUID_STR_LEN];
1371
1372                 ast_uuid_generate_str(uuid, sizeof(uuid));
1373                 details->object->id = ast_strdup(uuid);
1374         } else {
1375                 details->object->id = ast_strdup(id);
1376         }
1377
1378         ast_copy_string(details->object->type, type, sizeof(details->object->type));
1379
1380         if (aco_set_defaults(&object_type->type, id, details)) {
1381                 ao2_ref(details, -1);
1382                 return NULL;
1383         }
1384
1385         return details;
1386 }
1387
1388 void *ast_sorcery_copy(const struct ast_sorcery *sorcery, const void *object)
1389 {
1390         const struct ast_sorcery_object_details *details = object;
1391         RAII_VAR(struct ast_sorcery_object_type *, object_type, ao2_find(sorcery->types, details->object->type, OBJ_KEY), ao2_cleanup);
1392         struct ast_sorcery_object_details *copy = ast_sorcery_alloc(sorcery, details->object->type, details->object->id);
1393         RAII_VAR(struct ast_variable *, objectset, NULL, ast_variables_destroy);
1394         int res = 0;
1395
1396         if (!copy) {
1397                 return NULL;
1398         } else if (object_type->copy) {
1399                 res = object_type->copy(object, copy);
1400         } else if ((objectset = ast_sorcery_objectset_create(sorcery, object))) {
1401                 res = ast_sorcery_objectset_apply(sorcery, copy, objectset);
1402         } else {
1403                 /* No native copy available and could not create an objectset, this copy has failed */
1404                 res = -1;
1405         }
1406
1407         if (res) {
1408                 ao2_cleanup(copy);
1409                 copy = NULL;
1410         }
1411
1412         return copy;
1413 }
1414
1415 int ast_sorcery_diff(const struct ast_sorcery *sorcery, const void *original, const void *modified, struct ast_variable **changes)
1416 {
1417         RAII_VAR(struct ast_sorcery_object_type *, object_type, ao2_find(sorcery->types, ast_sorcery_object_get_type(original), OBJ_KEY), ao2_cleanup);
1418
1419         *changes = NULL;
1420
1421         if (strcmp(ast_sorcery_object_get_type(original), ast_sorcery_object_get_type(modified))) {
1422                 return -1;
1423         }
1424
1425         if (original == modified) {
1426                 return 0;
1427         } else if (!object_type->diff) {
1428                 RAII_VAR(struct ast_variable *, objectset1, NULL, ast_variables_destroy);
1429                 RAII_VAR(struct ast_variable *, objectset2, NULL, ast_variables_destroy);
1430
1431                 objectset1 = ast_sorcery_objectset_create(sorcery, original);
1432                 objectset2 = ast_sorcery_objectset_create(sorcery, modified);
1433
1434                 return ast_sorcery_changeset_create(objectset1, objectset2, changes);
1435         } else {
1436                 return object_type->diff(original, modified, changes);
1437         }
1438 }
1439
1440 /*! \brief Structure used when calling create, update, or delete */
1441 struct sorcery_details {
1442         /*! \brief Pointer to the sorcery instance */
1443         const struct ast_sorcery *sorcery;
1444         /*! \brief Pointer to the object itself */
1445         void *obj;
1446 };
1447
1448 /*! \brief Internal function used to create an object in caching wizards */
1449 static int sorcery_cache_create(void *obj, void *arg, int flags)
1450 {
1451         const struct ast_sorcery_object_wizard *object_wizard = obj;
1452         const struct sorcery_details *details = arg;
1453
1454         if (!object_wizard->caching || !object_wizard->wizard->create) {
1455                 return 0;
1456         }
1457
1458         object_wizard->wizard->create(details->sorcery, object_wizard->data, details->obj);
1459
1460         return 0;
1461 }
1462
1463 void *ast_sorcery_retrieve_by_id(const struct ast_sorcery *sorcery, const char *type, const char *id)
1464 {
1465         RAII_VAR(struct ast_sorcery_object_type *, object_type, ao2_find(sorcery->types, type, OBJ_KEY), ao2_cleanup);
1466         void *object = NULL;
1467         struct ao2_iterator i;
1468         struct ast_sorcery_object_wizard *wizard;
1469         unsigned int cached = 0;
1470
1471         if (!object_type || ast_strlen_zero(id)) {
1472                 return NULL;
1473         }
1474
1475         i = ao2_iterator_init(object_type->wizards, 0);
1476         for (; (wizard = ao2_iterator_next(&i)); ao2_ref(wizard, -1)) {
1477                 if (wizard->wizard->retrieve_id &&
1478                         !(object = wizard->wizard->retrieve_id(sorcery, wizard->data, object_type->name, id))) {
1479                         continue;
1480                 }
1481
1482                 cached = wizard->caching;
1483
1484                 ao2_ref(wizard, -1);
1485                 break;
1486         }
1487         ao2_iterator_destroy(&i);
1488
1489         if (!cached && object) {
1490                 ao2_callback(object_type->wizards, 0, sorcery_cache_create, object);
1491         }
1492
1493         return object;
1494 }
1495
1496 void *ast_sorcery_retrieve_by_fields(const struct ast_sorcery *sorcery, const char *type, unsigned int flags, struct ast_variable *fields)
1497 {
1498         RAII_VAR(struct ast_sorcery_object_type *, object_type, ao2_find(sorcery->types, type, OBJ_KEY), ao2_cleanup);
1499         void *object = NULL;
1500         struct ao2_iterator i;
1501         struct ast_sorcery_object_wizard *wizard;
1502         unsigned int cached = 0;
1503
1504         if (!object_type) {
1505                 return NULL;
1506         }
1507
1508         /* If returning multiple objects create a container to store them in */
1509         if ((flags & AST_RETRIEVE_FLAG_MULTIPLE)) {
1510                 if (!(object = ao2_container_alloc_options(AO2_ALLOC_OPT_LOCK_NOLOCK, 1, NULL, NULL))) {
1511                         return NULL;
1512                 }
1513         }
1514
1515         /* Inquire with the available wizards for retrieval */
1516         i = ao2_iterator_init(object_type->wizards, 0);
1517         for (; (wizard = ao2_iterator_next(&i)); ao2_ref(wizard, -1)) {
1518                 if ((flags & AST_RETRIEVE_FLAG_MULTIPLE)) {
1519                         if (wizard->wizard->retrieve_multiple) {
1520                                 wizard->wizard->retrieve_multiple(sorcery, wizard->data, object_type->name, object, fields);
1521                         }
1522                 } else if (fields && wizard->wizard->retrieve_fields) {
1523                         if (wizard->wizard->retrieve_fields) {
1524                                 object = wizard->wizard->retrieve_fields(sorcery, wizard->data, object_type->name, fields);
1525                         }
1526                 }
1527
1528                 if ((flags & AST_RETRIEVE_FLAG_MULTIPLE) || !object) {
1529                         continue;
1530                 }
1531
1532                 cached = wizard->caching;
1533
1534                 ao2_ref(wizard, -1);
1535                 break;
1536         }
1537         ao2_iterator_destroy(&i);
1538
1539         /* If we are returning a single object and it came from a non-cache source create it in any caches */
1540         if (!(flags & AST_RETRIEVE_FLAG_MULTIPLE) && !cached && object) {
1541                 ao2_callback(object_type->wizards, 0, sorcery_cache_create, object);
1542         }
1543
1544         return object;
1545 }
1546
1547 struct ao2_container *ast_sorcery_retrieve_by_regex(const struct ast_sorcery *sorcery, const char *type, const char *regex)
1548 {
1549         RAII_VAR(struct ast_sorcery_object_type *, object_type, ao2_find(sorcery->types, type, OBJ_KEY), ao2_cleanup);
1550         struct ao2_container *objects;
1551         struct ao2_iterator i;
1552         struct ast_sorcery_object_wizard *wizard;
1553
1554         if (!object_type || !(objects = ao2_container_alloc_options(AO2_ALLOC_OPT_LOCK_NOLOCK, 1, NULL, NULL))) {
1555                 return NULL;
1556         }
1557
1558         i = ao2_iterator_init(object_type->wizards, 0);
1559         for (; (wizard = ao2_iterator_next(&i)); ao2_ref(wizard, -1)) {
1560                 if (!wizard->wizard->retrieve_regex) {
1561                         continue;
1562                 }
1563
1564                 wizard->wizard->retrieve_regex(sorcery, wizard->data, object_type->name, objects, regex);
1565         }
1566         ao2_iterator_destroy(&i);
1567
1568         return objects;
1569 }
1570
1571 /*! \brief Internal function which returns if the wizard has created the object */
1572 static int sorcery_wizard_create(void *obj, void *arg, int flags)
1573 {
1574         const struct ast_sorcery_object_wizard *object_wizard = obj;
1575         const struct sorcery_details *details = arg;
1576
1577         return (!object_wizard->caching && !object_wizard->wizard->create(details->sorcery, object_wizard->data, details->obj)) ? CMP_MATCH | CMP_STOP : 0;
1578 }
1579
1580 /*! \brief Internal callback function which notifies an individual observer that an object has been created */
1581 static int sorcery_observer_notify_create(void *obj, void *arg, int flags)
1582 {
1583         const struct ast_sorcery_object_type_observer *observer = obj;
1584
1585         if (observer->callbacks->created) {
1586                 observer->callbacks->created(arg);
1587         }
1588
1589         return 0;
1590 }
1591
1592 /*! \brief Internal callback function which notifies observers that an object has been created */
1593 static int sorcery_observers_notify_create(void *data)
1594 {
1595         struct sorcery_observer_invocation *invocation = data;
1596
1597         ao2_callback(invocation->object_type->observers, OBJ_NODATA, sorcery_observer_notify_create, invocation->object);
1598         ao2_cleanup(invocation);
1599
1600         return 0;
1601 }
1602
1603 int ast_sorcery_create(const struct ast_sorcery *sorcery, void *object)
1604 {
1605         const struct ast_sorcery_object_details *details = object;
1606         RAII_VAR(struct ast_sorcery_object_type *, object_type, ao2_find(sorcery->types, details->object->type, OBJ_KEY), ao2_cleanup);
1607         RAII_VAR(struct ast_sorcery_object_wizard *, object_wizard, NULL, ao2_cleanup);
1608         struct sorcery_details sdetails = {
1609                 .sorcery = sorcery,
1610                 .obj = object,
1611         };
1612
1613         if (!object_type) {
1614                 return -1;
1615         }
1616
1617         if ((object_wizard = ao2_callback(object_type->wizards, 0, sorcery_wizard_create, &sdetails)) &&
1618                 ao2_container_count(object_type->observers)) {
1619                 struct sorcery_observer_invocation *invocation = sorcery_observer_invocation_alloc(object_type, object);
1620
1621                 if (invocation && ast_taskprocessor_push(object_type->serializer, sorcery_observers_notify_create, invocation)) {
1622                         ao2_cleanup(invocation);
1623                 }
1624         }
1625
1626         return object_wizard ? 0 : -1;
1627 }
1628
1629 /*! \brief Internal callback function which notifies an individual observer that an object has been updated */
1630 static int sorcery_observer_notify_update(void *obj, void *arg, int flags)
1631 {
1632         const struct ast_sorcery_object_type_observer *observer = obj;
1633
1634         if (observer->callbacks->updated) {
1635                 observer->callbacks->updated(arg);
1636         }
1637
1638         return 0;
1639 }
1640
1641 /*! \brief Internal callback function which notifies observers that an object has been updated */
1642 static int sorcery_observers_notify_update(void *data)
1643 {
1644         struct sorcery_observer_invocation *invocation = data;
1645
1646         ao2_callback(invocation->object_type->observers, OBJ_NODATA, sorcery_observer_notify_update, invocation->object);
1647         ao2_cleanup(invocation);
1648
1649         return 0;
1650 }
1651
1652 /*! \brief Internal function which returns if a wizard has updated the object */
1653 static int sorcery_wizard_update(void *obj, void *arg, int flags)
1654 {
1655         const struct ast_sorcery_object_wizard *object_wizard = obj;
1656         const struct sorcery_details *details = arg;
1657
1658         return (object_wizard->wizard->update && !object_wizard->wizard->update(details->sorcery, object_wizard->data, details->obj) &&
1659                 !object_wizard->caching) ? CMP_MATCH | CMP_STOP : 0;
1660 }
1661
1662 int ast_sorcery_update(const struct ast_sorcery *sorcery, void *object)
1663 {
1664         const struct ast_sorcery_object_details *details = object;
1665         RAII_VAR(struct ast_sorcery_object_type *, object_type, ao2_find(sorcery->types, details->object->type, OBJ_KEY), ao2_cleanup);
1666         RAII_VAR(struct ast_sorcery_object_wizard *, object_wizard, NULL, ao2_cleanup);
1667         struct sorcery_details sdetails = {
1668                 .sorcery = sorcery,
1669                 .obj = object,
1670         };
1671
1672         if (!object_type) {
1673                 return -1;
1674         }
1675
1676         if ((object_wizard = ao2_callback(object_type->wizards, 0, sorcery_wizard_update, &sdetails)) &&
1677                 ao2_container_count(object_type->observers)) {
1678                 struct sorcery_observer_invocation *invocation = sorcery_observer_invocation_alloc(object_type, object);
1679
1680                 if (invocation && ast_taskprocessor_push(object_type->serializer, sorcery_observers_notify_update, invocation)) {
1681                         ao2_cleanup(invocation);
1682                 }
1683         }
1684
1685         return object_wizard ? 0 : -1;
1686 }
1687
1688 /*! \brief Internal callback function which notifies an individual observer that an object has been deleted */
1689 static int sorcery_observer_notify_delete(void *obj, void *arg, int flags)
1690 {
1691         const struct ast_sorcery_object_type_observer *observer = obj;
1692
1693         if (observer->callbacks->deleted) {
1694                 observer->callbacks->deleted(arg);
1695         }
1696
1697         return 0;
1698 }
1699
1700 /*! \brief Internal callback function which notifies observers that an object has been deleted */
1701 static int sorcery_observers_notify_delete(void *data)
1702 {
1703         struct sorcery_observer_invocation *invocation = data;
1704
1705         ao2_callback(invocation->object_type->observers, OBJ_NODATA, sorcery_observer_notify_delete, invocation->object);
1706         ao2_cleanup(invocation);
1707
1708         return 0;
1709 }
1710
1711 /*! \brief Internal function which returns if a wizard has deleted the object */
1712 static int sorcery_wizard_delete(void *obj, void *arg, int flags)
1713 {
1714         const struct ast_sorcery_object_wizard *object_wizard = obj;
1715         const struct sorcery_details *details = arg;
1716
1717         return (object_wizard->wizard->delete && !object_wizard->wizard->delete(details->sorcery, object_wizard->data, details->obj) &&
1718                 !object_wizard->caching) ? CMP_MATCH | CMP_STOP : 0;
1719 }
1720
1721 int ast_sorcery_delete(const struct ast_sorcery *sorcery, void *object)
1722 {
1723         const struct ast_sorcery_object_details *details = object;
1724         RAII_VAR(struct ast_sorcery_object_type *, object_type, ao2_find(sorcery->types, details->object->type, OBJ_KEY), ao2_cleanup);
1725         RAII_VAR(struct ast_sorcery_object_wizard *, object_wizard, NULL, ao2_cleanup);
1726         struct sorcery_details sdetails = {
1727                 .sorcery = sorcery,
1728                 .obj = object,
1729         };
1730
1731         if (!object_type) {
1732                 return -1;
1733         }
1734
1735         if ((object_wizard = ao2_callback(object_type->wizards, 0, sorcery_wizard_delete, &sdetails)) &&
1736                 ao2_container_count(object_type->observers)) {
1737                 struct sorcery_observer_invocation *invocation = sorcery_observer_invocation_alloc(object_type, object);
1738
1739                 if (invocation && ast_taskprocessor_push(object_type->serializer, sorcery_observers_notify_delete, invocation)) {
1740                         ao2_cleanup(invocation);
1741                 }
1742         }
1743
1744         return object_wizard ? 0 : -1;
1745 }
1746
1747 void ast_sorcery_unref(struct ast_sorcery *sorcery)
1748 {
1749         if (sorcery) {
1750                 /* One ref for what we just released, the other for the instances container. */
1751                 ao2_wrlock(instances);
1752                 if (ao2_ref(sorcery, -1) == 2) {
1753                         ao2_unlink_flags(instances, sorcery, OBJ_NOLOCK);
1754                 }
1755                 ao2_unlock(instances);
1756         }
1757 }
1758
1759 const char *ast_sorcery_object_get_id(const void *object)
1760 {
1761         const struct ast_sorcery_object_details *details = object;
1762         return details->object->id;
1763 }
1764
1765 const char *ast_sorcery_object_get_type(const void *object)
1766 {
1767         const struct ast_sorcery_object_details *details = object;
1768         return details->object->type;
1769 }
1770
1771 const char *ast_sorcery_object_get_extended(const void *object, const char *name)
1772 {
1773         const struct ast_sorcery_object_details *details = object;
1774         struct ast_variable *field;
1775
1776         for (field = details->object->extended; field; field = field->next) {
1777                 if (!strcmp(field->name + 1, name)) {
1778                         return field->value;
1779                 }
1780         }
1781
1782         return NULL;
1783 }
1784
1785 int ast_sorcery_object_set_extended(const void *object, const char *name, const char *value)
1786 {
1787         RAII_VAR(struct ast_variable *, field, NULL, ast_variables_destroy);
1788         struct ast_variable *extended = ast_variable_new(name, value, ""), *previous = NULL;
1789         const struct ast_sorcery_object_details *details = object;
1790
1791         if (!extended) {
1792                 return -1;
1793         }
1794
1795         for (field = details->object->extended; field; previous = field, field = field->next) {
1796                 if (!strcmp(field->name, name)) {
1797                         if (previous) {
1798                                 previous->next = field->next;
1799                         } else {
1800                                 details->object->extended = field->next;
1801                         }
1802                         field->next = NULL;
1803                         break;
1804                 }
1805         }
1806
1807         extended->next = details->object->extended;
1808         details->object->extended = extended;
1809
1810         return 0;
1811 }
1812
1813 int ast_sorcery_observer_add(const struct ast_sorcery *sorcery, const char *type, const struct ast_sorcery_observer *callbacks)
1814 {
1815         RAII_VAR(struct ast_sorcery_object_type *, object_type, ao2_find(sorcery->types, type, OBJ_KEY), ao2_cleanup);
1816         struct ast_sorcery_object_type_observer *observer;
1817         int res;
1818
1819         if (!object_type || !callbacks) {
1820                 return -1;
1821         }
1822
1823         if (!(observer = ao2_alloc(sizeof(*observer), NULL))) {
1824                 return -1;
1825         }
1826
1827         observer->callbacks = callbacks;
1828         res = 0;
1829         if (!ao2_link(object_type->observers, observer)) {
1830                 res = -1;
1831         }
1832         ao2_ref(observer, -1);
1833
1834         return res;
1835 }
1836
1837 /*! \brief Internal callback function for removing an observer */
1838 static int sorcery_observer_remove(void *obj, void *arg, int flags)
1839 {
1840         const struct ast_sorcery_object_type_observer *observer = obj;
1841
1842         return (observer->callbacks == arg) ? CMP_MATCH | CMP_STOP : 0;
1843 }
1844
1845 void ast_sorcery_observer_remove(const struct ast_sorcery *sorcery, const char *type, const struct ast_sorcery_observer *callbacks)
1846 {
1847         RAII_VAR(struct ast_sorcery_object_type *, object_type, NULL, ao2_cleanup);
1848         struct ast_sorcery_observer *cbs = (struct ast_sorcery_observer *) callbacks;/* Remove const for traversal. */
1849
1850         if (!sorcery) {
1851                 return;
1852         }
1853         object_type = ao2_find(sorcery->types, type, OBJ_KEY);
1854         if (!object_type) {
1855                 return;
1856         }
1857
1858         ao2_callback(object_type->observers, OBJ_NODATA | OBJ_UNLINK,
1859                 sorcery_observer_remove, cbs);
1860 }
1861
1862 int ast_sorcery_object_id_sort(const void *obj, const void *arg, int flags)
1863 {
1864         const char *right_key = arg;
1865         int cmp;
1866
1867         switch (flags & OBJ_SEARCH_MASK) {
1868         case OBJ_SEARCH_OBJECT:
1869                 right_key = ast_sorcery_object_get_id(arg);
1870                 /* Fall through */
1871         case OBJ_SEARCH_KEY:
1872                 cmp = strcmp(ast_sorcery_object_get_id(obj), right_key);
1873                 break;
1874         case OBJ_SEARCH_PARTIAL_KEY:
1875                 cmp = strncmp(ast_sorcery_object_get_id(obj), right_key, strlen(right_key));
1876                 break;
1877         default:
1878                 cmp = 0;
1879                 break;
1880         }
1881         return cmp;
1882 }
1883
1884 int ast_sorcery_object_id_compare(void *obj, void *arg, int flags)
1885 {
1886         const char *right_key = arg;
1887         int cmp = 0;
1888
1889         switch (flags & OBJ_SEARCH_MASK) {
1890         case OBJ_SEARCH_OBJECT:
1891                 right_key = ast_sorcery_object_get_id(arg);
1892                 /* Fall through */
1893         case OBJ_SEARCH_KEY:
1894                 if (strcmp(ast_sorcery_object_get_id(obj), right_key) == 0) {
1895                         cmp = CMP_MATCH | CMP_STOP;
1896                 }
1897                 break;
1898         case OBJ_SEARCH_PARTIAL_KEY:
1899                 if (strncmp(ast_sorcery_object_get_id(obj), right_key, strlen(right_key)) == 0) {
1900                         cmp = CMP_MATCH;
1901                 }
1902                 break;
1903         default:
1904                 cmp = 0;
1905                 break;
1906         }
1907         return cmp;
1908 }
1909
1910 int ast_sorcery_object_id_hash(const void *obj, int flags) {
1911         if (flags & OBJ_SEARCH_OBJECT) {
1912                 return ast_str_hash(ast_sorcery_object_get_id(obj));
1913         } else if (flags & OBJ_SEARCH_KEY) {
1914                 return ast_str_hash(obj);
1915         }
1916         return -1;
1917 }
1918
1919 struct ast_sorcery_object_type *ast_sorcery_get_object_type(const struct ast_sorcery *sorcery,
1920                 const char *type)
1921 {
1922         return ao2_find(sorcery->types, type, OBJ_SEARCH_KEY);
1923 }
1924
1925 int ast_sorcery_is_object_field_registered(const struct ast_sorcery_object_type *object_type,
1926                 const char *field_name)
1927 {
1928         struct ast_sorcery_object_field *object_field;
1929         int res = 1;
1930
1931         ast_assert(object_type != NULL);
1932
1933         object_field = ao2_find(object_type->fields, field_name, OBJ_SEARCH_KEY);
1934         if (!object_field) {
1935                 res = 0;
1936         }
1937
1938         ao2_cleanup(object_field);
1939         return res;
1940 }