2 * Asterisk -- An open source telephony toolkit.
4 * Copyright (C) 2013, Digium, Inc.
6 * Joshua Colp <jcolp@digium.com>
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.
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.
22 * \brief Sorcery Astdb Object Wizard
24 * \author Joshua Colp <jcolp@digium.com>
28 <support_level>core</support_level>
33 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
37 #include "asterisk/module.h"
38 #include "asterisk/sorcery.h"
39 #include "asterisk/astdb.h"
40 #include "asterisk/json.h"
42 static void *sorcery_astdb_open(const char *data);
43 static int sorcery_astdb_create(const struct ast_sorcery *sorcery, void *data, void *object);
44 static void *sorcery_astdb_retrieve_id(const struct ast_sorcery *sorcery, void *data, const char *type, const char *id);
45 static void *sorcery_astdb_retrieve_fields(const struct ast_sorcery *sorcery, void *data, const char *type, const struct ast_variable *fields);
46 static void sorcery_astdb_retrieve_multiple(const struct ast_sorcery *sorcery, void *data, const char *type, struct ao2_container *objects,
47 const struct ast_variable *fields);
48 static void sorcery_astdb_retrieve_regex(const struct ast_sorcery *sorcery, void *data, const char *type, struct ao2_container *objects, const char *regex);
49 static int sorcery_astdb_update(const struct ast_sorcery *sorcery, void *data, void *object);
50 static int sorcery_astdb_delete(const struct ast_sorcery *sorcery, void *data, void *object);
51 static void sorcery_astdb_close(void *data);
53 static struct ast_sorcery_wizard astdb_object_wizard = {
55 .open = sorcery_astdb_open,
56 .create = sorcery_astdb_create,
57 .retrieve_id = sorcery_astdb_retrieve_id,
58 .retrieve_fields = sorcery_astdb_retrieve_fields,
59 .retrieve_multiple = sorcery_astdb_retrieve_multiple,
60 .retrieve_regex = sorcery_astdb_retrieve_regex,
61 .update = sorcery_astdb_update,
62 .delete = sorcery_astdb_delete,
63 .close = sorcery_astdb_close,
66 /*! \brief Helper function which converts from a sorcery object set to a json object */
67 static struct ast_json *sorcery_objectset_to_json(const struct ast_variable *objectset)
69 struct ast_json *json = ast_json_object_create();
70 const struct ast_variable *field;
72 for (field = objectset; field; field = field->next) {
73 struct ast_json *value = ast_json_string_create(field->value);
78 } else if (ast_json_object_set(json, field->name, value)) {
79 ast_json_unref(value);
88 /*! \brief Helper function which converts a json object to a sorcery object set */
89 static struct ast_variable *sorcery_json_to_objectset(struct ast_json *json)
91 struct ast_json_iter *field;
92 struct ast_variable *objset = NULL;
94 for (field = ast_json_object_iter(json); field; field = ast_json_object_iter_next(json, field)) {
95 struct ast_json *value = ast_json_object_iter_value(field);
96 struct ast_variable *variable = ast_variable_new(ast_json_object_iter_key(field), ast_json_string_get(value), "");
99 ast_variables_destroy(objset);
103 variable->next = objset;
110 /*! \brief Helper function which compares two json objects and sees if they are equal, but only looks at the criteria provided */
111 static int sorcery_json_equal(struct ast_json *object, struct ast_json *criteria)
113 struct ast_json_iter *field;
115 for (field = ast_json_object_iter(criteria); field; field = ast_json_object_iter_next(criteria, field)) {
116 struct ast_json *object_field = ast_json_object_get(object, ast_json_object_iter_key(field));
118 if (!object_field || !ast_json_equal(object_field, ast_json_object_iter_value(field))) {
126 static int sorcery_astdb_create(const struct ast_sorcery *sorcery, void *data, void *object)
128 RAII_VAR(struct ast_json *, objset, ast_sorcery_objectset_json_create(sorcery, object), ast_json_unref);
129 RAII_VAR(char *, value, NULL, ast_json_free);
130 const char *prefix = data;
131 char family[strlen(prefix) + strlen(ast_sorcery_object_get_type(object)) + 2];
133 if (!objset || !(value = ast_json_dump_string(objset))) {
137 snprintf(family, sizeof(family), "%s/%s", prefix, ast_sorcery_object_get_type(object));
139 return ast_db_put(family, ast_sorcery_object_get_id(object), value);
142 /*! \brief Internal helper function which retrieves an object, or multiple objects, using fields for criteria */
143 static void *sorcery_astdb_retrieve_fields_common(const struct ast_sorcery *sorcery, void *data, const char *type, const struct ast_variable *fields, struct ao2_container *objects)
145 const char *prefix = data;
146 char family[strlen(prefix) + strlen(type) + 2];
147 RAII_VAR(struct ast_db_entry *, entries, NULL, ast_db_freetree);
148 RAII_VAR(struct ast_json *, criteria, NULL, ast_json_unref);
149 struct ast_db_entry *entry;
151 snprintf(family, sizeof(family), "%s/%s", prefix, type);
153 if (!(entries = ast_db_gettree(family, NULL)) || (fields && !(criteria = sorcery_objectset_to_json(fields)))) {
157 for (entry = entries; entry; entry = entry->next) {
158 const char *key = entry->key + strlen(family) + 2;
159 RAII_VAR(struct ast_json *, json, NULL, ast_json_unref);
160 struct ast_json_error error;
161 RAII_VAR(struct ast_variable *, objset, NULL, ast_variables_destroy);
164 if (!(json = ast_json_load_string(entry->data, &error))) {
166 } else if (criteria && !sorcery_json_equal(json, criteria)) {
168 } else if (!(objset = sorcery_json_to_objectset(json)) ||
169 !(object = ast_sorcery_alloc(sorcery, type, key)) ||
170 ast_sorcery_objectset_apply(sorcery, object, objset)) {
179 ao2_link(objects, object);
186 static void *sorcery_astdb_retrieve_fields(const struct ast_sorcery *sorcery, void *data, const char *type, const struct ast_variable *fields)
188 return sorcery_astdb_retrieve_fields_common(sorcery, data, type, fields, NULL);
191 static void *sorcery_astdb_retrieve_id(const struct ast_sorcery *sorcery, void *data, const char *type, const char *id)
193 const char *prefix = data;
194 char family[strlen(prefix) + strlen(type) + 2];
195 RAII_VAR(char *, value, NULL, ast_free_ptr);
196 RAII_VAR(struct ast_json *, json, NULL, ast_json_unref);
197 struct ast_json_error error;
198 RAII_VAR(struct ast_variable *, objset, NULL, ast_variables_destroy);
201 snprintf(family, sizeof(family), "%s/%s", prefix, type);
203 if (ast_db_get_allocated(family, id, &value) || !(json = ast_json_load_string(value, &error)) ||
204 !(objset = sorcery_json_to_objectset(json)) || !(object = ast_sorcery_alloc(sorcery, type, id)) ||
205 ast_sorcery_objectset_apply(sorcery, object, objset)) {
213 static void sorcery_astdb_retrieve_multiple(const struct ast_sorcery *sorcery, void *data, const char *type, struct ao2_container *objects, const struct ast_variable *fields)
215 sorcery_astdb_retrieve_fields_common(sorcery, data, type, fields, objects);
220 * \brief Convert regex prefix pattern to astDB prefix pattern if possible.
222 * \param tree astDB prefix pattern buffer to fill.
223 * \param regex Extended regular expression with the start anchor character '^'.
225 * \note Since this is a helper function, the tree buffer is
226 * assumed to always be large enough.
228 * \retval 0 on success.
229 * \retval -1 on error. regex is invalid.
231 static int make_astdb_prefix_pattern(char *tree, const char *regex)
236 for (dst = tree, src = regex + 1; *src; ++src) {
238 /* Escaped regex char. */
241 /* Invalid regex. The caller escaped the string terminator. */
244 } else if (*src == '$') {
246 /* Remove the tail anchor character. */
250 } else if (strchr(".?*+{[(|", *src)) {
252 * The regex is not a simple prefix pattern.
254 * XXX With more logic, it is possible to simply
255 * use the current prefix pattern. The last character
256 * needs to be removed if possible when the current regex
257 * token is "?*{". Also the rest of the regex pattern
258 * would need to be checked for subgroup/alternation.
259 * Subgroup/alternation is too complex for a simple prefix
274 static void sorcery_astdb_retrieve_regex(const struct ast_sorcery *sorcery, void *data, const char *type, struct ao2_container *objects, const char *regex)
276 const char *prefix = data;
277 char family[strlen(prefix) + strlen(type) + 2];
278 char tree[strlen(regex) + 1];
279 RAII_VAR(struct ast_db_entry *, entries, NULL, ast_db_freetree);
281 struct ast_db_entry *entry;
283 snprintf(family, sizeof(family), "%s/%s", prefix, type);
285 if (regex[0] == '^') {
287 * For performance reasons, try to create an astDB prefix
288 * pattern from the regex to reduce the number of entries
289 * retrieved from astDB for regex to then match.
291 if (make_astdb_prefix_pattern(tree, regex)) {
298 if (!(entries = ast_db_gettree(family, tree))
299 || regcomp(&expression, regex, REG_EXTENDED | REG_NOSUB)) {
303 for (entry = entries; entry; entry = entry->next) {
304 /* The key in the entry includes the family, so we need to strip it out for regex purposes */
305 const char *key = entry->key + strlen(family) + 2;
306 RAII_VAR(struct ast_json *, json, NULL, ast_json_unref);
307 struct ast_json_error error;
308 RAII_VAR(void *, object, NULL, ao2_cleanup);
309 RAII_VAR(struct ast_variable *, objset, NULL, ast_variables_destroy);
311 if (regexec(&expression, key, 0, NULL, 0)) {
313 } else if (!(json = ast_json_load_string(entry->data, &error)) ||
314 !(objset = sorcery_json_to_objectset(json)) ||
315 !(object = ast_sorcery_alloc(sorcery, type, key)) ||
316 ast_sorcery_objectset_apply(sorcery, object, objset)) {
317 regfree(&expression);
321 ao2_link(objects, object);
324 regfree(&expression);
327 static int sorcery_astdb_update(const struct ast_sorcery *sorcery, void *data, void *object)
329 const char *prefix = data;
330 char family[strlen(prefix) + strlen(ast_sorcery_object_get_type(object)) + 2], value[2];
332 snprintf(family, sizeof(family), "%s/%s", prefix, ast_sorcery_object_get_type(object));
334 /* It is okay for the value to be truncated, we are only checking that it exists */
335 if (ast_db_get(family, ast_sorcery_object_get_id(object), value, sizeof(value))) {
339 /* The only difference between update and create is that for update the object must already exist */
340 return sorcery_astdb_create(sorcery, data, object);
343 static int sorcery_astdb_delete(const struct ast_sorcery *sorcery, void *data, void *object)
345 const char *prefix = data;
346 char family[strlen(prefix) + strlen(ast_sorcery_object_get_type(object)) + 2];
349 snprintf(family, sizeof(family), "%s/%s", prefix, ast_sorcery_object_get_type(object));
351 if (ast_db_get(family, ast_sorcery_object_get_id(object), value, sizeof(value))) {
355 return ast_db_del(family, ast_sorcery_object_get_id(object));
358 static void *sorcery_astdb_open(const char *data)
360 /* We require a prefix for family string generation, or else stuff could mix together */
361 if (ast_strlen_zero(data)) {
365 return ast_strdup(data);
368 static void sorcery_astdb_close(void *data)
373 static int load_module(void)
375 if (ast_sorcery_wizard_register(&astdb_object_wizard)) {
376 return AST_MODULE_LOAD_DECLINE;
379 return AST_MODULE_LOAD_SUCCESS;
382 static int unload_module(void)
384 ast_sorcery_wizard_unregister(&astdb_object_wizard);
388 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_GLOBAL_SYMBOLS | AST_MODFLAG_LOAD_ORDER, "Sorcery Astdb Object Wizard",
390 .unload = unload_module,
391 .load_pri = AST_MODPRI_REALTIME_DRIVER,