Update events to use Swagger 1.3 subtyping, and related aftermath
authorDavid M. Lee <dlee@digium.com>
Wed, 3 Jul 2013 16:32:41 +0000 (16:32 +0000)
committerDavid M. Lee <dlee@digium.com>
Wed, 3 Jul 2013 16:32:41 +0000 (16:32 +0000)
commitc9a3d4562ddb1ed5b34f7d5530efd6aa695377c2
treedd082285fbb5c7714164e26145acc5c966e663be
parentdcf03554a0b38806bf1fe258acc423b070533d6e
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
73 files changed:
Makefile
include/asterisk/json.h
include/asterisk/stasis_http.h
main/json.c
main/stasis_bridging.c
main/stasis_channels.c
main/stasis_endpoints.c
res/Makefile
res/res_ari_model.c [new file with mode: 0644]
res/res_ari_model.exports.in [new file with mode: 0644]
res/res_stasis.c
res/res_stasis_http.c
res/res_stasis_http_asterisk.c
res/res_stasis_http_bridges.c
res/res_stasis_http_channels.c
res/res_stasis_http_endpoints.c
res/res_stasis_http_events.c
res/res_stasis_http_playback.c
res/res_stasis_http_recordings.c
res/res_stasis_http_sounds.c
res/res_stasis_json_asterisk.c [deleted file]
res/res_stasis_json_asterisk.exports.in [deleted file]
res/res_stasis_json_bridges.c [deleted file]
res/res_stasis_json_bridges.exports.in [deleted file]
res/res_stasis_json_channels.c [deleted file]
res/res_stasis_json_channels.exports.in [deleted file]
res/res_stasis_json_endpoints.c [deleted file]
res/res_stasis_json_endpoints.exports.in [deleted file]
res/res_stasis_json_events.c [deleted file]
res/res_stasis_json_events.exports.in [deleted file]
res/res_stasis_json_playback.c [deleted file]
res/res_stasis_json_playback.exports.in [deleted file]
res/res_stasis_json_recordings.c [deleted file]
res/res_stasis_json_recordings.exports.in [deleted file]
res/res_stasis_json_sounds.c [deleted file]
res/res_stasis_json_sounds.exports.in [deleted file]
res/stasis_http/ari_model_validators.c [new file with mode: 0644]
res/stasis_http/ari_model_validators.h [new file with mode: 0644]
res/stasis_http/ari_websockets.c
res/stasis_http/resource_recordings.c
res/stasis_http/resource_recordings.h
res/stasis_json/resource_asterisk.h [deleted file]
res/stasis_json/resource_bridges.h [deleted file]
res/stasis_json/resource_channels.h [deleted file]
res/stasis_json/resource_endpoints.h [deleted file]
res/stasis_json/resource_events.h [deleted file]
res/stasis_json/resource_playback.h [deleted file]
res/stasis_json/resource_recordings.h [deleted file]
res/stasis_json/resource_sounds.h [deleted file]
rest-api-templates/api.wiki.mustache [new file with mode: 0644]
rest-api-templates/ari_model_validators.c.mustache [new file with mode: 0644]
rest-api-templates/ari_model_validators.h.mustache [new file with mode: 0644]
rest-api-templates/asterisk_processor.py
rest-api-templates/event_function_decl.mustache [deleted file]
rest-api-templates/make_ari_stubs.py [moved from rest-api-templates/make_stasis_http_stubs.py with 73% similarity]
rest-api-templates/models.wiki.mustache [new file with mode: 0644]
rest-api-templates/res_stasis_http_resource.c.mustache
rest-api-templates/res_stasis_json_resource.c.mustache [deleted file]
rest-api-templates/res_stasis_json_resource.exports.mustache [deleted file]
rest-api-templates/stasis_json_resource.h.mustache [deleted file]
rest-api-templates/swagger_model.py
rest-api-templates/transform.py
rest-api/api-docs/asterisk.json
rest-api/api-docs/bridges.json
rest-api/api-docs/channels.json
rest-api/api-docs/endpoints.json
rest-api/api-docs/events.json
rest-api/api-docs/playback.json
rest-api/api-docs/recordings.json
rest-api/api-docs/sounds.json
tests/test_ari_model.c [new file with mode: 0644]
tests/test_res_stasis.c
tests/test_stasis_channels.c