Search in sources :

Example 16 with WorkflowExecutor

use of org.wso2.carbon.apimgt.core.api.WorkflowExecutor in project carbon-apimgt by wso2.

the class SubscriptionDeletionWorkflow method completeWorkflow.

public WorkflowResponse completeWorkflow(WorkflowExecutor workflowExecutor) throws APIManagementException {
    if (subscription == null) {
        // this is when complete method is executed through workflow rest api
        this.subscription = apiSubscriptionDAO.getAPISubscription(getWorkflowReference());
    }
    WorkflowResponse response = workflowExecutor.complete(this);
    setStatus(response.getWorkflowStatus());
    List<SubscriptionValidationData> subscriptionValidationDataList = null;
    if (WorkflowStatus.APPROVED == response.getWorkflowStatus()) {
        if (log.isDebugEnabled()) {
            log.debug("Subscription deletion workflow complete: Approved");
        }
        if (subscription.getApi() != null && subscription.getApplication() != null) {
            subscriptionValidationDataList = apiSubscriptionDAO.getAPISubscriptionsOfAPIForValidation(subscription.getApi().getContext(), subscription.getApi().getVersion(), subscription.getApplication().getId());
        }
        apiSubscriptionDAO.deleteAPISubscription(getWorkflowReference());
    } else if (WorkflowStatus.REJECTED == response.getWorkflowStatus()) {
        if (log.isDebugEnabled()) {
            log.debug("Subscription deletion workflow complete: Rejected");
        }
    }
    updateWorkflowEntries(this);
    if (WorkflowStatus.APPROVED == response.getWorkflowStatus()) {
        if (subscriptionValidationDataList != null && !subscriptionValidationDataList.isEmpty()) {
            apiGateway.deleteAPISubscription(subscriptionValidationDataList);
            if (log.isDebugEnabled()) {
                log.debug("Subscription for API : " + subscription.getApi().getName() + " with " + "application : " + subscription.getApplication().getName() + " has been successfully " + "removed from gateway");
            }
        }
    }
    return response;
}
Also used : WorkflowResponse(org.wso2.carbon.apimgt.core.api.WorkflowResponse) SubscriptionValidationData(org.wso2.carbon.apimgt.core.models.SubscriptionValidationData)

Example 17 with WorkflowExecutor

use of org.wso2.carbon.apimgt.core.api.WorkflowExecutor in project carbon-apimgt by wso2.

the class WorkflowConfigHolder method loadWorkflowConfigurations.

private void loadWorkflowConfigurations(WorkflowExecutorInfo workflowConfig, String workflowExecutorType) throws ClassNotFoundException, IllegalAccessException, InstantiationException, WorkflowException {
    String executorClass = workflowConfig.getExecutor();
    Class clazz = WorkflowConfigHolder.class.getClassLoader().loadClass(executorClass);
    WorkflowExecutor workFlowExecutor = (WorkflowExecutor) clazz.newInstance();
    List<WorkflowConfigProperties> properties = workflowConfig.getProperty();
    if (properties != null) {
        loadProperties(properties, workFlowExecutor);
    }
    workflowExecutorMap.put(workflowExecutorType, workFlowExecutor);
}
Also used : WorkflowExecutor(org.wso2.carbon.apimgt.core.api.WorkflowExecutor) WorkflowConfigProperties(org.wso2.carbon.apimgt.core.models.WorkflowConfigProperties)

Example 18 with WorkflowExecutor

use of org.wso2.carbon.apimgt.core.api.WorkflowExecutor in project carbon-apimgt by wso2.

the class WorkflowsApiServiceImpl method workflowsWorkflowReferenceIdPut.

