Search in sources :

Example 91 with APIDefinition

use of org.wso2.carbon.apimgt.api.doc.model.APIDefinition in project carbon-apimgt by wso2.

the class RestApiUtil method getStoreAppResourceMapping.

/**
 * This is static method to return URI Templates map of API Store REST API.
 * This content need to load only one time and keep it in memory as content will not change
 * during runtime.
 *
 * @return URITemplate set associated with API Manager Store REST API
 */
public static Set<URITemplate> getStoreAppResourceMapping(String version) {
    API api = new API(new APIIdentifier(RestApiConstants.REST_API_PROVIDER, RestApiConstants.REST_API_STORE_CONTEXT, RestApiConstants.REST_API_STORE_VERSION_0));
    if (storeResourceMappings != null) {
        return storeResourceMappings;
    } else {
        try {
            String definition;
            if (RestApiConstants.REST_API_STORE_VERSION_0.equals(version)) {
                definition = IOUtils.toString(RestApiUtil.class.getResourceAsStream("/store-api.json"), "UTF-8");
            } else {
                definition = IOUtils.toString(RestApiUtil.class.getResourceAsStream("/devportal-api.yaml"), "UTF-8");
            }
            APIDefinition oasParser = OASParserUtil.getOASParser(definition);
            // Get URL templates from swagger content w created
            storeResourceMappings = oasParser.getURITemplates(definition);
        } catch (APIManagementException e) {
            log.error("Error while reading resource mappings for API: " + api.getId().getApiName(), e);
        } catch (IOException e) {
            log.error("Error while reading the swagger definition for API: " + api.getId().getApiName(), e);
        }
        return storeResourceMappings;
    }
}
Also used : APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) APIDefinition(org.wso2.carbon.apimgt.api.APIDefinition) API(org.wso2.carbon.apimgt.api.model.API) APIIdentifier(org.wso2.carbon.apimgt.api.model.APIIdentifier) IOException(java.io.IOException)

Example 92 with APIDefinition

use of org.wso2.carbon.apimgt.api.doc.model.APIDefinition 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)

Example 93 with APIDefinition

use of org.wso2.carbon.apimgt.api.doc.model.APIDefinition in project carbon-apimgt by wso2.

the class RestApiUtil method getPublisherAppResourceMapping.

/**
 * This is static method to return URI Templates map of API Publisher REST API.
 * This content need to load only one time and keep it in memory as content will not change
 * during runtime.
 *
 * @return URITemplate set associated with API Manager publisher REST API
 */
public static Set<URITemplate> getPublisherAppResourceMapping(String version) {
    API api = new API(new APIIdentifier(RestApiConstants.REST_API_PROVIDER, RestApiConstants.REST_API_STORE_CONTEXT, RestApiConstants.REST_API_STORE_VERSION_0));
    if (publisherResourceMappings != null) {
        return publisherResourceMappings;
    } else {
        try {
            String definition;
            if (RestApiConstants.REST_API_PUBLISHER_VERSION_0.equals(version)) {
                definition = IOUtils.toString(RestApiUtil.class.getResourceAsStream("/publisher-api.json"), "UTF-8");
            } else {
                definition = IOUtils.toString(RestApiUtil.class.getResourceAsStream("/publisher-api.yaml"), "UTF-8");
            }
            APIDefinition oasParser = OASParserUtil.getOASParser(definition);
            // Get URL templates from swagger content we created
            publisherResourceMappings = oasParser.getURITemplates(definition);
        } catch (APIManagementException e) {
            log.error("Error while reading resource mappings for API: " + api.getId().getApiName(), e);
        } catch (IOException e) {
            log.error("Error while reading the swagger definition for API: " + api.getId().getApiName(), e);
        }
        return publisherResourceMappings;
    }
}
Also used : APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) APIDefinition(org.wso2.carbon.apimgt.api.APIDefinition) API(org.wso2.carbon.apimgt.api.model.API) APIIdentifier(org.wso2.carbon.apimgt.api.model.APIIdentifier) IOException(java.io.IOException)

Example 94 with APIDefinition

use of org.wso2.carbon.apimgt.api.doc.model.APIDefinition in project carbon-apimgt by wso2.

the class RestApiUtil method getServiceCatalogAPIResourceMapping.

/**
 * This is static method to return URI Templates map of Service Catalog REST API.
 * This content need to load only one time and keep it in memory as content will not change
 * during runtime.
 *
 * @return URITemplate set associated with Service Catalog REST API
 */
