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