@Override
public Response workflowsWorkflowReferenceIdPut(String workflowReferenceId, WorkflowRequestDTO body, Request request) throws NotFoundException {
    try {
        APIMgtAdminService apiMgtAdminService = RestApiUtil.getAPIMgtAdminService();
        Workflow workflow = apiMgtAdminService.retrieveWorkflow(workflowReferenceId);
        if (workflow == null) {
            String errorMessage = "Workflow entry not found for: " + workflowReferenceId;
            APIMgtResourceNotFoundException e = new APIMgtResourceNotFoundException(errorMessage, ExceptionCodes.WORKFLOW_NOT_FOUND);
            Map<String, String> paramList = new HashMap<>();
            paramList.put(APIMgtConstants.ExceptionsConstants.WORKFLOW_REF_ID, workflowReferenceId);
            ErrorDTO errorDTO = RestApiUtil.getErrorDTO(e.getErrorHandler(), paramList);
            log.error(errorMessage, e);
            return Response.status(e.getErrorHandler().getHttpStatusCode()).entity(errorDTO).build();
        } else {
            if (WorkflowStatus.APPROVED == workflow.getStatus()) {
                String errorMessage = "Workflow is already in complete state";
                APIMgtResourceNotFoundException e = new APIMgtResourceNotFoundException(errorMessage, ExceptionCodes.WORKFLOW_ALREADY_COMPLETED);
                Map<String, String> paramList = new HashMap<>();
                paramList.put(APIMgtConstants.ExceptionsConstants.WORKFLOW_REF_ID, workflowReferenceId);
                ErrorDTO errorDTO = RestApiUtil.getErrorDTO(e.getErrorHandler(), paramList);
                log.error(errorMessage, e);
                return Response.status(e.getErrorHandler().getHttpStatusCode()).entity(errorDTO).build();
            } else {
                WorkflowExecutor workflowExecutor = WorkflowExecutorFactory.getInstance().getWorkflowExecutor(workflow.getWorkflowType());
                if (body == null) {
                    RestApiUtil.handleBadRequest("Request payload is missing", log);
                }
                if (body.getDescription() != null) {
                    workflow.setWorkflowDescription(body.getDescription());
                }
                if (body.getStatus() == null) {
                    String errorMessage = "Workflow status is not defined";
                    APIManagementException e = new APIManagementException(errorMessage, ExceptionCodes.WORKFLOW_STATE_MISSING);
                    Map<String, String> paramList = new HashMap<>();
                    paramList.put(APIMgtConstants.ExceptionsConstants.WORKFLOW_REF_ID, workflowReferenceId);
                    ErrorDTO errorDTO = RestApiUtil.getErrorDTO(e.getErrorHandler(), paramList);
                    log.error(errorMessage, e);
                    return Response.status(e.getErrorHandler().getHttpStatusCode()).entity(errorDTO).build();
                } else {
                    workflow.setStatus(WorkflowStatus.valueOf(body.getStatus().toString()));
                }
                if (body.getAttributes() != null) {
                    Map<String, String> existingAttributs = workflow.getAttributes();
                    Map<String, String> newAttributes = body.getAttributes();
                    if (existingAttributs == null) {
                        workflow.setAttributes(newAttributes);
                    } else {
                        newAttributes.forEach(existingAttributs::putIfAbsent);
                        workflow.setAttributes(existingAttributs);
                    }
                }
                WorkflowResponse response = apiMgtAdminService.completeWorkflow(workflowExecutor, workflow);
                WorkflowResponseDTO workflowResponseDTO = WorkflowMappingUtil.toWorkflowResponseDTO(response);
                return Response.ok().entity(workflowResponseDTO).build();
            }
        }
    } catch (APIManagementException e) {
        String errorMessage = "Error while completing workflow for reference : " + workflowReferenceId + ". " + e.getMessage();
        Map<String, String> paramList = new HashMap<>();
        paramList.put(APIMgtConstants.ExceptionsConstants.WORKFLOW_REF_ID, workflowReferenceId);
        ErrorDTO errorDTO = RestApiUtil.getErrorDTO(e.getErrorHandler(), paramList);
        log.error(errorMessage, e);
        return Response.status(e.getErrorHandler().getHttpStatusCode()).entity(errorDTO).build();
    }
}
Also used : HashMap(java.util.HashMap) ErrorDTO(org.wso2.carbon.apimgt.rest.api.common.dto.ErrorDTO) Workflow(org.wso2.carbon.apimgt.core.workflow.Workflow) APIMgtResourceNotFoundException(org.wso2.carbon.apimgt.core.exception.APIMgtResourceNotFoundException) APIMgtAdminService(org.wso2.carbon.apimgt.core.api.APIMgtAdminService) APIManagementException(org.wso2.carbon.apimgt.core.exception.APIManagementException) WorkflowResponse(org.wso2.carbon.apimgt.core.api.WorkflowResponse) WorkflowExecutor(org.wso2.carbon.apimgt.core.api.WorkflowExecutor) HashMap(java.util.HashMap) Map(java.util.Map)

Example 19 with WorkflowExecutor

use of org.wso2.carbon.apimgt.core.api.WorkflowExecutor in project carbon-apimgt by wso2.

the class APIPublisherImpl method cleanupPendingTaskForAPIStateChange.

private void cleanupPendingTaskForAPIStateChange(String apiId) throws APIManagementException {
    Optional<String> workflowExtRef = getWorkflowDAO().getExternalWorkflowReferenceForPendingTask(apiId, WorkflowConstants.WF_TYPE_AM_API_STATE);
    if (workflowExtRef.isPresent()) {
        WorkflowExecutor executor = WorkflowExecutorFactory.getInstance().getWorkflowExecutor(WorkflowConstants.WF_TYPE_AM_API_STATE);
        try {
            executor.cleanUpPendingTask(workflowExtRef.get());
        } catch (WorkflowException e) {
            String warn = "Failed to clean pending api state change task for " + apiId;
            // failed cleanup processes are ignored to prevent failing the deletion process
            log.warn(warn, e.getLocalizedMessage());
        }
        getWorkflowDAO().deleteWorkflowEntryforExternalReference(workflowExtRef.get());
    }
}
Also used : WorkflowException(org.wso2.carbon.apimgt.core.exception.WorkflowException) WorkflowExecutor(org.wso2.carbon.apimgt.core.api.WorkflowExecutor)

Example 20 with WorkflowExecutor

use of org.wso2.carbon.apimgt.core.api.WorkflowExecutor in project carbon-apimgt by wso2.

the class APIStoreImpl method deleteAPISubscription.

