Search in sources :

Example 1 with Aai20ChannelItem

use of io.apicurio.datamodels.asyncapi.v2.models.Aai20ChannelItem in project carbon-apimgt by wso2.

the class AsyncApiParser method getAsyncApiDefinitionForStore.

/**
 * Update AsyncAPI definition for store
 *
 * @param api            API
 * @param asyncAPIDefinition  AsyncAPI definition
 * @param hostsWithSchemes host addresses with protocol mapping
 * @return AsyncAPI definition
 * @throws APIManagementException throws if an error occurred
 */
public String getAsyncApiDefinitionForStore(API api, String asyncAPIDefinition, Map<String, String> hostsWithSchemes) throws APIManagementException {
    Aai20Document aai20Document = (Aai20Document) Library.readDocumentFromJSONString(asyncAPIDefinition);
    String channelName = api.getContext();
    String transports = api.getTransports();
    String url = StringUtils.EMPTY;
    String[] apiTransports = transports.split(",");
    if (ArrayUtils.contains(apiTransports, APIConstants.WSS_PROTOCOL) && hostsWithSchemes.get(APIConstants.WSS_PROTOCOL) != null) {
        url = hostsWithSchemes.get(APIConstants.WSS_PROTOCOL).trim().replace(APIConstants.WSS_PROTOCOL_URL_PREFIX, "");
    }
    if (ArrayUtils.contains(apiTransports, APIConstants.WSS_PROTOCOL) && hostsWithSchemes.get(APIConstants.WS_PROTOCOL) != null) {
        if (StringUtils.isEmpty(url)) {
            url = hostsWithSchemes.get(APIConstants.WS_PROTOCOL).trim().replace(APIConstants.WS_PROTOCOL_URL_PREFIX, "");
        }
    }
    Aai20Server server = (Aai20Server) aai20Document.getServers().get(0);
    server.url = url;
    Map<String, AaiChannelItem> channels = aai20Document.channels;
    Aai20ChannelItem channelDetails = null;
    for (String x : channels.keySet()) {
        channelDetails = (Aai20ChannelItem) channels.get(x);
        aai20Document.channels.remove(x);
    }
    assert channelDetails != null;
    channelDetails._name = channelName;
    aai20Document.channels.put(channelName, channelDetails);
    return Library.writeDocumentToJSONString(aai20Document);
}
Also used : AaiChannelItem(io.apicurio.datamodels.asyncapi.models.AaiChannelItem) Aai20Document(io.apicurio.datamodels.asyncapi.v2.models.Aai20Document) Aai20Server(io.apicurio.datamodels.asyncapi.v2.models.Aai20Server) Aai20ChannelItem(io.apicurio.datamodels.asyncapi.v2.models.Aai20ChannelItem)

Example 2 with Aai20ChannelItem

use of io.apicurio.datamodels.asyncapi.v2.models.Aai20ChannelItem in project carbon-apimgt by wso2.

the class AsyncApiParser method getURITemplates.

public Set<URITemplate> getURITemplates(String apiDefinition, boolean includePublish) throws APIManagementException {
    Set<URITemplate> uriTemplates = new HashSet<>();
    Set<Scope> scopes = getScopes(apiDefinition);
    Aai20Document document = (Aai20Document) Library.readDocumentFromJSONString(apiDefinition);
    if (document.channels != null && document.channels.size() > 0) {
        for (Map.Entry<String, AaiChannelItem> entry : document.channels.entrySet()) {
            Aai20ChannelItem channel = (Aai20ChannelItem) entry.getValue();
            if (includePublish && channel.publish != null) {
                uriTemplates.add(buildURITemplate(entry.getKey(), APIConstants.HTTP_VERB_PUBLISH, (Aai20Operation) channel.publish, scopes, channel));
            }
            if (channel.subscribe != null) {
                uriTemplates.add(buildURITemplate(entry.getKey(), APIConstants.HTTP_VERB_SUBSCRIBE, (Aai20Operation) channel.subscribe, scopes, channel));
            }
        }
    }
    return uriTemplates;
}
Also used : AaiChannelItem(io.apicurio.datamodels.asyncapi.models.AaiChannelItem) Scope(org.wso2.carbon.apimgt.api.model.Scope) Aai20Document(io.apicurio.datamodels.asyncapi.v2.models.Aai20Document) URITemplate(org.wso2.carbon.apimgt.api.model.URITemplate) Aai20ChannelItem(io.apicurio.datamodels.asyncapi.v2.models.Aai20ChannelItem) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet) Aai20Operation(io.apicurio.datamodels.asyncapi.v2.models.Aai20Operation)

