Search in sources :

Example 6 with SolaceAdminApis

use of org.wso2.carbon.apimgt.solace.SolaceAdminApis in project carbon-apimgt by wso2.

the class SolaceNotifierUtils method unsubscribeAPIProductFromSolaceApplication.

/**
 * Unsubscribe the given API product from the Solace application
 *
 * @param api         API object to be unsubscribed
 * @param application Solace application
 * @throws APIManagementException is error occurs when unsubscribing the API from application
 */
public static void unsubscribeAPIProductFromSolaceApplication(API api, Application application) throws APIManagementException {
    List<Environment> deployedSolaceEnvironments = getDeployedSolaceEnvironmentsFromRevisionDeployments(api);
    String applicationOrganizationName = getSolaceOrganizationName(deployedSolaceEnvironments);
    ArrayList<String> solaceApiProducts = new ArrayList<>();
    if (applicationOrganizationName != null) {
        for (Environment environment : deployedSolaceEnvironments) {
            solaceApiProducts.add(generateApiProductNameForSolaceBroker(api, environment.getName()));
        }
        SolaceAdminApis solaceAdminApis = SolaceNotifierUtils.getSolaceAdminApis();
        CloseableHttpResponse response = solaceAdminApis.applicationPatchRemoveSubscription(applicationOrganizationName, application, solaceApiProducts);
        if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
            try {
                String responseString = EntityUtils.toString(response.getEntity());
                org.json.JSONObject jsonObject = new org.json.JSONObject(responseString);
                String applicationName = String.valueOf(jsonObject.get("displayName"));
                log.info("API product unsubscribed from Solace application '" + applicationName + "'");
                if (jsonObject.getJSONArray("apiProducts") != null) {
                    if (jsonObject.getJSONArray("apiProducts").length() == 0) {
                        // delete application in Solace because of 0 number of api products
                        CloseableHttpResponse response2 = solaceAdminApis.deleteApplication(applicationOrganizationName, application.getUUID());
                        if (response2.getStatusLine().getStatusCode() == HttpStatus.SC_NO_CONTENT) {
                            log.info("Successfully deleted application '" + applicationName + "' in " + "Solace Broker");
                        } else {
                            if (log.isDebugEnabled()) {
                                log.error("Error while deleting application '" + applicationName + "' " + "in Solace. : " + response2.getStatusLine().toString());
                            }
                            throw new APIManagementException("Error while deleting application '" + applicationName + "' in Solace");
                        }
                    }
                }
            } catch (IOException e) {
                log.error(e.getMessage());
                throw new APIManagementException("Error while deleting application in Solace");
            }
        } else {
            if (log.isDebugEnabled()) {
                log.error("Error while unsubscribing API product from Solace Application '" + application.getName() + " : " + response.getStatusLine().toString());
            }
            throw new APIManagementException(response.getStatusLine().getStatusCode() + "-" + response.getStatusLine().getReasonPhrase());
        }
    } else {
        throw new APIManagementException("Multiple Solace organizations found");
    }
}
Also used : ArrayList(java.util.ArrayList) IOException(java.io.IOException) SolaceAdminApis(org.wso2.carbon.apimgt.solace.SolaceAdminApis) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) Environment(org.wso2.carbon.apimgt.api.model.Environment)

Example 7 with SolaceAdminApis

use of org.wso2.carbon.apimgt.solace.SolaceAdminApis 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 8 with SolaceAdminApis

use of org.wso2.carbon.apimgt.solace.SolaceAdminApis 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 9 with SolaceAdminApis

use of org.wso2.carbon.apimgt.solace.SolaceAdminApis in project carbon-apimgt by wso2.

the class SolaceNotifierUtils method checkApiProductAlreadyDeployedIntoSolaceEnvironments.

/**
 * Check whether the given API product is already deployed in the Solace environment
 *
 * @param api          Name of the API
 * @param environments List of the environments
 * @return returns true if the given API product is already deployed in one of environments
 * @throws IOException            If an error occurs when checking API product availability
 * @throws APIManagementException if an error occurs when getting Solace config
 */
