Search in sources :

Example 51 with APIRevision

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);
    }
}
Also used : DeployedAPIRevision(org.wso2.carbon.apimgt.api.model.DeployedAPIRevision) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet)

Example 52 with APIRevision

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());
    }
}
Also used : DeployedAPIRevision(org.wso2.carbon.apimgt.api.model.DeployedAPIRevision) APIRevision(org.wso2.carbon.apimgt.api.model.APIRevision)

Example 53 with APIRevision

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);
}
Also used : APIProductIdentifier(org.wso2.carbon.apimgt.api.model.APIProductIdentifier) DeployedAPIRevision(org.wso2.carbon.apimgt.api.model.DeployedAPIRevision) APIRevision(org.wso2.carbon.apimgt.api.model.APIRevision) APIPersistenceException(org.wso2.carbon.apimgt.persistence.exceptions.APIPersistenceException) Organization(org.wso2.carbon.apimgt.persistence.dto.Organization) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) APIMgtResourceNotFoundException(org.wso2.carbon.apimgt.api.APIMgtResourceNotFoundException)

Example 54 with 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;
}
Also used : OperationPolicyData(org.wso2.carbon.apimgt.api.model.OperationPolicyData) OperationPolicySpecification(org.wso2.carbon.apimgt.api.model.OperationPolicySpecification)

Example 55 with APIRevision

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);
    }
}
Also used : DeployedAPIRevision(org.wso2.carbon.apimgt.api.model.DeployedAPIRevision) APIRevision(org.wso2.carbon.apimgt.api.model.APIRevision) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) APIRevisionDeployment(org.wso2.carbon.apimgt.api.model.APIRevisionDeployment) APIMgtResourceNotFoundException(org.wso2.carbon.apimgt.api.APIMgtResourceNotFoundException) APIProductIdentifier(org.wso2.carbon.apimgt.api.model.APIProductIdentifier) PublisherAPIProduct(org.wso2.carbon.apimgt.persistence.dto.PublisherAPIProduct) APIProduct(org.wso2.carbon.apimgt.api.model.APIProduct) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet)

Aggregations

APIRevision (org.wso2.carbon.apimgt.api.model.APIRevision)48 APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)28 DeployedAPIRevision (org.wso2.carbon.apimgt.api.model.DeployedAPIRevision)23 APIIdentifier (org.wso2.carbon.apimgt.api.model.APIIdentifier)18 ArrayList (java.util.ArrayList)16 Connection (java.sql.Connection)15 PreparedStatement (java.sql.PreparedStatement)15 ResultSet (java.sql.ResultSet)14 SQLException (java.sql.SQLException)13 APIMgtResourceNotFoundException (org.wso2.carbon.apimgt.api.APIMgtResourceNotFoundException)13 APIProvider (org.wso2.carbon.apimgt.api.APIProvider)13 APIProductIdentifier (org.wso2.carbon.apimgt.api.model.APIProductIdentifier)13 API (org.wso2.carbon.apimgt.api.model.API)12 APIRevisionDeployment (org.wso2.carbon.apimgt.api.model.APIRevisionDeployment)12 HashSet (java.util.HashSet)11 LinkedHashSet (java.util.LinkedHashSet)11 SubscribedAPI (org.wso2.carbon.apimgt.api.model.SubscribedAPI)11 Organization (org.wso2.carbon.apimgt.persistence.dto.Organization)11 APIPersistenceException (org.wso2.carbon.apimgt.persistence.exceptions.APIPersistenceException)11 ImportExportAPI (org.wso2.carbon.apimgt.impl.importexport.ImportExportAPI)10