Search in sources :

Example 6 with ArtifactSynchronizerException

use of org.wso2.carbon.apimgt.impl.gatewayartifactsynchronizer.exception.ArtifactSynchronizerException in project carbon-apimgt by wso2.

the class InMemoryAPIDeployer method deployAPI.

/**
 * Deploy an API in the gateway using the deployAPI method in gateway admin.
 *
 * @param gatewayEvent Gateway Deployment event.
 * @return True if API artifact retrieved from the storage and successfully deployed without any error. else false
 */
public boolean deployAPI(DeployAPIInGatewayEvent gatewayEvent) throws ArtifactSynchronizerException {
    String apiId = gatewayEvent.getUuid();
    Set<String> gatewayLabels = gatewayEvent.getGatewayLabels();
    try {
        GatewayAPIDTO gatewayAPIDTO = retrieveArtifact(apiId, gatewayLabels);
        if (gatewayAPIDTO != null) {
            APIGatewayAdmin apiGatewayAdmin = new APIGatewayAdmin();
            MessageContext.setCurrentMessageContext(org.wso2.carbon.apimgt.gateway.utils.GatewayUtils.createAxis2MessageContext());
            unDeployAPI(apiGatewayAdmin, gatewayEvent);
            apiGatewayAdmin.deployAPI(gatewayAPIDTO);
            addDeployedCertificatesToAPIAssociation(gatewayAPIDTO);
            addDeployedGraphqlQLToAPI(gatewayAPIDTO);
            DataHolder.getInstance().addKeyManagerToAPIMapping(apiId, gatewayAPIDTO.getKeyManagers());
            if (debugEnabled) {
                log.debug("API with " + apiId + " is deployed in gateway with the labels " + String.join(",", gatewayLabels));
            }
            return true;
        }
    } catch (IOException | ArtifactSynchronizerException e) {
        String msg = "Error deploying " + apiId + " in Gateway";
        log.error(msg, e);
        throw new ArtifactSynchronizerException(msg, e);
    } finally {
        MessageContext.destroyCurrentMessageContext();
    }
    return true;
}
Also used : GatewayAPIDTO(org.wso2.carbon.apimgt.api.gateway.GatewayAPIDTO) ArtifactSynchronizerException(org.wso2.carbon.apimgt.impl.gatewayartifactsynchronizer.exception.ArtifactSynchronizerException) IOException(java.io.IOException) APIGatewayAdmin(org.wso2.carbon.apimgt.gateway.service.APIGatewayAdmin)

Example 7 with ArtifactSynchronizerException

use of org.wso2.carbon.apimgt.impl.gatewayartifactsynchronizer.exception.ArtifactSynchronizerException in project carbon-apimgt by wso2.

the class InMemoryAPIDeployer method reDeployAPI.

public void reDeployAPI(String apiName, String version, String tenantDomain) throws ArtifactSynchronizerException {
    SubscriptionDataStore tenantSubscriptionStore = SubscriptionDataHolder.getInstance().getTenantSubscriptionStore(tenantDomain);
    Set<String> gatewayLabels = gatewayArtifactSynchronizerProperties.getGatewayLabels();
    if (tenantSubscriptionStore != null) {
        org.wso2.carbon.apimgt.keymgt.model.entity.API retrievedAPI = tenantSubscriptionStore.getApiByNameAndVersion(apiName, version);
        if (retrievedAPI != null) {
            DeployAPIInGatewayEvent deployAPIInGatewayEvent = new DeployAPIInGatewayEvent(UUID.randomUUID().toString(), System.currentTimeMillis(), APIConstants.EventType.REMOVE_API_FROM_GATEWAY.name(), tenantDomain, retrievedAPI.getApiId(), retrievedAPI.getUuid(), gatewayLabels, apiName, version, retrievedAPI.getApiProvider(), retrievedAPI.getApiType(), retrievedAPI.getContext());
            deployAPI(deployAPIInGatewayEvent);
        }
    }
}
Also used : SubscriptionDataStore(org.wso2.carbon.apimgt.keymgt.model.SubscriptionDataStore) DeployAPIInGatewayEvent(org.wso2.carbon.apimgt.impl.notifier.events.DeployAPIInGatewayEvent)

Example 8 with ArtifactSynchronizerException

use of org.wso2.carbon.apimgt.impl.gatewayartifactsynchronizer.exception.ArtifactSynchronizerException in project carbon-apimgt by wso2.

