Search in sources :

Example 1 with NotifierException

use of org.wso2.carbon.apimgt.impl.notifier.exceptions.NotifierException in project carbon-apimgt by wso2.

the class ExternallyDeployedApiNotifier method process.

/**
 * Process API lifecycle notifier events related to APIs deployed in external gateway
 *
 * @param event related to deployments
 * @throws NotifierException if error occurs when casting event
 */
private void process(Event event) throws NotifierException {
    APIEvent apiEvent;
    apiEvent = (APIEvent) event;
    if (APIConstants.EventType.API_LIFECYCLE_CHANGE.name().equals(event.getType())) {
        // Handle API retiring life cycle change in external gateway
        undeployApiWhenRetiring(apiEvent);
    } else if (APIConstants.EventType.API_DELETE.name().equals(event.getType())) {
        // Handle API deletion in external gateway
        undeployWhenDeleting(apiEvent);
    }
}
Also used : APIEvent(org.wso2.carbon.apimgt.impl.notifier.events.APIEvent)

Example 2 with NotifierException

use of org.wso2.carbon.apimgt.impl.notifier.exceptions.NotifierException in project carbon-apimgt by wso2.

the class ExternallyDeployedApiNotifier method undeployApiWhenRetiring.

/**
 * Undeploy APIs from external gateway when life cycle state changed to retire
 *
 * @param apiEvent APIEvent to undeploy APIs from external gateway
 * @throws NotifierException if error occurs when undeploying APIs from external gateway
 */