Example 3 with Aai20ChannelItem

use of io.apicurio.datamodels.asyncapi.v2.models.Aai20ChannelItem in project carbon-apimgt by wso2.

the class AsyncApiParser method generateAsyncAPIDefinition.

public String generateAsyncAPIDefinition(API api) throws APIManagementException {
    Aai20Document aaiDocument = new Aai20Document();
    aaiDocument.info = aaiDocument.createInfo();
    aaiDocument.info.title = api.getId().getName();
    aaiDocument.info.version = api.getId().getVersion();
    if (!APIConstants.API_TYPE_WEBSUB.equals(api.getType())) {
        Aai20Server server = (Aai20Server) aaiDocument.createServer("production");
        JSONObject endpointConfig = new JSONObject(api.getEndpointConfig());
        server.url = endpointConfig.getJSONObject("production_endpoints").getString("url");
        server.protocol = api.getType().toLowerCase();
        aaiDocument.addServer("production", server);
    }
    Map<String, AaiChannelItem> channels = new HashMap<>();
    for (URITemplate uriTemplate : api.getUriTemplates()) {
        Aai20ChannelItem channelItem = aaiDocument.createChannelItem(uriTemplate.getUriTemplate());
        Aai20Operation subscribeOp = new Aai20Operation(channelItem, "subscribe");
        channelItem.subscribe = subscribeOp;
        if (APIConstants.API_TYPE_WS.equals(api.getType())) {
            Aai20Operation publishOp = new Aai20Operation(channelItem, "publish");
            channelItem.publish = publishOp;
        }
        channels.put(uriTemplate.getUriTemplate(), channelItem);
    }
    aaiDocument.channels = channels;
    return Library.writeDocumentToJSONString(aaiDocument);
}
Also used : AaiChannelItem(io.apicurio.datamodels.asyncapi.models.AaiChannelItem) JSONObject(org.json.JSONObject) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Aai20Document(io.apicurio.datamodels.asyncapi.v2.models.Aai20Document) URITemplate(org.wso2.carbon.apimgt.api.model.URITemplate) Aai20Server(io.apicurio.datamodels.asyncapi.v2.models.Aai20Server) Aai20ChannelItem(io.apicurio.datamodels.asyncapi.v2.models.Aai20ChannelItem) Aai20Operation(io.apicurio.datamodels.asyncapi.v2.models.Aai20Operation)

Aggregations

AaiChannelItem (io.apicurio.datamodels.asyncapi.models.AaiChannelItem)3 Aai20ChannelItem (io.apicurio.datamodels.asyncapi.v2.models.Aai20ChannelItem)3 Aai20Document (io.apicurio.datamodels.asyncapi.v2.models.Aai20Document)3 Aai20Operation (io.apicurio.datamodels.asyncapi.v2.models.Aai20Operation)2 Aai20Server (io.apicurio.datamodels.asyncapi.v2.models.Aai20Server)2 HashMap (java.util.HashMap)2 LinkedHashMap (java.util.LinkedHashMap)2 URITemplate (org.wso2.carbon.apimgt.api.model.URITemplate)2 HashSet (java.util.HashSet)1 LinkedHashSet (java.util.LinkedHashSet)1 Map (java.util.Map)1 JSONObject (org.json.JSONObject)1 Scope (org.wso2.carbon.apimgt.api.model.Scope)1