public static Set<URITemplate> getServiceCatalogAPIResourceMapping() {
    API api = new API(new APIIdentifier(RestApiConstants.REST_API_PROVIDER, RestApiConstants.REST_API_SERVICE_CATALOG_CONTEXT_FULL, "v0"));
    if (serviceCatalogAPIResourceMappings != null) {
        return serviceCatalogAPIResourceMappings;
    } else {
        try {
            String definition;
            definition = IOUtils.toString(RestApiUtil.class.getResourceAsStream("/service-catalog-api.yaml"), "UTF-8");
            APIDefinition oasParser = OASParserUtil.getOASParser(definition);
            // Get URL templates from swagger content we created
            serviceCatalogAPIResourceMappings = oasParser.getURITemplates(definition);
        } catch (APIManagementException e) {
            log.error("Error while reading resource mappings for API: " + api.getId().getApiName(), e);
        } catch (IOException e) {
            log.error("Error while reading the swagger definition for API: " + api.getId().getApiName(), e);
        }
        return serviceCatalogAPIResourceMappings;
    }
}
Also used : APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) APIDefinition(org.wso2.carbon.apimgt.api.APIDefinition) API(org.wso2.carbon.apimgt.api.model.API) APIIdentifier(org.wso2.carbon.apimgt.api.model.APIIdentifier) IOException(java.io.IOException)

Example 95 with APIDefinition

use of org.wso2.carbon.apimgt.api.doc.model.APIDefinition in project carbon-apimgt by wso2.

the class RestApiUtil method getScopesInfoFromAPIYamlDefinitions.

/**
 * This method is used to get the scope list from the yaml file
 *
 * @return MAP of scope list for all portal
 */
public static Map<String, List<String>> getScopesInfoFromAPIYamlDefinitions() throws APIManagementException {
    Map<String, List<String>> portalScopeList = new HashMap<>();
    String[] fileNameArray = { "/admin-api.yaml", "/publisher-api.yaml", "/devportal-api.yaml", "/service-catalog-api.yaml" };
    for (String fileName : fileNameArray) {
        String definition = null;
        try {
            definition = IOUtils.toString(RestApiUtil.class.getResourceAsStream(fileName), "UTF-8");
        } catch (IOException e) {
            throw new APIManagementException("Error while reading the swagger definition ,", ExceptionCodes.DEFINITION_EXCEPTION);
        }
        APIDefinition oasParser = OASParserUtil.getOASParser(definition);
        Set<Scope> scopeSet = oasParser.getScopes(definition);
        for (Scope entry : scopeSet) {
            List<String> list = new ArrayList<>();
            list.add(entry.getDescription());
            list.add((fileName.replaceAll("-api.yaml", "").replace("/", "")));
            portalScopeList.put(entry.getName(), list);
        }
    }
    return portalScopeList;
}
Also used : APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) Scope(org.wso2.carbon.apimgt.api.model.Scope) HashMap(java.util.HashMap) APIDefinition(org.wso2.carbon.apimgt.api.APIDefinition) ArrayList(java.util.ArrayList) List(java.util.List) ArrayList(java.util.ArrayList) IOException(java.io.IOException)

Aggregations

APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)32 HashMap (java.util.HashMap)30 APIDefinition (org.wso2.carbon.apimgt.api.APIDefinition)30 API (org.wso2.carbon.apimgt.core.models.API)30 CompositeAPI (org.wso2.carbon.apimgt.core.models.CompositeAPI)25 ArrayList (java.util.ArrayList)23 API (org.wso2.carbon.apimgt.api.model.API)20 Map (java.util.Map)19 IOException (java.io.IOException)18 Test (org.testng.annotations.Test)18 ApiDAO (org.wso2.carbon.apimgt.core.dao.ApiDAO)18 URITemplate (org.wso2.carbon.apimgt.api.model.URITemplate)16 APIIdentifier (org.wso2.carbon.apimgt.api.model.APIIdentifier)15 Scope (org.wso2.carbon.apimgt.api.model.Scope)15 HashSet (java.util.HashSet)13 APIPolicy (org.wso2.carbon.apimgt.core.models.policy.APIPolicy)13 APIDefinitionValidationResponse (org.wso2.carbon.apimgt.api.APIDefinitionValidationResponse)12 SwaggerData (org.wso2.carbon.apimgt.api.model.SwaggerData)12 BusinessInformation (org.wso2.carbon.apimgt.core.models.BusinessInformation)12 CorsConfiguration (org.wso2.carbon.apimgt.core.models.CorsConfiguration)12