Search in sources :

Example 26 with Broker

use of org.wso2.carbon.apimgt.core.api.Broker in project carbon-apimgt by wso2.

the class ExternalGatewayNotifier method unDeployApi.

/**
 * Undeploy APIs from external gateway
 *
 * @param deployAPIInGatewayEvent DeployAPIInGatewayEvent to undeploy APIs from external gateway
 * @throws NotifierException if error occurs when undeploying APIs from external gateway
 */
private void unDeployApi(DeployAPIInGatewayEvent deployAPIInGatewayEvent) throws NotifierException {
    boolean deleted;
    Set<String> gateways = deployAPIInGatewayEvent.getGatewayLabels();
    String apiId = deployAPIInGatewayEvent.getUuid();
    try {
        Map<String, Environment> environments = APIUtil.getEnvironments(deployAPIInGatewayEvent.getTenantDomain());
        APIProvider apiProvider = APIManagerFactory.getInstance().getAPIProvider(deployAPIInGatewayEvent.getProvider());
        API api = apiProvider.getAPIbyUUID(apiId, apiMgtDAO.getOrganizationByAPIUUID(apiId));
        for (String deploymentEnv : gateways) {
            if (environments.containsKey(deploymentEnv)) {
                ExternalGatewayDeployer deployer = ServiceReferenceHolder.getInstance().getExternalGatewayDeployer(environments.get(deploymentEnv).getProvider());
                if (deployer != null) {
                    try {
                        deleted = deployer.undeploy(api.getId().getName(), api.getId().getVersion(), api.getContext(), environments.get(deploymentEnv));
                        if (!deleted) {
                            throw new NotifierException("Error while deleting API product from Solace broker");
                        }
                    } catch (DeployerException e) {
                        throw new NotifierException(e.getMessage());
                    }
                }
            }
        }
    } catch (APIManagementException e) {
        throw new NotifierException(e.getMessage());
    }
}
Also used : APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) ExternalGatewayDeployer(org.wso2.carbon.apimgt.impl.deployer.ExternalGatewayDeployer) Environment(org.wso2.carbon.apimgt.api.model.Environment) DeployerException(org.wso2.carbon.apimgt.impl.deployer.exceptions.DeployerException) API(org.wso2.carbon.apimgt.api.model.API) APIProvider(org.wso2.carbon.apimgt.api.APIProvider) NotifierException(org.wso2.carbon.apimgt.impl.notifier.exceptions.NotifierException)

Example 27 with Broker

use of org.wso2.carbon.apimgt.core.api.Broker 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 28 with Broker

use of org.wso2.carbon.apimgt.core.api.Broker in project carbon-apimgt by wso2.

the class SolaceNotifierUtils method getThirdPartySolaceBrokerOrganizationNameOfAPIDeployment.

/**
 * Get third party Solace broker organization Name for API deployment
 *
 * @param api Name of the API
 * @return String of the name of organization in Solace broker
 * @throws APIManagementException is error occurs when getting the name of the organization name
 */
public static String getThirdPartySolaceBrokerOrganizationNameOfAPIDeployment(API api) throws APIManagementException {
    apiMgtDAO = ApiMgtDAO.getInstance();
    Map<String, Environment> gatewayEnvironments = APIUtil.getReadOnlyGatewayEnvironments();
    List<APIRevisionDeployment> deployments = apiMgtDAO.getAPIRevisionDeploymentByApiUUID(api.getUuid());
    for (APIRevisionDeployment deployment : deployments) {
        if (deployment.isDisplayOnDevportal()) {
            String environmentName = deployment.getDeployment();
            if (gatewayEnvironments.containsKey(environmentName)) {
                Environment deployedEnvironment = gatewayEnvironments.get(environmentName);
                if (SolaceConstants.SOLACE_ENVIRONMENT.equalsIgnoreCase(deployedEnvironment.getProvider())) {
                    return deployedEnvironment.getAdditionalProperties().get(SolaceConstants.SOLACE_ENVIRONMENT_ORGANIZATION);
                }
            }
        }
    }
    return null;
}
Also used : Environment(org.wso2.carbon.apimgt.api.model.Environment) APIRevisionDeployment(org.wso2.carbon.apimgt.api.model.APIRevisionDeployment)

Example 29 with Broker

use of org.wso2.carbon.apimgt.core.api.Broker in project carbon-apimgt by wso2.

the class SolaceApplicationNotifier method removeSolaceApplication.