the class InMemoryAPIDeployer method unDeployAPI.

public void unDeployAPI(DeployAPIInGatewayEvent gatewayEvent) throws ArtifactSynchronizerException {
    try {
        APIGatewayAdmin apiGatewayAdmin = new APIGatewayAdmin();
        MessageContext.setCurrentMessageContext(org.wso2.carbon.apimgt.gateway.utils.GatewayUtils.createAxis2MessageContext());
        unDeployAPI(apiGatewayAdmin, gatewayEvent);
    } catch (AxisFault axisFault) {
        throw new ArtifactSynchronizerException("Error while unDeploying api ", axisFault);
    } finally {
        MessageContext.destroyCurrentMessageContext();
    }
}
Also used : AxisFault(org.apache.axis2.AxisFault) ArtifactSynchronizerException(org.wso2.carbon.apimgt.impl.gatewayartifactsynchronizer.exception.ArtifactSynchronizerException) APIGatewayAdmin(org.wso2.carbon.apimgt.gateway.service.APIGatewayAdmin)

Example 9 with ArtifactSynchronizerException

use of org.wso2.carbon.apimgt.impl.gatewayartifactsynchronizer.exception.ArtifactSynchronizerException in project carbon-apimgt by wso2.

the class InMemoryAPIDeployer method unDeployAPI.

public void unDeployAPI(String apiName, String version, String tenantDomain) throws ArtifactSynchronizerException {
    SubscriptionDataStore tenantSubscriptionStore = SubscriptionDataHolder.getInstance().getTenantSubscriptionStore(tenantDomain);
    Set<String> gatewayLabels = gatewayArtifactSynchronizerProperties.getGatewayLabels();
    if (tenantSubscriptionStore != null) {
        org.wso2.carbon.apimgt.keymgt.model.entity.API retrievedAPI = tenantSubscriptionStore.getApiByNameAndVersion(apiName, version);
        if (retrievedAPI != null) {
            DeployAPIInGatewayEvent deployAPIInGatewayEvent = new DeployAPIInGatewayEvent(UUID.randomUUID().toString(), System.currentTimeMillis(), APIConstants.EventType.REMOVE_API_FROM_GATEWAY.name(), tenantDomain, retrievedAPI.getApiId(), retrievedAPI.getUuid(), gatewayLabels, apiName, version, retrievedAPI.getApiProvider(), retrievedAPI.getApiType(), retrievedAPI.getContext());
            unDeployAPI(deployAPIInGatewayEvent);
        }
    }
}
Also used : SubscriptionDataStore(org.wso2.carbon.apimgt.keymgt.model.SubscriptionDataStore) DeployAPIInGatewayEvent(org.wso2.carbon.apimgt.impl.notifier.events.DeployAPIInGatewayEvent)

Example 10 with ArtifactSynchronizerException

use of org.wso2.carbon.apimgt.impl.gatewayartifactsynchronizer.exception.ArtifactSynchronizerException in project carbon-apimgt by wso2.

the class InMemoryAPIDeployer method deployAllAPIsAtGatewayStartup.

/**
 * Deploy an API in the gateway using the deployAPI method in gateway admin.
 *
 * @param assignedGatewayLabels - The labels which the gateway subscribed to
 * @param tenantDomain          tenantDomain of API.
 * @return True if all API artifacts retrieved from the storage and successfully deployed without any error. else
 * false
 */
