Search in sources :

Example 1 with APIGatewayAdmin

use of org.wso2.carbon.apimgt.gateway.service.APIGatewayAdmin 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 2 with APIGatewayAdmin

use of org.wso2.carbon.apimgt.gateway.service.APIGatewayAdmin 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 3 with APIGatewayAdmin

use of org.wso2.carbon.apimgt.gateway.service.APIGatewayAdmin 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)

Example 4 with APIGatewayAdmin

use of org.wso2.carbon.apimgt.gateway.service.APIGatewayAdmin in project carbon-apimgt by wso2.

the class APIGatewayAdminTest method addPrototypeApiScriptImplForTenant.

@Test
public void addPrototypeApiScriptImplForTenant() throws Exception {
    RESTAPIAdminServiceProxy restapiAdminServiceProxy = Mockito.mock(RESTAPIAdminServiceProxy.class);
    restapiAdminServiceProxy.setTenantDomain(tenantDomain);
    Mockito.when(restapiAdminServiceProxy.addApi(config)).thenReturn(true);
    APIGatewayAdmin apiGatewayAdmin = new APIGatewayAdminWrapper(restapiAdminServiceProxy, null, null);
    Assert.assertTrue(apiGatewayAdmin.addPrototypeApiScriptImplForTenant(provider, name, version, config, tenantDomain));
}
Also used : RESTAPIAdminServiceProxy(org.wso2.carbon.apimgt.gateway.utils.RESTAPIAdminServiceProxy) Test(org.junit.Test)

Example 5 with APIGatewayAdmin

use of org.wso2.carbon.apimgt.gateway.service.APIGatewayAdmin in project carbon-apimgt by wso2.

the class APIGatewayAdminTest method addApiForTenant.

@Test
public void addApiForTenant() throws Exception {
    RESTAPIAdminServiceProxy restapiAdminServiceProxy = Mockito.mock(RESTAPIAdminServiceProxy.class);
    restapiAdminServiceProxy.setTenantDomain(tenantDomain);
    Mockito.when(restapiAdminServiceProxy.addApi(config)).thenReturn(true);
    APIGatewayAdmin apiGatewayAdmin = new APIGatewayAdminWrapper(restapiAdminServiceProxy, null, null);
    Assert.assertTrue(apiGatewayAdmin.addApiForTenant(provider, name, version, config, tenantDomain));
}
Also used : RESTAPIAdminServiceProxy(org.wso2.carbon.apimgt.gateway.utils.RESTAPIAdminServiceProxy) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)32 RESTAPIAdminServiceProxy (org.wso2.carbon.apimgt.gateway.utils.RESTAPIAdminServiceProxy)28 EndpointAdminServiceProxy (org.wso2.carbon.apimgt.gateway.utils.EndpointAdminServiceProxy)12 SequenceAdminServiceProxy (org.wso2.carbon.apimgt.gateway.utils.SequenceAdminServiceProxy)8 APIData (org.wso2.carbon.rest.api.APIData)4 OMElement (org.apache.axiom.om.OMElement)3 GatewayAPIDTO (org.wso2.carbon.apimgt.api.gateway.GatewayAPIDTO)3 APIGatewayAdmin (org.wso2.carbon.apimgt.gateway.service.APIGatewayAdmin)3 ArtifactSynchronizerException (org.wso2.carbon.apimgt.impl.gatewayartifactsynchronizer.exception.ArtifactSynchronizerException)3 OMFactory (org.apache.axiom.om.OMFactory)2 AxisFault (org.apache.axis2.AxisFault)2 Gson (com.google.gson.Gson)1 IOException (java.io.IOException)1 API (org.wso2.carbon.apimgt.api.model.API)1 APIIdentifier (org.wso2.carbon.apimgt.api.model.APIIdentifier)1 APIProductIdentifier (org.wso2.carbon.apimgt.api.model.APIProductIdentifier)1 APIEvent (org.wso2.carbon.apimgt.impl.notifier.events.APIEvent)1 ResourceData (org.wso2.carbon.rest.api.ResourceData)1