/**
 * Remove applications from Solace broker
 *
 * @param event ApplicationEvent to remove Solace applications
 * @throws NotifierException if error occurs when removing applications from Solace broker
 */
private void removeSolaceApplication(ApplicationEvent event) throws NotifierException {
    // get list of subscribed APIs in the application
    Subscriber subscriber = new Subscriber(event.getSubscriber());
    try {
        Set<SubscribedAPI> subscriptions = apiMgtDAO.getSubscribedAPIs(subscriber, event.getApplicationName(), event.getGroupId());
        List<SubscribedAPI> subscribedApiList = new ArrayList<>(subscriptions);
        boolean hasSubscribedAPIDeployedInSolace = false;
        String organizationNameOfSolaceDeployment = null;
        Map<String, Environment> gatewayEnvironments = APIUtil.getReadOnlyGatewayEnvironments();
        labelOne: for (SubscribedAPI api : subscribedApiList) {
            List<APIRevisionDeployment> deployments = apiMgtDAO.getAPIRevisionDeploymentByApiUUID(api.getUUID());
            for (APIRevisionDeployment deployment : deployments) {
                if (gatewayEnvironments.containsKey(deployment.getDeployment())) {
                    if (SolaceConstants.SOLACE_ENVIRONMENT.equalsIgnoreCase(gatewayEnvironments.get(deployment.getDeployment()).getProvider())) {
                        hasSubscribedAPIDeployedInSolace = true;
                        organizationNameOfSolaceDeployment = gatewayEnvironments.get(deployment.getDeployment()).getAdditionalProperties().get(SolaceConstants.SOLACE_ENVIRONMENT_ORGANIZATION);
                        break labelOne;
                    }
                }
            }
        }
        boolean applicationFoundInSolaceBroker = false;
        if (hasSubscribedAPIDeployedInSolace) {
            SolaceAdminApis solaceAdminApis = SolaceNotifierUtils.getSolaceAdminApis();
            // check existence of application in Solace Broker
            CloseableHttpResponse response1 = solaceAdminApis.applicationGet(organizationNameOfSolaceDeployment, event.getUuid(), "default");
            if (response1.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
                applicationFoundInSolaceBroker = true;
                if (log.isDebugEnabled()) {
                    log.info("Found application '" + event.getApplicationName() + "' in Solace broker");
                    log.info("Waiting until application removing workflow gets finished");
                }
            } else if (response1.getStatusLine().getStatusCode() == HttpStatus.SC_NOT_FOUND) {
                throw new NotifierException("Application '" + event.getApplicationName() + "' cannot be found in " + "Solace Broker");
            } else {
                if (log.isDebugEnabled()) {
                    log.error("Error while searching for application '" + event.getApplicationName() + "'" + " in Solace Broker. : " + response1.getStatusLine().toString());
                }
                throw new NotifierException("Error while searching for application '" + event.getApplicationName() + "' in Solace Broker");
            }
        }
        if (applicationFoundInSolaceBroker) {
            log.info("Deleting application from Solace Broker");
            // delete application from solace
            SolaceAdminApis solaceAdminApis = SolaceNotifierUtils.getSolaceAdminApis();
            CloseableHttpResponse response2 = solaceAdminApis.deleteApplication(organizationNameOfSolaceDeployment, event.getUuid());
            if (response2.getStatusLine().getStatusCode() == HttpStatus.SC_NO_CONTENT) {
                log.info("Successfully deleted application '" + event.getApplicationName() + "' " + "in Solace Broker");
            } else {
                if (log.isDebugEnabled()) {
                    log.error("Error while deleting application " + event.getApplicationName() + " in Solace. :" + response2.getStatusLine().toString());
                }
                throw new NotifierException("Error while deleting application '" + event.getApplicationName() + "' in Solace");
            }
        }
    } catch (APIManagementException e) {
        throw new NotifierException(e.getMessage());
    }
}
Also used : ArrayList(java.util.ArrayList) APIRevisionDeployment(org.wso2.carbon.apimgt.api.model.APIRevisionDeployment) NotifierException(org.wso2.carbon.apimgt.impl.notifier.exceptions.NotifierException) SolaceAdminApis(org.wso2.carbon.apimgt.solace.SolaceAdminApis) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) Subscriber(org.wso2.carbon.apimgt.api.model.Subscriber) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) SubscribedAPI(org.wso2.carbon.apimgt.api.model.SubscribedAPI) Environment(org.wso2.carbon.apimgt.api.model.Environment) ArrayList(java.util.ArrayList) List(java.util.List)

