Search in sources :

Example 6 with Aai20Document

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

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

Example 8 with Aai20Document

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

Example 9 with Aai20Document

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

the class APIMappingUtil method getScopesFromAsyncAPI.

private static List<ScopeDTO> getScopesFromAsyncAPI(String asyncAPIDefinition) {
    Aai20Document document = (Aai20Document) Library.readDocumentFromJSONString(asyncAPIDefinition);
    List<ScopeDTO> scopeDTOS = new ArrayList<>();
    if (document.components == null || document.components.securitySchemes == null || document.components.securitySchemes.get("oauth2") == null) {
        return scopeDTOS;
    }
    AaiSecurityScheme securityScheme = document.components.securitySchemes.get("oauth2");
    if (securityScheme.flows == null || securityScheme.flows.implicit == null || securityScheme.flows.implicit.scopes == null) {
        return scopeDTOS;
    }
    Map<String, String> scopes = securityScheme.flows.implicit.scopes;
    Map<String, String> scopeBindings = new HashMap<>();
    Extension xScopesBindings = securityScheme.flows.implicit.getExtension("x-scopes-bindings");
    if (xScopesBindings != null) {
        scopeBindings = (Map<String, String>) xScopesBindings.value;
    }
    for (Map.Entry<String, String> aScope : scopes.entrySet()) {
        ScopeDTO scopeDTO = new ScopeDTO();
        scopeDTO.setName(aScope.getKey());
        scopeDTO.setDisplayName(aScope.getKey());
        scopeDTO.setDescription(aScope.getValue());
        String roles = scopeBindings.get(aScope.getKey());
        if (roles == null || roles.isEmpty()) {
            scopeDTO.setBindings(Collections.emptyList());
        } else {
            scopeDTO.setBindings(Arrays.asList((roles).split(",")));
        }
        scopeDTOS.add(scopeDTO);
    }
    return scopeDTOS;
}
Also used : Extension(io.apicurio.datamodels.core.models.Extension) AaiSecurityScheme(io.apicurio.datamodels.asyncapi.models.AaiSecurityScheme) HashMap(java.util.HashMap) ScopeDTO(org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.ScopeDTO) APIScopeDTO(org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.APIScopeDTO) Aai20Document(io.apicurio.datamodels.asyncapi.v2.models.Aai20Document) ArrayList(java.util.ArrayList) Map(java.util.Map) HashMap(java.util.HashMap)

Example 10 with Aai20Document

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

the class SolaceBrokerDeployer method deploy.

/**
 * Deploy API artifact to provided environment
 *
 * @param api         API to be deployed into Solace broker
 * @param environment Environment to be deployed
 * @throws DeployerException if error occurs when deploying APIs to Solace broker
 */
