Search in sources :

Example 6 with WorkflowException

use of org.wso2.carbon.apimgt.core.exception.WorkflowException in project carbon-apimgt by wso2.

the class APIStoreImpl method addApplication.

@Override
public ApplicationCreationResponse addApplication(Application application) throws APIManagementException {
    ApplicationCreationResponse applicationResponse = null;
    try {
        if (getApplicationDAO().isApplicationNameExists(application.getName())) {
            String message = "An application already exists with a duplicate name - " + application.getName();
            log.error(message);
            throw new APIMgtResourceAlreadyExistsException(message, ExceptionCodes.APPLICATION_ALREADY_EXISTS);
        }
        // Tier validation
        Policy tier = application.getPolicy();
        if (tier == null) {
            String message = "Tier name cannot be null - " + application.getName();
            log.error(message);
            throw new APIManagementException(message, ExceptionCodes.TIER_CANNOT_BE_NULL);
        } else {
            Policy policy = getPolicyDAO().getSimplifiedPolicyByLevelAndName(APIMgtAdminService.PolicyLevel.application, tier.getPolicyName());
            if (policy == null) {
                String message = "Specified tier " + tier.getPolicyName() + " is invalid";
                log.error(message);
                throw new APIManagementException(message, ExceptionCodes.TIER_CANNOT_BE_NULL);
            }
            application.setPolicy(policy);
        }
        // Generate UUID for application
        String generatedUuid = UUID.randomUUID().toString();
        application.setId(generatedUuid);
        String permissionString = application.getPermissionString();
        if (permissionString != null && !("").equals(permissionString)) {
            HashMap roleNamePermissionList;
            roleNamePermissionList = getAPIPermissionArray(permissionString);
            application.setPermissionMap(roleNamePermissionList);
        }
        application.setCreatedTime(LocalDateTime.now());
        getApplicationDAO().addApplication(application);
        WorkflowExecutor appCreationWFExecutor = WorkflowExecutorFactory.getInstance().getWorkflowExecutor(WorkflowConstants.WF_TYPE_AM_APPLICATION_CREATION);
        ApplicationCreationWorkflow workflow = new ApplicationCreationWorkflow(getApplicationDAO(), getWorkflowDAO(), getApiGateway());
        workflow.setApplication(application);
        workflow.setCreatedBy(getUsername());
        workflow.setWorkflowReference(application.getId());
        workflow.setExternalWorkflowReference(UUID.randomUUID().toString());
        workflow.setCreatedTime(LocalDateTime.now());
        String workflowDescription = "Application [ " + application.getName() + " ] creation request from application creator - " + getUsername() + " with throttling tier - " + tier.getPolicyName() + "";
        workflow.setWorkflowDescription(workflowDescription);
        WorkflowResponse response = appCreationWFExecutor.execute(workflow);
        workflow.setStatus(response.getWorkflowStatus());
        if (WorkflowStatus.CREATED != response.getWorkflowStatus()) {
            completeWorkflow(appCreationWFExecutor, workflow);
        } else {
            getApplicationDAO().updateApplicationState(generatedUuid, APIMgtConstants.ApplicationStatus.APPLICATION_ONHOLD);
            addWorkflowEntries(workflow);
        }
        APIUtils.logDebug("successfully added application with appId " + application.getId(), log);
        applicationResponse = new ApplicationCreationResponse(application.getId(), response);
    } catch (APIMgtDAOException e) {
        String errorMsg = "Error occurred while creating the application - " + application.getName();
        log.error(errorMsg, e);
        throw new APIManagementException(errorMsg, e, e.getErrorHandler());
    } catch (ParseException e) {
        String errorMsg = "Error occurred while parsing the permission json from swagger in application - " + application.getName();
        log.error(errorMsg, e);
        throw new APIManagementException(errorMsg, e, ExceptionCodes.SWAGGER_PARSE_EXCEPTION);
    } catch (WorkflowException e) {
        String errorMsg = "Error occurred in workflow";
        log.error(errorMsg, e);
        throw new APIManagementException(errorMsg, e, ExceptionCodes.WORKFLOW_EXCEPTION);
    }
    return applicationResponse;
}
Also used : Policy(org.wso2.carbon.apimgt.core.models.policy.Policy) APIMgtDAOException(org.wso2.carbon.apimgt.core.exception.APIMgtDAOException) ApplicationCreationResponse(org.wso2.carbon.apimgt.core.workflow.ApplicationCreationResponse) APIManagementException(org.wso2.carbon.apimgt.core.exception.APIManagementException) HashMap(java.util.HashMap) WorkflowException(org.wso2.carbon.apimgt.core.exception.WorkflowException) APIMgtResourceAlreadyExistsException(org.wso2.carbon.apimgt.core.exception.APIMgtResourceAlreadyExistsException) ApplicationCreationWorkflow(org.wso2.carbon.apimgt.core.workflow.ApplicationCreationWorkflow) WorkflowResponse(org.wso2.carbon.apimgt.core.api.WorkflowResponse) WorkflowExecutor(org.wso2.carbon.apimgt.core.api.WorkflowExecutor) ParseException(org.json.simple.parser.ParseException)

Example 7 with WorkflowException

