use of org.wso2.carbon.apimgt.api.model.APIRevision in project carbon-apimgt by wso2.
the class APIProviderImpl method addDeployedAPIRevision.
/**
* Adds a new APIRevisionDeployment to an existing API
*
* @param apiId API UUID
* @param apiRevisionUUID API Revision UUID
* @param deployedAPIRevisionList List of APIRevisionDeployment objects
* @throws APIManagementException if failed to add APIRevision
*/
@Override
public void addDeployedAPIRevision(String apiId, String apiRevisionUUID, List<DeployedAPIRevision> deployedAPIRevisionList) throws APIManagementException {
List<DeployedAPIRevision> currentDeployedApiRevisionList = apiMgtDAO.getDeployedAPIRevisionByApiUUID(apiId);
Set<DeployedAPIRevision> environmentsToRemove = new HashSet<>();
// Deployments to add
List<DeployedAPIRevision> environmentsToAdd = new ArrayList<>();
List<String> envNames = new ArrayList<>();
for (DeployedAPIRevision deployedAPIRevision : deployedAPIRevisionList) {
// Remove duplicate entries for same revision uuid and env from incoming list
if (!envNames.contains(deployedAPIRevision.getDeployment())) {
envNames.add(deployedAPIRevision.getDeployment());
environmentsToAdd.add(deployedAPIRevision);
// Remove old deployed-revision entries of same env and apiid from existing db records
for (DeployedAPIRevision currentapiRevisionDeployment : currentDeployedApiRevisionList) {
if (StringUtils.equalsIgnoreCase(currentapiRevisionDeployment.getDeployment(), deployedAPIRevision.getDeployment())) {
environmentsToRemove.add(currentapiRevisionDeployment);
}
}
}
}
// Discard old deployment info
if (environmentsToRemove.size() > 0) {
apiMgtDAO.removeDeployedAPIRevision(apiId, environmentsToRemove);
}
// Add new deployed revision update to db
if (deployedAPIRevisionList.size() > 0) {
apiMgtDAO.addDeployedAPIRevision(apiRevisionUUID, environmentsToAdd);
}
}
use of org.wso2.carbon.apimgt.api.model.APIRevision in project carbon-apimgt by wso2.
the class APIProviderImpl method populateRevisionInformation.
private void populateRevisionInformation(APIProduct apiProduct, String revisionUUID) throws APIManagementException {
APIRevision apiRevision = apiMgtDAO.checkAPIUUIDIsARevisionUUID(revisionUUID);
if (apiRevision != null && !StringUtils.isEmpty(apiRevision.getApiUUID())) {
apiProduct.setRevision(true);
apiProduct.setRevisionedApiProductId(apiRevision.getApiUUID());
apiProduct.setRevisionId(apiRevision.getId());
}
}
use of org.wso2.carbon.apimgt.api.model.APIRevision in project carbon-apimgt by wso2.
the class APIProviderImpl method restoreAPIProductRevision.
@Override
public void restoreAPIProductRevision(String apiProductId, String apiRevisionId, String organization) throws APIManagementException {
APIProductIdentifier apiProductIdentifier = APIUtil.getAPIProductIdentifierFromUUID(apiProductId);
if (apiProductIdentifier == null) {
throw new APIMgtResourceNotFoundException("Couldn't retrieve existing API Product with ID: " + apiProductId, ExceptionCodes.from(ExceptionCodes.API_NOT_FOUND, apiProductId));
}
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));
}
apiProductIdentifier.setUUID(apiProductId);
try {
apiPersistenceInstance.restoreAPIRevision(new Organization(organization), apiProductIdentifier.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.restoreAPIProductRevision(apiRevision);
}
use of org.wso2.carbon.apimgt.api.model.APIRevision in project carbon-apimgt by wso2.
the class APIProviderImpl method importOperationPolicy.
/**
* This method will be used to import Operation policy. This will check existing API specific policy first and
* then common policy.
* If API specific policy exists and MD5 hash matches, it will not import and will return the existing API specific policy.
* If the existing API specific policy is different in md5, it will be updated the existing policy
* If a common policy exists and MD5 hash match, it will return the common policy's id. This policy will be imported at the API update.
* If the common policy is different then the imported policy, a new API specific policy will be created.
* If there aren't any existing policies, a new API specific policy will be created.
*
* @param importedPolicyData Imported policy
* @param organization Organization name
* @return corrosponding policy ID for imported data
* @throws APIManagementException if failed to delete APIRevision
*/
@Override
public String importOperationPolicy(OperationPolicyData importedPolicyData, String organization) throws APIManagementException {
OperationPolicySpecification importedSpec = importedPolicyData.getSpecification();
OperationPolicyData existingOperationPolicy = getAPISpecificOperationPolicyByPolicyName(importedSpec.getName(), importedPolicyData.getApiUUID(), null, organization, false);
String policyId = null;
if (existingOperationPolicy != null) {
if (existingOperationPolicy.getMd5Hash().equals(importedPolicyData.getMd5Hash())) {
if (log.isDebugEnabled()) {
log.debug("Matching API specific policy found for imported policy and MD5 hashes match.");
}
} else {
if (log.isDebugEnabled()) {
log.debug("Even though existing API specific policy name match with imported policy, " + "the MD5 hashes does not match in the policy " + existingOperationPolicy.getPolicyId() + ".Therefore updating the existing policy");
}
updateOperationPolicy(existingOperationPolicy.getPolicyId(), importedPolicyData, organization);
}
policyId = existingOperationPolicy.getPolicyId();
} else {
existingOperationPolicy = getCommonOperationPolicyByPolicyName(importedSpec.getName(), organization, false);
if (existingOperationPolicy != null) {
if (existingOperationPolicy.getMd5Hash().equals(importedPolicyData.getMd5Hash())) {
if (log.isDebugEnabled()) {
log.debug("Matching common policy found for imported policy and Md5 hashes match.");
}
policyId = existingOperationPolicy.getPolicyId();
} else {
importedSpec.setName(importedSpec.getName() + "_imported");
importedSpec.setDisplayName(importedSpec.getDisplayName() + " Imported");
importedPolicyData.setSpecification(importedSpec);
importedPolicyData.setMd5Hash(APIUtil.getMd5OfOperationPolicy(importedPolicyData));
policyId = addAPISpecificOperationPolicy(importedPolicyData.getApiUUID(), importedPolicyData, organization);
if (log.isDebugEnabled()) {
log.debug("Even though existing common policy name match with imported policy, " + "the MD5 hashes does not match in the policy " + existingOperationPolicy.getPolicyId() + ". A new policy created with ID " + policyId);
}
}
} else {
policyId = addAPISpecificOperationPolicy(importedPolicyData.getApiUUID(), importedPolicyData, organization);
if (log.isDebugEnabled()) {
log.debug("There aren't any existing policies for the imported policy. A new policy created with ID " + policyId);
}
}
}
return policyId;
}
use of org.wso2.carbon.apimgt.api.model.APIRevision in project carbon-apimgt by wso2.
the class APIProviderImpl method deployAPIProductRevision.
@Override
public void deployAPIProductRevision(String apiProductId, String apiRevisionId, List<APIRevisionDeployment> apiRevisionDeployments) throws APIManagementException {
APIProductIdentifier apiProductIdentifier = APIUtil.getAPIProductIdentifierFromUUID(apiProductId);
if (apiProductIdentifier == null) {
throw new APIMgtResourceNotFoundException("Couldn't retrieve existing API Product with ID: " + apiProductId, ExceptionCodes.from(ExceptionCodes.API_NOT_FOUND, apiProductId));
}
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));
}
APIProduct product = getAPIProductbyUUID(apiRevisionId, tenantDomain);
product.setUuid(apiProductId);
List<APIRevisionDeployment> currentApiRevisionDeploymentList = apiMgtDAO.getAPIRevisionDeploymentsByApiUUID(apiProductId);
APIGatewayManager gatewayManager = APIGatewayManager.getInstance();
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(apiProductId, environmentsToRemove);
removeFromGateway(product, tenantDomain, environmentsToRemove, environmentsToAdd);
}
GatewayArtifactsMgtDAO.getInstance().addAndRemovePublishedGatewayLabels(apiProductId, apiRevisionId, environmentsToAdd, gatewayVhosts, environmentsToRemove);
apiMgtDAO.addAPIRevisionDeployment(apiRevisionId, apiRevisionDeployments);
if (environmentsToAdd.size() > 0) {
gatewayManager.deployToGateway(product, tenantDomain, environmentsToAdd);
}
}
Aggregations