use of org.wso2.carbon.apimgt.core.workflow.Workflow in project carbon-apimgt by wso2.
the class APIStoreImpl method addApiSubscription.
@Override
public SubscriptionResponse addApiSubscription(String apiId, String applicationId, String tier) throws APIManagementException {
SubscriptionResponse subScriptionResponse;
// Generate UUID for application
String subscriptionId = UUID.randomUUID().toString();
try {
API api = getAPIbyUUID(apiId);
Application application = getApplicationByUuid(applicationId);
if (application == null) {
String errorMsg = "Cannot find an application for given applicationId - " + applicationId;
log.error(errorMsg);
throw new APIManagementException(errorMsg, ExceptionCodes.APPLICATION_NOT_FOUND);
}
Policy policy = getPolicyDAO().getSimplifiedPolicyByLevelAndName(APIMgtAdminService.PolicyLevel.subscription, tier);
if (policy == null) {
String errorMsg = "Cannot find an subscription policy for given policy name - " + tier;
log.error(errorMsg);
throw new APIManagementException(errorMsg, ExceptionCodes.POLICY_NOT_FOUND);
}
getApiSubscriptionDAO().addAPISubscription(subscriptionId, apiId, applicationId, policy.getUuid(), APIMgtConstants.SubscriptionStatus.ON_HOLD);
WorkflowExecutor addSubscriptionWFExecutor = WorkflowExecutorFactory.getInstance().getWorkflowExecutor(WorkflowConstants.WF_TYPE_AM_SUBSCRIPTION_CREATION);
// Instead of quering the db, we create same subscription object
Subscription subscription = new Subscription(subscriptionId, application, api, policy);
subscription.setStatus(APIMgtConstants.SubscriptionStatus.ON_HOLD);
SubscriptionCreationWorkflow workflow = new SubscriptionCreationWorkflow(getApiSubscriptionDAO(), getWorkflowDAO(), getApiGateway());
workflow.setCreatedTime(LocalDateTime.now());
workflow.setExternalWorkflowReference(UUID.randomUUID().toString());
workflow.setWorkflowReference(subscriptionId);
workflow.setWorkflowType(WorkflowConstants.WF_TYPE_AM_SUBSCRIPTION_CREATION);
workflow.setSubscription(subscription);
workflow.setSubscriber(getUsername());
String workflowDescription = "API [ " + subscription.getApi().getName() + " - " + subscription.getApi().getVersion() + " ] subscription creation request from subscriber - " + getUsername() + " for the application - " + subscription.getApplication().getName() + "";
workflow.setWorkflowDescription(workflowDescription);
WorkflowResponse response = addSubscriptionWFExecutor.execute(workflow);
workflow.setStatus(response.getWorkflowStatus());
if (WorkflowStatus.CREATED != response.getWorkflowStatus()) {
completeWorkflow(addSubscriptionWFExecutor, workflow);
} else {
// only add entry to workflow table if it is a pending task
addWorkflowEntries(workflow);
}
subScriptionResponse = new SubscriptionResponse(subscriptionId, response);
} catch (APIMgtDAOException e) {
String errorMsg = "Error occurred while adding api subscription for api - " + apiId;
log.error(errorMsg, e);
throw new APIManagementException(errorMsg, e, e.getErrorHandler());
}
return subScriptionResponse;
}
use of org.wso2.carbon.apimgt.core.workflow.Workflow 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;
}
use of org.wso2.carbon.apimgt.core.workflow.Workflow in project carbon-apimgt by wso2.
the class APIStoreImpl method updateApplication.
@Override
public WorkflowResponse updateApplication(String uuid, Application application) throws APIManagementException {
try {
// get old app
Application existingApplication = getApplicationDAO().getApplication(uuid);
if (existingApplication != null) {
WorkflowExecutor executor = WorkflowExecutorFactory.getInstance().getWorkflowExecutor(WorkflowConstants.WF_TYPE_AM_APPLICATION_UPDATE);
ApplicationUpdateWorkflow workflow = new ApplicationUpdateWorkflow(getApplicationDAO(), getWorkflowDAO(), getApiGateway());
application.setId(uuid);
application.setUpdatedUser(getUsername());
application.setUpdatedTime(LocalDateTime.now());
Policy appTier = application.getPolicy();
if (appTier != null && !appTier.getPolicyName().equals(existingApplication.getPolicy().getPolicyName())) {
Policy policy = getPolicyDAO().getSimplifiedPolicyByLevelAndName(APIMgtAdminService.PolicyLevel.application, appTier.getPolicyName());
if (policy == null) {
String message = "Specified tier " + appTier + " is invalid";
log.error(message);
throw new APIManagementException(message, ExceptionCodes.TIER_NAME_INVALID);
}
application.setPolicy(policy);
}
workflow.setExistingApplication(existingApplication);
workflow.setUpdatedApplication(application);
workflow.setCreatedBy(getUsername());
workflow.setWorkflowReference(application.getId());
workflow.setExternalWorkflowReference(UUID.randomUUID().toString());
workflow.setCreatedTime(LocalDateTime.now());
String workflowDescription = "Update application " + existingApplication.getName() + " with tier " + existingApplication.getPolicy().getPolicyName() + " and description \'" + existingApplication.getDescription() + "\' To " + application.getName() + " with tier " + application.getPolicy().getPolicyName() + " and description \'" + application.getDescription() + "\' by " + getUsername();
workflow.setWorkflowDescription(workflowDescription);
// setting attributes for internal use. These are set to use from outside the executor's method
// these will be saved in the AM_WORKFLOW table so these can be retrieved later for external wf approval
// scenarios. this won't get stored for simple wfs
workflow.setAttribute(WorkflowConstants.ATTRIBUTE_APPLICATION_NAME, application.getName());
workflow.setAttribute(WorkflowConstants.ATTRIBUTE_APPLICATION_UPDATEDBY, application.getUpdatedUser());
workflow.setAttribute(WorkflowConstants.ATTRIBUTE_APPLICATION_TIER, application.getPolicy().getPolicyName());
workflow.setAttribute(WorkflowConstants.ATTRIBUTE_APPLICATION_POLICY_ID, application.getPolicy().getUuid());
workflow.setAttribute(WorkflowConstants.ATTRIBUTE_APPLICATION_DESCRIPTION, application.getDescription());
workflow.setAttribute(WorkflowConstants.ATTRIBUTE_APPLICATION_PERMISSION, application.getPermissionString());
workflow.setAttribute(WorkflowConstants.ATTRIBUTE_APPLICATION_EXISTIN_APP_STATUS, existingApplication.getStatus());
WorkflowResponse response = executor.execute(workflow);
workflow.setStatus(response.getWorkflowStatus());
if (WorkflowStatus.CREATED != response.getWorkflowStatus()) {
completeWorkflow(executor, workflow);
} else {
getApplicationDAO().updateApplicationState(uuid, ApplicationStatus.APPLICATION_ONHOLD);
addWorkflowEntries(workflow);
}
return response;
} else {
String errorMsg = "Applicaiton does not exist - " + uuid;
log.error(errorMsg);
throw new APIManagementException(errorMsg, ExceptionCodes.APPLICATION_NOT_FOUND);
}
} catch (APIMgtDAOException e) {
String errorMsg = "Error occurred while updating the application - " + uuid;
log.error(errorMsg, e);
throw new APIManagementException(errorMsg, e, e.getErrorHandler());
}
}
use of org.wso2.carbon.apimgt.core.workflow.Workflow in project carbon-apimgt by wso2.
the class WorkflowDAOIT method testUpdateWorkflowStatus.
@Test
public void testUpdateWorkflowStatus() throws Exception {
WorkflowDAO workflowDAO = DAOFactory.getWorkflowDAO();
String workflowRefId = UUID.randomUUID().toString();
Workflow workflow = SampleTestObjectCreator.createWorkflow(workflowRefId);
workflowDAO.addWorkflowEntry(workflow);
Workflow retrieveWorflow = workflowDAO.retrieveWorkflow(workflow.getExternalWorkflowReference());
Assert.assertEquals(retrieveWorflow.getStatus(), workflow.getStatus());
workflow.setStatus(WorkflowStatus.APPROVED);
workflow.setUpdatedTime(LocalDateTime.now());
workflowDAO.updateWorkflowStatus(workflow);
retrieveWorflow = workflowDAO.retrieveWorkflow(workflow.getExternalWorkflowReference());
Assert.assertEquals(retrieveWorflow.getStatus(), WorkflowStatus.APPROVED);
}
use of org.wso2.carbon.apimgt.core.workflow.Workflow in project carbon-apimgt by wso2.
the class WorkflowDAOIT method testGetWorkflowsList.
@Test
public void testGetWorkflowsList() throws Exception {
WorkflowDAO workflowDAO = DAOFactory.getWorkflowDAO();
// pending application 1
String workflowExtRefId1 = UUID.randomUUID().toString();
String applicationId1 = UUID.randomUUID().toString();
Workflow workflow1 = SampleTestObjectCreator.createWorkflow(workflowExtRefId1);
workflow1.setWorkflowType(WorkflowConstants.WF_TYPE_AM_APPLICATION_CREATION);
workflow1.setWorkflowReference(applicationId1);
workflow1.setStatus(WorkflowStatus.CREATED);
workflowDAO.addWorkflowEntry(workflow1);
// pending application 2
String workflowExtRefId2 = UUID.randomUUID().toString();
String applicationId2 = UUID.randomUUID().toString();
Workflow workflow2 = SampleTestObjectCreator.createWorkflow(workflowExtRefId2);
workflow2.setWorkflowType(WorkflowConstants.WF_TYPE_AM_APPLICATION_CREATION);
workflow2.setWorkflowReference(applicationId2);
workflow2.setStatus(WorkflowStatus.CREATED);
workflowDAO.addWorkflowEntry(workflow2);
// completed application 1
String workflowExtRefId3 = UUID.randomUUID().toString();
String applicationId3 = UUID.randomUUID().toString();
Workflow workflow3 = SampleTestObjectCreator.createWorkflow(workflowExtRefId3);
workflow3.setWorkflowType(WorkflowConstants.WF_TYPE_AM_APPLICATION_CREATION);
workflow3.setWorkflowReference(applicationId3);
workflow3.setStatus(WorkflowStatus.APPROVED);
workflowDAO.addWorkflowEntry(workflow3);
// pending subscription 1
String workflowExtRefId4 = UUID.randomUUID().toString();
String applicationId4 = UUID.randomUUID().toString();
Workflow workflow4 = SampleTestObjectCreator.createWorkflow(workflowExtRefId4);
workflow4.setWorkflowType(WorkflowConstants.WF_TYPE_AM_SUBSCRIPTION_CREATION);
workflow4.setWorkflowReference(applicationId4);
workflow4.setStatus(WorkflowStatus.CREATED);
workflowDAO.addWorkflowEntry(workflow4);
// completed subscription 1
String workflowExtRefId5 = UUID.randomUUID().toString();
String applicationId5 = UUID.randomUUID().toString();
Workflow workflow5 = SampleTestObjectCreator.createWorkflow(workflowExtRefId5);
workflow5.setWorkflowType(WorkflowConstants.WF_TYPE_AM_SUBSCRIPTION_CREATION);
workflow5.setWorkflowReference(applicationId5);
workflow5.setStatus(WorkflowStatus.APPROVED);
workflowDAO.addWorkflowEntry(workflow5);
List<Workflow> list = workflowDAO.retrieveUncompleteWorkflows(WorkflowConstants.WF_TYPE_AM_APPLICATION_CREATION);
for (Iterator iterator = list.iterator(); iterator.hasNext(); ) {
Workflow workflow = (Workflow) iterator.next();
// check whether there are any completed workflows
Assert.assertFalse(WorkflowStatus.APPROVED == workflow.getStatus(), "Retrieved list contains approved workflows");
// check whether it has subscription workflow entry
Assert.assertFalse(WorkflowConstants.WF_TYPE_AM_SUBSCRIPTION_CREATION.equals(workflow.getWorkflowType()), "Application workflow list contains subscription workflow entry");
}
// get all the pending workflows
List<Workflow> fullList = workflowDAO.retrieveUncompleteWorkflows();
Assert.assertEquals(fullList.size(), 3, "Retrived workflow list does not contain valid entires");
}
Aggregations