private void undeployApiWhenRetiring(APIEvent apiEvent) throws NotifierException {
    apiMgtDAO = ApiMgtDAO.getInstance();
    Map<String, Environment> gatewayEnvironments = APIUtil.getReadOnlyGatewayEnvironments();
    boolean deleted;
    String apiId = apiEvent.getUuid();
    if (!APIConstants.RETIRED.equals(apiEvent.getApiStatus())) {
        return;
    }
    try {
        APIProvider apiProvider = APIManagerFactory.getInstance().getAPIProvider(CarbonContext.getThreadLocalCarbonContext().getUsername());
        API api = apiProvider.getAPIbyUUID(apiId, apiMgtDAO.getOrganizationByAPIUUID(apiId));
        List<APIRevisionDeployment> test = apiMgtDAO.getAPIRevisionDeploymentsByApiUUID(apiId);
        for (APIRevisionDeployment deployment : test) {
            String deploymentEnv = deployment.getDeployment();
            if (gatewayEnvironments.containsKey(deploymentEnv)) {
                ExternalGatewayDeployer deployer = ServiceReferenceHolder.getInstance().getExternalGatewayDeployer(gatewayEnvironments.get(deploymentEnv).getProvider());
                if (deployer != null) {
                    try {
                        deleted = deployer.undeployWhenRetire(api, gatewayEnvironments.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) APIRevisionDeployment(org.wso2.carbon.apimgt.api.model.APIRevisionDeployment) APIProvider(org.wso2.carbon.apimgt.api.APIProvider) NotifierException(org.wso2.carbon.apimgt.impl.notifier.exceptions.NotifierException)

Example 3 with NotifierException

use of org.wso2.carbon.apimgt.impl.notifier.exceptions.NotifierException 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 4 with NotifierException

use of org.wso2.carbon.apimgt.impl.notifier.exceptions.NotifierException 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)

Example 5 with NotifierException

use of org.wso2.carbon.apimgt.impl.notifier.exceptions.NotifierException in project carbon-apimgt by wso2.

the class SolaceSubscriptionsNotifier method crateSubscription.

/**
 * Create subscriptions to Solace APIs
 *
 * @param event SubscriptionEvent to create Solace API subscriptions
 * @throws NotifierException if error occurs when creating subscription for Solace APIs
 */
private void crateSubscription(SubscriptionEvent event) throws NotifierException {
    String apiUUID = event.getApiUUID();
    String applicationUUID = event.getApplicationUUID();
    try {
        APIProvider apiProvider = APIManagerFactory.getInstance().getAPIProvider(CarbonContext.getThreadLocalCarbonContext().getUsername());
        API api = apiProvider.getAPIbyUUID(apiUUID, apiMgtDAO.getOrganizationByAPIUUID(apiUUID));
        APIConsumer apiConsumer = APIManagerFactory.getInstance().getAPIConsumer(CarbonContext.getThreadLocalCarbonContext().getUsername());
        Application application = apiMgtDAO.getApplicationByUUID(applicationUUID);
        Set<APIKey> consumerKeys = apiConsumer.getApplicationKeysOfApplication(application.getId());
        for (APIKey apiKey : consumerKeys) {
            application.addKey(apiKey);
        }
        // Check whether the subscription is belongs to an API deployed in Solace
        if (SolaceConstants.SOLACE_ENVIRONMENT.equals(api.getGatewayVendor())) {
            ArrayList<String> solaceApiProducts = new ArrayList<>();
            List<Environment> deployedSolaceEnvironments = SolaceNotifierUtils.getDeployedSolaceEnvironmentsFromRevisionDeployments(api);
            String applicationOrganizationName = SolaceNotifierUtils.getSolaceOrganizationName(deployedSolaceEnvironments);
            if (applicationOrganizationName != null) {
                try {
                    boolean apiProductDeployedIntoSolace = SolaceNotifierUtils.checkApiProductAlreadyDeployedIntoSolaceEnvironments(api, deployedSolaceEnvironments);
                    if (apiProductDeployedIntoSolace) {
                        for (Environment environment : deployedSolaceEnvironments) {
                            solaceApiProducts.add(SolaceNotifierUtils.generateApiProductNameForSolaceBroker(api, environment.getName()));
                        }
                        SolaceNotifierUtils.deployApplicationToSolaceBroker(application, solaceApiProducts, applicationOrganizationName);
                    }
                } catch (IOException e) {
                    log.error(e.getMessage());
                }
            } else {
                if (log.isDebugEnabled()) {
                    log.error("Cannot create solace application " + application.getName() + "with API product " + "deployed in different organizations...");
                }
                throw new APIManagementException("Cannot create solace application " + application.getName() + "with API product deployed in different organizations...");
            }
        }
    } catch (APIManagementException e) {
        throw new NotifierException(e.getMessage());
    }
}
Also used : ArrayList(java.util.ArrayList) IOException(java.io.IOException) APIProvider(org.wso2.carbon.apimgt.api.APIProvider) NotifierException(org.wso2.carbon.apimgt.impl.notifier.exceptions.NotifierException) APIKey(org.wso2.carbon.apimgt.api.model.APIKey) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) Environment(org.wso2.carbon.apimgt.api.model.Environment) API(org.wso2.carbon.apimgt.api.model.API) APIConsumer(org.wso2.carbon.apimgt.api.APIConsumer) Application(org.wso2.carbon.apimgt.api.model.Application)

Aggregations

APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)10 NotifierException (org.wso2.carbon.apimgt.impl.notifier.exceptions.NotifierException)10 Environment (org.wso2.carbon.apimgt.api.model.Environment)9 APIProvider (org.wso2.carbon.apimgt.api.APIProvider)6 API (org.wso2.carbon.apimgt.api.model.API)6 APIRevisionDeployment (org.wso2.carbon.apimgt.api.model.APIRevisionDeployment)5 Application (org.wso2.carbon.apimgt.api.model.Application)5 ArrayList (java.util.ArrayList)4 ExternalGatewayDeployer (org.wso2.carbon.apimgt.impl.deployer.ExternalGatewayDeployer)4 DeployerException (org.wso2.carbon.apimgt.impl.deployer.exceptions.DeployerException)4 List (java.util.List)3 APIConsumer (org.wso2.carbon.apimgt.api.APIConsumer)3 APIKey (org.wso2.carbon.apimgt.api.model.APIKey)3 SubscribedAPI (org.wso2.carbon.apimgt.api.model.SubscribedAPI)3 IOException (java.io.IOException)2 Subscriber (org.wso2.carbon.apimgt.api.model.Subscriber)2 CloseableHttpResponse (org.apache.http.client.methods.CloseableHttpResponse)1 APIEvent (org.wso2.carbon.apimgt.impl.notifier.events.APIEvent)1 ApplicationEvent (org.wso2.carbon.apimgt.impl.notifier.events.ApplicationEvent)1 ApplicationRegistrationEvent (org.wso2.carbon.apimgt.impl.notifier.events.ApplicationRegistrationEvent)1