Search in sources :

Example 1 with AaiChannelItem

use of io.apicurio.datamodels.asyncapi.models.AaiChannelItem 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 AaiChannelItem

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

the class SolaceAdminApis method buildAPIProductRequestBody.

/**
 * Build the request body for API product creation request
 *
 * @param aai20Document      Async definition of the Solace API
 * @param environment        name of the Environment in Solace
 * @param apiNameWithContext name of the API with context
 * @param registeredApiName  name of the API in Solace
 * @return org.json.JSON Object of request body
 */
private org.json.JSONObject buildAPIProductRequestBody(Aai20Document aai20Document, String environment, String apiNameWithContext, String registeredApiName) {
    org.json.JSONObject requestBody = new org.json.JSONObject();
    org.json.JSONArray apiName = new org.json.JSONArray();
    apiName.put(registeredApiName);
    requestBody.put("apis", apiName);
    requestBody.put("approvalType", "auto");
    if (aai20Document.info.description != null) {
        requestBody.put("description", aai20Document.info.description);
    } else {
        requestBody.put("description", apiNameWithContext);
    }
    requestBody.put("displayName", apiNameWithContext);
    requestBody.put("pubResources", new org.json.JSONArray());
    requestBody.put("subResources", new org.json.JSONArray());
    requestBody.put("name", apiNameWithContext);
    org.json.JSONArray environments = new org.json.JSONArray();
    environments.put(environment);
    requestBody.put("environments", environments);
    HashSet<String> parameters1 = new HashSet<>();
    org.json.JSONArray attributes1 = new org.json.JSONArray();
    for (AaiChannelItem channel : aai20Document.getChannels()) {
        if (channel.parameters != null) {
            Set<String> parameterKeySet = channel.parameters.keySet();
            for (String parameterName : parameterKeySet) {
                if (!parameters1.contains(parameterName)) {
                    AaiParameter parameterObject = channel.parameters.get(parameterName);
                    if (parameterObject.schema != null) {
                        org.json.JSONObject attributeObj = getAttributesFromParameterSchema(parameterName, parameterObject);
                        if (attributeObj != null) {
                            attributes1.put(attributeObj);
                            parameters1.add(parameterName);
                        }
                    } else if (parameterObject.$ref != null) {
                        if (aai20Document.components.parameters != null) {
                            AaiParameter parameterObject1 = aai20Document.components.parameters.get(parameterName);
                            if (parameterObject1.schema != null) {
                                org.json.JSONObject attributeObj = getAttributesFromParameterSchema(parameterName, parameterObject1);
                                if (attributeObj != null) {
                                    attributes1.put(attributeObj);
                                    parameters1.add(parameterName);
                                }
                            }
                        }
                    }
                }
            }
        }
    }
    requestBody.put("attributes", attributes1);
    HashSet<String> protocolsHashSet = new HashSet<>();
    for (AaiChannelItem channel : aai20Document.getChannels()) {
        protocolsHashSet.addAll(AsyncApiParser.getProtocols(channel));
    }
    org.json.JSONArray protocols = new org.json.JSONArray();
    for (String protocol : protocolsHashSet) {
        org.json.JSONObject protocolObject = new org.json.JSONObject();
        protocolObject.put("name", protocol);
        protocolObject.put("version", getProtocolVersion(protocol));
        protocols.put(protocolObject);
    }
    requestBody.put("protocols", protocols);
    return requestBody;
}
Also used : AaiParameter(io.apicurio.datamodels.asyncapi.models.AaiParameter) JSONObject(org.json.JSONObject) AaiChannelItem(io.apicurio.datamodels.asyncapi.models.AaiChannelItem) JSONObject(org.json.JSONObject) JSONArray(org.json.JSONArray) JSONArray(org.json.JSONArray) HashSet(java.util.HashSet)

Example 3 with AaiChannelItem

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

the class AsyncApiParser method getTransportProtocolsForAsyncAPI.

/**
 * Get available transport protocols for the Async API
 *
 * @param definition Async API Definition
 * @return List<String> List of available transport protocols
 * @throws APIManagementException If the async env configuration if not provided properly
 */
public static List<String> getTransportProtocolsForAsyncAPI(String definition) throws APIManagementException {
    Aai20Document aai20Document = (Aai20Document) Library.readDocumentFromJSONString(definition);
    HashSet<String> asyncTransportProtocols = new HashSet<>();
    for (AaiChannelItem channel : aai20Document.getChannels()) {
        asyncTransportProtocols.addAll(getProtocols(channel));
    }
    ArrayList<String> asyncTransportProtocolsList = new ArrayList<>(asyncTransportProtocols);
    return asyncTransportProtocolsList;
}
Also used : AaiChannelItem(io.apicurio.datamodels.asyncapi.models.AaiChannelItem) Aai20Document(io.apicurio.datamodels.asyncapi.v2.models.Aai20Document) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet)

Example 4 with AaiChannelItem

use of io.apicurio.datamodels.asyncapi.models.AaiChannelItem 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 5 with AaiChannelItem

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

the class AsyncApiParser method buildWSUriMapping.

public Map<String, String> buildWSUriMapping(String apiDefinition) {
    Map<String, String> wsUriMapping = new HashMap<>();
    Aai20Document document = (Aai20Document) Library.readDocumentFromJSONString(apiDefinition);
    for (Map.Entry<String, AaiChannelItem> entry : document.channels.entrySet()) {
        AaiOperation publishOperation = entry.getValue().publish;
        if (publishOperation != null) {
            Extension xUriMapping = publishOperation.getExtension("x-uri-mapping");
            if (xUriMapping != null) {
                wsUriMapping.put("PUBLISH_" + entry.getKey(), xUriMapping.value.toString());
            }
        }
        AaiOperation subscribeOperation = entry.getValue().subscribe;
        if (subscribeOperation != null) {
            Extension xUriMapping = subscribeOperation.getExtension("x-uri-mapping");
            if (xUriMapping != null) {
                wsUriMapping.put("SUBSCRIBE_" + entry.getKey(), xUriMapping.value.toString());
            }
        }
    }
    return wsUriMapping;
}
Also used : Extension(io.apicurio.datamodels.core.models.Extension) AaiOperation(io.apicurio.datamodels.asyncapi.models.AaiOperation) AaiChannelItem(io.apicurio.datamodels.asyncapi.models.AaiChannelItem) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Aai20Document(io.apicurio.datamodels.asyncapi.v2.models.Aai20Document) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Aggregations

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