A Modeling Editor and Code Generator for AsyncAPI
Tags: asyncapi, api, showdev, ide
Published: 2019-12-15 17:23:12

IIoT (Industrial IoT) architectures are typically distributed and asynchronous, with communication being event-driven, such as the publication (and corresponding subscription) of messages. These asynchronous architectures enhance scalability and tolerance to changes, but raise interoperability issues as <strong>the explicit knowledge of the internal structure of the messages and their categorization (topics) is diluted</strong> among the elements of the architecture. 

In fact, this was also a problem for REST APIs, until the industry came together and proposed a standard way to define the structure and schema of synchronous APIs: <a href="https://www.openapis.org/" target="_blank" rel="noopener noreferrer">OpenAPI</a> (derived from <a href="https://modeling-languages.com/modeling-web-api-comparing/" target="_blank" rel="noopener noreferrer">Swagger</a>). For asynchronous architectures, and inspired by OpenAPI, the <a href="https://www.asyncapi.com/" rel="nofollow">AsyncAPI</a> has appeared recently:

<blockquote><em>AsyncAPI provides a specification that allows you to define Message-Driven APIs in a machine-readable format. It's protocol-agnostic, so you can use it for APIs that work over Kafka, MQTT, AMQP, WebSockets, STOMP, etc. The spec is very similar to OpenAPI/Swagger so, if you're familiar with it, AsyncAPI should be easy for you.</em></blockquote>

In AsyncAPI, the specifications of an API can be defined in YAML or JSON, which allows specifying, for example, the message brokers, the topics of interest, or the different message formats associated with each one of the topics, among other aspects. AsyncAPI is, however, in the early stages of development, and the <strong>AsyncAPI tool market is underdeveloped</strong>, mainly limited to the generation of documentation to be consumed by humans.

Similarly to what we've done for OpenAPI (see our <a href="https://modeling-languages.com/rest-api-composer/" target="_blank" rel="noopener noreferrer">API Composer</a> or our <a href="https://modeling-languages.com/automatic-discovery-web-api-specifications/" target="_blank" rel="noopener noreferrer">API Discoverer</a>), we believe<strong> a model-based approach would facilitate the modeling of AsyncAPI specifications and the development of Message-Driven APIs from them. </strong>

The overall view of our approach is illustrated in the following figure. 

<a href="http://modeling-languages.com/wp-content/uploads/2019/12/asyncapitoolkit.png"><img src="http://modeling-languages.com/wp-content/uploads/2019/12/asyncapitoolkit.png" alt="AsyncAPI editor" width="801" height="309" class="aligncenter size-full wp-image-7294" /></a>

The <a href="https://github.com/SOM-Research/asyncapi-toolkit" target="_blank" rel="noopener noreferrer">AsyncAPI toolkit is available on GitHub</a>, make sure you star/watch it to follow its evolution!.

<h2>Importing / Modeling an AsyncAPI specification</h2>

First, based on the AsyncAPI specification, we created an <a href="https://www.eclipse.org/Xtext/" target="_blank" rel="noopener noreferrer">Xtext</a> grammar. From this grammar, an Ecore metamodel is automatically derived, together with a set of editors and Eclipse-based tools. These editors allow creating JSON-based specifications of message-driven APIs using AsyncAPI. Specifications created using these editors are automatically parsed and reified as instances of the AsyncAPI metamodel.

<a href="http://modeling-languages.com/wp-content/uploads/2019/12/AsyncApiMetamodel.png"><img class="wp-image-7296 size-large" src="https://modeling-languages.com/wp-content/uploads/2019/12/AsyncApiMetamodel-1024x524.png" alt="AsyncAPI metamodel" width="1024" height="524" /></a> 

<h2> Generating code to easily process messages from an AsyncAPI specification </h2>
Additionally, the prototype is able to generate Java code supporting the creation and serialization of JSON-based message payloads according to the modeled AsyncAPI, including nested JSON objects. No support for arrays is provided yet at this point however. The excerpt below shows an example of an AsyncAPI specification supported by the prototype:

<pre>

{
  "asyncapi": "1.2.0",
  "info": {
    "title": "Sample AsyncAPI specification",
    "version": "0.1.0",
  },
  "servers": [
    {
      "url": "broker.url:{port}",
      "scheme": "mqtt",
      "description": "This is an example description",
      "variables": {
        "port": {
          "default": "1883",
          "enum": [ "1883", "8883" ]
        }
      }
    }
  ],
  "topics": {
    "messages/device2controller": {
      "publish": { "$ref" : "#/components/messages/request“ }
    }
  }
},
"components": {
  "schemas": {
    "protocol_version": {
      "title": "Protocol version",
      "type": "integer",
      "default": 2,
      "x-friendly-name": "ProtocolVersion"
    },
    "id": {
      "title": "ID",
      "type": "string",
      "format": "XXXXXX YY ZZZZZZ W"
    },
    "status": {
      "title": "Status",
      "type": "string",
      "enum": ["OK", "ERROR"],
      "x-friendly-name" : "Status"
    },
    "environment": {
      "title": "Environment",
      "type": "string",
      "enum": ["DEV", "STAG","PROD" ],
      "x-friendly-name" : "Environment"
    }
  },
  "messages" : {
    "request" : {
      "summary" : "Request connectivity.",
      "description": "Request connectivity when status changes",
      "payload": {
        "type": "object",
        "properties": {
          "P": { "$ref": "#/components/schemas/protocol_version" },
          "ID": { "$ref": "#/components/schemas/id" },
          "E": { "$ref": "#/components/schemas/environment" },
          "M": {
            "x-friendly-name" : "Message",
            "properties": {
              "S": { "$ref": "#/components/schemas/status" },
              "C": {
                "title": "Content",
                "type": "string",
                "x-friendly-name": "Content"
              }
            }
          }
        }
      }
    }
  }
}

</pre>

A specification like the above, allows generating messages as follows:

<pre>
package tests;
import messages.device2controller.Request;
import messages.device2controller.Request.Payload.Environment;
import messages.device2controller.Request.Payload.Message;
import messages.device2controller.Request.Payload.PayloadBuilder;
import messages.device2controller.Request.Payload.Message.Status;

public class Test {

  public static void main(String[] args) {

    PayloadBuilder builder = Request.payloadBuilder();
 
    Request.Payload payload = builder
      .withProtocolVersion(2)
      .withEnvironment(Environment.DEV)
      .withID("id")
      .withMessage(
        Message.newBuilder()
          .withStatus(Status.OK)
          .withContent("Content")
          .build()
      ).build();
 
    System.out.println(payload.toJson(true));
    System.out.println(Request.Payload.fromJson(payload.toJson()).toJson(true));
  }
}

</pre>

The code generated by our toolkit also allows to easily publish the messages built as explained above, and to subscribe to them using the servers configured in the AsyncAPI specification. <a href="https://github.com/SOM-Research/asyncapi-toolkit/blob/master/README.md#quick-start-guide">Check our online documentation for an example!</a>

<h2> Generating a new AsyncAPI from an Ecore model </h2>
Until now, we assumed that either you already had an AsyncAPI file to import or you would be using our AsyncAPI editor to create one. In fact, there is a third alternative: <strong>take an existing Ecore model you already have available and generate an skeleton AsyncAPI specification from it</strong>.

The generator will create a reusable JSON Schema for each domain class. Channels will be created out of annotated EClasses. Moreover, hosts information can also be specified via EAnnotations (<a href="https://github.com/SOM-Research/asyncapi-toolkit#generating-an-asyncapi-specification-from-an-ecore-model" rel="noopener noreferrer" target="_blank">more details</a>).