d4d62bfdb616eda1153c50808f9a451d612aeea8
[asterisk/asterisk.git] / include / asterisk / sorcery.h
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  * \brief Sorcery Data Access Layer API
21  * \author Joshua Colp <jcolp@digium.com>
22  * \ref AstSorcery
23  */
24
25 /*!
26  * \page AstSorcery Data Access Layer API
27  *
28  * Sorcery is a unifying data access layer which utilizes the configuration framework,
29  * realtime, and astdb to allow object creation, retrieval, updating, and deletion.
30  *
31  * \par Initialization
32  *
33  * Usage of sorcery is accomplished by first opening a sorcery structure. This structure holds
34  * all information about the object types, object fields, and object mappings. All API functions
35  * require the sorcery structure to operate. When sorcery is no longer needed the structure can
36  * be unreferenced using \ref ast_sorcery_unref
37  *
38  * Once opened the sorcery structure must have object mappings applied to it. This maps the
39  * object types to their respective wizards (object storage modules). If the developer would like
40  * to allow the user to configure this using the sorcery.conf configuration file the
41  * \ref ast_sorcery_apply_config API call can be used to read in the configuration file and apply the
42  * mappings. If the storage of the object types are such that a default wizard can be used this can
43  * be applied using the \ref ast_sorcery_apply_default API call. Note that the default mappings will not
44  * override configured mappings. They are only used in the case where no configured mapping exists.
45  *
46  * Configuring object mappings implicitly creates a basic version of an object type. The object type
47  * must be fully registered, however, using the \ref ast_sorcery_object_register API call before any
48  * objects of the type can be allocated, created, or retrieved.
49  *
50  * Once the object type itself has been fully registered the individual fields within the object must
51  * be registered using the \ref ast_sorcery_object_field_register API call. Note that not all fields *need*
52  * be registered. Only fields that should be accessible using the sorcery API have to be registered.
53  *
54  * \par Creating Objects
55  *
56  * Before an object can be created within the sorcery API it must first be allocated using the
57  * \ref ast_sorcery_alloc API call. This allocates a new instance of the object, sets sorcery specific
58  * details, and applies default values to the object. A unique identifier can optionally be specified
59  * when allocating an object. If it is not provided one will be automatically generated. Allocating
60  * an object does not create it within any object storage mechanisms that are configured for the
61  * object type. Creation must explicitly be done using the \ref ast_sorcery_create API call. This API call
62  * passes the object to each configured object storage mechanism for the object type until one
63  * successfully persists the object.
64  *
65  * \par Retrieving Objects
66  *
67  * To retrieve a single object using its unique identifier the \ref ast_sorcery_retrieve_by_id API call
68  * can be used.
69  *
70  * To retrieve potentially multiple objects using specific fields the \ref ast_sorcery_retrieve_by_fields
71  * API call can be used. The behavior of this API call is controlled using different flags. If the
72  * AST_RETRIEVE_FLAG_MULTIPLE flag is used a container will be returned which contains all matching objects.
73  * To retrieve all objects the AST_RETRIEVE_FLAG_ALL flag can be specified. Note that when specifying this flag
74  * you do not need to pass any fields.
75  *
76  * Both API calls return shared objects. Modification of the object can not occur until it has been copied.
77  *
78  * \par Updating Objects
79  *
80  * As retrieved objects may be shared the first step to updating the object with new details is creating a
81  * copy using the \ref ast_sorcery_copy API call. This will return a new object which is specific to the caller.
82  * Any field within the object may be modified as needed. Once changes are done the changes can be committed
83  * using the \ref ast_sorcery_update API call. Note that as the copied object is specific to the caller it must
84  * be unreferenced after use.
85  *
86  * \par Deleting Objects
87  *
88  * To delete an object simply call the \ref ast_sorcery_delete API call with an object retrieved using the
89  * ast_sorcery_retrieve_by_* API calls or a copy returned from \ref ast_sorcery_copy.
90  */
91
92 #ifndef _ASTERISK_SORCERY_H
93 #define _ASTERISK_SORCERY_H
94
95 #if defined(__cplusplus) || defined(c_plusplus)
96 extern "C" {
97 #endif
98
99 #include "asterisk/config_options.h"
100 #include "asterisk/uuid.h"
101
102 /*! \brief Maximum size of an object type */
103 #define MAX_OBJECT_TYPE 64
104
105 /*! \brief Maximum length of an object field name */
106 #define MAX_OBJECT_FIELD 128
107
108 /*!
109  * \brief Retrieval flags
110  */
111 enum ast_sorcery_retrieve_flags {
112         /*! \brief Default retrieval flags */
113         AST_RETRIEVE_FLAG_DEFAULT = 0,
114
115         /*! \brief Return all matching objects */
116         AST_RETRIEVE_FLAG_MULTIPLE = (1 << 0),
117
118         /*! \brief Perform no matching, return all objects */
119         AST_RETRIEVE_FLAG_ALL = (1 << 1),
120 };
121
122 /*!
123  * \brief Field handler flags
124  */
125 enum ast_sorcery_field_handler_flags {
126         /*! \brief Try both handlers, string first */
127         AST_HANDLER_PREFER_STRING,
128
129         /*! \brief Try both handlers, list first */
130         AST_HANDLER_PREFER_LIST,
131
132         /*! \brief Use string handler only */
133         AST_HANDLER_ONLY_STRING,
134
135         /*! \brief Use list handler only */
136         AST_HANDLER_ONLY_LIST,
137 };
138
139
140 /*! \brief Forward declaration for the sorcery main structure */
141 struct ast_sorcery;
142
143 /*!
144  * \brief A callback function for translating a value into a string
145  *
146  * \param obj Object to get value from
147  * \param args Where the field is
148  * \param buf Pointer to the buffer that the handler has created which contains the field value
149  *
150  * \retval 0 success
151  * \retval -1 failure
152  */
153 typedef int (*sorcery_field_handler)(const void *obj, const intptr_t *args, char **buf);
154
155 /*!
156  * \brief A callback function for translating multiple values into an ast_variable list
157  *
158  * \param obj Object to get values from
159  * \param fields Pointer to store the list of fields
160  *
161  * \retval 0 success
162  * \retval -1 failure
163  */
164 typedef int (*sorcery_fields_handler)(const void *obj, struct ast_variable **fields);
165
166 /*!
167  * \brief A callback function for performing a transformation on an object set
168  *
169  * \param set The existing object set
170  *
171  * \retval non-NULL new object set if changed
172  * \retval NULL if no changes present
173  *
174  * \note The returned ast_variable list must be *new*. You can not return the input set.
175  */
176 typedef struct ast_variable *(*sorcery_transform_handler)(struct ast_variable *set);
177
178 /*!
179  * \brief A callback function for when an object set is successfully applied to an object
180  *
181  * \note On a failure return, the state of the object is left undefined. It is a bad
182  * idea to try to use this object.
183  *
184  * \param sorcery Sorcery structure in use
185  * \param obj The object itself
186  * \retval 0 Success
187  * \retval non-zero Failure
188  */
189 typedef int (*sorcery_apply_handler)(const struct ast_sorcery *sorcery, void *obj);
190
191 /*!
192  * \brief A callback function for copying the contents of one object to another
193  *
194  * \param src The source object
195  * \param dst The destination object
196  *
197  * \retval 0 success
198  * \retval -1 failure
199  */
200 typedef int (*sorcery_copy_handler)(const void *src, void *dst);
201
202 /*!
203  * \brief A callback function for generating a changeset between two objects
204  *
205  * \param original The original object
206  * \param modified The modified object
207  * \param changes The changeset
208  *
209  * \param 0 success
210  * \param -1 failure
211  */
212 typedef int (*sorcery_diff_handler)(const void *original, const void *modified, struct ast_variable **changes);
213
214 /*! \brief Interface for a sorcery wizard */
215 struct ast_sorcery_wizard {
216         /*! \brief Name of the wizard */
217         const char *name;
218
219         /*! \brief Pointer to the Asterisk module this wizard is implemented by */
220         struct ast_module *module;
221
222         /*! \brief Callback for opening a wizard */
223         void *(*open)(const char *data);
224
225         /*! \brief Optional callback for loading persistent objects */
226         void (*load)(void *data, const struct ast_sorcery *sorcery, const char *type);
227
228         /*! \brief Optional callback for reloading persistent objects */
229         void (*reload)(void *data, const struct ast_sorcery *sorcery, const char *type);
230
231         /*! \brief Callback for creating an object */
232         int (*create)(const struct ast_sorcery *sorcery, void *data, void *object);
233
234         /*! \brief Callback for retrieving an object using an id */
235         void *(*retrieve_id)(const struct ast_sorcery *sorcery, void *data, const char *type, const char *id);
236
237         /*! \brief Callback for retrieving multiple objects using a regex on their id */
238         void (*retrieve_regex)(const struct ast_sorcery *sorcery, void *data, const char *type, struct ao2_container *objects, const char *regex);
239
240         /*! \brief Optional callback for retrieving an object using fields */
241         void *(*retrieve_fields)(const struct ast_sorcery *sorcery, void *data, const char *type, const struct ast_variable *fields);
242
243         /*! \brief Optional callback for retrieving multiple objects using some optional field criteria */
244         void (*retrieve_multiple)(const struct ast_sorcery *sorcery, void *data, const char *type, struct ao2_container *objects, const struct ast_variable *fields);
245
246         /*! \brief Callback for updating an object */
247         int (*update)(const struct ast_sorcery *sorcery, void *data, void *object);
248
249         /*! \brief Callback for deleting an object */
250         int (*delete)(const struct ast_sorcery *sorcery, void *data, void *object);
251
252         /*! \brief Callback for closing a wizard */
253         void (*close)(void *data);
254 };
255
256 /*! \brief Interface for a sorcery object type observer */
257 struct ast_sorcery_observer {
258         /*! \brief Callback for when an object is created */
259         void (*created)(const void *object);
260
261         /*! \brief Callback for when an object is updated */
262         void (*updated)(const void *object);
263
264         /*! \brief Callback for when an object is deleted */
265         void (*deleted)(const void *object);
266
267         /*! \brief Callback for when an object type is loaded/reloaded */
268         void (*loaded)(const char *object_type);
269 };
270
271 /*! \brief Opaque structure for internal sorcery object */
272 struct ast_sorcery_object;
273
274 /*! \brief Structure which contains details about a sorcery object */
275 struct ast_sorcery_object_details {
276         /*! \brief Pointer to internal sorcery object information */
277         struct ast_sorcery_object *object;
278 };
279
280 /*! \brief Macro which must be used at the beginning of each sorcery capable object */
281 #define SORCERY_OBJECT(details)                    \
282 struct {                                           \
283         struct ast_sorcery_object_details details; \
284 }                                                  \
285
286 /*!
287  * \brief Initialize the sorcery API
288  *
289  * \retval 0 success
290  * \retval -1 failure
291  */
292 int ast_sorcery_init(void);
293
294 /*!
295  * \brief Register a sorcery wizard
296  *
297  * \param interface Pointer to a wizard interface
298  * \param module Pointer to the module implementing the interface
299  *
300  * \retval 0 success
301  * \retval -1 failure
302  */
303 int __ast_sorcery_wizard_register(const struct ast_sorcery_wizard *interface, struct ast_module *module);
304
305 /*!
306  * \brief See \ref __ast_sorcery_wizard_register()
307  */
308 #define ast_sorcery_wizard_register(interface) __ast_sorcery_wizard_register(interface, ast_module_info ? ast_module_info->self : NULL)
309
310 /*!
311  * \brief Unregister a sorcery wizard
312  *
313  * \param interface Pointer to the wizard interface
314  *
315  * \retval 0 success
316  * \retval -1 failure
317  */
318 int ast_sorcery_wizard_unregister(const struct ast_sorcery_wizard *interface);
319
320 /*!
321  * \brief Open a new sorcery structure
322  *
323  * \param module The module name (AST_MODULE)
324  *
325  * \retval non-NULL success
326  * \retval NULL if allocation failed
327  */
328 struct ast_sorcery *__ast_sorcery_open(const char *module);
329
330 #define ast_sorcery_open() __ast_sorcery_open(AST_MODULE)
331
332 /*!
333  * \brief Retrieves an existing sorcery instance by module name
334  *
335  * \param module The module name
336  *
337  * \retval non-NULL success
338  * \retval NULL if no instance was found
339  *
340  * \note The returned instance has its reference count incremented.  The caller
341  * must decrement the count when they're finished with it.
342  *
343  */
344 struct ast_sorcery *ast_sorcery_retrieve_by_module_name(const char *module);
345
346 /*!
347  * \brief Apply configured wizard mappings
348  *
349  * \param sorcery Pointer to a sorcery structure
350  * \param name Name of the category to use within the configuration file, normally the module name
351  * \param module The module name (AST_MODULE)
352  *
353  * \retval 0 success
354  * \retval -1 failure
355  */
356 int __ast_sorcery_apply_config(struct ast_sorcery *sorcery, const char *name, const char *module);
357
358 #define ast_sorcery_apply_config(sorcery, name) \
359         __ast_sorcery_apply_config((sorcery), (name), AST_MODULE)
360
361 /*!
362  * \brief Apply default object wizard mappings
363  *
364  * \param sorcery Pointer to a sorcery structure
365  * \param type Type of object to apply to
366  * \param module The name of the module, typically AST_MODULE
367  * \param name Name of the wizard to use
368  * \param data Data to be passed to wizard
369  *
370  * \retval 0 success
371  * \retval -1 failure
372  *
373  * \note This should be called *after* applying configuration sourced mappings
374  *
375  * \note Only a single default can exist per object type
376  */
377 int __ast_sorcery_apply_default(struct ast_sorcery *sorcery, const char *type, const char *module, const char *name, const char *data);
378
379 #define ast_sorcery_apply_default(sorcery, type, name, data) \
380         __ast_sorcery_apply_default((sorcery), (type), AST_MODULE, (name), (data))
381
382 /*!
383  * \brief Register an object type
384  *
385  * \param sorcery Pointer to a sorcery structure
386  * \param type Type of object
387  * \param hidden All objects of this type are internal and should not be manipulated by users
388  * \param reloadable All objects of this type are reloadable
389  * \param alloc Required object allocation callback
390  * \param transform Optional transformation callback
391  * \param apply Optional object set apply callback
392  *
393  * \note In general, this function should not be used directly. One of the various
394  * macro'd versions should be used instead.
395  *
396  * \retval 0 success
397  * \retval -1 failure
398  */
399 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);
400
401 /*!
402  * \brief Register an object type
403  *
404  * \param sorcery Pointer to a sorcery structure
405  * \param type Type of object
406  * \param alloc Required object allocation callback
407  * \param transform Optional transformation callback
408  * \param apply Optional object set apply callback
409  *
410  * \retval 0 success
411  * \retval -1 failure
412  */
413 #define ast_sorcery_object_register(sorcery, type, alloc, transform, apply) \
414         __ast_sorcery_object_register((sorcery), (type), 0, 1, (alloc), (transform), (apply))
415
416 /*!
417  * \brief Register an object type that is not reloadable
418  *
419  * \param sorcery Pointer to a sorcery structure
420  * \param type Type of object
421  * \param alloc Required object allocation callback
422  * \param transform Optional transformation callback
423  * \param apply Optional object set apply callback
424  *
425  * \retval 0 success
426  * \retval -1 failure
427  */
428 #define ast_sorcery_object_register_no_reload(sorcery, type, alloc, transform, apply) \
429         __ast_sorcery_object_register((sorcery), (type), 0, 0, (alloc), (transform), (apply))
430
431 /*!
432  * \brief Register an internal, hidden object type
433  *
434  * \param sorcery Pointer to a sorcery structure
435  * \param type Type of object
436  * \param alloc Required object allocation callback
437  * \param transform Optional transformation callback
438  * \param apply Optional object set apply callback
439  *
440  * \retval 0 success
441  * \retval -1 failure
442  */
443 #define ast_sorcery_internal_object_register(sorcery, type, alloc, transform, apply) \
444         __ast_sorcery_object_register((sorcery), (type), 1, 1, (alloc), (transform), (apply))
445
446 /*!
447  * \brief Set the copy handler for an object type
448  *
449  * \param sorcery Pointer to a sorcery structure
450  * \param type Type of object
451  * \param copy Copy handler
452  */
453 void ast_sorcery_object_set_copy_handler(struct ast_sorcery *sorcery, const char *type, sorcery_copy_handler copy);
454
455 /*!
456  * \brief Set the diff handler for an object type
457  *
458  * \param sorcery Pointer to a sorcery structure
459  * \param type Type of object
460  * \param diff Diff handler
461  */
462 void ast_sorcery_object_set_diff_handler(struct ast_sorcery *sorcery, const char *type, sorcery_diff_handler diff);
463
464 /*!
465  * \brief Register a regex for multiple fields within an object
466  *
467  * \param sorcery Pointer to a sorcery structure
468  * \param type Type of object
469  * \param regex A regular expression pattern for the fields
470  * \param config_handler A custom handler for translating the string representation of the fields
471  * \param sorcery_handler A custom handler for translating the native representation of the fields
472  *
473  * \retval 0 success
474  * \retval -1 failure
475  */
476 int ast_sorcery_object_fields_register(struct ast_sorcery *sorcery, const char *type, const char *regex, aco_option_handler config_handler,
477                                                                            sorcery_fields_handler sorcery_handler);
478
479 /*!
480  * \brief Register a field within an object
481  *
482  * \param sorcery Pointer to a sorcery structure
483  * \param type Type of object
484  * \param name Name of the field
485  * \param default_val Default value of the field
486  * \param config_handler A custom handler for translating the string representation of the fields
487  * \param sorcery_handler A custom handler for translating the native representation of the fields
488  * \param multiple_handler A custom handler for translating the native representation of the fields
489  * \param opt_type Option type
490  * \param flags Option type specific flags
491  *
492  * \retval 0 success
493  * \retval -1 failure
494  */
495 int __ast_sorcery_object_field_register(struct ast_sorcery *sorcery, const char *type,
496         const char *name, const char *default_val, enum aco_option_type opt_type,
497         aco_option_handler config_handler, sorcery_field_handler sorcery_handler,
498         sorcery_fields_handler multiple_handler, unsigned int flags, unsigned int no_doc, size_t argc, ...);
499
500 /*!
501  * \brief Register a field within an object
502  *
503  * \param sorcery Pointer to a sorcery structure
504  * \param type Type of object
505  * \param name Name of the field
506  * \param default_val Default value of the field
507  * \param opt_type Option type
508  * \param flags Option type specific flags
509  *
510  * \retval 0 success
511  * \retval -1 failure
512  */
513 #define ast_sorcery_object_field_register(sorcery, type, name, default_val, opt_type, flags, ...) \
514     __ast_sorcery_object_field_register(sorcery, type, name, default_val, opt_type, NULL, NULL, NULL, flags, 0, VA_NARGS(__VA_ARGS__), __VA_ARGS__)
515
516 /*!
517  * \brief Register a field within an object without documentation
518  *
519  * \param sorcery Pointer to a sorcery structure
520  * \param type Type of object
521  * \param name Name of the field
522  * \param default_val Default value of the field
523  * \param opt_type Option type
524  * \param flags Option type specific flags
525  *
526  * \retval 0 success
527  * \retval -1 failure
528  */
529 #define ast_sorcery_object_field_register_nodoc(sorcery, type, name, default_val, opt_type, flags, ...) \
530     __ast_sorcery_object_field_register(sorcery, type, name, default_val, opt_type, NULL, NULL, NULL, flags, 1, VA_NARGS(__VA_ARGS__), __VA_ARGS__)
531
532 /*!
533  * \brief Register a field within an object with custom handlers
534  *
535  * \param sorcery Pointer to a sorcery structure
536  * \param type Type of object
537  * \param name Name of the field
538  * \param default_val Default value of the field
539  * \param config_handler Custom configuration handler
540  * \param sorcery_handler Custom sorcery handler
541  * \param multiple_handler Custom multiple handler
542  * \param flags Option type specific flags
543  *
544  * \retval 0 success
545  * \retval -1 failure
546  */
547 #define ast_sorcery_object_field_register_custom(sorcery, type, name, default_val, config_handler, sorcery_handler, multiple_handler, flags, ...) \
548     __ast_sorcery_object_field_register(sorcery, type, name, default_val, OPT_CUSTOM_T, config_handler, sorcery_handler, multiple_handler, flags, 0, VA_NARGS(__VA_ARGS__), __VA_ARGS__);
549
550 /*!
551  * \brief Register a field within an object with custom handlers without documentation
552  *
553  * \param sorcery Pointer to a sorcery structure
554  * \param type Type of object
555  * \param name Name of the field
556  * \param default_val Default value of the field
557  * \param config_handler Custom configuration handler
558  * \param sorcery_handler Custom sorcery handler
559  * \param multiple_handler Custom multiple handler
560  * \param flags Option type specific flags
561  *
562  * \retval 0 success
563  * \retval -1 failure
564  */
565 #define ast_sorcery_object_field_register_custom_nodoc(sorcery, type, name, default_val, config_handler, sorcery_handler, multiple_handler, flags, ...) \
566     __ast_sorcery_object_field_register(sorcery, type, name, default_val, OPT_CUSTOM_T, config_handler, sorcery_handler, multiple_handler, flags, 1, VA_NARGS(__VA_ARGS__), __VA_ARGS__);
567
568 /*!
569  * \brief Inform any wizards to load persistent objects
570  *
571  * \param sorcery Pointer to a sorcery structure
572  */
573 void ast_sorcery_load(const struct ast_sorcery *sorcery);
574
575 /*!
576  * \brief Inform any wizards of a specific object type to load persistent objects
577  *
578  * \param sorcery Pointer to a sorcery structure
579  * \param type Name of the object type to load
580  */
581 void ast_sorcery_load_object(const struct ast_sorcery *sorcery, const char *type);
582
583 /*!
584  * \brief Inform any wizards to reload persistent objects
585  *
586  * \param sorcery Pointer to a sorcery structure
587  */
588 void ast_sorcery_reload(const struct ast_sorcery *sorcery);
589
590 /*!
591  * \brief Inform any wizards of a specific object type to reload persistent objects
592  *
593  * \param sorcery Pointer to a sorcery structure
594  * \param type Name of the object type to reload
595  */
596 void ast_sorcery_reload_object(const struct ast_sorcery *sorcery, const char *type);
597
598 /*!
599  * \brief Increase the reference count of a sorcery structure
600  *
601  * \param sorcery Pointer to a sorcery structure
602  */
603 void ast_sorcery_ref(struct ast_sorcery *sorcery);
604
605
606 /*!
607  * \brief Create an object set (KVP list) for an object
608  *
609  * \param sorcery Pointer to a sorcery structure
610  * \param object Pointer to a sorcery object
611  * \param flags Flags indicating which handler to use and in what order.
612  *
613  * \retval non-NULL success
614  * \retval NULL if error occurred
615  *
616  * \note The returned ast_variable list must be destroyed using ast_variables_destroy
617  */
618 struct ast_variable *ast_sorcery_objectset_create2(const struct ast_sorcery *sorcery,
619         const void *object, enum ast_sorcery_field_handler_flags flags);
620
621 /*!
622  * \brief Create an object set (KVP list) for an object
623  *
624  * \param sorcery Pointer to a sorcery structure
625  * \param object Pointer to a sorcery object
626  *
627  * \retval non-NULL success
628  * \retval NULL if error occurred
629  *
630  * \note The returned ast_variable list must be destroyed using ast_variables_destroy
631  *
632  * \note This function attempts to use a field's sorcery_fields_handler first and if that
633  * doesn't exist or fails, a field's sorcery_field_handler is used.  The difference is
634  * that the former may return multiple list entries for the same field and the latter will only
635  * return 1.  It's up to the field itself to determine what the appropriate content is.
636  */
637 #define ast_sorcery_objectset_create(sorcery, object) \
638         ast_sorcery_objectset_create2(sorcery, object, AST_HANDLER_PREFER_LIST)
639
640 /*!
641  * \brief Create an object set in JSON format for an object
642  *
643  * \param sorcery Pointer to a sorcery structure
644  * \param object Pointer to a sorcery object
645  *
646  * \retval non-NULL success
647  * \retval NULL if error occurred
648  *
649  * \note The returned ast_json object must be unreferenced using ast_json_unref
650  */
651 struct ast_json *ast_sorcery_objectset_json_create(const struct ast_sorcery *sorcery, const void *object);
652
653 /*!
654  * \brief Apply an object set (KVP list) to an object
655  *
656  * \param sorcery Pointer to a sorcery structure
657  * \param object Pointer to a sorcery object
658  * \param objectset Object set itself
659  *
660  * \retval 0 success
661  * \retval -1 failure
662  *
663  * \note This operation is *not* atomic. If this fails it is possible for the object to be left with a partially
664  *       applied object set.
665  */
666 int ast_sorcery_objectset_apply(const struct ast_sorcery *sorcery, void *object, struct ast_variable *objectset);
667
668 /*!
669  * \brief Create a changeset given two object sets
670  *
671  * \param original Original object set
672  * \param modified Modified object set
673  * \param changes Pointer to hold any changes between the object sets
674  *
675  * \retval 0 success
676  * \retval -1 failure
677  *
678  * \note The returned ast_variable list must be destroyed using ast_variables_destroy
679  */
680 int ast_sorcery_changeset_create(const struct ast_variable *original, const struct ast_variable *modified, struct ast_variable **changes);
681
682 /*!
683  * \brief Allocate a generic sorcery capable object
684  *
685  * \param size Size of the object
686  * \param destructor Optional destructor function
687  *
688  * \retval non-NULL success
689  * \retval NULL failure
690  */
691 void *ast_sorcery_generic_alloc(size_t size, ao2_destructor_fn destructor);
692
693 /*!
694  * \brief Allocate an object
695  *
696  * \param sorcery Pointer to a sorcery structure
697  * \param type Type of object to allocate
698  * \param id Optional unique identifier, if none is provided one will be generated
699  *
700  * \retval non-NULL success
701  * \retval NULL failure
702  */
703 void *ast_sorcery_alloc(const struct ast_sorcery *sorcery, const char *type, const char *id);
704
705 /*!
706  * \brief Create a copy of an object
707  *
708  * \param sorcery Pointer to a sorcery structure
709  * \param object Existing object
710  *
711  * \retval non-NULL success
712  * \retval NULL failure
713  */
714 void *ast_sorcery_copy(const struct ast_sorcery *sorcery, const void *object);
715
716 /*!
717  * \brief Create a changeset of two objects
718  *
719  * \param sorcery Pointer to a sorcery structure
720  * \param original Original object
721  * \param modified Modified object
722  * \param changes Pointer which will be populated with changes if any exist
723  *
724  * \retval 0 success
725  * \retval -1 failure
726  *
727  * \note The returned ast_variable list must be destroyed using ast_variables_destroy
728  *
729  * \note While the objects must be of the same type they do not have to be the same object
730  */
731 int ast_sorcery_diff(const struct ast_sorcery *sorcery, const void *original, const void *modified, struct ast_variable **changes);
732
733 /*!
734  * \brief Add an observer to a specific object type
735  *
736  * \param sorcery Pointer to a sorcery structure
737  * \param type Type of object that should be observed
738  * \param callbacks Implementation of the observer interface
739  *
740  * \retval 0 success
741  * \retval -1 failure
742  *
743  * \note You must be ready to accept observer invocations before this function is called
744  */
745 int ast_sorcery_observer_add(const struct ast_sorcery *sorcery, const char *type, const struct ast_sorcery_observer *callbacks);
746
747 /*!
748  * \brief Remove an observer from a specific object type
749  *
750  * \param sorcery Pointer to a sorcery structure
751  * \param type Type of object that should no longer be observed
752  * \param callbacks Implementation of the observer interface
753  *
754  * \retval 0 success
755  * \retval -1 failure
756  */
757 void ast_sorcery_observer_remove(const struct ast_sorcery *sorcery, const char *type, const struct ast_sorcery_observer *callbacks);
758
759 /*!
760  * \brief Create and potentially persist an object using an available wizard
761  *
762  * \param sorcery Pointer to a sorcery structure
763  * \param object Pointer to a sorcery object
764  *
765  * \retval 0 success
766  * \retval -1 failure
767  */
768 int ast_sorcery_create(const struct ast_sorcery *sorcery, void *object);
769
770 /*!
771  * \brief Retrieve an object using its unique identifier
772  *
773  * \param sorcery Pointer to a sorcery structure
774  * \param type Type of object to retrieve
775  * \param id Unique object identifier
776  *
777  * \retval non-NULL if found
778  * \retval NULL if not found
779  */
780 void *ast_sorcery_retrieve_by_id(const struct ast_sorcery *sorcery, const char *type, const char *id);
781
782 /*!
783  * \brief Retrieve an object or multiple objects using specific fields
784  *
785  * \param sorcery Pointer to a sorcery structure
786  * \param type Type of object to retrieve
787  * \param flags Flags to control behavior
788  * \param fields Optional object fields and values to match against
789  *
790  * \retval non-NULL if found
791  * \retval NULL if not found
792  *
793  * \note If the AST_RETRIEVE_FLAG_MULTIPLE flag is specified the returned value will be an
794  *       ao2_container that must be unreferenced after use.
795  *
796  * \note If the AST_RETRIEVE_FLAG_ALL flag is used you may omit fields to retrieve all objects
797  *       of the given type.
798  */
799 void *ast_sorcery_retrieve_by_fields(const struct ast_sorcery *sorcery, const char *type, unsigned int flags, struct ast_variable *fields);
800
801 /*!
802  * \brief Retrieve multiple objects using a regular expression on their id
803  *
804  * \param sorcery Pointer to a sorcery structure
805  * \param type Type of object to retrieve
806  * \param regex Regular expression
807  *
808  * \retval non-NULL if error occurs
809  * \retval NULL success
810  *
811  * \note The provided regex is treated as extended case sensitive.
812  */
813 struct ao2_container *ast_sorcery_retrieve_by_regex(const struct ast_sorcery *sorcery, const char *type, const char *regex);
814
815 /*!
816  * \brief Update an object
817  *
818  * \param sorcery Pointer to a sorcery structure
819  * \param object Pointer to a sorcery object
820  *
821  * \retval 0 success
822  * \retval -1 failure
823  */
824 int ast_sorcery_update(const struct ast_sorcery *sorcery, void *object);
825
826 /*!
827  * \brief Delete an object
828  *
829  * \param sorcery Pointer to a sorcery structure
830  * \param object Pointer to a sorcery object
831  *
832  * \retval 0 success
833  * \retval -1 failure
834  */
835 int ast_sorcery_delete(const struct ast_sorcery *sorcery, void *object);
836
837 /*!
838  * \brief Decrease the reference count of a sorcery structure
839  *
840  * \param sorcery Pointer to a sorcery structure
841  */
842 void ast_sorcery_unref(struct ast_sorcery *sorcery);
843
844 /*!
845  * \brief Get the unique identifier of a sorcery object
846  *
847  * \param object Pointer to a sorcery object
848  *
849  * \retval unique identifier
850  */
851 const char *ast_sorcery_object_get_id(const void *object);
852
853 /*!
854  * \brief Get the type of a sorcery object
855  *
856  * \param object Pointer to a sorcery object
857  *
858  * \retval type of object
859  */
860 const char *ast_sorcery_object_get_type(const void *object);
861
862 /*!
863  * \brief Get an extended field value from a sorcery object
864  *
865  * \param object Pointer to a sorcery object
866  * \param name Name of the extended field value
867  *
868  * \retval non-NULL if found
869  * \retval NULL if not found
870  *
871  * \note The returned string does NOT need to be freed and is guaranteed to remain valid for the lifetime of the object
872  */
873 const char *ast_sorcery_object_get_extended(const void *object, const char *name);
874
875 /*!
876  * \brief Set an extended field value on a sorcery object
877  *
878  * \param object Pointer to a sorcery object
879  * \param name Name of the extended field
880  * \param value Value of the extended field
881  *
882  * \retval 0 success
883  * \retval -1 failure
884  *
885  * \note The field name MUST begin with '@' to indicate it is an extended field.
886  * \note If the extended field already exists it will be overwritten with the new value.
887  */
888 int ast_sorcery_object_set_extended(const void *object, const char *name, const char *value);
889
890 /*!
891  * \brief Sorcery object comparator based on id.
892  */
893 int ast_sorcery_object_id_compare(const void *obj_left, const void *obj_right, int flags);
894
895
896 #if defined(__cplusplus) || defined(c_plusplus)
897 }
898 #endif
899
900 #endif /* _ASTERISK_SORCERY_H */