Example 30 with Broker

use of org.wso2.carbon.apimgt.core.api.Broker in project carbon-apimgt by wso2.

the class SolaceApplicationNotifier method renameSolaceApplication.

/**
 * Rename applications on the Solace broker
 *
 * @param event ApplicationEvent to rename Solace applications
 * @throws NotifierException if error occurs when renaming applications on the Solace broker
 */
private void renameSolaceApplication(ApplicationEvent event) throws NotifierException {
    // get list of subscribed APIs in the application
    Subscriber subscriber = new Subscriber(event.getSubscriber());
    try {
        Application application = apiMgtDAO.getApplicationByUUID(event.getUuid());
        Set<SubscribedAPI> subscriptions = apiMgtDAO.getSubscribedAPIs(subscriber, event.getApplicationName(), event.getGroupId());
        Map<String, Environment> gatewayEnvironments = APIUtil.getReadOnlyGatewayEnvironments();
        boolean isContainsSolaceApis = false;
        String organizationNameOfSolaceDeployment = null;
        labelOne: // Check whether the application needs to be updated has a Solace API subscription
        for (SubscribedAPI api : subscriptions) {
            List<APIRevisionDeployment> deployments = apiMgtDAO.getAPIRevisionDeploymentByApiUUID(api.getIdentifier().getUUID());
            for (APIRevisionDeployment deployment : deployments) {
                if (gatewayEnvironments.containsKey(deployment.getDeployment())) {
                    if (SolaceConstants.SOLACE_ENVIRONMENT.equalsIgnoreCase(gatewayEnvironments.get(deployment.getDeployment()).getProvider())) {
                        isContainsSolaceApis = true;
                        organizationNameOfSolaceDeployment = gatewayEnvironments.get(deployment.getDeployment()).getAdditionalProperties().get(SolaceConstants.SOLACE_ENVIRONMENT_ORGANIZATION);
                        break labelOne;
                    }
                }
            }
        }
        // Renaming application using Solace Admin Apis
        if (isContainsSolaceApis) {
            SolaceNotifierUtils.renameSolaceApplication(organizationNameOfSolaceDeployment, application);
        }
    } catch (APIManagementException e) {
        throw new NotifierException(e.getMessage());
    }
}
Also used : APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) Subscriber(org.wso2.carbon.apimgt.api.model.Subscriber) SubscribedAPI(org.wso2.carbon.apimgt.api.model.SubscribedAPI) Environment(org.wso2.carbon.apimgt.api.model.Environment) ArrayList(java.util.ArrayList) List(java.util.List) APIRevisionDeployment(org.wso2.carbon.apimgt.api.model.APIRevisionDeployment) Application(org.wso2.carbon.apimgt.api.model.Application) NotifierException(org.wso2.carbon.apimgt.impl.notifier.exceptions.NotifierException)

Aggregations

Test (org.testng.annotations.Test)19 APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)19 SiddhiAppRuntime (org.wso2.siddhi.core.SiddhiAppRuntime)16 SiddhiManager (org.wso2.siddhi.core.SiddhiManager)16 InMemoryBroker (org.wso2.siddhi.core.util.transport.InMemoryBroker)16 Environment (org.wso2.carbon.apimgt.api.model.Environment)15 InputHandler (org.wso2.siddhi.core.stream.input.InputHandler)13 ArrayList (java.util.ArrayList)11 SolaceAdminApis (org.wso2.carbon.apimgt.solace.SolaceAdminApis)9 APIRevisionDeployment (org.wso2.carbon.apimgt.api.model.APIRevisionDeployment)8 CloseableHttpResponse (org.apache.http.client.methods.CloseableHttpResponse)7 DeployerException (org.wso2.carbon.apimgt.impl.deployer.exceptions.DeployerException)7 NotifierException (org.wso2.carbon.apimgt.impl.notifier.exceptions.NotifierException)7 IOException (java.io.IOException)6 Broker (org.wso2.carbon.apimgt.core.api.Broker)6 Test (org.junit.Test)5 API (org.wso2.carbon.apimgt.core.models.API)5 APIProvider (org.wso2.carbon.apimgt.api.APIProvider)4 ExternalGatewayDeployer (org.wso2.carbon.apimgt.impl.deployer.ExternalGatewayDeployer)4 JsonParser (com.google.gson.JsonParser)3