Search in sources :

Example 6 with DeployerException

use of org.wso2.carbon.apimgt.impl.deployer.exceptions.DeployerException in project carbon-apimgt by wso2.

the class SolaceBrokerDeployer method undeploy.

/**
 * Undeploy API artifact from provided environment
 *
 * @param apiName     Name of the API to be undeployed from Solace broker
 * @param apiVersion  Version of the API to be undeployed from Solace broker
 * @param apiContext  Context of the API to be undeployed from Solace broker
 * @param environment Environment needed to be undeployed API from
 * @throws DeployerException if error occurs when undeploying APIs from Solace broker
 */
@Override
public boolean undeploy(String apiName, String apiVersion, String apiContext, Environment environment) throws DeployerException {
    String apiNameForRegistration = apiName + "-" + apiVersion;
    String[] apiContextParts = apiContext.split("/");
    String apiNameWithContext = environment.getName() + "-" + apiName + "-" + apiContextParts[1] + "-" + apiContextParts[2];
    SolaceAdminApis solaceAdminApis;
    try {
        solaceAdminApis = SolaceNotifierUtils.getSolaceAdminApis();
    } catch (APIManagementException e) {
        throw new DeployerException(e.getMessage());
    }
    // delete API product from Solace
    CloseableHttpResponse response1 = solaceAdminApis.deleteApiProduct(environment.getAdditionalProperties().get(SolaceConstants.SOLACE_ENVIRONMENT_ORGANIZATION), apiNameWithContext);
    if (response1.getStatusLine().getStatusCode() == HttpStatus.SC_NO_CONTENT) {
        log.info("API product '" + apiNameWithContext + "' has been deleted from Solace Broker");
        // delete registered API from Solace
        CloseableHttpResponse response2 = solaceAdminApis.deleteRegisteredAPI(environment.getAdditionalProperties().get(SolaceConstants.SOLACE_ENVIRONMENT_ORGANIZATION), apiNameForRegistration);
        if (response2.getStatusLine().getStatusCode() == HttpStatus.SC_NO_CONTENT) {
            if (log.isDebugEnabled()) {
                log.info("API product '" + apiNameWithContext + "' and API '" + apiNameForRegistration + "' have " + "been deleted from Solace broker");
            }
            return true;
        } else if (response2.getStatusLine().getStatusCode() == HttpStatus.SC_CONFLICT) {
            if (log.isDebugEnabled()) {
                log.info("Registered API '" + apiNameWithContext + "' is still referenced for another API product. " + "Skipping API Deletion....");
            }
            return true;
        } else {
            if (log.isDebugEnabled()) {
                log.error("Error occurred while deleting the API '" + apiNameForRegistration + "' from Solace " + "Broker. :" + response2.getStatusLine().toString());
            }
            throw new DeployerException(response2.getStatusLine().toString());
        }
    } else if (response1.getStatusLine().getStatusCode() == HttpStatus.SC_CONFLICT) {
        if (log.isDebugEnabled()) {
            log.error("Cannot undeploy. Solace API product '" + apiNameWithContext + "' is subscribed for a solace " + "Application.");
        }
        throw new DeployerException(response1.getStatusLine().toString());
    } else if (response1.getStatusLine().getStatusCode() == HttpStatus.SC_NOT_FOUND) {
        if (log.isDebugEnabled()) {
            log.warn("Cannot undeploy. Solace API product '" + apiNameWithContext + "' is already " + " un deployed.");
        }
        return true;
    } else {
        if (log.isDebugEnabled()) {
            log.error("Error occurred while deleting the API Product '" + apiNameWithContext + "' from Solace " + "Broker. :" + 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) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) DeployerException(org.wso2.carbon.apimgt.impl.deployer.exceptions.DeployerException)

Example 7 with DeployerException

use of org.wso2.carbon.apimgt.impl.deployer.exceptions.DeployerException 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

APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)7 DeployerException (org.wso2.carbon.apimgt.impl.deployer.exceptions.DeployerException)7 APIProvider (org.wso2.carbon.apimgt.api.APIProvider)4 Environment (org.wso2.carbon.apimgt.api.model.Environment)4 ExternalGatewayDeployer (org.wso2.carbon.apimgt.impl.deployer.ExternalGatewayDeployer)4 NotifierException (org.wso2.carbon.apimgt.impl.notifier.exceptions.NotifierException)4 API (org.wso2.carbon.apimgt.api.model.API)3 CloseableHttpResponse (org.apache.http.client.methods.CloseableHttpResponse)2 APIRevisionDeployment (org.wso2.carbon.apimgt.api.model.APIRevisionDeployment)2 SolaceAdminApis (org.wso2.carbon.apimgt.solace.SolaceAdminApis)2 Aai20Document (io.apicurio.datamodels.asyncapi.v2.models.Aai20Document)1 Application (org.wso2.carbon.apimgt.api.model.Application)1 SubscribedAPI (org.wso2.carbon.apimgt.api.model.SubscribedAPI)1