ARI: Add ability to raise arbitrary User Events
[asterisk/asterisk.git] / main / sorcery.c
index d0f2e7d..9488dee 100644 (file)
@@ -34,10 +34,16 @@ ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
 #include "asterisk/logger.h"
 #include "asterisk/sorcery.h"
 #include "asterisk/astobj2.h"
+#include "asterisk/format.h"
+#include "asterisk/format_cap.h"
 #include "asterisk/strings.h"
 #include "asterisk/config_options.h"
 #include "asterisk/netsock2.h"
 #include "asterisk/module.h"
+#include "asterisk/taskprocessor.h"
+#include "asterisk/threadpool.h"
+#include "asterisk/json.h"
+#include "asterisk/format_pref.h"
 
 /* To prevent DEBUG_FD_LEAKS from interfering with things we undef open and close */
 #undef open
@@ -49,8 +55,29 @@ ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
 /*! \brief Number of buckets for types (should be prime for performance reasons) */
 #define TYPE_BUCKETS 53
 
-/*! \brief Maximum length of an object field name */
-#define MAX_OBJECT_FIELD 128
+/*! \brief Number of buckets for instances (should be prime for performance reasons) */
+#define INSTANCE_BUCKETS 17
+
+/*! \brief Number of buckets for object fields (should be prime for performance reasons) */
+#define OBJECT_FIELD_BUCKETS 29
+
+/*! \brief Thread pool for observers */
+static struct ast_threadpool *threadpool;
+
+/*! \brief Structure for internal sorcery object information */
+struct ast_sorcery_object {
+       /*! \brief Unique identifier of this object */
+       char *id;
+
+       /*! \brief Type of object */
+       char type[MAX_OBJECT_TYPE];
+
+       /*! \brief Optional object destructor */
+       ao2_destructor_fn destructor;
+
+       /*! \brief Extended object fields */
+       struct ast_variable *extended;
+};
 
 /*! \brief Structure for registered object type */
 struct ast_sorcery_object_type {
@@ -83,6 +110,30 @@ struct ast_sorcery_object_type {
 
        /*! \brief Type details */
        struct aco_type type;
+
+       /*! \brief Observers */
+       struct ao2_container *observers;
+
+       /*! \brief Serializer for observers */
+       struct ast_taskprocessor *serializer;
+
+       /*! \brief Specifies if object type is reloadable or not */
+       unsigned int reloadable:1;
+};
+
+/*! \brief Structure for registered object type observer */
+struct ast_sorcery_object_type_observer {
+       /*! \brief Pointer to the observer implementation */
+       const struct ast_sorcery_observer *callbacks;
+};
+
+/*! \brief Structure used for observer invocations */
+struct sorcery_observer_invocation {
+       /*! \brief Pointer to the object type */
+       struct ast_sorcery_object_type *object_type;
+
+       /*! \brief Pointer to the object */
+       void *object;
 };
 
 /*! \brief Structure for registered object field */
@@ -116,6 +167,8 @@ struct ast_sorcery_object_wizard {
 struct ast_sorcery {
        /*! \brief Container for known object types */
        struct ao2_container *types;
+       /*! \brief The name of the module owning this sorcery instance */
+       char module_name[0];
 };
 
 /*! \brief Structure for passing load/reload details */
@@ -131,7 +184,10 @@ struct sorcery_load_details {
 };
 
 /*! \brief Registered sorcery wizards */
-struct ao2_container *wizards;
+static struct ao2_container *wizards;
+
+/*! \brief Registered sorcery instances */
+static struct ao2_container *instances;
 
 static int int_handler_fn(const void *obj, const intptr_t *args, char **buf)
 {
@@ -175,11 +231,20 @@ static int chararray_handler_fn(const void *obj, const intptr_t *args, char **bu
        return !(*buf = ast_strdup(field)) ? -1 : 0;
 }
 
+static int codec_handler_fn(const void *obj, const intptr_t *args, char **buf)
+{
+       char tmp_buf[256];
+       struct ast_codec_pref *pref = (struct ast_codec_pref *)(obj + args[0]);
+       ast_codec_pref_string(pref, tmp_buf, sizeof(tmp_buf));
+       return !(*buf = ast_strdup(tmp_buf));
+}
+
 static sorcery_field_handler sorcery_field_default_handler(enum aco_option_type type)
 {
        switch(type) {
        case OPT_BOOL_T: return bool_handler_fn;
        case OPT_CHAR_ARRAY_T: return chararray_handler_fn;
+       case OPT_CODEC_T: return codec_handler_fn;
        case OPT_DOUBLE_T: return double_handler_fn;
        case OPT_INT_T: return int_handler_fn;
        case OPT_SOCKADDR_T: return sockaddr_handler_fn;
@@ -196,29 +261,197 @@ static sorcery_field_handler sorcery_field_default_handler(enum aco_option_type
 /*! \brief Hashing function for sorcery wizards */
 static int sorcery_wizard_hash(const void *obj, const int flags)
 {
-       const struct ast_sorcery_wizard *wizard = obj;
-       const char *name = obj;
+       const struct ast_sorcery_wizard *object;
+       const char *key;
 
-       return ast_str_hash(flags & OBJ_KEY ? name : wizard->name);
+       switch (flags & OBJ_SEARCH_MASK) {
+       case OBJ_SEARCH_KEY:
+               key = obj;
+               break;
+       case OBJ_SEARCH_OBJECT:
+               object = obj;
+               key = object->name;
+               break;
+       default:
+               ast_assert(0);
+               return 0;
+       }
+       return ast_str_hash(key);
 }
 
 /*! \brief Comparator function for sorcery wizards */
 static int sorcery_wizard_cmp(void *obj, void *arg, int flags)
 {
-       struct ast_sorcery_wizard *wizard1 = obj, *wizard2 = arg;
-       const char *name = arg;
+       const struct ast_sorcery_wizard *object_left = obj;
+       const struct ast_sorcery_wizard *object_right = arg;
+       const char *right_key = arg;
+       int cmp;
+
+       switch (flags & OBJ_SEARCH_MASK) {
+       case OBJ_SEARCH_OBJECT:
+               right_key = object_right->name;
+               /* Fall through */
+       case OBJ_SEARCH_KEY:
+               cmp = strcmp(object_left->name, right_key);
+               break;
+       case OBJ_SEARCH_PARTIAL_KEY:
+               cmp = strncmp(object_left->name, right_key, strlen(right_key));
+               break;
+       default:
+               cmp = 0;
+               break;
+       }
+       if (cmp) {
+               return 0;
+       }
+       return CMP_MATCH;
+}
+
+/*! \brief Hashing function for sorcery wizards */
+static int object_type_field_hash(const void *obj, const int flags)
+{
+       const struct ast_sorcery_object_field *object_field;
+       const char *key;
+
+       switch (flags & OBJ_SEARCH_MASK) {
+       case OBJ_SEARCH_KEY:
+               key = obj;
+               break;
+       case OBJ_SEARCH_OBJECT:
+               object_field = obj;
+               key = object_field->name;
+               break;
+       default:
+               ast_assert(0);
+               return 0;
+       }
+       return ast_str_hash(key);
+}
 
-       return !strcmp(wizard1->name, flags & OBJ_KEY ? name : wizard2->name) ? CMP_MATCH | CMP_STOP : 0;
+static int object_type_field_cmp(void *obj, void *arg, int flags)
+{
+       const struct ast_sorcery_object_field *field_left = obj;
+       const struct ast_sorcery_object_field *field_right = arg;
+       const char *right_key = arg;
+       int cmp;
+
+       switch (flags & OBJ_SEARCH_MASK) {
+       case OBJ_SEARCH_OBJECT:
+               right_key = field_right->name;
+               /* Fall through */
+       case OBJ_SEARCH_KEY:
+               cmp = strcmp(field_left->name, right_key);
+               break;
+       case OBJ_SEARCH_PARTIAL_KEY:
+               cmp = strncmp(field_left->name, right_key, strlen(right_key));
+               break;
+       default:
+               cmp = 0;
+               break;
+       }
+       if (cmp) {
+               return 0;
+       }
+       return CMP_MATCH;
+}
+
+/*! \brief Cleanup function */
+static void sorcery_exit(void)
+{
+       ast_threadpool_shutdown(threadpool);
+       threadpool = NULL;
+}
+
+/*! \brief Cleanup function for graceful shutdowns */
+static void sorcery_cleanup(void)
+{
+       ao2_cleanup(wizards);
+       wizards = NULL;
+       ao2_cleanup(instances);
+       instances = NULL;
+}
+
+/*! \brief Compare function for sorcery instances */
+static int sorcery_instance_cmp(void *obj, void *arg, int flags)
+{
+       const struct ast_sorcery *object_left = obj;
+       const struct ast_sorcery *object_right = arg;
+       const char *right_key = arg;
+       int cmp;
+
+       switch (flags & OBJ_SEARCH_MASK) {
+       case OBJ_SEARCH_OBJECT:
+               right_key = object_right->module_name;
+               /* Fall through */
+       case OBJ_SEARCH_KEY:
+               cmp = strcmp(object_left->module_name, right_key);
+               break;
+       case OBJ_SEARCH_PARTIAL_KEY:
+               cmp = strncmp(object_left->module_name, right_key, strlen(right_key));
+               break;
+       default:
+               cmp = 0;
+               break;
+       }
+       if (cmp) {
+               return 0;
+       }
+       return CMP_MATCH;
+}
+
+/*! \brief Hashing function for sorcery instances */
+static int sorcery_instance_hash(const void *obj, const int flags)
+{
+       const struct ast_sorcery *object;
+       const char *key;
+
+       switch (flags & OBJ_SEARCH_MASK) {
+       case OBJ_SEARCH_KEY:
+               key = obj;
+               break;
+       case OBJ_SEARCH_OBJECT:
+               object = obj;
+               key = object->module_name;
+               break;
+       default:
+               ast_assert(0);
+               return 0;
+       }
+       return ast_str_hash(key);
 }
 
 int ast_sorcery_init(void)
 {
+       struct ast_threadpool_options options = {
+               .version = AST_THREADPOOL_OPTIONS_VERSION,
+               .auto_increment = 1,
+               .max_size = 0,
+               .idle_timeout = 60,
+               .initial_size = 0,
+       };
        ast_assert(wizards == NULL);
 
+       if (!(threadpool = ast_threadpool_create("Sorcery", NULL, &options))) {
+               threadpool = NULL;
+               return -1;
+       }
+
        if (!(wizards = ao2_container_alloc(WIZARD_BUCKETS, sorcery_wizard_hash, sorcery_wizard_cmp))) {
+               ast_threadpool_shutdown(threadpool);
                return -1;
        }
 
+       instances = ao2_container_alloc_options(AO2_ALLOC_OPT_LOCK_RWLOCK, INSTANCE_BUCKETS,
+               sorcery_instance_hash, sorcery_instance_cmp);
+       if (!instances) {
+               sorcery_cleanup();
+               sorcery_exit();
+               return -1;
+       }
+
+       ast_register_cleanup(sorcery_cleanup);
+       ast_register_atexit(sorcery_exit);
+
        return 0;
 }
 
@@ -279,37 +512,95 @@ static void sorcery_destructor(void *obj)
 /*! \brief Hashing function for sorcery types */
 static int sorcery_type_hash(const void *obj, const int flags)
 {
-       const struct ast_sorcery_object_type *type = obj;
-       const char *name = obj;
+       const struct ast_sorcery_object_type *object;
+       const char *key;
 
-       return ast_str_hash(flags & OBJ_KEY ? name : type->name);
+       switch (flags & OBJ_SEARCH_MASK) {
+       case OBJ_SEARCH_KEY:
+               key = obj;
+               break;
+       case OBJ_SEARCH_OBJECT:
+               object = obj;
+               key = object->name;
+               break;
+       default:
+               ast_assert(0);
+               return 0;
+       }
+       return ast_str_hash(key);
 }
 
 /*! \brief Comparator function for sorcery types */
 static int sorcery_type_cmp(void *obj, void *arg, int flags)
 {
-       struct ast_sorcery_object_type *type1 = obj, *type2 = arg;
-       const char *name = arg;
-
-       return !strcmp(type1->name, flags & OBJ_KEY ? name : type2->name) ? CMP_MATCH | CMP_STOP : 0;
+       const struct ast_sorcery_object_type *object_left = obj;
+       const struct ast_sorcery_object_type *object_right = arg;
+       const char *right_key = arg;
+       int cmp;
+
+       switch (flags & OBJ_SEARCH_MASK) {
+       case OBJ_SEARCH_OBJECT:
+               right_key = object_right->name;
+               /* Fall through */
+       case OBJ_SEARCH_KEY:
+               cmp = strcmp(object_left->name, right_key);
+               break;
+       case OBJ_SEARCH_PARTIAL_KEY:
+               cmp = strncmp(object_left->name, right_key, strlen(right_key));
+               break;
+       default:
+               cmp = 0;
+               break;
+       }
+       if (cmp) {
+               return 0;
+       }
+       return CMP_MATCH;
 }
 
-struct ast_sorcery *ast_sorcery_open(void)
+struct ast_sorcery *__ast_sorcery_open(const char *module_name)
 {
        struct ast_sorcery *sorcery;
 
-       if (!(sorcery = ao2_alloc(sizeof(*sorcery), sorcery_destructor))) {
-               return NULL;
+       ast_assert(module_name != NULL);
+
+       ao2_wrlock(instances);
+       if ((sorcery = ao2_find(instances, module_name, OBJ_SEARCH_KEY | OBJ_NOLOCK))) {
+               goto done;
+       }
+
+       if (!(sorcery = ao2_alloc(sizeof(*sorcery) + strlen(module_name) + 1, sorcery_destructor))) {
+               goto done;
        }
 
        if (!(sorcery->types = ao2_container_alloc_options(AO2_ALLOC_OPT_LOCK_RWLOCK, TYPE_BUCKETS, sorcery_type_hash, sorcery_type_cmp))) {
                ao2_ref(sorcery, -1);
                sorcery = NULL;
+               goto done;
        }
 
+       strcpy(sorcery->module_name, module_name); /* Safe */
+
+       if (__ast_sorcery_apply_config(sorcery, module_name, module_name) == AST_SORCERY_APPLY_FAIL) {
+               ast_log(LOG_ERROR, "Error attempting to apply configuration %s to sorcery.\n", module_name);
+               ao2_cleanup(sorcery);
+               sorcery = NULL;
+               goto done;
+       }
+
+       ao2_link_flags(instances, sorcery, OBJ_NOLOCK);
+
+done:
+       ao2_unlock(instances);
        return sorcery;
 }
 
+/*! \brief Search function for sorcery instances */
+struct ast_sorcery *ast_sorcery_retrieve_by_module_name(const char *module_name)
+{
+       return ao2_find(instances, module_name, OBJ_SEARCH_KEY);
+}
+
 /*! \brief Destructor function for object types */
 static void sorcery_object_type_destructor(void *obj)
 {
@@ -317,6 +608,7 @@ static void sorcery_object_type_destructor(void *obj)
 
        ao2_cleanup(object_type->wizards);
        ao2_cleanup(object_type->fields);
+       ao2_cleanup(object_type->observers);
 
        if (object_type->info) {
                aco_info_destroy(object_type->info);
@@ -324,24 +616,33 @@ static void sorcery_object_type_destructor(void *obj)
        }
 
        ast_free(object_type->file);
+
+       ast_taskprocessor_unreference(object_type->serializer);
 }
 
 /*! \brief Internal function which allocates an object type structure */
 static struct ast_sorcery_object_type *sorcery_object_type_alloc(const char *type, const char *module)
 {
        struct ast_sorcery_object_type *object_type;
+       char uuid[AST_UUID_STR_LEN];
 
        if (!(object_type = ao2_alloc(sizeof(*object_type), sorcery_object_type_destructor))) {
                return NULL;
        }
 
        /* Order matters for object wizards */
-       if (!(object_type->wizards = ao2_container_alloc_options(AO2_ALLOC_OPT_LOCK_NOLOCK, 1, NULL, NULL))) {
+       if (!(object_type->wizards = ao2_container_alloc_options(AO2_ALLOC_OPT_LOCK_NOLOCK, 1, NULL, sorcery_wizard_cmp))) {
+               ao2_ref(object_type, -1);
+               return NULL;
+       }
+
+       if (!(object_type->fields = ao2_container_alloc_options(AO2_ALLOC_OPT_LOCK_NOLOCK, OBJECT_FIELD_BUCKETS,
+                                       object_type_field_hash, object_type_field_cmp))) {
                ao2_ref(object_type, -1);
                return NULL;
        }
 
-       if (!(object_type->fields = ao2_container_alloc_options(AO2_ALLOC_OPT_LOCK_NOLOCK, 1, NULL, NULL))) {
+       if (!(object_type->observers = ao2_container_alloc_options(AO2_ALLOC_OPT_LOCK_RWLOCK, 1, NULL, NULL))) {
                ao2_ref(object_type, -1);
                return NULL;
        }
@@ -356,6 +657,16 @@ static struct ast_sorcery_object_type *sorcery_object_type_alloc(const char *typ
                return NULL;
        }
 
+       if (!ast_uuid_generate_str(uuid, sizeof(uuid))) {
+               ao2_ref(object_type, -1);
+               return NULL;
+       }
+
+       if (!(object_type->serializer = ast_threadpool_serializer(uuid, threadpool))) {
+               ao2_ref(object_type, -1);
+               return NULL;
+       }
+
        object_type->info->files[0] = object_type->file;
        object_type->info->files[1] = NULL;
        object_type->info->module = module;
@@ -377,34 +688,49 @@ static void sorcery_object_wizard_destructor(void *obj)
        if (object_wizard->wizard) {
                ast_module_unref(object_wizard->wizard->module);
        }
+
+       ao2_cleanup(object_wizard->wizard);
 }
 
 /*! \brief Internal function which creates an object type and adds a wizard mapping */
-static int sorcery_apply_wizard_mapping(struct ast_sorcery *sorcery, const char *type, const char *module, const char *name, const char *data, unsigned int caching)
+static enum ast_sorcery_apply_result sorcery_apply_wizard_mapping(struct ast_sorcery *sorcery,
+               const char *type, const char *module, const char *name, const char *data, unsigned int caching)
 {
-       RAII_VAR(struct ast_sorcery_object_type *, object_type,  ao2_find(sorcery->types, type, OBJ_KEY), ao2_cleanup);
+       RAII_VAR(struct ast_sorcery_object_type *, object_type, ao2_find(sorcery->types, type, OBJ_KEY), ao2_cleanup);
        RAII_VAR(struct ast_sorcery_wizard *, wizard, ao2_find(wizards, name, OBJ_KEY), ao2_cleanup);
        RAII_VAR(struct ast_sorcery_object_wizard *, object_wizard, ao2_alloc(sizeof(*object_wizard), sorcery_object_wizard_destructor), ao2_cleanup);
        int created = 0;
 
        if (!wizard || !object_wizard) {
-               return -1;
+               return AST_SORCERY_APPLY_FAIL;
        }
 
        if (!object_type) {
                if (!(object_type = sorcery_object_type_alloc(type, module))) {
-                       return -1;
+                       return AST_SORCERY_APPLY_FAIL;
                }
                created = 1;
        }
 
+       if (!created) {
+               struct ast_sorcery_wizard *found;
+
+               found = ao2_find(object_type->wizards, wizard, OBJ_SEARCH_OBJECT);
+               if (found) {
+                       ast_debug(1, "Wizard %s already applied to object type %s\n",
+                                       wizard->name, object_type->name);
+                       ao2_cleanup(found);
+                       return AST_SORCERY_APPLY_DUPLICATE;
+               }
+       }
+
        if (wizard->open && !(object_wizard->data = wizard->open(data))) {
-               return -1;
+               return AST_SORCERY_APPLY_FAIL;
        }
 
        ast_module_ref(wizard->module);
 
-       object_wizard->wizard = wizard;
+       object_wizard->wizard = ao2_bump(wizard);
        object_wizard->caching = caching;
 
        ao2_link(object_type->wizards, object_wizard);
@@ -413,29 +739,35 @@ static int sorcery_apply_wizard_mapping(struct ast_sorcery *sorcery, const char
                ao2_link(sorcery->types, object_type);
        }
 
-       return 0;
+       return AST_SORCERY_APPLY_SUCCESS;
 }
 
-int __ast_sorcery_apply_config(struct ast_sorcery *sorcery, const char *name, const char *module)
+enum ast_sorcery_apply_result  __ast_sorcery_apply_config(struct ast_sorcery *sorcery, const char *name, const char *module)
 {
        struct ast_flags flags = { 0 };
        struct ast_config *config = ast_config_load2("sorcery.conf", "sorcery", flags);
        struct ast_variable *mapping;
-       int res = 0;
+       int res = AST_SORCERY_APPLY_SUCCESS;
 
-       if (!config || (config == CONFIG_STATUS_FILEMISSING) || (config == CONFIG_STATUS_FILEINVALID)) {
-               return -1;
+       if (!config) {
+               return AST_SORCERY_APPLY_NO_CONFIGURATION;
+       }
+
+       if (config == CONFIG_STATUS_FILEINVALID) {
+               return AST_SORCERY_APPLY_FAIL;
        }
 
        for (mapping = ast_variable_browse(config, name); mapping; mapping = mapping->next) {
                RAII_VAR(char *, mapping_name, ast_strdup(mapping->name), ast_free);
                RAII_VAR(char *, mapping_value, ast_strdup(mapping->value), ast_free);
-               char *options = mapping_name, *name = strsep(&options, "/");
-               char *data = mapping_value, *wizard = strsep(&data, ",");
+               char *options = mapping_name;
+               char *type = strsep(&options, "/");
+               char *data = mapping_value;
+               char *wizard = strsep(&data, ",");
                unsigned int caching = 0;
 
-               /* If no wizard exists just skip, nothing we can do */
-               if (ast_strlen_zero(wizard)) {
+               /* If no object type or wizard exists just skip, nothing we can do */
+               if (ast_strlen_zero(type) || ast_strlen_zero(wizard)) {
                        continue;
                }
 
@@ -445,7 +777,8 @@ int __ast_sorcery_apply_config(struct ast_sorcery *sorcery, const char *name, co
                }
 
                /* Any error immediately causes us to stop */
-               if ((res = sorcery_apply_wizard_mapping(sorcery, name, module, wizard, data, caching))) {
+               if (sorcery_apply_wizard_mapping(sorcery, type, module, wizard, data, caching) == AST_SORCERY_APPLY_FAIL) {
+                       res = AST_SORCERY_APPLY_FAIL;
                        break;
                }
        }
@@ -455,19 +788,37 @@ int __ast_sorcery_apply_config(struct ast_sorcery *sorcery, const char *name, co
        return res;
 }
 
-int __ast_sorcery_apply_default(struct ast_sorcery *sorcery, const char *type, const char *module, const char *name, const char *data)
+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)
 {
-       RAII_VAR(struct ast_sorcery_object_type *, object_type,  ao2_find(sorcery->types, type, OBJ_KEY), ao2_cleanup);
+       RAII_VAR(struct ast_sorcery_object_type *, object_type, ao2_find(sorcery->types, type, OBJ_KEY), ao2_cleanup);
 
        /* Defaults can not be added if any existing mapping exists */
        if (object_type) {
-               return -1;
+               return AST_SORCERY_APPLY_DEFAULT_UNNECESSARY;
        }
 
        return sorcery_apply_wizard_mapping(sorcery, type, module, name, data, 0);
 }
 
-int ast_sorcery_object_register(struct ast_sorcery *sorcery, const char *type, aco_type_item_alloc alloc, sorcery_transform_handler transform, sorcery_apply_handler apply)
+static int sorcery_extended_config_handler(const struct aco_option *opt, struct ast_variable *var, void *obj)
+{
+       return ast_sorcery_object_set_extended(obj, var->name, var->value);
+}
+
+static int sorcery_extended_fields_handler(const void *obj, struct ast_variable **fields)
+{
+       const struct ast_sorcery_object_details *details = obj;
+
+       if (details->object->extended) {
+               *fields = ast_variables_dup(details->object->extended);
+       } else {
+               *fields = NULL;
+       }
+
+       return 0;
+}
+
+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)
 {
        RAII_VAR(struct ast_sorcery_object_type *, object_type, ao2_find(sorcery->types, type, OBJ_KEY), ao2_cleanup);
 
@@ -475,10 +826,13 @@ int ast_sorcery_object_register(struct ast_sorcery *sorcery, const char *type, a
                return -1;
        }
 
+       object_type->type.name = object_type->name;
        object_type->type.type = ACO_ITEM;
-       object_type->type.category = "";
+       object_type->type.category = ".?";
        object_type->type.item_alloc = alloc;
+       object_type->type.hidden = hidden;
 
+       object_type->reloadable = reloadable;
        object_type->transform = transform;
        object_type->apply = apply;
        object_type->file->types[0] = &object_type->type;
@@ -488,6 +842,10 @@ int ast_sorcery_object_register(struct ast_sorcery *sorcery, const char *type, a
                return -1;
        }
 
+       if (ast_sorcery_object_fields_register(sorcery, type, "^@", sorcery_extended_config_handler, sorcery_extended_fields_handler)) {
+               return -1;
+       }
+
        return 0;
 }
 
@@ -526,20 +884,20 @@ int ast_sorcery_object_fields_register(struct ast_sorcery *sorcery, const char *
        object_field->multiple_handler = sorcery_handler;
 
        ao2_link(object_type->fields, object_field);
-       __aco_option_register(object_type->info, regex, ACO_REGEX, object_type->file->types, "", OPT_CUSTOM_T, config_handler, 0, 0);
+       __aco_option_register(object_type->info, regex, ACO_REGEX, object_type->file->types, "", OPT_CUSTOM_T, config_handler, 0, 1, 0);
 
        return 0;
 }
 
 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,
-                                       aco_option_handler config_handler, sorcery_field_handler sorcery_handler, unsigned int flags, size_t argc, ...)
+                                       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, ...)
 {
        RAII_VAR(struct ast_sorcery_object_type *, object_type, ao2_find(sorcery->types, type, OBJ_KEY), ao2_cleanup);
        RAII_VAR(struct ast_sorcery_object_field *, object_field, NULL, ao2_cleanup);
        int pos;
        va_list args;
 
-       if (!object_type || !object_type->type.item_alloc) {
+       if (!strcmp(type, "id") || !object_type || !object_type->type.item_alloc) {
                return -1;
        }
 
@@ -553,6 +911,7 @@ int __ast_sorcery_object_field_register(struct ast_sorcery *sorcery, const char
 
        ast_copy_string(object_field->name, name, sizeof(object_field->name));
        object_field->handler = sorcery_handler;
+       object_field->multiple_handler = multiple_handler;
 
        va_start(args, argc);
        for (pos = 0; pos < argc; pos++) {
@@ -560,20 +919,22 @@ int __ast_sorcery_object_field_register(struct ast_sorcery *sorcery, const char
        }
        va_end(args);
 
-       ao2_link(object_type->fields, object_field);
+       if (!alias) {
+               ao2_link(object_type->fields, object_field);
+       }
 
        /* TODO: Improve this hack */
        if (!argc) {
-               __aco_option_register(object_type->info, name, ACO_EXACT, object_type->file->types, default_val, opt_type, config_handler, flags, argc);
+               __aco_option_register(object_type->info, name, ACO_EXACT, object_type->file->types, default_val, opt_type, config_handler, flags, no_doc, argc);
        } else if (argc == 1) {
-               __aco_option_register(object_type->info, name, ACO_EXACT, object_type->file->types, default_val, opt_type, config_handler, flags, argc,
-                                     object_field->args[0]);
+               __aco_option_register(object_type->info, name, ACO_EXACT, object_type->file->types, default_val, opt_type, config_handler, flags, no_doc, argc,
+                       object_field->args[0]);
        } else if (argc == 2) {
-               __aco_option_register(object_type->info, name, ACO_EXACT, object_type->file->types, default_val, opt_type, config_handler, flags, argc,
-                                     object_field->args[0], object_field->args[1]);
+               __aco_option_register(object_type->info, name, ACO_EXACT, object_type->file->types, default_val, opt_type, config_handler, flags, no_doc, argc,
+                       object_field->args[0], object_field->args[1]);
        } else if (argc == 3) {
-               __aco_option_register(object_type->info, name, ACO_EXACT, object_type->file->types, default_val, opt_type, config_handler, flags, argc,
-                                     object_field->args[0], object_field->args[1], object_field->args[2]);
+               __aco_option_register(object_type->info, name, ACO_EXACT, object_type->file->types, default_val, opt_type, config_handler, flags, no_doc, argc,
+                       object_field->args[0], object_field->args[1], object_field->args[2]);
        } else {
                ast_assert(0); /* The hack... she does us no good for this */
        }
@@ -581,12 +942,26 @@ int __ast_sorcery_object_field_register(struct ast_sorcery *sorcery, const char
        return 0;
 }
 
+/*! \brief Retrieves whether or not the type is reloadable */
+static int sorcery_reloadable(const struct ast_sorcery *sorcery, const char *type)
+{
+       RAII_VAR(struct ast_sorcery_object_type *, object_type,
+                ao2_find(sorcery->types, type, OBJ_KEY), ao2_cleanup);
+       return object_type && object_type->reloadable;
+}
+
 static int sorcery_wizard_load(void *obj, void *arg, int flags)
 {
        struct ast_sorcery_object_wizard *wizard = obj;
        struct sorcery_load_details *details = arg;
        void (*load)(void *data, const struct ast_sorcery *sorcery, const char *type);
 
+       if (details->reload && !sorcery_reloadable(details->sorcery, details->type)) {
+               ast_log(LOG_NOTICE, "Type '%s' is not reloadable, "
+                       "maintaining previous values\n", details->type);
+               return 0;
+       }
+
        load = !details->reload ? wizard->wizard->load : wizard->wizard->reload;
 
        if (load) {
@@ -596,6 +971,58 @@ static int sorcery_wizard_load(void *obj, void *arg, int flags)
        return 0;
 }
 
+/*! \brief Destructor for observer invocation */
+static void sorcery_observer_invocation_destroy(void *obj)
+{
+       struct sorcery_observer_invocation *invocation = obj;
+
+       ao2_cleanup(invocation->object_type);
+       ao2_cleanup(invocation->object);
+}
+
+/*! \brief Allocator function for observer invocation */
+static struct sorcery_observer_invocation *sorcery_observer_invocation_alloc(struct ast_sorcery_object_type *object_type, void *object)
+{
+       struct sorcery_observer_invocation *invocation = ao2_alloc(sizeof(*invocation), sorcery_observer_invocation_destroy);
+
+       if (!invocation) {
+               return NULL;
+       }
+
+       ao2_ref(object_type, +1);
+       invocation->object_type = object_type;
+
+       if (object) {
+               ao2_ref(object, +1);
+               invocation->object = object;
+       }
+
+       return invocation;
+}
+
+/*! \brief Internal callback function which notifies an individual observer that an object type has been loaded */
+static int sorcery_observer_notify_loaded(void *obj, void *arg, int flags)
+{
+       const struct ast_sorcery_object_type_observer *observer = obj;
+
+       if (observer->callbacks->loaded) {
+               observer->callbacks->loaded(arg);
+       }
+
+       return 0;
+}
+
+/*! \brief Internal callback function which notifies observers that an object type has been loaded */
+static int sorcery_observers_notify_loaded(void *data)
+{
+       struct sorcery_observer_invocation *invocation = data;
+
+       ao2_callback(invocation->object_type->observers, OBJ_NODATA, sorcery_observer_notify_loaded, invocation->object_type->name);
+       ao2_cleanup(invocation);
+
+       return 0;
+}
+
 static int sorcery_object_load(void *obj, void *arg, int flags)
 {
        struct ast_sorcery_object_type *type = obj;
@@ -604,6 +1031,14 @@ static int sorcery_object_load(void *obj, void *arg, int flags)
        details->type = type->name;
        ao2_callback(type->wizards, OBJ_NODATA, sorcery_wizard_load, details);
 
+       if (ao2_container_count(type->observers)) {
+               struct sorcery_observer_invocation *invocation = sorcery_observer_invocation_alloc(type, NULL);
+
+               if (invocation && ast_taskprocessor_push(type->serializer, sorcery_observers_notify_loaded, invocation)) {
+                       ao2_cleanup(invocation);
+               }
+       }
+
        return 0;
 }
 
@@ -662,14 +1097,48 @@ void ast_sorcery_ref(struct ast_sorcery *sorcery)
        ao2_ref(sorcery, +1);
 }
 
-struct ast_variable *ast_sorcery_objectset_create(const struct ast_sorcery *sorcery, const void *object)
+static struct ast_variable *get_single_field_as_var_list(const void *object, struct ast_sorcery_object_field *object_field)
+{
+       struct ast_variable *tmp = NULL;
+       char *buf = NULL;
+
+       if (!object_field->handler) {
+               return NULL;
+       }
+
+       if (!(object_field->handler(object, object_field->args, &buf))) {
+               tmp = ast_variable_new(object_field->name, S_OR(buf, ""), "");
+       }
+       ast_free(buf);
+
+       return tmp;
+}
+
+static struct ast_variable *get_multiple_fields_as_var_list(const void *object, struct ast_sorcery_object_field *object_field)
+{
+       struct ast_variable *tmp = NULL;
+
+       if (!object_field->multiple_handler) {
+               return NULL;
+       }
+
+       if (object_field->multiple_handler(object, &tmp)) {
+               ast_variables_destroy(tmp);
+               tmp = NULL;
+       }
+
+       return tmp;
+}
+
+struct ast_variable *ast_sorcery_objectset_create2(const struct ast_sorcery *sorcery,
+       const void *object,     enum ast_sorcery_field_handler_flags flags)
 {
        const struct ast_sorcery_object_details *details = object;
-       RAII_VAR(struct ast_sorcery_object_type *, object_type, ao2_find(sorcery->types, details->type, OBJ_KEY), ao2_cleanup);
+       RAII_VAR(struct ast_sorcery_object_type *, object_type, ao2_find(sorcery->types, details->object->type, OBJ_KEY), ao2_cleanup);
        struct ao2_iterator i;
        struct ast_sorcery_object_field *object_field;
-       struct ast_variable *fields = NULL;
-       int res = 0;
+       struct ast_variable *head = NULL;
+       struct ast_variable *tail = NULL;
 
        if (!object_type) {
                return NULL;
@@ -677,18 +1146,87 @@ struct ast_variable *ast_sorcery_objectset_create(const struct ast_sorcery *sorc
 
        i = ao2_iterator_init(object_type->fields, 0);
 
-       for (; (object_field = ao2_iterator_next(&i)) && !res; ao2_ref(object_field, -1)) {
-               struct ast_variable *tmp = NULL;
+       for (; (object_field = ao2_iterator_next(&i)); ao2_ref(object_field, -1)) {
+               struct ast_variable *tmp;
+
+               switch (flags) {
+               case AST_HANDLER_PREFER_LIST:
+                       if ((tmp = get_multiple_fields_as_var_list(object, object_field)) ||
+                               (tmp = get_single_field_as_var_list(object, object_field))) {
+                               break;
+                       }
+                       continue;
+               case AST_HANDLER_PREFER_STRING:
+                       if ((tmp = get_single_field_as_var_list(object, object_field)) ||
+                               (tmp = get_multiple_fields_as_var_list(object, object_field))) {
+                               break;
+                       }
+                       continue;
+               case AST_HANDLER_ONLY_LIST:
+                       if ((tmp = get_multiple_fields_as_var_list(object, object_field))) {
+                               break;
+                       }
+                       continue;
+               case AST_HANDLER_ONLY_STRING:
+                       if ((tmp = get_single_field_as_var_list(object, object_field))) {
+                               break;
+                       }
+                       continue;
+               default:
+                       continue;
+               }
+
+               tail = ast_variable_list_append_hint(&head, tail, tmp);
+       }
+
+       ao2_iterator_destroy(&i);
+
+       return head;
+}
+
+struct ast_json *ast_sorcery_objectset_json_create(const struct ast_sorcery *sorcery, const void *object)
+{
+       const struct ast_sorcery_object_details *details = object;
+       RAII_VAR(struct ast_sorcery_object_type *, object_type, ao2_find(sorcery->types, details->object->type, OBJ_KEY), ao2_cleanup);
+       struct ao2_iterator i;
+       struct ast_sorcery_object_field *object_field;
+       struct ast_json *json = ast_json_object_create();
+       int res = 0;
+
+       if (!object_type || !json) {
+               return NULL;
+       }
+
+       i = ao2_iterator_init(object_type->fields, 0);
 
+       for (; !res && (object_field = ao2_iterator_next(&i)); ao2_ref(object_field, -1)) {
                if (object_field->multiple_handler) {
+                       struct ast_variable *tmp = NULL;
+                       struct ast_variable *field;
+
                        if ((res = object_field->multiple_handler(object, &tmp))) {
                                ast_variables_destroy(tmp);
+                               ao2_ref(object_field, -1);
+                               break;
+                       }
+
+                       for (field = tmp; field; field = field->next) {
+                               struct ast_json *value = ast_json_string_create(field->value);
+
+                               if (!value || ast_json_object_set(json, field->name, value)) {
+                                       res = -1;
+                                       break;
+                               }
                        }
+
+                       ast_variables_destroy(tmp);
                } else if (object_field->handler) {
                        char *buf = NULL;
+                       struct ast_json *value = NULL;
 
-                       if ((res = object_field->handler(object, object_field->args, &buf)) ||
-                               !(tmp = ast_variable_new(object_field->name, S_OR(buf, ""), ""))) {
+                       if ((res = object_field->handler(object, object_field->args, &buf))
+                               || !(value = ast_json_string_create(buf))
+                               || ast_json_object_set(json, object_field->name, value)) {
                                res = -1;
                        }
 
@@ -696,28 +1234,23 @@ struct ast_variable *ast_sorcery_objectset_create(const struct ast_sorcery *sorc
                } else {
                        continue;
                }
-
-               if (!res) {
-                       tmp->next = fields;
-                       fields = tmp;
-               }
        }
 
        ao2_iterator_destroy(&i);
 
-       /* If any error occurs we destroy all fields handled before so a partial objectset is not returned */
+       /* If any error occurs we destroy the JSON object so a partial objectset is not returned */
        if (res) {
-               ast_variables_destroy(fields);
-               fields = NULL;
+               ast_json_unref(json);
+               json = NULL;
        }
 
-       return fields;
+       return json;
 }
 
 int ast_sorcery_objectset_apply(const struct ast_sorcery *sorcery, void *object, struct ast_variable *objectset)
 {
        const struct ast_sorcery_object_details *details = object;
-       RAII_VAR(struct ast_sorcery_object_type *, object_type, ao2_find(sorcery->types, details->type, OBJ_KEY), ao2_cleanup);
+       RAII_VAR(struct ast_sorcery_object_type *, object_type, ao2_find(sorcery->types, details->object->type, OBJ_KEY), ao2_cleanup);
        RAII_VAR(struct ast_variable *, transformed, NULL, ast_variables_destroy);
        struct ast_variable *field;
        int res = 0;
@@ -733,13 +1266,13 @@ int ast_sorcery_objectset_apply(const struct ast_sorcery *sorcery, void *object,
        }
 
        for (; field; field = field->next) {
-               if ((res = aco_process_var(&object_type->type, details->id, field, object))) {
+               if ((res = aco_process_var(&object_type->type, details->object->id, field, object))) {
                        break;
                }
        }
 
        if (!res && object_type->apply) {
-               object_type->apply(sorcery, object);
+               res = object_type->apply(sorcery, object);
        }
 
        return res;
@@ -796,23 +1329,53 @@ int ast_sorcery_changeset_create(const struct ast_variable *original, const stru
        return res;
 }
 
+static void sorcery_object_destructor(void *object)
+{
+       struct ast_sorcery_object_details *details = object;
+
+       if (details->object->destructor) {
+               details->object->destructor(object);
+       }
+
+       ast_variables_destroy(details->object->extended);
+       ast_free(details->object->id);
+}
+
+void *ast_sorcery_generic_alloc(size_t size, ao2_destructor_fn destructor)
+{
+       void *object = ao2_alloc_options(size + sizeof(struct ast_sorcery_object), sorcery_object_destructor, AO2_ALLOC_OPT_LOCK_NOLOCK);
+       struct ast_sorcery_object_details *details = object;
+
+       if (!object) {
+               return NULL;
+       }
+
+       details->object = object + size;
+       details->object->destructor = destructor;
+
+       return object;
+}
+
 void *ast_sorcery_alloc(const struct ast_sorcery *sorcery, const char *type, const char *id)
 {
        RAII_VAR(struct ast_sorcery_object_type *, object_type, ao2_find(sorcery->types, type, OBJ_KEY), ao2_cleanup);
        struct ast_sorcery_object_details *details;
 
        if (!object_type || !object_type->type.item_alloc ||
-           !(details = object_type->type.item_alloc(""))) {
+               !(details = object_type->type.item_alloc(id))) {
                return NULL;
        }
 
        if (ast_strlen_zero(id)) {
-               ast_uuid_generate_str(details->id, sizeof(details->id));
+               char uuid[AST_UUID_STR_LEN];
+
+               ast_uuid_generate_str(uuid, sizeof(uuid));
+               details->object->id = ast_strdup(uuid);
        } else {
-               ast_copy_string(details->id, id, sizeof(details->id));
+               details->object->id = ast_strdup(id);
        }
 
-       ast_copy_string(details->type, type, sizeof(details->type));
+       ast_copy_string(details->object->type, type, sizeof(details->object->type));
 
        if (aco_set_defaults(&object_type->type, id, details)) {
                ao2_ref(details, -1);
@@ -825,8 +1388,8 @@ void *ast_sorcery_alloc(const struct ast_sorcery *sorcery, const char *type, con
 void *ast_sorcery_copy(const struct ast_sorcery *sorcery, const void *object)
 {
        const struct ast_sorcery_object_details *details = object;
-       RAII_VAR(struct ast_sorcery_object_type *, object_type, ao2_find(sorcery->types, details->type, OBJ_KEY), ao2_cleanup);
-       struct ast_sorcery_object_details *copy = ast_sorcery_alloc(sorcery, details->type, details->id);
+       RAII_VAR(struct ast_sorcery_object_type *, object_type, ao2_find(sorcery->types, details->object->type, OBJ_KEY), ao2_cleanup);
+       struct ast_sorcery_object_details *copy = ast_sorcery_alloc(sorcery, details->object->type, details->object->id);
        RAII_VAR(struct ast_variable *, objectset, NULL, ast_variables_destroy);
        int res = 0;
 
@@ -912,7 +1475,7 @@ void *ast_sorcery_retrieve_by_id(const struct ast_sorcery *sorcery, const char *
        i = ao2_iterator_init(object_type->wizards, 0);
        for (; (wizard = ao2_iterator_next(&i)); ao2_ref(wizard, -1)) {
                if (wizard->wizard->retrieve_id &&
-                   !(object = wizard->wizard->retrieve_id(sorcery, wizard->data, object_type->name, id))) {
+                       !(object = wizard->wizard->retrieve_id(sorcery, wizard->data, object_type->name, id))) {
                        continue;
                }
 
@@ -921,7 +1484,7 @@ void *ast_sorcery_retrieve_by_id(const struct ast_sorcery *sorcery, const char *
                ao2_ref(wizard, -1);
                break;
        }
-        ao2_iterator_destroy(&i);
+       ao2_iterator_destroy(&i);
 
        if (!cached && object) {
                ao2_callback(object_type->wizards, 0, sorcery_cache_create, object);
@@ -1014,10 +1577,33 @@ static int sorcery_wizard_create(void *obj, void *arg, int flags)
        return (!object_wizard->caching && !object_wizard->wizard->create(details->sorcery, object_wizard->data, details->obj)) ? CMP_MATCH | CMP_STOP : 0;
 }
 
+/*! \brief Internal callback function which notifies an individual observer that an object has been created */
+static int sorcery_observer_notify_create(void *obj, void *arg, int flags)
+{
+       const struct ast_sorcery_object_type_observer *observer = obj;
+
+       if (observer->callbacks->created) {
+               observer->callbacks->created(arg);
+       }
+
+       return 0;
+}
+
+/*! \brief Internal callback function which notifies observers that an object has been created */
+static int sorcery_observers_notify_create(void *data)
+{
+       struct sorcery_observer_invocation *invocation = data;
+
+       ao2_callback(invocation->object_type->observers, OBJ_NODATA, sorcery_observer_notify_create, invocation->object);
+       ao2_cleanup(invocation);
+
+       return 0;
+}
+
 int ast_sorcery_create(const struct ast_sorcery *sorcery, void *object)
 {
        const struct ast_sorcery_object_details *details = object;
-       RAII_VAR(struct ast_sorcery_object_type *, object_type, ao2_find(sorcery->types, details->type, OBJ_KEY), ao2_cleanup);
+       RAII_VAR(struct ast_sorcery_object_type *, object_type, ao2_find(sorcery->types, details->object->type, OBJ_KEY), ao2_cleanup);
        RAII_VAR(struct ast_sorcery_object_wizard *, object_wizard, NULL, ao2_cleanup);
        struct sorcery_details sdetails = {
                .sorcery = sorcery,
@@ -1028,11 +1614,41 @@ int ast_sorcery_create(const struct ast_sorcery *sorcery, void *object)
                return -1;
        }
 
-       object_wizard = ao2_callback(object_type->wizards, 0, sorcery_wizard_create, &sdetails);
+       if ((object_wizard = ao2_callback(object_type->wizards, 0, sorcery_wizard_create, &sdetails)) &&
+               ao2_container_count(object_type->observers)) {
+               struct sorcery_observer_invocation *invocation = sorcery_observer_invocation_alloc(object_type, object);
+
+               if (invocation && ast_taskprocessor_push(object_type->serializer, sorcery_observers_notify_create, invocation)) {
+                       ao2_cleanup(invocation);
+               }
+       }
 
        return object_wizard ? 0 : -1;
 }
 
+/*! \brief Internal callback function which notifies an individual observer that an object has been updated */
+static int sorcery_observer_notify_update(void *obj, void *arg, int flags)
+{
+       const struct ast_sorcery_object_type_observer *observer = obj;
+
+       if (observer->callbacks->updated) {
+               observer->callbacks->updated(arg);
+       }
+
+       return 0;
+}
+
+/*! \brief Internal callback function which notifies observers that an object has been updated */
+static int sorcery_observers_notify_update(void *data)
+{
+       struct sorcery_observer_invocation *invocation = data;
+
+       ao2_callback(invocation->object_type->observers, OBJ_NODATA, sorcery_observer_notify_update, invocation->object);
+       ao2_cleanup(invocation);
+
+       return 0;
+}
+
 /*! \brief Internal function which returns if a wizard has updated the object */
 static int sorcery_wizard_update(void *obj, void *arg, int flags)
 {
@@ -1046,7 +1662,7 @@ static int sorcery_wizard_update(void *obj, void *arg, int flags)
 int ast_sorcery_update(const struct ast_sorcery *sorcery, void *object)
 {
        const struct ast_sorcery_object_details *details = object;
-       RAII_VAR(struct ast_sorcery_object_type *, object_type, ao2_find(sorcery->types, details->type, OBJ_KEY), ao2_cleanup);
+       RAII_VAR(struct ast_sorcery_object_type *, object_type, ao2_find(sorcery->types, details->object->type, OBJ_KEY), ao2_cleanup);
        RAII_VAR(struct ast_sorcery_object_wizard *, object_wizard, NULL, ao2_cleanup);
        struct sorcery_details sdetails = {
                .sorcery = sorcery,
@@ -1057,11 +1673,41 @@ int ast_sorcery_update(const struct ast_sorcery *sorcery, void *object)
                return -1;
        }
 
-       object_wizard = ao2_callback(object_type->wizards, 0, sorcery_wizard_update, &sdetails);
+       if ((object_wizard = ao2_callback(object_type->wizards, 0, sorcery_wizard_update, &sdetails)) &&
+               ao2_container_count(object_type->observers)) {
+               struct sorcery_observer_invocation *invocation = sorcery_observer_invocation_alloc(object_type, object);
+
+               if (invocation && ast_taskprocessor_push(object_type->serializer, sorcery_observers_notify_update, invocation)) {
+                       ao2_cleanup(invocation);
+               }
+       }
 
        return object_wizard ? 0 : -1;
 }
 
+/*! \brief Internal callback function which notifies an individual observer that an object has been deleted */
+static int sorcery_observer_notify_delete(void *obj, void *arg, int flags)
+{
+       const struct ast_sorcery_object_type_observer *observer = obj;
+
+       if (observer->callbacks->deleted) {
+               observer->callbacks->deleted(arg);
+       }
+
+       return 0;
+}
+
+/*! \brief Internal callback function which notifies observers that an object has been deleted */
+static int sorcery_observers_notify_delete(void *data)
+{
+       struct sorcery_observer_invocation *invocation = data;
+
+       ao2_callback(invocation->object_type->observers, OBJ_NODATA, sorcery_observer_notify_delete, invocation->object);
+       ao2_cleanup(invocation);
+
+       return 0;
+}
+
 /*! \brief Internal function which returns if a wizard has deleted the object */
 static int sorcery_wizard_delete(void *obj, void *arg, int flags)
 {
@@ -1075,7 +1721,7 @@ static int sorcery_wizard_delete(void *obj, void *arg, int flags)
 int ast_sorcery_delete(const struct ast_sorcery *sorcery, void *object)
 {
        const struct ast_sorcery_object_details *details = object;
-       RAII_VAR(struct ast_sorcery_object_type *, object_type, ao2_find(sorcery->types, details->type, OBJ_KEY), ao2_cleanup);
+       RAII_VAR(struct ast_sorcery_object_type *, object_type, ao2_find(sorcery->types, details->object->type, OBJ_KEY), ao2_cleanup);
        RAII_VAR(struct ast_sorcery_object_wizard *, object_wizard, NULL, ao2_cleanup);
        struct sorcery_details sdetails = {
                .sorcery = sorcery,
@@ -1086,24 +1732,209 @@ int ast_sorcery_delete(const struct ast_sorcery *sorcery, void *object)
                return -1;
        }
 
-       object_wizard = ao2_callback(object_type->wizards, 0, sorcery_wizard_delete, &sdetails);
+       if ((object_wizard = ao2_callback(object_type->wizards, 0, sorcery_wizard_delete, &sdetails)) &&
+               ao2_container_count(object_type->observers)) {
+               struct sorcery_observer_invocation *invocation = sorcery_observer_invocation_alloc(object_type, object);
+
+               if (invocation && ast_taskprocessor_push(object_type->serializer, sorcery_observers_notify_delete, invocation)) {
+                       ao2_cleanup(invocation);
+               }
+       }
 
        return object_wizard ? 0 : -1;
 }
 
 void ast_sorcery_unref(struct ast_sorcery *sorcery)
 {
-       ao2_cleanup(sorcery);
+       if (sorcery) {
+               /* One ref for what we just released, the other for the instances container. */
+               ao2_wrlock(instances);
+               if (ao2_ref(sorcery, -1) == 2) {
+                       ao2_unlink_flags(instances, sorcery, OBJ_NOLOCK);
+               }
+               ao2_unlock(instances);
+       }
 }
 
 const char *ast_sorcery_object_get_id(const void *object)
 {
        const struct ast_sorcery_object_details *details = object;
-       return details->id;
+       return details->object->id;
 }
 
 const char *ast_sorcery_object_get_type(const void *object)
 {
        const struct ast_sorcery_object_details *details = object;
-       return details->type;
+       return details->object->type;
+}
+
+const char *ast_sorcery_object_get_extended(const void *object, const char *name)
+{
+       const struct ast_sorcery_object_details *details = object;
+       struct ast_variable *field;
+
+       for (field = details->object->extended; field; field = field->next) {
+               if (!strcmp(field->name + 1, name)) {
+                       return field->value;
+               }
+       }
+
+       return NULL;
+}
+
+int ast_sorcery_object_set_extended(const void *object, const char *name, const char *value)
+{
+       RAII_VAR(struct ast_variable *, field, NULL, ast_variables_destroy);
+       struct ast_variable *extended = ast_variable_new(name, value, ""), *previous = NULL;
+       const struct ast_sorcery_object_details *details = object;
+
+       if (!extended) {
+               return -1;
+       }
+
+       for (field = details->object->extended; field; previous = field, field = field->next) {
+               if (!strcmp(field->name, name)) {
+                       if (previous) {
+                               previous->next = field->next;
+                       } else {
+                               details->object->extended = field->next;
+                       }
+                       field->next = NULL;
+                       break;
+               }
+       }
+
+       extended->next = details->object->extended;
+       details->object->extended = extended;
+
+       return 0;
+}
+
+int ast_sorcery_observer_add(const struct ast_sorcery *sorcery, const char *type, const struct ast_sorcery_observer *callbacks)
+{
+       RAII_VAR(struct ast_sorcery_object_type *, object_type, ao2_find(sorcery->types, type, OBJ_KEY), ao2_cleanup);
+       struct ast_sorcery_object_type_observer *observer;
+       int res;
+
+       if (!object_type || !callbacks) {
+               return -1;
+       }
+
+       if (!(observer = ao2_alloc(sizeof(*observer), NULL))) {
+               return -1;
+       }
+
+       observer->callbacks = callbacks;
+       res = 0;
+       if (!ao2_link(object_type->observers, observer)) {
+               res = -1;
+       }
+       ao2_ref(observer, -1);
+
+       return res;
+}
+
+/*! \brief Internal callback function for removing an observer */
+static int sorcery_observer_remove(void *obj, void *arg, int flags)
+{
+       const struct ast_sorcery_object_type_observer *observer = obj;
+
+       return (observer->callbacks == arg) ? CMP_MATCH | CMP_STOP : 0;
+}
+
+void ast_sorcery_observer_remove(const struct ast_sorcery *sorcery, const char *type, const struct ast_sorcery_observer *callbacks)
+{
+       RAII_VAR(struct ast_sorcery_object_type *, object_type, NULL, ao2_cleanup);
+       struct ast_sorcery_observer *cbs = (struct ast_sorcery_observer *) callbacks;/* Remove const for traversal. */
+
+       if (!sorcery) {
+               return;
+       }
+       object_type = ao2_find(sorcery->types, type, OBJ_KEY);
+       if (!object_type) {
+               return;
+       }
+
+       ao2_callback(object_type->observers, OBJ_NODATA | OBJ_UNLINK,
+               sorcery_observer_remove, cbs);
+}
+
+int ast_sorcery_object_id_sort(const void *obj, const void *arg, int flags)
+{
+       const char *right_key = arg;
+       int cmp;
+
+       switch (flags & OBJ_SEARCH_MASK) {
+       case OBJ_SEARCH_OBJECT:
+               right_key = ast_sorcery_object_get_id(arg);
+               /* Fall through */
+       case OBJ_SEARCH_KEY:
+               cmp = strcmp(ast_sorcery_object_get_id(obj), right_key);
+               break;
+       case OBJ_SEARCH_PARTIAL_KEY:
+               cmp = strncmp(ast_sorcery_object_get_id(obj), right_key, strlen(right_key));
+               break;
+       default:
+               cmp = 0;
+               break;
+       }
+       return cmp;
+}
+
+int ast_sorcery_object_id_compare(void *obj, void *arg, int flags)
+{
+       const char *right_key = arg;
+       int cmp = 0;
+
+       switch (flags & OBJ_SEARCH_MASK) {
+       case OBJ_SEARCH_OBJECT:
+               right_key = ast_sorcery_object_get_id(arg);
+               /* Fall through */
+       case OBJ_SEARCH_KEY:
+               if (strcmp(ast_sorcery_object_get_id(obj), right_key) == 0) {
+                       cmp = CMP_MATCH | CMP_STOP;
+               }
+               break;
+       case OBJ_SEARCH_PARTIAL_KEY:
+               if (strncmp(ast_sorcery_object_get_id(obj), right_key, strlen(right_key)) == 0) {
+                       cmp = CMP_MATCH;
+               }
+               break;
+       default:
+               cmp = 0;
+               break;
+       }
+       return cmp;
+}
+
+int ast_sorcery_object_id_hash(const void *obj, int flags) {
+       if (flags & OBJ_SEARCH_OBJECT) {
+               return ast_str_hash(ast_sorcery_object_get_id(obj));
+       } else if (flags & OBJ_SEARCH_KEY) {
+               return ast_str_hash(obj);
+       }
+       return -1;
+}
+
+struct ast_sorcery_object_type *ast_sorcery_get_object_type(const struct ast_sorcery *sorcery,
+               const char *type)
+{
+       return ao2_find(sorcery->types, type, OBJ_SEARCH_KEY);
+}
+
+int ast_sorcery_is_object_field_registered(const struct ast_sorcery_object_type *object_type,
+               const char *field_name)
+{
+       struct ast_sorcery_object_field *object_field;
+       int res = 1;
+
+       ast_assert(object_type != NULL);
+
+       object_field = ao2_find(object_type->fields, field_name, OBJ_SEARCH_KEY);
+       if (!object_field) {
+               res = 0;
+       }
+
+       ao2_cleanup(object_field);
+       return res;
 }