use of org.wso2.carbon.apimgt.persistence.dto.Organization in project carbon-apimgt by wso2.
the class APIProviderImpl method deployAPIRevision.
/**
* Adds a new APIRevisionDeployment to an existing API
*
* @param apiId API UUID
* @param apiRevisionId API Revision UUID
* @param apiRevisionDeployments List of APIRevisionDeployment objects
* @param organization identifier of the organization
* @throws APIManagementException if failed to add APIRevision
*/
@Override
public void deployAPIRevision(String apiId, String apiRevisionId, List<APIRevisionDeployment> apiRevisionDeployments, String organization) throws APIManagementException {
APIIdentifier apiIdentifier = APIUtil.getAPIIdentifierFromUUID(apiId);
if (apiIdentifier == null) {
throw new APIMgtResourceNotFoundException("Couldn't retrieve existing API with API UUID: " + apiId, ExceptionCodes.from(ExceptionCodes.API_NOT_FOUND, apiId));
}
APIRevision apiRevision = apiMgtDAO.getRevisionByRevisionUUID(apiRevisionId);
if (apiRevision == null) {
throw new APIMgtResourceNotFoundException("Couldn't retrieve existing API Revision with Revision UUID: " + apiRevisionId, ExceptionCodes.from(ExceptionCodes.API_REVISION_NOT_FOUND, apiRevisionId));
}
List<APIRevisionDeployment> currentApiRevisionDeploymentList = apiMgtDAO.getAPIRevisionDeploymentsByApiUUID(apiId);
APIGatewayManager gatewayManager = APIGatewayManager.getInstance();
API api = getLightweightAPIByUUID(apiId, organization);
api.setRevisionedApiId(apiRevision.getRevisionUUID());
api.setRevisionId(apiRevision.getId());
api.setUuid(apiId);
api.getId().setUuid(apiId);
api.setOrganization(organization);
Set<String> environmentsToAdd = new HashSet<>();
Map<String, String> gatewayVhosts = new HashMap<>();
Set<APIRevisionDeployment> environmentsToRemove = new HashSet<>();
for (APIRevisionDeployment apiRevisionDeployment : apiRevisionDeployments) {
for (APIRevisionDeployment currentapiRevisionDeployment : currentApiRevisionDeploymentList) {
if (StringUtils.equalsIgnoreCase(currentapiRevisionDeployment.getDeployment(), apiRevisionDeployment.getDeployment())) {
environmentsToRemove.add(currentapiRevisionDeployment);
}
}
environmentsToAdd.add(apiRevisionDeployment.getDeployment());
gatewayVhosts.put(apiRevisionDeployment.getDeployment(), apiRevisionDeployment.getVhost());
}
if (environmentsToRemove.size() > 0) {
apiMgtDAO.removeAPIRevisionDeployment(apiId, environmentsToRemove);
removeFromGateway(api, environmentsToRemove, environmentsToAdd);
}
GatewayArtifactsMgtDAO.getInstance().addAndRemovePublishedGatewayLabels(apiId, apiRevisionId, environmentsToAdd, gatewayVhosts, environmentsToRemove);
apiMgtDAO.addAPIRevisionDeployment(apiRevisionId, apiRevisionDeployments);
if (environmentsToAdd.size() > 0) {
// TODO remove this to organization once the microgateway can build gateway based on organization.
gatewayManager.deployToGateway(api, tenantDomain, environmentsToAdd);
}
String publishedDefaultVersion = getPublishedDefaultVersion(apiIdentifier);
String defaultVersion = getDefaultVersion(apiIdentifier);
apiMgtDAO.updateDefaultAPIPublishedVersion(apiIdentifier);
if (publishedDefaultVersion != null) {
if (apiIdentifier.getVersion().equals(defaultVersion)) {
api.setAsPublishedDefaultVersion(true);
}
if (api.isPublishedDefaultVersion() && !apiIdentifier.getVersion().equals(publishedDefaultVersion)) {
APIIdentifier previousDefaultVersionIdentifier = new APIIdentifier(api.getId().getProviderName(), api.getId().getApiName(), publishedDefaultVersion);
sendUpdateEventToPreviousDefaultVersion(previousDefaultVersionIdentifier, organization);
}
}
}
use of org.wso2.carbon.apimgt.persistence.dto.Organization in project carbon-apimgt by wso2.
the class APIProviderImpl method sendUpdateEventToPreviousDefaultVersion.
private void sendUpdateEventToPreviousDefaultVersion(APIIdentifier apiIdentifier, String organization) throws APIManagementException {
API api = apiMgtDAO.getLightWeightAPIInfoByAPIIdentifier(apiIdentifier, organization);
APIEvent apiEvent = new APIEvent(UUID.randomUUID().toString(), System.currentTimeMillis(), APIConstants.EventType.API_UPDATE.name(), tenantId, tenantDomain, apiIdentifier.getApiName(), api.getId().getId(), api.getUuid(), api.getId().getVersion(), api.getType(), api.getContext(), APIUtil.replaceEmailDomainBack(api.getId().getProviderName()), api.getStatus());
APIUtil.sendNotification(apiEvent, APIConstants.NotifierType.API.name());
}
use of org.wso2.carbon.apimgt.persistence.dto.Organization in project carbon-apimgt by wso2.
the class APIProviderImpl method deleteAPIRevision.
/**
* Delete an API Revision
*
* @param apiId API UUID
* @param apiRevisionId API Revision UUID
* @param organization identifier of the organization
* @throws APIManagementException if failed to delete APIRevision
*/
@Override
public void deleteAPIRevision(String apiId, String apiRevisionId, String organization) throws APIManagementException {
APIIdentifier apiIdentifier = APIUtil.getAPIIdentifierFromUUID(apiId);
if (apiIdentifier == null) {
throw new APIMgtResourceNotFoundException("Couldn't retrieve existing API with API UUID: " + apiId, ExceptionCodes.from(ExceptionCodes.API_NOT_FOUND, apiId));
}
APIRevision apiRevision = apiMgtDAO.getRevisionByRevisionUUID(apiRevisionId);
if (apiRevision == null) {
throw new APIMgtResourceNotFoundException("Couldn't retrieve existing API Revision with Revision UUID: " + apiRevisionId, ExceptionCodes.from(ExceptionCodes.API_REVISION_NOT_FOUND, apiRevisionId));
}
List<APIRevisionDeployment> apiRevisionDeploymentsResponse = getAPIRevisionDeploymentList(apiRevisionId);
if (apiRevisionDeploymentsResponse.size() != 0) {
String errorMessage = "Couldn't delete API revision since API revision is currently deployed to a gateway" + "." + "You need to undeploy the API Revision from the gateway before attempting deleting API Revision: " + apiRevision.getRevisionUUID();
throw new APIManagementException(errorMessage, ExceptionCodes.from(ExceptionCodes.EXISTING_API_REVISION_DEPLOYMENT_FOUND, apiRevisionId));
}
apiIdentifier.setUuid(apiId);
try {
apiPersistenceInstance.deleteAPIRevision(new Organization(organization), apiIdentifier.getUUID(), apiRevision.getRevisionUUID(), apiRevision.getId());
} catch (APIPersistenceException e) {
String errorMessage = "Failed to delete registry artifacts";
throw new APIManagementException(errorMessage, ExceptionCodes.from(ExceptionCodes.ERROR_DELETING_API_REVISION, apiRevision.getApiUUID()));
}
apiMgtDAO.deleteAPIRevision(apiRevision);
gatewayArtifactsMgtDAO.deleteGatewayArtifact(apiRevision.getApiUUID(), apiRevision.getRevisionUUID());
if (artifactSaver != null) {
try {
artifactSaver.removeArtifact(apiRevision.getApiUUID(), apiIdentifier.getApiName(), apiIdentifier.getVersion(), apiRevision.getRevisionUUID(), organization);
} catch (ArtifactSynchronizerException e) {
log.error("Error while deleting Runtime artifacts from artifact Store", e);
}
}
}
use of org.wso2.carbon.apimgt.persistence.dto.Organization in project carbon-apimgt by wso2.
the class APIProviderImpl method addApiSpecificMediationPolicy.
@Override
public Mediation addApiSpecificMediationPolicy(String apiId, Mediation mediationPolicy, String organization) throws APIManagementException {
if (StringUtils.isNotBlank(mediationPolicy.getName()) && mediationPolicy.getName().length() > APIConstants.MAX_LENGTH_MEDIATION_POLICY_NAME) {
throw new APIManagementException(ExceptionCodes.from(ExceptionCodes.MEDIATION_POLICY_NAME_TOO_LONG, APIConstants.MAX_LENGTH_MEDIATION_POLICY_NAME + ""));
}
try {
org.wso2.carbon.apimgt.persistence.dto.Mediation mappedPolicy = new org.wso2.carbon.apimgt.persistence.dto.Mediation();
mappedPolicy.setConfig(mediationPolicy.getConfig());
mappedPolicy.setName(mediationPolicy.getName());
mappedPolicy.setType(mediationPolicy.getType());
org.wso2.carbon.apimgt.persistence.dto.Mediation returnedMappedPolicy = apiPersistenceInstance.addMediationPolicy(new Organization(organization), apiId, mappedPolicy);
if (returnedMappedPolicy != null) {
mediationPolicy.setUuid(returnedMappedPolicy.getId());
return mediationPolicy;
}
} catch (MediationPolicyPersistenceException e) {
if (e.getErrorHandler() == ExceptionCodes.API_NOT_FOUND) {
throw new APIMgtResourceNotFoundException(e);
} else if (e.getErrorHandler() == ExceptionCodes.MEDIATION_POLICY_API_ALREADY_EXISTS) {
throw new APIManagementException(ExceptionCodes.MEDIATION_POLICY_API_ALREADY_EXISTS);
} else {
throw new APIManagementException("Error while saving mediation policy ", e);
}
}
return null;
}
use of org.wso2.carbon.apimgt.persistence.dto.Organization in project carbon-apimgt by wso2.
the class APIProviderImpl method restoreAPIRevision.
/**
* Restore a provided API Revision as the current API of the API
*
* @param apiId API UUID
* @param apiRevisionId API Revision UUID
* @throws APIManagementException if failed to restore APIRevision
*/
@Override
public void restoreAPIRevision(String apiId, String apiRevisionId, String organization) throws APIManagementException {
APIIdentifier apiIdentifier = APIUtil.getAPIIdentifierFromUUID(apiId);
if (apiIdentifier == null) {
throw new APIMgtResourceNotFoundException("Couldn't retrieve existing API with API UUID: " + apiId, ExceptionCodes.from(ExceptionCodes.API_NOT_FOUND, apiId));
}
APIRevision apiRevision = apiMgtDAO.getRevisionByRevisionUUID(apiRevisionId);
if (apiRevision == null) {
throw new APIMgtResourceNotFoundException("Couldn't retrieve existing API Revision with Revision UUID: " + apiRevisionId, ExceptionCodes.from(ExceptionCodes.API_REVISION_NOT_FOUND, apiRevisionId));
}
apiIdentifier.setUuid(apiId);
try {
apiPersistenceInstance.restoreAPIRevision(new Organization(organization), apiIdentifier.getUUID(), apiRevision.getRevisionUUID(), apiRevision.getId());
} catch (APIPersistenceException e) {
String errorMessage = "Failed to restore registry artifacts";
throw new APIManagementException(errorMessage, ExceptionCodes.from(ExceptionCodes.ERROR_RESTORING_API_REVISION, apiRevision.getApiUUID()));
}
apiMgtDAO.restoreAPIRevision(apiRevision);
}
Aggregations