public boolean deployAllAPIsAtGatewayStartup(Set<String> assignedGatewayLabels, String tenantDomain) throws ArtifactSynchronizerException {
    boolean result = false;
    if (gatewayArtifactSynchronizerProperties.isRetrieveFromStorageEnabled()) {
        if (artifactRetriever != null) {
            try {
                int errorCount = 0;
                String labelString = String.join("|", assignedGatewayLabels);
                String encodedString = Base64.encodeBase64URLSafeString(labelString.getBytes());
                APIGatewayAdmin apiGatewayAdmin = new APIGatewayAdmin();
                MessageContext.setCurrentMessageContext(org.wso2.carbon.apimgt.gateway.utils.GatewayUtils.createAxis2MessageContext());
                PrivilegedCarbonContext.startTenantFlow();
                PrivilegedCarbonContext.getThreadLocalCarbonContext().setTenantDomain(tenantDomain, true);
                List<String> gatewayRuntimeArtifacts = ServiceReferenceHolder.getInstance().getArtifactRetriever().retrieveAllArtifacts(encodedString, tenantDomain);
                if (gatewayRuntimeArtifacts.size() == 0) {
                    return true;
                }
                for (String runtimeArtifact : gatewayRuntimeArtifacts) {
                    GatewayAPIDTO gatewayAPIDTO = null;
                    try {
                        if (StringUtils.isNotEmpty(runtimeArtifact)) {
                            gatewayAPIDTO = new Gson().fromJson(runtimeArtifact, GatewayAPIDTO.class);
                            log.info("Deploying synapse artifacts of " + gatewayAPIDTO.getName());
                            apiGatewayAdmin.deployAPI(gatewayAPIDTO);
                            addDeployedCertificatesToAPIAssociation(gatewayAPIDTO);
                            addDeployedGraphqlQLToAPI(gatewayAPIDTO);
                            DataHolder.getInstance().addKeyManagerToAPIMapping(gatewayAPIDTO.getApiId(), gatewayAPIDTO.getKeyManagers());
                        }
                    } catch (AxisFault axisFault) {
                        log.error("Error in deploying " + gatewayAPIDTO.getName() + " to the Gateway ", axisFault);
                        errorCount++;
                    }
                }
                // reload dynamic profiles to avoid delays in loading certs in mutual ssl enabled APIs upon
                // server restart
                DynamicProfileReloaderHolder.getInstance().reloadAllHandlers();
                if (debugEnabled) {
                    log.debug("APIs deployed in gateway with the labels of " + labelString);
                }
                result = true;
                // Setting the result to false only if all the API deployments are failed
                if (gatewayRuntimeArtifacts.size() == errorCount) {
                    return false;
                }
            } catch (ArtifactSynchronizerException | AxisFault e) {
                String msg = "Error deploying APIs to the Gateway ";
                log.error(msg, e);
                return false;
            } finally {
                MessageContext.destroyCurrentMessageContext();
                PrivilegedCarbonContext.endTenantFlow();
            }
        } else {
            String msg = "Artifact retriever not found";
            log.error(msg);
            throw new ArtifactSynchronizerException(msg);
        }
    }
    return result;
}
Also used : GatewayAPIDTO(org.wso2.carbon.apimgt.api.gateway.GatewayAPIDTO) AxisFault(org.apache.axis2.AxisFault) ArtifactSynchronizerException(org.wso2.carbon.apimgt.impl.gatewayartifactsynchronizer.exception.ArtifactSynchronizerException) Gson(com.google.gson.Gson) APIGatewayAdmin(org.wso2.carbon.apimgt.gateway.service.APIGatewayAdmin)

Aggregations

ArtifactSynchronizerException (org.wso2.carbon.apimgt.impl.gatewayartifactsynchronizer.exception.ArtifactSynchronizerException)22 APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)12 IOException (java.io.IOException)7 Organization (org.wso2.carbon.apimgt.persistence.dto.Organization)6 APIPersistenceException (org.wso2.carbon.apimgt.persistence.exceptions.APIPersistenceException)6 Gson (com.google.gson.Gson)4 CloseableHttpResponse (org.apache.http.client.methods.CloseableHttpResponse)4 APIMgtResourceNotFoundException (org.wso2.carbon.apimgt.api.APIMgtResourceNotFoundException)4 APIIdentifier (org.wso2.carbon.apimgt.api.model.APIIdentifier)4 APIRevision (org.wso2.carbon.apimgt.api.model.APIRevision)4 APIImportExportException (org.wso2.carbon.apimgt.impl.importexport.APIImportExportException)4 File (java.io.File)3 URL (java.net.URL)3 HttpClient (org.apache.http.client.HttpClient)3 HttpGet (org.apache.http.client.methods.HttpGet)3 GatewayAPIDTO (org.wso2.carbon.apimgt.api.gateway.GatewayAPIDTO)3 APIProductIdentifier (org.wso2.carbon.apimgt.api.model.APIProductIdentifier)3 InMemoryAPIDeployer (org.wso2.carbon.apimgt.gateway.InMemoryAPIDeployer)3 APIGatewayAdmin (org.wso2.carbon.apimgt.gateway.service.APIGatewayAdmin)3 DeployAPIInGatewayEvent (org.wso2.carbon.apimgt.impl.notifier.events.DeployAPIInGatewayEvent)3