@Override
public boolean deploy(API api, Environment environment) throws DeployerException {
    String apiDefinition = api.getAsyncApiDefinition();
    Aai20Document aai20Document = (Aai20Document) Library.readDocumentFromJSONString(apiDefinition);
    String apiNameForRegistration = api.getId().getApiName() + "-" + api.getId().getVersion();
    String[] apiContextParts = api.getContext().split("/");
    String apiNameWithContext = environment.getName() + "-" + api.getId().getName() + "-" + apiContextParts[1] + "-" + apiContextParts[2];
    SolaceAdminApis solaceAdminApis;
    try {
        solaceAdminApis = SolaceNotifierUtils.getSolaceAdminApis();
    } catch (APIManagementException e) {
        throw new DeployerException(e.getMessage());
    }
    // check availability of environment
    CloseableHttpResponse response1 = solaceAdminApis.environmentGET(environment.getAdditionalProperties().get(SolaceConstants.SOLACE_ENVIRONMENT_ORGANIZATION), environment.getName());
    if (response1.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
        if (log.isDebugEnabled()) {
            log.info("environment '" + environment.getName() + "' found in Solace broker");
        }
        // check api product already exists in solace
        CloseableHttpResponse response4 = solaceAdminApis.apiProductGet(environment.getAdditionalProperties().get(SolaceConstants.SOLACE_ENVIRONMENT_ORGANIZATION), apiNameWithContext);
        if (response4.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
            // api Product Already found in solace. No need to deploy again into Solace
            if (log.isDebugEnabled()) {
                log.info("API product '" + apiNameWithContext + "' already found in Solace. No need to create " + "again");
            }
            return true;
        } else if (response4.getStatusLine().getStatusCode() == HttpStatus.SC_NOT_FOUND) {
            // api product not found in solace. check existence of registered API in solace
            if (log.isDebugEnabled()) {
                log.info("API product '" + apiNameWithContext + "' not found in Solace. Checking the existence " + "of API");
            }
            CloseableHttpResponse response5 = solaceAdminApis.registeredAPIGet(environment.getAdditionalProperties().get(SolaceConstants.SOLACE_ENVIRONMENT_ORGANIZATION), apiNameForRegistration);
            if (response5.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
                if (log.isDebugEnabled()) {
                    log.info("API '" + apiNameForRegistration + "' already registered in Solace. Creating API " + "product using registered API");
                }
                // create API product only
                CloseableHttpResponse response3 = solaceAdminApis.createAPIProduct(environment.getAdditionalProperties().get(SolaceConstants.SOLACE_ENVIRONMENT_ORGANIZATION), environment.getName(), aai20Document, apiNameWithContext, apiNameForRegistration);
                if (response3.getStatusLine().getStatusCode() == HttpStatus.SC_CREATED) {
                    log.info("API product " + apiNameWithContext + " has been created in Solace broker");
                    return true;
                } else {
                    if (log.isDebugEnabled()) {
                        log.error("Error while creating API product" + apiNameWithContext + " in Solace." + response3.getStatusLine().toString());
                    }
                    throw new DeployerException(response3.getStatusLine().toString());
                }
            } else if (response5.getStatusLine().getStatusCode() == HttpStatus.SC_NOT_FOUND) {
                if (log.isDebugEnabled()) {
                    log.info("API '" + apiNameForRegistration + "' not registered in Solace. Creating both API " + "and API product. : " + response5.getStatusLine().toString());
                }
                // register the API in Solace Broker
                CloseableHttpResponse response2 = solaceAdminApis.registerAPI(environment.getAdditionalProperties().get(SolaceConstants.SOLACE_ENVIRONMENT_ORGANIZATION), apiNameForRegistration, apiDefinition);
                if (response2.getStatusLine().getStatusCode() == HttpStatus.SC_CREATED) {
                    if (log.isDebugEnabled()) {
                        log.info("API '" + apiNameForRegistration + "' has been registered in Solace broker");
                    }
                    // create API Product in Solace broker
                    CloseableHttpResponse response3 = solaceAdminApis.createAPIProduct(environment.getAdditionalProperties().get(SolaceConstants.SOLACE_ENVIRONMENT_ORGANIZATION), environment.getName(), aai20Document, apiNameWithContext, apiNameForRegistration);
                    if (response3.getStatusLine().getStatusCode() == HttpStatus.SC_CREATED) {
                        log.info("API product '" + apiNameWithContext + "' has been created in Solace broker");
                        return true;
                    } else {
                        if (log.isDebugEnabled()) {
                            log.error("Error while creating API product in Solace. : " + response2.getStatusLine().toString());
                        }
                        // delete registered API in solace
                        CloseableHttpResponse response6 = solaceAdminApis.deleteRegisteredAPI(environment.getAdditionalProperties().get(SolaceConstants.SOLACE_ENVIRONMENT_ORGANIZATION), apiNameForRegistration);
                        if (response6.getStatusLine().getStatusCode() == HttpStatus.SC_NO_CONTENT) {
                            log.info("Successfully deleted registered API '" + apiNameForRegistration + "' " + "from Solace");
                        } else {
                            if (log.isDebugEnabled()) {
                                log.error("Error while deleting registered API '" + apiNameForRegistration + "' " + "in Solace. : " + response6.getStatusLine().toString());
                            }
                            throw new DeployerException(response6.getStatusLine().toString());
                        }
                        if (log.isDebugEnabled()) {
                            log.error("Error while deleting registered API '" + apiNameForRegistration + "' " + "in Solace. : " + response3.getStatusLine().toString());
                        }
                        throw new DeployerException(response3.getStatusLine().toString());
                    }
                } else {
                    log.error("Error while registering API in Solace - '" + apiNameForRegistration + "'");
                    throw new DeployerException(response2.getStatusLine().toString());
                }
            } else {
                if (log.isDebugEnabled()) {
                    log.error("Error while finding API '" + apiNameForRegistration + "' in Solace. : " + response5.getStatusLine().toString());
                }
                throw new DeployerException(response5.getStatusLine().toString());
            }
        } else {
            if (log.isDebugEnabled()) {
                log.error("Error while finding API product '" + apiNameWithContext + "' in Solace. : " + response4.getStatusLine().toString());
            }
            throw new DeployerException(response4.getStatusLine().toString());
        }
    } else {
        if (log.isDebugEnabled()) {
            log.error("Cannot find specified Solace environment - '" + environment.getName() + ". : " + response1.getStatusLine().toString());
        }
        throw new DeployerException(response1.getStatusLine().toString());
    }
}
Also used : SolaceAdminApis(org.wso2.carbon.apimgt.solace.SolaceAdminApis) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) Aai20Document(io.apicurio.datamodels.asyncapi.v2.models.Aai20Document) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) DeployerException(org.wso2.carbon.apimgt.impl.deployer.exceptions.DeployerException)

Aggregations

Aai20Document (io.apicurio.datamodels.asyncapi.v2.models.Aai20Document)10 HashMap (java.util.HashMap)6 AaiChannelItem (io.apicurio.datamodels.asyncapi.models.AaiChannelItem)5 Extension (io.apicurio.datamodels.core.models.Extension)5 LinkedHashMap (java.util.LinkedHashMap)5 Map (java.util.Map)5 Aai20ChannelItem (io.apicurio.datamodels.asyncapi.v2.models.Aai20ChannelItem)3 LinkedHashSet (java.util.LinkedHashSet)3 Scope (org.wso2.carbon.apimgt.api.model.Scope)3 Aai20Operation (io.apicurio.datamodels.asyncapi.v2.models.Aai20Operation)2 Aai20SecurityScheme (io.apicurio.datamodels.asyncapi.v2.models.Aai20SecurityScheme)2 Aai20Server (io.apicurio.datamodels.asyncapi.v2.models.Aai20Server)2 ArrayList (java.util.ArrayList)2 HashSet (java.util.HashSet)2 URITemplate (org.wso2.carbon.apimgt.api.model.URITemplate)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 AaiOperation (io.apicurio.datamodels.asyncapi.models.AaiOperation)1 AaiSecurityScheme (io.apicurio.datamodels.asyncapi.models.AaiSecurityScheme)1 Aai20ImplicitOAuthFlow (io.apicurio.datamodels.asyncapi.v2.models.Aai20ImplicitOAuthFlow)1 Aai20OAuthFlows (io.apicurio.datamodels.asyncapi.v2.models.Aai20OAuthFlows)1