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;
}
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();
}
}
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;
}
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));
}
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));
}
Aggregations