use of org.wso2.carbon.apimgt.core.exception.WorkflowException in project carbon-apimgt by wso2.

the class DefaultWorkflowExecutorTestCase method testWorkflowResponses.

@Test(description = "Test workflow responses")
public void testWorkflowResponses() throws WorkflowException {
    WorkflowExecutor executor = new DefaultWorkflowExecutor();
    APISubscriptionDAO apiSubscriptionDAO = Mockito.mock(APISubscriptionDAO.class);
    WorkflowDAO workflowDAO = Mockito.mock(WorkflowDAO.class);
    APIGateway apiGateway = Mockito.mock(APIGateway.class);
    Workflow workflow = new SubscriptionCreationWorkflow(apiSubscriptionDAO, workflowDAO, apiGateway);
    WorkflowResponse response = executor.execute(workflow);
    Assert.assertEquals(response.getJSONPayload(), "");
    Assert.assertEquals(response.getWorkflowStatus(), WorkflowStatus.APPROVED);
    workflow.setStatus(WorkflowStatus.APPROVED);
    response = executor.complete(workflow);
    Assert.assertEquals(response.getWorkflowStatus(), WorkflowStatus.APPROVED);
    executor.cleanUpPendingTask(workflow.getExternalWorkflowReference());
}
Also used : WorkflowDAO(org.wso2.carbon.apimgt.core.dao.WorkflowDAO) APISubscriptionDAO(org.wso2.carbon.apimgt.core.dao.APISubscriptionDAO) WorkflowResponse(org.wso2.carbon.apimgt.core.api.WorkflowResponse) WorkflowExecutor(org.wso2.carbon.apimgt.core.api.WorkflowExecutor) APIGateway(org.wso2.carbon.apimgt.core.api.APIGateway) Test(org.testng.annotations.Test)

Example 8 with WorkflowException

use of org.wso2.carbon.apimgt.core.exception.WorkflowException in project carbon-apimgt by wso2.

the class DefaultWorkflowExecutor method complete.

/**
 * Complete the external process status
 * Based on the workflow status we will update the status column of the
 * Application table
 *
 * @param workFlow - Workflow
 */
public WorkflowResponse complete(Workflow workFlow) throws WorkflowException {
    if (log.isDebugEnabled()) {
        log.debug("Executing complete() in Workflow for " + workFlow.getWorkflowType());
    }
    WorkflowResponse workflowResponse = new GeneralWorkflowResponse();
    workflowResponse.setWorkflowStatus(workFlow.getStatus());
    return workflowResponse;
}
Also used : WorkflowResponse(org.wso2.carbon.apimgt.core.api.WorkflowResponse)

Example 9 with WorkflowException

use of org.wso2.carbon.apimgt.core.exception.WorkflowException in project carbon-apimgt by wso2.

the class DefaultWorkflowExecutor method execute.

/**
 * {@inheritDoc}
 */
@Override
public WorkflowResponse execute(Workflow workFlow) throws WorkflowException {
    if (log.isDebugEnabled()) {
        log.debug("Executing execute() in Workflow for " + workFlow.getWorkflowType());
    }
    WorkflowResponse workflowResponse = new GeneralWorkflowResponse();
    // set the state to approved
    workflowResponse.setWorkflowStatus(WorkflowStatus.APPROVED);
    return workflowResponse;
}
Also used : WorkflowResponse(org.wso2.carbon.apimgt.core.api.WorkflowResponse)

Example 10 with WorkflowException

use of org.wso2.carbon.apimgt.core.exception.WorkflowException 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)

Aggregations

WorkflowResponse (org.wso2.carbon.apimgt.core.api.WorkflowResponse)7 WorkflowExecutor (org.wso2.carbon.apimgt.core.api.WorkflowExecutor)5 WorkflowException (org.wso2.carbon.apimgt.core.exception.WorkflowException)3 Test (org.testng.annotations.Test)2 APIGateway (org.wso2.carbon.apimgt.core.api.APIGateway)2 APISubscriptionDAO (org.wso2.carbon.apimgt.core.dao.APISubscriptionDAO)2 WorkflowDAO (org.wso2.carbon.apimgt.core.dao.WorkflowDAO)2 WorkflowConfigProperties (org.wso2.carbon.apimgt.core.models.WorkflowConfigProperties)2 HashMap (java.util.HashMap)1 Iterator (java.util.Iterator)1 ParseException (org.json.simple.parser.ParseException)1 APIManagementException (org.wso2.carbon.apimgt.core.exception.APIManagementException)1 APIMgtDAOException (org.wso2.carbon.apimgt.core.exception.APIMgtDAOException)1 APIMgtResourceAlreadyExistsException (org.wso2.carbon.apimgt.core.exception.APIMgtResourceAlreadyExistsException)1 WorkflowConfig (org.wso2.carbon.apimgt.core.models.WorkflowConfig)1 Policy (org.wso2.carbon.apimgt.core.models.policy.Policy)1 ApplicationCreationResponse (org.wso2.carbon.apimgt.core.workflow.ApplicationCreationResponse)1 ApplicationCreationWorkflow (org.wso2.carbon.apimgt.core.workflow.ApplicationCreationWorkflow)1