public static boolean checkApiProductAlreadyDeployedIntoSolaceEnvironments(API api, List<Environment> environments) throws IOException, APIManagementException {
    int numberOfDeployedEnvironmentsInSolace = 0;
    for (Environment environment : environments) {
        String apiNameWithContext = generateApiProductNameForSolaceBroker(api, environment.getName());
        SolaceAdminApis solaceAdminApis = SolaceNotifierUtils.getSolaceAdminApis();
        CloseableHttpResponse response = solaceAdminApis.apiProductGet(environment.getAdditionalProperties().get(SolaceConstants.SOLACE_ENVIRONMENT_ORGANIZATION), apiNameWithContext);
        if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
            if (log.isDebugEnabled()) {
                log.info("API product found in Solace Broker");
            }
            numberOfDeployedEnvironmentsInSolace++;
        } else if (response.getStatusLine().getStatusCode() == HttpStatus.SC_NOT_FOUND) {
            if (log.isDebugEnabled()) {
                log.error("API product not found in Solace broker");
                log.error(EntityUtils.toString(response.getEntity()));
            }
            throw new HttpResponseException(response.getStatusLine().getStatusCode(), response.getStatusLine().getReasonPhrase());
        } else {
            if (log.isDebugEnabled()) {
                log.error("Cannot find API product in Solace Broker");
                log.error(EntityUtils.toString(response.getEntity()));
            }
            throw new HttpResponseException(response.getStatusLine().getStatusCode(), response.getStatusLine().getReasonPhrase());
        }
    }
    return numberOfDeployedEnvironmentsInSolace == environments.size();
}
Also used : SolaceAdminApis(org.wso2.carbon.apimgt.solace.SolaceAdminApis) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) Environment(org.wso2.carbon.apimgt.api.model.Environment) HttpResponseException(org.apache.http.client.HttpResponseException)

Example 10 with SolaceAdminApis

use of org.wso2.carbon.apimgt.solace.SolaceAdminApis in project carbon-apimgt by wso2.

the class SolaceNotifierUtils method getSolaceAdminApis.

/**
 * Get and patch client id for Solace application
 *
 * @return SolaceAdminApis  object to invoke Solace
 * @throws APIManagementException If the Solace env configuration if not provided properly
 */
public static SolaceAdminApis getSolaceAdminApis() throws APIManagementException {
    Map<String, Environment> thirdPartyEnvironments = APIUtil.getReadOnlyGatewayEnvironments();
    Environment solaceEnvironment = null;
    for (Map.Entry<String, Environment> entry : thirdPartyEnvironments.entrySet()) {
        if (SolaceConstants.SOLACE_ENVIRONMENT.equals(entry.getValue().getProvider())) {
            solaceEnvironment = entry.getValue();
        }
    }
    if (solaceEnvironment != null) {
        return new SolaceAdminApis(solaceEnvironment.getServerURL(), solaceEnvironment.getUserName(), solaceEnvironment.getPassword(), solaceEnvironment.getAdditionalProperties().get(SolaceConstants.SOLACE_ENVIRONMENT_DEV_NAME));
    } else {
        throw new APIManagementException("Solace Environment configurations are not provided properly");
    }
}
Also used : SolaceAdminApis(org.wso2.carbon.apimgt.solace.SolaceAdminApis) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) Environment(org.wso2.carbon.apimgt.api.model.Environment) Map(java.util.Map)

Aggregations

SolaceAdminApis (org.wso2.carbon.apimgt.solace.SolaceAdminApis)13 CloseableHttpResponse (org.apache.http.client.methods.CloseableHttpResponse)9 APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)9 Environment (org.wso2.carbon.apimgt.api.model.Environment)6 ArrayList (java.util.ArrayList)5 IOException (java.io.IOException)4 HttpResponse (org.apache.http.HttpResponse)3 HttpResponseException (org.apache.http.client.HttpResponseException)3 JSONArray (org.json.JSONArray)3 DeployerException (org.wso2.carbon.apimgt.impl.deployer.exceptions.DeployerException)2 SolaceURLsDTO (org.wso2.carbon.apimgt.solace.dtos.SolaceURLsDTO)2 Aai20Document (io.apicurio.datamodels.asyncapi.v2.models.Aai20Document)1 List (java.util.List)1 Map (java.util.Map)1 JSONObject (org.json.JSONObject)1 APIRevisionDeployment (org.wso2.carbon.apimgt.api.model.APIRevisionDeployment)1 AsyncProtocolEndpoint (org.wso2.carbon.apimgt.api.model.AsyncProtocolEndpoint)1 SubscribedAPI (org.wso2.carbon.apimgt.api.model.SubscribedAPI)1 Subscriber (org.wso2.carbon.apimgt.api.model.Subscriber)1 NotifierException (org.wso2.carbon.apimgt.impl.notifier.exceptions.NotifierException)1