/**
 * @see APIStore#deleteAPISubscription(String)
 */
@Override
public WorkflowResponse deleteAPISubscription(String subscriptionId) throws APIManagementException {
    try {
        WorkflowExecutor removeSubscriptionWFExecutor = WorkflowExecutorFactory.getInstance().getWorkflowExecutor(WorkflowConstants.WF_TYPE_AM_SUBSCRIPTION_DELETION);
        // check for pending subscription creation
        if (subscriptionId == null) {
            String errorMsg = "Subscription Id is not provided";
            log.error(errorMsg);
            throw new APIManagementException(errorMsg, ExceptionCodes.PARAMETER_NOT_PROVIDED);
        }
        Subscription subscription = getApiSubscriptionDAO().getAPISubscription(subscriptionId);
        if (subscription == null) {
            String errorMsg = "Subscription not found for the id - " + subscriptionId;
            log.error(errorMsg);
            throw new APIManagementException(errorMsg, ExceptionCodes.SUBSCRIPTION_NOT_FOUND);
        } else {
            // remove pending tasks for subscription creation first
            cleanupPendingTaskForSubscriptionDeletion(subscription);
            SubscriptionDeletionWorkflow workflow = new SubscriptionDeletionWorkflow(getApiSubscriptionDAO(), getWorkflowDAO(), getApiGateway());
            workflow.setWorkflowReference(subscriptionId);
            workflow.setSubscription(subscription);
            workflow.setWorkflowType(WorkflowConstants.WF_TYPE_AM_SUBSCRIPTION_DELETION);
            workflow.setStatus(WorkflowStatus.CREATED);
            workflow.setCreatedTime(LocalDateTime.now());
            workflow.setExternalWorkflowReference(UUID.randomUUID().toString());
            workflow.setSubscriber(getUsername());
            String workflowDescription = "API [ " + subscription.getApi().getName() + " - " + subscription.getApi().getVersion() + " ] subscription deletion request from subscriber - " + getUsername() + "  for the application - " + subscription.getApplication().getName() + "";
            workflow.setWorkflowDescription(workflowDescription);
            WorkflowResponse response = removeSubscriptionWFExecutor.execute(workflow);
            workflow.setStatus(response.getWorkflowStatus());
            if (WorkflowStatus.CREATED != response.getWorkflowStatus()) {
                completeWorkflow(removeSubscriptionWFExecutor, workflow);
            } else {
                // add entry to workflow table if it is only in pending state
                // haven't changed the subscription's state to allow to use it till approval
                addWorkflowEntries(workflow);
            }
            return response;
        }
    } catch (APIMgtDAOException e) {
        String errorMsg = "Error occurred while deleting api subscription - " + subscriptionId;
        log.error(errorMsg, e);
        throw new APIManagementException(errorMsg, e, e.getErrorHandler());
    }
}
Also used : APIMgtDAOException(org.wso2.carbon.apimgt.core.exception.APIMgtDAOException) APIManagementException(org.wso2.carbon.apimgt.core.exception.APIManagementException) WorkflowResponse(org.wso2.carbon.apimgt.core.api.WorkflowResponse) WorkflowExecutor(org.wso2.carbon.apimgt.core.api.WorkflowExecutor) Subscription(org.wso2.carbon.apimgt.core.models.Subscription) SubscriptionDeletionWorkflow(org.wso2.carbon.apimgt.core.workflow.SubscriptionDeletionWorkflow)

Aggregations

WorkflowResponse (org.wso2.carbon.apimgt.core.api.WorkflowResponse)15 WorkflowExecutor (org.wso2.carbon.apimgt.core.api.WorkflowExecutor)13 APIManagementException (org.wso2.carbon.apimgt.core.exception.APIManagementException)8 APIMgtDAOException (org.wso2.carbon.apimgt.core.exception.APIMgtDAOException)6 Application (org.wso2.carbon.apimgt.core.models.Application)4 HashMap (java.util.HashMap)3 Test (org.testng.annotations.Test)3 APIGateway (org.wso2.carbon.apimgt.core.api.APIGateway)3 WorkflowDAO (org.wso2.carbon.apimgt.core.dao.WorkflowDAO)3 API (org.wso2.carbon.apimgt.core.models.API)3 Subscription (org.wso2.carbon.apimgt.core.models.Subscription)3 Policy (org.wso2.carbon.apimgt.core.models.policy.Policy)3 Iterator (java.util.Iterator)2 Map (java.util.Map)2 APISubscriptionDAO (org.wso2.carbon.apimgt.core.dao.APISubscriptionDAO)2 APIMgtResourceNotFoundException (org.wso2.carbon.apimgt.core.exception.APIMgtResourceNotFoundException)2 GatewayException (org.wso2.carbon.apimgt.core.exception.GatewayException)2 WorkflowException (org.wso2.carbon.apimgt.core.exception.WorkflowException)2 SubscriptionValidationData (org.wso2.carbon.apimgt.core.models.SubscriptionValidationData)2 WorkflowConfigProperties (org.wso2.carbon.apimgt.core.models.WorkflowConfigProperties)2