Search in sources :

Example 96 with Organization

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);
        }
    }
}
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) APIIdentifier(org.wso2.carbon.apimgt.api.model.APIIdentifier) API(org.wso2.carbon.apimgt.api.model.API) ImportExportAPI(org.wso2.carbon.apimgt.impl.importexport.ImportExportAPI) SubscribedAPI(org.wso2.carbon.apimgt.api.model.SubscribedAPI) PublisherAPI(org.wso2.carbon.apimgt.persistence.dto.PublisherAPI) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet)

Example 97 with 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());
}
Also used : APIEvent(org.wso2.carbon.apimgt.impl.notifier.events.APIEvent) API(org.wso2.carbon.apimgt.api.model.API) ImportExportAPI(org.wso2.carbon.apimgt.impl.importexport.ImportExportAPI) SubscribedAPI(org.wso2.carbon.apimgt.api.model.SubscribedAPI) PublisherAPI(org.wso2.carbon.apimgt.persistence.dto.PublisherAPI)

Example 98 with Organization

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);
        }
    }
}
Also used : 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) ArtifactSynchronizerException(org.wso2.carbon.apimgt.impl.gatewayartifactsynchronizer.exception.ArtifactSynchronizerException) APIIdentifier(org.wso2.carbon.apimgt.api.model.APIIdentifier) APIRevisionDeployment(org.wso2.carbon.apimgt.api.model.APIRevisionDeployment) APIMgtResourceNotFoundException(org.wso2.carbon.apimgt.api.APIMgtResourceNotFoundException)

Example 99 with Organization

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

Example 100 with Organization

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

Aggregations

APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)304 APIProvider (org.wso2.carbon.apimgt.api.APIProvider)106 API (org.wso2.carbon.apimgt.api.model.API)100 SubscribedAPI (org.wso2.carbon.apimgt.api.model.SubscribedAPI)89 ArrayList (java.util.ArrayList)79 APIPersistenceException (org.wso2.carbon.apimgt.persistence.exceptions.APIPersistenceException)72 UserRegistry (org.wso2.carbon.registry.core.session.UserRegistry)70 APIIdentifier (org.wso2.carbon.apimgt.api.model.APIIdentifier)65 Organization (org.wso2.carbon.apimgt.persistence.dto.Organization)64 IOException (java.io.IOException)61 Registry (org.wso2.carbon.registry.core.Registry)58 ImportExportAPI (org.wso2.carbon.apimgt.impl.importexport.ImportExportAPI)57 APIConsumer (org.wso2.carbon.apimgt.api.APIConsumer)56 HashMap (java.util.HashMap)54 RegistryException (org.wso2.carbon.registry.core.exceptions.RegistryException)53 Resource (org.wso2.carbon.registry.core.Resource)51 APIMgtResourceNotFoundException (org.wso2.carbon.apimgt.api.APIMgtResourceNotFoundException)49 JSONObject (org.json.simple.JSONObject)45 GenericArtifact (org.wso2.carbon.governance.api.generic.dataobjects.GenericArtifact)44 URISyntaxException (java.net.URISyntaxException)42