summaryrefslogtreecommitdiff
path: root/res/res_ari_model.c
diff options
context:
space:
mode:
authorDavid M. Lee <dlee@digium.com>2013-07-03 16:32:41 +0000
committerDavid M. Lee <dlee@digium.com>2013-07-03 16:32:41 +0000
commitc9a3d4562ddb1ed5b34f7d5530efd6aa695377c2 (patch)
treedd082285fbb5c7714164e26145acc5c966e663be /res/res_ari_model.c
parentdcf03554a0b38806bf1fe258acc423b070533d6e (diff)
Update events to use Swagger 1.3 subtyping, and related aftermath
This patch started with the simple idea of changing the /events data model to be more sane. The original model would send out events like: { "stasis_start": { "args": [], "channel": { ... } } } The event discriminator was the field name instead of being a value in the object, due to limitations in how Swagger 1.1 could model objects. While technically sufficient in communicating event information, it was really difficult to deal with in terms of client side JSON handling. This patch takes advantage of a proposed extension[1] to Swagger which allows type variance through the use of a discriminator field. This had a domino effect that made this a surprisingly large patch. [1]: https://groups.google.com/d/msg/wordnik-api/EC3rGajE0os/ey_5dBI_jWcJ In changing the models, I also had to change the swagger_model.py processor so it can handle the type discriminator and subtyping. I took that a big step forward, and using that information to generate an ari_model module, which can validate a JSON object against the Swagger model. The REST and WebSocket generators were changed to take advantage of the validators. If compiled with AST_DEVMODE enabled, JSON objects that don't match their corresponding models will not be sent out. For REST API calls, a 500 Internal Server response is sent. For WebSockets, the invalid JSON message is replaced with an error message. Since this took over about half of the job of the existing JSON generators, and the .to_json virtual function on messages took over the other half, I reluctantly removed the generators. The validators turned up all sorts of errors and inconsistencies in our data models, and the code. These were cleaned up, with checks in the code generator avoid some of the consistency problems in the future. * The model for a channel snapshot was trimmed down to match the information sent via AMI. Many of the field being sent were not useful in the general case. * The model for a bridge snapshot was updated to be more consistent with the other ARI models. Another impact of introducing subtyping was that the swagger-codegen documentation generator was insufficient (at least until it catches up with Swagger 1.2). I wanted it to be easier to generate docs for the API anyways, so I ported the wiki pages to use the Asterisk Swagger generator. In the process, I was able to clean up many of the model links, which would occasionally give inconsistent results on the wiki. I also added error responses to the wiki docs, making the wiki documentation more complete. Finally, since Stasis-HTTP will now be named Asterisk REST Interface (ARI), any new functions and files I created carry the ari_ prefix. I changed a few stasis_http references to ari where it was non-intrusive and made sense. (closes issue ASTERISK-21885) Review: https://reviewboard.asterisk.org/r/2639/ git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@393529 65c4cc65-6c06-0410-ace0-fbb531ad65f3
Diffstat (limited to 'res/res_ari_model.c')
-rw-r--r--res/res_ari_model.c207
1 files changed, 207 insertions, 0 deletions
diff --git a/res/res_ari_model.c b/res/res_ari_model.c
new file mode 100644
index 000000000..fd2ec6493
--- /dev/null
+++ b/res/res_ari_model.c
@@ -0,0 +1,207 @@
+/*
+ * Asterisk -- An open source telephony toolkit.
+ *
+ * Copyright (C) 2013, Digium, Inc.
+ *
+ * David M. Lee, II <dlee@digium.com>
+ *
+ * See http://www.asterisk.org for more information about
+ * the Asterisk project. Please do not directly contact
+ * any of the maintainers of this project for assistance;
+ * the project provides a web site, mailing lists and IRC
+ * channels for your use.
+ *
+ * This program is free software, distributed under the terms of
+ * the GNU General Public License Version 2. See the LICENSE file
+ * at the top of the source tree.
+ */
+
+/*! \file
+ *
+ * \brief Implementation Swagger validators.
+ *
+ * \author David M. Lee, II <dlee@digium.com>
+ */
+
+/*** MODULEINFO
+ <support_level>core</support_level>
+ ***/
+
+#include "asterisk.h"
+
+ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
+
+#include "stasis_http/ari_model_validators.h"
+#include "asterisk/logger.h"
+#include "asterisk/module.h"
+#include "asterisk/utils.h"
+
+#include <regex.h>
+
+/* Regex to match date strings */
+static regex_t date_regex;
+
+/* Regex for YYYY-MM-DD */
+#define REGEX_YMD "[0-9]{4}-[01][0-9]-[0-3][0-9]"
+
+/* Regex for hh:mm(:ss(.s)); seconds and subseconds optional
+ * Handles the probably impossible case of a leap second, too */
+#define REGEX_HMS "[0-2][0-9]:[0-5][0-9](:[0-6][0-9](.[0-9]+)?)?"
+
+/* Regex for timezone: (+|-)hh(:mm), with optional colon. */
+#define REGEX_TZ "(Z|[-+][0-2][0-9](:?[0-5][0-9])?)"
+
+/* REGEX for ISO 8601, the time specifier optional */
+#define ISO8601_PATTERN "^" REGEX_YMD "(T" REGEX_HMS REGEX_TZ ")?$"
+
+static int check_type(struct ast_json *json, enum ast_json_type expected)
+{
+ enum ast_json_type actual;
+
+ if (!json) {
+ ast_log(LOG_ERROR, "Expected type %s, was NULL\n",
+ ast_json_typename(expected));
+ return 0;
+ }
+
+ actual = ast_json_typeof(json);
+ if (expected != actual) {
+ ast_log(LOG_ERROR, "Expected type %s, was %s\n",
+ ast_json_typename(expected), ast_json_typename(actual));
+ return 0;
+ }
+ return 1;
+}
+
+static int check_range(intmax_t minval, intmax_t maxval, struct ast_json *json)
+{
+ intmax_t v;
+
+ if (!check_type(json, AST_JSON_INTEGER)) {
+ return 0;
+ }
+
+ v = ast_json_integer_get(json);
+
+ if (v < minval || maxval < v) {
+ ast_log(LOG_ERROR, "Value out of range. Expected %jd <= %jd <= %jd\n", minval, v, maxval);
+ return 0;
+ }
+ return 1;
+}
+
+int ari_validate_void(struct ast_json *json)
+{
+ return check_type(json, AST_JSON_NULL);
+}
+
+int ari_validate_byte(struct ast_json *json)
+{
+ /* Java bytes are signed, which accounts for great fun for all */
+ return check_range(-128, 255, json);
+}
+
+int ari_validate_boolean(struct ast_json *json)
+{
+ enum ast_json_type actual = ast_json_typeof(json);
+ switch (actual) {
+ case AST_JSON_TRUE:
+ case AST_JSON_FALSE:
+ return 1;
+ default:
+ ast_log(LOG_ERROR, "Expected type boolean, was %s\n",
+ ast_json_typename(actual));
+ return 0;
+ }
+}
+
+int ari_validate_int(struct ast_json *json)
+{
+ /* Swagger int's are 32-bit */
+ return check_range(-2147483648, 2147483647, json);
+}
+
+int ari_validate_long(struct ast_json *json)
+{
+ /* All integral values are valid longs. No need for range check. */
+ return check_type(json, AST_JSON_INTEGER);
+}
+
+int ari_validate_float(struct ast_json *json)
+{
+ return check_type(json, AST_JSON_REAL);
+}
+
+int ari_validate_double(struct ast_json *json)
+{
+ return check_type(json, AST_JSON_REAL);
+}
+
+int ari_validate_string(struct ast_json *json)
+{
+ return check_type(json, AST_JSON_STRING);
+}
+
+int ari_validate_date(struct ast_json *json)
+{
+ /* Dates are ISO-8601 strings */
+ const char *str;
+ if (!check_type(json, AST_JSON_STRING)) {
+ return 0;
+ }
+ str = ast_json_string_get(json);
+ ast_assert(str != NULL);
+ if (regexec(&date_regex, str, 0, NULL, 0) != 0) {
+ ast_log(LOG_ERROR, "Date field is malformed: '%s'\n", str);
+ return 0;
+ }
+ return 1;
+}
+
+int ari_validate_list(struct ast_json *json, int (*fn)(struct ast_json *))
+{
+ int res = 1;
+ size_t i;
+
+ if (!check_type(json, AST_JSON_ARRAY)) {
+ return 0;
+ }
+
+ for (i = 0; i < ast_json_array_size(json); ++i) {
+ int member_res;
+ member_res = fn(ast_json_array_get(json, i));
+ if (!member_res) {
+ ast_log(LOG_ERROR,
+ "Array member %zd failed validation\n", i);
+ res = 0;
+ }
+ }
+
+ return res;
+}
+
+static int load_module(void)
+{
+ int res;
+ res = regcomp(&date_regex, ISO8601_PATTERN,
+ REG_EXTENDED | REG_ICASE | REG_NOSUB);
+
+ if (res != 0) {
+ return AST_MODULE_LOAD_FAILURE;
+ }
+ return AST_MODULE_LOAD_SUCCESS;
+}
+
+static int unload_module(void)
+{
+ regfree(&date_regex);
+ return 0;
+}
+
+AST_MODULE_INFO(ASTERISK_GPL_KEY,
+ AST_MODFLAG_LOAD_ORDER | AST_MODFLAG_GLOBAL_SYMBOLS,
+ "ARI Model validators",
+ .load = load_module,
+ .unload = unload_module,
+ .load_pri = AST_MODPRI_APP_DEPEND,
+ );